coretemp.c 15 KB

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