pkgtemp.c 11 KB

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