blob: 467488c278e9c2b4000ef8b0b160ac5fd10759b3 [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>
Rudolf Marekbebe4672007-05-08 17:22:02 +020024#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>
Yong Wang1fe63ab2010-01-10 20:52:34 +010035#include <linux/pci.h>
Rudolf Marekbebe4672007-05-08 17:22:02 +020036#include <asm/msr.h>
37#include <asm/processor.h>
Guenter Roeckfff20172010-09-27 18:01:49 -070038#include <asm/smp.h>
Rudolf Marekbebe4672007-05-08 17:22:02 +020039
40#define DRVNAME "coretemp"
41
Rudolf Marek6369a2882008-01-18 00:42:54 +010042typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL,
43 SHOW_NAME } SHOW;
Rudolf Marekbebe4672007-05-08 17:22:02 +020044
45/*
46 * Functions declaration
47 */
48
49static struct coretemp_data *coretemp_update_device(struct device *dev);
50
51struct coretemp_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070052 struct device *hwmon_dev;
Rudolf Marekbebe4672007-05-08 17:22:02 +020053 struct mutex update_lock;
54 const char *name;
55 u32 id;
Jean Delvare3f4f09b2010-07-09 16:22:51 +020056 u16 core_id;
Rudolf Marekbebe4672007-05-08 17:22:02 +020057 char valid; /* zero until following fields are valid */
58 unsigned long last_updated; /* in jiffies */
59 int temp;
60 int tjmax;
Rudolf Marek6369a2882008-01-18 00:42:54 +010061 int ttarget;
Rudolf Marekbebe4672007-05-08 17:22:02 +020062 u8 alarm;
63};
64
Rudolf Marekbebe4672007-05-08 17:22:02 +020065/*
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 */
Jean Delvare3f4f09b2010-07-09 16:22:51 +020079 ret = sprintf(buf, "Core %d\n", data->core_id);
Rudolf Marekbebe4672007-05-08 17:22:02 +020080 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;
Rudolf Marek6369a2882008-01-18 00:42:54 +0100100 else if (attr->index == SHOW_TJMAX)
Rudolf Marekbebe4672007-05-08 17:22:02 +0200101 err = sprintf(buf, "%d\n", data->tjmax);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100102 else
103 err = sprintf(buf, "%d\n", data->ttarget);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200104 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);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100111static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL,
112 SHOW_TTARGET);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200113static 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
Rudolf Marek118a8872008-02-17 22:59:39 +0100157static 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;
Rudolf Marekeccfed42009-09-23 22:59:42 +0200162 int tjmax_ee = 85000;
Rudolf Marek708a62b2009-09-23 22:59:42 +0200163 int usemsr_ee = 1;
Rudolf Marek118a8872008-02-17 22:59:39 +0100164 int err;
165 u32 eax, edx;
Yong Wang1fe63ab2010-01-10 20:52:34 +0100166 struct pci_dev *host_bridge;
Rudolf Marek118a8872008-02-17 22:59:39 +0100167
168 /* Early chips have no MSR for TjMax */
169
170 if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
Rudolf Marek708a62b2009-09-23 22:59:42 +0200171 usemsr_ee = 0;
Rudolf Marek118a8872008-02-17 22:59:39 +0100172 }
173
Yong Wang1fe63ab2010-01-10 20:52:34 +0100174 /* Atom CPUs */
Rudolf Marek708a62b2009-09-23 22:59:42 +0200175
176 if (c->x86_model == 0x1c) {
177 usemsr_ee = 0;
Yong Wang1fe63ab2010-01-10 20:52:34 +0100178
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);
Rudolf Marek708a62b2009-09-23 22:59:42 +0200189 }
190
191 if ((c->x86_model > 0xe) && (usemsr_ee)) {
Rudolf Marekeccfed42009-09-23 22:59:42 +0200192 u8 platform_id;
Rudolf Marek118a8872008-02-17 22:59:39 +0100193
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");
Rudolf Marek708a62b2009-09-23 22:59:42 +0200204 usemsr_ee = 0;
Rudolf Marekeccfed42009-09-23 22:59:42 +0200205 } 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 */
Rudolf Marek708a62b2009-09-23 22:59:42 +0200209 usemsr_ee = 0;
Rudolf Marekeccfed42009-09-23 22:59:42 +0200210 } 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 }
Rudolf Marek118a8872008-02-17 22:59:39 +0100223 }
224 }
225
Rudolf Marek708a62b2009-09-23 22:59:42 +0200226 if (usemsr_ee) {
Rudolf Marek118a8872008-02-17 22:59:39 +0100227
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"
Dean Nelson4d7a5642010-03-29 22:03:00 +0200232 " at default\n");
Rudolf Marek118a8872008-02-17 22:59:39 +0100233 } else if (eax & 0x40000000) {
Rudolf Marekeccfed42009-09-23 22:59:42 +0200234 tjmax = tjmax_ee;
Rudolf Marek118a8872008-02-17 22:59:39 +0100235 }
Rudolf Marek708a62b2009-09-23 22:59:42 +0200236 /* if we dont use msr EE it means we are desktop CPU (with exeception
237 of Atom) */
238 } else if (tjmax == 100000) {
Rudolf Marek118a8872008-02-17 22:59:39 +0100239 dev_warn(dev, "Using relative temperature scale!\n");
240 }
241
242 return tjmax;
243}
244
Carsten Emdea321ced2010-05-24 14:33:41 -0700245static 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;
Carsten Emdea321ced2010-05-24 14:33:41 -0700282 case 0x17:
283 case 0x1c: /* Atom CPUs */
284 return adjust_tjmax(c, id, dev);
Carsten Emdea321ced2010-05-24 14:33:41 -0700285 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
Rudolf Marekbebe4672007-05-08 17:22:02 +0200292static int __devinit coretemp_probe(struct platform_device *pdev)
293{
294 struct coretemp_data *data;
Mike Travis92cb7612007-10-19 20:35:04 +0200295 struct cpuinfo_x86 *c = &cpu_data(pdev->id);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200296 int err;
297 u32 eax, edx;
298
299 if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
300 err = -ENOMEM;
301 dev_err(&pdev->dev, "Out of memory\n");
302 goto exit;
303 }
304
305 data->id = pdev->id;
Jean Delvare3f4f09b2010-07-09 16:22:51 +0200306#ifdef CONFIG_SMP
307 data->core_id = c->cpu_core_id;
308#endif
Rudolf Marekbebe4672007-05-08 17:22:02 +0200309 data->name = "coretemp";
310 mutex_init(&data->update_lock);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200311
312 /* test if we can access the THERM_STATUS MSR */
313 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
314 if (err) {
315 dev_err(&pdev->dev,
316 "Unable to access THERM_STATUS MSR, giving up\n");
317 goto exit_free;
318 }
319
Rudolf Marek67f363b2007-05-27 22:17:43 +0200320 /* Check if we have problem with errata AE18 of Core processors:
321 Readings might stop update when processor visited too deep sleep,
322 fixed for stepping D0 (6EC).
323 */
324
325 if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
326 /* check for microcode update */
327 rdmsr_on_cpu(data->id, MSR_IA32_UCODE_REV, &eax, &edx);
328 if (edx < 0x39) {
Jean Delvare6d79af72007-06-23 17:16:24 -0700329 err = -ENODEV;
Rudolf Marek67f363b2007-05-27 22:17:43 +0200330 dev_err(&pdev->dev,
331 "Errata AE18 not fixed, update BIOS or "
332 "microcode of the CPU!\n");
333 goto exit_free;
334 }
335 }
336
Carsten Emdea321ced2010-05-24 14:33:41 -0700337 data->tjmax = get_tjmax(c, data->id, &pdev->dev);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200338 platform_set_drvdata(pdev, data);
339
Carsten Emdea321ced2010-05-24 14:33:41 -0700340 /*
341 * read the still undocumented IA32_TEMPERATURE_TARGET. It exists
342 * on older CPUs but not in this register,
343 * Atoms don't have it either.
344 */
Rudolf Marek6369a2882008-01-18 00:42:54 +0100345
Rudolf Marek708a62b2009-09-23 22:59:42 +0200346 if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) {
Carsten Emdea321ced2010-05-24 14:33:41 -0700347 err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET,
348 &eax, &edx);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100349 if (err) {
350 dev_warn(&pdev->dev, "Unable to read"
351 " IA32_TEMPERATURE_TARGET MSR\n");
352 } else {
353 data->ttarget = data->tjmax -
354 (((eax >> 8) & 0xff) * 1000);
355 err = device_create_file(&pdev->dev,
356 &sensor_dev_attr_temp1_max.dev_attr);
357 if (err)
358 goto exit_free;
359 }
360 }
361
Rudolf Marekbebe4672007-05-08 17:22:02 +0200362 if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
Rudolf Marek6369a2882008-01-18 00:42:54 +0100363 goto exit_dev;
Rudolf Marekbebe4672007-05-08 17:22:02 +0200364
Tony Jones1beeffe2007-08-20 13:46:20 -0700365 data->hwmon_dev = hwmon_device_register(&pdev->dev);
366 if (IS_ERR(data->hwmon_dev)) {
367 err = PTR_ERR(data->hwmon_dev);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200368 dev_err(&pdev->dev, "Class registration failed (%d)\n",
369 err);
370 goto exit_class;
371 }
372
373 return 0;
374
375exit_class:
376 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100377exit_dev:
378 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200379exit_free:
380 kfree(data);
381exit:
382 return err;
383}
384
385static int __devexit coretemp_remove(struct platform_device *pdev)
386{
387 struct coretemp_data *data = platform_get_drvdata(pdev);
388
Tony Jones1beeffe2007-08-20 13:46:20 -0700389 hwmon_device_unregister(data->hwmon_dev);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200390 sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
Rudolf Marek6369a2882008-01-18 00:42:54 +0100391 device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200392 platform_set_drvdata(pdev, NULL);
393 kfree(data);
394 return 0;
395}
396
397static struct platform_driver coretemp_driver = {
398 .driver = {
399 .owner = THIS_MODULE,
400 .name = DRVNAME,
401 },
402 .probe = coretemp_probe,
403 .remove = __devexit_p(coretemp_remove),
404};
405
406struct pdev_entry {
407 struct list_head list;
408 struct platform_device *pdev;
409 unsigned int cpu;
Jean Delvared883b9f2010-07-09 16:22:49 +0200410#ifdef CONFIG_SMP
411 u16 phys_proc_id;
412 u16 cpu_core_id;
413#endif
Rudolf Marekbebe4672007-05-08 17:22:02 +0200414};
415
416static LIST_HEAD(pdev_list);
417static DEFINE_MUTEX(pdev_list_mutex);
418
419static int __cpuinit coretemp_device_add(unsigned int cpu)
420{
421 int err;
422 struct platform_device *pdev;
423 struct pdev_entry *pdev_entry;
Jean Delvared883b9f2010-07-09 16:22:49 +0200424 struct cpuinfo_x86 *c = &cpu_data(cpu);
Jan Beulicha4659052010-09-23 22:21:34 -0700425
426 /*
427 * CPUID.06H.EAX[0] indicates whether the CPU has thermal
428 * sensors. We check this bit only, all the early CPUs
429 * without thermal sensors will be filtered out.
430 */
431 if (!cpu_has(c, X86_FEATURE_DTS)) {
432 printk(KERN_INFO DRVNAME ": CPU (model=0x%x)"
433 " has no thermal sensor.\n", c->x86_model);
434 return 0;
435 }
Jean Delvared883b9f2010-07-09 16:22:49 +0200436
437 mutex_lock(&pdev_list_mutex);
438
439#ifdef CONFIG_SMP
440 /* Skip second HT entry of each core */
441 list_for_each_entry(pdev_entry, &pdev_list, list) {
442 if (c->phys_proc_id == pdev_entry->phys_proc_id &&
443 c->cpu_core_id == pdev_entry->cpu_core_id) {
444 err = 0; /* Not an error */
445 goto exit;
446 }
447 }
448#endif
Rudolf Marekbebe4672007-05-08 17:22:02 +0200449
450 pdev = platform_device_alloc(DRVNAME, cpu);
451 if (!pdev) {
452 err = -ENOMEM;
453 printk(KERN_ERR DRVNAME ": Device allocation failed\n");
454 goto exit;
455 }
456
457 pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
458 if (!pdev_entry) {
459 err = -ENOMEM;
460 goto exit_device_put;
461 }
462
463 err = platform_device_add(pdev);
464 if (err) {
465 printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
466 err);
467 goto exit_device_free;
468 }
469
470 pdev_entry->pdev = pdev;
471 pdev_entry->cpu = cpu;
Jean Delvared883b9f2010-07-09 16:22:49 +0200472#ifdef CONFIG_SMP
473 pdev_entry->phys_proc_id = c->phys_proc_id;
474 pdev_entry->cpu_core_id = c->cpu_core_id;
475#endif
Rudolf Marekbebe4672007-05-08 17:22:02 +0200476 list_add_tail(&pdev_entry->list, &pdev_list);
477 mutex_unlock(&pdev_list_mutex);
478
479 return 0;
480
481exit_device_free:
482 kfree(pdev_entry);
483exit_device_put:
484 platform_device_put(pdev);
485exit:
Jean Delvared883b9f2010-07-09 16:22:49 +0200486 mutex_unlock(&pdev_list_mutex);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200487 return err;
488}
489
Jan Beulicha5f42a62010-09-23 22:31:10 -0700490static void __cpuinit coretemp_device_remove(unsigned int cpu)
Rudolf Marekbebe4672007-05-08 17:22:02 +0200491{
Jan Beuliche40cc4b2010-09-13 10:23:05 +0000492 struct pdev_entry *p;
493 unsigned int i;
494
Rudolf Marekbebe4672007-05-08 17:22:02 +0200495 mutex_lock(&pdev_list_mutex);
Jan Beuliche40cc4b2010-09-13 10:23:05 +0000496 list_for_each_entry(p, &pdev_list, list) {
497 if (p->cpu != cpu)
498 continue;
499
500 platform_device_unregister(p->pdev);
501 list_del(&p->list);
502 mutex_unlock(&pdev_list_mutex);
503 kfree(p);
504 for_each_cpu(i, cpu_sibling_mask(cpu))
505 if (i != cpu && !coretemp_device_add(i))
506 break;
507 return;
Rudolf Marekbebe4672007-05-08 17:22:02 +0200508 }
509 mutex_unlock(&pdev_list_mutex);
510}
511
Sam Ravnborgba7c1922008-02-17 13:22:51 +0100512static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
Rudolf Marekbebe4672007-05-08 17:22:02 +0200513 unsigned long action, void *hcpu)
514{
515 unsigned int cpu = (unsigned long) hcpu;
516
517 switch (action) {
518 case CPU_ONLINE:
Rafael J. Wysocki561d9a92007-12-03 18:01:50 +0100519 case CPU_DOWN_FAILED:
Rudolf Marekbebe4672007-05-08 17:22:02 +0200520 coretemp_device_add(cpu);
521 break;
Rafael J. Wysocki561d9a92007-12-03 18:01:50 +0100522 case CPU_DOWN_PREPARE:
Rudolf Marekbebe4672007-05-08 17:22:02 +0200523 coretemp_device_remove(cpu);
524 break;
525 }
526 return NOTIFY_OK;
527}
528
Sam Ravnborgba7c1922008-02-17 13:22:51 +0100529static struct notifier_block coretemp_cpu_notifier __refdata = {
Rudolf Marekbebe4672007-05-08 17:22:02 +0200530 .notifier_call = coretemp_cpu_callback,
531};
Rudolf Marekbebe4672007-05-08 17:22:02 +0200532
533static int __init coretemp_init(void)
534{
535 int i, err = -ENODEV;
Rudolf Marekbebe4672007-05-08 17:22:02 +0200536
Rudolf Marekbebe4672007-05-08 17:22:02 +0200537 /* quick check if we run Intel */
Mike Travis92cb7612007-10-19 20:35:04 +0200538 if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
Rudolf Marekbebe4672007-05-08 17:22:02 +0200539 goto exit;
540
541 err = platform_driver_register(&coretemp_driver);
542 if (err)
543 goto exit;
544
Jan Beulicha4659052010-09-23 22:21:34 -0700545 for_each_online_cpu(i)
546 coretemp_device_add(i);
Jan Beulich89a3fd32010-09-13 10:05:51 +0000547
548#ifndef CONFIG_HOTPLUG_CPU
Rudolf Marekbebe4672007-05-08 17:22:02 +0200549 if (list_empty(&pdev_list)) {
550 err = -ENODEV;
551 goto exit_driver_unreg;
552 }
Jan Beulich89a3fd32010-09-13 10:05:51 +0000553#endif
Rudolf Marekbebe4672007-05-08 17:22:02 +0200554
Rudolf Marekbebe4672007-05-08 17:22:02 +0200555 register_hotcpu_notifier(&coretemp_cpu_notifier);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200556 return 0;
557
Chen Gong0dca94b2010-08-09 17:21:09 -0700558#ifndef CONFIG_HOTPLUG_CPU
Jan Beulich89a3fd32010-09-13 10:05:51 +0000559exit_driver_unreg:
Rudolf Marekbebe4672007-05-08 17:22:02 +0200560 platform_driver_unregister(&coretemp_driver);
Chen Gong0dca94b2010-08-09 17:21:09 -0700561#endif
Rudolf Marekbebe4672007-05-08 17:22:02 +0200562exit:
563 return err;
564}
565
566static void __exit coretemp_exit(void)
567{
568 struct pdev_entry *p, *n;
Chen Gong17c10d62010-10-08 22:01:48 -0400569
Rudolf Marekbebe4672007-05-08 17:22:02 +0200570 unregister_hotcpu_notifier(&coretemp_cpu_notifier);
Rudolf Marekbebe4672007-05-08 17:22:02 +0200571 mutex_lock(&pdev_list_mutex);
572 list_for_each_entry_safe(p, n, &pdev_list, list) {
573 platform_device_unregister(p->pdev);
574 list_del(&p->list);
575 kfree(p);
576 }
577 mutex_unlock(&pdev_list_mutex);
578 platform_driver_unregister(&coretemp_driver);
579}
580
581MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
582MODULE_DESCRIPTION("Intel Core temperature monitor");
583MODULE_LICENSE("GPL");
584
585module_init(coretemp_init)
586module_exit(coretemp_exit)