pkgtemp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * pkgtemp.c - Linux kernel module for processor package hardware monitoring
  3. *
  4. * Copyright (C) 2010 Fenghua Yu <fenghua.yu@intel.com>
  5. *
  6. * Inspired from many hwmon drivers especially coretemp.
  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 "pkgtemp"
  39. enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL, SHOW_NAME };
  40. /*
  41. * Functions declaration
  42. */
  43. static struct pkgtemp_data *pkgtemp_update_device(struct device *dev);
  44. struct pkgtemp_data {
  45. struct device *hwmon_dev;
  46. struct mutex update_lock;
  47. const char *name;
  48. u32 id;
  49. u16 phys_proc_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 pkgtemp_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, "physical id %d\n",
  70. data->phys_proc_id);
  71. return ret;
  72. }
  73. static ssize_t show_alarm(struct device *dev, struct device_attribute
  74. *devattr, char *buf)
  75. {
  76. struct pkgtemp_data *data = pkgtemp_update_device(dev);
  77. /* read the Out-of-spec log, never clear */
  78. return sprintf(buf, "%d\n", data->alarm);
  79. }
  80. static ssize_t show_temp(struct device *dev,
  81. struct device_attribute *devattr, char *buf)
  82. {
  83. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  84. struct pkgtemp_data *data = pkgtemp_update_device(dev);
  85. int err = 0;
  86. if (attr->index == SHOW_TEMP)
  87. err = data->valid ? sprintf(buf, "%d\n", data->temp) : -EAGAIN;
  88. else if (attr->index == SHOW_TJMAX)
  89. err = sprintf(buf, "%d\n", data->tjmax);
  90. else
  91. err = sprintf(buf, "%d\n", data->ttarget);
  92. return err;
  93. }
  94. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, SHOW_TEMP);
  95. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, SHOW_TJMAX);
  96. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, SHOW_TTARGET);
  97. static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
  98. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
  99. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
  100. static struct attribute *pkgtemp_attributes[] = {
  101. &sensor_dev_attr_name.dev_attr.attr,
  102. &sensor_dev_attr_temp1_label.dev_attr.attr,
  103. &dev_attr_temp1_crit_alarm.attr,
  104. &sensor_dev_attr_temp1_input.dev_attr.attr,
  105. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  106. NULL
  107. };
  108. static const struct attribute_group pkgtemp_group = {
  109. .attrs = pkgtemp_attributes,
  110. };
  111. static struct pkgtemp_data *pkgtemp_update_device(struct device *dev)
  112. {
  113. struct pkgtemp_data *data = dev_get_drvdata(dev);
  114. unsigned int cpu;
  115. int err;
  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. cpu = data->id;
  121. err = rdmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_STATUS,
  122. &eax, &edx);
  123. if (!err) {
  124. data->alarm = (eax >> 5) & 1;
  125. data->temp = data->tjmax - (((eax >> 16)
  126. & 0x7f) * 1000);
  127. data->valid = 1;
  128. } else
  129. dev_dbg(dev, "Temperature data invalid (0x%x)\n", eax);
  130. data->last_updated = jiffies;
  131. }
  132. mutex_unlock(&data->update_lock);
  133. return data;
  134. }
  135. static int get_tjmax(int cpu, struct device *dev)
  136. {
  137. int default_tjmax = 100000;
  138. int err;
  139. u32 eax, edx;
  140. u32 val;
  141. /* IA32_TEMPERATURE_TARGET contains the TjMax value */
  142. err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  143. if (!err) {
  144. val = (eax >> 16) & 0xff;
  145. if ((val > 80) && (val < 120)) {
  146. dev_info(dev, "TjMax is %d C.\n", val);
  147. return val * 1000;
  148. }
  149. }
  150. dev_warn(dev, "Unable to read TjMax from CPU.\n");
  151. return default_tjmax;
  152. }
  153. static int __devinit pkgtemp_probe(struct platform_device *pdev)
  154. {
  155. struct pkgtemp_data *data;
  156. int err;
  157. u32 eax, edx;
  158. #ifdef CONFIG_SMP
  159. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  160. #endif
  161. data = kzalloc(sizeof(struct pkgtemp_data), GFP_KERNEL);
  162. if (!data) {
  163. err = -ENOMEM;
  164. dev_err(&pdev->dev, "Out of memory\n");
  165. goto exit;
  166. }
  167. data->id = pdev->id;
  168. #ifdef CONFIG_SMP
  169. data->phys_proc_id = c->phys_proc_id;
  170. #endif
  171. data->name = "pkgtemp";
  172. mutex_init(&data->update_lock);
  173. /* test if we can access the THERM_STATUS MSR */
  174. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_PACKAGE_THERM_STATUS,
  175. &eax, &edx);
  176. if (err) {
  177. dev_err(&pdev->dev,
  178. "Unable to access THERM_STATUS MSR, giving up\n");
  179. goto exit_free;
  180. }
  181. data->tjmax = get_tjmax(data->id, &pdev->dev);
  182. platform_set_drvdata(pdev, data);
  183. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET,
  184. &eax, &edx);
  185. if (err) {
  186. dev_warn(&pdev->dev, "Unable to read"
  187. " IA32_TEMPERATURE_TARGET MSR\n");
  188. } else {
  189. data->ttarget = data->tjmax - (((eax >> 8) & 0xff) * 1000);
  190. err = device_create_file(&pdev->dev,
  191. &sensor_dev_attr_temp1_max.dev_attr);
  192. if (err)
  193. goto exit_free;
  194. }
  195. err = sysfs_create_group(&pdev->dev.kobj, &pkgtemp_group);
  196. if (err)
  197. goto exit_free;
  198. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  199. if (IS_ERR(data->hwmon_dev)) {
  200. err = PTR_ERR(data->hwmon_dev);
  201. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  202. err);
  203. goto exit_class;
  204. }
  205. return 0;
  206. exit_class:
  207. sysfs_remove_group(&pdev->dev.kobj, &pkgtemp_group);
  208. exit_free:
  209. kfree(data);
  210. exit:
  211. return err;
  212. }
  213. static int __devexit pkgtemp_remove(struct platform_device *pdev)
  214. {
  215. struct pkgtemp_data *data = platform_get_drvdata(pdev);
  216. hwmon_device_unregister(data->hwmon_dev);
  217. sysfs_remove_group(&pdev->dev.kobj, &pkgtemp_group);
  218. platform_set_drvdata(pdev, NULL);
  219. kfree(data);
  220. return 0;
  221. }
  222. static struct platform_driver pkgtemp_driver = {
  223. .driver = {
  224. .owner = THIS_MODULE,
  225. .name = DRVNAME,
  226. },
  227. .probe = pkgtemp_probe,
  228. .remove = __devexit_p(pkgtemp_remove),
  229. };
  230. struct pdev_entry {
  231. struct list_head list;
  232. struct platform_device *pdev;
  233. unsigned int cpu;
  234. #ifdef CONFIG_SMP
  235. u16 phys_proc_id;
  236. #endif
  237. };
  238. static LIST_HEAD(pdev_list);
  239. static DEFINE_MUTEX(pdev_list_mutex);
  240. static int __cpuinit pkgtemp_device_add(unsigned int cpu)
  241. {
  242. int err;
  243. struct platform_device *pdev;
  244. struct pdev_entry *pdev_entry;
  245. #ifdef CONFIG_SMP
  246. struct cpuinfo_x86 *c = &cpu_data(cpu);
  247. #endif
  248. mutex_lock(&pdev_list_mutex);
  249. #ifdef CONFIG_SMP
  250. /* Only keep the first entry in each package */
  251. list_for_each_entry(pdev_entry, &pdev_list, list) {
  252. if (c->phys_proc_id == pdev_entry->phys_proc_id) {
  253. err = 0; /* Not an error */
  254. goto exit;
  255. }
  256. }
  257. #endif
  258. pdev = platform_device_alloc(DRVNAME, cpu);
  259. if (!pdev) {
  260. err = -ENOMEM;
  261. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  262. goto exit;
  263. }
  264. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  265. if (!pdev_entry) {
  266. err = -ENOMEM;
  267. goto exit_device_put;
  268. }
  269. err = platform_device_add(pdev);
  270. if (err) {
  271. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  272. err);
  273. goto exit_device_free;
  274. }
  275. #ifdef CONFIG_SMP
  276. pdev_entry->phys_proc_id = c->phys_proc_id;
  277. #endif
  278. pdev_entry->pdev = pdev;
  279. pdev_entry->cpu = cpu;
  280. list_add_tail(&pdev_entry->list, &pdev_list);
  281. mutex_unlock(&pdev_list_mutex);
  282. return 0;
  283. exit_device_free:
  284. kfree(pdev_entry);
  285. exit_device_put:
  286. platform_device_put(pdev);
  287. exit:
  288. mutex_unlock(&pdev_list_mutex);
  289. return err;
  290. }
  291. #ifdef CONFIG_HOTPLUG_CPU
  292. static void pkgtemp_device_remove(unsigned int cpu)
  293. {
  294. struct pdev_entry *p, *n;
  295. unsigned int i;
  296. int err;
  297. mutex_lock(&pdev_list_mutex);
  298. list_for_each_entry_safe(p, n, &pdev_list, list) {
  299. if (p->cpu != cpu)
  300. continue;
  301. platform_device_unregister(p->pdev);
  302. list_del(&p->list);
  303. kfree(p);
  304. for_each_cpu(i, cpu_core_mask(cpu)) {
  305. if (i != cpu) {
  306. err = pkgtemp_device_add(i);
  307. if (!err)
  308. break;
  309. }
  310. }
  311. break;
  312. }
  313. mutex_unlock(&pdev_list_mutex);
  314. }
  315. static int __cpuinit pkgtemp_cpu_callback(struct notifier_block *nfb,
  316. unsigned long action, void *hcpu)
  317. {
  318. unsigned int cpu = (unsigned long) hcpu;
  319. switch (action) {
  320. case CPU_ONLINE:
  321. case CPU_DOWN_FAILED:
  322. pkgtemp_device_add(cpu);
  323. break;
  324. case CPU_DOWN_PREPARE:
  325. pkgtemp_device_remove(cpu);
  326. break;
  327. }
  328. return NOTIFY_OK;
  329. }
  330. static struct notifier_block pkgtemp_cpu_notifier __refdata = {
  331. .notifier_call = pkgtemp_cpu_callback,
  332. };
  333. #endif /* !CONFIG_HOTPLUG_CPU */
  334. static int __init pkgtemp_init(void)
  335. {
  336. int i, err = -ENODEV;
  337. struct pdev_entry *p, *n;
  338. /* quick check if we run Intel */
  339. if (cpu_data(0).x86_vendor != X86_VENDOR_INTEL)
  340. goto exit;
  341. err = platform_driver_register(&pkgtemp_driver);
  342. if (err)
  343. goto exit;
  344. for_each_online_cpu(i) {
  345. struct cpuinfo_x86 *c = &cpu_data(i);
  346. if (!cpu_has(c, X86_FEATURE_PTS))
  347. continue;
  348. err = pkgtemp_device_add(i);
  349. if (err)
  350. goto exit_devices_unreg;
  351. }
  352. if (list_empty(&pdev_list)) {
  353. err = -ENODEV;
  354. goto exit_driver_unreg;
  355. }
  356. #ifdef CONFIG_HOTPLUG_CPU
  357. register_hotcpu_notifier(&pkgtemp_cpu_notifier);
  358. #endif
  359. return 0;
  360. exit_devices_unreg:
  361. mutex_lock(&pdev_list_mutex);
  362. list_for_each_entry_safe(p, n, &pdev_list, list) {
  363. platform_device_unregister(p->pdev);
  364. list_del(&p->list);
  365. kfree(p);
  366. }
  367. mutex_unlock(&pdev_list_mutex);
  368. exit_driver_unreg:
  369. platform_driver_unregister(&pkgtemp_driver);
  370. exit:
  371. return err;
  372. }
  373. static void __exit pkgtemp_exit(void)
  374. {
  375. struct pdev_entry *p, *n;
  376. #ifdef CONFIG_HOTPLUG_CPU
  377. unregister_hotcpu_notifier(&pkgtemp_cpu_notifier);
  378. #endif
  379. mutex_lock(&pdev_list_mutex);
  380. list_for_each_entry_safe(p, n, &pdev_list, list) {
  381. platform_device_unregister(p->pdev);
  382. list_del(&p->list);
  383. kfree(p);
  384. }
  385. mutex_unlock(&pdev_list_mutex);
  386. platform_driver_unregister(&pkgtemp_driver);
  387. }
  388. MODULE_AUTHOR("Fenghua Yu <fenghua.yu@intel.com>");
  389. MODULE_DESCRIPTION("Intel processor package temperature monitor");
  390. MODULE_LICENSE("GPL");
  391. module_init(pkgtemp_init)
  392. module_exit(pkgtemp_exit)