k8temp.c 8.8 KB

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