hwmon: (coretemp) Fix Atom CPUs support
[linux-2.6-block.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>
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
6369a288
RM
41typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL,
42 SHOW_NAME } SHOW;
bebe4678
RM
43
44/*
45 * Functions declaration
46 */
47
48static struct coretemp_data *coretemp_update_device(struct device *dev);
49
50struct coretemp_data {
1beeffe4 51 struct device *hwmon_dev;
bebe4678
RM
52 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;
6369a288 59 int ttarget;
bebe4678
RM
60 u8 alarm;
61};
62
bebe4678
RM
63/*
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;
6369a288 98 else if (attr->index == SHOW_TJMAX)
bebe4678 99 err = sprintf(buf, "%d\n", data->tjmax);
6369a288
RM
100 else
101 err = sprintf(buf, "%d\n", data->ttarget);
bebe4678
RM
102 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);
6369a288
RM
109static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL,
110 SHOW_TTARGET);
bebe4678
RM
111static 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
118a8871
RM
155static 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;
708a62bc 160 int usemsr_ee = 1;
118a8871
RM
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)) {
708a62bc 167 usemsr_ee = 0;
118a8871
RM
168 }
169
708a62bc
RM
170 /* Atoms seems to have TjMax at 90C */
171
172 if (c->x86_model == 0x1c) {
173 usemsr_ee = 0;
174 tjmax = 90000;
175 }
176
177 if ((c->x86_model > 0xe) && (usemsr_ee)) {
118a8871
RM
178
179 /* Now we can detect the mobile CPU using Intel provided table
180 http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
181 For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
182 */
183
184 err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
185 if (err) {
186 dev_warn(dev,
187 "Unable to access MSR 0x17, assuming desktop"
188 " CPU\n");
708a62bc 189 usemsr_ee = 0;
118a8871 190 } else if (!(eax & 0x10000000)) {
708a62bc 191 usemsr_ee = 0;
118a8871
RM
192 }
193 }
194
708a62bc 195 if (usemsr_ee) {
118a8871
RM
196
197 err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
198 if (err) {
199 dev_warn(dev,
200 "Unable to access MSR 0xEE, for Tjmax, left"
201 " at default");
202 } else if (eax & 0x40000000) {
203 tjmax = 85000;
204 }
708a62bc
RM
205 /* if we dont use msr EE it means we are desktop CPU (with exeception
206 of Atom) */
207 } else if (tjmax == 100000) {
118a8871
RM
208 dev_warn(dev, "Using relative temperature scale!\n");
209 }
210
211 return tjmax;
212}
213
bebe4678
RM
214static int __devinit coretemp_probe(struct platform_device *pdev)
215{
216 struct coretemp_data *data;
92cb7612 217 struct cpuinfo_x86 *c = &cpu_data(pdev->id);
bebe4678
RM
218 int err;
219 u32 eax, edx;
220
221 if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
222 err = -ENOMEM;
223 dev_err(&pdev->dev, "Out of memory\n");
224 goto exit;
225 }
226
227 data->id = pdev->id;
228 data->name = "coretemp";
229 mutex_init(&data->update_lock);
bebe4678
RM
230
231 /* test if we can access the THERM_STATUS MSR */
232 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
233 if (err) {
234 dev_err(&pdev->dev,
235 "Unable to access THERM_STATUS MSR, giving up\n");
236 goto exit_free;
237 }
238
67f363b1
RM
239 /* Check if we have problem with errata AE18 of Core processors:
240 Readings might stop update when processor visited too deep sleep,
241 fixed for stepping D0 (6EC).
242 */
243
244 if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
245 /* check for microcode update */
246 rdmsr_on_cpu(data->id, MSR_IA32_UCODE_REV, &eax, &edx);
247 if (edx < 0x39) {
6d79af70 248 err = -ENODEV;
67f363b1
RM
249 dev_err(&pdev->dev,
250 "Errata AE18 not fixed, update BIOS or "
251 "microcode of the CPU!\n");
252 goto exit_free;
253 }
254 }
255
118a8871 256 data->tjmax = adjust_tjmax(c, data->id, &pdev->dev);
bebe4678
RM
257 platform_set_drvdata(pdev, data);
258
6369a288 259 /* read the still undocumented IA32_TEMPERATURE_TARGET it exists
708a62bc 260 on older CPUs but not in this register, Atoms don't have it either */
6369a288 261
708a62bc 262 if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) {
6369a288
RM
263 err = rdmsr_safe_on_cpu(data->id, 0x1a2, &eax, &edx);
264 if (err) {
265 dev_warn(&pdev->dev, "Unable to read"
266 " IA32_TEMPERATURE_TARGET MSR\n");
267 } else {
268 data->ttarget = data->tjmax -
269 (((eax >> 8) & 0xff) * 1000);
270 err = device_create_file(&pdev->dev,
271 &sensor_dev_attr_temp1_max.dev_attr);
272 if (err)
273 goto exit_free;
274 }
275 }
276
bebe4678 277 if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
6369a288 278 goto exit_dev;
bebe4678 279
1beeffe4
TJ
280 data->hwmon_dev = hwmon_device_register(&pdev->dev);
281 if (IS_ERR(data->hwmon_dev)) {
282 err = PTR_ERR(data->hwmon_dev);
bebe4678
RM
283 dev_err(&pdev->dev, "Class registration failed (%d)\n",
284 err);
285 goto exit_class;
286 }
287
288 return 0;
289
290exit_class:
291 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
6369a288
RM
292exit_dev:
293 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
bebe4678
RM
294exit_free:
295 kfree(data);
296exit:
297 return err;
298}
299
300static int __devexit coretemp_remove(struct platform_device *pdev)
301{
302 struct coretemp_data *data = platform_get_drvdata(pdev);
303
1beeffe4 304 hwmon_device_unregister(data->hwmon_dev);
bebe4678 305 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
6369a288 306 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
bebe4678
RM
307 platform_set_drvdata(pdev, NULL);
308 kfree(data);
309 return 0;
310}
311
312static struct platform_driver coretemp_driver = {
313 .driver = {
314 .owner = THIS_MODULE,
315 .name = DRVNAME,
316 },
317 .probe = coretemp_probe,
318 .remove = __devexit_p(coretemp_remove),
319};
320
321struct pdev_entry {
322 struct list_head list;
323 struct platform_device *pdev;
324 unsigned int cpu;
325};
326
327static LIST_HEAD(pdev_list);
328static DEFINE_MUTEX(pdev_list_mutex);
329
330static int __cpuinit coretemp_device_add(unsigned int cpu)
331{
332 int err;
333 struct platform_device *pdev;
334 struct pdev_entry *pdev_entry;
335
336 pdev = platform_device_alloc(DRVNAME, cpu);
337 if (!pdev) {
338 err = -ENOMEM;
339 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
340 goto exit;
341 }
342
343 pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
344 if (!pdev_entry) {
345 err = -ENOMEM;
346 goto exit_device_put;
347 }
348
349 err = platform_device_add(pdev);
350 if (err) {
351 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
352 err);
353 goto exit_device_free;
354 }
355
356 pdev_entry->pdev = pdev;
357 pdev_entry->cpu = cpu;
358 mutex_lock(&pdev_list_mutex);
359 list_add_tail(&pdev_entry->list, &pdev_list);
360 mutex_unlock(&pdev_list_mutex);
361
362 return 0;
363
364exit_device_free:
365 kfree(pdev_entry);
366exit_device_put:
367 platform_device_put(pdev);
368exit:
369 return err;
370}
371
372#ifdef CONFIG_HOTPLUG_CPU
d2bc7b13 373static void coretemp_device_remove(unsigned int cpu)
bebe4678
RM
374{
375 struct pdev_entry *p, *n;
376 mutex_lock(&pdev_list_mutex);
377 list_for_each_entry_safe(p, n, &pdev_list, list) {
378 if (p->cpu == cpu) {
379 platform_device_unregister(p->pdev);
380 list_del(&p->list);
381 kfree(p);
382 }
383 }
384 mutex_unlock(&pdev_list_mutex);
385}
386
ba7c1927 387static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
bebe4678
RM
388 unsigned long action, void *hcpu)
389{
390 unsigned int cpu = (unsigned long) hcpu;
391
392 switch (action) {
393 case CPU_ONLINE:
561d9a96 394 case CPU_DOWN_FAILED:
bebe4678
RM
395 coretemp_device_add(cpu);
396 break;
561d9a96 397 case CPU_DOWN_PREPARE:
bebe4678
RM
398 coretemp_device_remove(cpu);
399 break;
400 }
401 return NOTIFY_OK;
402}
403
ba7c1927 404static struct notifier_block coretemp_cpu_notifier __refdata = {
bebe4678
RM
405 .notifier_call = coretemp_cpu_callback,
406};
407#endif /* !CONFIG_HOTPLUG_CPU */
408
409static int __init coretemp_init(void)
410{
411 int i, err = -ENODEV;
412 struct pdev_entry *p, *n;
413
bebe4678 414 /* quick check if we run Intel */
92cb7612 415 if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
bebe4678
RM
416 goto exit;
417
418 err = platform_driver_register(&coretemp_driver);
419 if (err)
420 goto exit;
421
422 for_each_online_cpu(i) {
92cb7612 423 struct cpuinfo_x86 *c = &cpu_data(i);
bebe4678 424
708a62bc 425 /* check if family 6, models 0xe, 0xf, 0x16, 0x17, 0x1A, 0x1c */
bebe4678 426 if ((c->cpuid_level < 0) || (c->x86 != 0x6) ||
c940336b 427 !((c->x86_model == 0xe) || (c->x86_model == 0xf) ||
34c86c1e 428 (c->x86_model == 0x16) || (c->x86_model == 0x17) ||
0bf41d9f 429 (c->x86_model == 0x1A) || (c->x86_model == 0x1c))) {
bebe4678
RM
430
431 /* supported CPU not found, but report the unknown
432 family 6 CPU */
433 if ((c->x86 == 0x6) && (c->x86_model > 0xf))
434 printk(KERN_WARNING DRVNAME ": Unknown CPU "
435 "model %x\n", c->x86_model);
436 continue;
437 }
438
439 err = coretemp_device_add(i);
440 if (err)
441 goto exit_devices_unreg;
442 }
443 if (list_empty(&pdev_list)) {
444 err = -ENODEV;
445 goto exit_driver_unreg;
446 }
447
448#ifdef CONFIG_HOTPLUG_CPU
449 register_hotcpu_notifier(&coretemp_cpu_notifier);
450#endif
451 return 0;
452
453exit_devices_unreg:
454 mutex_lock(&pdev_list_mutex);
455 list_for_each_entry_safe(p, n, &pdev_list, list) {
456 platform_device_unregister(p->pdev);
457 list_del(&p->list);
458 kfree(p);
459 }
460 mutex_unlock(&pdev_list_mutex);
461exit_driver_unreg:
462 platform_driver_unregister(&coretemp_driver);
463exit:
464 return err;
465}
466
467static void __exit coretemp_exit(void)
468{
469 struct pdev_entry *p, *n;
470#ifdef CONFIG_HOTPLUG_CPU
471 unregister_hotcpu_notifier(&coretemp_cpu_notifier);
472#endif
473 mutex_lock(&pdev_list_mutex);
474 list_for_each_entry_safe(p, n, &pdev_list, list) {
475 platform_device_unregister(p->pdev);
476 list_del(&p->list);
477 kfree(p);
478 }
479 mutex_unlock(&pdev_list_mutex);
480 platform_driver_unregister(&coretemp_driver);
481}
482
483MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
484MODULE_DESCRIPTION("Intel Core temperature monitor");
485MODULE_LICENSE("GPL");
486
487module_init(coretemp_init)
488module_exit(coretemp_exit)