Commit | Line | Data |
---|---|---|
241771ef | 1 | /* |
cdd6c482 | 2 | * Performance events x86 architecture code |
241771ef | 3 | * |
98144511 IM |
4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> |
5 | * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar | |
6 | * Copyright (C) 2009 Jaswinder Singh Rajput | |
7 | * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter | |
8 | * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> | |
30dd568c | 9 | * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com> |
1da53e02 | 10 | * Copyright (C) 2009 Google, Inc., Stephane Eranian |
241771ef IM |
11 | * |
12 | * For licencing details see kernel-base/COPYING | |
13 | */ | |
14 | ||
cdd6c482 | 15 | #include <linux/perf_event.h> |
241771ef IM |
16 | #include <linux/capability.h> |
17 | #include <linux/notifier.h> | |
18 | #include <linux/hardirq.h> | |
19 | #include <linux/kprobes.h> | |
4ac13294 | 20 | #include <linux/module.h> |
241771ef IM |
21 | #include <linux/kdebug.h> |
22 | #include <linux/sched.h> | |
d7d59fb3 | 23 | #include <linux/uaccess.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
30dd568c | 25 | #include <linux/cpu.h> |
272d30be | 26 | #include <linux/bitops.h> |
0c9d42ed | 27 | #include <linux/device.h> |
241771ef | 28 | |
241771ef | 29 | #include <asm/apic.h> |
d7d59fb3 | 30 | #include <asm/stacktrace.h> |
4e935e47 | 31 | #include <asm/nmi.h> |
257ef9d2 | 32 | #include <asm/compat.h> |
69092624 | 33 | #include <asm/smp.h> |
c8e5910e | 34 | #include <asm/alternative.h> |
e3f3541c | 35 | #include <asm/timer.h> |
241771ef | 36 | |
de0428a7 KW |
37 | #include "perf_event.h" |
38 | ||
7645a24c PZ |
39 | #if 0 |
40 | #undef wrmsrl | |
41 | #define wrmsrl(msr, val) \ | |
42 | do { \ | |
43 | trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\ | |
44 | (unsigned long)(val)); \ | |
45 | native_write_msr((msr), (u32)((u64)(val)), \ | |
46 | (u32)((u64)(val) >> 32)); \ | |
47 | } while (0) | |
48 | #endif | |
49 | ||
de0428a7 | 50 | struct x86_pmu x86_pmu __read_mostly; |
efc9f05d | 51 | |
de0428a7 | 52 | DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { |
b0f3f28e PZ |
53 | .enabled = 1, |
54 | }; | |
241771ef | 55 | |
de0428a7 | 56 | u64 __read_mostly hw_cache_event_ids |
8326f44d IM |
57 | [PERF_COUNT_HW_CACHE_MAX] |
58 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
59 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; | |
de0428a7 | 60 | u64 __read_mostly hw_cache_extra_regs |
e994d7d2 AK |
61 | [PERF_COUNT_HW_CACHE_MAX] |
62 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
63 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; | |
8326f44d | 64 | |
ee06094f | 65 | /* |
cdd6c482 IM |
66 | * Propagate event elapsed time into the generic event. |
67 | * Can only be executed on the CPU where the event is active. | |
ee06094f IM |
68 | * Returns the delta events processed. |
69 | */ | |
de0428a7 | 70 | u64 x86_perf_event_update(struct perf_event *event) |
ee06094f | 71 | { |
cc2ad4ba | 72 | struct hw_perf_event *hwc = &event->hw; |
948b1bb8 | 73 | int shift = 64 - x86_pmu.cntval_bits; |
ec3232bd | 74 | u64 prev_raw_count, new_raw_count; |
cc2ad4ba | 75 | int idx = hwc->idx; |
ec3232bd | 76 | s64 delta; |
ee06094f | 77 | |
30dd568c MM |
78 | if (idx == X86_PMC_IDX_FIXED_BTS) |
79 | return 0; | |
80 | ||
ee06094f | 81 | /* |
cdd6c482 | 82 | * Careful: an NMI might modify the previous event value. |
ee06094f IM |
83 | * |
84 | * Our tactic to handle this is to first atomically read and | |
85 | * exchange a new raw count - then add that new-prev delta | |
cdd6c482 | 86 | * count to the generic event atomically: |
ee06094f IM |
87 | */ |
88 | again: | |
e7850595 | 89 | prev_raw_count = local64_read(&hwc->prev_count); |
73d6e522 | 90 | rdmsrl(hwc->event_base, new_raw_count); |
ee06094f | 91 | |
e7850595 | 92 | if (local64_cmpxchg(&hwc->prev_count, prev_raw_count, |
ee06094f IM |
93 | new_raw_count) != prev_raw_count) |
94 | goto again; | |
95 | ||
96 | /* | |
97 | * Now we have the new raw value and have updated the prev | |
98 | * timestamp already. We can now calculate the elapsed delta | |
cdd6c482 | 99 | * (event-)time and add that to the generic event. |
ee06094f IM |
100 | * |
101 | * Careful, not all hw sign-extends above the physical width | |
ec3232bd | 102 | * of the count. |
ee06094f | 103 | */ |
ec3232bd PZ |
104 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
105 | delta >>= shift; | |
ee06094f | 106 | |
e7850595 PZ |
107 | local64_add(delta, &event->count); |
108 | local64_sub(delta, &hwc->period_left); | |
4b7bfd0d RR |
109 | |
110 | return new_raw_count; | |
ee06094f IM |
111 | } |
112 | ||
a7e3ed1e AK |
113 | /* |
114 | * Find and validate any extra registers to set up. | |
115 | */ | |
116 | static int x86_pmu_extra_regs(u64 config, struct perf_event *event) | |
117 | { | |
efc9f05d | 118 | struct hw_perf_event_extra *reg; |
a7e3ed1e AK |
119 | struct extra_reg *er; |
120 | ||
efc9f05d | 121 | reg = &event->hw.extra_reg; |
a7e3ed1e AK |
122 | |
123 | if (!x86_pmu.extra_regs) | |
124 | return 0; | |
125 | ||
126 | for (er = x86_pmu.extra_regs; er->msr; er++) { | |
127 | if (er->event != (config & er->config_mask)) | |
128 | continue; | |
129 | if (event->attr.config1 & ~er->valid_mask) | |
130 | return -EINVAL; | |
efc9f05d SE |
131 | |
132 | reg->idx = er->idx; | |
133 | reg->config = event->attr.config1; | |
134 | reg->reg = er->msr; | |
a7e3ed1e AK |
135 | break; |
136 | } | |
137 | return 0; | |
138 | } | |
139 | ||
cdd6c482 | 140 | static atomic_t active_events; |
4e935e47 PZ |
141 | static DEFINE_MUTEX(pmc_reserve_mutex); |
142 | ||
b27ea29c RR |
143 | #ifdef CONFIG_X86_LOCAL_APIC |
144 | ||
4e935e47 PZ |
145 | static bool reserve_pmc_hardware(void) |
146 | { | |
147 | int i; | |
148 | ||
948b1bb8 | 149 | for (i = 0; i < x86_pmu.num_counters; i++) { |
41bf4989 | 150 | if (!reserve_perfctr_nmi(x86_pmu_event_addr(i))) |
4e935e47 PZ |
151 | goto perfctr_fail; |
152 | } | |
153 | ||
948b1bb8 | 154 | for (i = 0; i < x86_pmu.num_counters; i++) { |
41bf4989 | 155 | if (!reserve_evntsel_nmi(x86_pmu_config_addr(i))) |
4e935e47 PZ |
156 | goto eventsel_fail; |
157 | } | |
158 | ||
159 | return true; | |
160 | ||
161 | eventsel_fail: | |
162 | for (i--; i >= 0; i--) | |
41bf4989 | 163 | release_evntsel_nmi(x86_pmu_config_addr(i)); |
4e935e47 | 164 | |
948b1bb8 | 165 | i = x86_pmu.num_counters; |
4e935e47 PZ |
166 | |
167 | perfctr_fail: | |
168 | for (i--; i >= 0; i--) | |
41bf4989 | 169 | release_perfctr_nmi(x86_pmu_event_addr(i)); |
4e935e47 | 170 | |
4e935e47 PZ |
171 | return false; |
172 | } | |
173 | ||
174 | static void release_pmc_hardware(void) | |
175 | { | |
176 | int i; | |
177 | ||
948b1bb8 | 178 | for (i = 0; i < x86_pmu.num_counters; i++) { |
41bf4989 RR |
179 | release_perfctr_nmi(x86_pmu_event_addr(i)); |
180 | release_evntsel_nmi(x86_pmu_config_addr(i)); | |
4e935e47 | 181 | } |
4e935e47 PZ |
182 | } |
183 | ||
b27ea29c RR |
184 | #else |
185 | ||
186 | static bool reserve_pmc_hardware(void) { return true; } | |
187 | static void release_pmc_hardware(void) {} | |
188 | ||
189 | #endif | |
190 | ||
33c6d6a7 DZ |
191 | static bool check_hw_exists(void) |
192 | { | |
193 | u64 val, val_new = 0; | |
4407204c | 194 | int i, reg, ret = 0; |
33c6d6a7 | 195 | |
4407204c PZ |
196 | /* |
197 | * Check to see if the BIOS enabled any of the counters, if so | |
198 | * complain and bail. | |
199 | */ | |
200 | for (i = 0; i < x86_pmu.num_counters; i++) { | |
41bf4989 | 201 | reg = x86_pmu_config_addr(i); |
4407204c PZ |
202 | ret = rdmsrl_safe(reg, &val); |
203 | if (ret) | |
204 | goto msr_fail; | |
205 | if (val & ARCH_PERFMON_EVENTSEL_ENABLE) | |
206 | goto bios_fail; | |
207 | } | |
208 | ||
209 | if (x86_pmu.num_counters_fixed) { | |
210 | reg = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; | |
211 | ret = rdmsrl_safe(reg, &val); | |
212 | if (ret) | |
213 | goto msr_fail; | |
214 | for (i = 0; i < x86_pmu.num_counters_fixed; i++) { | |
215 | if (val & (0x03 << i*4)) | |
216 | goto bios_fail; | |
217 | } | |
218 | } | |
219 | ||
220 | /* | |
221 | * Now write a value and read it back to see if it matches, | |
222 | * this is needed to detect certain hardware emulators (qemu/kvm) | |
223 | * that don't trap on the MSR access and always return 0s. | |
224 | */ | |
33c6d6a7 | 225 | val = 0xabcdUL; |
41bf4989 RR |
226 | ret = checking_wrmsrl(x86_pmu_event_addr(0), val); |
227 | ret |= rdmsrl_safe(x86_pmu_event_addr(0), &val_new); | |
33c6d6a7 | 228 | if (ret || val != val_new) |
4407204c | 229 | goto msr_fail; |
33c6d6a7 DZ |
230 | |
231 | return true; | |
4407204c PZ |
232 | |
233 | bios_fail: | |
45daae57 IM |
234 | /* |
235 | * We still allow the PMU driver to operate: | |
236 | */ | |
237 | printk(KERN_CONT "Broken BIOS detected, complain to your hardware vendor.\n"); | |
4407204c | 238 | printk(KERN_ERR FW_BUG "the BIOS has corrupted hw-PMU resources (MSR %x is %Lx)\n", reg, val); |
45daae57 IM |
239 | |
240 | return true; | |
4407204c PZ |
241 | |
242 | msr_fail: | |
243 | printk(KERN_CONT "Broken PMU hardware detected, using software events only.\n"); | |
45daae57 | 244 | |
4407204c | 245 | return false; |
33c6d6a7 DZ |
246 | } |
247 | ||
cdd6c482 | 248 | static void hw_perf_event_destroy(struct perf_event *event) |
4e935e47 | 249 | { |
cdd6c482 | 250 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { |
4e935e47 | 251 | release_pmc_hardware(); |
ca037701 | 252 | release_ds_buffers(); |
4e935e47 PZ |
253 | mutex_unlock(&pmc_reserve_mutex); |
254 | } | |
255 | } | |
256 | ||
85cf9dba RR |
257 | static inline int x86_pmu_initialized(void) |
258 | { | |
259 | return x86_pmu.handle_irq != NULL; | |
260 | } | |
261 | ||
8326f44d | 262 | static inline int |
e994d7d2 | 263 | set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event *event) |
8326f44d | 264 | { |
e994d7d2 | 265 | struct perf_event_attr *attr = &event->attr; |
8326f44d IM |
266 | unsigned int cache_type, cache_op, cache_result; |
267 | u64 config, val; | |
268 | ||
269 | config = attr->config; | |
270 | ||
271 | cache_type = (config >> 0) & 0xff; | |
272 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) | |
273 | return -EINVAL; | |
274 | ||
275 | cache_op = (config >> 8) & 0xff; | |
276 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) | |
277 | return -EINVAL; | |
278 | ||
279 | cache_result = (config >> 16) & 0xff; | |
280 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) | |
281 | return -EINVAL; | |
282 | ||
283 | val = hw_cache_event_ids[cache_type][cache_op][cache_result]; | |
284 | ||
285 | if (val == 0) | |
286 | return -ENOENT; | |
287 | ||
288 | if (val == -1) | |
289 | return -EINVAL; | |
290 | ||
291 | hwc->config |= val; | |
e994d7d2 AK |
292 | attr->config1 = hw_cache_extra_regs[cache_type][cache_op][cache_result]; |
293 | return x86_pmu_extra_regs(val, event); | |
8326f44d IM |
294 | } |
295 | ||
de0428a7 | 296 | int x86_setup_perfctr(struct perf_event *event) |
c1726f34 RR |
297 | { |
298 | struct perf_event_attr *attr = &event->attr; | |
299 | struct hw_perf_event *hwc = &event->hw; | |
300 | u64 config; | |
301 | ||
6c7e550f | 302 | if (!is_sampling_event(event)) { |
c1726f34 RR |
303 | hwc->sample_period = x86_pmu.max_period; |
304 | hwc->last_period = hwc->sample_period; | |
e7850595 | 305 | local64_set(&hwc->period_left, hwc->sample_period); |
c1726f34 RR |
306 | } else { |
307 | /* | |
308 | * If we have a PMU initialized but no APIC | |
309 | * interrupts, we cannot sample hardware | |
310 | * events (user-space has to fall back and | |
311 | * sample via a hrtimer based software event): | |
312 | */ | |
313 | if (!x86_pmu.apic) | |
314 | return -EOPNOTSUPP; | |
315 | } | |
316 | ||
317 | if (attr->type == PERF_TYPE_RAW) | |
ed13ec58 | 318 | return x86_pmu_extra_regs(event->attr.config, event); |
c1726f34 RR |
319 | |
320 | if (attr->type == PERF_TYPE_HW_CACHE) | |
e994d7d2 | 321 | return set_ext_hw_attr(hwc, event); |
c1726f34 RR |
322 | |
323 | if (attr->config >= x86_pmu.max_events) | |
324 | return -EINVAL; | |
325 | ||
326 | /* | |
327 | * The generic map: | |
328 | */ | |
329 | config = x86_pmu.event_map(attr->config); | |
330 | ||
331 | if (config == 0) | |
332 | return -ENOENT; | |
333 | ||
334 | if (config == -1LL) | |
335 | return -EINVAL; | |
336 | ||
337 | /* | |
338 | * Branch tracing: | |
339 | */ | |
18a073a3 PZ |
340 | if (attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS && |
341 | !attr->freq && hwc->sample_period == 1) { | |
c1726f34 | 342 | /* BTS is not supported by this architecture. */ |
6809b6ea | 343 | if (!x86_pmu.bts_active) |
c1726f34 RR |
344 | return -EOPNOTSUPP; |
345 | ||
346 | /* BTS is currently only allowed for user-mode. */ | |
347 | if (!attr->exclude_kernel) | |
348 | return -EOPNOTSUPP; | |
349 | } | |
350 | ||
351 | hwc->config |= config; | |
352 | ||
353 | return 0; | |
354 | } | |
4261e0e0 | 355 | |
de0428a7 | 356 | int x86_pmu_hw_config(struct perf_event *event) |
a072738e | 357 | { |
ab608344 PZ |
358 | if (event->attr.precise_ip) { |
359 | int precise = 0; | |
360 | ||
361 | /* Support for constant skid */ | |
6809b6ea | 362 | if (x86_pmu.pebs_active) { |
ab608344 PZ |
363 | precise++; |
364 | ||
5553be26 PZ |
365 | /* Support for IP fixup */ |
366 | if (x86_pmu.lbr_nr) | |
367 | precise++; | |
368 | } | |
ab608344 PZ |
369 | |
370 | if (event->attr.precise_ip > precise) | |
371 | return -EOPNOTSUPP; | |
372 | } | |
373 | ||
a072738e CG |
374 | /* |
375 | * Generate PMC IRQs: | |
376 | * (keep 'enabled' bit clear for now) | |
377 | */ | |
b4cdc5c2 | 378 | event->hw.config = ARCH_PERFMON_EVENTSEL_INT; |
a072738e CG |
379 | |
380 | /* | |
381 | * Count user and OS events unless requested not to | |
382 | */ | |
b4cdc5c2 PZ |
383 | if (!event->attr.exclude_user) |
384 | event->hw.config |= ARCH_PERFMON_EVENTSEL_USR; | |
385 | if (!event->attr.exclude_kernel) | |
386 | event->hw.config |= ARCH_PERFMON_EVENTSEL_OS; | |
a072738e | 387 | |
b4cdc5c2 PZ |
388 | if (event->attr.type == PERF_TYPE_RAW) |
389 | event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK; | |
a072738e | 390 | |
9d0fcba6 | 391 | return x86_setup_perfctr(event); |
a098f448 RR |
392 | } |
393 | ||
241771ef | 394 | /* |
0d48696f | 395 | * Setup the hardware configuration for a given attr_type |
241771ef | 396 | */ |
b0a873eb | 397 | static int __x86_pmu_event_init(struct perf_event *event) |
241771ef | 398 | { |
4e935e47 | 399 | int err; |
241771ef | 400 | |
85cf9dba RR |
401 | if (!x86_pmu_initialized()) |
402 | return -ENODEV; | |
241771ef | 403 | |
4e935e47 | 404 | err = 0; |
cdd6c482 | 405 | if (!atomic_inc_not_zero(&active_events)) { |
4e935e47 | 406 | mutex_lock(&pmc_reserve_mutex); |
cdd6c482 | 407 | if (atomic_read(&active_events) == 0) { |
30dd568c MM |
408 | if (!reserve_pmc_hardware()) |
409 | err = -EBUSY; | |
f80c9e30 PZ |
410 | else |
411 | reserve_ds_buffers(); | |
30dd568c MM |
412 | } |
413 | if (!err) | |
cdd6c482 | 414 | atomic_inc(&active_events); |
4e935e47 PZ |
415 | mutex_unlock(&pmc_reserve_mutex); |
416 | } | |
417 | if (err) | |
418 | return err; | |
419 | ||
cdd6c482 | 420 | event->destroy = hw_perf_event_destroy; |
a1792cda | 421 | |
4261e0e0 RR |
422 | event->hw.idx = -1; |
423 | event->hw.last_cpu = -1; | |
424 | event->hw.last_tag = ~0ULL; | |
b690081d | 425 | |
efc9f05d SE |
426 | /* mark unused */ |
427 | event->hw.extra_reg.idx = EXTRA_REG_NONE; | |
428 | ||
b36817e8 SE |
429 | /* mark not used */ |
430 | event->hw.extra_reg.idx = EXTRA_REG_NONE; | |
431 | event->hw.branch_reg.idx = EXTRA_REG_NONE; | |
432 | ||
9d0fcba6 | 433 | return x86_pmu.hw_config(event); |
4261e0e0 RR |
434 | } |
435 | ||
de0428a7 | 436 | void x86_pmu_disable_all(void) |
f87ad35d | 437 | { |
cdd6c482 | 438 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
9e35ad38 PZ |
439 | int idx; |
440 | ||
948b1bb8 | 441 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
b0f3f28e PZ |
442 | u64 val; |
443 | ||
43f6201a | 444 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 445 | continue; |
41bf4989 | 446 | rdmsrl(x86_pmu_config_addr(idx), val); |
bb1165d6 | 447 | if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE)) |
4295ee62 | 448 | continue; |
bb1165d6 | 449 | val &= ~ARCH_PERFMON_EVENTSEL_ENABLE; |
41bf4989 | 450 | wrmsrl(x86_pmu_config_addr(idx), val); |
f87ad35d | 451 | } |
f87ad35d JSR |
452 | } |
453 | ||
a4eaf7f1 | 454 | static void x86_pmu_disable(struct pmu *pmu) |
b56a3802 | 455 | { |
1da53e02 SE |
456 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
457 | ||
85cf9dba | 458 | if (!x86_pmu_initialized()) |
9e35ad38 | 459 | return; |
1da53e02 | 460 | |
1a6e21f7 PZ |
461 | if (!cpuc->enabled) |
462 | return; | |
463 | ||
464 | cpuc->n_added = 0; | |
465 | cpuc->enabled = 0; | |
466 | barrier(); | |
1da53e02 SE |
467 | |
468 | x86_pmu.disable_all(); | |
b56a3802 | 469 | } |
241771ef | 470 | |
de0428a7 | 471 | void x86_pmu_enable_all(int added) |
f87ad35d | 472 | { |
cdd6c482 | 473 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
f87ad35d JSR |
474 | int idx; |
475 | ||
948b1bb8 | 476 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
d45dd923 | 477 | struct hw_perf_event *hwc = &cpuc->events[idx]->hw; |
b0f3f28e | 478 | |
43f6201a | 479 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 480 | continue; |
984b838c | 481 | |
d45dd923 | 482 | __x86_pmu_enable_event(hwc, ARCH_PERFMON_EVENTSEL_ENABLE); |
f87ad35d JSR |
483 | } |
484 | } | |
485 | ||
51b0fe39 | 486 | static struct pmu pmu; |
1da53e02 SE |
487 | |
488 | static inline int is_x86_event(struct perf_event *event) | |
489 | { | |
490 | return event->pmu == &pmu; | |
491 | } | |
492 | ||
1e2ad28f RR |
493 | /* |
494 | * Event scheduler state: | |
495 | * | |
496 | * Assign events iterating over all events and counters, beginning | |
497 | * with events with least weights first. Keep the current iterator | |
498 | * state in struct sched_state. | |
499 | */ | |
500 | struct sched_state { | |
501 | int weight; | |
502 | int event; /* event index */ | |
503 | int counter; /* counter index */ | |
504 | int unassigned; /* number of events to be assigned left */ | |
505 | unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | |
506 | }; | |
507 | ||
bc1738f6 RR |
508 | /* Total max is X86_PMC_IDX_MAX, but we are O(n!) limited */ |
509 | #define SCHED_STATES_MAX 2 | |
510 | ||
1e2ad28f RR |
511 | struct perf_sched { |
512 | int max_weight; | |
513 | int max_events; | |
514 | struct event_constraint **constraints; | |
515 | struct sched_state state; | |
bc1738f6 RR |
516 | int saved_states; |
517 | struct sched_state saved[SCHED_STATES_MAX]; | |
1e2ad28f RR |
518 | }; |
519 | ||
520 | /* | |
521 | * Initialize interator that runs through all events and counters. | |
522 | */ | |
523 | static void perf_sched_init(struct perf_sched *sched, struct event_constraint **c, | |
524 | int num, int wmin, int wmax) | |
525 | { | |
526 | int idx; | |
527 | ||
528 | memset(sched, 0, sizeof(*sched)); | |
529 | sched->max_events = num; | |
530 | sched->max_weight = wmax; | |
531 | sched->constraints = c; | |
532 | ||
533 | for (idx = 0; idx < num; idx++) { | |
534 | if (c[idx]->weight == wmin) | |
535 | break; | |
536 | } | |
537 | ||
538 | sched->state.event = idx; /* start with min weight */ | |
539 | sched->state.weight = wmin; | |
540 | sched->state.unassigned = num; | |
541 | } | |
542 | ||
bc1738f6 RR |
543 | static void perf_sched_save_state(struct perf_sched *sched) |
544 | { | |
545 | if (WARN_ON_ONCE(sched->saved_states >= SCHED_STATES_MAX)) | |
546 | return; | |
547 | ||
548 | sched->saved[sched->saved_states] = sched->state; | |
549 | sched->saved_states++; | |
550 | } | |
551 | ||
552 | static bool perf_sched_restore_state(struct perf_sched *sched) | |
553 | { | |
554 | if (!sched->saved_states) | |
555 | return false; | |
556 | ||
557 | sched->saved_states--; | |
558 | sched->state = sched->saved[sched->saved_states]; | |
559 | ||
560 | /* continue with next counter: */ | |
561 | clear_bit(sched->state.counter++, sched->state.used); | |
562 | ||
563 | return true; | |
564 | } | |
565 | ||
1e2ad28f RR |
566 | /* |
567 | * Select a counter for the current event to schedule. Return true on | |
568 | * success. | |
569 | */ | |
bc1738f6 | 570 | static bool __perf_sched_find_counter(struct perf_sched *sched) |
1e2ad28f RR |
571 | { |
572 | struct event_constraint *c; | |
573 | int idx; | |
574 | ||
575 | if (!sched->state.unassigned) | |
576 | return false; | |
577 | ||
578 | if (sched->state.event >= sched->max_events) | |
579 | return false; | |
580 | ||
581 | c = sched->constraints[sched->state.event]; | |
582 | ||
4defea85 PZ |
583 | /* Prefer fixed purpose counters */ |
584 | if (x86_pmu.num_counters_fixed) { | |
585 | idx = X86_PMC_IDX_FIXED; | |
586 | for_each_set_bit_cont(idx, c->idxmsk, X86_PMC_IDX_MAX) { | |
587 | if (!__test_and_set_bit(idx, sched->state.used)) | |
588 | goto done; | |
589 | } | |
590 | } | |
1e2ad28f RR |
591 | /* Grab the first unused counter starting with idx */ |
592 | idx = sched->state.counter; | |
4defea85 | 593 | for_each_set_bit_cont(idx, c->idxmsk, X86_PMC_IDX_FIXED) { |
1e2ad28f | 594 | if (!__test_and_set_bit(idx, sched->state.used)) |
4defea85 | 595 | goto done; |
1e2ad28f | 596 | } |
1e2ad28f | 597 | |
4defea85 PZ |
598 | return false; |
599 | ||
600 | done: | |
601 | sched->state.counter = idx; | |
1e2ad28f | 602 | |
bc1738f6 RR |
603 | if (c->overlap) |
604 | perf_sched_save_state(sched); | |
605 | ||
606 | return true; | |
607 | } | |
608 | ||
609 | static bool perf_sched_find_counter(struct perf_sched *sched) | |
610 | { | |
611 | while (!__perf_sched_find_counter(sched)) { | |
612 | if (!perf_sched_restore_state(sched)) | |
613 | return false; | |
614 | } | |
615 | ||
1e2ad28f RR |
616 | return true; |
617 | } | |
618 | ||
619 | /* | |
620 | * Go through all unassigned events and find the next one to schedule. | |
621 | * Take events with the least weight first. Return true on success. | |
622 | */ | |
623 | static bool perf_sched_next_event(struct perf_sched *sched) | |
624 | { | |
625 | struct event_constraint *c; | |
626 | ||
627 | if (!sched->state.unassigned || !--sched->state.unassigned) | |
628 | return false; | |
629 | ||
630 | do { | |
631 | /* next event */ | |
632 | sched->state.event++; | |
633 | if (sched->state.event >= sched->max_events) { | |
634 | /* next weight */ | |
635 | sched->state.event = 0; | |
636 | sched->state.weight++; | |
637 | if (sched->state.weight > sched->max_weight) | |
638 | return false; | |
639 | } | |
640 | c = sched->constraints[sched->state.event]; | |
641 | } while (c->weight != sched->state.weight); | |
642 | ||
643 | sched->state.counter = 0; /* start with first counter */ | |
644 | ||
645 | return true; | |
646 | } | |
647 | ||
648 | /* | |
649 | * Assign a counter for each event. | |
650 | */ | |
651 | static int perf_assign_events(struct event_constraint **constraints, int n, | |
652 | int wmin, int wmax, int *assign) | |
653 | { | |
654 | struct perf_sched sched; | |
655 | ||
656 | perf_sched_init(&sched, constraints, n, wmin, wmax); | |
657 | ||
658 | do { | |
659 | if (!perf_sched_find_counter(&sched)) | |
660 | break; /* failed */ | |
661 | if (assign) | |
662 | assign[sched.state.event] = sched.state.counter; | |
663 | } while (perf_sched_next_event(&sched)); | |
664 | ||
665 | return sched.state.unassigned; | |
666 | } | |
667 | ||
de0428a7 | 668 | int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) |
1da53e02 | 669 | { |
63b14649 | 670 | struct event_constraint *c, *constraints[X86_PMC_IDX_MAX]; |
1da53e02 | 671 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
1e2ad28f | 672 | int i, wmin, wmax, num = 0; |
1da53e02 SE |
673 | struct hw_perf_event *hwc; |
674 | ||
675 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); | |
676 | ||
1e2ad28f | 677 | for (i = 0, wmin = X86_PMC_IDX_MAX, wmax = 0; i < n; i++) { |
b622d644 PZ |
678 | c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]); |
679 | constraints[i] = c; | |
1e2ad28f RR |
680 | wmin = min(wmin, c->weight); |
681 | wmax = max(wmax, c->weight); | |
1da53e02 SE |
682 | } |
683 | ||
8113070d SE |
684 | /* |
685 | * fastpath, try to reuse previous register | |
686 | */ | |
c933c1a6 | 687 | for (i = 0; i < n; i++) { |
8113070d | 688 | hwc = &cpuc->event_list[i]->hw; |
81269a08 | 689 | c = constraints[i]; |
8113070d SE |
690 | |
691 | /* never assigned */ | |
692 | if (hwc->idx == -1) | |
693 | break; | |
694 | ||
695 | /* constraint still honored */ | |
63b14649 | 696 | if (!test_bit(hwc->idx, c->idxmsk)) |
8113070d SE |
697 | break; |
698 | ||
699 | /* not already used */ | |
700 | if (test_bit(hwc->idx, used_mask)) | |
701 | break; | |
702 | ||
34538ee7 | 703 | __set_bit(hwc->idx, used_mask); |
8113070d SE |
704 | if (assign) |
705 | assign[i] = hwc->idx; | |
706 | } | |
8113070d | 707 | |
1e2ad28f RR |
708 | /* slow path */ |
709 | if (i != n) | |
710 | num = perf_assign_events(constraints, n, wmin, wmax, assign); | |
8113070d | 711 | |
1da53e02 SE |
712 | /* |
713 | * scheduling failed or is just a simulation, | |
714 | * free resources if necessary | |
715 | */ | |
716 | if (!assign || num) { | |
717 | for (i = 0; i < n; i++) { | |
718 | if (x86_pmu.put_event_constraints) | |
719 | x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]); | |
720 | } | |
721 | } | |
aa2bc1ad | 722 | return num ? -EINVAL : 0; |
1da53e02 SE |
723 | } |
724 | ||
725 | /* | |
726 | * dogrp: true if must collect siblings events (group) | |
727 | * returns total number of events and error code | |
728 | */ | |
729 | static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp) | |
730 | { | |
731 | struct perf_event *event; | |
732 | int n, max_count; | |
733 | ||
948b1bb8 | 734 | max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed; |
1da53e02 SE |
735 | |
736 | /* current number of events already accepted */ | |
737 | n = cpuc->n_events; | |
738 | ||
739 | if (is_x86_event(leader)) { | |
740 | if (n >= max_count) | |
aa2bc1ad | 741 | return -EINVAL; |
1da53e02 SE |
742 | cpuc->event_list[n] = leader; |
743 | n++; | |
744 | } | |
745 | if (!dogrp) | |
746 | return n; | |
747 | ||
748 | list_for_each_entry(event, &leader->sibling_list, group_entry) { | |
749 | if (!is_x86_event(event) || | |
8113070d | 750 | event->state <= PERF_EVENT_STATE_OFF) |
1da53e02 SE |
751 | continue; |
752 | ||
753 | if (n >= max_count) | |
aa2bc1ad | 754 | return -EINVAL; |
1da53e02 SE |
755 | |
756 | cpuc->event_list[n] = event; | |
757 | n++; | |
758 | } | |
759 | return n; | |
760 | } | |
761 | ||
1da53e02 | 762 | static inline void x86_assign_hw_event(struct perf_event *event, |
447a194b | 763 | struct cpu_hw_events *cpuc, int i) |
1da53e02 | 764 | { |
447a194b SE |
765 | struct hw_perf_event *hwc = &event->hw; |
766 | ||
767 | hwc->idx = cpuc->assign[i]; | |
768 | hwc->last_cpu = smp_processor_id(); | |
769 | hwc->last_tag = ++cpuc->tags[i]; | |
1da53e02 SE |
770 | |
771 | if (hwc->idx == X86_PMC_IDX_FIXED_BTS) { | |
772 | hwc->config_base = 0; | |
773 | hwc->event_base = 0; | |
774 | } else if (hwc->idx >= X86_PMC_IDX_FIXED) { | |
775 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; | |
fc66c521 | 776 | hwc->event_base = MSR_ARCH_PERFMON_FIXED_CTR0 + (hwc->idx - X86_PMC_IDX_FIXED); |
1da53e02 | 777 | } else { |
73d6e522 RR |
778 | hwc->config_base = x86_pmu_config_addr(hwc->idx); |
779 | hwc->event_base = x86_pmu_event_addr(hwc->idx); | |
1da53e02 SE |
780 | } |
781 | } | |
782 | ||
447a194b SE |
783 | static inline int match_prev_assignment(struct hw_perf_event *hwc, |
784 | struct cpu_hw_events *cpuc, | |
785 | int i) | |
786 | { | |
787 | return hwc->idx == cpuc->assign[i] && | |
788 | hwc->last_cpu == smp_processor_id() && | |
789 | hwc->last_tag == cpuc->tags[i]; | |
790 | } | |
791 | ||
a4eaf7f1 | 792 | static void x86_pmu_start(struct perf_event *event, int flags); |
2e841873 | 793 | |
a4eaf7f1 | 794 | static void x86_pmu_enable(struct pmu *pmu) |
ee06094f | 795 | { |
1da53e02 SE |
796 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
797 | struct perf_event *event; | |
798 | struct hw_perf_event *hwc; | |
11164cd4 | 799 | int i, added = cpuc->n_added; |
1da53e02 | 800 | |
85cf9dba | 801 | if (!x86_pmu_initialized()) |
2b9ff0db | 802 | return; |
1a6e21f7 PZ |
803 | |
804 | if (cpuc->enabled) | |
805 | return; | |
806 | ||
1da53e02 | 807 | if (cpuc->n_added) { |
19925ce7 | 808 | int n_running = cpuc->n_events - cpuc->n_added; |
1da53e02 SE |
809 | /* |
810 | * apply assignment obtained either from | |
811 | * hw_perf_group_sched_in() or x86_pmu_enable() | |
812 | * | |
813 | * step1: save events moving to new counters | |
814 | * step2: reprogram moved events into new counters | |
815 | */ | |
19925ce7 | 816 | for (i = 0; i < n_running; i++) { |
1da53e02 SE |
817 | event = cpuc->event_list[i]; |
818 | hwc = &event->hw; | |
819 | ||
447a194b SE |
820 | /* |
821 | * we can avoid reprogramming counter if: | |
822 | * - assigned same counter as last time | |
823 | * - running on same CPU as last time | |
824 | * - no other event has used the counter since | |
825 | */ | |
826 | if (hwc->idx == -1 || | |
827 | match_prev_assignment(hwc, cpuc, i)) | |
1da53e02 SE |
828 | continue; |
829 | ||
a4eaf7f1 PZ |
830 | /* |
831 | * Ensure we don't accidentally enable a stopped | |
832 | * counter simply because we rescheduled. | |
833 | */ | |
834 | if (hwc->state & PERF_HES_STOPPED) | |
835 | hwc->state |= PERF_HES_ARCH; | |
836 | ||
837 | x86_pmu_stop(event, PERF_EF_UPDATE); | |
1da53e02 SE |
838 | } |
839 | ||
840 | for (i = 0; i < cpuc->n_events; i++) { | |
1da53e02 SE |
841 | event = cpuc->event_list[i]; |
842 | hwc = &event->hw; | |
843 | ||
45e16a68 | 844 | if (!match_prev_assignment(hwc, cpuc, i)) |
447a194b | 845 | x86_assign_hw_event(event, cpuc, i); |
45e16a68 PZ |
846 | else if (i < n_running) |
847 | continue; | |
1da53e02 | 848 | |
a4eaf7f1 PZ |
849 | if (hwc->state & PERF_HES_ARCH) |
850 | continue; | |
851 | ||
852 | x86_pmu_start(event, PERF_EF_RELOAD); | |
1da53e02 SE |
853 | } |
854 | cpuc->n_added = 0; | |
855 | perf_events_lapic_init(); | |
856 | } | |
1a6e21f7 PZ |
857 | |
858 | cpuc->enabled = 1; | |
859 | barrier(); | |
860 | ||
11164cd4 | 861 | x86_pmu.enable_all(added); |
ee06094f | 862 | } |
ee06094f | 863 | |
245b2e70 | 864 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
241771ef | 865 | |
ee06094f IM |
866 | /* |
867 | * Set the next IRQ period, based on the hwc->period_left value. | |
cdd6c482 | 868 | * To be called with the event disabled in hw: |
ee06094f | 869 | */ |
de0428a7 | 870 | int x86_perf_event_set_period(struct perf_event *event) |
241771ef | 871 | { |
07088edb | 872 | struct hw_perf_event *hwc = &event->hw; |
e7850595 | 873 | s64 left = local64_read(&hwc->period_left); |
e4abb5d4 | 874 | s64 period = hwc->sample_period; |
7645a24c | 875 | int ret = 0, idx = hwc->idx; |
ee06094f | 876 | |
30dd568c MM |
877 | if (idx == X86_PMC_IDX_FIXED_BTS) |
878 | return 0; | |
879 | ||
ee06094f | 880 | /* |
af901ca1 | 881 | * If we are way outside a reasonable range then just skip forward: |
ee06094f IM |
882 | */ |
883 | if (unlikely(left <= -period)) { | |
884 | left = period; | |
e7850595 | 885 | local64_set(&hwc->period_left, left); |
9e350de3 | 886 | hwc->last_period = period; |
e4abb5d4 | 887 | ret = 1; |
ee06094f IM |
888 | } |
889 | ||
890 | if (unlikely(left <= 0)) { | |
891 | left += period; | |
e7850595 | 892 | local64_set(&hwc->period_left, left); |
9e350de3 | 893 | hwc->last_period = period; |
e4abb5d4 | 894 | ret = 1; |
ee06094f | 895 | } |
1c80f4b5 | 896 | /* |
dfc65094 | 897 | * Quirk: certain CPUs dont like it if just 1 hw_event is left: |
1c80f4b5 IM |
898 | */ |
899 | if (unlikely(left < 2)) | |
900 | left = 2; | |
241771ef | 901 | |
e4abb5d4 PZ |
902 | if (left > x86_pmu.max_period) |
903 | left = x86_pmu.max_period; | |
904 | ||
245b2e70 | 905 | per_cpu(pmc_prev_left[idx], smp_processor_id()) = left; |
ee06094f IM |
906 | |
907 | /* | |
cdd6c482 | 908 | * The hw event starts counting from this event offset, |
ee06094f IM |
909 | * mark it to be able to extra future deltas: |
910 | */ | |
e7850595 | 911 | local64_set(&hwc->prev_count, (u64)-left); |
ee06094f | 912 | |
73d6e522 | 913 | wrmsrl(hwc->event_base, (u64)(-left) & x86_pmu.cntval_mask); |
68aa00ac CG |
914 | |
915 | /* | |
916 | * Due to erratum on certan cpu we need | |
917 | * a second write to be sure the register | |
918 | * is updated properly | |
919 | */ | |
920 | if (x86_pmu.perfctr_second_write) { | |
73d6e522 | 921 | wrmsrl(hwc->event_base, |
948b1bb8 | 922 | (u64)(-left) & x86_pmu.cntval_mask); |
68aa00ac | 923 | } |
e4abb5d4 | 924 | |
cdd6c482 | 925 | perf_event_update_userpage(event); |
194002b2 | 926 | |
e4abb5d4 | 927 | return ret; |
2f18d1e8 IM |
928 | } |
929 | ||
de0428a7 | 930 | void x86_pmu_enable_event(struct perf_event *event) |
7c90cc45 | 931 | { |
0a3aee0d | 932 | if (__this_cpu_read(cpu_hw_events.enabled)) |
31fa58af RR |
933 | __x86_pmu_enable_event(&event->hw, |
934 | ARCH_PERFMON_EVENTSEL_ENABLE); | |
241771ef IM |
935 | } |
936 | ||
b690081d | 937 | /* |
a4eaf7f1 | 938 | * Add a single event to the PMU. |
1da53e02 SE |
939 | * |
940 | * The event is added to the group of enabled events | |
941 | * but only if it can be scehduled with existing events. | |
fe9081cc | 942 | */ |
a4eaf7f1 | 943 | static int x86_pmu_add(struct perf_event *event, int flags) |
fe9081cc PZ |
944 | { |
945 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | |
1da53e02 SE |
946 | struct hw_perf_event *hwc; |
947 | int assign[X86_PMC_IDX_MAX]; | |
948 | int n, n0, ret; | |
fe9081cc | 949 | |
1da53e02 | 950 | hwc = &event->hw; |
fe9081cc | 951 | |
33696fc0 | 952 | perf_pmu_disable(event->pmu); |
1da53e02 | 953 | n0 = cpuc->n_events; |
24cd7f54 PZ |
954 | ret = n = collect_events(cpuc, event, false); |
955 | if (ret < 0) | |
956 | goto out; | |
53b441a5 | 957 | |
a4eaf7f1 PZ |
958 | hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED; |
959 | if (!(flags & PERF_EF_START)) | |
960 | hwc->state |= PERF_HES_ARCH; | |
961 | ||
4d1c52b0 LM |
962 | /* |
963 | * If group events scheduling transaction was started, | |
0d2eb44f | 964 | * skip the schedulability test here, it will be performed |
a4eaf7f1 | 965 | * at commit time (->commit_txn) as a whole |
4d1c52b0 | 966 | */ |
8d2cacbb | 967 | if (cpuc->group_flag & PERF_EVENT_TXN) |
24cd7f54 | 968 | goto done_collect; |
4d1c52b0 | 969 | |
a072738e | 970 | ret = x86_pmu.schedule_events(cpuc, n, assign); |
1da53e02 | 971 | if (ret) |
24cd7f54 | 972 | goto out; |
1da53e02 SE |
973 | /* |
974 | * copy new assignment, now we know it is possible | |
975 | * will be used by hw_perf_enable() | |
976 | */ | |
977 | memcpy(cpuc->assign, assign, n*sizeof(int)); | |
7e2ae347 | 978 | |
24cd7f54 | 979 | done_collect: |
1da53e02 | 980 | cpuc->n_events = n; |
356e1f2e | 981 | cpuc->n_added += n - n0; |
90151c35 | 982 | cpuc->n_txn += n - n0; |
95cdd2e7 | 983 | |
24cd7f54 PZ |
984 | ret = 0; |
985 | out: | |
33696fc0 | 986 | perf_pmu_enable(event->pmu); |
24cd7f54 | 987 | return ret; |
241771ef IM |
988 | } |
989 | ||
a4eaf7f1 | 990 | static void x86_pmu_start(struct perf_event *event, int flags) |
d76a0812 | 991 | { |
c08053e6 PZ |
992 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
993 | int idx = event->hw.idx; | |
994 | ||
a4eaf7f1 PZ |
995 | if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED))) |
996 | return; | |
997 | ||
998 | if (WARN_ON_ONCE(idx == -1)) | |
999 | return; | |
1000 | ||
1001 | if (flags & PERF_EF_RELOAD) { | |
1002 | WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE)); | |
1003 | x86_perf_event_set_period(event); | |
1004 | } | |
1005 | ||
1006 | event->hw.state = 0; | |
d76a0812 | 1007 | |
c08053e6 PZ |
1008 | cpuc->events[idx] = event; |
1009 | __set_bit(idx, cpuc->active_mask); | |
63e6be6d | 1010 | __set_bit(idx, cpuc->running); |
aff3d91a | 1011 | x86_pmu.enable(event); |
c08053e6 | 1012 | perf_event_update_userpage(event); |
a78ac325 PZ |
1013 | } |
1014 | ||
cdd6c482 | 1015 | void perf_event_print_debug(void) |
241771ef | 1016 | { |
2f18d1e8 | 1017 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
ca037701 | 1018 | u64 pebs; |
cdd6c482 | 1019 | struct cpu_hw_events *cpuc; |
5bb9efe3 | 1020 | unsigned long flags; |
1e125676 IM |
1021 | int cpu, idx; |
1022 | ||
948b1bb8 | 1023 | if (!x86_pmu.num_counters) |
1e125676 | 1024 | return; |
241771ef | 1025 | |
5bb9efe3 | 1026 | local_irq_save(flags); |
241771ef IM |
1027 | |
1028 | cpu = smp_processor_id(); | |
cdd6c482 | 1029 | cpuc = &per_cpu(cpu_hw_events, cpu); |
241771ef | 1030 | |
faa28ae0 | 1031 | if (x86_pmu.version >= 2) { |
a1ef58f4 JSR |
1032 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
1033 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); | |
1034 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); | |
1035 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); | |
ca037701 | 1036 | rdmsrl(MSR_IA32_PEBS_ENABLE, pebs); |
a1ef58f4 JSR |
1037 | |
1038 | pr_info("\n"); | |
1039 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); | |
1040 | pr_info("CPU#%d: status: %016llx\n", cpu, status); | |
1041 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); | |
1042 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); | |
ca037701 | 1043 | pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs); |
f87ad35d | 1044 | } |
7645a24c | 1045 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); |
241771ef | 1046 | |
948b1bb8 | 1047 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
41bf4989 RR |
1048 | rdmsrl(x86_pmu_config_addr(idx), pmc_ctrl); |
1049 | rdmsrl(x86_pmu_event_addr(idx), pmc_count); | |
241771ef | 1050 | |
245b2e70 | 1051 | prev_left = per_cpu(pmc_prev_left[idx], cpu); |
241771ef | 1052 | |
a1ef58f4 | 1053 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
241771ef | 1054 | cpu, idx, pmc_ctrl); |
a1ef58f4 | 1055 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
241771ef | 1056 | cpu, idx, pmc_count); |
a1ef58f4 | 1057 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
ee06094f | 1058 | cpu, idx, prev_left); |
241771ef | 1059 | } |
948b1bb8 | 1060 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { |
2f18d1e8 IM |
1061 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
1062 | ||
a1ef58f4 | 1063 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
2f18d1e8 IM |
1064 | cpu, idx, pmc_count); |
1065 | } | |
5bb9efe3 | 1066 | local_irq_restore(flags); |
241771ef IM |
1067 | } |
1068 | ||
de0428a7 | 1069 | void x86_pmu_stop(struct perf_event *event, int flags) |
241771ef | 1070 | { |
d76a0812 | 1071 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
cdd6c482 | 1072 | struct hw_perf_event *hwc = &event->hw; |
241771ef | 1073 | |
a4eaf7f1 PZ |
1074 | if (__test_and_clear_bit(hwc->idx, cpuc->active_mask)) { |
1075 | x86_pmu.disable(event); | |
1076 | cpuc->events[hwc->idx] = NULL; | |
1077 | WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED); | |
1078 | hwc->state |= PERF_HES_STOPPED; | |
1079 | } | |
30dd568c | 1080 | |
a4eaf7f1 PZ |
1081 | if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { |
1082 | /* | |
1083 | * Drain the remaining delta count out of a event | |
1084 | * that we are disabling: | |
1085 | */ | |
1086 | x86_perf_event_update(event); | |
1087 | hwc->state |= PERF_HES_UPTODATE; | |
1088 | } | |
2e841873 PZ |
1089 | } |
1090 | ||
a4eaf7f1 | 1091 | static void x86_pmu_del(struct perf_event *event, int flags) |
2e841873 PZ |
1092 | { |
1093 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | |
1094 | int i; | |
1095 | ||
90151c35 SE |
1096 | /* |
1097 | * If we're called during a txn, we don't need to do anything. | |
1098 | * The events never got scheduled and ->cancel_txn will truncate | |
1099 | * the event_list. | |
1100 | */ | |
8d2cacbb | 1101 | if (cpuc->group_flag & PERF_EVENT_TXN) |
90151c35 SE |
1102 | return; |
1103 | ||
a4eaf7f1 | 1104 | x86_pmu_stop(event, PERF_EF_UPDATE); |
194002b2 | 1105 | |
1da53e02 SE |
1106 | for (i = 0; i < cpuc->n_events; i++) { |
1107 | if (event == cpuc->event_list[i]) { | |
1108 | ||
1109 | if (x86_pmu.put_event_constraints) | |
1110 | x86_pmu.put_event_constraints(cpuc, event); | |
1111 | ||
1112 | while (++i < cpuc->n_events) | |
1113 | cpuc->event_list[i-1] = cpuc->event_list[i]; | |
1114 | ||
1115 | --cpuc->n_events; | |
6c9687ab | 1116 | break; |
1da53e02 SE |
1117 | } |
1118 | } | |
cdd6c482 | 1119 | perf_event_update_userpage(event); |
241771ef IM |
1120 | } |
1121 | ||
de0428a7 | 1122 | int x86_pmu_handle_irq(struct pt_regs *regs) |
a29aa8a7 | 1123 | { |
df1a132b | 1124 | struct perf_sample_data data; |
cdd6c482 IM |
1125 | struct cpu_hw_events *cpuc; |
1126 | struct perf_event *event; | |
11d1578f | 1127 | int idx, handled = 0; |
9029a5e3 IM |
1128 | u64 val; |
1129 | ||
dc1d628a | 1130 | perf_sample_data_init(&data, 0); |
df1a132b | 1131 | |
cdd6c482 | 1132 | cpuc = &__get_cpu_var(cpu_hw_events); |
962bf7a6 | 1133 | |
2bce5dac DZ |
1134 | /* |
1135 | * Some chipsets need to unmask the LVTPC in a particular spot | |
1136 | * inside the nmi handler. As a result, the unmasking was pushed | |
1137 | * into all the nmi handlers. | |
1138 | * | |
1139 | * This generic handler doesn't seem to have any issues where the | |
1140 | * unmasking occurs so it was left at the top. | |
1141 | */ | |
1142 | apic_write(APIC_LVTPC, APIC_DM_NMI); | |
1143 | ||
948b1bb8 | 1144 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
63e6be6d RR |
1145 | if (!test_bit(idx, cpuc->active_mask)) { |
1146 | /* | |
1147 | * Though we deactivated the counter some cpus | |
1148 | * might still deliver spurious interrupts still | |
1149 | * in flight. Catch them: | |
1150 | */ | |
1151 | if (__test_and_clear_bit(idx, cpuc->running)) | |
1152 | handled++; | |
a29aa8a7 | 1153 | continue; |
63e6be6d | 1154 | } |
962bf7a6 | 1155 | |
cdd6c482 | 1156 | event = cpuc->events[idx]; |
a4016a79 | 1157 | |
cc2ad4ba | 1158 | val = x86_perf_event_update(event); |
948b1bb8 | 1159 | if (val & (1ULL << (x86_pmu.cntval_bits - 1))) |
48e22d56 | 1160 | continue; |
962bf7a6 | 1161 | |
9e350de3 | 1162 | /* |
cdd6c482 | 1163 | * event overflow |
9e350de3 | 1164 | */ |
4177c42a | 1165 | handled++; |
cdd6c482 | 1166 | data.period = event->hw.last_period; |
9e350de3 | 1167 | |
07088edb | 1168 | if (!x86_perf_event_set_period(event)) |
e4abb5d4 PZ |
1169 | continue; |
1170 | ||
a8b0ca17 | 1171 | if (perf_event_overflow(event, &data, regs)) |
a4eaf7f1 | 1172 | x86_pmu_stop(event, 0); |
a29aa8a7 | 1173 | } |
962bf7a6 | 1174 | |
9e350de3 PZ |
1175 | if (handled) |
1176 | inc_irq_stat(apic_perf_irqs); | |
1177 | ||
a29aa8a7 RR |
1178 | return handled; |
1179 | } | |
39d81eab | 1180 | |
cdd6c482 | 1181 | void perf_events_lapic_init(void) |
241771ef | 1182 | { |
04da8a43 | 1183 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
241771ef | 1184 | return; |
85cf9dba | 1185 | |
241771ef | 1186 | /* |
c323d95f | 1187 | * Always use NMI for PMU |
241771ef | 1188 | */ |
c323d95f | 1189 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
241771ef IM |
1190 | } |
1191 | ||
1192 | static int __kprobes | |
9c48f1c6 | 1193 | perf_event_nmi_handler(unsigned int cmd, struct pt_regs *regs) |
241771ef | 1194 | { |
cdd6c482 | 1195 | if (!atomic_read(&active_events)) |
9c48f1c6 | 1196 | return NMI_DONE; |
4177c42a | 1197 | |
9c48f1c6 | 1198 | return x86_pmu.handle_irq(regs); |
241771ef IM |
1199 | } |
1200 | ||
de0428a7 KW |
1201 | struct event_constraint emptyconstraint; |
1202 | struct event_constraint unconstrained; | |
f87ad35d | 1203 | |
3f6da390 PZ |
1204 | static int __cpuinit |
1205 | x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu) | |
1206 | { | |
1207 | unsigned int cpu = (long)hcpu; | |
7fdba1ca | 1208 | struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); |
b38b24ea | 1209 | int ret = NOTIFY_OK; |
3f6da390 PZ |
1210 | |
1211 | switch (action & ~CPU_TASKS_FROZEN) { | |
1212 | case CPU_UP_PREPARE: | |
7fdba1ca | 1213 | cpuc->kfree_on_online = NULL; |
3f6da390 | 1214 | if (x86_pmu.cpu_prepare) |
b38b24ea | 1215 | ret = x86_pmu.cpu_prepare(cpu); |
3f6da390 PZ |
1216 | break; |
1217 | ||
1218 | case CPU_STARTING: | |
0c9d42ed PZ |
1219 | if (x86_pmu.attr_rdpmc) |
1220 | set_in_cr4(X86_CR4_PCE); | |
3f6da390 PZ |
1221 | if (x86_pmu.cpu_starting) |
1222 | x86_pmu.cpu_starting(cpu); | |
1223 | break; | |
1224 | ||
7fdba1ca PZ |
1225 | case CPU_ONLINE: |
1226 | kfree(cpuc->kfree_on_online); | |
1227 | break; | |
1228 | ||
3f6da390 PZ |
1229 | case CPU_DYING: |
1230 | if (x86_pmu.cpu_dying) | |
1231 | x86_pmu.cpu_dying(cpu); | |
1232 | break; | |
1233 | ||
b38b24ea | 1234 | case CPU_UP_CANCELED: |
3f6da390 PZ |
1235 | case CPU_DEAD: |
1236 | if (x86_pmu.cpu_dead) | |
1237 | x86_pmu.cpu_dead(cpu); | |
1238 | break; | |
1239 | ||
1240 | default: | |
1241 | break; | |
1242 | } | |
1243 | ||
b38b24ea | 1244 | return ret; |
3f6da390 PZ |
1245 | } |
1246 | ||
12558038 CG |
1247 | static void __init pmu_check_apic(void) |
1248 | { | |
1249 | if (cpu_has_apic) | |
1250 | return; | |
1251 | ||
1252 | x86_pmu.apic = 0; | |
1253 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); | |
1254 | pr_info("no hardware sampling interrupt available.\n"); | |
1255 | } | |
1256 | ||
dda99116 | 1257 | static int __init init_hw_perf_events(void) |
b56a3802 | 1258 | { |
c1d6f42f | 1259 | struct x86_pmu_quirk *quirk; |
b622d644 | 1260 | struct event_constraint *c; |
72eae04d RR |
1261 | int err; |
1262 | ||
cdd6c482 | 1263 | pr_info("Performance Events: "); |
1123e3ad | 1264 | |
b56a3802 JSR |
1265 | switch (boot_cpu_data.x86_vendor) { |
1266 | case X86_VENDOR_INTEL: | |
72eae04d | 1267 | err = intel_pmu_init(); |
b56a3802 | 1268 | break; |
f87ad35d | 1269 | case X86_VENDOR_AMD: |
72eae04d | 1270 | err = amd_pmu_init(); |
f87ad35d | 1271 | break; |
4138960a | 1272 | default: |
004417a6 | 1273 | return 0; |
b56a3802 | 1274 | } |
1123e3ad | 1275 | if (err != 0) { |
cdd6c482 | 1276 | pr_cont("no PMU driver, software events only.\n"); |
004417a6 | 1277 | return 0; |
1123e3ad | 1278 | } |
b56a3802 | 1279 | |
12558038 CG |
1280 | pmu_check_apic(); |
1281 | ||
33c6d6a7 | 1282 | /* sanity check that the hardware exists or is emulated */ |
4407204c | 1283 | if (!check_hw_exists()) |
004417a6 | 1284 | return 0; |
33c6d6a7 | 1285 | |
1123e3ad | 1286 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
faa28ae0 | 1287 | |
c1d6f42f PZ |
1288 | for (quirk = x86_pmu.quirks; quirk; quirk = quirk->next) |
1289 | quirk->func(); | |
3c44780b | 1290 | |
948b1bb8 | 1291 | if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) { |
cdd6c482 | 1292 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", |
948b1bb8 RR |
1293 | x86_pmu.num_counters, X86_PMC_MAX_GENERIC); |
1294 | x86_pmu.num_counters = X86_PMC_MAX_GENERIC; | |
241771ef | 1295 | } |
948b1bb8 | 1296 | x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1; |
241771ef | 1297 | |
948b1bb8 | 1298 | if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) { |
cdd6c482 | 1299 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", |
948b1bb8 RR |
1300 | x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED); |
1301 | x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED; | |
703e937c | 1302 | } |
862a1a5f | 1303 | |
d6dc0b4e | 1304 | x86_pmu.intel_ctrl |= |
948b1bb8 | 1305 | ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED; |
241771ef | 1306 | |
cdd6c482 | 1307 | perf_events_lapic_init(); |
9c48f1c6 | 1308 | register_nmi_handler(NMI_LOCAL, perf_event_nmi_handler, 0, "PMI"); |
1123e3ad | 1309 | |
63b14649 | 1310 | unconstrained = (struct event_constraint) |
948b1bb8 | 1311 | __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1, |
bc1738f6 | 1312 | 0, x86_pmu.num_counters, 0); |
63b14649 | 1313 | |
b622d644 | 1314 | if (x86_pmu.event_constraints) { |
cd09c0c4 SE |
1315 | /* |
1316 | * event on fixed counter2 (REF_CYCLES) only works on this | |
1317 | * counter, so do not extend mask to generic counters | |
1318 | */ | |
b622d644 | 1319 | for_each_event_constraint(c, x86_pmu.event_constraints) { |
cd09c0c4 SE |
1320 | if (c->cmask != X86_RAW_EVENT_MASK |
1321 | || c->idxmsk64 == X86_PMC_MSK_FIXED_REF_CYCLES) { | |
b622d644 | 1322 | continue; |
cd09c0c4 | 1323 | } |
b622d644 | 1324 | |
948b1bb8 RR |
1325 | c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1; |
1326 | c->weight += x86_pmu.num_counters; | |
b622d644 PZ |
1327 | } |
1328 | } | |
1329 | ||
0c9d42ed PZ |
1330 | x86_pmu.attr_rdpmc = 1; /* enable userspace RDPMC usage by default */ |
1331 | ||
57c0c15b | 1332 | pr_info("... version: %d\n", x86_pmu.version); |
948b1bb8 RR |
1333 | pr_info("... bit width: %d\n", x86_pmu.cntval_bits); |
1334 | pr_info("... generic registers: %d\n", x86_pmu.num_counters); | |
1335 | pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask); | |
57c0c15b | 1336 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
948b1bb8 | 1337 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed); |
d6dc0b4e | 1338 | pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl); |
3f6da390 | 1339 | |
2e80a82a | 1340 | perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW); |
3f6da390 | 1341 | perf_cpu_notifier(x86_pmu_notifier); |
004417a6 PZ |
1342 | |
1343 | return 0; | |
241771ef | 1344 | } |
004417a6 | 1345 | early_initcall(init_hw_perf_events); |
621a01ea | 1346 | |
cdd6c482 | 1347 | static inline void x86_pmu_read(struct perf_event *event) |
ee06094f | 1348 | { |
cc2ad4ba | 1349 | x86_perf_event_update(event); |
ee06094f IM |
1350 | } |
1351 | ||
4d1c52b0 LM |
1352 | /* |
1353 | * Start group events scheduling transaction | |
1354 | * Set the flag to make pmu::enable() not perform the | |
1355 | * schedulability test, it will be performed at commit time | |
1356 | */ | |
51b0fe39 | 1357 | static void x86_pmu_start_txn(struct pmu *pmu) |
4d1c52b0 | 1358 | { |
33696fc0 | 1359 | perf_pmu_disable(pmu); |
0a3aee0d TH |
1360 | __this_cpu_or(cpu_hw_events.group_flag, PERF_EVENT_TXN); |
1361 | __this_cpu_write(cpu_hw_events.n_txn, 0); | |
4d1c52b0 LM |
1362 | } |
1363 | ||
1364 | /* | |
1365 | * Stop group events scheduling transaction | |
1366 | * Clear the flag and pmu::enable() will perform the | |
1367 | * schedulability test. | |
1368 | */ | |
51b0fe39 | 1369 | static void x86_pmu_cancel_txn(struct pmu *pmu) |
4d1c52b0 | 1370 | { |
0a3aee0d | 1371 | __this_cpu_and(cpu_hw_events.group_flag, ~PERF_EVENT_TXN); |
90151c35 SE |
1372 | /* |
1373 | * Truncate the collected events. | |
1374 | */ | |
0a3aee0d TH |
1375 | __this_cpu_sub(cpu_hw_events.n_added, __this_cpu_read(cpu_hw_events.n_txn)); |
1376 | __this_cpu_sub(cpu_hw_events.n_events, __this_cpu_read(cpu_hw_events.n_txn)); | |
33696fc0 | 1377 | perf_pmu_enable(pmu); |
4d1c52b0 LM |
1378 | } |
1379 | ||
1380 | /* | |
1381 | * Commit group events scheduling transaction | |
1382 | * Perform the group schedulability test as a whole | |
1383 | * Return 0 if success | |
1384 | */ | |
51b0fe39 | 1385 | static int x86_pmu_commit_txn(struct pmu *pmu) |
4d1c52b0 LM |
1386 | { |
1387 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | |
1388 | int assign[X86_PMC_IDX_MAX]; | |
1389 | int n, ret; | |
1390 | ||
1391 | n = cpuc->n_events; | |
1392 | ||
1393 | if (!x86_pmu_initialized()) | |
1394 | return -EAGAIN; | |
1395 | ||
1396 | ret = x86_pmu.schedule_events(cpuc, n, assign); | |
1397 | if (ret) | |
1398 | return ret; | |
1399 | ||
1400 | /* | |
1401 | * copy new assignment, now we know it is possible | |
1402 | * will be used by hw_perf_enable() | |
1403 | */ | |
1404 | memcpy(cpuc->assign, assign, n*sizeof(int)); | |
1405 | ||
8d2cacbb | 1406 | cpuc->group_flag &= ~PERF_EVENT_TXN; |
33696fc0 | 1407 | perf_pmu_enable(pmu); |
4d1c52b0 LM |
1408 | return 0; |
1409 | } | |
cd8a38d3 SE |
1410 | /* |
1411 | * a fake_cpuc is used to validate event groups. Due to | |
1412 | * the extra reg logic, we need to also allocate a fake | |
1413 | * per_core and per_cpu structure. Otherwise, group events | |
1414 | * using extra reg may conflict without the kernel being | |
1415 | * able to catch this when the last event gets added to | |
1416 | * the group. | |
1417 | */ | |
1418 | static void free_fake_cpuc(struct cpu_hw_events *cpuc) | |
1419 | { | |
1420 | kfree(cpuc->shared_regs); | |
1421 | kfree(cpuc); | |
1422 | } | |
1423 | ||
1424 | static struct cpu_hw_events *allocate_fake_cpuc(void) | |
1425 | { | |
1426 | struct cpu_hw_events *cpuc; | |
1427 | int cpu = raw_smp_processor_id(); | |
1428 | ||
1429 | cpuc = kzalloc(sizeof(*cpuc), GFP_KERNEL); | |
1430 | if (!cpuc) | |
1431 | return ERR_PTR(-ENOMEM); | |
1432 | ||
1433 | /* only needed, if we have extra_regs */ | |
1434 | if (x86_pmu.extra_regs) { | |
1435 | cpuc->shared_regs = allocate_shared_regs(cpu); | |
1436 | if (!cpuc->shared_regs) | |
1437 | goto error; | |
1438 | } | |
1439 | return cpuc; | |
1440 | error: | |
1441 | free_fake_cpuc(cpuc); | |
1442 | return ERR_PTR(-ENOMEM); | |
1443 | } | |
4d1c52b0 | 1444 | |
ca037701 PZ |
1445 | /* |
1446 | * validate that we can schedule this event | |
1447 | */ | |
1448 | static int validate_event(struct perf_event *event) | |
1449 | { | |
1450 | struct cpu_hw_events *fake_cpuc; | |
1451 | struct event_constraint *c; | |
1452 | int ret = 0; | |
1453 | ||
cd8a38d3 SE |
1454 | fake_cpuc = allocate_fake_cpuc(); |
1455 | if (IS_ERR(fake_cpuc)) | |
1456 | return PTR_ERR(fake_cpuc); | |
ca037701 PZ |
1457 | |
1458 | c = x86_pmu.get_event_constraints(fake_cpuc, event); | |
1459 | ||
1460 | if (!c || !c->weight) | |
aa2bc1ad | 1461 | ret = -EINVAL; |
ca037701 PZ |
1462 | |
1463 | if (x86_pmu.put_event_constraints) | |
1464 | x86_pmu.put_event_constraints(fake_cpuc, event); | |
1465 | ||
cd8a38d3 | 1466 | free_fake_cpuc(fake_cpuc); |
ca037701 PZ |
1467 | |
1468 | return ret; | |
1469 | } | |
1470 | ||
1da53e02 SE |
1471 | /* |
1472 | * validate a single event group | |
1473 | * | |
1474 | * validation include: | |
184f412c IM |
1475 | * - check events are compatible which each other |
1476 | * - events do not compete for the same counter | |
1477 | * - number of events <= number of counters | |
1da53e02 SE |
1478 | * |
1479 | * validation ensures the group can be loaded onto the | |
1480 | * PMU if it was the only group available. | |
1481 | */ | |
fe9081cc PZ |
1482 | static int validate_group(struct perf_event *event) |
1483 | { | |
1da53e02 | 1484 | struct perf_event *leader = event->group_leader; |
502568d5 | 1485 | struct cpu_hw_events *fake_cpuc; |
aa2bc1ad | 1486 | int ret = -EINVAL, n; |
fe9081cc | 1487 | |
cd8a38d3 SE |
1488 | fake_cpuc = allocate_fake_cpuc(); |
1489 | if (IS_ERR(fake_cpuc)) | |
1490 | return PTR_ERR(fake_cpuc); | |
1da53e02 SE |
1491 | /* |
1492 | * the event is not yet connected with its | |
1493 | * siblings therefore we must first collect | |
1494 | * existing siblings, then add the new event | |
1495 | * before we can simulate the scheduling | |
1496 | */ | |
502568d5 | 1497 | n = collect_events(fake_cpuc, leader, true); |
1da53e02 | 1498 | if (n < 0) |
cd8a38d3 | 1499 | goto out; |
fe9081cc | 1500 | |
502568d5 PZ |
1501 | fake_cpuc->n_events = n; |
1502 | n = collect_events(fake_cpuc, event, false); | |
1da53e02 | 1503 | if (n < 0) |
cd8a38d3 | 1504 | goto out; |
fe9081cc | 1505 | |
502568d5 | 1506 | fake_cpuc->n_events = n; |
1da53e02 | 1507 | |
a072738e | 1508 | ret = x86_pmu.schedule_events(fake_cpuc, n, NULL); |
502568d5 | 1509 | |
502568d5 | 1510 | out: |
cd8a38d3 | 1511 | free_fake_cpuc(fake_cpuc); |
502568d5 | 1512 | return ret; |
fe9081cc PZ |
1513 | } |
1514 | ||
dda99116 | 1515 | static int x86_pmu_event_init(struct perf_event *event) |
621a01ea | 1516 | { |
51b0fe39 | 1517 | struct pmu *tmp; |
621a01ea IM |
1518 | int err; |
1519 | ||
b0a873eb PZ |
1520 | switch (event->attr.type) { |
1521 | case PERF_TYPE_RAW: | |
1522 | case PERF_TYPE_HARDWARE: | |
1523 | case PERF_TYPE_HW_CACHE: | |
1524 | break; | |
1525 | ||
1526 | default: | |
1527 | return -ENOENT; | |
1528 | } | |
1529 | ||
1530 | err = __x86_pmu_event_init(event); | |
fe9081cc | 1531 | if (!err) { |
8113070d SE |
1532 | /* |
1533 | * we temporarily connect event to its pmu | |
1534 | * such that validate_group() can classify | |
1535 | * it as an x86 event using is_x86_event() | |
1536 | */ | |
1537 | tmp = event->pmu; | |
1538 | event->pmu = &pmu; | |
1539 | ||
fe9081cc PZ |
1540 | if (event->group_leader != event) |
1541 | err = validate_group(event); | |
ca037701 PZ |
1542 | else |
1543 | err = validate_event(event); | |
8113070d SE |
1544 | |
1545 | event->pmu = tmp; | |
fe9081cc | 1546 | } |
a1792cda | 1547 | if (err) { |
cdd6c482 IM |
1548 | if (event->destroy) |
1549 | event->destroy(event); | |
a1792cda | 1550 | } |
621a01ea | 1551 | |
b0a873eb | 1552 | return err; |
621a01ea | 1553 | } |
d7d59fb3 | 1554 | |
fe4a3308 PZ |
1555 | static int x86_pmu_event_idx(struct perf_event *event) |
1556 | { | |
1557 | int idx = event->hw.idx; | |
1558 | ||
1559 | if (x86_pmu.num_counters_fixed && idx >= X86_PMC_IDX_FIXED) { | |
1560 | idx -= X86_PMC_IDX_FIXED; | |
1561 | idx |= 1 << 30; | |
1562 | } | |
1563 | ||
1564 | return idx + 1; | |
1565 | } | |
1566 | ||
0c9d42ed PZ |
1567 | static ssize_t get_attr_rdpmc(struct device *cdev, |
1568 | struct device_attribute *attr, | |
1569 | char *buf) | |
1570 | { | |
1571 | return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc); | |
1572 | } | |
1573 | ||
1574 | static void change_rdpmc(void *info) | |
1575 | { | |
1576 | bool enable = !!(unsigned long)info; | |
1577 | ||
1578 | if (enable) | |
1579 | set_in_cr4(X86_CR4_PCE); | |
1580 | else | |
1581 | clear_in_cr4(X86_CR4_PCE); | |
1582 | } | |
1583 | ||
1584 | static ssize_t set_attr_rdpmc(struct device *cdev, | |
1585 | struct device_attribute *attr, | |
1586 | const char *buf, size_t count) | |
1587 | { | |
1588 | unsigned long val = simple_strtoul(buf, NULL, 0); | |
1589 | ||
1590 | if (!!val != !!x86_pmu.attr_rdpmc) { | |
1591 | x86_pmu.attr_rdpmc = !!val; | |
1592 | smp_call_function(change_rdpmc, (void *)val, 1); | |
1593 | } | |
1594 | ||
1595 | return count; | |
1596 | } | |
1597 | ||
1598 | static DEVICE_ATTR(rdpmc, S_IRUSR | S_IWUSR, get_attr_rdpmc, set_attr_rdpmc); | |
1599 | ||
1600 | static struct attribute *x86_pmu_attrs[] = { | |
1601 | &dev_attr_rdpmc.attr, | |
1602 | NULL, | |
1603 | }; | |
1604 | ||
1605 | static struct attribute_group x86_pmu_attr_group = { | |
1606 | .attrs = x86_pmu_attrs, | |
1607 | }; | |
1608 | ||
1609 | static const struct attribute_group *x86_pmu_attr_groups[] = { | |
1610 | &x86_pmu_attr_group, | |
1611 | NULL, | |
1612 | }; | |
1613 | ||
b0a873eb | 1614 | static struct pmu pmu = { |
a4eaf7f1 PZ |
1615 | .pmu_enable = x86_pmu_enable, |
1616 | .pmu_disable = x86_pmu_disable, | |
1617 | ||
0c9d42ed PZ |
1618 | .attr_groups = x86_pmu_attr_groups, |
1619 | ||
b0a873eb | 1620 | .event_init = x86_pmu_event_init, |
a4eaf7f1 PZ |
1621 | |
1622 | .add = x86_pmu_add, | |
1623 | .del = x86_pmu_del, | |
b0a873eb PZ |
1624 | .start = x86_pmu_start, |
1625 | .stop = x86_pmu_stop, | |
1626 | .read = x86_pmu_read, | |
a4eaf7f1 | 1627 | |
b0a873eb PZ |
1628 | .start_txn = x86_pmu_start_txn, |
1629 | .cancel_txn = x86_pmu_cancel_txn, | |
1630 | .commit_txn = x86_pmu_commit_txn, | |
fe4a3308 PZ |
1631 | |
1632 | .event_idx = x86_pmu_event_idx, | |
b0a873eb PZ |
1633 | }; |
1634 | ||
e3f3541c PZ |
1635 | void perf_update_user_clock(struct perf_event_mmap_page *userpg, u64 now) |
1636 | { | |
1637 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) | |
1638 | return; | |
1639 | ||
1640 | if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) | |
1641 | return; | |
1642 | ||
1643 | userpg->time_mult = this_cpu_read(cyc2ns); | |
1644 | userpg->time_shift = CYC2NS_SCALE_FACTOR; | |
1645 | userpg->time_offset = this_cpu_read(cyc2ns_offset) - now; | |
1646 | } | |
1647 | ||
d7d59fb3 PZ |
1648 | /* |
1649 | * callchain support | |
1650 | */ | |
1651 | ||
d7d59fb3 PZ |
1652 | static int backtrace_stack(void *data, char *name) |
1653 | { | |
038e836e | 1654 | return 0; |
d7d59fb3 PZ |
1655 | } |
1656 | ||
1657 | static void backtrace_address(void *data, unsigned long addr, int reliable) | |
1658 | { | |
1659 | struct perf_callchain_entry *entry = data; | |
1660 | ||
70791ce9 | 1661 | perf_callchain_store(entry, addr); |
d7d59fb3 PZ |
1662 | } |
1663 | ||
1664 | static const struct stacktrace_ops backtrace_ops = { | |
d7d59fb3 PZ |
1665 | .stack = backtrace_stack, |
1666 | .address = backtrace_address, | |
06d65bda | 1667 | .walk_stack = print_context_stack_bp, |
d7d59fb3 PZ |
1668 | }; |
1669 | ||
56962b44 FW |
1670 | void |
1671 | perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs) | |
d7d59fb3 | 1672 | { |
927c7a9e FW |
1673 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
1674 | /* TODO: We don't support guest os callchain now */ | |
ed805261 | 1675 | return; |
927c7a9e FW |
1676 | } |
1677 | ||
70791ce9 | 1678 | perf_callchain_store(entry, regs->ip); |
d7d59fb3 | 1679 | |
e8e999cf | 1680 | dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry); |
d7d59fb3 PZ |
1681 | } |
1682 | ||
257ef9d2 TE |
1683 | #ifdef CONFIG_COMPAT |
1684 | static inline int | |
1685 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
74193ef0 | 1686 | { |
257ef9d2 TE |
1687 | /* 32-bit process in 64-bit kernel. */ |
1688 | struct stack_frame_ia32 frame; | |
1689 | const void __user *fp; | |
74193ef0 | 1690 | |
257ef9d2 TE |
1691 | if (!test_thread_flag(TIF_IA32)) |
1692 | return 0; | |
1693 | ||
1694 | fp = compat_ptr(regs->bp); | |
1695 | while (entry->nr < PERF_MAX_STACK_DEPTH) { | |
1696 | unsigned long bytes; | |
1697 | frame.next_frame = 0; | |
1698 | frame.return_address = 0; | |
1699 | ||
1700 | bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); | |
1701 | if (bytes != sizeof(frame)) | |
1702 | break; | |
74193ef0 | 1703 | |
257ef9d2 TE |
1704 | if (fp < compat_ptr(regs->sp)) |
1705 | break; | |
74193ef0 | 1706 | |
70791ce9 | 1707 | perf_callchain_store(entry, frame.return_address); |
257ef9d2 TE |
1708 | fp = compat_ptr(frame.next_frame); |
1709 | } | |
1710 | return 1; | |
d7d59fb3 | 1711 | } |
257ef9d2 TE |
1712 | #else |
1713 | static inline int | |
1714 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
1715 | { | |
1716 | return 0; | |
1717 | } | |
1718 | #endif | |
d7d59fb3 | 1719 | |
56962b44 FW |
1720 | void |
1721 | perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs) | |
d7d59fb3 PZ |
1722 | { |
1723 | struct stack_frame frame; | |
1724 | const void __user *fp; | |
1725 | ||
927c7a9e FW |
1726 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
1727 | /* TODO: We don't support guest os callchain now */ | |
ed805261 | 1728 | return; |
927c7a9e | 1729 | } |
5a6cec3a | 1730 | |
74193ef0 | 1731 | fp = (void __user *)regs->bp; |
d7d59fb3 | 1732 | |
70791ce9 | 1733 | perf_callchain_store(entry, regs->ip); |
d7d59fb3 | 1734 | |
20afc60f AV |
1735 | if (!current->mm) |
1736 | return; | |
1737 | ||
257ef9d2 TE |
1738 | if (perf_callchain_user32(regs, entry)) |
1739 | return; | |
1740 | ||
f9188e02 | 1741 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
257ef9d2 | 1742 | unsigned long bytes; |
038e836e | 1743 | frame.next_frame = NULL; |
d7d59fb3 PZ |
1744 | frame.return_address = 0; |
1745 | ||
257ef9d2 TE |
1746 | bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); |
1747 | if (bytes != sizeof(frame)) | |
d7d59fb3 PZ |
1748 | break; |
1749 | ||
5a6cec3a | 1750 | if ((unsigned long)fp < regs->sp) |
d7d59fb3 PZ |
1751 | break; |
1752 | ||
70791ce9 | 1753 | perf_callchain_store(entry, frame.return_address); |
038e836e | 1754 | fp = frame.next_frame; |
d7d59fb3 PZ |
1755 | } |
1756 | } | |
1757 | ||
39447b38 ZY |
1758 | unsigned long perf_instruction_pointer(struct pt_regs *regs) |
1759 | { | |
1760 | unsigned long ip; | |
dcf46b94 | 1761 | |
39447b38 ZY |
1762 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) |
1763 | ip = perf_guest_cbs->get_guest_ip(); | |
1764 | else | |
1765 | ip = instruction_pointer(regs); | |
dcf46b94 | 1766 | |
39447b38 ZY |
1767 | return ip; |
1768 | } | |
1769 | ||
1770 | unsigned long perf_misc_flags(struct pt_regs *regs) | |
1771 | { | |
1772 | int misc = 0; | |
dcf46b94 | 1773 | |
39447b38 | 1774 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
dcf46b94 ZY |
1775 | if (perf_guest_cbs->is_user_mode()) |
1776 | misc |= PERF_RECORD_MISC_GUEST_USER; | |
1777 | else | |
1778 | misc |= PERF_RECORD_MISC_GUEST_KERNEL; | |
1779 | } else { | |
1780 | if (user_mode(regs)) | |
1781 | misc |= PERF_RECORD_MISC_USER; | |
1782 | else | |
1783 | misc |= PERF_RECORD_MISC_KERNEL; | |
1784 | } | |
1785 | ||
39447b38 | 1786 | if (regs->flags & PERF_EFLAGS_EXACT) |
ab608344 | 1787 | misc |= PERF_RECORD_MISC_EXACT_IP; |
39447b38 ZY |
1788 | |
1789 | return misc; | |
1790 | } | |
b3d9468a GN |
1791 | |
1792 | void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) | |
1793 | { | |
1794 | cap->version = x86_pmu.version; | |
1795 | cap->num_counters_gp = x86_pmu.num_counters; | |
1796 | cap->num_counters_fixed = x86_pmu.num_counters_fixed; | |
1797 | cap->bit_width_gp = x86_pmu.cntval_bits; | |
1798 | cap->bit_width_fixed = x86_pmu.cntval_bits; | |
1799 | cap->events_mask = (unsigned int)x86_pmu.events_maskl; | |
1800 | cap->events_mask_len = x86_pmu.events_mask_len; | |
1801 | } | |
1802 | EXPORT_SYMBOL_GPL(perf_get_x86_pmu_capability); |