k8temp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 && (data->sensorsp & SEL_CORE))
  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 DEFINE_PCI_DEVICE_TABLE(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 is_rev_g_desktop(u8 model)
  122. {
  123. u32 brandidx;
  124. if (model < 0x69)
  125. return 0;
  126. if (model == 0xc1 || model == 0x6c || model == 0x7c)
  127. return 0;
  128. /*
  129. * Differentiate between AM2 and ASB1.
  130. * See "Constructing the processor Name String" in "Revision
  131. * Guide for AMD NPT Family 0Fh Processors" (33610).
  132. */
  133. brandidx = cpuid_ebx(0x80000001);
  134. brandidx = (brandidx >> 9) & 0x1f;
  135. /* Single core */
  136. if ((model == 0x6f || model == 0x7f) &&
  137. (brandidx == 0x7 || brandidx == 0x9 || brandidx == 0xc))
  138. return 0;
  139. /* Dual core */
  140. if (model == 0x6b &&
  141. (brandidx == 0xb || brandidx == 0xc))
  142. return 0;
  143. return 1;
  144. }
  145. static int __devinit k8temp_probe(struct pci_dev *pdev,
  146. const struct pci_device_id *id)
  147. {
  148. int err;
  149. u8 scfg;
  150. u32 temp;
  151. u8 model, stepping;
  152. struct k8temp_data *data;
  153. data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL);
  154. if (!data) {
  155. err = -ENOMEM;
  156. goto exit;
  157. }
  158. model = boot_cpu_data.x86_model;
  159. stepping = boot_cpu_data.x86_mask;
  160. /* feature available since SH-C0, exclude older revisions */
  161. if (((model == 4) && (stepping == 0)) ||
  162. ((model == 5) && (stepping <= 1))) {
  163. err = -ENODEV;
  164. goto exit_free;
  165. }
  166. /*
  167. * AMD NPT family 0fh, i.e. RevF and RevG:
  168. * meaning of SEL_CORE bit is inverted
  169. */
  170. if (model >= 0x40) {
  171. data->swap_core_select = 1;
  172. dev_warn(&pdev->dev, "Temperature readouts might be wrong - "
  173. "check erratum #141\n");
  174. }
  175. /*
  176. * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
  177. * additional offset, otherwise reported temperature is below
  178. * ambient temperature
  179. */
  180. if (is_rev_g_desktop(model))
  181. data->temp_offset = 21000;
  182. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  183. scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
  184. pci_write_config_byte(pdev, REG_TEMP, scfg);
  185. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  186. if (scfg & (SEL_PLACE | SEL_CORE)) {
  187. dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
  188. err = -ENODEV;
  189. goto exit_free;
  190. }
  191. scfg |= (SEL_PLACE | SEL_CORE);
  192. pci_write_config_byte(pdev, REG_TEMP, scfg);
  193. /* now we know if we can change core and/or sensor */
  194. pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
  195. if (data->sensorsp & SEL_PLACE) {
  196. scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
  197. pci_write_config_byte(pdev, REG_TEMP, scfg);
  198. pci_read_config_dword(pdev, REG_TEMP, &temp);
  199. scfg |= SEL_CORE; /* prepare for next selection */
  200. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
  201. data->sensorsp &= ~SEL_PLACE;
  202. }
  203. if (data->sensorsp & SEL_CORE) {
  204. scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
  205. pci_write_config_byte(pdev, REG_TEMP, scfg);
  206. pci_read_config_dword(pdev, REG_TEMP, &temp);
  207. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
  208. data->sensorsp &= ~SEL_CORE;
  209. }
  210. data->name = "k8temp";
  211. mutex_init(&data->update_lock);
  212. pci_set_drvdata(pdev, data);
  213. /* Register sysfs hooks */
  214. err = device_create_file(&pdev->dev,
  215. &sensor_dev_attr_temp1_input.dev_attr);
  216. if (err)
  217. goto exit_remove;
  218. /* sensor can be changed and reports something */
  219. if (data->sensorsp & SEL_PLACE) {
  220. err = device_create_file(&pdev->dev,
  221. &sensor_dev_attr_temp2_input.dev_attr);
  222. if (err)
  223. goto exit_remove;
  224. }
  225. /* core can be changed and reports something */
  226. if (data->sensorsp & SEL_CORE) {
  227. err = device_create_file(&pdev->dev,
  228. &sensor_dev_attr_temp3_input.dev_attr);
  229. if (err)
  230. goto exit_remove;
  231. if (data->sensorsp & SEL_PLACE) {
  232. err = device_create_file(&pdev->dev,
  233. &sensor_dev_attr_temp4_input.
  234. dev_attr);
  235. if (err)
  236. goto exit_remove;
  237. }
  238. }
  239. err = device_create_file(&pdev->dev, &dev_attr_name);
  240. if (err)
  241. goto exit_remove;
  242. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  243. if (IS_ERR(data->hwmon_dev)) {
  244. err = PTR_ERR(data->hwmon_dev);
  245. goto exit_remove;
  246. }
  247. return 0;
  248. exit_remove:
  249. device_remove_file(&pdev->dev,
  250. &sensor_dev_attr_temp1_input.dev_attr);
  251. device_remove_file(&pdev->dev,
  252. &sensor_dev_attr_temp2_input.dev_attr);
  253. device_remove_file(&pdev->dev,
  254. &sensor_dev_attr_temp3_input.dev_attr);
  255. device_remove_file(&pdev->dev,
  256. &sensor_dev_attr_temp4_input.dev_attr);
  257. device_remove_file(&pdev->dev, &dev_attr_name);
  258. exit_free:
  259. pci_set_drvdata(pdev, NULL);
  260. kfree(data);
  261. exit:
  262. return err;
  263. }
  264. static void __devexit k8temp_remove(struct pci_dev *pdev)
  265. {
  266. struct k8temp_data *data = pci_get_drvdata(pdev);
  267. hwmon_device_unregister(data->hwmon_dev);
  268. device_remove_file(&pdev->dev,
  269. &sensor_dev_attr_temp1_input.dev_attr);
  270. device_remove_file(&pdev->dev,
  271. &sensor_dev_attr_temp2_input.dev_attr);
  272. device_remove_file(&pdev->dev,
  273. &sensor_dev_attr_temp3_input.dev_attr);
  274. device_remove_file(&pdev->dev,
  275. &sensor_dev_attr_temp4_input.dev_attr);
  276. device_remove_file(&pdev->dev, &dev_attr_name);
  277. pci_set_drvdata(pdev, NULL);
  278. kfree(data);
  279. }
  280. static struct pci_driver k8temp_driver = {
  281. .name = "k8temp",
  282. .id_table = k8temp_ids,
  283. .probe = k8temp_probe,
  284. .remove = __devexit_p(k8temp_remove),
  285. };
  286. static int __init k8temp_init(void)
  287. {
  288. return pci_register_driver(&k8temp_driver);
  289. }
  290. static void __exit k8temp_exit(void)
  291. {
  292. pci_unregister_driver(&k8temp_driver);
  293. }
  294. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  295. MODULE_DESCRIPTION("AMD K8 core temperature monitor");
  296. MODULE_LICENSE("GPL");
  297. module_init(k8temp_init)
  298. module_exit(k8temp_exit)