1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * coretemp.c - Linux kernel module for hardware monitoring 4 * 5 * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz> 6 * 7 * Inspired from many hwmon drivers 8 */ 9 10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12 #include <linux/module.h> 13 #include <linux/init.h> 14 #include <linux/slab.h> 15 #include <linux/jiffies.h> 16 #include <linux/hwmon.h> 17 #include <linux/sysfs.h> 18 #include <linux/hwmon-sysfs.h> 19 #include <linux/err.h> 20 #include <linux/mutex.h> 21 #include <linux/list.h> 22 #include <linux/platform_device.h> 23 #include <linux/cpu.h> 24 #include <linux/smp.h> 25 #include <linux/moduleparam.h> 26 #include <linux/pci.h> 27 #include <asm/msr.h> 28 #include <linux/processor.h> 29 #include <asm/cpu_device_id.h> 30 #include <linux/sched/isolation.h> 31 32 #define DRVNAME "coretemp" 33 34 /* 35 * force_tjmax only matters when TjMax can't be read from the CPU itself. 36 * When set, it replaces the driver's suboptimal heuristic. 37 */ 38 static int force_tjmax; 39 module_param_named(tjmax, force_tjmax, int, 0444); 40 MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius"); 41 42 #define CORETEMP_NAME_LENGTH 28 /* String Length of attrs */ 43 44 enum coretemp_attr_index { 45 ATTR_LABEL, 46 ATTR_CRIT_ALARM, 47 ATTR_TEMP, 48 ATTR_TJMAX, 49 ATTR_TTARGET, 50 MAX_CORE_ATTRS = ATTR_TJMAX + 1, /* Maximum no of basic attrs */ 51 TOTAL_ATTRS = ATTR_TTARGET + 1 /* Maximum no of possible attrs */ 52 }; 53 54 #ifdef CONFIG_SMP 55 #define for_each_sibling(i, cpu) \ 56 for_each_cpu(i, topology_sibling_cpumask(cpu)) 57 #else 58 #define for_each_sibling(i, cpu) for (i = 0; false; ) 59 #endif 60 61 /* 62 * Per-Core Temperature Data 63 * @temp_valid: Whether the CPU reported @temp as valid. @temp is not 64 * reported when this is false. 65 * @tjmax: The static tjmax value when tjmax cannot be retrieved from 66 * IA32_TEMPERATURE_TARGET MSR. 67 * @last_updated: The time when the current temperature value was updated 68 * earlier (in jiffies). 69 * @cpu_core_id: The CPU Core from which temperature values should be read 70 * This value is passed as "id" field to rdmsr/wrmsr functions. 71 * @status_reg: One of IA32_THERM_STATUS or IA32_PACKAGE_THERM_STATUS, 72 * from where the temperature values should be read. 73 * @attr_size: Number of sysfs attributes in this temperature data group. 74 */ 75 struct temp_data { 76 int temp; 77 bool temp_valid; 78 int tjmax; 79 u64 last_updated; 80 unsigned int cpu; 81 int index; 82 u32 cpu_core_id; 83 u32 status_reg; 84 int attr_size; 85 struct device_attribute sd_attrs[TOTAL_ATTRS]; 86 char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH]; 87 struct attribute *attrs[TOTAL_ATTRS + 1]; 88 struct attribute_group attr_group; 89 struct mutex update_lock; 90 }; 91 92 /* Platform Data per Physical CPU */ 93 struct platform_data { 94 struct device *hwmon_dev; 95 u16 pkg_id; 96 int nr_cores; 97 struct ida ida; 98 struct cpumask cpumask; 99 struct temp_data *pkg_data; 100 struct temp_data **core_data; 101 struct device_attribute name_attr; 102 }; 103 104 struct tjmax_pci { 105 unsigned int device; 106 int tjmax; 107 }; 108 109 static const struct tjmax_pci tjmax_pci_table[] = { 110 { 0x0708, 110000 }, /* CE41x0 (Sodaville ) */ 111 { 0x0c72, 102000 }, /* Atom S1240 (Centerton) */ 112 { 0x0c73, 95000 }, /* Atom S1220 (Centerton) */ 113 { 0x0c75, 95000 }, /* Atom S1260 (Centerton) */ 114 }; 115 116 struct tjmax { 117 char const *id; 118 int tjmax; 119 }; 120 121 static const struct tjmax tjmax_table[] = { 122 { "CPU 230", 100000 }, /* Model 0x1c, stepping 2 */ 123 { "CPU 330", 125000 }, /* Model 0x1c, stepping 2 */ 124 }; 125 126 struct tjmax_model { 127 u32 vfm; 128 u8 stepping_mask; 129 int tjmax; 130 }; 131 132 #define ANY 0xff 133 134 static const struct tjmax_model tjmax_model_table[] = { 135 { INTEL_ATOM_BONNELL, 10, 100000 }, /* D4xx, K4xx, N4xx, D5xx, K5xx, N5xx */ 136 { INTEL_ATOM_BONNELL, ANY, 90000 }, /* Z5xx, N2xx, possibly others 137 * Note: Also matches 230 and 330, 138 * which are covered by tjmax_table 139 */ 140 { INTEL_ATOM_BONNELL_MID, ANY, 90000 }, /* Atom Tunnel Creek (Exx), Lincroft (Z6xx) 141 * Note: TjMax for E6xxT is 110C, but CPU 142 * type is undetectable by software 143 */ 144 { INTEL_ATOM_SALTWELL_MID, ANY, 90000 }, /* Atom Medfield (Z2460) */ 145 { INTEL_ATOM_SALTWELL_TABLET, ANY, 90000 }, /* Atom Clover Trail/Cloverview (Z27x0) */ 146 { INTEL_ATOM_SALTWELL, ANY, 100000 }, /* Atom Cedar Trail/Cedarview (N2xxx, D2xxx) 147 * Also matches S12x0 (stepping 9), covered 148 * by PCI table 149 */ 150 { INTEL_ATOM_SILVERMONT, 9, 110000 }, /* Atom Bay Trail E38xx (embedded) */ 151 { INTEL_ATOM_SILVERMONT, ANY, 90000 }, /* Atom Bay Trail Z37xx (tablet) */ 152 { INTEL_ATOM_SILVERMONT_MID, ANY, 90000 }, /* Atom Merrifield (Z34xx) */ 153 { INTEL_ATOM_SILVERMONT_MID2, ANY, 90000 }, /* Atom Moorefield (Z35xx) */ 154 { INTEL_ATOM_AIRMONT, ANY, 90000 }, /* Atom Cherry Trail (Z8xxx) */ 155 { INTEL_ATOM_GOLDMONT, ANY, 105000 }, /* Atom Apollo Lake (J3xxx, N3xxx, E39xx) */ 156 { INTEL_ATOM_GOLDMONT_PLUS, ANY, 105000 }, /* Atom Gemini Lake (J4xxx, N4xxx, N5xxx) */ 157 { INTEL_ATOM_TREMONT, ANY, 105000 }, /* Atom Elkhart Lake */ 158 { INTEL_ATOM_TREMONT_L, ANY, 105000 }, /* Atom Jasper Lake */ 159 }; 160 161 static bool is_pkg_temp_data(struct temp_data *tdata) 162 { 163 return tdata->index < 0; 164 } 165 166 static int adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev) 167 { 168 /* The 100C is default for both mobile and non mobile CPUs */ 169 170 int tjmax = 100000; 171 int tjmax_ee = 85000; 172 int usemsr_ee = 1; 173 int err; 174 u64 val; 175 int i; 176 u16 devfn = PCI_DEVFN(0, 0); 177 struct pci_dev *host_bridge = pci_get_domain_bus_and_slot(0, 0, devfn); 178 179 /* 180 * Explicit tjmax table entries override heuristics. 181 * First try PCI host bridge IDs, followed by model ID strings 182 * and model/stepping information. 183 */ 184 if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL) { 185 for (i = 0; i < ARRAY_SIZE(tjmax_pci_table); i++) { 186 if (host_bridge->device == tjmax_pci_table[i].device) { 187 pci_dev_put(host_bridge); 188 return tjmax_pci_table[i].tjmax; 189 } 190 } 191 } 192 pci_dev_put(host_bridge); 193 194 /* 195 * This is literally looking for "CPU XXX" in the model string. 196 * Not checking it against the model as well. Just purely a 197 * string search. 198 */ 199 for (i = 0; i < ARRAY_SIZE(tjmax_table); i++) { 200 if (strstr(c->x86_model_id, tjmax_table[i].id)) 201 return tjmax_table[i].tjmax; 202 } 203 204 for (i = 0; i < ARRAY_SIZE(tjmax_model_table); i++) { 205 const struct tjmax_model *tm = &tjmax_model_table[i]; 206 207 if (c->x86_vfm == tm->vfm && 208 (tm->stepping_mask == ANY || 209 tm->stepping_mask == c->x86_stepping)) 210 return tm->tjmax; 211 } 212 213 /* Early chips have no MSR for TjMax */ 214 215 if (c->x86_vfm == INTEL_CORE2_MEROM && c->x86_stepping < 4) 216 usemsr_ee = 0; 217 218 if (c->x86_vfm > INTEL_CORE_YONAH && usemsr_ee) { 219 u8 platform_id; 220 221 /* 222 * Now we can detect the mobile CPU using Intel provided table 223 * http://softwarecommunity.intel.com/Wiki/Mobility/720.htm 224 * For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU 225 */ 226 err = rdmsrq_safe_on_cpu(id, 0x17, &val); 227 if (err) { 228 dev_warn(dev, 229 "Unable to access MSR 0x17, assuming desktop CPU\n"); 230 usemsr_ee = 0; 231 } else if (c->x86_vfm < INTEL_CORE2_PENRYN && 232 !(val & 0x10000000)) { 233 /* 234 * Trust bit 28 up to Penryn, I could not find any 235 * documentation on that; if you happen to know 236 * someone at Intel please ask 237 */ 238 usemsr_ee = 0; 239 } else { 240 /* Platform ID bits 52:50 */ 241 platform_id = (val >> 50) & 0x7; 242 243 /* 244 * Mobile Penryn CPU seems to be platform ID 7 or 5 245 * (guesswork) 246 */ 247 if (c->x86_vfm == INTEL_CORE2_PENRYN && 248 (platform_id == 5 || platform_id == 7)) { 249 /* 250 * If MSR EE bit is set, set it to 90 degrees C, 251 * otherwise 105 degrees C 252 */ 253 tjmax_ee = 90000; 254 tjmax = 105000; 255 } 256 } 257 } 258 259 if (usemsr_ee) { 260 err = rdmsrq_safe_on_cpu(id, 0xee, &val); 261 if (err) { 262 dev_warn(dev, 263 "Unable to access MSR 0xEE, for Tjmax, left at default\n"); 264 } else if (val & 0x40000000) { 265 tjmax = tjmax_ee; 266 } 267 } else if (tjmax == 100000) { 268 /* 269 * If we don't use msr EE it means we are desktop CPU 270 * (with exeception of Atom) 271 */ 272 dev_warn(dev, "Using relative temperature scale!\n"); 273 } 274 275 return tjmax; 276 } 277 278 static int get_tjmax(struct temp_data *tdata, struct device *dev) 279 { 280 struct cpuinfo_x86 *c = &cpu_data(tdata->cpu); 281 int err; 282 u64 msrval; 283 u32 val; 284 285 /* use static tjmax once it is set */ 286 if (tdata->tjmax) 287 return tdata->tjmax; 288 289 /* 290 * A new feature of current Intel(R) processors, the 291 * IA32_TEMPERATURE_TARGET contains the TjMax value 292 */ 293 err = rdmsrq_safe_on_cpu(tdata->cpu, MSR_IA32_TEMPERATURE_TARGET, &msrval); 294 if (err) { 295 dev_warn_once(dev, "Unable to read TjMax from CPU %u\n", tdata->cpu); 296 } else { 297 val = (msrval >> 16) & 0xff; 298 if (val) 299 return val * 1000; 300 } 301 302 if (force_tjmax) { 303 dev_notice(dev, "TjMax forced to %d degrees C by user\n", 304 force_tjmax); 305 tdata->tjmax = force_tjmax * 1000; 306 } else { 307 /* 308 * An assumption is made for early CPUs and unreadable MSR. 309 * NOTE: the calculated value may not be correct. 310 */ 311 tdata->tjmax = adjust_tjmax(c, tdata->cpu, dev); 312 } 313 return tdata->tjmax; 314 } 315 316 static int get_ttarget(struct temp_data *tdata, struct device *dev) 317 { 318 u64 val; 319 int tjmax, ttarget_offset, ret; 320 321 /* 322 * ttarget is valid only if tjmax can be retrieved from 323 * MSR_IA32_TEMPERATURE_TARGET 324 */ 325 if (tdata->tjmax) 326 return -ENODEV; 327 328 ret = rdmsrq_safe_on_cpu(tdata->cpu, MSR_IA32_TEMPERATURE_TARGET, &val); 329 if (ret) 330 return ret; 331 332 tjmax = (val >> 16) & 0xff; 333 334 /* Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET. */ 335 ttarget_offset = (val >> 8) & 0xff; 336 337 return (tjmax - ttarget_offset) * 1000; 338 } 339 340 /* Keep track of how many zone pointers we allocated in init() */ 341 static int max_zones __read_mostly; 342 /* Array of zone pointers. Serialized by cpu hotplug lock */ 343 static struct platform_device **zone_devices; 344 345 static ssize_t show_label(struct device *dev, 346 struct device_attribute *devattr, char *buf) 347 { 348 struct platform_data *pdata = dev_get_drvdata(dev); 349 struct temp_data *tdata = container_of(devattr, struct temp_data, sd_attrs[ATTR_LABEL]); 350 351 if (is_pkg_temp_data(tdata)) 352 return sprintf(buf, "Package id %u\n", pdata->pkg_id); 353 354 return sprintf(buf, "Core %u\n", tdata->cpu_core_id); 355 } 356 357 static ssize_t show_crit_alarm(struct device *dev, 358 struct device_attribute *devattr, char *buf) 359 { 360 struct msr val; 361 struct temp_data *tdata = container_of(devattr, struct temp_data, 362 sd_attrs[ATTR_CRIT_ALARM]); 363 364 mutex_lock(&tdata->update_lock); 365 rdmsrq_on_cpu(tdata->cpu, tdata->status_reg, &val.q); 366 mutex_unlock(&tdata->update_lock); 367 368 return sprintf(buf, "%d\n", (val.l >> 5) & 1); 369 } 370 371 static ssize_t show_tjmax(struct device *dev, 372 struct device_attribute *devattr, char *buf) 373 { 374 struct temp_data *tdata = container_of(devattr, struct temp_data, sd_attrs[ATTR_TJMAX]); 375 int tjmax; 376 377 mutex_lock(&tdata->update_lock); 378 tjmax = get_tjmax(tdata, dev); 379 mutex_unlock(&tdata->update_lock); 380 381 return sprintf(buf, "%d\n", tjmax); 382 } 383 384 static ssize_t show_ttarget(struct device *dev, 385 struct device_attribute *devattr, char *buf) 386 { 387 struct temp_data *tdata = container_of(devattr, struct temp_data, sd_attrs[ATTR_TTARGET]); 388 int ttarget; 389 390 mutex_lock(&tdata->update_lock); 391 ttarget = get_ttarget(tdata, dev); 392 mutex_unlock(&tdata->update_lock); 393 394 if (ttarget < 0) 395 return ttarget; 396 return sprintf(buf, "%d\n", ttarget); 397 } 398 399 static ssize_t show_temp(struct device *dev, 400 struct device_attribute *devattr, char *buf) 401 { 402 struct msr val; 403 struct temp_data *tdata = container_of(devattr, struct temp_data, sd_attrs[ATTR_TEMP]); 404 int tjmax, temp; 405 bool valid; 406 407 mutex_lock(&tdata->update_lock); 408 409 /* Check whether the time interval has elapsed */ 410 if (time_after64(get_jiffies_64(), tdata->last_updated + HZ)) { 411 tjmax = get_tjmax(tdata, dev); 412 rdmsrq_on_cpu(tdata->cpu, tdata->status_reg, &val.q); 413 /* 414 * Bit 31 indicates whether the reading is valid. Some CPUs 415 * clear it while the temperature is too low to be measured. 416 * Such a reading is not a temperature. Do not report it. 417 */ 418 tdata->temp_valid = (val.l >> 31) & 1; 419 tdata->temp = tjmax - ((val.l >> 16) & 0xff) * 1000; 420 tdata->last_updated = get_jiffies_64(); 421 } 422 423 valid = tdata->temp_valid; 424 temp = tdata->temp; 425 mutex_unlock(&tdata->update_lock); 426 427 if (!valid) 428 return -ENODATA; 429 return sprintf(buf, "%d\n", temp); 430 } 431 432 static int create_core_attrs(struct temp_data *tdata, struct device *dev) 433 { 434 static ssize_t (*const rd_ptr[TOTAL_ATTRS]) (struct device *dev, 435 struct device_attribute *devattr, char *buf) = { 436 show_label, show_crit_alarm, show_temp, show_tjmax, 437 show_ttarget }; 438 static const char *const suffixes[TOTAL_ATTRS] = { 439 "label", "crit_alarm", "input", "crit", "max" 440 }; 441 int i; 442 443 for (i = 0; i < tdata->attr_size; i++) { 444 /* 445 * We map the attr number to core id of the CPU 446 * The attr number is always core id + 2 447 * The Pkgtemp will always show up as temp1_*, if available 448 */ 449 int attr_no = is_pkg_temp_data(tdata) ? 1 : tdata->cpu_core_id + 2; 450 451 snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH, 452 "temp%d_%s", attr_no, suffixes[i]); 453 sysfs_attr_init(&tdata->sd_attrs[i].attr); 454 tdata->sd_attrs[i].attr.name = tdata->attr_name[i]; 455 tdata->sd_attrs[i].attr.mode = 0444; 456 tdata->sd_attrs[i].show = rd_ptr[i]; 457 tdata->attrs[i] = &tdata->sd_attrs[i].attr; 458 } 459 tdata->attr_group.attrs = tdata->attrs; 460 return sysfs_create_group(&dev->kobj, &tdata->attr_group); 461 } 462 463 static int chk_ucode_version(unsigned int cpu) 464 { 465 struct cpuinfo_x86 *c = &cpu_data(cpu); 466 467 /* 468 * Check if we have problem with errata AE18 of Core processors: 469 * Readings might stop update when processor visited too deep sleep, 470 * fixed for stepping D0 (6EC). 471 */ 472 if (c->x86_vfm == INTEL_CORE_YONAH && c->x86_stepping < 0xc && c->microcode < 0x39) { 473 pr_err("Errata AE18 not fixed, update BIOS or microcode of the CPU!\n"); 474 return -ENODEV; 475 } 476 return 0; 477 } 478 479 static struct platform_device *coretemp_get_pdev(unsigned int cpu) 480 { 481 int id = topology_logical_die_id(cpu); 482 483 if (id >= 0 && id < max_zones) 484 return zone_devices[id]; 485 return NULL; 486 } 487 488 static struct temp_data * 489 init_temp_data(struct platform_data *pdata, unsigned int cpu, int pkg_flag) 490 { 491 struct temp_data *tdata; 492 493 if (!pdata->core_data) { 494 int nr_cores = topology_num_cores_per_package(); 495 496 pdata->core_data = kzalloc_objs(struct temp_data *, nr_cores); 497 if (!pdata->core_data) 498 return NULL; 499 pdata->nr_cores = nr_cores; 500 } 501 502 tdata = kzalloc_obj(struct temp_data); 503 if (!tdata) 504 return NULL; 505 506 if (pkg_flag) { 507 pdata->pkg_data = tdata; 508 /* Use tdata->index as indicator of package temp data */ 509 tdata->index = -1; 510 } else { 511 tdata->index = ida_alloc_max(&pdata->ida, pdata->nr_cores - 1, GFP_KERNEL); 512 if (tdata->index < 0) { 513 kfree(tdata); 514 return NULL; 515 } 516 pdata->core_data[tdata->index] = tdata; 517 } 518 519 tdata->status_reg = pkg_flag ? MSR_IA32_PACKAGE_THERM_STATUS : 520 MSR_IA32_THERM_STATUS; 521 tdata->cpu = cpu; 522 tdata->cpu_core_id = topology_core_id(cpu); 523 tdata->attr_size = MAX_CORE_ATTRS; 524 /* Backdate to force a refresh on the first read */ 525 tdata->last_updated = get_jiffies_64() - HZ - 1; 526 mutex_init(&tdata->update_lock); 527 return tdata; 528 } 529 530 static void destroy_temp_data(struct platform_data *pdata, struct temp_data *tdata) 531 { 532 if (is_pkg_temp_data(tdata)) { 533 pdata->pkg_data = NULL; 534 kfree(pdata->core_data); 535 pdata->core_data = NULL; 536 pdata->nr_cores = 0; 537 } else { 538 pdata->core_data[tdata->index] = NULL; 539 ida_free(&pdata->ida, tdata->index); 540 } 541 kfree(tdata); 542 } 543 544 static struct temp_data *get_temp_data(struct platform_data *pdata, int cpu) 545 { 546 int i; 547 548 /* cpu < 0 means get pkg temp_data */ 549 if (cpu < 0) 550 return pdata->pkg_data; 551 552 for (i = 0; i < pdata->nr_cores; i++) { 553 if (pdata->core_data[i] && 554 pdata->core_data[i]->cpu_core_id == topology_core_id(cpu)) 555 return pdata->core_data[i]; 556 } 557 return NULL; 558 } 559 560 static int create_core_data(struct platform_device *pdev, unsigned int cpu, 561 int pkg_flag) 562 { 563 struct temp_data *tdata; 564 struct platform_data *pdata = platform_get_drvdata(pdev); 565 struct cpuinfo_x86 *c = &cpu_data(cpu); 566 u64 val; 567 int err; 568 569 tdata = init_temp_data(pdata, cpu, pkg_flag); 570 if (!tdata) 571 return -ENOMEM; 572 573 /* Test if we can access the status register */ 574 err = rdmsrq_safe_on_cpu(cpu, tdata->status_reg, &val); 575 if (err) 576 goto err; 577 578 /* Make sure tdata->tjmax is a valid indicator for dynamic/static tjmax */ 579 get_tjmax(tdata, &pdev->dev); 580 581 /* 582 * The target temperature is available on older CPUs but not in the 583 * MSR_IA32_TEMPERATURE_TARGET register. Atoms don't have the register 584 * at all. 585 */ 586 if (c->x86_vfm > INTEL_CORE_YONAH && c->x86_vfm != INTEL_ATOM_BONNELL) 587 if (get_ttarget(tdata, &pdev->dev) >= 0) 588 tdata->attr_size++; 589 590 /* Create sysfs interfaces */ 591 err = create_core_attrs(tdata, pdata->hwmon_dev); 592 if (err) 593 goto err; 594 595 return 0; 596 597 err: 598 destroy_temp_data(pdata, tdata); 599 return err; 600 } 601 602 static void 603 coretemp_add_core(struct platform_device *pdev, unsigned int cpu, int pkg_flag) 604 { 605 if (create_core_data(pdev, cpu, pkg_flag)) 606 dev_err(&pdev->dev, "Adding Core %u failed\n", cpu); 607 } 608 609 static void coretemp_remove_core(struct platform_data *pdata, struct temp_data *tdata) 610 { 611 /* if we errored on add then this is already gone */ 612 if (!tdata) 613 return; 614 615 /* Remove the sysfs attributes */ 616 sysfs_remove_group(&pdata->hwmon_dev->kobj, &tdata->attr_group); 617 618 destroy_temp_data(pdata, tdata); 619 } 620 621 static int coretemp_device_add(int zoneid) 622 { 623 struct platform_device *pdev; 624 struct platform_data *pdata; 625 int err; 626 627 /* Initialize the per-zone data structures */ 628 pdata = kzalloc_obj(*pdata); 629 if (!pdata) 630 return -ENOMEM; 631 632 pdata->pkg_id = zoneid; 633 ida_init(&pdata->ida); 634 635 pdev = platform_device_alloc(DRVNAME, zoneid); 636 if (!pdev) { 637 err = -ENOMEM; 638 goto err_free_pdata; 639 } 640 641 err = platform_device_add(pdev); 642 if (err) 643 goto err_put_dev; 644 645 platform_set_drvdata(pdev, pdata); 646 zone_devices[zoneid] = pdev; 647 return 0; 648 649 err_put_dev: 650 platform_device_put(pdev); 651 err_free_pdata: 652 kfree(pdata); 653 return err; 654 } 655 656 static void coretemp_device_remove(int zoneid) 657 { 658 struct platform_device *pdev = zone_devices[zoneid]; 659 struct platform_data *pdata = platform_get_drvdata(pdev); 660 661 ida_destroy(&pdata->ida); 662 kfree(pdata->core_data); 663 kfree(pdata); 664 platform_device_unregister(pdev); 665 } 666 667 static int coretemp_cpu_online(unsigned int cpu) 668 { 669 struct platform_device *pdev = coretemp_get_pdev(cpu); 670 struct cpuinfo_x86 *c = &cpu_data(cpu); 671 struct platform_data *pdata; 672 673 /* 674 * Don't execute this on resume as the offline callback did 675 * not get executed on suspend. 676 */ 677 if (cpuhp_tasks_frozen) 678 return 0; 679 680 /* 681 * CPUID.06H.EAX[0] indicates whether the CPU has thermal 682 * sensors. We check this bit only, all the early CPUs 683 * without thermal sensors will be filtered out. 684 */ 685 if (!cpu_has(c, X86_FEATURE_DTHERM)) 686 return -ENODEV; 687 688 /* 689 * Keep isolated CPUs out of pdata->cpumask. Besides tracking the CPUs 690 * available to this driver, the mask is used to select the CPU on which 691 * to read the temperature MSRs. 692 */ 693 if (!housekeeping_cpu(cpu, HK_TYPE_MISC)) 694 return 0; 695 696 if (!pdev) 697 return 0; 698 699 pdata = platform_get_drvdata(pdev); 700 if (!pdata->hwmon_dev) { 701 struct device *hwmon; 702 703 /* Check the microcode version of the CPU */ 704 if (chk_ucode_version(cpu)) 705 return -EINVAL; 706 707 /* 708 * Alright, we have DTS support. 709 * We are bringing the _first_ core in this pkg 710 * online. So, initialize per-pkg data structures and 711 * then bring this core online. 712 */ 713 hwmon = hwmon_device_register_with_groups(&pdev->dev, DRVNAME, 714 pdata, NULL); 715 if (IS_ERR(hwmon)) 716 return PTR_ERR(hwmon); 717 pdata->hwmon_dev = hwmon; 718 719 /* 720 * Check whether pkgtemp support is available. 721 * If so, add interfaces for pkgtemp. 722 */ 723 if (cpu_has(c, X86_FEATURE_PTS)) 724 coretemp_add_core(pdev, cpu, 1); 725 } 726 727 /* 728 * Check whether a housekeeping thread sibling is already online. If 729 * not, add the interface for this CPU core. 730 */ 731 if (!cpumask_intersects(&pdata->cpumask, topology_sibling_cpumask(cpu))) 732 coretemp_add_core(pdev, cpu, 0); 733 734 cpumask_set_cpu(cpu, &pdata->cpumask); 735 return 0; 736 } 737 738 static int coretemp_cpu_offline(unsigned int cpu) 739 { 740 struct platform_device *pdev = coretemp_get_pdev(cpu); 741 struct platform_data *pd; 742 struct temp_data *tdata; 743 int target; 744 745 /* No need to tear down any interfaces for suspend */ 746 if (cpuhp_tasks_frozen) 747 return 0; 748 749 /* If the physical CPU device does not exist, just return */ 750 if (!pdev) 751 return 0; 752 753 pd = platform_get_drvdata(pdev); 754 if (!pd->hwmon_dev) 755 return 0; 756 757 tdata = get_temp_data(pd, cpu); 758 759 cpumask_clear_cpu(cpu, &pd->cpumask); 760 761 /* 762 * If this is the last housekeeping thread sibling, remove the CPU core 763 * interface. If there is still a housekeeping sibling online, transfer 764 * the target CPU of that core interface to it. 765 */ 766 target = cpumask_any_and(&pd->cpumask, topology_sibling_cpumask(cpu)); 767 if (target >= nr_cpu_ids) { 768 coretemp_remove_core(pd, tdata); 769 } else if (tdata && tdata->cpu == cpu) { 770 mutex_lock(&tdata->update_lock); 771 tdata->cpu = target; 772 mutex_unlock(&tdata->update_lock); 773 } 774 775 /* 776 * If all housekeeping cores in this pkg are offline, remove the 777 * interface. 778 */ 779 tdata = get_temp_data(pd, -1); 780 if (cpumask_empty(&pd->cpumask)) { 781 if (tdata) 782 coretemp_remove_core(pd, tdata); 783 hwmon_device_unregister(pd->hwmon_dev); 784 pd->hwmon_dev = NULL; 785 return 0; 786 } 787 788 /* 789 * Check whether this core is the target for the package 790 * interface. We need to assign it to some other housekeeping CPU. 791 */ 792 if (tdata && tdata->cpu == cpu) { 793 target = cpumask_first(&pd->cpumask); 794 mutex_lock(&tdata->update_lock); 795 tdata->cpu = target; 796 mutex_unlock(&tdata->update_lock); 797 } 798 return 0; 799 } 800 801 static const struct x86_cpu_id coretemp_ids[] __initconst = { 802 X86_MATCH_VENDOR_FEATURE(INTEL, X86_FEATURE_DTHERM, NULL), 803 {} 804 }; 805 MODULE_DEVICE_TABLE(x86cpu, coretemp_ids); 806 807 static enum cpuhp_state coretemp_hp_online; 808 809 static int __init coretemp_init(void) 810 { 811 int i, err; 812 813 /* 814 * CPUID.06H.EAX[0] indicates whether the CPU has thermal 815 * sensors. We check this bit only, all the early CPUs 816 * without thermal sensors will be filtered out. This 817 * includes all the Family 5 and Family 15 (Pentium 4) 818 * models, since they never set the CPUID bit. 819 */ 820 if (!x86_match_cpu(coretemp_ids)) 821 return -ENODEV; 822 823 max_zones = topology_max_packages() * topology_max_dies_per_package(); 824 zone_devices = kzalloc_objs(struct platform_device *, max_zones); 825 if (!zone_devices) 826 return -ENOMEM; 827 828 for (i = 0; i < max_zones; i++) { 829 err = coretemp_device_add(i); 830 if (err) 831 goto outzone; 832 } 833 834 err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hwmon/coretemp:online", 835 coretemp_cpu_online, coretemp_cpu_offline); 836 if (err < 0) 837 goto outzone; 838 coretemp_hp_online = err; 839 return 0; 840 841 outzone: 842 while (i--) 843 coretemp_device_remove(i); 844 kfree(zone_devices); 845 return err; 846 } 847 module_init(coretemp_init) 848 849 static void __exit coretemp_exit(void) 850 { 851 int i; 852 853 cpuhp_remove_state(coretemp_hp_online); 854 for (i = 0; i < max_zones; i++) 855 coretemp_device_remove(i); 856 kfree(zone_devices); 857 } 858 module_exit(coretemp_exit) 859 860 MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>"); 861 MODULE_DESCRIPTION("Intel Core temperature monitor"); 862 MODULE_LICENSE("GPL"); 863