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. .name = DRVNAME,
  175. },
  176. .probe = via_cputemp_probe,
  177. .remove = via_cputemp_remove,
  178. };
  179. struct pdev_entry {
  180. struct list_head list;
  181. struct platform_device *pdev;
  182. unsigned int cpu;
  183. };
  184. static LIST_HEAD(pdev_list);
  185. static DEFINE_MUTEX(pdev_list_mutex);
  186. static int via_cputemp_device_add(unsigned int cpu)
  187. {
  188. int err;
  189. struct platform_device *pdev;
  190. struct pdev_entry *pdev_entry;
  191. pdev = platform_device_alloc(DRVNAME, cpu);
  192. if (!pdev) {
  193. err = -ENOMEM;
  194. pr_err("Device allocation failed\n");
  195. goto exit;
  196. }
  197. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  198. if (!pdev_entry) {
  199. err = -ENOMEM;
  200. goto exit_device_put;
  201. }
  202. err = platform_device_add(pdev);
  203. if (err) {
  204. pr_err("Device addition failed (%d)\n", err);
  205. goto exit_device_free;
  206. }
  207. pdev_entry->pdev = pdev;
  208. pdev_entry->cpu = cpu;
  209. mutex_lock(&pdev_list_mutex);
  210. list_add_tail(&pdev_entry->list, &pdev_list);
  211. mutex_unlock(&pdev_list_mutex);
  212. return 0;
  213. exit_device_free:
  214. kfree(pdev_entry);
  215. exit_device_put:
  216. platform_device_put(pdev);
  217. exit:
  218. return err;
  219. }
  220. static void via_cputemp_device_remove(unsigned int cpu)
  221. {
  222. struct pdev_entry *p;
  223. mutex_lock(&pdev_list_mutex);
  224. list_for_each_entry(p, &pdev_list, list) {
  225. if (p->cpu == cpu) {
  226. platform_device_unregister(p->pdev);
  227. list_del(&p->list);
  228. mutex_unlock(&pdev_list_mutex);
  229. kfree(p);
  230. return;
  231. }
  232. }
  233. mutex_unlock(&pdev_list_mutex);
  234. }
  235. static int via_cputemp_cpu_callback(struct notifier_block *nfb,
  236. unsigned long action, void *hcpu)
  237. {
  238. unsigned int cpu = (unsigned long) hcpu;
  239. switch (action) {
  240. case CPU_ONLINE:
  241. case CPU_DOWN_FAILED:
  242. via_cputemp_device_add(cpu);
  243. break;
  244. case CPU_DOWN_PREPARE:
  245. via_cputemp_device_remove(cpu);
  246. break;
  247. }
  248. return NOTIFY_OK;
  249. }
  250. static struct notifier_block via_cputemp_cpu_notifier __refdata = {
  251. .notifier_call = via_cputemp_cpu_callback,
  252. };
  253. static const struct x86_cpu_id __initconst cputemp_ids[] = {
  254. { X86_VENDOR_CENTAUR, 6, 0xa, }, /* C7 A */
  255. { X86_VENDOR_CENTAUR, 6, 0xd, }, /* C7 D */
  256. { X86_VENDOR_CENTAUR, 6, 0xf, }, /* Nano */
  257. {}
  258. };
  259. MODULE_DEVICE_TABLE(x86cpu, cputemp_ids);
  260. static int __init via_cputemp_init(void)
  261. {
  262. int i, err;
  263. if (!x86_match_cpu(cputemp_ids))
  264. return -ENODEV;
  265. err = platform_driver_register(&via_cputemp_driver);
  266. if (err)
  267. goto exit;
  268. cpu_notifier_register_begin();
  269. for_each_online_cpu(i) {
  270. struct cpuinfo_x86 *c = &cpu_data(i);
  271. if (c->x86 != 6)
  272. continue;
  273. if (c->x86_model < 0x0a)
  274. continue;
  275. if (c->x86_model > 0x0f) {
  276. pr_warn("Unknown CPU model 0x%x\n", c->x86_model);
  277. continue;
  278. }
  279. via_cputemp_device_add(i);
  280. }
  281. #ifndef CONFIG_HOTPLUG_CPU
  282. if (list_empty(&pdev_list)) {
  283. cpu_notifier_register_done();
  284. err = -ENODEV;
  285. goto exit_driver_unreg;
  286. }
  287. #endif
  288. __register_hotcpu_notifier(&via_cputemp_cpu_notifier);
  289. cpu_notifier_register_done();
  290. return 0;
  291. #ifndef CONFIG_HOTPLUG_CPU
  292. exit_driver_unreg:
  293. platform_driver_unregister(&via_cputemp_driver);
  294. #endif
  295. exit:
  296. return err;
  297. }
  298. static void __exit via_cputemp_exit(void)
  299. {
  300. struct pdev_entry *p, *n;
  301. cpu_notifier_register_begin();
  302. __unregister_hotcpu_notifier(&via_cputemp_cpu_notifier);
  303. mutex_lock(&pdev_list_mutex);
  304. list_for_each_entry_safe(p, n, &pdev_list, list) {
  305. platform_device_unregister(p->pdev);
  306. list_del(&p->list);
  307. kfree(p);
  308. }
  309. mutex_unlock(&pdev_list_mutex);
  310. cpu_notifier_register_done();
  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)