via-cputemp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include <asm/cpu_device_id.h>
  39. #define DRVNAME "via_cputemp"
  40. enum { SHOW_TEMP, SHOW_LABEL, SHOW_NAME };
  41. /*
  42. * Functions declaration
  43. */
  44. struct via_cputemp_data {
  45. struct device *hwmon_dev;
  46. const char *name;
  47. u8 vrm;
  48. u32 id;
  49. u32 msr_temp;
  50. u32 msr_vid;
  51. };
  52. /*
  53. * Sysfs stuff
  54. */
  55. static ssize_t show_name(struct device *dev, struct device_attribute
  56. *devattr, char *buf)
  57. {
  58. int ret;
  59. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  60. struct via_cputemp_data *data = dev_get_drvdata(dev);
  61. if (attr->index == SHOW_NAME)
  62. ret = sprintf(buf, "%s\n", data->name);
  63. else /* show label */
  64. ret = sprintf(buf, "Core %d\n", data->id);
  65. return ret;
  66. }
  67. static ssize_t show_temp(struct device *dev,
  68. struct device_attribute *devattr, char *buf)
  69. {
  70. struct via_cputemp_data *data = dev_get_drvdata(dev);
  71. u32 eax, edx;
  72. int err;
  73. err = rdmsr_safe_on_cpu(data->id, data->msr_temp, &eax, &edx);
  74. if (err)
  75. return -EAGAIN;
  76. return sprintf(buf, "%lu\n", ((unsigned long)eax & 0xffffff) * 1000);
  77. }
  78. static ssize_t show_cpu_vid(struct device *dev,
  79. struct device_attribute *devattr, char *buf)
  80. {
  81. struct via_cputemp_data *data = dev_get_drvdata(dev);
  82. u32 eax, edx;
  83. int err;
  84. err = rdmsr_safe_on_cpu(data->id, data->msr_vid, &eax, &edx);
  85. if (err)
  86. return -EAGAIN;
  87. return sprintf(buf, "%d\n", vid_from_reg(~edx & 0x7f, data->vrm));
  88. }
  89. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL,
  90. SHOW_TEMP);
  91. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_name, NULL, SHOW_LABEL);
  92. static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, SHOW_NAME);
  93. static struct attribute *via_cputemp_attributes[] = {
  94. &sensor_dev_attr_name.dev_attr.attr,
  95. &sensor_dev_attr_temp1_label.dev_attr.attr,
  96. &sensor_dev_attr_temp1_input.dev_attr.attr,
  97. NULL
  98. };
  99. static const struct attribute_group via_cputemp_group = {
  100. .attrs = via_cputemp_attributes,
  101. };
  102. /* Optional attributes */
  103. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_cpu_vid, NULL);
  104. static int via_cputemp_probe(struct platform_device *pdev)
  105. {
  106. struct via_cputemp_data *data;
  107. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  108. int err;
  109. u32 eax, edx;
  110. data = devm_kzalloc(&pdev->dev, sizeof(struct via_cputemp_data),
  111. GFP_KERNEL);
  112. if (!data)
  113. return -ENOMEM;
  114. data->id = pdev->id;
  115. data->name = "via_cputemp";
  116. switch (c->x86_model) {
  117. case 0xA:
  118. /* C7 A */
  119. case 0xD:
  120. /* C7 D */
  121. data->msr_temp = 0x1169;
  122. data->msr_vid = 0x198;
  123. break;
  124. case 0xF:
  125. /* Nano */
  126. data->msr_temp = 0x1423;
  127. break;
  128. default:
  129. return -ENODEV;
  130. }
  131. /* test if we can access the TEMPERATURE MSR */
  132. err = rdmsr_safe_on_cpu(data->id, data->msr_temp, &eax, &edx);
  133. if (err) {
  134. dev_err(&pdev->dev,
  135. "Unable to access TEMPERATURE MSR, giving up\n");
  136. return err;
  137. }
  138. platform_set_drvdata(pdev, data);
  139. err = sysfs_create_group(&pdev->dev.kobj, &via_cputemp_group);
  140. if (err)
  141. return err;
  142. if (data->msr_vid)
  143. data->vrm = vid_which_vrm();
  144. if (data->vrm) {
  145. err = device_create_file(&pdev->dev, &dev_attr_cpu0_vid);
  146. if (err)
  147. goto exit_remove;
  148. }
  149. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  150. if (IS_ERR(data->hwmon_dev)) {
  151. err = PTR_ERR(data->hwmon_dev);
  152. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  153. err);
  154. goto exit_remove;
  155. }
  156. return 0;
  157. exit_remove:
  158. if (data->vrm)
  159. device_remove_file(&pdev->dev, &dev_attr_cpu0_vid);
  160. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  161. return err;
  162. }
  163. static int via_cputemp_remove(struct platform_device *pdev)
  164. {
  165. struct via_cputemp_data *data = platform_get_drvdata(pdev);
  166. hwmon_device_unregister(data->hwmon_dev);
  167. if (data->vrm)
  168. device_remove_file(&pdev->dev, &dev_attr_cpu0_vid);
  169. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  170. return 0;
  171. }
  172. static struct platform_driver via_cputemp_driver = {
  173. .driver = {
  174. .owner = THIS_MODULE,
  175. .name = DRVNAME,
  176. },
  177. .probe = via_cputemp_probe,
  178. .remove = via_cputemp_remove,
  179. };
  180. struct pdev_entry {
  181. struct list_head list;
  182. struct platform_device *pdev;
  183. unsigned int cpu;
  184. };
  185. static LIST_HEAD(pdev_list);
  186. static DEFINE_MUTEX(pdev_list_mutex);
  187. static int __cpuinit via_cputemp_device_add(unsigned int cpu)
  188. {
  189. int err;
  190. struct platform_device *pdev;
  191. struct pdev_entry *pdev_entry;
  192. pdev = platform_device_alloc(DRVNAME, cpu);
  193. if (!pdev) {
  194. err = -ENOMEM;
  195. pr_err("Device allocation failed\n");
  196. goto exit;
  197. }
  198. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  199. if (!pdev_entry) {
  200. err = -ENOMEM;
  201. goto exit_device_put;
  202. }
  203. err = platform_device_add(pdev);
  204. if (err) {
  205. pr_err("Device addition failed (%d)\n", err);
  206. goto exit_device_free;
  207. }
  208. pdev_entry->pdev = pdev;
  209. pdev_entry->cpu = cpu;
  210. mutex_lock(&pdev_list_mutex);
  211. list_add_tail(&pdev_entry->list, &pdev_list);
  212. mutex_unlock(&pdev_list_mutex);
  213. return 0;
  214. exit_device_free:
  215. kfree(pdev_entry);
  216. exit_device_put:
  217. platform_device_put(pdev);
  218. exit:
  219. return err;
  220. }
  221. static void __cpuinit via_cputemp_device_remove(unsigned int cpu)
  222. {
  223. struct pdev_entry *p;
  224. mutex_lock(&pdev_list_mutex);
  225. list_for_each_entry(p, &pdev_list, list) {
  226. if (p->cpu == cpu) {
  227. platform_device_unregister(p->pdev);
  228. list_del(&p->list);
  229. mutex_unlock(&pdev_list_mutex);
  230. kfree(p);
  231. return;
  232. }
  233. }
  234. mutex_unlock(&pdev_list_mutex);
  235. }
  236. static int __cpuinit via_cputemp_cpu_callback(struct notifier_block *nfb,
  237. unsigned long action, void *hcpu)
  238. {
  239. unsigned int cpu = (unsigned long) hcpu;
  240. switch (action) {
  241. case CPU_ONLINE:
  242. case CPU_DOWN_FAILED:
  243. via_cputemp_device_add(cpu);
  244. break;
  245. case CPU_DOWN_PREPARE:
  246. via_cputemp_device_remove(cpu);
  247. break;
  248. }
  249. return NOTIFY_OK;
  250. }
  251. static struct notifier_block via_cputemp_cpu_notifier __refdata = {
  252. .notifier_call = via_cputemp_cpu_callback,
  253. };
  254. static const struct x86_cpu_id __initconst cputemp_ids[] = {
  255. { X86_VENDOR_CENTAUR, 6, 0xa, }, /* C7 A */
  256. { X86_VENDOR_CENTAUR, 6, 0xd, }, /* C7 D */
  257. { X86_VENDOR_CENTAUR, 6, 0xf, }, /* Nano */
  258. {}
  259. };
  260. MODULE_DEVICE_TABLE(x86cpu, cputemp_ids);
  261. static int __init via_cputemp_init(void)
  262. {
  263. int i, err;
  264. if (!x86_match_cpu(cputemp_ids))
  265. return -ENODEV;
  266. err = platform_driver_register(&via_cputemp_driver);
  267. if (err)
  268. goto exit;
  269. get_online_cpus();
  270. for_each_online_cpu(i) {
  271. struct cpuinfo_x86 *c = &cpu_data(i);
  272. if (c->x86 != 6)
  273. continue;
  274. if (c->x86_model < 0x0a)
  275. continue;
  276. if (c->x86_model > 0x0f) {
  277. pr_warn("Unknown CPU model 0x%x\n", c->x86_model);
  278. continue;
  279. }
  280. via_cputemp_device_add(i);
  281. }
  282. #ifndef CONFIG_HOTPLUG_CPU
  283. if (list_empty(&pdev_list)) {
  284. put_online_cpus();
  285. err = -ENODEV;
  286. goto exit_driver_unreg;
  287. }
  288. #endif
  289. register_hotcpu_notifier(&via_cputemp_cpu_notifier);
  290. put_online_cpus();
  291. return 0;
  292. #ifndef CONFIG_HOTPLUG_CPU
  293. exit_driver_unreg:
  294. platform_driver_unregister(&via_cputemp_driver);
  295. #endif
  296. exit:
  297. return err;
  298. }
  299. static void __exit via_cputemp_exit(void)
  300. {
  301. struct pdev_entry *p, *n;
  302. get_online_cpus();
  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. put_online_cpus();
  312. platform_driver_unregister(&via_cputemp_driver);
  313. }
  314. MODULE_AUTHOR("Harald Welte <HaraldWelte@viatech.com>");
  315. MODULE_DESCRIPTION("VIA CPU temperature monitor");
  316. MODULE_LICENSE("GPL");
  317. module_init(via_cputemp_init)
  318. module_exit(via_cputemp_exit)