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