coretemp.c 13 KB

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