k8temp.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * k8temp.c - Linux kernel module for hardware monitoring
  4. *
  5. * Copyright (C) 2006 Rudolf Marek <r.marek@assembler.cz>
  6. *
  7. * Inspired from the w83785 and amd756 drivers.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/pci.h>
  13. #include <linux/hwmon.h>
  14. #include <linux/err.h>
  15. #include <linux/mutex.h>
  16. #include <asm/processor.h>
  17. #define TEMP_FROM_REG(val) (((((val) >> 16) & 0xff) - 49) * 1000)
  18. #define REG_TEMP 0xe4
  19. #define SEL_PLACE 0x40
  20. #define SEL_CORE 0x04
  21. struct k8temp_data {
  22. struct mutex update_lock;
  23. /* registers values */
  24. u8 sensorsp; /* sensor presence bits - SEL_CORE, SEL_PLACE */
  25. u8 swap_core_select; /* meaning of SEL_CORE is inverted */
  26. u32 temp_offset;
  27. };
  28. static const struct pci_device_id k8temp_ids[] = {
  29. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_K8_NB_MISC) },
  30. { 0 },
  31. };
  32. MODULE_DEVICE_TABLE(pci, k8temp_ids);
  33. static int is_rev_g_desktop(u8 model)
  34. {
  35. u32 brandidx;
  36. if (model < 0x69)
  37. return 0;
  38. if (model == 0xc1 || model == 0x6c || model == 0x7c)
  39. return 0;
  40. /*
  41. * Differentiate between AM2 and ASB1.
  42. * See "Constructing the processor Name String" in "Revision
  43. * Guide for AMD NPT Family 0Fh Processors" (33610).
  44. */
  45. brandidx = cpuid_ebx(0x80000001);
  46. brandidx = (brandidx >> 9) & 0x1f;
  47. /* Single core */
  48. if ((model == 0x6f || model == 0x7f) &&
  49. (brandidx == 0x7 || brandidx == 0x9 || brandidx == 0xc))
  50. return 0;
  51. /* Dual core */
  52. if (model == 0x6b &&
  53. (brandidx == 0xb || brandidx == 0xc))
  54. return 0;
  55. return 1;
  56. }
  57. static umode_t
  58. k8temp_is_visible(const void *drvdata, enum hwmon_sensor_types type,
  59. u32 attr, int channel)
  60. {
  61. const struct k8temp_data *data = drvdata;
  62. if ((channel & 1) && !(data->sensorsp & SEL_PLACE))
  63. return 0;
  64. if ((channel & 2) && !(data->sensorsp & SEL_CORE))
  65. return 0;
  66. return 0444;
  67. }
  68. static int
  69. k8temp_read(struct device *dev, enum hwmon_sensor_types type,
  70. u32 attr, int channel, long *val)
  71. {
  72. struct k8temp_data *data = dev_get_drvdata(dev);
  73. struct pci_dev *pdev = to_pci_dev(dev->parent);
  74. int core, place;
  75. u32 temp;
  76. u8 tmp;
  77. core = (channel >> 1) & 1;
  78. place = channel & 1;
  79. core ^= data->swap_core_select;
  80. mutex_lock(&data->update_lock);
  81. pci_read_config_byte(pdev, REG_TEMP, &tmp);
  82. tmp &= ~(SEL_PLACE | SEL_CORE);
  83. if (core)
  84. tmp |= SEL_CORE;
  85. if (place)
  86. tmp |= SEL_PLACE;
  87. pci_write_config_byte(pdev, REG_TEMP, tmp);
  88. pci_read_config_dword(pdev, REG_TEMP, &temp);
  89. mutex_unlock(&data->update_lock);
  90. *val = TEMP_FROM_REG(temp) + data->temp_offset;
  91. return 0;
  92. }
  93. static const struct hwmon_ops k8temp_ops = {
  94. .is_visible = k8temp_is_visible,
  95. .read = k8temp_read,
  96. };
  97. static const struct hwmon_channel_info *k8temp_info[] = {
  98. HWMON_CHANNEL_INFO(temp,
  99. HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT, HWMON_T_INPUT),
  100. NULL
  101. };
  102. static const struct hwmon_chip_info k8temp_chip_info = {
  103. .ops = &k8temp_ops,
  104. .info = k8temp_info,
  105. };
  106. static int k8temp_probe(struct pci_dev *pdev,
  107. const struct pci_device_id *id)
  108. {
  109. u8 scfg;
  110. u32 temp;
  111. u8 model, stepping;
  112. struct k8temp_data *data;
  113. struct device *hwmon_dev;
  114. data = devm_kzalloc(&pdev->dev, sizeof(struct k8temp_data), GFP_KERNEL);
  115. if (!data)
  116. return -ENOMEM;
  117. model = boot_cpu_data.x86_model;
  118. stepping = boot_cpu_data.x86_stepping;
  119. /* feature available since SH-C0, exclude older revisions */
  120. if ((model == 4 && stepping == 0) ||
  121. (model == 5 && stepping <= 1))
  122. return -ENODEV;
  123. /*
  124. * AMD NPT family 0fh, i.e. RevF and RevG:
  125. * meaning of SEL_CORE bit is inverted
  126. */
  127. if (model >= 0x40) {
  128. data->swap_core_select = 1;
  129. dev_warn(&pdev->dev,
  130. "Temperature readouts might be wrong - check erratum #141\n");
  131. }
  132. /*
  133. * RevG desktop CPUs (i.e. no socket S1G1 or ASB1 parts) need
  134. * additional offset, otherwise reported temperature is below
  135. * ambient temperature
  136. */
  137. if (is_rev_g_desktop(model))
  138. data->temp_offset = 21000;
  139. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  140. scfg &= ~(SEL_PLACE | SEL_CORE); /* Select sensor 0, core0 */
  141. pci_write_config_byte(pdev, REG_TEMP, scfg);
  142. pci_read_config_byte(pdev, REG_TEMP, &scfg);
  143. if (scfg & (SEL_PLACE | SEL_CORE)) {
  144. dev_err(&pdev->dev, "Configuration bit(s) stuck at 1!\n");
  145. return -ENODEV;
  146. }
  147. scfg |= (SEL_PLACE | SEL_CORE);
  148. pci_write_config_byte(pdev, REG_TEMP, scfg);
  149. /* now we know if we can change core and/or sensor */
  150. pci_read_config_byte(pdev, REG_TEMP, &data->sensorsp);
  151. if (data->sensorsp & SEL_PLACE) {
  152. scfg &= ~SEL_CORE; /* Select sensor 1, core0 */
  153. pci_write_config_byte(pdev, REG_TEMP, scfg);
  154. pci_read_config_dword(pdev, REG_TEMP, &temp);
  155. scfg |= SEL_CORE; /* prepare for next selection */
  156. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
  157. data->sensorsp &= ~SEL_PLACE;
  158. }
  159. if (data->sensorsp & SEL_CORE) {
  160. scfg &= ~SEL_PLACE; /* Select sensor 0, core1 */
  161. pci_write_config_byte(pdev, REG_TEMP, scfg);
  162. pci_read_config_dword(pdev, REG_TEMP, &temp);
  163. if (!((temp >> 16) & 0xff)) /* if temp is 0 -49C is unlikely */
  164. data->sensorsp &= ~SEL_CORE;
  165. }
  166. mutex_init(&data->update_lock);
  167. hwmon_dev = devm_hwmon_device_register_with_info(&pdev->dev,
  168. "k8temp",
  169. data,
  170. &k8temp_chip_info,
  171. NULL);
  172. return PTR_ERR_OR_ZERO(hwmon_dev);
  173. }
  174. static struct pci_driver k8temp_driver = {
  175. .name = "k8temp",
  176. .id_table = k8temp_ids,
  177. .probe = k8temp_probe,
  178. };
  179. module_pci_driver(k8temp_driver);
  180. MODULE_AUTHOR("Rudolf Marek <r.marek@assembler.cz>");
  181. MODULE_DESCRIPTION("AMD K8 core temperature monitor");
  182. MODULE_LICENSE("GPL");