coretemp.c 21 KB

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