power.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Performance events - AMD Processor Power Reporting Mechanism
  4. *
  5. * Copyright (C) 2016 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Huang Rui <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/perf_event.h>
  12. #include <asm/cpu_device_id.h>
  13. #include "../perf_event.h"
  14. /* Event code: LSB 8 bits, passed in attr->config any other bit is reserved. */
  15. #define AMD_POWER_EVENT_MASK 0xFFULL
  16. /*
  17. * Accumulated power status counters.
  18. */
  19. #define AMD_POWER_EVENTSEL_PKG 1
  20. /*
  21. * The ratio of compute unit power accumulator sample period to the
  22. * PTSC period.
  23. */
  24. static unsigned int cpu_pwr_sample_ratio;
  25. /* Maximum accumulated power of a compute unit. */
  26. static u64 max_cu_acc_power;
  27. static struct pmu pmu_class;
  28. /*
  29. * Accumulated power represents the sum of each compute unit's (CU) power
  30. * consumption. On any core of each CU we read the total accumulated power from
  31. * MSR_F15H_CU_PWR_ACCUMULATOR. cpu_mask represents CPU bit map of all cores
  32. * which are picked to measure the power for the CUs they belong to.
  33. */
  34. static cpumask_t cpu_mask;
  35. static void event_update(struct perf_event *event)
  36. {
  37. struct hw_perf_event *hwc = &event->hw;
  38. u64 prev_pwr_acc, new_pwr_acc, prev_ptsc, new_ptsc;
  39. u64 delta, tdelta;
  40. prev_pwr_acc = hwc->pwr_acc;
  41. prev_ptsc = hwc->ptsc;
  42. rdmsrl(MSR_F15H_CU_PWR_ACCUMULATOR, new_pwr_acc);
  43. rdmsrl(MSR_F15H_PTSC, new_ptsc);
  44. /*
  45. * Calculate the CU power consumption over a time period, the unit of
  46. * final value (delta) is micro-Watts. Then add it to the event count.
  47. */
  48. if (new_pwr_acc < prev_pwr_acc) {
  49. delta = max_cu_acc_power + new_pwr_acc;
  50. delta -= prev_pwr_acc;
  51. } else
  52. delta = new_pwr_acc - prev_pwr_acc;
  53. delta *= cpu_pwr_sample_ratio * 1000;
  54. tdelta = new_ptsc - prev_ptsc;
  55. do_div(delta, tdelta);
  56. local64_add(delta, &event->count);
  57. }
  58. static void __pmu_event_start(struct perf_event *event)
  59. {
  60. if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
  61. return;
  62. event->hw.state = 0;
  63. rdmsrl(MSR_F15H_PTSC, event->hw.ptsc);
  64. rdmsrl(MSR_F15H_CU_PWR_ACCUMULATOR, event->hw.pwr_acc);
  65. }
  66. static void pmu_event_start(struct perf_event *event, int mode)
  67. {
  68. __pmu_event_start(event);
  69. }
  70. static void pmu_event_stop(struct perf_event *event, int mode)
  71. {
  72. struct hw_perf_event *hwc = &event->hw;
  73. /* Mark event as deactivated and stopped. */
  74. if (!(hwc->state & PERF_HES_STOPPED))
  75. hwc->state |= PERF_HES_STOPPED;
  76. /* Check if software counter update is necessary. */
  77. if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
  78. /*
  79. * Drain the remaining delta count out of an event
  80. * that we are disabling:
  81. */
  82. event_update(event);
  83. hwc->state |= PERF_HES_UPTODATE;
  84. }
  85. }
  86. static int pmu_event_add(struct perf_event *event, int mode)
  87. {
  88. struct hw_perf_event *hwc = &event->hw;
  89. hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
  90. if (mode & PERF_EF_START)
  91. __pmu_event_start(event);
  92. return 0;
  93. }
  94. static void pmu_event_del(struct perf_event *event, int flags)
  95. {
  96. pmu_event_stop(event, PERF_EF_UPDATE);
  97. }
  98. static int pmu_event_init(struct perf_event *event)
  99. {
  100. u64 cfg = event->attr.config & AMD_POWER_EVENT_MASK;
  101. /* Only look at AMD power events. */
  102. if (event->attr.type != pmu_class.type)
  103. return -ENOENT;
  104. /* Unsupported modes and filters. */
  105. if (event->attr.sample_period)
  106. return -EINVAL;
  107. if (cfg != AMD_POWER_EVENTSEL_PKG)
  108. return -EINVAL;
  109. return 0;
  110. }
  111. static void pmu_event_read(struct perf_event *event)
  112. {
  113. event_update(event);
  114. }
  115. static ssize_t
  116. get_attr_cpumask(struct device *dev, struct device_attribute *attr, char *buf)
  117. {
  118. return cpumap_print_to_pagebuf(true, buf, &cpu_mask);
  119. }
  120. static DEVICE_ATTR(cpumask, S_IRUGO, get_attr_cpumask, NULL);
  121. static struct attribute *pmu_attrs[] = {
  122. &dev_attr_cpumask.attr,
  123. NULL,
  124. };
  125. static struct attribute_group pmu_attr_group = {
  126. .attrs = pmu_attrs,
  127. };
  128. /*
  129. * Currently it only supports to report the power of each
  130. * processor/package.
  131. */
  132. EVENT_ATTR_STR(power-pkg, power_pkg, "event=0x01");
  133. EVENT_ATTR_STR(power-pkg.unit, power_pkg_unit, "mWatts");
  134. /* Convert the count from micro-Watts to milli-Watts. */
  135. EVENT_ATTR_STR(power-pkg.scale, power_pkg_scale, "1.000000e-3");
  136. static struct attribute *events_attr[] = {
  137. EVENT_PTR(power_pkg),
  138. EVENT_PTR(power_pkg_unit),
  139. EVENT_PTR(power_pkg_scale),
  140. NULL,
  141. };
  142. static struct attribute_group pmu_events_group = {
  143. .name = "events",
  144. .attrs = events_attr,
  145. };
  146. PMU_FORMAT_ATTR(event, "config:0-7");
  147. static struct attribute *formats_attr[] = {
  148. &format_attr_event.attr,
  149. NULL,
  150. };
  151. static struct attribute_group pmu_format_group = {
  152. .name = "format",
  153. .attrs = formats_attr,
  154. };
  155. static const struct attribute_group *attr_groups[] = {
  156. &pmu_attr_group,
  157. &pmu_format_group,
  158. &pmu_events_group,
  159. NULL,
  160. };
  161. static struct pmu pmu_class = {
  162. .attr_groups = attr_groups,
  163. /* system-wide only */
  164. .task_ctx_nr = perf_invalid_context,
  165. .event_init = pmu_event_init,
  166. .add = pmu_event_add,
  167. .del = pmu_event_del,
  168. .start = pmu_event_start,
  169. .stop = pmu_event_stop,
  170. .read = pmu_event_read,
  171. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  172. .module = THIS_MODULE,
  173. };
  174. static int power_cpu_exit(unsigned int cpu)
  175. {
  176. int target;
  177. if (!cpumask_test_and_clear_cpu(cpu, &cpu_mask))
  178. return 0;
  179. /*
  180. * Find a new CPU on the same compute unit, if was set in cpumask
  181. * and still some CPUs on compute unit. Then migrate event and
  182. * context to new CPU.
  183. */
  184. target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
  185. if (target < nr_cpumask_bits) {
  186. cpumask_set_cpu(target, &cpu_mask);
  187. perf_pmu_migrate_context(&pmu_class, cpu, target);
  188. }
  189. return 0;
  190. }
  191. static int power_cpu_init(unsigned int cpu)
  192. {
  193. int target;
  194. /*
  195. * 1) If any CPU is set at cpu_mask in the same compute unit, do
  196. * nothing.
  197. * 2) If no CPU is set at cpu_mask in the same compute unit,
  198. * set current ONLINE CPU.
  199. *
  200. * Note: if there is a CPU aside of the new one already in the
  201. * sibling mask, then it is also in cpu_mask.
  202. */
  203. target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
  204. if (target >= nr_cpumask_bits)
  205. cpumask_set_cpu(cpu, &cpu_mask);
  206. return 0;
  207. }
  208. static const struct x86_cpu_id cpu_match[] = {
  209. X86_MATCH_VENDOR_FAM(AMD, 0x15, NULL),
  210. {},
  211. };
  212. static int __init amd_power_pmu_init(void)
  213. {
  214. int ret;
  215. if (!x86_match_cpu(cpu_match))
  216. return -ENODEV;
  217. if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
  218. return -ENODEV;
  219. cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
  220. if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &max_cu_acc_power)) {
  221. pr_err("Failed to read max compute unit power accumulator MSR\n");
  222. return -ENODEV;
  223. }
  224. cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_POWER_ONLINE,
  225. "perf/x86/amd/power:online",
  226. power_cpu_init, power_cpu_exit);
  227. ret = perf_pmu_register(&pmu_class, "power", -1);
  228. if (WARN_ON(ret)) {
  229. pr_warn("AMD Power PMU registration failed\n");
  230. return ret;
  231. }
  232. pr_info("AMD Power PMU detected\n");
  233. return ret;
  234. }
  235. module_init(amd_power_pmu_init);
  236. static void __exit amd_power_pmu_exit(void)
  237. {
  238. cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_AMD_POWER_ONLINE);
  239. perf_pmu_unregister(&pmu_class);
  240. }
  241. module_exit(amd_power_pmu_exit);
  242. MODULE_AUTHOR("Huang Rui <[email protected]>");
  243. MODULE_DESCRIPTION("AMD Processor Power Reporting Mechanism");
  244. MODULE_LICENSE("GPL v2");