]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/hwmon/coretemp.c
hwmon: (w83795) Fix LSB reading of voltage limits
[net-next-2.6.git] / drivers / hwmon / coretemp.c
CommitLineData
bebe4678
RM
1/*
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>
bebe4678
RM
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/jiffies.h>
27#include <linux/hwmon.h>
28#include <linux/sysfs.h>
29#include <linux/hwmon-sysfs.h>
30#include <linux/err.h>
31#include <linux/mutex.h>
32#include <linux/list.h>
33#include <linux/platform_device.h>
34#include <linux/cpu.h>
1fe63ab4 35#include <linux/pci.h>
bebe4678
RM
36#include <asm/msr.h>
37#include <asm/processor.h>
fff20173 38#include <asm/smp.h>
bebe4678
RM
39
40#define DRVNAME "coretemp"
41
6369a288
RM
42typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL,
43 SHOW_NAME } SHOW;
bebe4678
RM
44
45/*
46 * Functions declaration
47 */
48
49static struct coretemp_data *coretemp_update_device(struct device *dev);
50
51struct coretemp_data {
1beeffe4 52 struct device *hwmon_dev;
bebe4678
RM
53 struct mutex update_lock;
54 const char *name;
55 u32 id;
3f4f09b4 56 u16 core_id;
bebe4678
RM
57 char valid; /* zero until following fields are valid */
58 unsigned long last_updated; /* in jiffies */
59 int temp;
60 int tjmax;
6369a288 61 int ttarget;
bebe4678
RM
62 u8 alarm;
63};
64
bebe4678
RM
65/*
66 * Sysfs stuff
67 */
68
69static ssize_t show_name(struct device *dev, struct device_attribute
70 *devattr, char *buf)
71{
72 int ret;
73 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
74 struct coretemp_data *data = dev_get_drvdata(dev);
75
76 if (attr->index == SHOW_NAME)
77 ret = sprintf(buf, "%s\n", data->name);
78 else /* show label */
3f4f09b4 79 ret = sprintf(buf, "Core %d\n", data->core_id);
bebe4678
RM
80 return ret;
81}
82
83static ssize_t show_alarm(struct device *dev, struct device_attribute
84 *devattr, char *buf)
85{
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);
89}
90
91static ssize_t show_temp(struct device *dev,
92 struct device_attribute *devattr, char *buf)
93{
94 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
95 struct coretemp_data *data = coretemp_update_device(dev);
96 int err;
97
98 if (attr->index == SHOW_TEMP)
99 err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN;
6369a288 100 else if (attr->index == SHOW_TJMAX)
bebe4678 101 err = sprintf(buf, "%d\n", data->tjmax);
6369a288
RM
102 else
103 err = sprintf(buf, "%d\n", data->ttarget);
bebe4678
RM
104 return err;
105}
106
107static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
108 SHOW_TEMP);
109static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
110 SHOW_TJMAX);
6369a288
RM
111static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL,
112 SHOW_TTARGET);
bebe4678
RM
113static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
114static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
115static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
116
117static 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
124};
125
126static const struct attribute_group coretemp_group = {
127 .attrs = coretemp_attributes,
128};
129
130static struct coretemp_data *coretemp_update_device(struct device *dev)
131{
132 struct coretemp_data *data = dev_get_drvdata(dev);
133
134 mutex_lock(&data->update_lock);
135
136 if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
137 u32 eax, edx;
138
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);
149 }
150 data->last_updated = jiffies;
151 }
152
153 mutex_unlock(&data->update_lock);
154 return data;
155}
156
118a8871
RM
157static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
158{
159 /* The 100C is default for both mobile and non mobile CPUs */
160
161 int tjmax = 100000;
eccfed42 162 int tjmax_ee = 85000;
708a62bc 163 int usemsr_ee = 1;
118a8871
RM
164 int err;
165 u32 eax, edx;
1fe63ab4 166 struct pci_dev *host_bridge;
118a8871
RM
167
168 /* Early chips have no MSR for TjMax */
169
170 if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
708a62bc 171 usemsr_ee = 0;
118a8871
RM
172 }
173
1fe63ab4 174 /* Atom CPUs */
708a62bc
RM
175
176 if (c->x86_model == 0x1c) {
177 usemsr_ee = 0;
1fe63ab4
YW
178
179 host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
180
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;
187
188 pci_dev_put(host_bridge);
708a62bc
RM
189 }
190
191 if ((c->x86_model > 0xe) && (usemsr_ee)) {
eccfed42 192 u8 platform_id;
118a8871
RM
193
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
197 */
198
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");
708a62bc 204 usemsr_ee = 0;
eccfed42
RM
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 */
708a62bc 209 usemsr_ee = 0;
eccfed42
RM
210 } else {
211 /* Platform ID bits 52:50 (EDX starts at bit 32) */
212 platform_id = (edx >> 18) & 0x7;
213
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;
222 }
118a8871
RM
223 }
224 }
225
708a62bc 226 if (usemsr_ee) {
118a8871
RM
227
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"
4d7a5644 232 " at default\n");
118a8871 233 } else if (eax & 0x40000000) {
eccfed42 234 tjmax = tjmax_ee;
118a8871 235 }
708a62bc
RM
236 /* if we dont use msr EE it means we are desktop CPU (with exeception
237 of Atom) */
238 } else if (tjmax == 100000) {
118a8871
RM
239 dev_warn(dev, "Using relative temperature scale!\n");
240 }
241
242 return tjmax;
243}
244
a321cedb
CE
245static int __devinit get_tjmax(struct cpuinfo_x86 *c, u32 id,
246 struct device *dev)
247{
248 /* The 100C is default for both mobile and non mobile CPUs */
249 int err;
250 u32 eax, edx;
251 u32 val;
252
253 /* A new feature of current Intel(R) processors, the
254 IA32_TEMPERATURE_TARGET contains the TjMax value */
255 err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
256 if (err) {
257 dev_warn(dev, "Unable to read TjMax from CPU.\n");
258 } else {
259 val = (eax >> 16) & 0xff;
260 /*
261 * If the TjMax is not plausible, an assumption
262 * will be used
263 */
264 if ((val > 80) && (val < 120)) {
265 dev_info(dev, "TjMax is %d C.\n", val);
266 return val * 1000;
267 }
268 }
269
270 /*
271 * An assumption is made for early CPUs and unreadable MSR.
272 * NOTE: the given value may not be correct.
273 */
274
275 switch (c->x86_model) {
276 case 0xe:
277 case 0xf:
278 case 0x16:
279 case 0x1a:
280 dev_warn(dev, "TjMax is assumed as 100 C!\n");
281 return 100000;
a321cedb
CE
282 case 0x17:
283 case 0x1c: /* Atom CPUs */
284 return adjust_tjmax(c, id, dev);
a321cedb
CE
285 default:
286 dev_warn(dev, "CPU (model=0x%x) is not supported yet,"
287 " using default TjMax of 100C.\n", c->x86_model);
288 return 100000;
289 }
290}
291
32478006
JB
292static void __devinit get_ucode_rev_on_cpu(void *edx)
293{
294 u32 eax;
295
296 wrmsr(MSR_IA32_UCODE_REV, 0, 0);
297 sync_core();
298 rdmsr(MSR_IA32_UCODE_REV, eax, *(u32 *)edx);
299}
300
bebe4678
RM
301static int __devinit coretemp_probe(struct platform_device *pdev)
302{
303 struct coretemp_data *data;
92cb7612 304 struct cpuinfo_x86 *c = &cpu_data(pdev->id);
bebe4678
RM
305 int err;
306 u32 eax, edx;
307
308 if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
309 err = -ENOMEM;
310 dev_err(&pdev->dev, "Out of memory\n");
311 goto exit;
312 }
313
314 data->id = pdev->id;
3f4f09b4
JD
315#ifdef CONFIG_SMP
316 data->core_id = c->cpu_core_id;
317#endif
bebe4678
RM
318 data->name = "coretemp";
319 mutex_init(&data->update_lock);
bebe4678
RM
320
321 /* test if we can access the THERM_STATUS MSR */
322 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
323 if (err) {
324 dev_err(&pdev->dev,
325 "Unable to access THERM_STATUS MSR, giving up\n");
326 goto exit_free;
327 }
328
67f363b1
RM
329 /* Check if we have problem with errata AE18 of Core processors:
330 Readings might stop update when processor visited too deep sleep,
331 fixed for stepping D0 (6EC).
332 */
333
334 if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
335 /* check for microcode update */
32478006
JB
336 err = smp_call_function_single(data->id, get_ucode_rev_on_cpu,
337 &edx, 1);
338 if (err) {
339 dev_err(&pdev->dev,
340 "Cannot determine microcode revision of "
341 "CPU#%u (%d)!\n", data->id, err);
342 err = -ENODEV;
343 goto exit_free;
344 } else if (edx < 0x39) {
6d79af70 345 err = -ENODEV;
67f363b1
RM
346 dev_err(&pdev->dev,
347 "Errata AE18 not fixed, update BIOS or "
348 "microcode of the CPU!\n");
349 goto exit_free;
350 }
351 }
352
a321cedb 353 data->tjmax = get_tjmax(c, data->id, &pdev->dev);
bebe4678
RM
354 platform_set_drvdata(pdev, data);
355
a321cedb
CE
356 /*
357 * read the still undocumented IA32_TEMPERATURE_TARGET. It exists
358 * on older CPUs but not in this register,
359 * Atoms don't have it either.
360 */
6369a288 361
708a62bc 362 if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) {
a321cedb
CE
363 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET,
364 &eax, &edx);
6369a288
RM
365 if (err) {
366 dev_warn(&pdev->dev, "Unable to read"
367 " IA32_TEMPERATURE_TARGET MSR\n");
368 } else {
369 data->ttarget = data->tjmax -
370 (((eax >> 8) & 0xff) * 1000);
371 err = device_create_file(&pdev->dev,
372 &sensor_dev_attr_temp1_max.dev_attr);
373 if (err)
374 goto exit_free;
375 }
376 }
377
bebe4678 378 if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
6369a288 379 goto exit_dev;
bebe4678 380
1beeffe4
TJ
381 data->hwmon_dev = hwmon_device_register(&pdev->dev);
382 if (IS_ERR(data->hwmon_dev)) {
383 err = PTR_ERR(data->hwmon_dev);
bebe4678
RM
384 dev_err(&pdev->dev, "Class registration failed (%d)\n",
385 err);
386 goto exit_class;
387 }
388
389 return 0;
390
391exit_class:
392 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
6369a288
RM
393exit_dev:
394 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
bebe4678
RM
395exit_free:
396 kfree(data);
397exit:
398 return err;
399}
400
401static int __devexit coretemp_remove(struct platform_device *pdev)
402{
403 struct coretemp_data *data = platform_get_drvdata(pdev);
404
1beeffe4 405 hwmon_device_unregister(data->hwmon_dev);
bebe4678 406 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
6369a288 407 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
bebe4678
RM
408 platform_set_drvdata(pdev, NULL);
409 kfree(data);
410 return 0;
411}
412
413static struct platform_driver coretemp_driver = {
414 .driver = {
415 .owner = THIS_MODULE,
416 .name = DRVNAME,
417 },
418 .probe = coretemp_probe,
419 .remove = __devexit_p(coretemp_remove),
420};
421
422struct pdev_entry {
423 struct list_head list;
424 struct platform_device *pdev;
425 unsigned int cpu;
d883b9f0
JD
426#ifdef CONFIG_SMP
427 u16 phys_proc_id;
428 u16 cpu_core_id;
429#endif
bebe4678
RM
430};
431
432static LIST_HEAD(pdev_list);
433static DEFINE_MUTEX(pdev_list_mutex);
434
435static int __cpuinit coretemp_device_add(unsigned int cpu)
436{
437 int err;
438 struct platform_device *pdev;
439 struct pdev_entry *pdev_entry;
d883b9f0 440 struct cpuinfo_x86 *c = &cpu_data(cpu);
a4659053
JB
441
442 /*
443 * CPUID.06H.EAX[0] indicates whether the CPU has thermal
444 * sensors. We check this bit only, all the early CPUs
445 * without thermal sensors will be filtered out.
446 */
447 if (!cpu_has(c, X86_FEATURE_DTS)) {
448 printk(KERN_INFO DRVNAME ": CPU (model=0x%x)"
449 " has no thermal sensor.\n", c->x86_model);
450 return 0;
451 }
d883b9f0
JD
452
453 mutex_lock(&pdev_list_mutex);
454
455#ifdef CONFIG_SMP
456 /* Skip second HT entry of each core */
457 list_for_each_entry(pdev_entry, &pdev_list, list) {
458 if (c->phys_proc_id == pdev_entry->phys_proc_id &&
459 c->cpu_core_id == pdev_entry->cpu_core_id) {
460 err = 0; /* Not an error */
461 goto exit;
462 }
463 }
464#endif
bebe4678
RM
465
466 pdev = platform_device_alloc(DRVNAME, cpu);
467 if (!pdev) {
468 err = -ENOMEM;
469 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
470 goto exit;
471 }
472
473 pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
474 if (!pdev_entry) {
475 err = -ENOMEM;
476 goto exit_device_put;
477 }
478
479 err = platform_device_add(pdev);
480 if (err) {
481 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
482 err);
483 goto exit_device_free;
484 }
485
486 pdev_entry->pdev = pdev;
487 pdev_entry->cpu = cpu;
d883b9f0
JD
488#ifdef CONFIG_SMP
489 pdev_entry->phys_proc_id = c->phys_proc_id;
490 pdev_entry->cpu_core_id = c->cpu_core_id;
491#endif
bebe4678
RM
492 list_add_tail(&pdev_entry->list, &pdev_list);
493 mutex_unlock(&pdev_list_mutex);
494
495 return 0;
496
497exit_device_free:
498 kfree(pdev_entry);
499exit_device_put:
500 platform_device_put(pdev);
501exit:
d883b9f0 502 mutex_unlock(&pdev_list_mutex);
bebe4678
RM
503 return err;
504}
505
a5f42a6b 506static void __cpuinit coretemp_device_remove(unsigned int cpu)
bebe4678 507{
e40cc4bd
JB
508 struct pdev_entry *p;
509 unsigned int i;
510
bebe4678 511 mutex_lock(&pdev_list_mutex);
e40cc4bd
JB
512 list_for_each_entry(p, &pdev_list, list) {
513 if (p->cpu != cpu)
514 continue;
515
516 platform_device_unregister(p->pdev);
517 list_del(&p->list);
518 mutex_unlock(&pdev_list_mutex);
519 kfree(p);
520 for_each_cpu(i, cpu_sibling_mask(cpu))
521 if (i != cpu && !coretemp_device_add(i))
522 break;
523 return;
bebe4678
RM
524 }
525 mutex_unlock(&pdev_list_mutex);
526}
527
ba7c1927 528static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
bebe4678
RM
529 unsigned long action, void *hcpu)
530{
531 unsigned int cpu = (unsigned long) hcpu;
532
533 switch (action) {
534 case CPU_ONLINE:
561d9a96 535 case CPU_DOWN_FAILED:
bebe4678
RM
536 coretemp_device_add(cpu);
537 break;
561d9a96 538 case CPU_DOWN_PREPARE:
bebe4678
RM
539 coretemp_device_remove(cpu);
540 break;
541 }
542 return NOTIFY_OK;
543}
544
ba7c1927 545static struct notifier_block coretemp_cpu_notifier __refdata = {
bebe4678
RM
546 .notifier_call = coretemp_cpu_callback,
547};
bebe4678
RM
548
549static int __init coretemp_init(void)
550{
551 int i, err = -ENODEV;
bebe4678 552
bebe4678 553 /* quick check if we run Intel */
92cb7612 554 if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
bebe4678
RM
555 goto exit;
556
557 err = platform_driver_register(&coretemp_driver);
558 if (err)
559 goto exit;
560
a4659053
JB
561 for_each_online_cpu(i)
562 coretemp_device_add(i);
89a3fd35
JB
563
564#ifndef CONFIG_HOTPLUG_CPU
bebe4678
RM
565 if (list_empty(&pdev_list)) {
566 err = -ENODEV;
567 goto exit_driver_unreg;
568 }
89a3fd35 569#endif
bebe4678 570
bebe4678 571 register_hotcpu_notifier(&coretemp_cpu_notifier);
bebe4678
RM
572 return 0;
573
0dca94ba 574#ifndef CONFIG_HOTPLUG_CPU
89a3fd35 575exit_driver_unreg:
bebe4678 576 platform_driver_unregister(&coretemp_driver);
0dca94ba 577#endif
bebe4678
RM
578exit:
579 return err;
580}
581
582static void __exit coretemp_exit(void)
583{
584 struct pdev_entry *p, *n;
17c10d61 585
bebe4678 586 unregister_hotcpu_notifier(&coretemp_cpu_notifier);
bebe4678
RM
587 mutex_lock(&pdev_list_mutex);
588 list_for_each_entry_safe(p, n, &pdev_list, list) {
589 platform_device_unregister(p->pdev);
590 list_del(&p->list);
591 kfree(p);
592 }
593 mutex_unlock(&pdev_list_mutex);
594 platform_driver_unregister(&coretemp_driver);
595}
596
597MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
598MODULE_DESCRIPTION("Intel Core temperature monitor");
599MODULE_LICENSE("GPL");
600
601module_init(coretemp_init)
602module_exit(coretemp_exit)