k8temp.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 const 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 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. if (!(data = kzalloc(sizeof(struct k8temp_data), GFP_KERNEL))) {
  154. err = -ENOMEM;
  155. goto exit;
  156. }
  157. model = boot_cpu_data.x86_model;
  158. stepping = boot_cpu_data.x86_mask;
  159. /* feature available since SH-C0, exclude older revisions */
  160. if (((model == 4) && (stepping == 0)) ||
  161. ((model == 5) && (stepping <= 1))) {
  162. err = -ENODEV;
  163. goto exit_free;
  164. }
  165. /*
  166. * AMD NPT family 0fh, i.e. RevF and RevG:
  167. * meaning of SEL_CORE bit is inverted
  168. */
  169. if (model >= 0x40) {
  170. data->swap_core_select = 1;
  171. dev_warn(&pdev->dev, "Temperature readouts might be wrong - "
  172. "check erratum #141\n");
  173. }
  174. /*
  175. * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
  176. * additional offset, otherwise reported temperature is below
  177. * ambient temperature
  178. */
  179. if (is_rev_g_desktop(model))
  180. data->temp_offset = 21000;
  181. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  182. scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
  183. pci_write_config_byte(pdev, REG_TEMP, scfg);
  184. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  185. if (scfg & (SEL_PLACE | SEL_CORE)) {
  186. dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
  187. err = -ENODEV;
  188. goto exit_free;
  189. }
  190. scfg |= (SEL_PLACE | SEL_CORE);
  191. pci_write_config_byte(pdev, REG_TEMP, scfg);
  192. /* now we know if we can change core and/or sensor */
  193. pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
  194. if (data->sensorsp & SEL_PLACE) {
  195. scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
  196. pci_write_config_byte(pdev, REG_TEMP, scfg);
  197. pci_read_config_dword(pdev, REG_TEMP, &temp);
  198. scfg |= SEL_CORE; /* prepare for next selection */
  199. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */
  200. data->sensorsp &= ~SEL_PLACE;
  201. }
  202. if (data->sensorsp & SEL_CORE) {
  203. scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
  204. pci_write_config_byte(pdev, REG_TEMP, scfg);
  205. pci_read_config_dword(pdev, REG_TEMP, &temp);
  206. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is not likely */
  207. data->sensorsp &= ~SEL_CORE;
  208. }
  209. data->name = "k8temp";
  210. mutex_init(&data->update_lock);
  211. pci_set_drvdata(pdev, data);
  212. /* Register sysfs hooks */
  213. err = device_create_file(&pdev->dev,
  214. &sensor_dev_attr_temp1_input.dev_attr);
  215. if (err)
  216. goto exit_remove;
  217. /* sensor can be changed and reports something */
  218. if (data->sensorsp & SEL_PLACE) {
  219. err = device_create_file(&pdev->dev,
  220. &sensor_dev_attr_temp2_input.dev_attr);
  221. if (err)
  222. goto exit_remove;
  223. }
  224. /* core can be changed and reports something */
  225. if (data->sensorsp & SEL_CORE) {
  226. err = device_create_file(&pdev->dev,
  227. &sensor_dev_attr_temp3_input.dev_attr);
  228. if (err)
  229. goto exit_remove;
  230. if (data->sensorsp & SEL_PLACE) {
  231. err = device_create_file(&pdev->dev,
  232. &sensor_dev_attr_temp4_input.
  233. dev_attr);
  234. if (err)
  235. goto exit_remove;
  236. }
  237. }
  238. err = device_create_file(&pdev->dev, &dev_attr_name);
  239. if (err)
  240. goto exit_remove;
  241. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  242. if (IS_ERR(data->hwmon_dev)) {
  243. err = PTR_ERR(data->hwmon_dev);
  244. goto exit_remove;
  245. }
  246. return 0;
  247. exit_remove:
  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. exit_free:
  258. pci_set_drvdata(pdev, NULL);
  259. kfree(data);
  260. exit:
  261. return err;
  262. }
  263. static void __devexit k8temp_remove(struct pci_dev *pdev)
  264. {
  265. struct k8temp_data *data = pci_get_drvdata(pdev);
  266. hwmon_device_unregister(data->hwmon_dev);
  267. device_remove_file(&pdev->dev,
  268. &sensor_dev_attr_temp1_input.dev_attr);
  269. device_remove_file(&pdev->dev,
  270. &sensor_dev_attr_temp2_input.dev_attr);
  271. device_remove_file(&pdev->dev,
  272. &sensor_dev_attr_temp3_input.dev_attr);
  273. device_remove_file(&pdev->dev,
  274. &sensor_dev_attr_temp4_input.dev_attr);
  275. device_remove_file(&pdev->dev, &dev_attr_name);
  276. pci_set_drvdata(pdev, NULL);
  277. kfree(data);
  278. }
  279. static struct pci_driver k8temp_driver = {
  280. .name = "k8temp",
  281. .id_table = k8temp_ids,
  282. .probe = k8temp_probe,
  283. .remove = __devexit_p(k8temp_remove),
  284. };
  285. static int __init k8temp_init(void)
  286. {
  287. return pci_register_driver(&k8temp_driver);
  288. }
  289. static void __exit k8temp_exit(void)
  290. {
  291. pci_unregister_driver(&k8temp_driver);
  292. }
  293. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  294. MODULE_DESCRIPTION("AMD K8 core temperature monitor");
  295. MODULE_LICENSE("GPL");
  296. module_init(k8temp_init)
  297. module_exit(k8temp_exit)