pkgtemp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #include <asm/smp.h>
  37. #define DRVNAME "pkgtemp"
  38. enum { SHOW_TEMP, SHOW_TJMAX, SHOW_TTARGET, SHOW_LABEL, SHOW_NAME };
  39. /*
  40. * Functions declaration
  41. */
  42. static struct pkgtemp_data *pkgtemp_update_device(struct device *dev);
  43. struct pkgtemp_data {
  44. struct device *hwmon_dev;
  45. struct mutex update_lock;
  46. const char *name;
  47. u32 id;
  48. u16 phys_proc_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 pkgtemp_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, "physical id %d\n",
  69. data->phys_proc_id);
  70. return ret;
  71. }
  72. static ssize_t show_alarm(struct device *dev, struct device_attribute
  73. *devattr, char *buf)
  74. {
  75. struct pkgtemp_data *data = pkgtemp_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 pkgtemp_data *data = pkgtemp_update_device(dev);
  84. int err = 0;
  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, SHOW_TEMP);
  94. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, SHOW_TJMAX);
  95. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, SHOW_TTARGET);
  96. static DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL);
  97. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
  98. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
  99. static struct attribute *pkgtemp_attributes[] = {
  100. &sensor_dev_attr_name.dev_attr.attr,
  101. &sensor_dev_attr_temp1_label.dev_attr.attr,
  102. &dev_attr_temp1_crit_alarm.attr,
  103. &sensor_dev_attr_temp1_input.dev_attr.attr,
  104. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  105. NULL
  106. };
  107. static const struct attribute_group pkgtemp_group = {
  108. .attrs = pkgtemp_attributes,
  109. };
  110. static struct pkgtemp_data *pkgtemp_update_device(struct device *dev)
  111. {
  112. struct pkgtemp_data *data = dev_get_drvdata(dev);
  113. unsigned int cpu;
  114. int err;
  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. cpu = data->id;
  120. err = rdmsr_on_cpu(cpu, MSR_IA32_PACKAGE_THERM_STATUS,
  121. &eax, &edx);
  122. if (!err) {
  123. data->alarm = (eax >> 5) & 1;
  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. data->last_updated = jiffies;
  130. }
  131. mutex_unlock(&data->update_lock);
  132. return data;
  133. }
  134. static int get_tjmax(int cpu, struct device *dev)
  135. {
  136. int default_tjmax = 100000;
  137. int err;
  138. u32 eax, edx;
  139. u32 val;
  140. /* IA32_TEMPERATURE_TARGET contains the TjMax value */
  141. err = rdmsr_safe_on_cpu(cpu, MSR_IA32_TEMPERATURE_TARGET, &eax, &edx);
  142. if (!err) {
  143. val = (eax >> 16) & 0xff;
  144. if ((val > 80) && (val < 120)) {
  145. dev_info(dev, "TjMax is %d C.\n", val);
  146. return val * 1000;
  147. }
  148. }
  149. dev_warn(dev, "Unable to read TjMax from CPU.\n");
  150. return default_tjmax;
  151. }
  152. static int __devinit pkgtemp_probe(struct platform_device *pdev)
  153. {
  154. struct pkgtemp_data *data;
  155. int err;
  156. u32 eax, edx;
  157. #ifdef CONFIG_SMP
  158. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  159. #endif
  160. data = kzalloc(sizeof(struct pkgtemp_data), GFP_KERNEL);
  161. if (!data) {
  162. err = -ENOMEM;
  163. dev_err(&pdev->dev, "Out of memory\n");
  164. goto exit;
  165. }
  166. data->id = pdev->id;
  167. #ifdef CONFIG_SMP
  168. data->phys_proc_id = c->phys_proc_id;
  169. #endif
  170. data->name = "pkgtemp";
  171. mutex_init(&data->update_lock);
  172. /* test if we can access the THERM_STATUS MSR */
  173. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_PACKAGE_THERM_STATUS,
  174. &eax, &edx);
  175. if (err) {
  176. dev_err(&pdev->dev,
  177. "Unable to access THERM_STATUS MSR, giving up\n");
  178. goto exit_free;
  179. }
  180. data->tjmax = get_tjmax(data->id, &pdev->dev);
  181. platform_set_drvdata(pdev, data);
  182. err = rdmsr_safe_on_cpu(data->id, MSR_IA32_TEMPERATURE_TARGET,
  183. &eax, &edx);
  184. if (err) {
  185. dev_warn(&pdev->dev, "Unable to read"
  186. " IA32_TEMPERATURE_TARGET MSR\n");
  187. } else {
  188. data->ttarget = data->tjmax - (((eax >> 8) & 0xff) * 1000);
  189. err = device_create_file(&pdev->dev,
  190. &sensor_dev_attr_temp1_max.dev_attr);
  191. if (err)
  192. goto exit_free;
  193. }
  194. err = sysfs_create_group(&pdev->dev.kobj, &pkgtemp_group);
  195. if (err)
  196. goto exit_dev;
  197. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  198. if (IS_ERR(data->hwmon_dev)) {
  199. err = PTR_ERR(data->hwmon_dev);
  200. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  201. err);
  202. goto exit_class;
  203. }
  204. return 0;
  205. exit_class:
  206. sysfs_remove_group(&pdev->dev.kobj, &pkgtemp_group);
  207. exit_dev:
  208. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  209. exit_free:
  210. kfree(data);
  211. exit:
  212. return err;
  213. }
  214. static int __devexit pkgtemp_remove(struct platform_device *pdev)
  215. {
  216. struct pkgtemp_data *data = platform_get_drvdata(pdev);
  217. hwmon_device_unregister(data->hwmon_dev);
  218. sysfs_remove_group(&pdev->dev.kobj, &pkgtemp_group);
  219. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  220. platform_set_drvdata(pdev, NULL);
  221. kfree(data);
  222. return 0;
  223. }
  224. static struct platform_driver pkgtemp_driver = {
  225. .driver = {
  226. .owner = THIS_MODULE,
  227. .name = DRVNAME,
  228. },
  229. .probe = pkgtemp_probe,
  230. .remove = __devexit_p(pkgtemp_remove),
  231. };
  232. struct pdev_entry {
  233. struct list_head list;
  234. struct platform_device *pdev;
  235. unsigned int cpu;
  236. #ifdef CONFIG_SMP
  237. u16 phys_proc_id;
  238. #endif
  239. };
  240. static LIST_HEAD(pdev_list);
  241. static DEFINE_MUTEX(pdev_list_mutex);
  242. static int __cpuinit pkgtemp_device_add(unsigned int cpu)
  243. {
  244. int err;
  245. struct platform_device *pdev;
  246. struct pdev_entry *pdev_entry;
  247. struct cpuinfo_x86 *c = &cpu_data(cpu);
  248. if (!cpu_has(c, X86_FEATURE_PTS))
  249. return 0;
  250. mutex_lock(&pdev_list_mutex);
  251. #ifdef CONFIG_SMP
  252. /* Only keep the first entry in each package */
  253. list_for_each_entry(pdev_entry, &pdev_list, list) {
  254. if (c->phys_proc_id == pdev_entry->phys_proc_id) {
  255. err = 0; /* Not an error */
  256. goto exit;
  257. }
  258. }
  259. #endif
  260. pdev = platform_device_alloc(DRVNAME, cpu);
  261. if (!pdev) {
  262. err = -ENOMEM;
  263. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  264. goto exit;
  265. }
  266. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  267. if (!pdev_entry) {
  268. err = -ENOMEM;
  269. goto exit_device_put;
  270. }
  271. err = platform_device_add(pdev);
  272. if (err) {
  273. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  274. err);
  275. goto exit_device_free;
  276. }
  277. #ifdef CONFIG_SMP
  278. pdev_entry->phys_proc_id = c->phys_proc_id;
  279. #endif
  280. pdev_entry->pdev = pdev;
  281. pdev_entry->cpu = cpu;
  282. list_add_tail(&pdev_entry->list, &pdev_list);
  283. mutex_unlock(&pdev_list_mutex);
  284. return 0;
  285. exit_device_free:
  286. kfree(pdev_entry);
  287. exit_device_put:
  288. platform_device_put(pdev);
  289. exit:
  290. mutex_unlock(&pdev_list_mutex);
  291. return err;
  292. }
  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. static int __init pkgtemp_init(void)
  336. {
  337. int i, err = -ENODEV;
  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. pkgtemp_device_add(i);
  346. #ifndef CONFIG_HOTPLUG_CPU
  347. if (list_empty(&pdev_list)) {
  348. err = -ENODEV;
  349. goto exit_driver_unreg;
  350. }
  351. #endif
  352. register_hotcpu_notifier(&pkgtemp_cpu_notifier);
  353. return 0;
  354. #ifndef CONFIG_HOTPLUG_CPU
  355. exit_driver_unreg:
  356. platform_driver_unregister(&pkgtemp_driver);
  357. #endif
  358. exit:
  359. return err;
  360. }
  361. static void __exit pkgtemp_exit(void)
  362. {
  363. struct pdev_entry *p, *n;
  364. unregister_hotcpu_notifier(&pkgtemp_cpu_notifier);
  365. mutex_lock(&pdev_list_mutex);
  366. list_for_each_entry_safe(p, n, &pdev_list, list) {
  367. platform_device_unregister(p->pdev);
  368. list_del(&p->list);
  369. kfree(p);
  370. }
  371. mutex_unlock(&pdev_list_mutex);
  372. platform_driver_unregister(&pkgtemp_driver);
  373. }
  374. MODULE_AUTHOR("Fenghua Yu <fenghua.yu@intel.com>");
  375. MODULE_DESCRIPTION("Intel processor package temperature monitor");
  376. MODULE_LICENSE("GPL");
  377. module_init(pkgtemp_init)
  378. module_exit(pkgtemp_exit)