fam15h_power.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * fam15h_power.c - AMD Family 15h processor power monitoring
  3. *
  4. * Copyright (c) 2011 Advanced Micro Devices, Inc.
  5. * Author: Andreas Herrmann <andreas.herrmann3@amd.com>
  6. *
  7. *
  8. * This driver is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this driver; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/err.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-sysfs.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/bitops.h>
  27. #include <asm/processor.h>
  28. MODULE_DESCRIPTION("AMD Family 15h CPU processor power monitor");
  29. MODULE_AUTHOR("Andreas Herrmann <andreas.herrmann3@amd.com>");
  30. MODULE_LICENSE("GPL");
  31. /* Family 16h Northbridge's function 4 PCI ID */
  32. #define PCI_DEVICE_ID_AMD_16H_NB_F4 0x1534
  33. /* D18F3 */
  34. #define REG_NORTHBRIDGE_CAP 0xe8
  35. /* D18F4 */
  36. #define REG_PROCESSOR_TDP 0x1b8
  37. /* D18F5 */
  38. #define REG_TDP_RUNNING_AVERAGE 0xe0
  39. #define REG_TDP_LIMIT3 0xe8
  40. struct fam15h_power_data {
  41. struct device *hwmon_dev;
  42. unsigned int tdp_to_watts;
  43. unsigned int base_tdp;
  44. unsigned int processor_pwr_watts;
  45. };
  46. static ssize_t show_power(struct device *dev,
  47. struct device_attribute *attr, char *buf)
  48. {
  49. u32 val, tdp_limit, running_avg_range;
  50. s32 running_avg_capture;
  51. u64 curr_pwr_watts;
  52. struct pci_dev *f4 = to_pci_dev(dev);
  53. struct fam15h_power_data *data = dev_get_drvdata(dev);
  54. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
  55. REG_TDP_RUNNING_AVERAGE, &val);
  56. running_avg_capture = (val >> 4) & 0x3fffff;
  57. running_avg_capture = sign_extend32(running_avg_capture, 21);
  58. running_avg_range = (val & 0xf) + 1;
  59. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
  60. REG_TDP_LIMIT3, &val);
  61. tdp_limit = val >> 16;
  62. curr_pwr_watts = (tdp_limit + data->base_tdp) << running_avg_range;
  63. curr_pwr_watts -= running_avg_capture;
  64. curr_pwr_watts *= data->tdp_to_watts;
  65. /*
  66. * Convert to microWatt
  67. *
  68. * power is in Watt provided as fixed point integer with
  69. * scaling factor 1/(2^16). For conversion we use
  70. * (10^6)/(2^16) = 15625/(2^10)
  71. */
  72. curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range);
  73. return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts);
  74. }
  75. static DEVICE_ATTR(power1_input, S_IRUGO, show_power, NULL);
  76. static ssize_t show_power_crit(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct fam15h_power_data *data = dev_get_drvdata(dev);
  80. return sprintf(buf, "%u\n", data->processor_pwr_watts);
  81. }
  82. static DEVICE_ATTR(power1_crit, S_IRUGO, show_power_crit, NULL);
  83. static ssize_t show_name(struct device *dev,
  84. struct device_attribute *attr, char *buf)
  85. {
  86. return sprintf(buf, "fam15h_power\n");
  87. }
  88. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  89. static struct attribute *fam15h_power_attrs[] = {
  90. &dev_attr_power1_input.attr,
  91. &dev_attr_power1_crit.attr,
  92. &dev_attr_name.attr,
  93. NULL
  94. };
  95. static const struct attribute_group fam15h_power_attr_group = {
  96. .attrs = fam15h_power_attrs,
  97. };
  98. static bool __devinit fam15h_power_is_internal_node0(struct pci_dev *f4)
  99. {
  100. u32 val;
  101. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3),
  102. REG_NORTHBRIDGE_CAP, &val);
  103. if ((val & BIT(29)) && ((val >> 30) & 3))
  104. return false;
  105. return true;
  106. }
  107. /*
  108. * Newer BKDG versions have an updated recommendation on how to properly
  109. * initialize the running average range (was: 0xE, now: 0x9). This avoids
  110. * counter saturations resulting in bogus power readings.
  111. * We correct this value ourselves to cope with older BIOSes.
  112. */
  113. static const struct pci_device_id affected_device[] = {
  114. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
  115. { 0 }
  116. };
  117. static void tweak_runavg_range(struct pci_dev *pdev)
  118. {
  119. u32 val;
  120. /*
  121. * let this quirk apply only to the current version of the
  122. * northbridge, since future versions may change the behavior
  123. */
  124. if (!pci_match_id(affected_device, pdev))
  125. return;
  126. pci_bus_read_config_dword(pdev->bus,
  127. PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
  128. REG_TDP_RUNNING_AVERAGE, &val);
  129. if ((val & 0xf) != 0xe)
  130. return;
  131. val &= ~0xf;
  132. val |= 0x9;
  133. pci_bus_write_config_dword(pdev->bus,
  134. PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
  135. REG_TDP_RUNNING_AVERAGE, val);
  136. }
  137. #ifdef CONFIG_PM
  138. static int fam15h_power_resume(struct pci_dev *pdev)
  139. {
  140. tweak_runavg_range(pdev);
  141. return 0;
  142. }
  143. #else
  144. #define fam15h_power_resume NULL
  145. #endif
  146. static void __devinit fam15h_power_init_data(struct pci_dev *f4,
  147. struct fam15h_power_data *data)
  148. {
  149. u32 val;
  150. u64 tmp;
  151. pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val);
  152. data->base_tdp = val >> 16;
  153. tmp = val & 0xffff;
  154. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
  155. REG_TDP_LIMIT3, &val);
  156. data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f);
  157. tmp *= data->tdp_to_watts;
  158. /* result not allowed to be >= 256W */
  159. if ((tmp >> 16) >= 256)
  160. dev_warn(&f4->dev, "Bogus value for ProcessorPwrWatts "
  161. "(processor_pwr_watts>=%u)\n",
  162. (unsigned int) (tmp >> 16));
  163. /* convert to microWatt */
  164. data->processor_pwr_watts = (tmp * 15625) >> 10;
  165. }
  166. static int __devinit fam15h_power_probe(struct pci_dev *pdev,
  167. const struct pci_device_id *id)
  168. {
  169. struct fam15h_power_data *data;
  170. struct device *dev;
  171. int err;
  172. /*
  173. * though we ignore every other northbridge, we still have to
  174. * do the tweaking on _each_ node in MCM processors as the counters
  175. * are working hand-in-hand
  176. */
  177. tweak_runavg_range(pdev);
  178. if (!fam15h_power_is_internal_node0(pdev)) {
  179. err = -ENODEV;
  180. goto exit;
  181. }
  182. data = kzalloc(sizeof(struct fam15h_power_data), GFP_KERNEL);
  183. if (!data) {
  184. err = -ENOMEM;
  185. goto exit;
  186. }
  187. fam15h_power_init_data(pdev, data);
  188. dev = &pdev->dev;
  189. dev_set_drvdata(dev, data);
  190. err = sysfs_create_group(&dev->kobj, &fam15h_power_attr_group);
  191. if (err)
  192. goto exit_free_data;
  193. data->hwmon_dev = hwmon_device_register(dev);
  194. if (IS_ERR(data->hwmon_dev)) {
  195. err = PTR_ERR(data->hwmon_dev);
  196. goto exit_remove_group;
  197. }
  198. return 0;
  199. exit_remove_group:
  200. sysfs_remove_group(&dev->kobj, &fam15h_power_attr_group);
  201. exit_free_data:
  202. kfree(data);
  203. exit:
  204. return err;
  205. }
  206. static void __devexit fam15h_power_remove(struct pci_dev *pdev)
  207. {
  208. struct device *dev;
  209. struct fam15h_power_data *data;
  210. dev = &pdev->dev;
  211. data = dev_get_drvdata(dev);
  212. hwmon_device_unregister(data->hwmon_dev);
  213. sysfs_remove_group(&dev->kobj, &fam15h_power_attr_group);
  214. dev_set_drvdata(dev, NULL);
  215. kfree(data);
  216. }
  217. static DEFINE_PCI_DEVICE_TABLE(fam15h_power_id_table) = {
  218. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
  219. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
  220. {}
  221. };
  222. MODULE_DEVICE_TABLE(pci, fam15h_power_id_table);
  223. static struct pci_driver fam15h_power_driver = {
  224. .name = "fam15h_power",
  225. .id_table = fam15h_power_id_table,
  226. .probe = fam15h_power_probe,
  227. .remove = __devexit_p(fam15h_power_remove),
  228. .resume = fam15h_power_resume,
  229. };
  230. static int __init fam15h_power_init(void)
  231. {
  232. return pci_register_driver(&fam15h_power_driver);
  233. }
  234. static void __exit fam15h_power_exit(void)
  235. {
  236. pci_unregister_driver(&fam15h_power_driver);
  237. }
  238. module_init(fam15h_power_init)
  239. module_exit(fam15h_power_exit)