fam15h_power.c 13 KB

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