coretemp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 <asm/msr.h>
  37. #include <asm/processor.h>
  38. #include <asm/smp.h>
  39. #define DRVNAME "coretemp"
  40. typedef enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL,
  41. SHOW_NAME } SHOW;
  42. /*
  43. * Functions declaration
  44. */
  45. static struct coretemp_data *coretemp_update_device(struct device *dev);
  46. struct coretemp_data {
  47. struct device *hwmon_dev;
  48. struct mutex update_lock;
  49. const char *name;
  50. u32 id;
  51. u16 core_id;
  52. char valid; /* zero until following fields are valid */
  53. unsigned long last_updated; /* in jiffies */
  54. int temp;
  55. int tjmax;
  56. int ttarget;
  57. u8 alarm;
  58. };
  59. /*
  60. * Sysfs stuff
  61. */
  62. static ssize_t show_name(struct device *dev, struct device_attribute
  63. *devattr, char *buf)
  64. {
  65. int ret;
  66. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  67. struct coretemp_data *data = dev_get_drvdata(dev);
  68. if (attr->index == SHOW_NAME)
  69. ret = sprintf(buf, "%s\n", data->name);
  70. else /* show label */
  71. ret = sprintf(buf, "Core %d\n", data->core_id);
  72. return ret;
  73. }
  74. static ssize_t show_alarm(struct device *dev, struct device_attribute
  75. *devattr, char *buf)
  76. {
  77. struct coretemp_data *data = coretemp_update_device(dev);
  78. /* read the Out-of-spec log, never clear */
  79. return sprintf(buf, "%d\n", data->alarm);
  80. }
  81. static ssize_t show_temp(struct device *dev,
  82. struct device_attribute *devattr, char *buf)
  83. {
  84. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  85. struct coretemp_data *data = coretemp_update_device(dev);
  86. int err;
  87. if (attr->index == SHOW_TEMP)
  88. err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN;
  89. else if (attr->index == SHOW_TJMAX)
  90. err = sprintf(buf, "%d\n", data->tjmax);
  91. else
  92. err = sprintf(buf, "%d\n", data->ttarget);
  93. return err;
  94. }
  95. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
  96. SHOW_TEMP);
  97. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
  98. SHOW_TJMAX);
  99. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL,
  100. SHOW_TTARGET);
  101. static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
  102. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
  103. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
  104. static struct attribute *coretemp_attributes[] = {
  105. &sensor_dev_attr_name.dev_attr.attr,
  106. &sensor_dev_attr_temp1_label.dev_attr.attr,
  107. &dev_attr_temp1_crit_alarm.attr,
  108. &sensor_dev_attr_temp1_input.dev_attr.attr,
  109. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  110. NULL
  111. };
  112. static const struct attribute_group coretemp_group = {
  113. .attrs = coretemp_attributes,
  114. };
  115. static struct coretemp_data *coretemp_update_device(struct device *dev)
  116. {
  117. struct coretemp_data *data = dev_get_drvdata(dev);
  118. mutex_lock(&data->update_lock);
  119. if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
  120. u32 eax, edx;
  121. data->valid = 0;
  122. rdmsr_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
  123. data->alarm = (eax >> 5) & 1;
  124. /* update only if data has been valid */
  125. if (eax & 0x80000000) {
  126. data->temp = data->tjmax - (((eax >> 16)
  127. & 0x7f) * 1000);
  128. data->valid = 1;
  129. } else {
  130. dev_dbg(dev, "Temperature data invalid (0x%x)\n", eax);
  131. }
  132. data->last_updated = jiffies;
  133. }
  134. mutex_unlock(&data->update_lock);
  135. return data;
  136. }
  137. static int __devinit adjust_tjmax(struct cpuinfo_x86 *c, u32 id, struct device *dev)
  138. {
  139. /* The 100C is default for both mobile and non mobile CPUs */
  140. int tjmax = 100000;
  141. int tjmax_ee = 85000;
  142. int usemsr_ee = 1;
  143. int err;
  144. u32 eax, edx;
  145. struct pci_dev *host_bridge;
  146. /* Early chips have no MSR for TjMax */
  147. if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
  148. usemsr_ee = 0;
  149. }
  150. /* Atom CPUs */
  151. if (c->x86_model == 0x1c) {
  152. usemsr_ee = 0;
  153. host_bridge = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0));
  154. if (host_bridge && host_bridge->vendor == PCI_VENDOR_ID_INTEL
  155. && (host_bridge->device == 0xa000 /* NM10 based nettop */
  156. || host_bridge->device == 0xa010)) /* NM10 based netbook */
  157. tjmax = 100000;
  158. else
  159. tjmax = 90000;
  160. pci_dev_put(host_bridge);
  161. }
  162. if ((c->x86_model > 0xe) && (usemsr_ee)) {
  163. u8 platform_id;
  164. /* Now we can detect the mobile CPU using Intel provided table
  165. http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
  166. For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
  167. */
  168. err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
  169. if (err) {
  170. dev_warn(dev,
  171. "Unable to access MSR 0x17, assuming desktop"
  172. " CPU\n");
  173. usemsr_ee = 0;
  174. } else if (c->x86_model < 0x17 && !(eax & 0x10000000)) {
  175. /* Trust bit 28 up to Penryn, I could not find any
  176. documentation on that; if you happen to know
  177. someone at Intel please ask */
  178. usemsr_ee = 0;
  179. } else {
  180. /* Platform ID bits 52:50 (EDX starts at bit 32) */
  181. platform_id = (edx >> 18) & 0x7;
  182. /* Mobile Penryn CPU seems to be platform ID 7 or 5
  183. (guesswork) */
  184. if ((c->x86_model == 0x17) &&
  185. ((platform_id == 5) || (platform_id == 7))) {
  186. /* If MSR EE bit is set, set it to 90 degrees C,
  187. otherwise 105 degrees C */
  188. tjmax_ee = 90000;
  189. tjmax = 105000;
  190. }
  191. }
  192. }
  193. if (usemsr_ee) {
  194. err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
  195. if (err) {
  196. dev_warn(dev,
  197. "Unable to access MSR 0xEE, for Tjmax, left"
  198. " at default\n");
  199. } else if (eax & 0x40000000) {
  200. tjmax = tjmax_ee;
  201. }
  202. /* if we dont use msr EE it means we are desktop CPU (with exeception
  203. of Atom) */
  204. } else if (tjmax == 100000) {
  205. dev_warn(dev, "Using relative temperature scale!\n");
  206. }
  207. return tjmax;
  208. }
  209. static int __devinit get_tjmax(struct cpuinfo_x86 *c, u32 id,
  210. struct device *dev)
  211. {
  212. /* The 100C is default for both mobile and non mobile CPUs */
  213. int err;
  214. u32 eax, edx;
  215. u32 val;
  216. /* A new feature of current Intel(R) processors, the
  217. IA32_TEMPERATURE_TARGET contains the TjMax value */
  218. err = rdmsr_safe_on_cpu(id, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  219. if (err) {
  220. dev_warn(dev, "Unable to read TjMax from CPU.\n");
  221. } else {
  222. val = (eax >> 16) & 0xff;
  223. /*
  224. * If the TjMax is not plausible, an assumption
  225. * will be used
  226. */
  227. if ((val > 80) && (val < 120)) {
  228. dev_info(dev, "TjMax is %d C.\n", val);
  229. return val * 1000;
  230. }
  231. }
  232. /*
  233. * An assumption is made for early CPUs and unreadable MSR.
  234. * NOTE: the given value may not be correct.
  235. */
  236. switch (c->x86_model) {
  237. case 0xe:
  238. case 0xf:
  239. case 0x16:
  240. case 0x1a:
  241. dev_warn(dev, "TjMax is assumed as 100 C!\n");
  242. return 100000;
  243. case 0x17:
  244. case 0x1c: /* Atom CPUs */
  245. return adjust_tjmax(c, id, dev);
  246. default:
  247. dev_warn(dev, "CPU (model=0x%x) is not supported yet,"
  248. " using default TjMax of 100C.\n", c->x86_model);
  249. return 100000;
  250. }
  251. }
  252. static void __devinit get_ucode_rev_on_cpu(void *edx)
  253. {
  254. u32 eax;
  255. wrmsr(MSR_IA32_UCODE_REV, 0, 0);
  256. sync_core();
  257. rdmsr(MSR_IA32_UCODE_REV, eax, *(u32 *)edx);
  258. }
  259. static int __devinit coretemp_probe(struct platform_device *pdev)
  260. {
  261. struct coretemp_data *data;
  262. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  263. int err;
  264. u32 eax, edx;
  265. if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
  266. err = -ENOMEM;
  267. dev_err(&pdev->dev, "Out of memory\n");
  268. goto exit;
  269. }
  270. data->id = pdev->id;
  271. #ifdef CONFIG_SMP
  272. data->core_id = c->cpu_core_id;
  273. #endif
  274. data->name = "coretemp";
  275. mutex_init(&data->update_lock);
  276. /* test if we can access the THERM_STATUS MSR */
  277. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
  278. if (err) {
  279. dev_err(&pdev->dev,
  280. "Unable to access THERM_STATUS MSR, giving up\n");
  281. goto exit_free;
  282. }
  283. /* Check if we have problem with errata AE18 of Core processors:
  284. Readings might stop update when processor visited too deep sleep,
  285. fixed for stepping D0 (6EC).
  286. */
  287. if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
  288. /* check for microcode update */
  289. err = smp_call_function_single(data->id, get_ucode_rev_on_cpu,
  290. &edx, 1);
  291. if (err) {
  292. dev_err(&pdev->dev,
  293. "Cannot determine microcode revision of "
  294. "CPU#%u (%d)!\n", data->id, err);
  295. err = -ENODEV;
  296. goto exit_free;
  297. } else if (edx < 0x39) {
  298. err = -ENODEV;
  299. dev_err(&pdev->dev,
  300. "Errata AE18 not fixed, update BIOS or "
  301. "microcode of the CPU!\n");
  302. goto exit_free;
  303. }
  304. }
  305. data->tjmax = get_tjmax(c, data->id, &pdev->dev);
  306. platform_set_drvdata(pdev, data);
  307. /*
  308. * read the still undocumented IA32_TEMPERATURE_TARGET. It exists
  309. * on older CPUs but not in this register,
  310. * Atoms don't have it either.
  311. */
  312. if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) {
  313. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET,
  314. &eax, &edx);
  315. if (err) {
  316. dev_warn(&pdev->dev, "Unable to read"
  317. " IA32_TEMPERATURE_TARGET MSR\n");
  318. } else {
  319. data->ttarget = data->tjmax -
  320. (((eax >> 8) & 0xff) * 1000);
  321. err = device_create_file(&pdev->dev,
  322. &sensor_dev_attr_temp1_max.dev_attr);
  323. if (err)
  324. goto exit_free;
  325. }
  326. }
  327. if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
  328. goto exit_dev;
  329. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  330. if (IS_ERR(data->hwmon_dev)) {
  331. err = PTR_ERR(data->hwmon_dev);
  332. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  333. err);
  334. goto exit_class;
  335. }
  336. return 0;
  337. exit_class:
  338. sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
  339. exit_dev:
  340. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  341. exit_free:
  342. kfree(data);
  343. exit:
  344. return err;
  345. }
  346. static int __devexit coretemp_remove(struct platform_device *pdev)
  347. {
  348. struct coretemp_data *data = platform_get_drvdata(pdev);
  349. hwmon_device_unregister(data->hwmon_dev);
  350. sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
  351. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  352. platform_set_drvdata(pdev, NULL);
  353. kfree(data);
  354. return 0;
  355. }
  356. static struct platform_driver coretemp_driver = {
  357. .driver = {
  358. .owner = THIS_MODULE,
  359. .name = DRVNAME,
  360. },
  361. .probe = coretemp_probe,
  362. .remove = __devexit_p(coretemp_remove),
  363. };
  364. struct pdev_entry {
  365. struct list_head list;
  366. struct platform_device *pdev;
  367. unsigned int cpu;
  368. #ifdef CONFIG_SMP
  369. u16 phys_proc_id;
  370. u16 cpu_core_id;
  371. #endif
  372. };
  373. static LIST_HEAD(pdev_list);
  374. static DEFINE_MUTEX(pdev_list_mutex);
  375. static int __cpuinit coretemp_device_add(unsigned int cpu)
  376. {
  377. int err;
  378. struct platform_device *pdev;
  379. struct pdev_entry *pdev_entry;
  380. struct cpuinfo_x86 *c = &cpu_data(cpu);
  381. /*
  382. * CPUID.06H.EAX[0] indicates whether the CPU has thermal
  383. * sensors. We check this bit only, all the early CPUs
  384. * without thermal sensors will be filtered out.
  385. */
  386. if (!cpu_has(c, X86_FEATURE_DTS)) {
  387. pr_info("CPU (model=0x%x) has no thermal sensor\n",
  388. c->x86_model);
  389. return 0;
  390. }
  391. mutex_lock(&pdev_list_mutex);
  392. #ifdef CONFIG_SMP
  393. /* Skip second HT entry of each core */
  394. list_for_each_entry(pdev_entry, &pdev_list, list) {
  395. if (c->phys_proc_id == pdev_entry->phys_proc_id &&
  396. c->cpu_core_id == pdev_entry->cpu_core_id) {
  397. err = 0; /* Not an error */
  398. goto exit;
  399. }
  400. }
  401. #endif
  402. pdev = platform_device_alloc(DRVNAME, cpu);
  403. if (!pdev) {
  404. err = -ENOMEM;
  405. pr_err("Device allocation failed\n");
  406. goto exit;
  407. }
  408. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  409. if (!pdev_entry) {
  410. err = -ENOMEM;
  411. goto exit_device_put;
  412. }
  413. err = platform_device_add(pdev);
  414. if (err) {
  415. pr_err("Device addition failed (%d)\n", err);
  416. goto exit_device_free;
  417. }
  418. pdev_entry->pdev = pdev;
  419. pdev_entry->cpu = cpu;
  420. #ifdef CONFIG_SMP
  421. pdev_entry->phys_proc_id = c->phys_proc_id;
  422. pdev_entry->cpu_core_id = c->cpu_core_id;
  423. #endif
  424. list_add_tail(&pdev_entry->list, &pdev_list);
  425. mutex_unlock(&pdev_list_mutex);
  426. return 0;
  427. exit_device_free:
  428. kfree(pdev_entry);
  429. exit_device_put:
  430. platform_device_put(pdev);
  431. exit:
  432. mutex_unlock(&pdev_list_mutex);
  433. return err;
  434. }
  435. static void __cpuinit coretemp_device_remove(unsigned int cpu)
  436. {
  437. struct pdev_entry *p;
  438. unsigned int i;
  439. mutex_lock(&pdev_list_mutex);
  440. list_for_each_entry(p, &pdev_list, list) {
  441. if (p->cpu != cpu)
  442. continue;
  443. platform_device_unregister(p->pdev);
  444. list_del(&p->list);
  445. mutex_unlock(&pdev_list_mutex);
  446. kfree(p);
  447. for_each_cpu(i, cpu_sibling_mask(cpu))
  448. if (i != cpu && !coretemp_device_add(i))
  449. break;
  450. return;
  451. }
  452. mutex_unlock(&pdev_list_mutex);
  453. }
  454. static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
  455. unsigned long action, void *hcpu)
  456. {
  457. unsigned int cpu = (unsigned long) hcpu;
  458. switch (action) {
  459. case CPU_ONLINE:
  460. case CPU_DOWN_FAILED:
  461. coretemp_device_add(cpu);
  462. break;
  463. case CPU_DOWN_PREPARE:
  464. coretemp_device_remove(cpu);
  465. break;
  466. }
  467. return NOTIFY_OK;
  468. }
  469. static struct notifier_block coretemp_cpu_notifier __refdata = {
  470. .notifier_call = coretemp_cpu_callback,
  471. };
  472. static int __init coretemp_init(void)
  473. {
  474. int i, err = -ENODEV;
  475. /* quick check if we run Intel */
  476. if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
  477. goto exit;
  478. err = platform_driver_register(&coretemp_driver);
  479. if (err)
  480. goto exit;
  481. for_each_online_cpu(i)
  482. coretemp_device_add(i);
  483. #ifndef CONFIG_HOTPLUG_CPU
  484. if (list_empty(&pdev_list)) {
  485. err = -ENODEV;
  486. goto exit_driver_unreg;
  487. }
  488. #endif
  489. register_hotcpu_notifier(&coretemp_cpu_notifier);
  490. return 0;
  491. #ifndef CONFIG_HOTPLUG_CPU
  492. exit_driver_unreg:
  493. platform_driver_unregister(&coretemp_driver);
  494. #endif
  495. exit:
  496. return err;
  497. }
  498. static void __exit coretemp_exit(void)
  499. {
  500. struct pdev_entry *p, *n;
  501. unregister_hotcpu_notifier(&coretemp_cpu_notifier);
  502. mutex_lock(&pdev_list_mutex);
  503. list_for_each_entry_safe(p, n, &pdev_list, list) {
  504. platform_device_unregister(p->pdev);
  505. list_del(&p->list);
  506. kfree(p);
  507. }
  508. mutex_unlock(&pdev_list_mutex);
  509. platform_driver_unregister(&coretemp_driver);
  510. }
  511. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  512. MODULE_DESCRIPTION("Intel Core temperature monitor");
  513. MODULE_LICENSE("GPL");
  514. module_init(coretemp_init)
  515. module_exit(coretemp_exit)