1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/module.h>
14#include <linux/slab.h>
15#include <linux/perf_event.h>
16#include <asm/cpu_device_id.h>
17#include "../perf_event.h"
18
19#define MSR_F15H_CU_PWR_ACCUMULATOR 0xc001007a
20#define MSR_F15H_CU_MAX_PWR_ACCUMULATOR 0xc001007b
21#define MSR_F15H_PTSC 0xc0010280
22
23
24#define AMD_POWER_EVENT_MASK 0xFFULL
25
26
27
28
29#define AMD_POWER_EVENTSEL_PKG 1
30
31
32
33
34
35static unsigned int cpu_pwr_sample_ratio;
36
37
38static u64 max_cu_acc_power;
39
40static struct pmu pmu_class;
41
42
43
44
45
46
47
48static cpumask_t cpu_mask;
49
50static void event_update(struct perf_event *event)
51{
52 struct hw_perf_event *hwc = &event->hw;
53 u64 prev_pwr_acc, new_pwr_acc, prev_ptsc, new_ptsc;
54 u64 delta, tdelta;
55
56 prev_pwr_acc = hwc->pwr_acc;
57 prev_ptsc = hwc->ptsc;
58 rdmsrl(MSR_F15H_CU_PWR_ACCUMULATOR, new_pwr_acc);
59 rdmsrl(MSR_F15H_PTSC, new_ptsc);
60
61
62
63
64
65 if (new_pwr_acc < prev_pwr_acc) {
66 delta = max_cu_acc_power + new_pwr_acc;
67 delta -= prev_pwr_acc;
68 } else
69 delta = new_pwr_acc - prev_pwr_acc;
70
71 delta *= cpu_pwr_sample_ratio * 1000;
72 tdelta = new_ptsc - prev_ptsc;
73
74 do_div(delta, tdelta);
75 local64_add(delta, &event->count);
76}
77
78static void __pmu_event_start(struct perf_event *event)
79{
80 if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
81 return;
82
83 event->hw.state = 0;
84
85 rdmsrl(MSR_F15H_PTSC, event->hw.ptsc);
86 rdmsrl(MSR_F15H_CU_PWR_ACCUMULATOR, event->hw.pwr_acc);
87}
88
89static void pmu_event_start(struct perf_event *event, int mode)
90{
91 __pmu_event_start(event);
92}
93
94static void pmu_event_stop(struct perf_event *event, int mode)
95{
96 struct hw_perf_event *hwc = &event->hw;
97
98
99 if (!(hwc->state & PERF_HES_STOPPED))
100 hwc->state |= PERF_HES_STOPPED;
101
102
103 if ((mode & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) {
104
105
106
107
108 event_update(event);
109 hwc->state |= PERF_HES_UPTODATE;
110 }
111}
112
113static int pmu_event_add(struct perf_event *event, int mode)
114{
115 struct hw_perf_event *hwc = &event->hw;
116
117 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
118
119 if (mode & PERF_EF_START)
120 __pmu_event_start(event);
121
122 return 0;
123}
124
125static void pmu_event_del(struct perf_event *event, int flags)
126{
127 pmu_event_stop(event, PERF_EF_UPDATE);
128}
129
130static int pmu_event_init(struct perf_event *event)
131{
132 u64 cfg = event->attr.config & AMD_POWER_EVENT_MASK;
133
134
135 if (event->attr.type != pmu_class.type)
136 return -ENOENT;
137
138
139 if (event->attr.sample_period)
140 return -EINVAL;
141
142 if (cfg != AMD_POWER_EVENTSEL_PKG)
143 return -EINVAL;
144
145 return 0;
146}
147
148static void pmu_event_read(struct perf_event *event)
149{
150 event_update(event);
151}
152
153static ssize_t
154get_attr_cpumask(struct device *dev, struct device_attribute *attr, char *buf)
155{
156 return cpumap_print_to_pagebuf(true, buf, &cpu_mask);
157}
158
159static DEVICE_ATTR(cpumask, S_IRUGO, get_attr_cpumask, NULL);
160
161static struct attribute *pmu_attrs[] = {
162 &dev_attr_cpumask.attr,
163 NULL,
164};
165
166static struct attribute_group pmu_attr_group = {
167 .attrs = pmu_attrs,
168};
169
170
171
172
173
174EVENT_ATTR_STR(power-pkg, power_pkg, "event=0x01");
175
176EVENT_ATTR_STR(power-pkg.unit, power_pkg_unit, "mWatts");
177
178
179EVENT_ATTR_STR(power-pkg.scale, power_pkg_scale, "1.000000e-3");
180
181static struct attribute *events_attr[] = {
182 EVENT_PTR(power_pkg),
183 EVENT_PTR(power_pkg_unit),
184 EVENT_PTR(power_pkg_scale),
185 NULL,
186};
187
188static struct attribute_group pmu_events_group = {
189 .name = "events",
190 .attrs = events_attr,
191};
192
193PMU_FORMAT_ATTR(event, "config:0-7");
194
195static struct attribute *formats_attr[] = {
196 &format_attr_event.attr,
197 NULL,
198};
199
200static struct attribute_group pmu_format_group = {
201 .name = "format",
202 .attrs = formats_attr,
203};
204
205static const struct attribute_group *attr_groups[] = {
206 &pmu_attr_group,
207 &pmu_format_group,
208 &pmu_events_group,
209 NULL,
210};
211
212static struct pmu pmu_class = {
213 .attr_groups = attr_groups,
214
215 .task_ctx_nr = perf_invalid_context,
216 .event_init = pmu_event_init,
217 .add = pmu_event_add,
218 .del = pmu_event_del,
219 .start = pmu_event_start,
220 .stop = pmu_event_stop,
221 .read = pmu_event_read,
222 .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
223};
224
225static int power_cpu_exit(unsigned int cpu)
226{
227 int target;
228
229 if (!cpumask_test_and_clear_cpu(cpu, &cpu_mask))
230 return 0;
231
232
233
234
235
236
237 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
238 if (target < nr_cpumask_bits) {
239 cpumask_set_cpu(target, &cpu_mask);
240 perf_pmu_migrate_context(&pmu_class, cpu, target);
241 }
242 return 0;
243}
244
245static int power_cpu_init(unsigned int cpu)
246{
247 int target;
248
249
250
251
252
253
254
255
256
257
258 target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
259 if (target >= nr_cpumask_bits)
260 cpumask_set_cpu(cpu, &cpu_mask);
261 return 0;
262}
263
264static const struct x86_cpu_id cpu_match[] = {
265 X86_MATCH_VENDOR_FAM(AMD, 0x15, NULL),
266 {},
267};
268
269static int __init amd_power_pmu_init(void)
270{
271 int ret;
272
273 if (!x86_match_cpu(cpu_match))
274 return -ENODEV;
275
276 if (!boot_cpu_has(X86_FEATURE_ACC_POWER))
277 return -ENODEV;
278
279 cpu_pwr_sample_ratio = cpuid_ecx(0x80000007);
280
281 if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &max_cu_acc_power)) {
282 pr_err("Failed to read max compute unit power accumulator MSR\n");
283 return -ENODEV;
284 }
285
286
287 cpuhp_setup_state(CPUHP_AP_PERF_X86_AMD_POWER_ONLINE,
288 "perf/x86/amd/power:online",
289 power_cpu_init, power_cpu_exit);
290
291 ret = perf_pmu_register(&pmu_class, "power", -1);
292 if (WARN_ON(ret)) {
293 pr_warn("AMD Power PMU registration failed\n");
294 return ret;
295 }
296
297 pr_info("AMD Power PMU detected\n");
298 return ret;
299}
300module_init(amd_power_pmu_init);
301
302static void __exit amd_power_pmu_exit(void)
303{
304 cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_AMD_POWER_ONLINE);
305 perf_pmu_unregister(&pmu_class);
306}
307module_exit(amd_power_pmu_exit);
308
309MODULE_AUTHOR("Huang Rui <ray.huang@amd.com>");
310MODULE_DESCRIPTION("AMD Processor Power Reporting Mechanism");
311MODULE_LICENSE("GPL v2");
312