fam15h_power.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * fam15h_power.c - AMD Family 15h processor power monitoring
  4. *
  5. * Copyright (c) 2011-2016 Advanced Micro Devices, Inc.
  6. * Author: Andreas Herrmann <[email protected]>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/hwmon.h>
  10. #include <linux/hwmon-sysfs.h>
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/bitops.h>
  15. #include <linux/cpu.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/time.h>
  18. #include <linux/sched.h>
  19. #include <asm/processor.h>
  20. #include <asm/msr.h>
  21. MODULE_DESCRIPTION("AMD Family 15h CPU processor power monitor");
  22. MODULE_AUTHOR("Andreas Herrmann <[email protected]>");
  23. MODULE_LICENSE("GPL");
  24. /* D18F3 */
  25. #define REG_NORTHBRIDGE_CAP 0xe8
  26. /* D18F4 */
  27. #define REG_PROCESSOR_TDP 0x1b8
  28. /* D18F5 */
  29. #define REG_TDP_RUNNING_AVERAGE 0xe0
  30. #define REG_TDP_LIMIT3 0xe8
  31. #define FAM15H_MIN_NUM_ATTRS 2
  32. #define FAM15H_NUM_GROUPS 2
  33. #define MAX_CUS 8
  34. /* set maximum interval as 1 second */
  35. #define MAX_INTERVAL 1000
  36. #define PCI_DEVICE_ID_AMD_15H_M70H_NB_F4 0x15b4
  37. struct fam15h_power_data {
  38. struct pci_dev *pdev;
  39. unsigned int tdp_to_watts;
  40. unsigned int base_tdp;
  41. unsigned int processor_pwr_watts;
  42. unsigned int cpu_pwr_sample_ratio;
  43. const struct attribute_group *groups[FAM15H_NUM_GROUPS];
  44. struct attribute_group group;
  45. /* maximum accumulated power of a compute unit */
  46. u64 max_cu_acc_power;
  47. /* accumulated power of the compute units */
  48. u64 cu_acc_power[MAX_CUS];
  49. /* performance timestamp counter */
  50. u64 cpu_sw_pwr_ptsc[MAX_CUS];
  51. /* online/offline status of current compute unit */
  52. int cu_on[MAX_CUS];
  53. unsigned long power_period;
  54. };
  55. static bool is_carrizo_or_later(void)
  56. {
  57. return boot_cpu_data.x86 == 0x15 && boot_cpu_data.x86_model >= 0x60;
  58. }
  59. static ssize_t power1_input_show(struct device *dev,
  60. struct device_attribute *attr, char *buf)
  61. {
  62. u32 val, tdp_limit, running_avg_range;
  63. s32 running_avg_capture;
  64. u64 curr_pwr_watts;
  65. struct fam15h_power_data *data = dev_get_drvdata(dev);
  66. struct pci_dev *f4 = data->pdev;
  67. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
  68. REG_TDP_RUNNING_AVERAGE, &val);
  69. /*
  70. * On Carrizo and later platforms, TdpRunAvgAccCap bit field
  71. * is extended to 4:31 from 4:25.
  72. */
  73. if (is_carrizo_or_later()) {
  74. running_avg_capture = val >> 4;
  75. running_avg_capture = sign_extend32(running_avg_capture, 27);
  76. } else {
  77. running_avg_capture = (val >> 4) & 0x3fffff;
  78. running_avg_capture = sign_extend32(running_avg_capture, 21);
  79. }
  80. running_avg_range = (val & 0xf) + 1;
  81. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
  82. REG_TDP_LIMIT3, &val);
  83. /*
  84. * On Carrizo and later platforms, ApmTdpLimit bit field
  85. * is extended to 16:31 from 16:28.
  86. */
  87. if (is_carrizo_or_later())
  88. tdp_limit = val >> 16;
  89. else
  90. tdp_limit = (val >> 16) & 0x1fff;
  91. curr_pwr_watts = ((u64)(tdp_limit +
  92. data->base_tdp)) << running_avg_range;
  93. curr_pwr_watts -= running_avg_capture;
  94. curr_pwr_watts *= data->tdp_to_watts;
  95. /*
  96. * Convert to microWatt
  97. *
  98. * power is in Watt provided as fixed point integer with
  99. * scaling factor 1/(2^16). For conversion we use
  100. * (10^6)/(2^16) = 15625/(2^10)
  101. */
  102. curr_pwr_watts = (curr_pwr_watts * 15625) >> (10 + running_avg_range);
  103. return sprintf(buf, "%u\n", (unsigned int) curr_pwr_watts);
  104. }
  105. static DEVICE_ATTR_RO(power1_input);
  106. static ssize_t power1_crit_show(struct device *dev,
  107. struct device_attribute *attr, char *buf)
  108. {
  109. struct fam15h_power_data *data = dev_get_drvdata(dev);
  110. return sprintf(buf, "%u\n", data->processor_pwr_watts);
  111. }
  112. static DEVICE_ATTR_RO(power1_crit);
  113. static void do_read_registers_on_cu(void *_data)
  114. {
  115. struct fam15h_power_data *data = _data;
  116. int cpu, cu;
  117. cpu = smp_processor_id();
  118. /*
  119. * With the new x86 topology modelling, cpu core id actually
  120. * is compute unit id.
  121. */
  122. cu = cpu_data(cpu).cpu_core_id;
  123. rdmsrl_safe(MSR_F15H_CU_PWR_ACCUMULATOR, &data->cu_acc_power[cu]);
  124. rdmsrl_safe(MSR_F15H_PTSC, &data->cpu_sw_pwr_ptsc[cu]);
  125. data->cu_on[cu] = 1;
  126. }
  127. /*
  128. * This function is only able to be called when CPUID
  129. * Fn8000_0007:EDX[12] is set.
  130. */
  131. static int read_registers(struct fam15h_power_data *data)
  132. {
  133. int core, this_core;
  134. cpumask_var_t mask;
  135. int ret, cpu;
  136. ret = zalloc_cpumask_var(&mask, GFP_KERNEL);
  137. if (!ret)
  138. return -ENOMEM;
  139. memset(data->cu_on, 0, sizeof(int) * MAX_CUS);
  140. cpus_read_lock();
  141. /*
  142. * Choose the first online core of each compute unit, and then
  143. * read their MSR value of power and ptsc in a single IPI,
  144. * because the MSR value of CPU core represent the compute
  145. * unit's.
  146. */
  147. core = -1;
  148. for_each_online_cpu(cpu) {
  149. this_core = topology_core_id(cpu);
  150. if (this_core == core)
  151. continue;
  152. core = this_core;
  153. /* get any CPU on this compute unit */
  154. cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(cpu)), mask);
  155. }
  156. on_each_cpu_mask(mask, do_read_registers_on_cu, data, true);
  157. cpus_read_unlock();
  158. free_cpumask_var(mask);
  159. return 0;
  160. }
  161. static ssize_t power1_average_show(struct device *dev,
  162. struct device_attribute *attr, char *buf)
  163. {
  164. struct fam15h_power_data *data = dev_get_drvdata(dev);
  165. u64 prev_cu_acc_power[MAX_CUS], prev_ptsc[MAX_CUS],
  166. jdelta[MAX_CUS];
  167. u64 tdelta, avg_acc;
  168. int cu, cu_num, ret;
  169. signed long leftover;
  170. /*
  171. * With the new x86 topology modelling, x86_max_cores is the
  172. * compute unit number.
  173. */
  174. cu_num = boot_cpu_data.x86_max_cores;
  175. ret = read_registers(data);
  176. if (ret)
  177. return 0;
  178. for (cu = 0; cu < cu_num; cu++) {
  179. prev_cu_acc_power[cu] = data->cu_acc_power[cu];
  180. prev_ptsc[cu] = data->cpu_sw_pwr_ptsc[cu];
  181. }
  182. leftover = schedule_timeout_interruptible(msecs_to_jiffies(data->power_period));
  183. if (leftover)
  184. return 0;
  185. ret = read_registers(data);
  186. if (ret)
  187. return 0;
  188. for (cu = 0, avg_acc = 0; cu < cu_num; cu++) {
  189. /* check if current compute unit is online */
  190. if (data->cu_on[cu] == 0)
  191. continue;
  192. if (data->cu_acc_power[cu] < prev_cu_acc_power[cu]) {
  193. jdelta[cu] = data->max_cu_acc_power + data->cu_acc_power[cu];
  194. jdelta[cu] -= prev_cu_acc_power[cu];
  195. } else {
  196. jdelta[cu] = data->cu_acc_power[cu] - prev_cu_acc_power[cu];
  197. }
  198. tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu];
  199. jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000;
  200. do_div(jdelta[cu], tdelta);
  201. /* the unit is microWatt */
  202. avg_acc += jdelta[cu];
  203. }
  204. return sprintf(buf, "%llu\n", (unsigned long long)avg_acc);
  205. }
  206. static DEVICE_ATTR_RO(power1_average);
  207. static ssize_t power1_average_interval_show(struct device *dev,
  208. struct device_attribute *attr,
  209. char *buf)
  210. {
  211. struct fam15h_power_data *data = dev_get_drvdata(dev);
  212. return sprintf(buf, "%lu\n", data->power_period);
  213. }
  214. static ssize_t power1_average_interval_store(struct device *dev,
  215. struct device_attribute *attr,
  216. const char *buf, size_t count)
  217. {
  218. struct fam15h_power_data *data = dev_get_drvdata(dev);
  219. unsigned long temp;
  220. int ret;
  221. ret = kstrtoul(buf, 10, &temp);
  222. if (ret)
  223. return ret;
  224. if (temp > MAX_INTERVAL)
  225. return -EINVAL;
  226. /* the interval value should be greater than 0 */
  227. if (temp <= 0)
  228. return -EINVAL;
  229. data->power_period = temp;
  230. return count;
  231. }
  232. static DEVICE_ATTR_RW(power1_average_interval);
  233. static int fam15h_power_init_attrs(struct pci_dev *pdev,
  234. struct fam15h_power_data *data)
  235. {
  236. int n = FAM15H_MIN_NUM_ATTRS;
  237. struct attribute **fam15h_power_attrs;
  238. struct cpuinfo_x86 *c = &boot_cpu_data;
  239. if (c->x86 == 0x15 &&
  240. (c->x86_model <= 0xf ||
  241. (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
  242. n += 1;
  243. /* check if processor supports accumulated power */
  244. if (boot_cpu_has(X86_FEATURE_ACC_POWER))
  245. n += 2;
  246. fam15h_power_attrs = devm_kcalloc(&pdev->dev, n,
  247. sizeof(*fam15h_power_attrs),
  248. GFP_KERNEL);
  249. if (!fam15h_power_attrs)
  250. return -ENOMEM;
  251. n = 0;
  252. fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr;
  253. if (c->x86 == 0x15 &&
  254. (c->x86_model <= 0xf ||
  255. (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
  256. fam15h_power_attrs[n++] = &dev_attr_power1_input.attr;
  257. if (boot_cpu_has(X86_FEATURE_ACC_POWER)) {
  258. fam15h_power_attrs[n++] = &dev_attr_power1_average.attr;
  259. fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr;
  260. }
  261. data->group.attrs = fam15h_power_attrs;
  262. return 0;
  263. }
  264. static bool should_load_on_this_node(struct pci_dev *f4)
  265. {
  266. u32 val;
  267. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3),
  268. REG_NORTHBRIDGE_CAP, &val);
  269. if ((val & BIT(29)) && ((val >> 30) & 3))
  270. return false;
  271. return true;
  272. }
  273. /*
  274. * Newer BKDG versions have an updated recommendation on how to properly
  275. * initialize the running average range (was: 0xE, now: 0x9). This avoids
  276. * counter saturations resulting in bogus power readings.
  277. * We correct this value ourselves to cope with older BIOSes.
  278. */
  279. static const struct pci_device_id affected_device[] = {
  280. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
  281. { 0 }
  282. };
  283. static void tweak_runavg_range(struct pci_dev *pdev)
  284. {
  285. u32 val;
  286. /*
  287. * let this quirk apply only to the current version of the
  288. * northbridge, since future versions may change the behavior
  289. */
  290. if (!pci_match_id(affected_device, pdev))
  291. return;
  292. pci_bus_read_config_dword(pdev->bus,
  293. PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
  294. REG_TDP_RUNNING_AVERAGE, &val);
  295. if ((val & 0xf) != 0xe)
  296. return;
  297. val &= ~0xf;
  298. val |= 0x9;
  299. pci_bus_write_config_dword(pdev->bus,
  300. PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
  301. REG_TDP_RUNNING_AVERAGE, val);
  302. }
  303. #ifdef CONFIG_PM
  304. static int fam15h_power_resume(struct pci_dev *pdev)
  305. {
  306. tweak_runavg_range(pdev);
  307. return 0;
  308. }
  309. #else
  310. #define fam15h_power_resume NULL
  311. #endif
  312. static int fam15h_power_init_data(struct pci_dev *f4,
  313. struct fam15h_power_data *data)
  314. {
  315. u32 val;
  316. u64 tmp;
  317. int ret;
  318. pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val);
  319. data->base_tdp = val >> 16;
  320. tmp = val & 0xffff;
  321. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
  322. REG_TDP_LIMIT3, &val);
  323. data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f);
  324. tmp *= data->tdp_to_watts;
  325. /* result not allowed to be >= 256W */
  326. if ((tmp >> 16) >= 256)
  327. dev_warn(&f4->dev,
  328. "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n",
  329. (unsigned int) (tmp >> 16));
  330. /* convert to microWatt */
  331. data->processor_pwr_watts = (tmp * 15625) >> 10;
  332. ret = fam15h_power_init_attrs(f4, data);
  333. if (ret)
  334. return ret;
  335. /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */
  336. if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
  337. return 0;
  338. /*
  339. * determine the ratio of the compute unit power accumulator
  340. * sample period to the PTSC counter period by executing CPUID
  341. * Fn8000_0007:ECX
  342. */
  343. data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
  344. if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) {
  345. pr_err("Failed to read max compute unit power accumulator MSR\n");
  346. return -ENODEV;
  347. }
  348. data->max_cu_acc_power = tmp;
  349. /*
  350. * Milliseconds are a reasonable interval for the measurement.
  351. * But it shouldn't set too long here, because several seconds
  352. * would cause the read function to hang. So set default
  353. * interval as 10 ms.
  354. */
  355. data->power_period = 10;
  356. return read_registers(data);
  357. }
  358. static int fam15h_power_probe(struct pci_dev *pdev,
  359. const struct pci_device_id *id)
  360. {
  361. struct fam15h_power_data *data;
  362. struct device *dev = &pdev->dev;
  363. struct device *hwmon_dev;
  364. int ret;
  365. /*
  366. * though we ignore every other northbridge, we still have to
  367. * do the tweaking on _each_ node in MCM processors as the counters
  368. * are working hand-in-hand
  369. */
  370. tweak_runavg_range(pdev);
  371. if (!should_load_on_this_node(pdev))
  372. return -ENODEV;
  373. data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL);
  374. if (!data)
  375. return -ENOMEM;
  376. ret = fam15h_power_init_data(pdev, data);
  377. if (ret)
  378. return ret;
  379. data->pdev = pdev;
  380. data->groups[0] = &data->group;
  381. hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power",
  382. data,
  383. &data->groups[0]);
  384. return PTR_ERR_OR_ZERO(hwmon_dev);
  385. }
  386. static const struct pci_device_id fam15h_power_id_table[] = {
  387. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
  388. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) },
  389. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) },
  390. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) },
  391. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
  392. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) },
  393. {}
  394. };
  395. MODULE_DEVICE_TABLE(pci, fam15h_power_id_table);
  396. static struct pci_driver fam15h_power_driver = {
  397. .name = "fam15h_power",
  398. .id_table = fam15h_power_id_table,
  399. .probe = fam15h_power_probe,
  400. .resume = fam15h_power_resume,
  401. };
  402. module_pci_driver(fam15h_power_driver);