]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/hwmon/coretemp.c
d73d0ba4785e3b103a7434b7ccd23b6db12d0fc7
[karo-tx-linux.git] / drivers / hwmon / coretemp.c
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
41 typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL,
42                 SHOW_NAME } SHOW;
43
44 /*
45  * Functions declaration
46  */
47
48 static struct coretemp_data *coretemp_update_device(struct device *dev);
49
50 struct coretemp_data {
51         struct device *hwmon_dev;
52         struct mutex update_lock;
53         const char *name;
54         u32 id;
55         u16 core_id;
56         char valid;             /* zero until following fields are valid */
57         unsigned long last_updated;     /* in jiffies */
58         int temp;
59         int tjmax;
60         int ttarget;
61         u8 alarm;
62 };
63
64 /*
65  * Sysfs stuff
66  */
67
68 static ssize_t show_name(struct device *dev, struct device_attribute
69                           *devattr, char *buf)
70 {
71         int ret;
72         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
73         struct coretemp_data *data = dev_get_drvdata(dev);
74
75         if (attr->index == SHOW_NAME)
76                 ret = sprintf(buf, "%s\n", data->name);
77         else    /* show label */
78                 ret = sprintf(buf, "Core %d\n", data->core_id);
79         return ret;
80 }
81
82 static ssize_t show_alarm(struct device *dev, struct device_attribute
83                           *devattr, char *buf)
84 {
85         struct coretemp_data *data = coretemp_update_device(dev);
86         /* read the Out-of-spec log, never clear */
87         return sprintf(buf, "%d\n", data->alarm);
88 }
89
90 static ssize_t show_temp(struct device *dev,
91                          struct device_attribute *devattr, char *buf)
92 {
93         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
94         struct coretemp_data *data = coretemp_update_device(dev);
95         int err;
96
97         if (attr->index == SHOW_TEMP)
98                 err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN;
99         else if (attr->index == SHOW_TJMAX)
100                 err = sprintf(buf, "%d\n", data->tjmax);
101         else
102                 err = sprintf(buf, "%d\n", data->ttarget);
103         return err;
104 }
105
106 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
107                           SHOW_TEMP);
108 static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
109                           SHOW_TJMAX);
110 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL,
111                           SHOW_TTARGET);
112 static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
113 static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
114 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
115
116 static struct attribute *coretemp_attributes[] = {
117         &sensor_dev_attr_name.dev_attr.attr,
118         &sensor_dev_attr_temp1_label.dev_attr.attr,
119         &dev_attr_temp1_crit_alarm.attr,
120         &sensor_dev_attr_temp1_input.dev_attr.attr,
121         &sensor_dev_attr_temp1_crit.dev_attr.attr,
122         NULL
123 };
124
125 static const struct attribute_group coretemp_group = {
126         .attrs = coretemp_attributes,
127 };
128
129 static struct coretemp_data *coretemp_update_device(struct device *dev)
130 {
131         struct coretemp_data *data = dev_get_drvdata(dev);
132
133         mutex_lock(&data->update_lock);
134
135         if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
136                 u32 eax, edx;
137
138                 data->valid = 0;
139                 rdmsr_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
140                 data->alarm = (eax >> 5) & 1;
141                 /* update only if data has been valid */
142                 if (eax & 0x80000000) {
143                         data->temp = data->tjmax - (((eax >> 16)
144                                                         & 0x7f) * 1000);
145                         data->valid = 1;
146                 } else {
147                         dev_dbg(dev, "Temperature data invalid (0x%x)\n", eax);
148                 }
149                 data->last_updated = jiffies;
150         }
151
152         mutex_unlock(&data->update_lock);
153         return data;
154 }
155
156 static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
157 {
158         /* The 100C is default for both mobile and non mobile CPUs */
159
160         int tjmax = 100000;
161         int ismobile = 1;
162         int err;
163         u32 eax, edx;
164
165         /* Early chips have no MSR for TjMax */
166
167         if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
168                 ismobile = 0;
169         }
170
171         if ((c->x86_model > 0xe) && (ismobile)) {
172
173                 /* Now we can detect the mobile CPU using Intel provided table
174                    http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
175                    For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
176                 */
177
178                 err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
179                 if (err) {
180                         dev_warn(dev,
181                                  "Unable to access MSR 0x17, assuming desktop"
182                                  " CPU\n");
183                         ismobile = 0;
184                 } else if (!(eax & 0x10000000)) {
185                         ismobile = 0;
186                 }
187         }
188
189         if (ismobile) {
190
191                 err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
192                 if (err) {
193                         dev_warn(dev,
194                                  "Unable to access MSR 0xEE, for Tjmax, left"
195                                  " at default\n");
196                 } else if (eax & 0x40000000) {
197                         tjmax = 85000;
198                 }
199         } else {
200                 dev_warn(dev, "Using relative temperature scale!\n");
201         }
202
203         return tjmax;
204 }
205
206 static int __devinit coretemp_probe(struct platform_device *pdev)
207 {
208         struct coretemp_data *data;
209         struct cpuinfo_x86 *c = &cpu_data(pdev->id);
210         int err;
211         u32 eax, edx;
212
213         if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
214                 err = -ENOMEM;
215                 dev_err(&pdev->dev, "Out of memory\n");
216                 goto exit;
217         }
218
219         data->id = pdev->id;
220 #ifdef CONFIG_SMP
221         data->core_id = c->cpu_core_id;
222 #endif
223         data->name = "coretemp";
224         mutex_init(&data->update_lock);
225
226         /* test if we can access the THERM_STATUS MSR */
227         err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
228         if (err) {
229                 dev_err(&pdev->dev,
230                         "Unable to access THERM_STATUS MSR, giving up\n");
231                 goto exit_free;
232         }
233
234         /* Check if we have problem with errata AE18 of Core processors:
235            Readings might stop update when processor visited too deep sleep,
236            fixed for stepping D0 (6EC).
237         */
238
239         if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
240                 /* check for microcode update */
241                 rdmsr_on_cpu(data->id, MSR_IA32_UCODE_REV, &eax, &edx);
242                 if (edx < 0x39) {
243                         err = -ENODEV;
244                         dev_err(&pdev->dev,
245                                 "Errata AE18 not fixed, update BIOS or "
246                                 "microcode of the CPU!\n");
247                         goto exit_free;
248                 }
249         }
250
251         data->tjmax = adjust_tjmax(c, data->id, &pdev->dev);
252         platform_set_drvdata(pdev, data);
253
254         /* read the still undocumented IA32_TEMPERATURE_TARGET it exists
255            on older CPUs but not in this register */
256
257         if (c->x86_model > 0xe) {
258                 err = rdmsr_safe_on_cpu(data->id, 0x1a2, &eax, &edx);
259                 if (err) {
260                         dev_warn(&pdev->dev, "Unable to read"
261                                         " IA32_TEMPERATURE_TARGET MSR\n");
262                 } else {
263                         data->ttarget = data->tjmax -
264                                         (((eax >> 8) & 0xff) * 1000);
265                         err = device_create_file(&pdev->dev,
266                                         &sensor_dev_attr_temp1_max.dev_attr);
267                         if (err)
268                                 goto exit_free;
269                 }
270         }
271
272         if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
273                 goto exit_dev;
274
275         data->hwmon_dev = hwmon_device_register(&pdev->dev);
276         if (IS_ERR(data->hwmon_dev)) {
277                 err = PTR_ERR(data->hwmon_dev);
278                 dev_err(&pdev->dev, "Class registration failed (%d)\n",
279                         err);
280                 goto exit_class;
281         }
282
283         return 0;
284
285 exit_class:
286         sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
287 exit_dev:
288         device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
289 exit_free:
290         kfree(data);
291 exit:
292         return err;
293 }
294
295 static int __devexit coretemp_remove(struct platform_device *pdev)
296 {
297         struct coretemp_data *data = platform_get_drvdata(pdev);
298
299         hwmon_device_unregister(data->hwmon_dev);
300         sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
301         device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
302         platform_set_drvdata(pdev, NULL);
303         kfree(data);
304         return 0;
305 }
306
307 static struct platform_driver coretemp_driver = {
308         .driver = {
309                 .owner = THIS_MODULE,
310                 .name = DRVNAME,
311         },
312         .probe = coretemp_probe,
313         .remove = __devexit_p(coretemp_remove),
314 };
315
316 struct pdev_entry {
317         struct list_head list;
318         struct platform_device *pdev;
319         unsigned int cpu;
320 };
321
322 static LIST_HEAD(pdev_list);
323 static DEFINE_MUTEX(pdev_list_mutex);
324
325 static int __cpuinit coretemp_device_add(unsigned int cpu)
326 {
327         int err;
328         struct platform_device *pdev;
329         struct pdev_entry *pdev_entry;
330
331         pdev = platform_device_alloc(DRVNAME, cpu);
332         if (!pdev) {
333                 err = -ENOMEM;
334                 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
335                 goto exit;
336         }
337
338         pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
339         if (!pdev_entry) {
340                 err = -ENOMEM;
341                 goto exit_device_put;
342         }
343
344         err = platform_device_add(pdev);
345         if (err) {
346                 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
347                        err);
348                 goto exit_device_free;
349         }
350
351         pdev_entry->pdev = pdev;
352         pdev_entry->cpu = cpu;
353         mutex_lock(&pdev_list_mutex);
354         list_add_tail(&pdev_entry->list, &pdev_list);
355         mutex_unlock(&pdev_list_mutex);
356
357         return 0;
358
359 exit_device_free:
360         kfree(pdev_entry);
361 exit_device_put:
362         platform_device_put(pdev);
363 exit:
364         return err;
365 }
366
367 #ifdef CONFIG_HOTPLUG_CPU
368 static void coretemp_device_remove(unsigned int cpu)
369 {
370         struct pdev_entry *p, *n;
371         mutex_lock(&pdev_list_mutex);
372         list_for_each_entry_safe(p, n, &pdev_list, list) {
373                 if (p->cpu == cpu) {
374                         platform_device_unregister(p->pdev);
375                         list_del(&p->list);
376                         kfree(p);
377                 }
378         }
379         mutex_unlock(&pdev_list_mutex);
380 }
381
382 static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
383                                  unsigned long action, void *hcpu)
384 {
385         unsigned int cpu = (unsigned long) hcpu;
386
387         switch (action) {
388         case CPU_ONLINE:
389         case CPU_DOWN_FAILED:
390                 coretemp_device_add(cpu);
391                 break;
392         case CPU_DOWN_PREPARE:
393                 coretemp_device_remove(cpu);
394                 break;
395         }
396         return NOTIFY_OK;
397 }
398
399 static struct notifier_block coretemp_cpu_notifier __refdata = {
400         .notifier_call = coretemp_cpu_callback,
401 };
402 #endif                          /* !CONFIG_HOTPLUG_CPU */
403
404 static int __init coretemp_init(void)
405 {
406         int i, err = -ENODEV;
407         struct pdev_entry *p, *n;
408
409         /* quick check if we run Intel */
410         if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
411                 goto exit;
412
413         err = platform_driver_register(&coretemp_driver);
414         if (err)
415                 goto exit;
416
417         for_each_online_cpu(i) {
418                 struct cpuinfo_x86 *c = &cpu_data(i);
419
420                 /* check if family 6, models 0xe, 0xf, 0x16, 0x17, 0x1A */
421                 if ((c->cpuid_level < 0) || (c->x86 != 0x6) ||
422                     !((c->x86_model == 0xe) || (c->x86_model == 0xf) ||
423                         (c->x86_model == 0x16) || (c->x86_model == 0x17) ||
424                         (c->x86_model == 0x1A))) {
425
426                         /* supported CPU not found, but report the unknown
427                            family 6 CPU */
428                         if ((c->x86 == 0x6) && (c->x86_model > 0xf))
429                                 printk(KERN_WARNING DRVNAME ": Unknown CPU "
430                                         "model %x\n", c->x86_model);
431                         continue;
432                 }
433
434                 err = coretemp_device_add(i);
435                 if (err)
436                         goto exit_devices_unreg;
437         }
438         if (list_empty(&pdev_list)) {
439                 err = -ENODEV;
440                 goto exit_driver_unreg;
441         }
442
443 #ifdef CONFIG_HOTPLUG_CPU
444         register_hotcpu_notifier(&coretemp_cpu_notifier);
445 #endif
446         return 0;
447
448 exit_devices_unreg:
449         mutex_lock(&pdev_list_mutex);
450         list_for_each_entry_safe(p, n, &pdev_list, list) {
451                 platform_device_unregister(p->pdev);
452                 list_del(&p->list);
453                 kfree(p);
454         }
455         mutex_unlock(&pdev_list_mutex);
456 exit_driver_unreg:
457         platform_driver_unregister(&coretemp_driver);
458 exit:
459         return err;
460 }
461
462 static void __exit coretemp_exit(void)
463 {
464         struct pdev_entry *p, *n;
465 #ifdef CONFIG_HOTPLUG_CPU
466         unregister_hotcpu_notifier(&coretemp_cpu_notifier);
467 #endif
468         mutex_lock(&pdev_list_mutex);
469         list_for_each_entry_safe(p, n, &pdev_list, list) {
470                 platform_device_unregister(p->pdev);
471                 list_del(&p->list);
472                 kfree(p);
473         }
474         mutex_unlock(&pdev_list_mutex);
475         platform_driver_unregister(&coretemp_driver);
476 }
477
478 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
479 MODULE_DESCRIPTION("Intel Core temperature monitor");
480 MODULE_LICENSE("GPL");
481
482 module_init(coretemp_init)
483 module_exit(coretemp_exit)