coretemp.c 14 KB

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