pkgtemp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/module.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 <asm/msr.h>
  36. #include <asm/processor.h>
  37. #include <asm/smp.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_dev;
  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_dev:
  209. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  210. exit_free:
  211. kfree(data);
  212. exit:
  213. return err;
  214. }
  215. static int __devexit pkgtemp_remove(struct platform_device *pdev)
  216. {
  217. struct pkgtemp_data *data = platform_get_drvdata(pdev);
  218. hwmon_device_unregister(data->hwmon_dev);
  219. sysfs_remove_group(&pdev->dev.kobj, &pkgtemp_group);
  220. device_remove_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  221. platform_set_drvdata(pdev, NULL);
  222. kfree(data);
  223. return 0;
  224. }
  225. static struct platform_driver pkgtemp_driver = {
  226. .driver = {
  227. .owner = THIS_MODULE,
  228. .name = DRVNAME,
  229. },
  230. .probe = pkgtemp_probe,
  231. .remove = __devexit_p(pkgtemp_remove),
  232. };
  233. struct pdev_entry {
  234. struct list_head list;
  235. struct platform_device *pdev;
  236. unsigned int cpu;
  237. #ifdef CONFIG_SMP
  238. u16 phys_proc_id;
  239. #endif
  240. };
  241. static LIST_HEAD(pdev_list);
  242. static DEFINE_MUTEX(pdev_list_mutex);
  243. static int __cpuinit pkgtemp_device_add(unsigned int cpu)
  244. {
  245. int err;
  246. struct platform_device *pdev;
  247. struct pdev_entry *pdev_entry;
  248. struct cpuinfo_x86 *c = &cpu_data(cpu);
  249. if (!cpu_has(c, X86_FEATURE_PTS))
  250. return 0;
  251. mutex_lock(&pdev_list_mutex);
  252. #ifdef CONFIG_SMP
  253. /* Only keep the first entry in each package */
  254. list_for_each_entry(pdev_entry, &pdev_list, list) {
  255. if (c->phys_proc_id == pdev_entry->phys_proc_id) {
  256. err = 0; /* Not an error */
  257. goto exit;
  258. }
  259. }
  260. #endif
  261. pdev = platform_device_alloc(DRVNAME, cpu);
  262. if (!pdev) {
  263. err = -ENOMEM;
  264. pr_err("Device allocation failed\n");
  265. goto exit;
  266. }
  267. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  268. if (!pdev_entry) {
  269. err = -ENOMEM;
  270. goto exit_device_put;
  271. }
  272. err = platform_device_add(pdev);
  273. if (err) {
  274. pr_err("Device addition failed (%d)\n", 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)