via-cputemp.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * via-cputemp.c - Driver for VIA CPU core temperature monitoring
  4. * Copyright (C) 2009 VIA Technologies, Inc.
  5. *
  6. * based on existing coretemp.c, which is
  7. *
  8. * Copyright (C) 2007 Rudolf Marek <[email protected]>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/hwmon.h>
  15. #include <linux/hwmon-vid.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/hwmon-sysfs.h>
  18. #include <linux/err.h>
  19. #include <linux/mutex.h>
  20. #include <linux/list.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/cpu.h>
  23. #include <asm/msr.h>
  24. #include <asm/processor.h>
  25. #include <asm/cpu_device_id.h>
  26. #define DRVNAME "via_cputemp"
  27. enum { SHOW_TEMP, SHOW_LABEL, SHOW_NAME };
  28. /*
  29. * Functions declaration
  30. */
  31. struct via_cputemp_data {
  32. struct device *hwmon_dev;
  33. const char *name;
  34. u8 vrm;
  35. u32 id;
  36. u32 msr_temp;
  37. u32 msr_vid;
  38. };
  39. /*
  40. * Sysfs stuff
  41. */
  42. static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
  43. char *buf)
  44. {
  45. int ret;
  46. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  47. struct via_cputemp_data *data = dev_get_drvdata(dev);
  48. if (attr->index == SHOW_NAME)
  49. ret = sprintf(buf, "%s\n", data->name);
  50. else /* show label */
  51. ret = sprintf(buf, "Core %d\n", data->id);
  52. return ret;
  53. }
  54. static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
  55. char *buf)
  56. {
  57. struct via_cputemp_data *data = dev_get_drvdata(dev);
  58. u32 eax, edx;
  59. int err;
  60. err = rdmsr_safe_on_cpu(data->id, data->msr_temp, &eax, &edx);
  61. if (err)
  62. return -EAGAIN;
  63. return sprintf(buf, "%lu\n", ((unsigned long)eax & 0xffffff) * 1000);
  64. }
  65. static ssize_t cpu0_vid_show(struct device *dev,
  66. struct device_attribute *devattr, char *buf)
  67. {
  68. struct via_cputemp_data *data = dev_get_drvdata(dev);
  69. u32 eax, edx;
  70. int err;
  71. err = rdmsr_safe_on_cpu(data->id, data->msr_vid, &eax, &edx);
  72. if (err)
  73. return -EAGAIN;
  74. return sprintf(buf, "%d\n", vid_from_reg(~edx & 0x7f, data->vrm));
  75. }
  76. static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, SHOW_TEMP);
  77. static SENSOR_DEVICE_ATTR_RO(temp1_label, name, SHOW_LABEL);
  78. static SENSOR_DEVICE_ATTR_RO(name, name, SHOW_NAME);
  79. static struct attribute *via_cputemp_attributes[] = {
  80. &sensor_dev_attr_name.dev_attr.attr,
  81. &sensor_dev_attr_temp1_label.dev_attr.attr,
  82. &sensor_dev_attr_temp1_input.dev_attr.attr,
  83. NULL
  84. };
  85. static const struct attribute_group via_cputemp_group = {
  86. .attrs = via_cputemp_attributes,
  87. };
  88. /* Optional attributes */
  89. static DEVICE_ATTR_RO(cpu0_vid);
  90. static int via_cputemp_probe(struct platform_device *pdev)
  91. {
  92. struct via_cputemp_data *data;
  93. struct cpuinfo_x86 *c = &cpu_data(pdev->id);
  94. int err;
  95. u32 eax, edx;
  96. data = devm_kzalloc(&pdev->dev, sizeof(struct via_cputemp_data),
  97. GFP_KERNEL);
  98. if (!data)
  99. return -ENOMEM;
  100. data->id = pdev->id;
  101. data->name = "via_cputemp";
  102. if (c->x86 == 7) {
  103. data->msr_temp = 0x1423;
  104. } else {
  105. switch (c->x86_model) {
  106. case 0xA:
  107. /* C7 A */
  108. case 0xD:
  109. /* C7 D */
  110. data->msr_temp = 0x1169;
  111. data->msr_vid = 0x198;
  112. break;
  113. case 0xF:
  114. /* Nano */
  115. data->msr_temp = 0x1423;
  116. break;
  117. default:
  118. return -ENODEV;
  119. }
  120. }
  121. /* test if we can access the TEMPERATURE MSR */
  122. err = rdmsr_safe_on_cpu(data->id, data->msr_temp, &eax, &edx);
  123. if (err) {
  124. dev_err(&pdev->dev,
  125. "Unable to access TEMPERATURE MSR, giving up\n");
  126. return err;
  127. }
  128. platform_set_drvdata(pdev, data);
  129. err = sysfs_create_group(&pdev->dev.kobj, &via_cputemp_group);
  130. if (err)
  131. return err;
  132. if (data->msr_vid)
  133. data->vrm = vid_which_vrm();
  134. if (data->vrm) {
  135. err = device_create_file(&pdev->dev, &dev_attr_cpu0_vid);
  136. if (err)
  137. goto exit_remove;
  138. }
  139. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  140. if (IS_ERR(data->hwmon_dev)) {
  141. err = PTR_ERR(data->hwmon_dev);
  142. dev_err(&pdev->dev, "Class registration failed (%d)\n",
  143. err);
  144. goto exit_remove;
  145. }
  146. return 0;
  147. exit_remove:
  148. if (data->vrm)
  149. device_remove_file(&pdev->dev, &dev_attr_cpu0_vid);
  150. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  151. return err;
  152. }
  153. static int via_cputemp_remove(struct platform_device *pdev)
  154. {
  155. struct via_cputemp_data *data = platform_get_drvdata(pdev);
  156. hwmon_device_unregister(data->hwmon_dev);
  157. if (data->vrm)
  158. device_remove_file(&pdev->dev, &dev_attr_cpu0_vid);
  159. sysfs_remove_group(&pdev->dev.kobj, &via_cputemp_group);
  160. return 0;
  161. }
  162. static struct platform_driver via_cputemp_driver = {
  163. .driver = {
  164. .name = DRVNAME,
  165. },
  166. .probe = via_cputemp_probe,
  167. .remove = via_cputemp_remove,
  168. };
  169. struct pdev_entry {
  170. struct list_head list;
  171. struct platform_device *pdev;
  172. unsigned int cpu;
  173. };
  174. static LIST_HEAD(pdev_list);
  175. static DEFINE_MUTEX(pdev_list_mutex);
  176. static int via_cputemp_online(unsigned int cpu)
  177. {
  178. int err;
  179. struct platform_device *pdev;
  180. struct pdev_entry *pdev_entry;
  181. pdev = platform_device_alloc(DRVNAME, cpu);
  182. if (!pdev) {
  183. err = -ENOMEM;
  184. pr_err("Device allocation failed\n");
  185. goto exit;
  186. }
  187. pdev_entry = kzalloc(sizeof(struct pdev_entry), GFP_KERNEL);
  188. if (!pdev_entry) {
  189. err = -ENOMEM;
  190. goto exit_device_put;
  191. }
  192. err = platform_device_add(pdev);
  193. if (err) {
  194. pr_err("Device addition failed (%d)\n", err);
  195. goto exit_device_free;
  196. }
  197. pdev_entry->pdev = pdev;
  198. pdev_entry->cpu = cpu;
  199. mutex_lock(&pdev_list_mutex);
  200. list_add_tail(&pdev_entry->list, &pdev_list);
  201. mutex_unlock(&pdev_list_mutex);
  202. return 0;
  203. exit_device_free:
  204. kfree(pdev_entry);
  205. exit_device_put:
  206. platform_device_put(pdev);
  207. exit:
  208. return err;
  209. }
  210. static int via_cputemp_down_prep(unsigned int cpu)
  211. {
  212. struct pdev_entry *p;
  213. mutex_lock(&pdev_list_mutex);
  214. list_for_each_entry(p, &pdev_list, list) {
  215. if (p->cpu == cpu) {
  216. platform_device_unregister(p->pdev);
  217. list_del(&p->list);
  218. mutex_unlock(&pdev_list_mutex);
  219. kfree(p);
  220. return 0;
  221. }
  222. }
  223. mutex_unlock(&pdev_list_mutex);
  224. return 0;
  225. }
  226. static const struct x86_cpu_id __initconst cputemp_ids[] = {
  227. X86_MATCH_VENDOR_FAM_MODEL(CENTAUR, 6, X86_CENTAUR_FAM6_C7_A, NULL),
  228. X86_MATCH_VENDOR_FAM_MODEL(CENTAUR, 6, X86_CENTAUR_FAM6_C7_D, NULL),
  229. X86_MATCH_VENDOR_FAM_MODEL(CENTAUR, 6, X86_CENTAUR_FAM6_NANO, NULL),
  230. X86_MATCH_VENDOR_FAM_MODEL(CENTAUR, 7, X86_MODEL_ANY, NULL),
  231. {}
  232. };
  233. MODULE_DEVICE_TABLE(x86cpu, cputemp_ids);
  234. static enum cpuhp_state via_temp_online;
  235. static int __init via_cputemp_init(void)
  236. {
  237. int err;
  238. if (!x86_match_cpu(cputemp_ids))
  239. return -ENODEV;
  240. err = platform_driver_register(&via_cputemp_driver);
  241. if (err)
  242. goto exit;
  243. err = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hwmon/via:online",
  244. via_cputemp_online, via_cputemp_down_prep);
  245. if (err < 0)
  246. goto exit_driver_unreg;
  247. via_temp_online = err;
  248. #ifndef CONFIG_HOTPLUG_CPU
  249. if (list_empty(&pdev_list)) {
  250. err = -ENODEV;
  251. goto exit_hp_unreg;
  252. }
  253. #endif
  254. return 0;
  255. #ifndef CONFIG_HOTPLUG_CPU
  256. exit_hp_unreg:
  257. cpuhp_remove_state_nocalls(via_temp_online);
  258. #endif
  259. exit_driver_unreg:
  260. platform_driver_unregister(&via_cputemp_driver);
  261. exit:
  262. return err;
  263. }
  264. static void __exit via_cputemp_exit(void)
  265. {
  266. cpuhp_remove_state(via_temp_online);
  267. platform_driver_unregister(&via_cputemp_driver);
  268. }
  269. MODULE_AUTHOR("Harald Welte <[email protected]>");
  270. MODULE_DESCRIPTION("VIA CPU temperature monitor");
  271. MODULE_LICENSE("GPL");
  272. module_init(via_cputemp_init)
  273. module_exit(via_cputemp_exit)