coretemp.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_LABEL, SHOW_NAME } SHOW;
  39. /*
  40. * Functions declaration
  41. */
  42. static struct coretemp_data *coretemp_update_device(struct device *dev);
  43. struct coretemp_data {
  44. struct class_device *class_dev;
  45. struct mutex update_lock;
  46. const char *name;
  47. u32 id;
  48. char valid; /* zero until following fields are valid */
  49. unsigned long last_updated; /* in jiffies */
  50. int temp;
  51. int tjmax;
  52. u8 alarm;
  53. };
  54. static struct coretemp_data *coretemp_update_device(struct device *dev);
  55. /*
  56. * Sysfs stuff
  57. */
  58. static ssize_t show_name(struct device *dev, struct device_attribute
  59. *devattr, char *buf)
  60. {
  61. int ret;
  62. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  63. struct coretemp_data *data = dev_get_drvdata(dev);
  64. if (attr->index == SHOW_NAME)
  65. ret = sprintf(buf, "%s\n", data->name);
  66. else /* show label */
  67. ret = sprintf(buf, "Core %d\n", data->id);
  68. return ret;
  69. }
  70. static ssize_t show_alarm(struct device *dev, struct device_attribute
  71. *devattr, char *buf)
  72. {
  73. struct coretemp_data *data = coretemp_update_device(dev);
  74. /* read the Out-of-spec log, never clear */
  75. return sprintf(buf, "%d\n", data->alarm);
  76. }
  77. static ssize_t show_temp(struct device *dev,
  78. struct device_attribute *devattr, char *buf)
  79. {
  80. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  81. struct coretemp_data *data = coretemp_update_device(dev);
  82. int err;
  83. if (attr->index == SHOW_TEMP)
  84. err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN;
  85. else
  86. err = sprintf(buf, "%d\n", data->tjmax);
  87. return err;
  88. }
  89. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
  90. SHOW_TEMP);
  91. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL,
  92. SHOW_TJMAX);
  93. static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
  94. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
  95. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
  96. static struct attribute *coretemp_attributes[] = {
  97. &sensor_dev_attr_name.dev_attr.attr,
  98. &sensor_dev_attr_temp1_label.dev_attr.attr,
  99. &dev_attr_temp1_crit_alarm.attr,
  100. &sensor_dev_attr_temp1_input.dev_attr.attr,
  101. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  102. NULL
  103. };
  104. static const struct attribute_group coretemp_group = {
  105. .attrs = coretemp_attributes,
  106. };
  107. static struct coretemp_data *coretemp_update_device(struct device *dev)
  108. {
  109. struct coretemp_data *data = dev_get_drvdata(dev);
  110. mutex_lock(&data->update_lock);
  111. if (!data->valid || time_after(jiffies, data->last_updated + HZ)) {
  112. u32 eax, edx;
  113. data->valid = 0;
  114. rdmsr_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
  115. data->alarm = (eax >> 5) & 1;
  116. /* update only if data has been valid */
  117. if (eax & 0x80000000) {
  118. data->temp = data->tjmax - (((eax >> 16)
  119. & 0x7f) * 1000);
  120. data->valid = 1;
  121. } else {
  122. dev_dbg(dev, "Temperature data invalid (0x%x)\n", eax);
  123. }
  124. data->last_updated = jiffies;
  125. }
  126. mutex_unlock(&data->update_lock);
  127. return data;
  128. }
  129. static int __devinit coretemp_probe(struct platform_device *pdev)
  130. {
  131. struct coretemp_data *data;
  132. struct cpuinfo_x86 *c = &(cpu_data)[pdev->id];
  133. int err;
  134. u32 eax, edx;
  135. if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
  136. err = -ENOMEM;
  137. dev_err(&pdev->dev, "Out of memory\n");
  138. goto exit;
  139. }
  140. data->id = pdev->id;
  141. data->name = "coretemp";
  142. mutex_init(&data->update_lock);
  143. /* Tjmax default is 100 degrees C */
  144. data->tjmax = 100000;
  145. /* test if we can access the THERM_STATUS MSR */
  146. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
  147. if (err) {
  148. dev_err(&pdev->dev,
  149. "Unable to access THERM_STATUS MSR, giving up\n");
  150. goto exit_free;
  151. }
  152. /* Some processors have Tjmax 85 following magic should detect it
  153. Intel won't disclose the information without signed NDA, but
  154. individuals cannot sign it. Catch(ed) 22.
  155. */
  156. if (((c->x86_model == 0xf) && (c->x86_mask > 3)) ||
  157. (c->x86_model == 0xe)) {
  158. err = rdmsr_safe_on_cpu(data->id, 0xee, &eax, &edx);
  159. if (err) {
  160. dev_warn(&pdev->dev,
  161. "Unable to access MSR 0xEE, Tjmax left at %d "
  162. "degrees C\n", data->tjmax/1000);
  163. } else if (eax & 0x40000000) {
  164. data->tjmax = 85000;
  165. }
  166. }
  167. platform_set_drvdata(pdev, data);
  168. if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
  169. goto exit_free;
  170. data->class_dev = hwmon_device_register(&pdev->dev);
  171. if (IS_ERR(data->class_dev)) {
  172. err = PTR_ERR(data->class_dev);
  173. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  174. err);
  175. goto exit_class;
  176. }
  177. return 0;
  178. exit_class:
  179. sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
  180. exit_free:
  181. kfree(data);
  182. exit:
  183. return err;
  184. }
  185. static int __devexit coretemp_remove(struct platform_device *pdev)
  186. {
  187. struct coretemp_data *data = platform_get_drvdata(pdev);
  188. hwmon_device_unregister(data->class_dev);
  189. sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
  190. platform_set_drvdata(pdev, NULL);
  191. kfree(data);
  192. return 0;
  193. }
  194. static struct platform_driver coretemp_driver = {
  195. .driver = {
  196. .owner = THIS_MODULE,
  197. .name = DRVNAME,
  198. },
  199. .probe = coretemp_probe,
  200. .remove = __devexit_p(coretemp_remove),
  201. };
  202. struct pdev_entry {
  203. struct list_head list;
  204. struct platform_device *pdev;
  205. unsigned int cpu;
  206. };
  207. static LIST_HEAD(pdev_list);
  208. static DEFINE_MUTEX(pdev_list_mutex);
  209. static int __cpuinit coretemp_device_add(unsigned int cpu)
  210. {
  211. int err;
  212. struct platform_device *pdev;
  213. struct pdev_entry *pdev_entry;
  214. pdev = platform_device_alloc(DRVNAME, cpu);
  215. if (!pdev) {
  216. err = -ENOMEM;
  217. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  218. goto exit;
  219. }
  220. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  221. if (!pdev_entry) {
  222. err = -ENOMEM;
  223. goto exit_device_put;
  224. }
  225. err = platform_device_add(pdev);
  226. if (err) {
  227. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  228. err);
  229. goto exit_device_free;
  230. }
  231. pdev_entry->pdev = pdev;
  232. pdev_entry->cpu = cpu;
  233. mutex_lock(&pdev_list_mutex);
  234. list_add_tail(&pdev_entry->list, &pdev_list);
  235. mutex_unlock(&pdev_list_mutex);
  236. return 0;
  237. exit_device_free:
  238. kfree(pdev_entry);
  239. exit_device_put:
  240. platform_device_put(pdev);
  241. exit:
  242. return err;
  243. }
  244. #ifdef CONFIG_HOTPLUG_CPU
  245. void coretemp_device_remove(unsigned int cpu)
  246. {
  247. struct pdev_entry *p, *n;
  248. mutex_lock(&pdev_list_mutex);
  249. list_for_each_entry_safe(p, n, &pdev_list, list) {
  250. if (p->cpu == cpu) {
  251. platform_device_unregister(p->pdev);
  252. list_del(&p->list);
  253. kfree(p);
  254. }
  255. }
  256. mutex_unlock(&pdev_list_mutex);
  257. }
  258. static int coretemp_cpu_callback(struct notifier_block *nfb,
  259. unsigned long action, void *hcpu)
  260. {
  261. unsigned int cpu = (unsigned long) hcpu;
  262. switch (action) {
  263. case CPU_ONLINE:
  264. case CPU_ONLINE_FROZEN:
  265. coretemp_device_add(cpu);
  266. break;
  267. case CPU_DEAD:
  268. case CPU_DEAD_FROZEN:
  269. coretemp_device_remove(cpu);
  270. break;
  271. }
  272. return NOTIFY_OK;
  273. }
  274. static struct notifier_block __cpuinitdata coretemp_cpu_notifier = {
  275. .notifier_call = coretemp_cpu_callback,
  276. };
  277. #endif /* !CONFIG_HOTPLUG_CPU */
  278. static int __init coretemp_init(void)
  279. {
  280. int i, err = -ENODEV;
  281. struct pdev_entry *p, *n;
  282. printk(KERN_NOTICE DRVNAME ": This driver uses undocumented features "
  283. "of Core CPU. Temperature might be wrong!\n");
  284. /* quick check if we run Intel */
  285. if (cpu_data[0].x86_vendor != X86_VENDOR_INTEL)
  286. goto exit;
  287. err = platform_driver_register(&coretemp_driver);
  288. if (err)
  289. goto exit;
  290. for_each_online_cpu(i) {
  291. struct cpuinfo_x86 *c = &(cpu_data)[i];
  292. /* check if family 6, models e, f */
  293. if ((c->cpuid_level < 0) || (c->x86 != 0x6) ||
  294. !((c->x86_model == 0xe) || (c->x86_model == 0xf))) {
  295. /* supported CPU not found, but report the unknown
  296. family 6 CPU */
  297. if ((c->x86 == 0x6) && (c->x86_model > 0xf))
  298. printk(KERN_WARNING DRVNAME ": Unknown CPU "
  299. "model %x\n", c->x86_model);
  300. continue;
  301. }
  302. err = coretemp_device_add(i);
  303. if (err)
  304. goto exit_devices_unreg;
  305. }
  306. if (list_empty(&pdev_list)) {
  307. err = -ENODEV;
  308. goto exit_driver_unreg;
  309. }
  310. #ifdef CONFIG_HOTPLUG_CPU
  311. register_hotcpu_notifier(&coretemp_cpu_notifier);
  312. #endif
  313. return 0;
  314. exit_devices_unreg:
  315. mutex_lock(&pdev_list_mutex);
  316. list_for_each_entry_safe(p, n, &pdev_list, list) {
  317. platform_device_unregister(p->pdev);
  318. list_del(&p->list);
  319. kfree(p);
  320. }
  321. mutex_unlock(&pdev_list_mutex);
  322. exit_driver_unreg:
  323. platform_driver_unregister(&coretemp_driver);
  324. exit:
  325. return err;
  326. }
  327. static void __exit coretemp_exit(void)
  328. {
  329. struct pdev_entry *p, *n;
  330. #ifdef CONFIG_HOTPLUG_CPU
  331. unregister_hotcpu_notifier(&coretemp_cpu_notifier);
  332. #endif
  333. mutex_lock(&pdev_list_mutex);
  334. list_for_each_entry_safe(p, n, &pdev_list, list) {
  335. platform_device_unregister(p->pdev);
  336. list_del(&p->list);
  337. kfree(p);
  338. }
  339. mutex_unlock(&pdev_list_mutex);
  340. platform_driver_unregister(&coretemp_driver);
  341. }
  342. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  343. MODULE_DESCRIPTION("Intel Core temperature monitor");
  344. MODULE_LICENSE("GPL");
  345. module_init(coretemp_init)
  346. module_exit(coretemp_exit)