coretemp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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/delay.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. #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. break;
  243. case 0x17:
  244. case 0x1c: /* Atom CPUs */
  245. return adjust_tjmax(c, id, dev);
  246. break;
  247. default:
  248. dev_warn(dev, "CPU (model=0x%x) is not supported yet,"
  249. " using default TjMax of 100C.\n", c->x86_model);
  250. return 100000;
  251. }
  252. }
  253. static int __devinit coretemp_probe(struct platform_device *pdev)
  254. {
  255. struct coretemp_data *data;
  256. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  257. int err;
  258. u32 eax, edx;
  259. if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
  260. err = -ENOMEM;
  261. dev_err(&pdev->dev, "Out of memory\n");
  262. goto exit;
  263. }
  264. data->id = pdev->id;
  265. #ifdef CONFIG_SMP
  266. data->core_id = c->cpu_core_id;
  267. #endif
  268. data->name = "coretemp";
  269. mutex_init(&data->update_lock);
  270. /* test if we can access the THERM_STATUS MSR */
  271. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
  272. if (err) {
  273. dev_err(&pdev->dev,
  274. "Unable to access THERM_STATUS MSR, giving up\n");
  275. goto exit_free;
  276. }
  277. /* Check if we have problem with errata AE18 of Core processors:
  278. Readings might stop update when processor visited too deep sleep,
  279. fixed for stepping D0 (6EC).
  280. */
  281. if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
  282. /* check for microcode update */
  283. rdmsr_on_cpu(data->id, MSR_IA32_UCODE_REV, &eax, &edx);
  284. if (edx < 0x39) {
  285. err = -ENODEV;
  286. dev_err(&pdev->dev,
  287. "Errata AE18 not fixed, update BIOS or "
  288. "microcode of the CPU!\n");
  289. goto exit_free;
  290. }
  291. }
  292. data->tjmax = get_tjmax(c, data->id, &pdev->dev);
  293. platform_set_drvdata(pdev, data);
  294. /*
  295. * read the still undocumented IA32_TEMPERATURE_TARGET. It exists
  296. * on older CPUs but not in this register,
  297. * Atoms don't have it either.
  298. */
  299. if ((c->x86_model > 0xe) && (c->x86_model != 0x1c)) {
  300. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET,
  301. &eax, &edx);
  302. if (err) {
  303. dev_warn(&pdev->dev, "Unable to read"
  304. " IA32_TEMPERATURE_TARGET MSR\n");
  305. } else {
  306. data->ttarget = data->tjmax -
  307. (((eax >> 8) & 0xff) * 1000);
  308. err = device_create_file(&pdev->dev,
  309. &sensor_dev_attr_temp1_max.dev_attr);
  310. if (err)
  311. goto exit_free;
  312. }
  313. }
  314. if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
  315. goto exit_dev;
  316. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  317. if (IS_ERR(data->hwmon_dev)) {
  318. err = PTR_ERR(data->hwmon_dev);
  319. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  320. err);
  321. goto exit_class;
  322. }
  323. return 0;
  324. exit_class:
  325. sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
  326. exit_dev:
  327. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  328. exit_free:
  329. kfree(data);
  330. exit:
  331. return err;
  332. }
  333. static int __devexit coretemp_remove(struct platform_device *pdev)
  334. {
  335. struct coretemp_data *data = platform_get_drvdata(pdev);
  336. hwmon_device_unregister(data->hwmon_dev);
  337. sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
  338. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  339. platform_set_drvdata(pdev, NULL);
  340. kfree(data);
  341. return 0;
  342. }
  343. static struct platform_driver coretemp_driver = {
  344. .driver = {
  345. .owner = THIS_MODULE,
  346. .name = DRVNAME,
  347. },
  348. .probe = coretemp_probe,
  349. .remove = __devexit_p(coretemp_remove),
  350. };
  351. struct pdev_entry {
  352. struct list_head list;
  353. struct platform_device *pdev;
  354. unsigned int cpu;
  355. #ifdef CONFIG_SMP
  356. u16 phys_proc_id;
  357. u16 cpu_core_id;
  358. #endif
  359. };
  360. static LIST_HEAD(pdev_list);
  361. static DEFINE_MUTEX(pdev_list_mutex);
  362. static int __cpuinit coretemp_device_add(unsigned int cpu)
  363. {
  364. int err;
  365. struct platform_device *pdev;
  366. struct pdev_entry *pdev_entry;
  367. #ifdef CONFIG_SMP
  368. struct cpuinfo_x86 *c = &cpu_data(cpu);
  369. #endif
  370. mutex_lock(&pdev_list_mutex);
  371. #ifdef CONFIG_SMP
  372. /* Skip second HT entry of each core */
  373. list_for_each_entry(pdev_entry, &pdev_list, list) {
  374. if (c->phys_proc_id == pdev_entry->phys_proc_id &&
  375. c->cpu_core_id == pdev_entry->cpu_core_id) {
  376. err = 0; /* Not an error */
  377. goto exit;
  378. }
  379. }
  380. #endif
  381. pdev = platform_device_alloc(DRVNAME, cpu);
  382. if (!pdev) {
  383. err = -ENOMEM;
  384. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  385. goto exit;
  386. }
  387. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  388. if (!pdev_entry) {
  389. err = -ENOMEM;
  390. goto exit_device_put;
  391. }
  392. err = platform_device_add(pdev);
  393. if (err) {
  394. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  395. err);
  396. goto exit_device_free;
  397. }
  398. pdev_entry->pdev = pdev;
  399. pdev_entry->cpu = cpu;
  400. #ifdef CONFIG_SMP
  401. pdev_entry->phys_proc_id = c->phys_proc_id;
  402. pdev_entry->cpu_core_id = c->cpu_core_id;
  403. #endif
  404. list_add_tail(&pdev_entry->list, &pdev_list);
  405. mutex_unlock(&pdev_list_mutex);
  406. return 0;
  407. exit_device_free:
  408. kfree(pdev_entry);
  409. exit_device_put:
  410. platform_device_put(pdev);
  411. exit:
  412. mutex_unlock(&pdev_list_mutex);
  413. return err;
  414. }
  415. #ifdef CONFIG_HOTPLUG_CPU
  416. static void coretemp_device_remove(unsigned int cpu)
  417. {
  418. struct pdev_entry *p, *n;
  419. mutex_lock(&pdev_list_mutex);
  420. list_for_each_entry_safe(p, n, &pdev_list, list) {
  421. if (p->cpu == cpu) {
  422. platform_device_unregister(p->pdev);
  423. list_del(&p->list);
  424. kfree(p);
  425. }
  426. }
  427. mutex_unlock(&pdev_list_mutex);
  428. }
  429. static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
  430. unsigned long action, void *hcpu)
  431. {
  432. unsigned int cpu = (unsigned long) hcpu;
  433. switch (action) {
  434. case CPU_ONLINE:
  435. case CPU_DOWN_FAILED:
  436. coretemp_device_add(cpu);
  437. break;
  438. case CPU_DOWN_PREPARE:
  439. coretemp_device_remove(cpu);
  440. break;
  441. }
  442. return NOTIFY_OK;
  443. }
  444. static struct notifier_block coretemp_cpu_notifier __refdata = {
  445. .notifier_call = coretemp_cpu_callback,
  446. };
  447. #endif /* !CONFIG_HOTPLUG_CPU */
  448. static int __init coretemp_init(void)
  449. {
  450. int i, err = -ENODEV;
  451. struct pdev_entry *p, *n;
  452. /* quick check if we run Intel */
  453. if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
  454. goto exit;
  455. err = platform_driver_register(&coretemp_driver);
  456. if (err)
  457. goto exit;
  458. for_each_online_cpu(i) {
  459. struct cpuinfo_x86 *c = &cpu_data(i);
  460. /*
  461. * CPUID.06H.EAX[0] indicates whether the CPU has thermal
  462. * sensors. We check this bit only, all the early CPUs
  463. * without thermal sensors will be filtered out.
  464. */
  465. if (c->cpuid_level >= 6 && (cpuid_eax(0x06) & 0x01)) {
  466. err = coretemp_device_add(i);
  467. if (err)
  468. goto exit_devices_unreg;
  469. } else {
  470. printk(KERN_INFO DRVNAME ": CPU (model=0x%x)"
  471. " has no thermal sensor.\n", c->x86_model);
  472. }
  473. }
  474. if (list_empty(&pdev_list)) {
  475. err = -ENODEV;
  476. goto exit_driver_unreg;
  477. }
  478. #ifdef CONFIG_HOTPLUG_CPU
  479. register_hotcpu_notifier(&coretemp_cpu_notifier);
  480. #endif
  481. return 0;
  482. exit_devices_unreg:
  483. mutex_lock(&pdev_list_mutex);
  484. list_for_each_entry_safe(p, n, &pdev_list, list) {
  485. platform_device_unregister(p->pdev);
  486. list_del(&p->list);
  487. kfree(p);
  488. }
  489. mutex_unlock(&pdev_list_mutex);
  490. exit_driver_unreg:
  491. platform_driver_unregister(&coretemp_driver);
  492. exit:
  493. return err;
  494. }
  495. static void __exit coretemp_exit(void)
  496. {
  497. struct pdev_entry *p, *n;
  498. #ifdef CONFIG_HOTPLUG_CPU
  499. unregister_hotcpu_notifier(&coretemp_cpu_notifier);
  500. #endif
  501. mutex_lock(&pdev_list_mutex);
  502. list_for_each_entry_safe(p, n, &pdev_list, list) {
  503. platform_device_unregister(p->pdev);
  504. list_del(&p->list);
  505. kfree(p);
  506. }
  507. mutex_unlock(&pdev_list_mutex);
  508. platform_driver_unregister(&coretemp_driver);
  509. }
  510. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  511. MODULE_DESCRIPTION("Intel Core temperature monitor");
  512. MODULE_LICENSE("GPL");
  513. module_init(coretemp_init)
  514. module_exit(coretemp_exit)