fam15h_power.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 power1_input_show(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_RO(power1_input);
  121. static ssize_t power1_crit_show(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_RO(power1_crit);
  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 power1_average_show(struct device *dev,
  177. struct device_attribute *attr, char *buf)
  178. {
  179. struct fam15h_power_data *data = dev_get_drvdata(dev);
  180. u64 prev_cu_acc_power[MAX_CUS], prev_ptsc[MAX_CUS],
  181. jdelta[MAX_CUS];
  182. u64 tdelta, avg_acc;
  183. int cu, cu_num, ret;
  184. signed long leftover;
  185. /*
  186. * With the new x86 topology modelling, x86_max_cores is the
  187. * compute unit number.
  188. */
  189. cu_num = boot_cpu_data.x86_max_cores;
  190. ret = read_registers(data);
  191. if (ret)
  192. return 0;
  193. for (cu = 0; cu < cu_num; cu++) {
  194. prev_cu_acc_power[cu] = data->cu_acc_power[cu];
  195. prev_ptsc[cu] = data->cpu_sw_pwr_ptsc[cu];
  196. }
  197. leftover = schedule_timeout_interruptible(msecs_to_jiffies(data->power_period));
  198. if (leftover)
  199. return 0;
  200. ret = read_registers(data);
  201. if (ret)
  202. return 0;
  203. for (cu = 0, avg_acc = 0; cu < cu_num; cu++) {
  204. /* check if current compute unit is online */
  205. if (data->cu_on[cu] == 0)
  206. continue;
  207. if (data->cu_acc_power[cu] < prev_cu_acc_power[cu]) {
  208. jdelta[cu] = data->max_cu_acc_power + data->cu_acc_power[cu];
  209. jdelta[cu] -= prev_cu_acc_power[cu];
  210. } else {
  211. jdelta[cu] = data->cu_acc_power[cu] - prev_cu_acc_power[cu];
  212. }
  213. tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu];
  214. jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000;
  215. do_div(jdelta[cu], tdelta);
  216. /* the unit is microWatt */
  217. avg_acc += jdelta[cu];
  218. }
  219. return sprintf(buf, "%llu\n", (unsigned long long)avg_acc);
  220. }
  221. static DEVICE_ATTR_RO(power1_average);
  222. static ssize_t power1_average_interval_show(struct device *dev,
  223. struct device_attribute *attr,
  224. char *buf)
  225. {
  226. struct fam15h_power_data *data = dev_get_drvdata(dev);
  227. return sprintf(buf, "%lu\n", data->power_period);
  228. }
  229. static ssize_t power1_average_interval_store(struct device *dev,
  230. struct device_attribute *attr,
  231. const char *buf, size_t count)
  232. {
  233. struct fam15h_power_data *data = dev_get_drvdata(dev);
  234. unsigned long temp;
  235. int ret;
  236. ret = kstrtoul(buf, 10, &temp);
  237. if (ret)
  238. return ret;
  239. if (temp > MAX_INTERVAL)
  240. return -EINVAL;
  241. /* the interval value should be greater than 0 */
  242. if (temp <= 0)
  243. return -EINVAL;
  244. data->power_period = temp;
  245. return count;
  246. }
  247. static DEVICE_ATTR_RW(power1_average_interval);
  248. static int fam15h_power_init_attrs(struct pci_dev *pdev,
  249. struct fam15h_power_data *data)
  250. {
  251. int n = FAM15H_MIN_NUM_ATTRS;
  252. struct attribute **fam15h_power_attrs;
  253. struct cpuinfo_x86 *c = &boot_cpu_data;
  254. if (c->x86 == 0x15 &&
  255. (c->x86_model <= 0xf ||
  256. (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
  257. n += 1;
  258. /* check if processor supports accumulated power */
  259. if (boot_cpu_has(X86_FEATURE_ACC_POWER))
  260. n += 2;
  261. fam15h_power_attrs = devm_kcalloc(&pdev->dev, n,
  262. sizeof(*fam15h_power_attrs),
  263. GFP_KERNEL);
  264. if (!fam15h_power_attrs)
  265. return -ENOMEM;
  266. n = 0;
  267. fam15h_power_attrs[n++] = &dev_attr_power1_crit.attr;
  268. if (c->x86 == 0x15 &&
  269. (c->x86_model <= 0xf ||
  270. (c->x86_model >= 0x60 && c->x86_model <= 0x7f)))
  271. fam15h_power_attrs[n++] = &dev_attr_power1_input.attr;
  272. if (boot_cpu_has(X86_FEATURE_ACC_POWER)) {
  273. fam15h_power_attrs[n++] = &dev_attr_power1_average.attr;
  274. fam15h_power_attrs[n++] = &dev_attr_power1_average_interval.attr;
  275. }
  276. data->group.attrs = fam15h_power_attrs;
  277. return 0;
  278. }
  279. static bool should_load_on_this_node(struct pci_dev *f4)
  280. {
  281. u32 val;
  282. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 3),
  283. REG_NORTHBRIDGE_CAP, &val);
  284. if ((val & BIT(29)) && ((val >> 30) & 3))
  285. return false;
  286. return true;
  287. }
  288. /*
  289. * Newer BKDG versions have an updated recommendation on how to properly
  290. * initialize the running average range (was: 0xE, now: 0x9). This avoids
  291. * counter saturations resulting in bogus power readings.
  292. * We correct this value ourselves to cope with older BIOSes.
  293. */
  294. static const struct pci_device_id affected_device[] = {
  295. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
  296. { 0 }
  297. };
  298. static void tweak_runavg_range(struct pci_dev *pdev)
  299. {
  300. u32 val;
  301. /*
  302. * let this quirk apply only to the current version of the
  303. * northbridge, since future versions may change the behavior
  304. */
  305. if (!pci_match_id(affected_device, pdev))
  306. return;
  307. pci_bus_read_config_dword(pdev->bus,
  308. PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
  309. REG_TDP_RUNNING_AVERAGE, &val);
  310. if ((val & 0xf) != 0xe)
  311. return;
  312. val &= ~0xf;
  313. val |= 0x9;
  314. pci_bus_write_config_dword(pdev->bus,
  315. PCI_DEVFN(PCI_SLOT(pdev->devfn), 5),
  316. REG_TDP_RUNNING_AVERAGE, val);
  317. }
  318. #ifdef CONFIG_PM
  319. static int fam15h_power_resume(struct pci_dev *pdev)
  320. {
  321. tweak_runavg_range(pdev);
  322. return 0;
  323. }
  324. #else
  325. #define fam15h_power_resume NULL
  326. #endif
  327. static int fam15h_power_init_data(struct pci_dev *f4,
  328. struct fam15h_power_data *data)
  329. {
  330. u32 val;
  331. u64 tmp;
  332. int ret;
  333. pci_read_config_dword(f4, REG_PROCESSOR_TDP, &val);
  334. data->base_tdp = val >> 16;
  335. tmp = val & 0xffff;
  336. pci_bus_read_config_dword(f4->bus, PCI_DEVFN(PCI_SLOT(f4->devfn), 5),
  337. REG_TDP_LIMIT3, &val);
  338. data->tdp_to_watts = ((val & 0x3ff) << 6) | ((val >> 10) & 0x3f);
  339. tmp *= data->tdp_to_watts;
  340. /* result not allowed to be >= 256W */
  341. if ((tmp >> 16) >= 256)
  342. dev_warn(&f4->dev,
  343. "Bogus value for ProcessorPwrWatts (processor_pwr_watts>=%u)\n",
  344. (unsigned int) (tmp >> 16));
  345. /* convert to microWatt */
  346. data->processor_pwr_watts = (tmp * 15625) >> 10;
  347. ret = fam15h_power_init_attrs(f4, data);
  348. if (ret)
  349. return ret;
  350. /* CPUID Fn8000_0007:EDX[12] indicates to support accumulated power */
  351. if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
  352. return 0;
  353. /*
  354. * determine the ratio of the compute unit power accumulator
  355. * sample period to the PTSC counter period by executing CPUID
  356. * Fn8000_0007:ECX
  357. */
  358. data->cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
  359. if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) {
  360. pr_err("Failed to read max compute unit power accumulator MSR\n");
  361. return -ENODEV;
  362. }
  363. data->max_cu_acc_power = tmp;
  364. /*
  365. * Milliseconds are a reasonable interval for the measurement.
  366. * But it shouldn't set too long here, because several seconds
  367. * would cause the read function to hang. So set default
  368. * interval as 10 ms.
  369. */
  370. data->power_period = 10;
  371. return read_registers(data);
  372. }
  373. static int fam15h_power_probe(struct pci_dev *pdev,
  374. const struct pci_device_id *id)
  375. {
  376. struct fam15h_power_data *data;
  377. struct device *dev = &pdev->dev;
  378. struct device *hwmon_dev;
  379. int ret;
  380. /*
  381. * though we ignore every other northbridge, we still have to
  382. * do the tweaking on _each_ node in MCM processors as the counters
  383. * are working hand-in-hand
  384. */
  385. tweak_runavg_range(pdev);
  386. if (!should_load_on_this_node(pdev))
  387. return -ENODEV;
  388. data = devm_kzalloc(dev, sizeof(struct fam15h_power_data), GFP_KERNEL);
  389. if (!data)
  390. return -ENOMEM;
  391. ret = fam15h_power_init_data(pdev, data);
  392. if (ret)
  393. return ret;
  394. data->pdev = pdev;
  395. data->groups[0] = &data->group;
  396. hwmon_dev = devm_hwmon_device_register_with_groups(dev, "fam15h_power",
  397. data,
  398. &data->groups[0]);
  399. return PTR_ERR_OR_ZERO(hwmon_dev);
  400. }
  401. static const struct pci_device_id fam15h_power_id_table[] = {
  402. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_NB_F4) },
  403. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M30H_NB_F4) },
  404. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M60H_NB_F4) },
  405. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_15H_M70H_NB_F4) },
  406. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_NB_F4) },
  407. { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_16H_M30H_NB_F4) },
  408. {}
  409. };
  410. MODULE_DEVICE_TABLE(pci, fam15h_power_id_table);
  411. static struct pci_driver fam15h_power_driver = {
  412. .name = "fam15h_power",
  413. .id_table = fam15h_power_id_table,
  414. .probe = fam15h_power_probe,
  415. .resume = fam15h_power_resume,
  416. };
  417. module_pci_driver(fam15h_power_driver);