k8temp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/pci.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. #include <asm/processor.h>
  33. #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
  34. #define REG_TEMP 0xe4
  35. #define SEL_PLACE 0x40
  36. #define SEL_CORE 0x04
  37. struct k8temp_data {
  38. struct device *hwmon_dev;
  39. struct mutex update_lock;
  40. const char *name;
  41. char valid; /* zero until following fields are valid */
  42. unsigned long last_updated; /* in jiffies */
  43. /* registers values */
  44. u8 sensorsp; /* sensor presence bits - SEL_CORE, SEL_PLACE */
  45. u32 temp[2][2]; /* core, place */
  46. u8 swap_core_select; /* meaning of SEL_CORE is inverted */
  47. u32 temp_offset;
  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. int temp;
  103. struct k8temp_data *data = k8temp_update_device(dev);
  104. if (data->swap_core_select && (data->sensorsp & SEL_CORE))
  105. core = core ? 0 : 1;
  106. temp = TEMP_FROM_REG(data->temp[core][place]) + data->temp_offset;
  107. return sprintf(buf, "%d\n", temp);
  108. }
  109. /* core, place */
  110. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
  111. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1);
  112. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 1, 0);
  113. static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 1, 1);
  114. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  115. static const struct pci_device_id k8temp_ids[] = {
  116. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
  117. { 0 },
  118. };
  119. MODULE_DEVICE_TABLE(pci, k8temp_ids);
  120. static int is_rev_g_desktop(u8 model)
  121. {
  122. u32 brandidx;
  123. if (model < 0x69)
  124. return 0;
  125. if (model == 0xc1 || model == 0x6c || model == 0x7c)
  126. return 0;
  127. /*
  128. * Differentiate between AM2 and ASB1.
  129. * See "Constructing the processor Name String" in "Revision
  130. * Guide for AMD NPT Family 0Fh Processors" (33610).
  131. */
  132. brandidx = cpuid_ebx(0x80000001);
  133. brandidx = (brandidx >> 9) & 0x1f;
  134. /* Single core */
  135. if ((model == 0x6f || model == 0x7f) &&
  136. (brandidx == 0x7 || brandidx == 0x9 || brandidx == 0xc))
  137. return 0;
  138. /* Dual core */
  139. if (model == 0x6b &&
  140. (brandidx == 0xb || brandidx == 0xc))
  141. return 0;
  142. return 1;
  143. }
  144. static int k8temp_probe(struct pci_dev *pdev,
  145. const struct pci_device_id *id)
  146. {
  147. int err;
  148. u8 scfg;
  149. u32 temp;
  150. u8 model, stepping;
  151. struct k8temp_data *data;
  152. data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
  153. if (!data)
  154. return -ENOMEM;
  155. model = boot_cpu_data.x86_model;
  156. stepping = boot_cpu_data.x86_mask;
  157. /* feature available since SH-C0, exclude older revisions */
  158. if ((model == 4 && stepping == 0) ||
  159. (model == 5 && stepping <= 1))
  160. return -ENODEV;
  161. /*
  162. * AMD NPT family 0fh, i.e. RevF and RevG:
  163. * meaning of SEL_CORE bit is inverted
  164. */
  165. if (model >= 0x40) {
  166. data->swap_core_select = 1;
  167. dev_warn(&pdev->dev,
  168. "Temperature readouts might be wrong - check erratum #141\n");
  169. }
  170. /*
  171. * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
  172. * additional offset, otherwise reported temperature is below
  173. * ambient temperature
  174. */
  175. if (is_rev_g_desktop(model))
  176. data->temp_offset = 21000;
  177. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  178. scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
  179. pci_write_config_byte(pdev, REG_TEMP, scfg);
  180. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  181. if (scfg & (SEL_PLACE | SEL_CORE)) {
  182. dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
  183. return -ENODEV;
  184. }
  185. scfg |= (SEL_PLACE | SEL_CORE);
  186. pci_write_config_byte(pdev, REG_TEMP, scfg);
  187. /* now we know if we can change core and/or sensor */
  188. pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
  189. if (data->sensorsp & SEL_PLACE) {
  190. scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
  191. pci_write_config_byte(pdev, REG_TEMP, scfg);
  192. pci_read_config_dword(pdev, REG_TEMP, &temp);
  193. scfg |= SEL_CORE; /* prepare for next selection */
  194. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
  195. data->sensorsp &= ~SEL_PLACE;
  196. }
  197. if (data->sensorsp & SEL_CORE) {
  198. scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
  199. pci_write_config_byte(pdev, REG_TEMP, scfg);
  200. pci_read_config_dword(pdev, REG_TEMP, &temp);
  201. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
  202. data->sensorsp &= ~SEL_CORE;
  203. }
  204. data->name = "k8temp";
  205. mutex_init(&data->update_lock);
  206. pci_set_drvdata(pdev, data);
  207. /* Register sysfs hooks */
  208. err = device_create_file(&pdev->dev,
  209. &sensor_dev_attr_temp1_input.dev_attr);
  210. if (err)
  211. goto exit_remove;
  212. /* sensor can be changed and reports something */
  213. if (data->sensorsp & SEL_PLACE) {
  214. err = device_create_file(&pdev->dev,
  215. &sensor_dev_attr_temp2_input.dev_attr);
  216. if (err)
  217. goto exit_remove;
  218. }
  219. /* core can be changed and reports something */
  220. if (data->sensorsp & SEL_CORE) {
  221. err = device_create_file(&pdev->dev,
  222. &sensor_dev_attr_temp3_input.dev_attr);
  223. if (err)
  224. goto exit_remove;
  225. if (data->sensorsp & SEL_PLACE) {
  226. err = device_create_file(&pdev->dev,
  227. &sensor_dev_attr_temp4_input.
  228. dev_attr);
  229. if (err)
  230. goto exit_remove;
  231. }
  232. }
  233. err = device_create_file(&pdev->dev, &dev_attr_name);
  234. if (err)
  235. goto exit_remove;
  236. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  237. if (IS_ERR(data->hwmon_dev)) {
  238. err = PTR_ERR(data->hwmon_dev);
  239. goto exit_remove;
  240. }
  241. return 0;
  242. exit_remove:
  243. device_remove_file(&pdev->dev,
  244. &sensor_dev_attr_temp1_input.dev_attr);
  245. device_remove_file(&pdev->dev,
  246. &sensor_dev_attr_temp2_input.dev_attr);
  247. device_remove_file(&pdev->dev,
  248. &sensor_dev_attr_temp3_input.dev_attr);
  249. device_remove_file(&pdev->dev,
  250. &sensor_dev_attr_temp4_input.dev_attr);
  251. device_remove_file(&pdev->dev, &dev_attr_name);
  252. return err;
  253. }
  254. static void k8temp_remove(struct pci_dev *pdev)
  255. {
  256. struct k8temp_data *data = pci_get_drvdata(pdev);
  257. hwmon_device_unregister(data->hwmon_dev);
  258. device_remove_file(&pdev->dev,
  259. &sensor_dev_attr_temp1_input.dev_attr);
  260. device_remove_file(&pdev->dev,
  261. &sensor_dev_attr_temp2_input.dev_attr);
  262. device_remove_file(&pdev->dev,
  263. &sensor_dev_attr_temp3_input.dev_attr);
  264. device_remove_file(&pdev->dev,
  265. &sensor_dev_attr_temp4_input.dev_attr);
  266. device_remove_file(&pdev->dev, &dev_attr_name);
  267. }
  268. static struct pci_driver k8temp_driver = {
  269. .name = "k8temp",
  270. .id_table = k8temp_ids,
  271. .probe = k8temp_probe,
  272. .remove = k8temp_remove,
  273. };
  274. module_pci_driver(k8temp_driver);
  275. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  276. MODULE_DESCRIPTION("AMD K8 core temperature monitor");
  277. MODULE_LICENSE("GPL");