k8temp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * k8temp.c - Linux kernel module for hardware monitoring
  3. *
  4. * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
  5. *
  6. * Inspired from the w83785 and amd756 drivers.
  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; either version 2 of the License, or
  11. * (at your option) any later version.
  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. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/pci.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/err.h>
  32. #include <linux/mutex.h>
  33. #include <asm/processor.h>
  34. #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
  35. #define REG_TEMP 0xe4
  36. #define SEL_PLACE 0x40
  37. #define SEL_CORE 0x04
  38. struct k8temp_data {
  39. struct device *hwmon_dev;
  40. struct mutex update_lock;
  41. const char *name;
  42. char valid; /* zero until following fields are valid */
  43. unsigned long last_updated; /* in jiffies */
  44. /* registers values */
  45. u8 sensorsp; /* sensor presence bits - SEL_CORE & SEL_PLACE */
  46. u32 temp[2][2]; /* core, place */
  47. u8 swap_core_select; /* meaning of SEL_CORE is inverted */
  48. };
  49. static struct k8temp_data *k8temp_update_device(struct device *dev)
  50. {
  51. struct k8temp_data *data = dev_get_drvdata(dev);
  52. struct pci_dev *pdev = to_pci_dev(dev);
  53. u8 tmp;
  54. mutex_lock(&data->update_lock);
  55. if (!data->valid
  56. || time_after(jiffies, data->last_updated + HZ)) {
  57. pci_read_config_byte(pdev, REG_TEMP, &tmp);
  58. tmp &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
  59. pci_write_config_byte(pdev, REG_TEMP, tmp);
  60. pci_read_config_dword(pdev, REG_TEMP, &data->temp[0][0]);
  61. if (data->sensorsp & SEL_PLACE) {
  62. tmp |= SEL_PLACE; /* Select sensor 1, core0 */
  63. pci_write_config_byte(pdev, REG_TEMP, tmp);
  64. pci_read_config_dword(pdev, REG_TEMP,
  65. &data->temp[0][1]);
  66. }
  67. if (data->sensorsp & SEL_CORE) {
  68. tmp &= ~SEL_PLACE; /* Select sensor 0, core1 */
  69. tmp |= SEL_CORE;
  70. pci_write_config_byte(pdev, REG_TEMP, tmp);
  71. pci_read_config_dword(pdev, REG_TEMP,
  72. &data->temp[1][0]);
  73. if (data->sensorsp & SEL_PLACE) {
  74. tmp |= SEL_PLACE; /* Select sensor 1, core1 */
  75. pci_write_config_byte(pdev, REG_TEMP, tmp);
  76. pci_read_config_dword(pdev, REG_TEMP,
  77. &data->temp[1][1]);
  78. }
  79. }
  80. data->last_updated = jiffies;
  81. data->valid = 1;
  82. }
  83. mutex_unlock(&data->update_lock);
  84. return data;
  85. }
  86. /*
  87. * Sysfs stuff
  88. */
  89. static ssize_t show_name(struct device *dev, struct device_attribute
  90. *devattr, char *buf)
  91. {
  92. struct k8temp_data *data = dev_get_drvdata(dev);
  93. return sprintf(buf, "%s\n", data->name);
  94. }
  95. static ssize_t show_temp(struct device *dev,
  96. struct device_attribute *devattr, char *buf)
  97. {
  98. struct sensor_device_attribute_2 *attr =
  99. to_sensor_dev_attr_2(devattr);
  100. int core = attr->nr;
  101. int place = attr->index;
  102. struct k8temp_data *data = k8temp_update_device(dev);
  103. if (data->swap_core_select)
  104. core = core ? 0 : 1;
  105. return sprintf(buf, "%d\n",
  106. TEMP_FROM_REG(data->temp[core][place]));
  107. }
  108. /* core, place */
  109. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
  110. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
  111. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0);
  112. static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1);
  113. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  114. static struct pci_device_id k8temp_ids[] = {
  115. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
  116. { 0 },
  117. };
  118. MODULE_DEVICE_TABLE(pci, k8temp_ids);
  119. static int __devinit k8temp_probe(struct pci_dev *pdev,
  120. const struct pci_device_id *id)
  121. {
  122. int err;
  123. u8 scfg;
  124. u32 temp;
  125. u8 model, stepping;
  126. struct k8temp_data *data;
  127. if (!(data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL))) {
  128. err = -ENOMEM;
  129. goto exit;
  130. }
  131. model = boot_cpu_data.x86_model;
  132. stepping = boot_cpu_data.x86_mask;
  133. switch (boot_cpu_data.x86) {
  134. case 0xf:
  135. /* feature available since SH-C0, exclude older revisions */
  136. if (((model == 4) && (stepping == 0)) ||
  137. ((model == 5) && (stepping <= 1))) {
  138. err = -ENODEV;
  139. goto exit_free;
  140. }
  141. /*
  142. * AMD NPT family 0fh, i.e. RevF and RevG:
  143. * meaning of SEL_CORE bit is inverted
  144. */
  145. if (model >= 0x40) {
  146. data->swap_core_select = 1;
  147. dev_warn(&pdev->dev, "Temperature readouts might be "
  148. "wrong - check erratum #141\n");
  149. }
  150. break;
  151. }
  152. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  153. scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
  154. pci_write_config_byte(pdev, REG_TEMP, scfg);
  155. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  156. if (scfg & (SEL_PLACE | SEL_CORE)) {
  157. dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
  158. err = -ENODEV;
  159. goto exit_free;
  160. }
  161. scfg |= (SEL_PLACE | SEL_CORE);
  162. pci_write_config_byte(pdev, REG_TEMP, scfg);
  163. /* now we know if we can change core and/or sensor */
  164. pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
  165. if (data->sensorsp & SEL_PLACE) {
  166. scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
  167. pci_write_config_byte(pdev, REG_TEMP, scfg);
  168. pci_read_config_dword(pdev, REG_TEMP, &temp);
  169. scfg |= SEL_CORE; /* prepare for next selection */
  170. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */
  171. data->sensorsp &= ~SEL_PLACE;
  172. }
  173. if (data->sensorsp & SEL_CORE) {
  174. scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
  175. pci_write_config_byte(pdev, REG_TEMP, scfg);
  176. pci_read_config_dword(pdev, REG_TEMP, &temp);
  177. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */
  178. data->sensorsp &= ~SEL_CORE;
  179. }
  180. data->name = "k8temp";
  181. mutex_init(&data->update_lock);
  182. dev_set_drvdata(&pdev->dev, data);
  183. /* Register sysfs hooks */
  184. err = device_create_file(&pdev->dev,
  185. &sensor_dev_attr_temp1_input.dev_attr);
  186. if (err)
  187. goto exit_remove;
  188. /* sensor can be changed and reports something */
  189. if (data->sensorsp & SEL_PLACE) {
  190. err = device_create_file(&pdev->dev,
  191. &sensor_dev_attr_temp2_input.dev_attr);
  192. if (err)
  193. goto exit_remove;
  194. }
  195. /* core can be changed and reports something */
  196. if (data->sensorsp & SEL_CORE) {
  197. err = device_create_file(&pdev->dev,
  198. &sensor_dev_attr_temp3_input.dev_attr);
  199. if (err)
  200. goto exit_remove;
  201. if (data->sensorsp & SEL_PLACE)
  202. err = device_create_file(&pdev->dev,
  203. &sensor_dev_attr_temp4_input.
  204. dev_attr);
  205. if (err)
  206. goto exit_remove;
  207. }
  208. err = device_create_file(&pdev->dev, &dev_attr_name);
  209. if (err)
  210. goto exit_remove;
  211. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  212. if (IS_ERR(data->hwmon_dev)) {
  213. err = PTR_ERR(data->hwmon_dev);
  214. goto exit_remove;
  215. }
  216. return 0;
  217. exit_remove:
  218. device_remove_file(&pdev->dev,
  219. &sensor_dev_attr_temp1_input.dev_attr);
  220. device_remove_file(&pdev->dev,
  221. &sensor_dev_attr_temp2_input.dev_attr);
  222. device_remove_file(&pdev->dev,
  223. &sensor_dev_attr_temp3_input.dev_attr);
  224. device_remove_file(&pdev->dev,
  225. &sensor_dev_attr_temp4_input.dev_attr);
  226. device_remove_file(&pdev->dev, &dev_attr_name);
  227. exit_free:
  228. dev_set_drvdata(&pdev->dev, NULL);
  229. kfree(data);
  230. exit:
  231. return err;
  232. }
  233. static void __devexit k8temp_remove(struct pci_dev *pdev)
  234. {
  235. struct k8temp_data *data = dev_get_drvdata(&pdev->dev);
  236. hwmon_device_unregister(data->hwmon_dev);
  237. device_remove_file(&pdev->dev,
  238. &sensor_dev_attr_temp1_input.dev_attr);
  239. device_remove_file(&pdev->dev,
  240. &sensor_dev_attr_temp2_input.dev_attr);
  241. device_remove_file(&pdev->dev,
  242. &sensor_dev_attr_temp3_input.dev_attr);
  243. device_remove_file(&pdev->dev,
  244. &sensor_dev_attr_temp4_input.dev_attr);
  245. device_remove_file(&pdev->dev, &dev_attr_name);
  246. dev_set_drvdata(&pdev->dev, NULL);
  247. kfree(data);
  248. }
  249. static struct pci_driver k8temp_driver = {
  250. .name = "k8temp",
  251. .id_table = k8temp_ids,
  252. .probe = k8temp_probe,
  253. .remove = __devexit_p(k8temp_remove),
  254. };
  255. static int __init k8temp_init(void)
  256. {
  257. return pci_register_driver(&k8temp_driver);
  258. }
  259. static void __exit k8temp_exit(void)
  260. {
  261. pci_unregister_driver(&k8temp_driver);
  262. }
  263. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  264. MODULE_DESCRIPTION("AMD K8 core temperature monitor");
  265. MODULE_LICENSE("GPL");
  266. module_init(k8temp_init)
  267. module_exit(k8temp_exit)