via-cputemp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * via-cputemp.c - Driver for VIA CPU core temperature monitoring
  3. * Copyright (C) 2009 VIA Technologies, Inc.
  4. *
  5. * based on existing coretemp.c, which is
  6. *
  7. * Copyright (C) 2007 Rudolf Marek <r.marek@assembler.cz>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/hwmon-vid.h>
  29. #include <linux/sysfs.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/err.h>
  32. #include <linux/mutex.h>
  33. #include <linux/list.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/cpu.h>
  36. #include <asm/msr.h>
  37. #include <asm/processor.h>
  38. #define DRVNAME "via_cputemp"
  39. enum { SHOW_TEMP, SHOW_LABEL, SHOW_NAME };
  40. /*
  41. * Functions declaration
  42. */
  43. struct via_cputemp_data {
  44. struct device *hwmon_dev;
  45. const char *name;
  46. u8 vrm;
  47. u32 id;
  48. u32 msr_temp;
  49. u32 msr_vid;
  50. };
  51. /*
  52. * Sysfs stuff
  53. */
  54. static ssize_t show_name(struct device *dev, struct device_attribute
  55. *devattr, char *buf)
  56. {
  57. int ret;
  58. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  59. struct via_cputemp_data *data = dev_get_drvdata(dev);
  60. if (attr->index == SHOW_NAME)
  61. ret = sprintf(buf, "%s\n", data->name);
  62. else /* show label */
  63. ret = sprintf(buf, "Core %d\n", data->id);
  64. return ret;
  65. }
  66. static ssize_t show_temp(struct device *dev,
  67. struct device_attribute *devattr, char *buf)
  68. {
  69. struct via_cputemp_data *data = dev_get_drvdata(dev);
  70. u32 eax, edx;
  71. int err;
  72. err = rdmsr_safe_on_cpu(data->id, data->msr_temp, &eax, &edx);
  73. if (err)
  74. return -EAGAIN;
  75. return sprintf(buf, "%lu\n", ((unsigned long)eax & 0xffffff) * 1000);
  76. }
  77. static ssize_t show_cpu_vid(struct device *dev,
  78. struct device_attribute *devattr, char *buf)
  79. {
  80. struct via_cputemp_data *data = dev_get_drvdata(dev);
  81. u32 eax, edx;
  82. int err;
  83. err = rdmsr_safe_on_cpu(data->id, data->msr_vid, &eax, &edx);
  84. if (err)
  85. return -EAGAIN;
  86. return sprintf(buf, "%d\n", vid_from_reg(~edx & 0x7f, data->vrm));
  87. }
  88. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
  89. SHOW_TEMP);
  90. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
  91. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
  92. static struct attribute *via_cputemp_attributes[] = {
  93. &sensor_dev_attr_name.dev_attr.attr,
  94. &sensor_dev_attr_temp1_label.dev_attr.attr,
  95. &sensor_dev_attr_temp1_input.dev_attr.attr,
  96. NULL
  97. };
  98. static const struct attribute_group via_cputemp_group = {
  99. .attrs = via_cputemp_attributes,
  100. };
  101. /* Optional attributes */
  102. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_cpu_vid, NULL);
  103. static int __devinit via_cputemp_probe(struct platform_device *pdev)
  104. {
  105. struct via_cputemp_data *data;
  106. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  107. int err;
  108. u32 eax, edx;
  109. data = kzalloc(sizeof(struct via_cputemp_data), GFP_KERNEL);
  110. if (!data) {
  111. err = -ENOMEM;
  112. dev_err(&pdev->dev, "Out of memory\n");
  113. goto exit;
  114. }
  115. data->id = pdev->id;
  116. data->name = "via_cputemp";
  117. switch (c->x86_model) {
  118. case 0xA:
  119. /* C7 A */
  120. case 0xD:
  121. /* C7 D */
  122. data->msr_temp = 0x1169;
  123. data->msr_vid = 0x198;
  124. break;
  125. case 0xF:
  126. /* Nano */
  127. data->msr_temp = 0x1423;
  128. break;
  129. default:
  130. err = -ENODEV;
  131. goto exit_free;
  132. }
  133. /* test if we can access the TEMPERATURE MSR */
  134. err = rdmsr_safe_on_cpu(data->id, data->msr_temp, &eax, &edx);
  135. if (err) {
  136. dev_err(&pdev->dev,
  137. "Unable to access TEMPERATURE MSR, giving up\n");
  138. goto exit_free;
  139. }
  140. platform_set_drvdata(pdev, data);
  141. err = sysfs_create_group(&pdev->dev.kobj, &via_cputemp_group);
  142. if (err)
  143. goto exit_free;
  144. if (data->msr_vid)
  145. data->vrm = vid_which_vrm();
  146. if (data->vrm) {
  147. err = device_create_file(&pdev->dev, &dev_attr_cpu0_vid);
  148. if (err)
  149. goto exit_remove;
  150. }
  151. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  152. if (IS_ERR(data->hwmon_dev)) {
  153. err = PTR_ERR(data->hwmon_dev);
  154. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  155. err);
  156. goto exit_remove;
  157. }
  158. return 0;
  159. exit_remove:
  160. if (data->vrm)
  161. device_remove_file(&pdev->dev, &dev_attr_cpu0_vid);
  162. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  163. exit_free:
  164. platform_set_drvdata(pdev, NULL);
  165. kfree(data);
  166. exit:
  167. return err;
  168. }
  169. static int __devexit via_cputemp_remove(struct platform_device *pdev)
  170. {
  171. struct via_cputemp_data *data = platform_get_drvdata(pdev);
  172. hwmon_device_unregister(data->hwmon_dev);
  173. if (data->vrm)
  174. device_remove_file(&pdev->dev, &dev_attr_cpu0_vid);
  175. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  176. platform_set_drvdata(pdev, NULL);
  177. kfree(data);
  178. return 0;
  179. }
  180. static struct platform_driver via_cputemp_driver = {
  181. .driver = {
  182. .owner = THIS_MODULE,
  183. .name = DRVNAME,
  184. },
  185. .probe = via_cputemp_probe,
  186. .remove = __devexit_p(via_cputemp_remove),
  187. };
  188. struct pdev_entry {
  189. struct list_head list;
  190. struct platform_device *pdev;
  191. unsigned int cpu;
  192. };
  193. static LIST_HEAD(pdev_list);
  194. static DEFINE_MUTEX(pdev_list_mutex);
  195. static int __cpuinit via_cputemp_device_add(unsigned int cpu)
  196. {
  197. int err;
  198. struct platform_device *pdev;
  199. struct pdev_entry *pdev_entry;
  200. pdev = platform_device_alloc(DRVNAME, cpu);
  201. if (!pdev) {
  202. err = -ENOMEM;
  203. pr_err("Device allocation failed\n");
  204. goto exit;
  205. }
  206. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  207. if (!pdev_entry) {
  208. err = -ENOMEM;
  209. goto exit_device_put;
  210. }
  211. err = platform_device_add(pdev);
  212. if (err) {
  213. pr_err("Device addition failed (%d)\n", err);
  214. goto exit_device_free;
  215. }
  216. pdev_entry->pdev = pdev;
  217. pdev_entry->cpu = cpu;
  218. mutex_lock(&pdev_list_mutex);
  219. list_add_tail(&pdev_entry->list, &pdev_list);
  220. mutex_unlock(&pdev_list_mutex);
  221. return 0;
  222. exit_device_free:
  223. kfree(pdev_entry);
  224. exit_device_put:
  225. platform_device_put(pdev);
  226. exit:
  227. return err;
  228. }
  229. static void __cpuinit via_cputemp_device_remove(unsigned int cpu)
  230. {
  231. struct pdev_entry *p;
  232. mutex_lock(&pdev_list_mutex);
  233. list_for_each_entry(p, &pdev_list, list) {
  234. if (p->cpu == cpu) {
  235. platform_device_unregister(p->pdev);
  236. list_del(&p->list);
  237. mutex_unlock(&pdev_list_mutex);
  238. kfree(p);
  239. return;
  240. }
  241. }
  242. mutex_unlock(&pdev_list_mutex);
  243. }
  244. static int __cpuinit via_cputemp_cpu_callback(struct notifier_block *nfb,
  245. unsigned long action, void *hcpu)
  246. {
  247. unsigned int cpu = (unsigned long) hcpu;
  248. switch (action) {
  249. case CPU_ONLINE:
  250. case CPU_DOWN_FAILED:
  251. via_cputemp_device_add(cpu);
  252. break;
  253. case CPU_DOWN_PREPARE:
  254. via_cputemp_device_remove(cpu);
  255. break;
  256. }
  257. return NOTIFY_OK;
  258. }
  259. static struct notifier_block via_cputemp_cpu_notifier __refdata = {
  260. .notifier_call = via_cputemp_cpu_callback,
  261. };
  262. static int __init via_cputemp_init(void)
  263. {
  264. int i, err;
  265. if (cpu_data(0).x86_vendor != X86_VENDOR_CENTAUR) {
  266. printk(KERN_DEBUG DRVNAME ": Not a VIA CPU\n");
  267. err = -ENODEV;
  268. goto exit;
  269. }
  270. err = platform_driver_register(&via_cputemp_driver);
  271. if (err)
  272. goto exit;
  273. for_each_online_cpu(i) {
  274. struct cpuinfo_x86 *c = &cpu_data(i);
  275. if (c->x86 != 6)
  276. continue;
  277. if (c->x86_model < 0x0a)
  278. continue;
  279. if (c->x86_model > 0x0f) {
  280. pr_warn("Unknown CPU model 0x%x\n", c->x86_model);
  281. continue;
  282. }
  283. via_cputemp_device_add(i);
  284. }
  285. #ifndef CONFIG_HOTPLUG_CPU
  286. if (list_empty(&pdev_list)) {
  287. err = -ENODEV;
  288. goto exit_driver_unreg;
  289. }
  290. #endif
  291. register_hotcpu_notifier(&via_cputemp_cpu_notifier);
  292. return 0;
  293. #ifndef CONFIG_HOTPLUG_CPU
  294. exit_driver_unreg:
  295. platform_driver_unregister(&via_cputemp_driver);
  296. #endif
  297. exit:
  298. return err;
  299. }
  300. static void __exit via_cputemp_exit(void)
  301. {
  302. struct pdev_entry *p, *n;
  303. unregister_hotcpu_notifier(&via_cputemp_cpu_notifier);
  304. mutex_lock(&pdev_list_mutex);
  305. list_for_each_entry_safe(p, n, &pdev_list, list) {
  306. platform_device_unregister(p->pdev);
  307. list_del(&p->list);
  308. kfree(p);
  309. }
  310. mutex_unlock(&pdev_list_mutex);
  311. platform_driver_unregister(&via_cputemp_driver);
  312. }
  313. MODULE_AUTHOR("Harald Welte <HaraldWelte@viatech.com>");
  314. MODULE_DESCRIPTION("VIA CPU temperature monitor");
  315. MODULE_LICENSE("GPL");
  316. module_init(via_cputemp_init)
  317. module_exit(via_cputemp_exit)