c959fe9f84a2544e20ae0aa95ed5181d6fd8e48e
[linux-flexiantxendom0-3.2.10.git] / drivers / hwmon / coretemp-xen.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/hwmon.h>
30 #include <linux/sysfs.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/err.h>
33 #include <linux/mutex.h>
34 #include <linux/list.h>
35 #include <linux/platform_device.h>
36 #include <linux/cpu.h>
37 #include <linux/pci.h>
38 #include <linux/smp.h>
39 #include <linux/moduleparam.h>
40 #include <asm/msr.h>
41 #include <asm/cpu_device_id.h>
42 #include <xen/pcpu.h>
43 #include "../xen/core/domctl.h"
44
45 #define DRVNAME "coretemp"
46
47 /*
48  * force_tjmax only matters when TjMax can't be read from the CPU itself.
49  * When set, it replaces the driver's suboptimal heuristic.
50  */
51 static int force_tjmax;
52 module_param_named(tjmax, force_tjmax, int, 0444);
53 MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
54
55 #define BASE_SYSFS_ATTR_NO      2       /* Sysfs Base attr no for coretemp */
56 #define NUM_REAL_CORES          16      /* Number of Real cores per cpu */
57 #define CORETEMP_NAME_LENGTH    17      /* String Length of attrs */
58 #define MAX_CORE_ATTRS          4       /* Maximum no of basic attrs */
59 #define TOTAL_ATTRS             (MAX_CORE_ATTRS + 1)
60 #define MAX_CORE_DATA           (NUM_REAL_CORES + BASE_SYSFS_ATTR_NO)
61
62 #define TO_PHYS_ID(cpu)         ({ \
63         u32 ppid; \
64         !xen_get_topology_info(cpu, NULL, &ppid, NULL) ? ppid : ~0; \
65 })
66 #define CORE_ATTR_NO(ccid)      ((ccid) + BASE_SYSFS_ATTR_NO)
67
68 /*
69  * Per-Core Temperature Data
70  * @last_updated: The time when the current temperature value was updated
71  *              earlier (in jiffies).
72  * @cpu_core_id: The CPU Core from which temperature values should be read
73  *              This value is passed as "id" field to rdmsr/wrmsr functions.
74  * @status_reg: One of IA32_THERM_STATUS or IA32_PACKAGE_THERM_STATUS,
75  *              from where the temperature values should be read.
76  * @attr_size:  Total number of pre-core attrs displayed in the sysfs.
77  * @is_pkg_data: If this is 1, the temp_data holds pkgtemp data.
78  *              Otherwise, temp_data holds coretemp data.
79  * @valid: If this is 1, the current temperature is valid.
80  */
81 struct temp_data {
82         int temp;
83         int ttarget;
84         int tjmax;
85         unsigned long last_updated;
86         unsigned int cpu;
87         u32 cpu_core_id;
88         u32 status_reg;
89         int attr_size;
90         bool is_pkg_data;
91         bool valid;
92         struct sensor_device_attribute sd_attrs[TOTAL_ATTRS];
93         char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH];
94         struct mutex update_lock;
95 };
96
97 /* Platform Data per Physical CPU */
98 struct platform_data {
99         struct device *hwmon_dev;
100         u16 phys_proc_id;
101         u8 x86_model, x86_mask;
102         struct temp_data *core_data[MAX_CORE_DATA];
103         struct device_attribute name_attr;
104 };
105
106 struct pdev_entry {
107         struct list_head list;
108         struct platform_device *pdev;
109         u16 phys_proc_id;
110 };
111
112 struct cpu_info {
113         struct platform_device *pdev;
114         u32 cpuid_6_eax, microcode;
115         u32 phys_proc_id, cpu_core_id;
116         u8 x86_model, x86_mask;
117 };
118
119 static LIST_HEAD(pdev_list);
120 static DEFINE_MUTEX(pdev_list_mutex);
121
122 static ssize_t show_name(struct device *dev,
123                         struct device_attribute *devattr, char *buf)
124 {
125         return sprintf(buf, "%s\n", DRVNAME);
126 }
127
128 static ssize_t show_label(struct device *dev,
129                                 struct device_attribute *devattr, char *buf)
130 {
131         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
132         struct platform_data *pdata = dev_get_drvdata(dev);
133         struct temp_data *tdata = pdata->core_data[attr->index];
134
135         if (tdata->is_pkg_data)
136                 return sprintf(buf, "Physical id %u\n", pdata->phys_proc_id);
137
138         return sprintf(buf, "Core %u\n", tdata->cpu_core_id);
139 }
140
141 static ssize_t show_crit_alarm(struct device *dev,
142                                 struct device_attribute *devattr, char *buf)
143 {
144         u32 eax, edx;
145         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
146         struct platform_data *pdata = dev_get_drvdata(dev);
147         struct temp_data *tdata = pdata->core_data[attr->index];
148
149         if (rdmsr_safe_on_pcpu(tdata->cpu, tdata->status_reg, &eax, &edx) < 0)
150                 return sprintf(buf, "\n");
151
152         return sprintf(buf, "%d\n", (eax >> 5) & 1);
153 }
154
155 static ssize_t show_tjmax(struct device *dev,
156                         struct device_attribute *devattr, char *buf)
157 {
158         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
159         struct platform_data *pdata = dev_get_drvdata(dev);
160
161         return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);
162 }
163
164 static ssize_t show_ttarget(struct device *dev,
165                                 struct device_attribute *devattr, char *buf)
166 {
167         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
168         struct platform_data *pdata = dev_get_drvdata(dev);
169
170         return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget);
171 }
172
173 static ssize_t show_temp(struct device *dev,
174                         struct device_attribute *devattr, char *buf)
175 {
176         u32 eax, edx;
177         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
178         struct platform_data *pdata = dev_get_drvdata(dev);
179         struct temp_data *tdata = pdata->core_data[attr->index];
180
181         mutex_lock(&tdata->update_lock);
182
183         /* Check whether the time interval has elapsed */
184         if (!tdata->valid || time_after(jiffies, tdata->last_updated + HZ)) {
185                 if (rdmsr_safe_on_pcpu(tdata->cpu, tdata->status_reg,
186                                          &eax, &edx) < 0)
187                         eax = ~0;
188                 tdata->valid = 0;
189                 /* Check whether the data is valid */
190                 if (eax & 0x80000000) {
191                         tdata->temp = tdata->tjmax -
192                                         ((eax >> 16) & 0x7f) * 1000;
193                         tdata->valid = 1;
194                 }
195                 tdata->last_updated = jiffies;
196         }
197
198         mutex_unlock(&tdata->update_lock);
199         return tdata->valid ? sprintf(buf, "%d\n", tdata->temp) : -EAGAIN;
200 }
201
202 static int adjust_tjmax(struct platform_data *c, u32 id, struct device *dev)
203 {
204         /* The 100C is default for both mobile and non mobile CPUs */
205
206         int tjmax = 100000;
207         int tjmax_ee = 85000;
208         int usemsr_ee = 1;
209         int err;
210         u32 eax, edx;
211         struct pci_dev *host_bridge;
212
213         /* Early chips have no MSR for TjMax */
214
215         if (c->x86_model == 0xf && c->x86_mask < 4)
216                 usemsr_ee = 0;
217
218         /* Atom CPUs */
219
220         if (c->x86_model == 0x1c) {
221                 usemsr_ee = 0;
222
223                 host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
224
225                 if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL
226                     && (host_bridge->device == 0xa000   /* NM10 based nettop */
227                     || host_bridge->device == 0xa010))  /* NM10 based netbook */
228                         tjmax = 100000;
229                 else
230                         tjmax = 90000;
231
232                 pci_dev_put(host_bridge);
233         }
234
235         if (c->x86_model > 0xe && usemsr_ee) {
236                 u8 platform_id;
237
238                 /*
239                  * Now we can detect the mobile CPU using Intel provided table
240                  * http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
241                  * For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
242                  */
243                 err = rdmsr_safe_on_pcpu(id, 0x17, &eax, &edx);
244                 if (err < 0) {
245                         dev_warn(dev,
246                                  "Unable to access MSR 0x17, assuming desktop"
247                                  " CPU\n");
248                         usemsr_ee = 0;
249                 } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
250                         /*
251                          * Trust bit 28 up to Penryn, I could not find any
252                          * documentation on that; if you happen to know
253                          * someone at Intel please ask
254                          */
255                         usemsr_ee = 0;
256                 } else {
257                         /* Platform ID bits 52:50 (EDX starts at bit 32) */
258                         platform_id = (edx >> 18) & 0x7;
259
260                         /*
261                          * Mobile Penryn CPU seems to be platform ID 7 or 5
262                          * (guesswork)
263                          */
264                         if (c->x86_model == 0x17 &&
265                             (platform_id == 5 || platform_id == 7)) {
266                                 /*
267                                  * If MSR EE bit is set, set it to 90 degrees C,
268                                  * otherwise 105 degrees C
269                                  */
270                                 tjmax_ee = 90000;
271                                 tjmax = 105000;
272                         }
273                 }
274         }
275
276         if (usemsr_ee) {
277                 err = rdmsr_safe_on_pcpu(id, 0xee, &eax, &edx);
278                 if (err < 0) {
279                         dev_warn(dev,
280                                  "Unable to access MSR 0xEE, for Tjmax, left"
281                                  " at default\n");
282                 } else if (eax & 0x40000000) {
283                         tjmax = tjmax_ee;
284                 }
285         } else if (tjmax == 100000) {
286                 /*
287                  * If we don't use msr EE it means we are desktop CPU
288                  * (with exeception of Atom)
289                  */
290                 dev_warn(dev, "Using relative temperature scale!\n");
291         }
292
293         return tjmax;
294 }
295
296 static int get_tjmax(struct platform_data *c, u32 id, struct device *dev)
297 {
298         int err;
299         u32 eax, edx;
300         u32 val;
301
302         /*
303          * A new feature of current Intel(R) processors, the
304          * IA32_TEMPERATURE_TARGET contains the TjMax value
305          */
306         err = rdmsr_safe_on_pcpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
307         if (err < 0) {
308                 if (c->x86_model > 0xe && c->x86_model != 0x1c)
309                         dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);
310         } else {
311                 val = (eax >> 16) & 0xff;
312                 /*
313                  * If the TjMax is not plausible, an assumption
314                  * will be used
315                  */
316                 if (val) {
317                         dev_dbg(dev, "TjMax is %d degrees C\n", val);
318                         return val * 1000;
319                 }
320         }
321
322         if (force_tjmax) {
323                 dev_notice(dev, "TjMax forced to %d degrees C by user\n",
324                            force_tjmax);
325                 return force_tjmax * 1000;
326         }
327
328         /*
329          * An assumption is made for early CPUs and unreadable MSR.
330          * NOTE: the calculated value may not be correct.
331          */
332         return adjust_tjmax(c, id, dev);
333 }
334
335 static int create_name_attr(struct platform_data *pdata, struct device *dev)
336 {
337         sysfs_attr_init(&pdata->name_attr.attr);
338         pdata->name_attr.attr.name = "name";
339         pdata->name_attr.attr.mode = S_IRUGO;
340         pdata->name_attr.show = show_name;
341         return device_create_file(dev, &pdata->name_attr);
342 }
343
344 static int create_core_attrs(struct temp_data *tdata, struct device *dev,
345                                 int attr_no)
346 {
347         int err, i;
348         static ssize_t (*const rd_ptr[TOTAL_ATTRS]) (struct device *dev,
349                         struct device_attribute *devattr, char *buf) = {
350                         show_label, show_crit_alarm, show_temp, show_tjmax,
351                         show_ttarget };
352         static const char *const names[TOTAL_ATTRS] = {
353                                         "temp%d_label", "temp%d_crit_alarm",
354                                         "temp%d_input", "temp%d_crit",
355                                         "temp%d_max" };
356
357         for (i = 0; i < tdata->attr_size; i++) {
358                 snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH, names[i],
359                         attr_no);
360                 sysfs_attr_init(&tdata->sd_attrs[i].dev_attr.attr);
361                 tdata->sd_attrs[i].dev_attr.attr.name = tdata->attr_name[i];
362                 tdata->sd_attrs[i].dev_attr.attr.mode = S_IRUGO;
363                 tdata->sd_attrs[i].dev_attr.show = rd_ptr[i];
364                 tdata->sd_attrs[i].index = attr_no;
365                 err = device_create_file(dev, &tdata->sd_attrs[i].dev_attr);
366                 if (err)
367                         goto exit_free;
368         }
369         return 0;
370
371 exit_free:
372         while (--i >= 0)
373                 device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
374         return err;
375 }
376
377
378 static int chk_ucode_version(unsigned int cpu, const struct cpu_info *c)
379 {
380         /*
381          * Check if we have problem with errata AE18 of Core processors:
382          * Readings might stop update when processor visited too deep sleep,
383          * fixed for stepping D0 (6EC).
384          */
385         if (c->x86_model == 0xe && c->x86_mask < 0xc && c->microcode < 0x39) {
386                 pr_err("Errata AE18 not fixed, update BIOS or "
387                        "microcode of the CPU!\n");
388                 return -ENODEV;
389         }
390         return 0;
391 }
392
393 static struct platform_device *coretemp_get_pdev(unsigned int cpu)
394 {
395         u16 phys_proc_id = TO_PHYS_ID(cpu);
396         struct pdev_entry *p;
397
398         mutex_lock(&pdev_list_mutex);
399
400         list_for_each_entry(p, &pdev_list, list)
401                 if (p->phys_proc_id == phys_proc_id) {
402                         mutex_unlock(&pdev_list_mutex);
403                         return p->pdev;
404                 }
405
406         mutex_unlock(&pdev_list_mutex);
407         return NULL;
408 }
409
410 static struct temp_data *init_temp_data(unsigned int cpu,
411                                         const struct cpu_info *c,
412                                         int pkg_flag)
413 {
414         struct temp_data *tdata;
415
416         tdata = kzalloc(sizeof(struct temp_data), GFP_KERNEL);
417         if (!tdata)
418                 return NULL;
419
420         tdata->status_reg = pkg_flag ? MSR_IA32_PACKAGE_THERM_STATUS :
421                                                         MSR_IA32_THERM_STATUS;
422         tdata->is_pkg_data = pkg_flag;
423         tdata->cpu = cpu;
424         tdata->cpu_core_id = c->cpu_core_id;
425         tdata->attr_size = MAX_CORE_ATTRS;
426         mutex_init(&tdata->update_lock);
427         return tdata;
428 }
429
430 static int create_core_data(struct platform_device *pdev,
431                             unsigned int cpu,
432                             const struct cpu_info *c, int pkg_flag)
433 {
434         struct temp_data *tdata;
435         struct platform_data *pdata = platform_get_drvdata(pdev);
436         u32 eax, edx;
437         int err, attr_no;
438
439         /*
440          * Find attr number for sysfs:
441          * We map the attr number to core id of the CPU
442          * The attr number is always core id + 2
443          * The Pkgtemp will always show up as temp1_*, if available
444          */
445         attr_no = pkg_flag ? 1 : CORE_ATTR_NO(c->cpu_core_id);
446
447         if (attr_no > MAX_CORE_DATA - 1)
448                 return -ERANGE;
449
450         /*
451          * Provide a single set of attributes for all HT siblings of a core
452          * to avoid duplicate sensors (the processor ID and core ID of all
453          * HT siblings of a core are the same).
454          * Skip if a HT sibling of this core is already registered.
455          * This is not an error.
456          */
457         if (pdata->core_data[attr_no] != NULL)
458                 return 0;
459
460         tdata = init_temp_data(cpu, c, pkg_flag);
461         if (!tdata)
462                 return -ENOMEM;
463
464         /* Test if we can access the status register */
465         err = rdmsr_safe_on_pcpu(cpu, tdata->status_reg, &eax, &edx);
466         if (err < 0)
467                 goto exit_free;
468
469         /* We can access status register. Get Critical Temperature */
470         tdata->tjmax = get_tjmax(pdata, cpu, &pdev->dev);
471
472         /*
473          * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
474          * The target temperature is available on older CPUs but not in this
475          * register. Atoms don't have the register at all.
476          */
477         if (c->x86_model > 0xe && c->x86_model != 0x1c) {
478                 err = rdmsr_safe_on_pcpu(cpu, MSR_IA32_TEMPERATURE_TARGET,
479                                          &eax, &edx);
480                 if (err >= 0) {
481                         tdata->ttarget
482                           = tdata->tjmax - ((eax >> 8) & 0xff) * 1000;
483                         tdata->attr_size++;
484                 }
485         }
486
487         pdata->core_data[attr_no] = tdata;
488
489         /* Create sysfs interfaces */
490         err = create_core_attrs(tdata, &pdev->dev, attr_no);
491         if (err)
492                 goto exit_free;
493
494         return 0;
495 exit_free:
496         pdata->core_data[attr_no] = NULL;
497         kfree(tdata);
498         return err;
499 }
500
501 static void coretemp_add_core(unsigned int cpu,
502                               const struct cpu_info *c, int pkg_flag)
503 {
504         struct platform_device *pdev = c->pdev;
505         int err;
506
507         err = create_core_data(pdev, cpu, c, pkg_flag);
508         if (err)
509                 dev_err(&pdev->dev, "Adding Core %u failed\n", cpu);
510 }
511
512 static void coretemp_remove_core(struct platform_data *pdata,
513                                 struct device *dev, int indx)
514 {
515         int i;
516         struct temp_data *tdata = pdata->core_data[indx];
517
518         /* Remove the sysfs attributes */
519         for (i = 0; i < tdata->attr_size; i++)
520                 device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
521
522         kfree(pdata->core_data[indx]);
523         pdata->core_data[indx] = NULL;
524 }
525
526 static int coretemp_probe(struct platform_device *pdev)
527 {
528         struct platform_data *pdata = platform_get_drvdata(pdev);
529         int err;
530
531         /* Initialize the per-package data structures */
532         err = create_name_attr(pdata, &pdev->dev);
533         if (err)
534                 return err;
535
536         pdata->hwmon_dev = hwmon_device_register(&pdev->dev);
537         if (IS_ERR(pdata->hwmon_dev)) {
538                 err = PTR_ERR(pdata->hwmon_dev);
539                 dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
540                 goto exit_name;
541         }
542         return 0;
543
544 exit_name:
545         device_remove_file(&pdev->dev, &pdata->name_attr);
546         return err;
547 }
548
549 static int coretemp_remove(struct platform_device *pdev)
550 {
551         struct platform_data *pdata = platform_get_drvdata(pdev);
552         int i;
553
554         for (i = MAX_CORE_DATA - 1; i >= 0; --i)
555                 if (pdata->core_data[i])
556                         coretemp_remove_core(pdata, &pdev->dev, i);
557
558         device_remove_file(&pdev->dev, &pdata->name_attr);
559         hwmon_device_unregister(pdata->hwmon_dev);
560         platform_set_drvdata(pdev, NULL);
561         kfree(pdata);
562         return 0;
563 }
564
565 static struct platform_driver coretemp_driver = {
566         .driver = {
567                 .owner = THIS_MODULE,
568                 .name = DRVNAME,
569         },
570         .probe = coretemp_probe,
571         .remove = coretemp_remove,
572 };
573
574 static int coretemp_device_add(unsigned int cpu, struct cpu_info *c)
575 {
576         int err;
577         struct platform_device *pdev;
578         struct pdev_entry *pdev_entry;
579         struct platform_data *pdata = NULL;
580
581         mutex_lock(&pdev_list_mutex);
582
583         pdev = platform_device_alloc(DRVNAME, c->phys_proc_id);
584         if (!pdev) {
585                 err = -ENOMEM;
586                 pr_err("Device allocation failed\n");
587                 goto exit;
588         }
589
590         pdata = kzalloc(sizeof(struct platform_data), GFP_KERNEL);
591         if (!pdata) {
592                 err = -ENOMEM;
593                 goto exit_device_put;
594         }
595
596         pdata->phys_proc_id = c->phys_proc_id;
597         pdata->x86_model = c->x86_model;
598         pdata->x86_mask = c->x86_mask;
599         platform_set_drvdata(pdev, pdata);
600
601         pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
602         if (!pdev_entry) {
603                 err = -ENOMEM;
604                 goto exit_device_put;
605         }
606
607         err = platform_device_add(pdev);
608         if (err) {
609                 pr_err("Device addition failed (%d)\n", err);
610                 goto exit_device_free;
611         }
612
613         pdev_entry->pdev = pdev;
614         pdev_entry->phys_proc_id = c->phys_proc_id;
615         c->pdev = pdev;
616
617         list_add_tail(&pdev_entry->list, &pdev_list);
618         mutex_unlock(&pdev_list_mutex);
619
620         return 0;
621
622 exit_device_free:
623         kfree(pdev_entry);
624 exit_device_put:
625         platform_device_put(pdev);
626         kfree(pdata);
627 exit:
628         mutex_unlock(&pdev_list_mutex);
629         return err;
630 }
631
632 static void coretemp_device_remove(unsigned int cpu)
633 {
634         struct pdev_entry *p, *n;
635         u16 phys_proc_id = TO_PHYS_ID(cpu);
636
637         mutex_lock(&pdev_list_mutex);
638         list_for_each_entry_safe(p, n, &pdev_list, list) {
639                 if (p->phys_proc_id != phys_proc_id)
640                         continue;
641                 platform_device_unregister(p->pdev);
642                 list_del(&p->list);
643                 kfree(p);
644         }
645         mutex_unlock(&pdev_list_mutex);
646 }
647
648 static bool is_any_core_online(struct platform_data *pdata)
649 {
650         int i;
651
652         /* Find online cores, except pkgtemp data */
653         for (i = MAX_CORE_DATA - 1; i >= 0; --i) {
654                 if (pdata->core_data[i] &&
655                         !pdata->core_data[i]->is_pkg_data) {
656                         return true;
657                 }
658         }
659         return false;
660 }
661
662 static void get_cpuid_info(void *arg)
663 {
664         struct cpu_info *info = arg;
665         u32 val = cpuid_eax(1);
666
667         info->x86_model = ((val >> 4) & 0xf) | ((val >> 12) & 0xf0);
668         info->x86_mask = val & 0xf;
669
670         if (((val >> 8) & 0xf) != 6 || ((val >> 20) & 0xff)
671             || !info->x86_model
672             || wrmsr_safe(MSR_IA32_UCODE_REV, 0, 0) < 0
673             || (sync_core(), rdmsr_safe(MSR_IA32_UCODE_REV,
674                                         &val, &info->microcode)) < 0)
675                 info->microcode = 0;
676
677         info->cpuid_6_eax = cpuid_eax(0) >= 6 ? cpuid_eax(6) : 0;
678 }
679
680 static void get_core_online(unsigned int cpu)
681 {
682         struct cpu_info info;
683         struct platform_device *pdev = coretemp_get_pdev(cpu);
684         int err;
685
686         info.pdev = pdev;
687
688         err = xen_set_physical_cpu_affinity(cpu);
689         if (!err) {
690                 get_cpuid_info(&info);
691                 WARN_ON_ONCE(xen_set_physical_cpu_affinity(-1));
692         } else if (err > 0) {
693                 static bool warned;
694
695                 if (!warned) {
696                         warned = true;
697                         pr_warn(DRVNAME "Cannot set physical CPU affinity"
698                                 " (assuming use of dom0_vcpus_pin)\n");
699                 }
700                 err = smp_call_function_single(cpu, get_cpuid_info, &info, 1);
701         }
702         if (err)
703                 return;
704
705         /*
706          * CPUID.06H.EAX[0] indicates whether the CPU has thermal
707          * sensors. We check this bit only, all the early CPUs
708          * without thermal sensors will be filtered out.
709          */
710         if (!(info.cpuid_6_eax & 0x1))
711                 return;
712
713         err = xen_get_topology_info(cpu, &info.cpu_core_id,
714                                     &info.phys_proc_id, NULL);
715         if (err)
716                 return;
717
718         if (!pdev) {
719                 /* Check the microcode version of the CPU */
720                 if (chk_ucode_version(cpu, &info))
721                         return;
722
723                 /*
724                  * Alright, we have DTS support.
725                  * We are bringing the _first_ core in this pkg
726                  * online. So, initialize per-pkg data structures and
727                  * then bring this core online.
728                  */
729                 err = coretemp_device_add(cpu, &info);
730                 if (err)
731                         return;
732                 /*
733                  * Check whether pkgtemp support is available.
734                  * If so, add interfaces for pkgtemp.
735                  */
736                 if (info.cpuid_6_eax & 0x40)
737                         coretemp_add_core(cpu, &info, 1);
738         }
739         /*
740          * Physical CPU device already exists.
741          * So, just add interfaces for this core.
742          */
743         coretemp_add_core(cpu, &info, 0);
744 }
745
746 static void put_core_offline(unsigned int cpu)
747 {
748         int i, indx;
749         struct platform_data *pdata;
750         struct platform_device *pdev = coretemp_get_pdev(cpu);
751         u32 cpu_core_id, phys_proc_id;
752
753         /* If the physical CPU device does not exist, just return */
754         if (!pdev)
755                 return;
756
757         pdata = platform_get_drvdata(pdev);
758
759         if (xen_get_topology_info(cpu, &cpu_core_id, &phys_proc_id, NULL))
760                 return;
761         indx = CORE_ATTR_NO(cpu_core_id);
762
763         if (pdata->core_data[indx] && pdata->core_data[indx]->cpu == cpu)
764                 coretemp_remove_core(pdata, &pdev->dev, indx);
765
766         /*
767          * If a HT sibling of a core is taken offline, but another HT sibling
768          * of the same core is still online, register the alternate sibling.
769          * This ensures that exactly one set of attributes is provided as long
770          * as at least one HT sibling of a core is online.
771          */
772         for (i = 0; ; ++i) {
773                 u32 cid, pid;
774                 int err;
775
776                 if (i != cpu) {
777                         err = xen_get_topology_info(i, &cid, &pid, NULL);
778                         if (err == -ENOENT)
779                                 continue;
780                         if (err)
781                                 break;
782                         if (pid != phys_proc_id || cid != cpu_core_id)
783                                 continue;
784                         get_core_online(i);
785                         /*
786                          * Display temperature sensor data for one HT sibling
787                          * per core only, so abort the loop after one such
788                          * sibling has been found.
789                          */
790                         break;
791                 }
792         }
793         /*
794          * If all cores in this pkg are offline, remove the device.
795          * coretemp_device_remove calls unregister_platform_device,
796          * which in turn calls coretemp_remove. This removes the
797          * pkgtemp entry and does other clean ups.
798          */
799         if (!is_any_core_online(pdata))
800                 coretemp_device_remove(cpu);
801 }
802
803 static int coretemp_cpu_callback(struct notifier_block *nfb,
804                                  unsigned long action, void *hcpu)
805 {
806         unsigned int cpu = (unsigned long) hcpu;
807
808         switch (action) {
809         case CPU_ONLINE:
810                 get_core_online(cpu);
811                 break;
812         case CPU_DEAD:
813                 put_core_offline(cpu);
814                 break;
815         }
816         return NOTIFY_OK;
817 }
818
819 static struct notifier_block coretemp_cpu_notifier = {
820         .notifier_call = coretemp_cpu_callback,
821 };
822
823 static const struct x86_cpu_id coretemp_ids[] = {
824         { X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_DTS },
825         {}
826 };
827 MODULE_DEVICE_TABLE(x86cpu, coretemp_ids);
828
829 static int __init coretemp_init(void)
830 {
831         int err = -ENODEV;
832
833         if (!is_initial_xendomain())
834                 goto exit;
835
836         /*
837          * CPUID.06H.EAX[0] indicates whether the CPU has thermal
838          * sensors. We check this bit only, all the early CPUs
839          * without thermal sensors will be filtered out.
840          */
841         if (!x86_match_cpu(coretemp_ids))
842                 return -ENODEV;
843
844         err = platform_driver_register(&coretemp_driver);
845         if (err)
846                 goto exit;
847
848         err = register_pcpu_notifier(&coretemp_cpu_notifier);
849         if (err)
850                 goto exit_driver_unreg;
851
852 #ifndef CONFIG_ACPI_HOTPLUG_CPU
853         if (list_empty(&pdev_list)) {
854                 unregister_pcpu_notifier(&coretemp_cpu_notifier);
855                 err = -ENODEV;
856                 goto exit_driver_unreg;
857         }
858 #endif
859
860         return 0;
861
862 exit_driver_unreg:
863         platform_driver_unregister(&coretemp_driver);
864 exit:
865         return err;
866 }
867
868 static void __exit coretemp_exit(void)
869 {
870         struct pdev_entry *p, *n;
871
872         unregister_pcpu_notifier(&coretemp_cpu_notifier);
873         mutex_lock(&pdev_list_mutex);
874         list_for_each_entry_safe(p, n, &pdev_list, list) {
875                 platform_device_unregister(p->pdev);
876                 list_del(&p->list);
877                 kfree(p);
878         }
879         mutex_unlock(&pdev_list_mutex);
880         platform_driver_unregister(&coretemp_driver);
881 }
882
883 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
884 MODULE_DESCRIPTION("Intel Core temperature monitor");
885 MODULE_LICENSE("GPL");
886
887 module_init(coretemp_init)
888 module_exit(coretemp_exit)