Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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> |
Yong Wang | 1fe63ab | 2010-01-10 20:52:34 +0100 | [diff] [blame] | 36 | #include <linux/pci.h> |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 37 | #include <asm/msr.h> |
| 38 | #include <asm/processor.h> |
| 39 | |
| 40 | #define DRVNAME "coretemp" |
| 41 | |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 42 | typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL, |
| 43 | SHOW_NAME } SHOW; |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * Functions declaration |
| 47 | */ |
| 48 | |
| 49 | static struct coretemp_data *coretemp_update_device(struct device *dev); |
| 50 | |
| 51 | struct coretemp_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 52 | struct device *hwmon_dev; |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 53 | struct mutex update_lock; |
| 54 | const char *name; |
| 55 | u32 id; |
| 56 | char valid; /* zero until following fields are valid */ |
| 57 | unsigned long last_updated; /* in jiffies */ |
| 58 | int temp; |
| 59 | int tjmax; |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 60 | int ttarget; |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 61 | u8 alarm; |
| 62 | }; |
| 63 | |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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->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; |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 99 | else if (attr->index == SHOW_TJMAX) |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 100 | err = sprintf(buf, "%d\n", data->tjmax); |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 101 | else |
| 102 | err = sprintf(buf, "%d\n", data->ttarget); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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); |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 110 | static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, |
| 111 | SHOW_TTARGET); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 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 | |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 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; |
Rudolf Marek | eccfed4 | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 161 | int tjmax_ee = 85000; |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 162 | int usemsr_ee = 1; |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 163 | int err; |
| 164 | u32 eax, edx; |
Yong Wang | 1fe63ab | 2010-01-10 20:52:34 +0100 | [diff] [blame] | 165 | struct pci_dev *host_bridge; |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 166 | |
| 167 | /* Early chips have no MSR for TjMax */ |
| 168 | |
| 169 | if ((c->x86_model == 0xf) && (c->x86_mask < 4)) { |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 170 | usemsr_ee = 0; |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Yong Wang | 1fe63ab | 2010-01-10 20:52:34 +0100 | [diff] [blame] | 173 | /* Atom CPUs */ |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 174 | |
| 175 | if (c->x86_model == 0x1c) { |
| 176 | usemsr_ee = 0; |
Yong Wang | 1fe63ab | 2010-01-10 20:52:34 +0100 | [diff] [blame] | 177 | |
| 178 | host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0)); |
| 179 | |
| 180 | if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL |
| 181 | && (host_bridge->device == 0xa000 /* NM10 based nettop */ |
| 182 | || host_bridge->device == 0xa010)) /* NM10 based netbook */ |
| 183 | tjmax = 100000; |
| 184 | else |
| 185 | tjmax = 90000; |
| 186 | |
| 187 | pci_dev_put(host_bridge); |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if ((c->x86_model > 0xe) && (usemsr_ee)) { |
Rudolf Marek | eccfed4 | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 191 | u8 platform_id; |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 192 | |
| 193 | /* Now we can detect the mobile CPU using Intel provided table |
| 194 | http://softwarecommunity.intel.com/Wiki/Mobility/720.htm |
| 195 | For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU |
| 196 | */ |
| 197 | |
| 198 | err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx); |
| 199 | if (err) { |
| 200 | dev_warn(dev, |
| 201 | "Unable to access MSR 0x17, assuming desktop" |
| 202 | " CPU\n"); |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 203 | usemsr_ee = 0; |
Rudolf Marek | eccfed4 | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 204 | } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) { |
| 205 | /* Trust bit 28 up to Penryn, I could not find any |
| 206 | documentation on that; if you happen to know |
| 207 | someone at Intel please ask */ |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 208 | usemsr_ee = 0; |
Rudolf Marek | eccfed4 | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 209 | } else { |
| 210 | /* Platform ID bits 52:50 (EDX starts at bit 32) */ |
| 211 | platform_id = (edx >> 18) & 0x7; |
| 212 | |
| 213 | /* Mobile Penryn CPU seems to be platform ID 7 or 5 |
| 214 | (guesswork) */ |
| 215 | if ((c->x86_model == 0x17) && |
| 216 | ((platform_id == 5) || (platform_id == 7))) { |
| 217 | /* If MSR EE bit is set, set it to 90 degrees C, |
| 218 | otherwise 105 degrees C */ |
| 219 | tjmax_ee = 90000; |
| 220 | tjmax = 105000; |
| 221 | } |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 225 | if (usemsr_ee) { |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 226 | |
| 227 | err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx); |
| 228 | if (err) { |
| 229 | dev_warn(dev, |
| 230 | "Unable to access MSR 0xEE, for Tjmax, left" |
Dean Nelson | 4d7a564 | 2010-03-29 22:03:00 +0200 | [diff] [blame] | 231 | " at default\n"); |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 232 | } else if (eax & 0x40000000) { |
Rudolf Marek | eccfed4 | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 233 | tjmax = tjmax_ee; |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 234 | } |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 235 | /* if we dont use msr EE it means we are desktop CPU (with exeception |
| 236 | of Atom) */ |
| 237 | } else if (tjmax == 100000) { |
Rudolf Marek | 118a887 | 2008-02-17 22:59:39 +0100 | [diff] [blame] | 238 | dev_warn(dev, "Using relative temperature scale!\n"); |
| 239 | } |
| 240 | |
| 241 | return tjmax; |
| 242 | } |
| 243 | |
Carsten Emde | a321ced | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 244 | static int __devinit get_tjmax(struct cpuinfo_x86 *c, u32 id, |
| 245 | struct device *dev) |
| 246 | { |
| 247 | /* The 100C is default for both mobile and non mobile CPUs */ |
| 248 | int err; |
| 249 | u32 eax, edx; |
| 250 | u32 val; |
| 251 | |
| 252 | /* A new feature of current Intel(R) processors, the |
| 253 | IA32_TEMPERATURE_TARGET contains the TjMax value */ |
| 254 | err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx); |
| 255 | if (err) { |
| 256 | dev_warn(dev, "Unable to read TjMax from CPU.\n"); |
| 257 | } else { |
| 258 | val = (eax >> 16) & 0xff; |
| 259 | /* |
| 260 | * If the TjMax is not plausible, an assumption |
| 261 | * will be used |
| 262 | */ |
| 263 | if ((val > 80) && (val < 120)) { |
| 264 | dev_info(dev, "TjMax is %d C.\n", val); |
| 265 | return val * 1000; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * An assumption is made for early CPUs and unreadable MSR. |
| 271 | * NOTE: the given value may not be correct. |
| 272 | */ |
| 273 | |
| 274 | switch (c->x86_model) { |
| 275 | case 0xe: |
| 276 | case 0xf: |
| 277 | case 0x16: |
| 278 | case 0x1a: |
| 279 | dev_warn(dev, "TjMax is assumed as 100 C!\n"); |
| 280 | return 100000; |
| 281 | break; |
| 282 | case 0x17: |
| 283 | case 0x1c: /* Atom CPUs */ |
| 284 | return adjust_tjmax(c, id, dev); |
| 285 | break; |
| 286 | default: |
| 287 | dev_warn(dev, "CPU (model=0x%x) is not supported yet," |
| 288 | " using default TjMax of 100C.\n", c->x86_model); |
| 289 | return 100000; |
| 290 | } |
| 291 | } |
| 292 | |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 293 | static int __devinit coretemp_probe(struct platform_device *pdev) |
| 294 | { |
| 295 | struct coretemp_data *data; |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 296 | struct cpuinfo_x86 *c = &cpu_data(pdev->id); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 297 | int err; |
| 298 | u32 eax, edx; |
| 299 | |
| 300 | if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) { |
| 301 | err = -ENOMEM; |
| 302 | dev_err(&pdev->dev, "Out of memory\n"); |
| 303 | goto exit; |
| 304 | } |
| 305 | |
| 306 | data->id = pdev->id; |
| 307 | data->name = "coretemp"; |
| 308 | mutex_init(&data->update_lock); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 309 | |
| 310 | /* test if we can access the THERM_STATUS MSR */ |
| 311 | err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx); |
| 312 | if (err) { |
| 313 | dev_err(&pdev->dev, |
| 314 | "Unable to access THERM_STATUS MSR, giving up\n"); |
| 315 | goto exit_free; |
| 316 | } |
| 317 | |
Rudolf Marek | 67f363b | 2007-05-27 22:17:43 +0200 | [diff] [blame] | 318 | /* Check if we have problem with errata AE18 of Core processors: |
| 319 | Readings might stop update when processor visited too deep sleep, |
| 320 | fixed for stepping D0 (6EC). |
| 321 | */ |
| 322 | |
| 323 | if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) { |
| 324 | /* check for microcode update */ |
| 325 | rdmsr_on_cpu(data->id, MSR_IA32_UCODE_REV, &eax, &edx); |
| 326 | if (edx < 0x39) { |
Jean Delvare | 6d79af7 | 2007-06-23 17:16:24 -0700 | [diff] [blame] | 327 | err = -ENODEV; |
Rudolf Marek | 67f363b | 2007-05-27 22:17:43 +0200 | [diff] [blame] | 328 | dev_err(&pdev->dev, |
| 329 | "Errata AE18 not fixed, update BIOS or " |
| 330 | "microcode of the CPU!\n"); |
| 331 | goto exit_free; |
| 332 | } |
| 333 | } |
| 334 | |
Carsten Emde | a321ced | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 335 | data->tjmax = get_tjmax(c, data->id, &pdev->dev); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 336 | platform_set_drvdata(pdev, data); |
| 337 | |
Carsten Emde | a321ced | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 338 | /* |
| 339 | * read the still undocumented IA32_TEMPERATURE_TARGET. It exists |
| 340 | * on older CPUs but not in this register, |
| 341 | * Atoms don't have it either. |
| 342 | */ |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 343 | |
Rudolf Marek | 708a62b | 2009-09-23 22:59:42 +0200 | [diff] [blame] | 344 | if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) { |
Carsten Emde | a321ced | 2010-05-24 14:33:41 -0700 | [diff] [blame] | 345 | err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET, |
| 346 | &eax, &edx); |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 347 | if (err) { |
| 348 | dev_warn(&pdev->dev, "Unable to read" |
| 349 | " IA32_TEMPERATURE_TARGET MSR\n"); |
| 350 | } else { |
| 351 | data->ttarget = data->tjmax - |
| 352 | (((eax >> 8) & 0xff) * 1000); |
| 353 | err = device_create_file(&pdev->dev, |
| 354 | &sensor_dev_attr_temp1_max.dev_attr); |
| 355 | if (err) |
| 356 | goto exit_free; |
| 357 | } |
| 358 | } |
| 359 | |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 360 | if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group))) |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 361 | goto exit_dev; |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 362 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 363 | data->hwmon_dev = hwmon_device_register(&pdev->dev); |
| 364 | if (IS_ERR(data->hwmon_dev)) { |
| 365 | err = PTR_ERR(data->hwmon_dev); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 366 | dev_err(&pdev->dev, "Class registration failed (%d)\n", |
| 367 | err); |
| 368 | goto exit_class; |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | |
| 373 | exit_class: |
| 374 | sysfs_remove_group(&pdev->dev.kobj, &coretemp_group); |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 375 | exit_dev: |
| 376 | device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 377 | exit_free: |
| 378 | kfree(data); |
| 379 | exit: |
| 380 | return err; |
| 381 | } |
| 382 | |
| 383 | static int __devexit coretemp_remove(struct platform_device *pdev) |
| 384 | { |
| 385 | struct coretemp_data *data = platform_get_drvdata(pdev); |
| 386 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 387 | hwmon_device_unregister(data->hwmon_dev); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 388 | sysfs_remove_group(&pdev->dev.kobj, &coretemp_group); |
Rudolf Marek | 6369a288 | 2008-01-18 00:42:54 +0100 | [diff] [blame] | 389 | device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 390 | platform_set_drvdata(pdev, NULL); |
| 391 | kfree(data); |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static struct platform_driver coretemp_driver = { |
| 396 | .driver = { |
| 397 | .owner = THIS_MODULE, |
| 398 | .name = DRVNAME, |
| 399 | }, |
| 400 | .probe = coretemp_probe, |
| 401 | .remove = __devexit_p(coretemp_remove), |
| 402 | }; |
| 403 | |
| 404 | struct pdev_entry { |
| 405 | struct list_head list; |
| 406 | struct platform_device *pdev; |
| 407 | unsigned int cpu; |
Jean Delvare | d883b9f | 2010-07-09 16:22:49 +0200 | [diff] [blame^] | 408 | #ifdef CONFIG_SMP |
| 409 | u16 phys_proc_id; |
| 410 | u16 cpu_core_id; |
| 411 | #endif |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | static LIST_HEAD(pdev_list); |
| 415 | static DEFINE_MUTEX(pdev_list_mutex); |
| 416 | |
| 417 | static int __cpuinit coretemp_device_add(unsigned int cpu) |
| 418 | { |
| 419 | int err; |
| 420 | struct platform_device *pdev; |
| 421 | struct pdev_entry *pdev_entry; |
Jean Delvare | d883b9f | 2010-07-09 16:22:49 +0200 | [diff] [blame^] | 422 | #ifdef CONFIG_SMP |
| 423 | struct cpuinfo_x86 *c = &cpu_data(cpu); |
| 424 | #endif |
| 425 | |
| 426 | mutex_lock(&pdev_list_mutex); |
| 427 | |
| 428 | #ifdef CONFIG_SMP |
| 429 | /* Skip second HT entry of each core */ |
| 430 | list_for_each_entry(pdev_entry, &pdev_list, list) { |
| 431 | if (c->phys_proc_id == pdev_entry->phys_proc_id && |
| 432 | c->cpu_core_id == pdev_entry->cpu_core_id) { |
| 433 | err = 0; /* Not an error */ |
| 434 | goto exit; |
| 435 | } |
| 436 | } |
| 437 | #endif |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 438 | |
| 439 | pdev = platform_device_alloc(DRVNAME, cpu); |
| 440 | if (!pdev) { |
| 441 | err = -ENOMEM; |
| 442 | printk(KERN_ERR DRVNAME ": Device allocation failed\n"); |
| 443 | goto exit; |
| 444 | } |
| 445 | |
| 446 | pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL); |
| 447 | if (!pdev_entry) { |
| 448 | err = -ENOMEM; |
| 449 | goto exit_device_put; |
| 450 | } |
| 451 | |
| 452 | err = platform_device_add(pdev); |
| 453 | if (err) { |
| 454 | printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", |
| 455 | err); |
| 456 | goto exit_device_free; |
| 457 | } |
| 458 | |
| 459 | pdev_entry->pdev = pdev; |
| 460 | pdev_entry->cpu = cpu; |
Jean Delvare | d883b9f | 2010-07-09 16:22:49 +0200 | [diff] [blame^] | 461 | #ifdef CONFIG_SMP |
| 462 | pdev_entry->phys_proc_id = c->phys_proc_id; |
| 463 | pdev_entry->cpu_core_id = c->cpu_core_id; |
| 464 | #endif |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 465 | list_add_tail(&pdev_entry->list, &pdev_list); |
| 466 | mutex_unlock(&pdev_list_mutex); |
| 467 | |
| 468 | return 0; |
| 469 | |
| 470 | exit_device_free: |
| 471 | kfree(pdev_entry); |
| 472 | exit_device_put: |
| 473 | platform_device_put(pdev); |
| 474 | exit: |
Jean Delvare | d883b9f | 2010-07-09 16:22:49 +0200 | [diff] [blame^] | 475 | mutex_unlock(&pdev_list_mutex); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 476 | return err; |
| 477 | } |
| 478 | |
| 479 | #ifdef CONFIG_HOTPLUG_CPU |
Adrian Bunk | d2bc7b1 | 2007-07-06 01:23:06 +0200 | [diff] [blame] | 480 | static void coretemp_device_remove(unsigned int cpu) |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 481 | { |
| 482 | struct pdev_entry *p, *n; |
| 483 | mutex_lock(&pdev_list_mutex); |
| 484 | list_for_each_entry_safe(p, n, &pdev_list, list) { |
| 485 | if (p->cpu == cpu) { |
| 486 | platform_device_unregister(p->pdev); |
| 487 | list_del(&p->list); |
| 488 | kfree(p); |
| 489 | } |
| 490 | } |
| 491 | mutex_unlock(&pdev_list_mutex); |
| 492 | } |
| 493 | |
Sam Ravnborg | ba7c192 | 2008-02-17 13:22:51 +0100 | [diff] [blame] | 494 | static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb, |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 495 | unsigned long action, void *hcpu) |
| 496 | { |
| 497 | unsigned int cpu = (unsigned long) hcpu; |
| 498 | |
| 499 | switch (action) { |
| 500 | case CPU_ONLINE: |
Rafael J. Wysocki | 561d9a9 | 2007-12-03 18:01:50 +0100 | [diff] [blame] | 501 | case CPU_DOWN_FAILED: |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 502 | coretemp_device_add(cpu); |
| 503 | break; |
Rafael J. Wysocki | 561d9a9 | 2007-12-03 18:01:50 +0100 | [diff] [blame] | 504 | case CPU_DOWN_PREPARE: |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 505 | coretemp_device_remove(cpu); |
| 506 | break; |
| 507 | } |
| 508 | return NOTIFY_OK; |
| 509 | } |
| 510 | |
Sam Ravnborg | ba7c192 | 2008-02-17 13:22:51 +0100 | [diff] [blame] | 511 | static struct notifier_block coretemp_cpu_notifier __refdata = { |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 512 | .notifier_call = coretemp_cpu_callback, |
| 513 | }; |
| 514 | #endif /* !CONFIG_HOTPLUG_CPU */ |
| 515 | |
| 516 | static int __init coretemp_init(void) |
| 517 | { |
| 518 | int i, err = -ENODEV; |
| 519 | struct pdev_entry *p, *n; |
| 520 | |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 521 | /* quick check if we run Intel */ |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 522 | if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL) |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 523 | goto exit; |
| 524 | |
| 525 | err = platform_driver_register(&coretemp_driver); |
| 526 | if (err) |
| 527 | goto exit; |
| 528 | |
| 529 | for_each_online_cpu(i) { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 530 | struct cpuinfo_x86 *c = &cpu_data(i); |
Carsten Emde | 5db47b0 | 2010-05-24 14:33:39 -0700 | [diff] [blame] | 531 | /* |
| 532 | * CPUID.06H.EAX[0] indicates whether the CPU has thermal |
| 533 | * sensors. We check this bit only, all the early CPUs |
| 534 | * without thermal sensors will be filtered out. |
| 535 | */ |
| 536 | if (c->cpuid_level >= 6 && (cpuid_eax(0x06) & 0x01)) { |
| 537 | err = coretemp_device_add(i); |
| 538 | if (err) |
| 539 | goto exit_devices_unreg; |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 540 | |
Carsten Emde | 5db47b0 | 2010-05-24 14:33:39 -0700 | [diff] [blame] | 541 | } else { |
| 542 | printk(KERN_INFO DRVNAME ": CPU (model=0x%x)" |
| 543 | " has no thermal sensor.\n", c->x86_model); |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 544 | } |
Rudolf Marek | bebe467 | 2007-05-08 17:22:02 +0200 | [diff] [blame] | 545 | } |
| 546 | if (list_empty(&pdev_list)) { |
| 547 | err = -ENODEV; |
| 548 | goto exit_driver_unreg; |
| 549 | } |
| 550 | |
| 551 | #ifdef CONFIG_HOTPLUG_CPU |
| 552 | register_hotcpu_notifier(&coretemp_cpu_notifier); |
| 553 | #endif |
| 554 | return 0; |
| 555 | |
| 556 | exit_devices_unreg: |
| 557 | mutex_lock(&pdev_list_mutex); |
| 558 | list_for_each_entry_safe(p, n, &pdev_list, list) { |
| 559 | platform_device_unregister(p->pdev); |
| 560 | list_del(&p->list); |
| 561 | kfree(p); |
| 562 | } |
| 563 | mutex_unlock(&pdev_list_mutex); |
| 564 | exit_driver_unreg: |
| 565 | platform_driver_unregister(&coretemp_driver); |
| 566 | exit: |
| 567 | return err; |
| 568 | } |
| 569 | |
| 570 | static void __exit coretemp_exit(void) |
| 571 | { |
| 572 | struct pdev_entry *p, *n; |
| 573 | #ifdef CONFIG_HOTPLUG_CPU |
| 574 | unregister_hotcpu_notifier(&coretemp_cpu_notifier); |
| 575 | #endif |
| 576 | mutex_lock(&pdev_list_mutex); |
| 577 | list_for_each_entry_safe(p, n, &pdev_list, list) { |
| 578 | platform_device_unregister(p->pdev); |
| 579 | list_del(&p->list); |
| 580 | kfree(p); |
| 581 | } |
| 582 | mutex_unlock(&pdev_list_mutex); |
| 583 | platform_driver_unregister(&coretemp_driver); |
| 584 | } |
| 585 | |
| 586 | MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>"); |
| 587 | MODULE_DESCRIPTION("Intel Core temperature monitor"); |
| 588 | MODULE_LICENSE("GPL"); |
| 589 | |
| 590 | module_init(coretemp_init) |
| 591 | module_exit(coretemp_exit) |