coretemp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 <[email protected]>
  6. *
  7. * Inspired from many hwmon drivers
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/hwmon.h>
  15. #include <linux/sysfs.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/err.h>
  18. #include <linux/mutex.h>
  19. #include <linux/list.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/cpu.h>
  22. #include <linux/smp.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/pci.h>
  25. #include <asm/msr.h>
  26. #include <asm/processor.h>
  27. #include <asm/cpu_device_id.h>
  28. #define DRVNAME "coretemp"
  29. /*
  30. * force_tjmax only matters when TjMax can't be read from the CPU itself.
  31. * When set, it replaces the driver's suboptimal heuristic.
  32. */
  33. static int force_tjmax;
  34. module_param_named(tjmax, force_tjmax, int, 0444);
  35. MODULE_PARM_DESC(tjmax, "TjMax value in degrees Celsius");
  36. #define PKG_SYSFS_ATTR_NO 1 /* Sysfs attribute for package temp */
  37. #define BASE_SYSFS_ATTR_NO 2 /* Sysfs Base attr no for coretemp */
  38. #define NUM_REAL_CORES 128 /* Number of Real cores per cpu */
  39. #define CORETEMP_NAME_LENGTH 28 /* String Length of attrs */
  40. #define MAX_CORE_ATTRS 4 /* Maximum no of basic attrs */
  41. #define TOTAL_ATTRS (MAX_CORE_ATTRS + 1)
  42. #define MAX_CORE_DATA (NUM_REAL_CORES + BASE_SYSFS_ATTR_NO)
  43. #ifdef CONFIG_SMP
  44. #define for_each_sibling(i, cpu) \
  45. for_each_cpu(i, topology_sibling_cpumask(cpu))
  46. #else
  47. #define for_each_sibling(i, cpu) for (i = 0; false; )
  48. #endif
  49. /*
  50. * Per-Core Temperature Data
  51. * @last_updated: The time when the current temperature value was updated
  52. * earlier (in jiffies).
  53. * @cpu_core_id: The CPU Core from which temperature values should be read
  54. * This value is passed as "id" field to rdmsr/wrmsr functions.
  55. * @status_reg: One of IA32_THERM_STATUS or IA32_PACKAGE_THERM_STATUS,
  56. * from where the temperature values should be read.
  57. * @attr_size: Total number of pre-core attrs displayed in the sysfs.
  58. * @is_pkg_data: If this is 1, the temp_data holds pkgtemp data.
  59. * Otherwise, temp_data holds coretemp data.
  60. * @valid: If this is 1, the current temperature is valid.
  61. */
  62. struct temp_data {
  63. int temp;
  64. int ttarget;
  65. int tjmax;
  66. unsigned long last_updated;
  67. unsigned int cpu;
  68. u32 cpu_core_id;
  69. u32 status_reg;
  70. int attr_size;
  71. bool is_pkg_data;
  72. bool valid;
  73. struct sensor_device_attribute sd_attrs[TOTAL_ATTRS];
  74. char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH];
  75. struct attribute *attrs[TOTAL_ATTRS + 1];
  76. struct attribute_group attr_group;
  77. struct mutex update_lock;
  78. };
  79. /* Platform Data per Physical CPU */
  80. struct platform_data {
  81. struct device *hwmon_dev;
  82. u16 pkg_id;
  83. u16 cpu_map[NUM_REAL_CORES];
  84. struct ida ida;
  85. struct cpumask cpumask;
  86. struct temp_data *core_data[MAX_CORE_DATA];
  87. struct device_attribute name_attr;
  88. };
  89. /* Keep track of how many zone pointers we allocated in init() */
  90. static int max_zones __read_mostly;
  91. /* Array of zone pointers. Serialized by cpu hotplug lock */
  92. static struct platform_device **zone_devices;
  93. static ssize_t show_label(struct device *dev,
  94. struct device_attribute *devattr, char *buf)
  95. {
  96. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  97. struct platform_data *pdata = dev_get_drvdata(dev);
  98. struct temp_data *tdata = pdata->core_data[attr->index];
  99. if (tdata->is_pkg_data)
  100. return sprintf(buf, "Package id %u\n", pdata->pkg_id);
  101. return sprintf(buf, "Core %u\n", tdata->cpu_core_id);
  102. }
  103. static ssize_t show_crit_alarm(struct device *dev,
  104. struct device_attribute *devattr, char *buf)
  105. {
  106. u32 eax, edx;
  107. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  108. struct platform_data *pdata = dev_get_drvdata(dev);
  109. struct temp_data *tdata = pdata->core_data[attr->index];
  110. mutex_lock(&tdata->update_lock);
  111. rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
  112. mutex_unlock(&tdata->update_lock);
  113. return sprintf(buf, "%d\n", (eax >> 5) & 1);
  114. }
  115. static ssize_t show_tjmax(struct device *dev,
  116. struct device_attribute *devattr, char *buf)
  117. {
  118. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  119. struct platform_data *pdata = dev_get_drvdata(dev);
  120. return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);
  121. }
  122. static ssize_t show_ttarget(struct device *dev,
  123. struct device_attribute *devattr, char *buf)
  124. {
  125. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  126. struct platform_data *pdata = dev_get_drvdata(dev);
  127. return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget);
  128. }
  129. static ssize_t show_temp(struct device *dev,
  130. struct device_attribute *devattr, char *buf)
  131. {
  132. u32 eax, edx;
  133. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  134. struct platform_data *pdata = dev_get_drvdata(dev);
  135. struct temp_data *tdata = pdata->core_data[attr->index];
  136. mutex_lock(&tdata->update_lock);
  137. /* Check whether the time interval has elapsed */
  138. if (!tdata->valid || time_after(jiffies, tdata->last_updated + HZ)) {
  139. rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
  140. /*
  141. * Ignore the valid bit. In all observed cases the register
  142. * value is either low or zero if the valid bit is 0.
  143. * Return it instead of reporting an error which doesn't
  144. * really help at all.
  145. */
  146. tdata->temp = tdata->tjmax - ((eax >> 16) & 0x7f) * 1000;
  147. tdata->valid = true;
  148. tdata->last_updated = jiffies;
  149. }
  150. mutex_unlock(&tdata->update_lock);
  151. return sprintf(buf, "%d\n", tdata->temp);
  152. }
  153. struct tjmax_pci {
  154. unsigned int device;
  155. int tjmax;
  156. };
  157. static const struct tjmax_pci tjmax_pci_table[] = {
  158. { 0x0708, 110000 }, /* CE41x0 (Sodaville ) */
  159. { 0x0c72, 102000 }, /* Atom S1240 (Centerton) */
  160. { 0x0c73, 95000 }, /* Atom S1220 (Centerton) */
  161. { 0x0c75, 95000 }, /* Atom S1260 (Centerton) */
  162. };
  163. struct tjmax {
  164. char const *id;
  165. int tjmax;
  166. };
  167. static const struct tjmax tjmax_table[] = {
  168. { "CPU 230", 100000 }, /* Model 0x1c, stepping 2 */
  169. { "CPU 330", 125000 }, /* Model 0x1c, stepping 2 */
  170. };
  171. struct tjmax_model {
  172. u8 model;
  173. u8 mask;
  174. int tjmax;
  175. };
  176. #define ANY 0xff
  177. static const struct tjmax_model tjmax_model_table[] = {
  178. { 0x1c, 10, 100000 }, /* D4xx, K4xx, N4xx, D5xx, K5xx, N5xx */
  179. { 0x1c, ANY, 90000 }, /* Z5xx, N2xx, possibly others
  180. * Note: Also matches 230 and 330,
  181. * which are covered by tjmax_table
  182. */
  183. { 0x26, ANY, 90000 }, /* Atom Tunnel Creek (Exx), Lincroft (Z6xx)
  184. * Note: TjMax for E6xxT is 110C, but CPU type
  185. * is undetectable by software
  186. */
  187. { 0x27, ANY, 90000 }, /* Atom Medfield (Z2460) */
  188. { 0x35, ANY, 90000 }, /* Atom Clover Trail/Cloverview (Z27x0) */
  189. { 0x36, ANY, 100000 }, /* Atom Cedar Trail/Cedarview (N2xxx, D2xxx)
  190. * Also matches S12x0 (stepping 9), covered by
  191. * PCI table
  192. */
  193. };
  194. static int adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
  195. {
  196. /* The 100C is default for both mobile and non mobile CPUs */
  197. int tjmax = 100000;
  198. int tjmax_ee = 85000;
  199. int usemsr_ee = 1;
  200. int err;
  201. u32 eax, edx;
  202. int i;
  203. u16 devfn = PCI_DEVFN(0, 0);
  204. struct pci_dev *host_bridge = pci_get_domain_bus_and_slot(0, 0, devfn);
  205. /*
  206. * Explicit tjmax table entries override heuristics.
  207. * First try PCI host bridge IDs, followed by model ID strings
  208. * and model/stepping information.
  209. */
  210. if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL) {
  211. for (i = 0; i < ARRAY_SIZE(tjmax_pci_table); i++) {
  212. if (host_bridge->device == tjmax_pci_table[i].device) {
  213. pci_dev_put(host_bridge);
  214. return tjmax_pci_table[i].tjmax;
  215. }
  216. }
  217. }
  218. pci_dev_put(host_bridge);
  219. for (i = 0; i < ARRAY_SIZE(tjmax_table); i++) {
  220. if (strstr(c->x86_model_id, tjmax_table[i].id))
  221. return tjmax_table[i].tjmax;
  222. }
  223. for (i = 0; i < ARRAY_SIZE(tjmax_model_table); i++) {
  224. const struct tjmax_model *tm = &tjmax_model_table[i];
  225. if (c->x86_model == tm->model &&
  226. (tm->mask == ANY || c->x86_stepping == tm->mask))
  227. return tm->tjmax;
  228. }
  229. /* Early chips have no MSR for TjMax */
  230. if (c->x86_model == 0xf && c->x86_stepping < 4)
  231. usemsr_ee = 0;
  232. if (c->x86_model > 0xe && usemsr_ee) {
  233. u8 platform_id;
  234. /*
  235. * Now we can detect the mobile CPU using Intel provided table
  236. * http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
  237. * For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
  238. */
  239. err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
  240. if (err) {
  241. dev_warn(dev,
  242. "Unable to access MSR 0x17, assuming desktop"
  243. " CPU\n");
  244. usemsr_ee = 0;
  245. } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
  246. /*
  247. * Trust bit 28 up to Penryn, I could not find any
  248. * documentation on that; if you happen to know
  249. * someone at Intel please ask
  250. */
  251. usemsr_ee = 0;
  252. } else {
  253. /* Platform ID bits 52:50 (EDX starts at bit 32) */
  254. platform_id = (edx >> 18) & 0x7;
  255. /*
  256. * Mobile Penryn CPU seems to be platform ID 7 or 5
  257. * (guesswork)
  258. */
  259. if (c->x86_model == 0x17 &&
  260. (platform_id == 5 || platform_id == 7)) {
  261. /*
  262. * If MSR EE bit is set, set it to 90 degrees C,
  263. * otherwise 105 degrees C
  264. */
  265. tjmax_ee = 90000;
  266. tjmax = 105000;
  267. }
  268. }
  269. }
  270. if (usemsr_ee) {
  271. err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
  272. if (err) {
  273. dev_warn(dev,
  274. "Unable to access MSR 0xEE, for Tjmax, left"
  275. " at default\n");
  276. } else if (eax & 0x40000000) {
  277. tjmax = tjmax_ee;
  278. }
  279. } else if (tjmax == 100000) {
  280. /*
  281. * If we don't use msr EE it means we are desktop CPU
  282. * (with exeception of Atom)
  283. */
  284. dev_warn(dev, "Using relative temperature scale!\n");
  285. }
  286. return tjmax;
  287. }
  288. static bool cpu_has_tjmax(struct cpuinfo_x86 *c)
  289. {
  290. u8 model = c->x86_model;
  291. return model > 0xe &&
  292. model != 0x1c &&
  293. model != 0x26 &&
  294. model != 0x27 &&
  295. model != 0x35 &&
  296. model != 0x36;
  297. }
  298. static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
  299. {
  300. int err;
  301. u32 eax, edx;
  302. u32 val;
  303. /*
  304. * A new feature of current Intel(R) processors, the
  305. * IA32_TEMPERATURE_TARGET contains the TjMax value
  306. */
  307. err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  308. if (err) {
  309. if (cpu_has_tjmax(c))
  310. dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);
  311. } else {
  312. val = (eax >> 16) & 0xff;
  313. /*
  314. * If the TjMax is not plausible, an assumption
  315. * will be used
  316. */
  317. if (val) {
  318. dev_dbg(dev, "TjMax is %d degrees C\n", val);
  319. return val * 1000;
  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. * An assumption is made for early CPUs and unreadable MSR.
  329. * NOTE: the calculated value may not be correct.
  330. */
  331. return adjust_tjmax(c, id, dev);
  332. }
  333. static int create_core_attrs(struct temp_data *tdata, struct device *dev,
  334. int attr_no)
  335. {
  336. int i;
  337. static ssize_t (*const rd_ptr[TOTAL_ATTRS]) (struct device *dev,
  338. struct device_attribute *devattr, char *buf) = {
  339. show_label, show_crit_alarm, show_temp, show_tjmax,
  340. show_ttarget };
  341. static const char *const suffixes[TOTAL_ATTRS] = {
  342. "label", "crit_alarm", "input", "crit", "max"
  343. };
  344. for (i = 0; i < tdata->attr_size; i++) {
  345. snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH,
  346. "temp%d_%s", attr_no, suffixes[i]);
  347. sysfs_attr_init(&tdata->sd_attrs[i].dev_attr.attr);
  348. tdata->sd_attrs[i].dev_attr.attr.name = tdata->attr_name[i];
  349. tdata->sd_attrs[i].dev_attr.attr.mode = 0444;
  350. tdata->sd_attrs[i].dev_attr.show = rd_ptr[i];
  351. tdata->sd_attrs[i].index = attr_no;
  352. tdata->attrs[i] = &tdata->sd_attrs[i].dev_attr.attr;
  353. }
  354. tdata->attr_group.attrs = tdata->attrs;
  355. return sysfs_create_group(&dev->kobj, &tdata->attr_group);
  356. }
  357. static int chk_ucode_version(unsigned int cpu)
  358. {
  359. struct cpuinfo_x86 *c = &cpu_data(cpu);
  360. /*
  361. * Check if we have problem with errata AE18 of Core processors:
  362. * Readings might stop update when processor visited too deep sleep,
  363. * fixed for stepping D0 (6EC).
  364. */
  365. if (c->x86_model == 0xe && c->x86_stepping < 0xc && c->microcode < 0x39) {
  366. pr_err("Errata AE18 not fixed, update BIOS or microcode of the CPU!\n");
  367. return -ENODEV;
  368. }
  369. return 0;
  370. }
  371. static struct platform_device *coretemp_get_pdev(unsigned int cpu)
  372. {
  373. int id = topology_logical_die_id(cpu);
  374. if (id >= 0 && id < max_zones)
  375. return zone_devices[id];
  376. return NULL;
  377. }
  378. static struct temp_data *init_temp_data(unsigned int cpu, int pkg_flag)
  379. {
  380. struct temp_data *tdata;
  381. tdata = kzalloc(sizeof(struct temp_data), GFP_KERNEL);
  382. if (!tdata)
  383. return NULL;
  384. tdata->status_reg = pkg_flag ? MSR_IA32_PACKAGE_THERM_STATUS :
  385. MSR_IA32_THERM_STATUS;
  386. tdata->is_pkg_data = pkg_flag;
  387. tdata->cpu = cpu;
  388. tdata->cpu_core_id = topology_core_id(cpu);
  389. tdata->attr_size = MAX_CORE_ATTRS;
  390. mutex_init(&tdata->update_lock);
  391. return tdata;
  392. }
  393. static int create_core_data(struct platform_device *pdev, unsigned int cpu,
  394. int pkg_flag)
  395. {
  396. struct temp_data *tdata;
  397. struct platform_data *pdata = platform_get_drvdata(pdev);
  398. struct cpuinfo_x86 *c = &cpu_data(cpu);
  399. u32 eax, edx;
  400. int err, index, attr_no;
  401. /*
  402. * Find attr number for sysfs:
  403. * We map the attr number to core id of the CPU
  404. * The attr number is always core id + 2
  405. * The Pkgtemp will always show up as temp1_*, if available
  406. */
  407. if (pkg_flag) {
  408. attr_no = PKG_SYSFS_ATTR_NO;
  409. } else {
  410. index = ida_alloc(&pdata->ida, GFP_KERNEL);
  411. if (index < 0)
  412. return index;
  413. pdata->cpu_map[index] = topology_core_id(cpu);
  414. attr_no = index + BASE_SYSFS_ATTR_NO;
  415. }
  416. if (attr_no > MAX_CORE_DATA - 1) {
  417. err = -ERANGE;
  418. goto ida_free;
  419. }
  420. tdata = init_temp_data(cpu, pkg_flag);
  421. if (!tdata) {
  422. err = -ENOMEM;
  423. goto ida_free;
  424. }
  425. /* Test if we can access the status register */
  426. err = rdmsr_safe_on_cpu(cpu, tdata->status_reg, &eax, &edx);
  427. if (err)
  428. goto exit_free;
  429. /* We can access status register. Get Critical Temperature */
  430. tdata->tjmax = get_tjmax(c, cpu, &pdev->dev);
  431. /*
  432. * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
  433. * The target temperature is available on older CPUs but not in this
  434. * register. Atoms don't have the register at all.
  435. */
  436. if (c->x86_model > 0xe && c->x86_model != 0x1c) {
  437. err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET,
  438. &eax, &edx);
  439. if (!err) {
  440. tdata->ttarget
  441. = tdata->tjmax - ((eax >> 8) & 0xff) * 1000;
  442. tdata->attr_size++;
  443. }
  444. }
  445. pdata->core_data[attr_no] = tdata;
  446. /* Create sysfs interfaces */
  447. err = create_core_attrs(tdata, pdata->hwmon_dev, attr_no);
  448. if (err)
  449. goto exit_free;
  450. return 0;
  451. exit_free:
  452. pdata->core_data[attr_no] = NULL;
  453. kfree(tdata);
  454. ida_free:
  455. if (!pkg_flag)
  456. ida_free(&pdata->ida, index);
  457. return err;
  458. }
  459. static void
  460. coretemp_add_core(struct platform_device *pdev, unsigned int cpu, int pkg_flag)
  461. {
  462. if (create_core_data(pdev, cpu, pkg_flag))
  463. dev_err(&pdev->dev, "Adding Core %u failed\n", cpu);
  464. }
  465. static void coretemp_remove_core(struct platform_data *pdata, int indx)
  466. {
  467. struct temp_data *tdata = pdata->core_data[indx];
  468. /* if we errored on add then this is already gone */
  469. if (!tdata)
  470. return;
  471. /* Remove the sysfs attributes */
  472. sysfs_remove_group(&pdata->hwmon_dev->kobj, &tdata->attr_group);
  473. kfree(pdata->core_data[indx]);
  474. pdata->core_data[indx] = NULL;
  475. if (indx >= BASE_SYSFS_ATTR_NO)
  476. ida_free(&pdata->ida, indx - BASE_SYSFS_ATTR_NO);
  477. }
  478. static int coretemp_device_add(int zoneid)
  479. {
  480. struct platform_device *pdev;
  481. struct platform_data *pdata;
  482. int err;
  483. /* Initialize the per-zone data structures */
  484. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  485. if (!pdata)
  486. return -ENOMEM;
  487. pdata->pkg_id = zoneid;
  488. ida_init(&pdata->ida);
  489. pdev = platform_device_alloc(DRVNAME, zoneid);
  490. if (!pdev) {
  491. err = -ENOMEM;
  492. goto err_free_pdata;
  493. }
  494. err = platform_device_add(pdev);
  495. if (err)
  496. goto err_put_dev;
  497. platform_set_drvdata(pdev, pdata);
  498. zone_devices[zoneid] = pdev;
  499. return 0;
  500. err_put_dev:
  501. platform_device_put(pdev);
  502. err_free_pdata:
  503. kfree(pdata);
  504. return err;
  505. }
  506. static void coretemp_device_remove(int zoneid)
  507. {
  508. struct platform_device *pdev = zone_devices[zoneid];
  509. struct platform_data *pdata = platform_get_drvdata(pdev);
  510. ida_destroy(&pdata->ida);
  511. kfree(pdata);
  512. platform_device_unregister(pdev);
  513. }
  514. static int coretemp_cpu_online(unsigned int cpu)
  515. {
  516. struct platform_device *pdev = coretemp_get_pdev(cpu);
  517. struct cpuinfo_x86 *c = &cpu_data(cpu);
  518. struct platform_data *pdata;
  519. /*
  520. * Don't execute this on resume as the offline callback did
  521. * not get executed on suspend.
  522. */
  523. if (cpuhp_tasks_frozen)
  524. return 0;
  525. /*
  526. * CPUID.06H.EAX[0] indicates whether the CPU has thermal
  527. * sensors. We check this bit only, all the early CPUs
  528. * without thermal sensors will be filtered out.
  529. */
  530. if (!cpu_has(c, X86_FEATURE_DTHERM))
  531. return -ENODEV;
  532. pdata = platform_get_drvdata(pdev);
  533. if (!pdata->hwmon_dev) {
  534. struct device *hwmon;
  535. /* Check the microcode version of the CPU */
  536. if (chk_ucode_version(cpu))
  537. return -EINVAL;
  538. /*
  539. * Alright, we have DTS support.
  540. * We are bringing the _first_ core in this pkg
  541. * online. So, initialize per-pkg data structures and
  542. * then bring this core online.
  543. */
  544. hwmon = hwmon_device_register_with_groups(&pdev->dev, DRVNAME,
  545. pdata, NULL);
  546. if (IS_ERR(hwmon))
  547. return PTR_ERR(hwmon);
  548. pdata->hwmon_dev = hwmon;
  549. /*
  550. * Check whether pkgtemp support is available.
  551. * If so, add interfaces for pkgtemp.
  552. */
  553. if (cpu_has(c, X86_FEATURE_PTS))
  554. coretemp_add_core(pdev, cpu, 1);
  555. }
  556. /*
  557. * Check whether a thread sibling is already online. If not add the
  558. * interface for this CPU core.
  559. */
  560. if (!cpumask_intersects(&pdata->cpumask, topology_sibling_cpumask(cpu)))
  561. coretemp_add_core(pdev, cpu, 0);
  562. cpumask_set_cpu(cpu, &pdata->cpumask);
  563. return 0;
  564. }
  565. static int coretemp_cpu_offline(unsigned int cpu)
  566. {
  567. struct platform_device *pdev = coretemp_get_pdev(cpu);
  568. struct platform_data *pd;
  569. struct temp_data *tdata;
  570. int i, indx = -1, target;
  571. /* No need to tear down any interfaces for suspend */
  572. if (cpuhp_tasks_frozen)
  573. return 0;
  574. /* If the physical CPU device does not exist, just return */
  575. pd = platform_get_drvdata(pdev);
  576. if (!pd->hwmon_dev)
  577. return 0;
  578. for (i = 0; i < NUM_REAL_CORES; i++) {
  579. if (pd->cpu_map[i] == topology_core_id(cpu)) {
  580. indx = i + BASE_SYSFS_ATTR_NO;
  581. break;
  582. }
  583. }
  584. /* Too many cores and this core is not populated, just return */
  585. if (indx < 0)
  586. return 0;
  587. tdata = pd->core_data[indx];
  588. cpumask_clear_cpu(cpu, &pd->cpumask);
  589. /*
  590. * If this is the last thread sibling, remove the CPU core
  591. * interface, If there is still a sibling online, transfer the
  592. * target cpu of that core interface to it.
  593. */
  594. target = cpumask_any_and(&pd->cpumask, topology_sibling_cpumask(cpu));
  595. if (target >= nr_cpu_ids) {
  596. coretemp_remove_core(pd, indx);
  597. } else if (tdata && tdata->cpu == cpu) {
  598. mutex_lock(&tdata->update_lock);
  599. tdata->cpu = target;
  600. mutex_unlock(&tdata->update_lock);
  601. }
  602. /*
  603. * If all cores in this pkg are offline, remove the interface.
  604. */
  605. tdata = pd->core_data[PKG_SYSFS_ATTR_NO];
  606. if (cpumask_empty(&pd->cpumask)) {
  607. if (tdata)
  608. coretemp_remove_core(pd, PKG_SYSFS_ATTR_NO);
  609. hwmon_device_unregister(pd->hwmon_dev);
  610. pd->hwmon_dev = NULL;
  611. return 0;
  612. }
  613. /*
  614. * Check whether this core is the target for the package
  615. * interface. We need to assign it to some other cpu.
  616. */
  617. if (tdata && tdata->cpu == cpu) {
  618. target = cpumask_first(&pd->cpumask);
  619. mutex_lock(&tdata->update_lock);
  620. tdata->cpu = target;
  621. mutex_unlock(&tdata->update_lock);
  622. }
  623. return 0;
  624. }
  625. static const struct x86_cpu_id __initconst coretemp_ids[] = {
  626. X86_MATCH_VENDOR_FEATURE(INTEL, X86_FEATURE_DTHERM, NULL),
  627. {}
  628. };
  629. MODULE_DEVICE_TABLE(x86cpu, coretemp_ids);
  630. static enum cpuhp_state coretemp_hp_online;
  631. static int __init coretemp_init(void)
  632. {
  633. int i, err;
  634. /*
  635. * CPUID.06H.EAX[0] indicates whether the CPU has thermal
  636. * sensors. We check this bit only, all the early CPUs
  637. * without thermal sensors will be filtered out.
  638. */
  639. if (!x86_match_cpu(coretemp_ids))
  640. return -ENODEV;
  641. max_zones = topology_max_packages() * topology_max_die_per_package();
  642. zone_devices = kcalloc(max_zones, sizeof(struct platform_device *),
  643. GFP_KERNEL);
  644. if (!zone_devices)
  645. return -ENOMEM;
  646. for (i = 0; i < max_zones; i++) {
  647. err = coretemp_device_add(i);
  648. if (err)
  649. goto outzone;
  650. }
  651. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hwmon/coretemp:online",
  652. coretemp_cpu_online, coretemp_cpu_offline);
  653. if (err < 0)
  654. goto outzone;
  655. coretemp_hp_online = err;
  656. return 0;
  657. outzone:
  658. while (i--)
  659. coretemp_device_remove(i);
  660. kfree(zone_devices);
  661. return err;
  662. }
  663. module_init(coretemp_init)
  664. static void __exit coretemp_exit(void)
  665. {
  666. int i;
  667. cpuhp_remove_state(coretemp_hp_online);
  668. for (i = 0; i < max_zones; i++)
  669. coretemp_device_remove(i);
  670. kfree(zone_devices);
  671. }
  672. module_exit(coretemp_exit)
  673. MODULE_AUTHOR("Rudolf Marek <[email protected]>");
  674. MODULE_DESCRIPTION("Intel Core temperature monitor");
  675. MODULE_LICENSE("GPL");