coretemp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 ismobile = 1;
  139. int err;
  140. u32 eax, edx;
  141. /* Early chips have no MSR for TjMax */
  142. if ((c->x86_model == 0xf) && (c->x86_mask < 4)) {
  143. ismobile = 0;
  144. }
  145. if ((c->x86_model > 0xe) && (ismobile)) {
  146. /* Now we can detect the mobile CPU using Intel provided table
  147. http://softwarecommunity.intel.com/Wiki/Mobility/720.htm
  148. For Core2 cores, check MSR 0x17, bit 28 1 = Mobile CPU
  149. */
  150. err = rdmsr_safe_on_cpu(id, 0x17, &eax, &edx);
  151. if (err) {
  152. dev_warn(dev,
  153. "Unable to access MSR 0x17, assuming desktop"
  154. " CPU\n");
  155. ismobile = 0;
  156. } else if (!(eax & 0x10000000)) {
  157. ismobile = 0;
  158. }
  159. }
  160. if (ismobile) {
  161. err = rdmsr_safe_on_cpu(id, 0xee, &eax, &edx);
  162. if (err) {
  163. dev_warn(dev,
  164. "Unable to access MSR 0xEE, for Tjmax, left"
  165. " at default");
  166. } else if (eax & 0x40000000) {
  167. tjmax = 85000;
  168. }
  169. } else {
  170. dev_warn(dev, "Using relative temperature scale!\n");
  171. }
  172. return tjmax;
  173. }
  174. static int __devinit coretemp_probe(struct platform_device *pdev)
  175. {
  176. struct coretemp_data *data;
  177. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  178. int err;
  179. u32 eax, edx;
  180. if (!(data = kzalloc(sizeof(struct coretemp_data), GFP_KERNEL))) {
  181. err = -ENOMEM;
  182. dev_err(&pdev->dev, "Out of memory\n");
  183. goto exit;
  184. }
  185. data->id = pdev->id;
  186. data->name = "coretemp";
  187. mutex_init(&data->update_lock);
  188. /* test if we can access the THERM_STATUS MSR */
  189. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_THERM_STATUS, &eax, &edx);
  190. if (err) {
  191. dev_err(&pdev->dev,
  192. "Unable to access THERM_STATUS MSR, giving up\n");
  193. goto exit_free;
  194. }
  195. /* Check if we have problem with errata AE18 of Core processors:
  196. Readings might stop update when processor visited too deep sleep,
  197. fixed for stepping D0 (6EC).
  198. */
  199. if ((c->x86_model == 0xe) && (c->x86_mask < 0xc)) {
  200. /* check for microcode update */
  201. rdmsr_on_cpu(data->id, MSR_IA32_UCODE_REV, &eax, &edx);
  202. if (edx < 0x39) {
  203. err = -ENODEV;
  204. dev_err(&pdev->dev,
  205. "Errata AE18 not fixed, update BIOS or "
  206. "microcode of the CPU!\n");
  207. goto exit_free;
  208. }
  209. }
  210. data->tjmax = adjust_tjmax(c, data->id, &pdev->dev);
  211. platform_set_drvdata(pdev, data);
  212. /* read the still undocumented IA32_TEMPERATURE_TARGET it exists
  213. on older CPUs but not in this register */
  214. if (c->x86_model > 0xe) {
  215. err = rdmsr_safe_on_cpu(data->id, 0x1a2, &eax, &edx);
  216. if (err) {
  217. dev_warn(&pdev->dev, "Unable to read"
  218. " IA32_TEMPERATURE_TARGET MSR\n");
  219. } else {
  220. data->ttarget = data->tjmax -
  221. (((eax >> 8) & 0xff) * 1000);
  222. err = device_create_file(&pdev->dev,
  223. &sensor_dev_attr_temp1_max.dev_attr);
  224. if (err)
  225. goto exit_free;
  226. }
  227. }
  228. if ((err = sysfs_create_group(&pdev->dev.kobj, &coretemp_group)))
  229. goto exit_dev;
  230. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  231. if (IS_ERR(data->hwmon_dev)) {
  232. err = PTR_ERR(data->hwmon_dev);
  233. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  234. err);
  235. goto exit_class;
  236. }
  237. return 0;
  238. exit_class:
  239. sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
  240. exit_dev:
  241. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  242. exit_free:
  243. kfree(data);
  244. exit:
  245. return err;
  246. }
  247. static int __devexit coretemp_remove(struct platform_device *pdev)
  248. {
  249. struct coretemp_data *data = platform_get_drvdata(pdev);
  250. hwmon_device_unregister(data->hwmon_dev);
  251. sysfs_remove_group(&pdev->dev.kobj, &coretemp_group);
  252. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  253. platform_set_drvdata(pdev, NULL);
  254. kfree(data);
  255. return 0;
  256. }
  257. static struct platform_driver coretemp_driver = {
  258. .driver = {
  259. .owner = THIS_MODULE,
  260. .name = DRVNAME,
  261. },
  262. .probe = coretemp_probe,
  263. .remove = __devexit_p(coretemp_remove),
  264. };
  265. struct pdev_entry {
  266. struct list_head list;
  267. struct platform_device *pdev;
  268. unsigned int cpu;
  269. };
  270. static LIST_HEAD(pdev_list);
  271. static DEFINE_MUTEX(pdev_list_mutex);
  272. static int __cpuinit coretemp_device_add(unsigned int cpu)
  273. {
  274. int err;
  275. struct platform_device *pdev;
  276. struct pdev_entry *pdev_entry;
  277. pdev = platform_device_alloc(DRVNAME, cpu);
  278. if (!pdev) {
  279. err = -ENOMEM;
  280. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  281. goto exit;
  282. }
  283. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  284. if (!pdev_entry) {
  285. err = -ENOMEM;
  286. goto exit_device_put;
  287. }
  288. err = platform_device_add(pdev);
  289. if (err) {
  290. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  291. err);
  292. goto exit_device_free;
  293. }
  294. pdev_entry->pdev = pdev;
  295. pdev_entry->cpu = cpu;
  296. mutex_lock(&pdev_list_mutex);
  297. list_add_tail(&pdev_entry->list, &pdev_list);
  298. mutex_unlock(&pdev_list_mutex);
  299. return 0;
  300. exit_device_free:
  301. kfree(pdev_entry);
  302. exit_device_put:
  303. platform_device_put(pdev);
  304. exit:
  305. return err;
  306. }
  307. #ifdef CONFIG_HOTPLUG_CPU
  308. static void coretemp_device_remove(unsigned int cpu)
  309. {
  310. struct pdev_entry *p, *n;
  311. mutex_lock(&pdev_list_mutex);
  312. list_for_each_entry_safe(p, n, &pdev_list, list) {
  313. if (p->cpu == cpu) {
  314. platform_device_unregister(p->pdev);
  315. list_del(&p->list);
  316. kfree(p);
  317. }
  318. }
  319. mutex_unlock(&pdev_list_mutex);
  320. }
  321. static int __cpuinit coretemp_cpu_callback(struct notifier_block *nfb,
  322. unsigned long action, void *hcpu)
  323. {
  324. unsigned int cpu = (unsigned long) hcpu;
  325. switch (action) {
  326. case CPU_ONLINE:
  327. case CPU_DOWN_FAILED:
  328. coretemp_device_add(cpu);
  329. break;
  330. case CPU_DOWN_PREPARE:
  331. coretemp_device_remove(cpu);
  332. break;
  333. }
  334. return NOTIFY_OK;
  335. }
  336. static struct notifier_block coretemp_cpu_notifier __refdata = {
  337. .notifier_call = coretemp_cpu_callback,
  338. };
  339. #endif /* !CONFIG_HOTPLUG_CPU */
  340. static int __init coretemp_init(void)
  341. {
  342. int i, err = -ENODEV;
  343. struct pdev_entry *p, *n;
  344. /* quick check if we run Intel */
  345. if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
  346. goto exit;
  347. err = platform_driver_register(&coretemp_driver);
  348. if (err)
  349. goto exit;
  350. for_each_online_cpu(i) {
  351. struct cpuinfo_x86 *c = &cpu_data(i);
  352. /* check if family 6, models 0xe, 0xf, 0x16, 0x17, 0x1A */
  353. if ((c->cpuid_level < 0) || (c->x86 != 0x6) ||
  354. !((c->x86_model == 0xe) || (c->x86_model == 0xf) ||
  355. (c->x86_model == 0x16) || (c->x86_model == 0x17) ||
  356. (c->x86_model == 0x1A))) {
  357. /* supported CPU not found, but report the unknown
  358. family 6 CPU */
  359. if ((c->x86 == 0x6) && (c->x86_model > 0xf))
  360. printk(KERN_WARNING DRVNAME ": Unknown CPU "
  361. "model %x\n", c->x86_model);
  362. continue;
  363. }
  364. err = coretemp_device_add(i);
  365. if (err)
  366. goto exit_devices_unreg;
  367. }
  368. if (list_empty(&pdev_list)) {
  369. err = -ENODEV;
  370. goto exit_driver_unreg;
  371. }
  372. #ifdef CONFIG_HOTPLUG_CPU
  373. register_hotcpu_notifier(&coretemp_cpu_notifier);
  374. #endif
  375. return 0;
  376. exit_devices_unreg:
  377. mutex_lock(&pdev_list_mutex);
  378. list_for_each_entry_safe(p, n, &pdev_list, list) {
  379. platform_device_unregister(p->pdev);
  380. list_del(&p->list);
  381. kfree(p);
  382. }
  383. mutex_unlock(&pdev_list_mutex);
  384. exit_driver_unreg:
  385. platform_driver_unregister(&coretemp_driver);
  386. exit:
  387. return err;
  388. }
  389. static void __exit coretemp_exit(void)
  390. {
  391. struct pdev_entry *p, *n;
  392. #ifdef CONFIG_HOTPLUG_CPU
  393. unregister_hotcpu_notifier(&coretemp_cpu_notifier);
  394. #endif
  395. mutex_lock(&pdev_list_mutex);
  396. list_for_each_entry_safe(p, n, &pdev_list, list) {
  397. platform_device_unregister(p->pdev);
  398. list_del(&p->list);
  399. kfree(p);
  400. }
  401. mutex_unlock(&pdev_list_mutex);
  402. platform_driver_unregister(&coretemp_driver);
  403. }
  404. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  405. MODULE_DESCRIPTION("Intel Core temperature monitor");
  406. MODULE_LICENSE("GPL");
  407. module_init(coretemp_init)
  408. module_exit(coretemp_exit)