coretemp.c 13 KB

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