coretemp.c 22 KB

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