coretemp.c 21 KB

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