coretemp.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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 BASE_SYSFS_ATTR_NO 2 /* Sysfs Base attr no for coretemp */
  50. #define NUM_REAL_CORES 32 /* Number of Real cores per cpu */
  51. #define CORETEMP_NAME_LENGTH 19 /* String Length of attrs */
  52. #define MAX_CORE_ATTRS 4 /* Maximum no of basic attrs */
  53. #define TOTAL_ATTRS (MAX_CORE_ATTRS + 1)
  54. #define MAX_CORE_DATA (NUM_REAL_CORES + BASE_SYSFS_ATTR_NO)
  55. #define TO_PHYS_ID(cpu) (cpu_data(cpu).phys_proc_id)
  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) for_each_cpu(i, cpu_sibling_mask(cpu))
  60. #else
  61. #define for_each_sibling(i, cpu) for (i = 0; false; )
  62. #endif
  63. /*
  64. * Per-Core Temperature Data
  65. * @last_updated: The time when the current temperature value was updated
  66. * earlier (in jiffies).
  67. * @cpu_core_id: The CPU Core from which temperature values should be read
  68. * This value is passed as "id" field to rdmsr/wrmsr functions.
  69. * @status_reg: One of IA32_THERM_STATUS or IA32_PACKAGE_THERM_STATUS,
  70. * from where the temperature values should be read.
  71. * @attr_size: Total number of pre-core attrs displayed in the sysfs.
  72. * @is_pkg_data: If this is 1, the temp_data holds pkgtemp data.
  73. * Otherwise, temp_data holds coretemp data.
  74. * @valid: If this is 1, the current temperature is valid.
  75. */
  76. struct temp_data {
  77. int temp;
  78. int ttarget;
  79. int tjmax;
  80. unsigned long last_updated;
  81. unsigned int cpu;
  82. u32 cpu_core_id;
  83. u32 status_reg;
  84. int attr_size;
  85. bool is_pkg_data;
  86. bool valid;
  87. struct sensor_device_attribute sd_attrs[TOTAL_ATTRS];
  88. char attr_name[TOTAL_ATTRS][CORETEMP_NAME_LENGTH];
  89. struct mutex update_lock;
  90. };
  91. /* Platform Data per Physical CPU */
  92. struct platform_data {
  93. struct device *hwmon_dev;
  94. u16 phys_proc_id;
  95. struct temp_data *core_data[MAX_CORE_DATA];
  96. struct device_attribute name_attr;
  97. };
  98. struct pdev_entry {
  99. struct list_head list;
  100. struct platform_device *pdev;
  101. u16 phys_proc_id;
  102. };
  103. static LIST_HEAD(pdev_list);
  104. static DEFINE_MUTEX(pdev_list_mutex);
  105. static ssize_t show_name(struct device *dev,
  106. struct device_attribute *devattr, char *buf)
  107. {
  108. return sprintf(buf, "%s\n", DRVNAME);
  109. }
  110. static ssize_t show_label(struct device *dev,
  111. struct device_attribute *devattr, char *buf)
  112. {
  113. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  114. struct platform_data *pdata = dev_get_drvdata(dev);
  115. struct temp_data *tdata = pdata->core_data[attr->index];
  116. if (tdata->is_pkg_data)
  117. return sprintf(buf, "Physical id %u\n", pdata->phys_proc_id);
  118. return sprintf(buf, "Core %u\n", tdata->cpu_core_id);
  119. }
  120. static ssize_t show_crit_alarm(struct device *dev,
  121. struct device_attribute *devattr, char *buf)
  122. {
  123. u32 eax, edx;
  124. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  125. struct platform_data *pdata = dev_get_drvdata(dev);
  126. struct temp_data *tdata = pdata->core_data[attr->index];
  127. rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
  128. return sprintf(buf, "%d\n", (eax >> 5) & 1);
  129. }
  130. static ssize_t show_tjmax(struct device *dev,
  131. struct device_attribute *devattr, char *buf)
  132. {
  133. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  134. struct platform_data *pdata = dev_get_drvdata(dev);
  135. return sprintf(buf, "%d\n", pdata->core_data[attr->index]->tjmax);
  136. }
  137. static ssize_t show_ttarget(struct device *dev,
  138. struct device_attribute *devattr, char *buf)
  139. {
  140. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  141. struct platform_data *pdata = dev_get_drvdata(dev);
  142. return sprintf(buf, "%d\n", pdata->core_data[attr->index]->ttarget);
  143. }
  144. static ssize_t show_temp(struct device *dev,
  145. struct device_attribute *devattr, char *buf)
  146. {
  147. u32 eax, edx;
  148. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  149. struct platform_data *pdata = dev_get_drvdata(dev);
  150. struct temp_data *tdata = pdata->core_data[attr->index];
  151. mutex_lock(&tdata->update_lock);
  152. /* Check whether the time interval has elapsed */
  153. if (!tdata->valid || time_after(jiffies, tdata->last_updated + HZ)) {
  154. rdmsr_on_cpu(tdata->cpu, tdata->status_reg, &eax, &edx);
  155. /*
  156. * Ignore the valid bit. In all observed cases the register
  157. * value is either low or zero if the valid bit is 0.
  158. * Return it instead of reporting an error which doesn't
  159. * really help at all.
  160. */
  161. tdata->temp = tdata->tjmax - ((eax >> 16) & 0x7f) * 1000;
  162. tdata->valid = 1;
  163. tdata->last_updated = jiffies;
  164. }
  165. mutex_unlock(&tdata->update_lock);
  166. return sprintf(buf, "%d\n", tdata->temp);
  167. }
  168. struct tjmax_pci {
  169. unsigned int device;
  170. int tjmax;
  171. };
  172. static const struct tjmax_pci tjmax_pci_table[] = {
  173. { 0x0708, 110000 }, /* CE41x0 (Sodaville ) */
  174. { 0x0c72, 102000 }, /* Atom S1240 (Centerton) */
  175. { 0x0c73, 95000 }, /* Atom S1220 (Centerton) */
  176. { 0x0c75, 95000 }, /* Atom S1260 (Centerton) */
  177. };
  178. struct tjmax {
  179. char const *id;
  180. int tjmax;
  181. };
  182. static const struct tjmax tjmax_table[] = {
  183. { "CPU 230", 100000 }, /* Model 0x1c, stepping 2 */
  184. { "CPU 330", 125000 }, /* Model 0x1c, stepping 2 */
  185. };
  186. struct tjmax_model {
  187. u8 model;
  188. u8 mask;
  189. int tjmax;
  190. };
  191. #define ANY 0xff
  192. static const struct tjmax_model tjmax_model_table[] = {
  193. { 0x1c, 10, 100000 }, /* D4xx, K4xx, N4xx, D5xx, K5xx, N5xx */
  194. { 0x1c, ANY, 90000 }, /* Z5xx, N2xx, possibly others
  195. * Note: Also matches 230 and 330,
  196. * which are covered by tjmax_table
  197. */
  198. { 0x26, ANY, 90000 }, /* Atom Tunnel Creek (Exx), Lincroft (Z6xx)
  199. * Note: TjMax for E6xxT is 110C, but CPU type
  200. * is undetectable by software
  201. */
  202. { 0x27, ANY, 90000 }, /* Atom Medfield (Z2460) */
  203. { 0x35, ANY, 90000 }, /* Atom Clover Trail/Cloverview (Z27x0) */
  204. { 0x36, ANY, 100000 }, /* Atom Cedar Trail/Cedarview (N2xxx, D2xxx)
  205. * Also matches S12x0 (stepping 9), covered by
  206. * PCI table
  207. */
  208. };
  209. static int adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
  210. {
  211. /* The 100C is default for both mobile and non mobile CPUs */
  212. int tjmax = 100000;
  213. int tjmax_ee = 85000;
  214. int usemsr_ee = 1;
  215. int err;
  216. u32 eax, edx;
  217. int i;
  218. struct pci_dev *host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  219. /*
  220. * Explicit tjmax table entries override heuristics.
  221. * First try PCI host bridge IDs, followed by model ID strings
  222. * and model/stepping information.
  223. */
  224. if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL) {
  225. for (i = 0; i < ARRAY_SIZE(tjmax_pci_table); i++) {
  226. if (host_bridge->device == tjmax_pci_table[i].device)
  227. return tjmax_pci_table[i].tjmax;
  228. }
  229. }
  230. for (i = 0; i < ARRAY_SIZE(tjmax_table); i++) {
  231. if (strstr(c->x86_model_id, tjmax_table[i].id))
  232. return tjmax_table[i].tjmax;
  233. }
  234. for (i = 0; i < ARRAY_SIZE(tjmax_model_table); i++) {
  235. const struct tjmax_model *tm = &tjmax_model_table[i];
  236. if (c->x86_model == tm->model &&
  237. (tm->mask == ANY || c->x86_mask == tm->mask))
  238. return tm->tjmax;
  239. }
  240. /* Early chips have no MSR for TjMax */
  241. if (c->x86_model == 0xf && c->x86_mask < 4)
  242. usemsr_ee = 0;
  243. if (c->x86_model > 0xe && usemsr_ee) {
  244. u8 platform_id;
  245. /*
  246. * Now we can detect the mobile CPU using Intel provided table
  247. * http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
  248. * For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
  249. */
  250. err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
  251. if (err) {
  252. dev_warn(dev,
  253. "Unable to access MSR 0x17, assuming desktop"
  254. " CPU\n");
  255. usemsr_ee = 0;
  256. } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
  257. /*
  258. * Trust bit 28 up to Penryn, I could not find any
  259. * documentation on that; if you happen to know
  260. * someone at Intel please ask
  261. */
  262. usemsr_ee = 0;
  263. } else {
  264. /* Platform ID bits 52:50 (EDX starts at bit 32) */
  265. platform_id = (edx >> 18) & 0x7;
  266. /*
  267. * Mobile Penryn CPU seems to be platform ID 7 or 5
  268. * (guesswork)
  269. */
  270. if (c->x86_model == 0x17 &&
  271. (platform_id == 5 || platform_id == 7)) {
  272. /*
  273. * If MSR EE bit is set, set it to 90 degrees C,
  274. * otherwise 105 degrees C
  275. */
  276. tjmax_ee = 90000;
  277. tjmax = 105000;
  278. }
  279. }
  280. }
  281. if (usemsr_ee) {
  282. err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
  283. if (err) {
  284. dev_warn(dev,
  285. "Unable to access MSR 0xEE, for Tjmax, left"
  286. " at default\n");
  287. } else if (eax & 0x40000000) {
  288. tjmax = tjmax_ee;
  289. }
  290. } else if (tjmax == 100000) {
  291. /*
  292. * If we don't use msr EE it means we are desktop CPU
  293. * (with exeception of Atom)
  294. */
  295. dev_warn(dev, "Using relative temperature scale!\n");
  296. }
  297. return tjmax;
  298. }
  299. static bool cpu_has_tjmax(struct cpuinfo_x86 *c)
  300. {
  301. u8 model = c->x86_model;
  302. return model > 0xe &&
  303. model != 0x1c &&
  304. model != 0x26 &&
  305. model != 0x27 &&
  306. model != 0x35 &&
  307. model != 0x36;
  308. }
  309. static int get_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
  310. {
  311. int err;
  312. u32 eax, edx;
  313. u32 val;
  314. /*
  315. * A new feature of current Intel(R) processors, the
  316. * IA32_TEMPERATURE_TARGET contains the TjMax value
  317. */
  318. err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  319. if (err) {
  320. if (cpu_has_tjmax(c))
  321. dev_warn(dev, "Unable to read TjMax from CPU %u\n", id);
  322. } else {
  323. val = (eax >> 16) & 0x7f;
  324. /*
  325. * If the TjMax is not plausible, an assumption
  326. * will be used
  327. */
  328. if (val >= 85) {
  329. dev_dbg(dev, "TjMax is %d degrees C\n", val);
  330. return val * 1000;
  331. }
  332. }
  333. if (force_tjmax) {
  334. dev_notice(dev, "TjMax forced to %d degrees C by user\n",
  335. force_tjmax);
  336. return force_tjmax * 1000;
  337. }
  338. /*
  339. * An assumption is made for early CPUs and unreadable MSR.
  340. * NOTE: the calculated value may not be correct.
  341. */
  342. return adjust_tjmax(c, id, dev);
  343. }
  344. static int create_name_attr(struct platform_data *pdata,
  345. struct device *dev)
  346. {
  347. sysfs_attr_init(&pdata->name_attr.attr);
  348. pdata->name_attr.attr.name = "name";
  349. pdata->name_attr.attr.mode = S_IRUGO;
  350. pdata->name_attr.show = show_name;
  351. return device_create_file(dev, &pdata->name_attr);
  352. }
  353. static int create_core_attrs(struct temp_data *tdata, struct device *dev,
  354. int attr_no)
  355. {
  356. int err, i;
  357. static ssize_t (*const rd_ptr[TOTAL_ATTRS]) (struct device *dev,
  358. struct device_attribute *devattr, char *buf) = {
  359. show_label, show_crit_alarm, show_temp, show_tjmax,
  360. show_ttarget };
  361. static const char *const names[TOTAL_ATTRS] = {
  362. "temp%d_label", "temp%d_crit_alarm",
  363. "temp%d_input", "temp%d_crit",
  364. "temp%d_max" };
  365. for (i = 0; i < tdata->attr_size; i++) {
  366. snprintf(tdata->attr_name[i], CORETEMP_NAME_LENGTH, names[i],
  367. attr_no);
  368. sysfs_attr_init(&tdata->sd_attrs[i].dev_attr.attr);
  369. tdata->sd_attrs[i].dev_attr.attr.name = tdata->attr_name[i];
  370. tdata->sd_attrs[i].dev_attr.attr.mode = S_IRUGO;
  371. tdata->sd_attrs[i].dev_attr.show = rd_ptr[i];
  372. tdata->sd_attrs[i].index = attr_no;
  373. err = device_create_file(dev, &tdata->sd_attrs[i].dev_attr);
  374. if (err)
  375. goto exit_free;
  376. }
  377. return 0;
  378. exit_free:
  379. while (--i >= 0)
  380. device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
  381. return err;
  382. }
  383. static int chk_ucode_version(unsigned int cpu)
  384. {
  385. struct cpuinfo_x86 *c = &cpu_data(cpu);
  386. /*
  387. * Check if we have problem with errata AE18 of Core processors:
  388. * Readings might stop update when processor visited too deep sleep,
  389. * fixed for stepping D0 (6EC).
  390. */
  391. if (c->x86_model == 0xe && c->x86_mask < 0xc && c->microcode < 0x39) {
  392. pr_err("Errata AE18 not fixed, update BIOS or microcode of the CPU!\n");
  393. return -ENODEV;
  394. }
  395. return 0;
  396. }
  397. static struct platform_device *coretemp_get_pdev(unsigned int cpu)
  398. {
  399. u16 phys_proc_id = TO_PHYS_ID(cpu);
  400. struct pdev_entry *p;
  401. mutex_lock(&pdev_list_mutex);
  402. list_for_each_entry(p, &pdev_list, list)
  403. if (p->phys_proc_id == phys_proc_id) {
  404. mutex_unlock(&pdev_list_mutex);
  405. return p->pdev;
  406. }
  407. mutex_unlock(&pdev_list_mutex);
  408. return NULL;
  409. }
  410. static struct temp_data *init_temp_data(unsigned int cpu, int pkg_flag)
  411. {
  412. struct temp_data *tdata;
  413. tdata = kzalloc(sizeof(struct temp_data), GFP_KERNEL);
  414. if (!tdata)
  415. return NULL;
  416. tdata->status_reg = pkg_flag ? MSR_IA32_PACKAGE_THERM_STATUS :
  417. MSR_IA32_THERM_STATUS;
  418. tdata->is_pkg_data = pkg_flag;
  419. tdata->cpu = cpu;
  420. tdata->cpu_core_id = TO_CORE_ID(cpu);
  421. tdata->attr_size = MAX_CORE_ATTRS;
  422. mutex_init(&tdata->update_lock);
  423. return tdata;
  424. }
  425. static int create_core_data(struct platform_device *pdev, unsigned int cpu,
  426. int pkg_flag)
  427. {
  428. struct temp_data *tdata;
  429. struct platform_data *pdata = platform_get_drvdata(pdev);
  430. struct cpuinfo_x86 *c = &cpu_data(cpu);
  431. u32 eax, edx;
  432. int err, attr_no;
  433. /*
  434. * Find attr number for sysfs:
  435. * We map the attr number to core id of the CPU
  436. * The attr number is always core id + 2
  437. * The Pkgtemp will always show up as temp1_*, if available
  438. */
  439. attr_no = pkg_flag ? 1 : TO_ATTR_NO(cpu);
  440. if (attr_no > MAX_CORE_DATA - 1)
  441. return -ERANGE;
  442. /*
  443. * Provide a single set of attributes for all HT siblings of a core
  444. * to avoid duplicate sensors (the processor ID and core ID of all
  445. * HT siblings of a core are the same).
  446. * Skip if a HT sibling of this core is already registered.
  447. * This is not an error.
  448. */
  449. if (pdata->core_data[attr_no] != NULL)
  450. return 0;
  451. tdata = init_temp_data(cpu, pkg_flag);
  452. if (!tdata)
  453. return -ENOMEM;
  454. /* Test if we can access the status register */
  455. err = rdmsr_safe_on_cpu(cpu, tdata->status_reg, &eax, &edx);
  456. if (err)
  457. goto exit_free;
  458. /* We can access status register. Get Critical Temperature */
  459. tdata->tjmax = get_tjmax(c, cpu, &pdev->dev);
  460. /*
  461. * Read the still undocumented bits 8:15 of IA32_TEMPERATURE_TARGET.
  462. * The target temperature is available on older CPUs but not in this
  463. * register. Atoms don't have the register at all.
  464. */
  465. if (c->x86_model > 0xe && c->x86_model != 0x1c) {
  466. err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET,
  467. &eax, &edx);
  468. if (!err) {
  469. tdata->ttarget
  470. = tdata->tjmax - ((eax >> 8) & 0xff) * 1000;
  471. tdata->attr_size++;
  472. }
  473. }
  474. pdata->core_data[attr_no] = tdata;
  475. /* Create sysfs interfaces */
  476. err = create_core_attrs(tdata, &pdev->dev, attr_no);
  477. if (err)
  478. goto exit_free;
  479. return 0;
  480. exit_free:
  481. pdata->core_data[attr_no] = NULL;
  482. kfree(tdata);
  483. return err;
  484. }
  485. static void coretemp_add_core(unsigned int cpu, int pkg_flag)
  486. {
  487. struct platform_device *pdev = coretemp_get_pdev(cpu);
  488. int err;
  489. if (!pdev)
  490. return;
  491. err = create_core_data(pdev, cpu, pkg_flag);
  492. if (err)
  493. dev_err(&pdev->dev, "Adding Core %u failed\n", cpu);
  494. }
  495. static void coretemp_remove_core(struct platform_data *pdata,
  496. struct device *dev, int indx)
  497. {
  498. int i;
  499. struct temp_data *tdata = pdata->core_data[indx];
  500. /* Remove the sysfs attributes */
  501. for (i = 0; i < tdata->attr_size; i++)
  502. device_remove_file(dev, &tdata->sd_attrs[i].dev_attr);
  503. kfree(pdata->core_data[indx]);
  504. pdata->core_data[indx] = NULL;
  505. }
  506. static int coretemp_probe(struct platform_device *pdev)
  507. {
  508. struct platform_data *pdata;
  509. int err;
  510. /* Initialize the per-package data structures */
  511. pdata = kzalloc(sizeof(struct platform_data), GFP_KERNEL);
  512. if (!pdata)
  513. return -ENOMEM;
  514. err = create_name_attr(pdata, &pdev->dev);
  515. if (err)
  516. goto exit_free;
  517. pdata->phys_proc_id = pdev->id;
  518. platform_set_drvdata(pdev, pdata);
  519. pdata->hwmon_dev = hwmon_device_register(&pdev->dev);
  520. if (IS_ERR(pdata->hwmon_dev)) {
  521. err = PTR_ERR(pdata->hwmon_dev);
  522. dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
  523. goto exit_name;
  524. }
  525. return 0;
  526. exit_name:
  527. device_remove_file(&pdev->dev, &pdata->name_attr);
  528. exit_free:
  529. kfree(pdata);
  530. return err;
  531. }
  532. static int coretemp_remove(struct platform_device *pdev)
  533. {
  534. struct platform_data *pdata = platform_get_drvdata(pdev);
  535. int i;
  536. for (i = MAX_CORE_DATA - 1; i >= 0; --i)
  537. if (pdata->core_data[i])
  538. coretemp_remove_core(pdata, &pdev->dev, i);
  539. device_remove_file(&pdev->dev, &pdata->name_attr);
  540. hwmon_device_unregister(pdata->hwmon_dev);
  541. kfree(pdata);
  542. return 0;
  543. }
  544. static struct platform_driver coretemp_driver = {
  545. .driver = {
  546. .owner = THIS_MODULE,
  547. .name = DRVNAME,
  548. },
  549. .probe = coretemp_probe,
  550. .remove = coretemp_remove,
  551. };
  552. static int coretemp_device_add(unsigned int cpu)
  553. {
  554. int err;
  555. struct platform_device *pdev;
  556. struct pdev_entry *pdev_entry;
  557. mutex_lock(&pdev_list_mutex);
  558. pdev = platform_device_alloc(DRVNAME, TO_PHYS_ID(cpu));
  559. if (!pdev) {
  560. err = -ENOMEM;
  561. pr_err("Device allocation failed\n");
  562. goto exit;
  563. }
  564. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  565. if (!pdev_entry) {
  566. err = -ENOMEM;
  567. goto exit_device_put;
  568. }
  569. err = platform_device_add(pdev);
  570. if (err) {
  571. pr_err("Device addition failed (%d)\n", err);
  572. goto exit_device_free;
  573. }
  574. pdev_entry->pdev = pdev;
  575. pdev_entry->phys_proc_id = pdev->id;
  576. list_add_tail(&pdev_entry->list, &pdev_list);
  577. mutex_unlock(&pdev_list_mutex);
  578. return 0;
  579. exit_device_free:
  580. kfree(pdev_entry);
  581. exit_device_put:
  582. platform_device_put(pdev);
  583. exit:
  584. mutex_unlock(&pdev_list_mutex);
  585. return err;
  586. }
  587. static void coretemp_device_remove(unsigned int cpu)
  588. {
  589. struct pdev_entry *p, *n;
  590. u16 phys_proc_id = TO_PHYS_ID(cpu);
  591. mutex_lock(&pdev_list_mutex);
  592. list_for_each_entry_safe(p, n, &pdev_list, list) {
  593. if (p->phys_proc_id != phys_proc_id)
  594. continue;
  595. platform_device_unregister(p->pdev);
  596. list_del(&p->list);
  597. kfree(p);
  598. }
  599. mutex_unlock(&pdev_list_mutex);
  600. }
  601. static bool is_any_core_online(struct platform_data *pdata)
  602. {
  603. int i;
  604. /* Find online cores, except pkgtemp data */
  605. for (i = MAX_CORE_DATA - 1; i >= 0; --i) {
  606. if (pdata->core_data[i] &&
  607. !pdata->core_data[i]->is_pkg_data) {
  608. return true;
  609. }
  610. }
  611. return false;
  612. }
  613. static void get_core_online(unsigned int cpu)
  614. {
  615. struct cpuinfo_x86 *c = &cpu_data(cpu);
  616. struct platform_device *pdev = coretemp_get_pdev(cpu);
  617. int err;
  618. /*
  619. * CPUID.06H.EAX[0] indicates whether the CPU has thermal
  620. * sensors. We check this bit only, all the early CPUs
  621. * without thermal sensors will be filtered out.
  622. */
  623. if (!cpu_has(c, X86_FEATURE_DTHERM))
  624. return;
  625. if (!pdev) {
  626. /* Check the microcode version of the CPU */
  627. if (chk_ucode_version(cpu))
  628. return;
  629. /*
  630. * Alright, we have DTS support.
  631. * We are bringing the _first_ core in this pkg
  632. * online. So, initialize per-pkg data structures and
  633. * then bring this core online.
  634. */
  635. err = coretemp_device_add(cpu);
  636. if (err)
  637. return;
  638. /*
  639. * Check whether pkgtemp support is available.
  640. * If so, add interfaces for pkgtemp.
  641. */
  642. if (cpu_has(c, X86_FEATURE_PTS))
  643. coretemp_add_core(cpu, 1);
  644. }
  645. /*
  646. * Physical CPU device already exists.
  647. * So, just add interfaces for this core.
  648. */
  649. coretemp_add_core(cpu, 0);
  650. }
  651. static void put_core_offline(unsigned int cpu)
  652. {
  653. int i, indx;
  654. struct platform_data *pdata;
  655. struct platform_device *pdev = coretemp_get_pdev(cpu);
  656. /* If the physical CPU device does not exist, just return */
  657. if (!pdev)
  658. return;
  659. pdata = platform_get_drvdata(pdev);
  660. indx = TO_ATTR_NO(cpu);
  661. /* The core id is too big, just return */
  662. if (indx > MAX_CORE_DATA - 1)
  663. return;
  664. if (pdata->core_data[indx] && pdata->core_data[indx]->cpu == cpu)
  665. coretemp_remove_core(pdata, &pdev->dev, indx);
  666. /*
  667. * If a HT sibling of a core is taken offline, but another HT sibling
  668. * of the same core is still online, register the alternate sibling.
  669. * This ensures that exactly one set of attributes is provided as long
  670. * as at least one HT sibling of a core is online.
  671. */
  672. for_each_sibling(i, cpu) {
  673. if (i != cpu) {
  674. get_core_online(i);
  675. /*
  676. * Display temperature sensor data for one HT sibling
  677. * per core only, so abort the loop after one such
  678. * sibling has been found.
  679. */
  680. break;
  681. }
  682. }
  683. /*
  684. * If all cores in this pkg are offline, remove the device.
  685. * coretemp_device_remove calls unregister_platform_device,
  686. * which in turn calls coretemp_remove. This removes the
  687. * pkgtemp entry and does other clean ups.
  688. */
  689. if (!is_any_core_online(pdata))
  690. coretemp_device_remove(cpu);
  691. }
  692. static int coretemp_cpu_callback(struct notifier_block *nfb,
  693. unsigned long action, void *hcpu)
  694. {
  695. unsigned int cpu = (unsigned long) hcpu;
  696. switch (action) {
  697. case CPU_ONLINE:
  698. case CPU_DOWN_FAILED:
  699. get_core_online(cpu);
  700. break;
  701. case CPU_DOWN_PREPARE:
  702. put_core_offline(cpu);
  703. break;
  704. }
  705. return NOTIFY_OK;
  706. }
  707. static struct notifier_block coretemp_cpu_notifier __refdata = {
  708. .notifier_call = coretemp_cpu_callback,
  709. };
  710. static const struct x86_cpu_id __initconst coretemp_ids[] = {
  711. { X86_VENDOR_INTEL, X86_FAMILY_ANY, X86_MODEL_ANY, X86_FEATURE_DTHERM },
  712. {}
  713. };
  714. MODULE_DEVICE_TABLE(x86cpu, coretemp_ids);
  715. static int __init coretemp_init(void)
  716. {
  717. int i, err;
  718. /*
  719. * CPUID.06H.EAX[0] indicates whether the CPU has thermal
  720. * sensors. We check this bit only, all the early CPUs
  721. * without thermal sensors will be filtered out.
  722. */
  723. if (!x86_match_cpu(coretemp_ids))
  724. return -ENODEV;
  725. err = platform_driver_register(&coretemp_driver);
  726. if (err)
  727. goto exit;
  728. get_online_cpus();
  729. for_each_online_cpu(i)
  730. get_core_online(i);
  731. #ifndef CONFIG_HOTPLUG_CPU
  732. if (list_empty(&pdev_list)) {
  733. put_online_cpus();
  734. err = -ENODEV;
  735. goto exit_driver_unreg;
  736. }
  737. #endif
  738. register_hotcpu_notifier(&coretemp_cpu_notifier);
  739. put_online_cpus();
  740. return 0;
  741. #ifndef CONFIG_HOTPLUG_CPU
  742. exit_driver_unreg:
  743. platform_driver_unregister(&coretemp_driver);
  744. #endif
  745. exit:
  746. return err;
  747. }
  748. static void __exit coretemp_exit(void)
  749. {
  750. struct pdev_entry *p, *n;
  751. get_online_cpus();
  752. unregister_hotcpu_notifier(&coretemp_cpu_notifier);
  753. mutex_lock(&pdev_list_mutex);
  754. list_for_each_entry_safe(p, n, &pdev_list, list) {
  755. platform_device_unregister(p->pdev);
  756. list_del(&p->list);
  757. kfree(p);
  758. }
  759. mutex_unlock(&pdev_list_mutex);
  760. put_online_cpus();
  761. platform_driver_unregister(&coretemp_driver);
  762. }
  763. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  764. MODULE_DESCRIPTION("Intel Core temperature monitor");
  765. MODULE_LICENSE("GPL");
  766. module_init(coretemp_init)
  767. module_exit(coretemp_exit)