]>
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> |
74193ef0 | 25 | #include <linux/highmem.h> |
30dd568c | 26 | #include <linux/cpu.h> |
272d30be | 27 | #include <linux/bitops.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> |
241771ef | 33 | |
7645a24c PZ |
34 | #if 0 |
35 | #undef wrmsrl | |
36 | #define wrmsrl(msr, val) \ | |
37 | do { \ | |
38 | trace_printk("wrmsrl(%lx, %lx)\n", (unsigned long)(msr),\ | |
39 | (unsigned long)(val)); \ | |
40 | native_write_msr((msr), (u32)((u64)(val)), \ | |
41 | (u32)((u64)(val) >> 32)); \ | |
42 | } while (0) | |
43 | #endif | |
44 | ||
ef21f683 PZ |
45 | /* |
46 | * best effort, GUP based copy_from_user() that assumes IRQ or NMI context | |
47 | */ | |
48 | static unsigned long | |
49 | copy_from_user_nmi(void *to, const void __user *from, unsigned long n) | |
50 | { | |
51 | unsigned long offset, addr = (unsigned long)from; | |
52 | int type = in_nmi() ? KM_NMI : KM_IRQ0; | |
53 | unsigned long size, len = 0; | |
54 | struct page *page; | |
55 | void *map; | |
56 | int ret; | |
57 | ||
58 | do { | |
59 | ret = __get_user_pages_fast(addr, 1, 0, &page); | |
60 | if (!ret) | |
61 | break; | |
62 | ||
63 | offset = addr & (PAGE_SIZE - 1); | |
64 | size = min(PAGE_SIZE - offset, n - len); | |
65 | ||
66 | map = kmap_atomic(page, type); | |
67 | memcpy(to, map+offset, size); | |
68 | kunmap_atomic(map, type); | |
69 | put_page(page); | |
70 | ||
71 | len += size; | |
72 | to += size; | |
73 | addr += size; | |
74 | ||
75 | } while (len < n); | |
76 | ||
77 | return len; | |
78 | } | |
79 | ||
1da53e02 | 80 | struct event_constraint { |
c91e0f5d PZ |
81 | union { |
82 | unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | |
b622d644 | 83 | u64 idxmsk64; |
c91e0f5d | 84 | }; |
b622d644 PZ |
85 | u64 code; |
86 | u64 cmask; | |
272d30be | 87 | int weight; |
1da53e02 SE |
88 | }; |
89 | ||
38331f62 SE |
90 | struct amd_nb { |
91 | int nb_id; /* NorthBridge id */ | |
92 | int refcnt; /* reference count */ | |
93 | struct perf_event *owners[X86_PMC_IDX_MAX]; | |
94 | struct event_constraint event_constraints[X86_PMC_IDX_MAX]; | |
95 | }; | |
96 | ||
caff2bef PZ |
97 | #define MAX_LBR_ENTRIES 16 |
98 | ||
cdd6c482 | 99 | struct cpu_hw_events { |
ca037701 PZ |
100 | /* |
101 | * Generic x86 PMC bits | |
102 | */ | |
1da53e02 | 103 | struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */ |
43f6201a | 104 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
b0f3f28e | 105 | int enabled; |
241771ef | 106 | |
1da53e02 SE |
107 | int n_events; |
108 | int n_added; | |
109 | int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ | |
447a194b | 110 | u64 tags[X86_PMC_IDX_MAX]; |
1da53e02 | 111 | struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ |
ca037701 PZ |
112 | |
113 | /* | |
114 | * Intel DebugStore bits | |
115 | */ | |
116 | struct debug_store *ds; | |
117 | u64 pebs_enabled; | |
118 | ||
caff2bef PZ |
119 | /* |
120 | * Intel LBR bits | |
121 | */ | |
122 | int lbr_users; | |
123 | void *lbr_context; | |
124 | struct perf_branch_stack lbr_stack; | |
125 | struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES]; | |
126 | ||
ca037701 PZ |
127 | /* |
128 | * AMD specific bits | |
129 | */ | |
38331f62 | 130 | struct amd_nb *amd_nb; |
b690081d SE |
131 | }; |
132 | ||
fce877e3 | 133 | #define __EVENT_CONSTRAINT(c, n, m, w) {\ |
b622d644 | 134 | { .idxmsk64 = (n) }, \ |
c91e0f5d PZ |
135 | .code = (c), \ |
136 | .cmask = (m), \ | |
fce877e3 | 137 | .weight = (w), \ |
c91e0f5d | 138 | } |
b690081d | 139 | |
fce877e3 PZ |
140 | #define EVENT_CONSTRAINT(c, n, m) \ |
141 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n)) | |
142 | ||
ca037701 PZ |
143 | /* |
144 | * Constraint on the Event code. | |
145 | */ | |
ed8777fc | 146 | #define INTEL_EVENT_CONSTRAINT(c, n) \ |
a098f448 | 147 | EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT) |
8433be11 | 148 | |
ca037701 PZ |
149 | /* |
150 | * Constraint on the Event code + UMask + fixed-mask | |
a098f448 RR |
151 | * |
152 | * filter mask to validate fixed counter events. | |
153 | * the following filters disqualify for fixed counters: | |
154 | * - inv | |
155 | * - edge | |
156 | * - cnt-mask | |
157 | * The other filters are supported by fixed counters. | |
158 | * The any-thread option is supported starting with v3. | |
ca037701 | 159 | */ |
ed8777fc | 160 | #define FIXED_EVENT_CONSTRAINT(c, n) \ |
a098f448 | 161 | EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK) |
8433be11 | 162 | |
ca037701 PZ |
163 | /* |
164 | * Constraint on the Event code + UMask | |
165 | */ | |
166 | #define PEBS_EVENT_CONSTRAINT(c, n) \ | |
167 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK) | |
168 | ||
ed8777fc PZ |
169 | #define EVENT_CONSTRAINT_END \ |
170 | EVENT_CONSTRAINT(0, 0, 0) | |
171 | ||
172 | #define for_each_event_constraint(e, c) \ | |
a1f2b70a | 173 | for ((e) = (c); (e)->weight; (e)++) |
b690081d | 174 | |
8db909a7 PZ |
175 | union perf_capabilities { |
176 | struct { | |
177 | u64 lbr_format : 6; | |
178 | u64 pebs_trap : 1; | |
179 | u64 pebs_arch_reg : 1; | |
180 | u64 pebs_format : 4; | |
181 | u64 smm_freeze : 1; | |
182 | }; | |
183 | u64 capabilities; | |
184 | }; | |
185 | ||
241771ef | 186 | /* |
5f4ec28f | 187 | * struct x86_pmu - generic x86 pmu |
241771ef | 188 | */ |
5f4ec28f | 189 | struct x86_pmu { |
ca037701 PZ |
190 | /* |
191 | * Generic x86 PMC bits | |
192 | */ | |
faa28ae0 RR |
193 | const char *name; |
194 | int version; | |
a3288106 | 195 | int (*handle_irq)(struct pt_regs *); |
9e35ad38 | 196 | void (*disable_all)(void); |
11164cd4 | 197 | void (*enable_all)(int added); |
aff3d91a PZ |
198 | void (*enable)(struct perf_event *); |
199 | void (*disable)(struct perf_event *); | |
b4cdc5c2 | 200 | int (*hw_config)(struct perf_event *event); |
a072738e | 201 | int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign); |
169e41eb JSR |
202 | unsigned eventsel; |
203 | unsigned perfctr; | |
b0f3f28e | 204 | u64 (*event_map)(int); |
169e41eb | 205 | int max_events; |
948b1bb8 RR |
206 | int num_counters; |
207 | int num_counters_fixed; | |
208 | int cntval_bits; | |
209 | u64 cntval_mask; | |
04da8a43 | 210 | int apic; |
c619b8ff | 211 | u64 max_period; |
63b14649 PZ |
212 | struct event_constraint * |
213 | (*get_event_constraints)(struct cpu_hw_events *cpuc, | |
214 | struct perf_event *event); | |
215 | ||
c91e0f5d PZ |
216 | void (*put_event_constraints)(struct cpu_hw_events *cpuc, |
217 | struct perf_event *event); | |
63b14649 | 218 | struct event_constraint *event_constraints; |
3c44780b | 219 | void (*quirks)(void); |
3f6da390 | 220 | |
b38b24ea | 221 | int (*cpu_prepare)(int cpu); |
3f6da390 PZ |
222 | void (*cpu_starting)(int cpu); |
223 | void (*cpu_dying)(int cpu); | |
224 | void (*cpu_dead)(int cpu); | |
ca037701 PZ |
225 | |
226 | /* | |
227 | * Intel Arch Perfmon v2+ | |
228 | */ | |
8db909a7 PZ |
229 | u64 intel_ctrl; |
230 | union perf_capabilities intel_cap; | |
ca037701 PZ |
231 | |
232 | /* | |
233 | * Intel DebugStore bits | |
234 | */ | |
235 | int bts, pebs; | |
236 | int pebs_record_size; | |
237 | void (*drain_pebs)(struct pt_regs *regs); | |
238 | struct event_constraint *pebs_constraints; | |
caff2bef PZ |
239 | |
240 | /* | |
241 | * Intel LBR | |
242 | */ | |
243 | unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */ | |
244 | int lbr_nr; /* hardware stack size */ | |
b56a3802 JSR |
245 | }; |
246 | ||
4a06bd85 | 247 | static struct x86_pmu x86_pmu __read_mostly; |
b56a3802 | 248 | |
cdd6c482 | 249 | static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = { |
b0f3f28e PZ |
250 | .enabled = 1, |
251 | }; | |
241771ef | 252 | |
07088edb | 253 | static int x86_perf_event_set_period(struct perf_event *event); |
b690081d | 254 | |
8326f44d | 255 | /* |
dfc65094 | 256 | * Generalized hw caching related hw_event table, filled |
8326f44d | 257 | * in on a per model basis. A value of 0 means |
dfc65094 IM |
258 | * 'not supported', -1 means 'hw_event makes no sense on |
259 | * this CPU', any other value means the raw hw_event | |
8326f44d IM |
260 | * ID. |
261 | */ | |
262 | ||
263 | #define C(x) PERF_COUNT_HW_CACHE_##x | |
264 | ||
265 | static u64 __read_mostly hw_cache_event_ids | |
266 | [PERF_COUNT_HW_CACHE_MAX] | |
267 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
268 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; | |
269 | ||
ee06094f | 270 | /* |
cdd6c482 IM |
271 | * Propagate event elapsed time into the generic event. |
272 | * Can only be executed on the CPU where the event is active. | |
ee06094f IM |
273 | * Returns the delta events processed. |
274 | */ | |
4b7bfd0d | 275 | static u64 |
cc2ad4ba | 276 | x86_perf_event_update(struct perf_event *event) |
ee06094f | 277 | { |
cc2ad4ba | 278 | struct hw_perf_event *hwc = &event->hw; |
948b1bb8 | 279 | int shift = 64 - x86_pmu.cntval_bits; |
ec3232bd | 280 | u64 prev_raw_count, new_raw_count; |
cc2ad4ba | 281 | int idx = hwc->idx; |
ec3232bd | 282 | s64 delta; |
ee06094f | 283 | |
30dd568c MM |
284 | if (idx == X86_PMC_IDX_FIXED_BTS) |
285 | return 0; | |
286 | ||
ee06094f | 287 | /* |
cdd6c482 | 288 | * Careful: an NMI might modify the previous event value. |
ee06094f IM |
289 | * |
290 | * Our tactic to handle this is to first atomically read and | |
291 | * exchange a new raw count - then add that new-prev delta | |
cdd6c482 | 292 | * count to the generic event atomically: |
ee06094f IM |
293 | */ |
294 | again: | |
295 | prev_raw_count = atomic64_read(&hwc->prev_count); | |
cdd6c482 | 296 | rdmsrl(hwc->event_base + idx, new_raw_count); |
ee06094f IM |
297 | |
298 | if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count, | |
299 | new_raw_count) != prev_raw_count) | |
300 | goto again; | |
301 | ||
302 | /* | |
303 | * Now we have the new raw value and have updated the prev | |
304 | * timestamp already. We can now calculate the elapsed delta | |
cdd6c482 | 305 | * (event-)time and add that to the generic event. |
ee06094f IM |
306 | * |
307 | * Careful, not all hw sign-extends above the physical width | |
ec3232bd | 308 | * of the count. |
ee06094f | 309 | */ |
ec3232bd PZ |
310 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
311 | delta >>= shift; | |
ee06094f | 312 | |
cdd6c482 | 313 | atomic64_add(delta, &event->count); |
ee06094f | 314 | atomic64_sub(delta, &hwc->period_left); |
4b7bfd0d RR |
315 | |
316 | return new_raw_count; | |
ee06094f IM |
317 | } |
318 | ||
cdd6c482 | 319 | static atomic_t active_events; |
4e935e47 PZ |
320 | static DEFINE_MUTEX(pmc_reserve_mutex); |
321 | ||
b27ea29c RR |
322 | #ifdef CONFIG_X86_LOCAL_APIC |
323 | ||
4e935e47 PZ |
324 | static bool reserve_pmc_hardware(void) |
325 | { | |
326 | int i; | |
327 | ||
328 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
329 | disable_lapic_nmi_watchdog(); | |
330 | ||
948b1bb8 | 331 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 | 332 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) |
4e935e47 PZ |
333 | goto perfctr_fail; |
334 | } | |
335 | ||
948b1bb8 | 336 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 | 337 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) |
4e935e47 PZ |
338 | goto eventsel_fail; |
339 | } | |
340 | ||
341 | return true; | |
342 | ||
343 | eventsel_fail: | |
344 | for (i--; i >= 0; i--) | |
4a06bd85 | 345 | release_evntsel_nmi(x86_pmu.eventsel + i); |
4e935e47 | 346 | |
948b1bb8 | 347 | i = x86_pmu.num_counters; |
4e935e47 PZ |
348 | |
349 | perfctr_fail: | |
350 | for (i--; i >= 0; i--) | |
4a06bd85 | 351 | release_perfctr_nmi(x86_pmu.perfctr + i); |
4e935e47 PZ |
352 | |
353 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
354 | enable_lapic_nmi_watchdog(); | |
355 | ||
356 | return false; | |
357 | } | |
358 | ||
359 | static void release_pmc_hardware(void) | |
360 | { | |
361 | int i; | |
362 | ||
948b1bb8 | 363 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 RR |
364 | release_perfctr_nmi(x86_pmu.perfctr + i); |
365 | release_evntsel_nmi(x86_pmu.eventsel + i); | |
4e935e47 PZ |
366 | } |
367 | ||
368 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
369 | enable_lapic_nmi_watchdog(); | |
370 | } | |
371 | ||
b27ea29c RR |
372 | #else |
373 | ||
374 | static bool reserve_pmc_hardware(void) { return true; } | |
375 | static void release_pmc_hardware(void) {} | |
376 | ||
377 | #endif | |
378 | ||
ca037701 PZ |
379 | static int reserve_ds_buffers(void); |
380 | static void release_ds_buffers(void); | |
30dd568c | 381 | |
cdd6c482 | 382 | static void hw_perf_event_destroy(struct perf_event *event) |
4e935e47 | 383 | { |
cdd6c482 | 384 | if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) { |
4e935e47 | 385 | release_pmc_hardware(); |
ca037701 | 386 | release_ds_buffers(); |
4e935e47 PZ |
387 | mutex_unlock(&pmc_reserve_mutex); |
388 | } | |
389 | } | |
390 | ||
85cf9dba RR |
391 | static inline int x86_pmu_initialized(void) |
392 | { | |
393 | return x86_pmu.handle_irq != NULL; | |
394 | } | |
395 | ||
8326f44d | 396 | static inline int |
cdd6c482 | 397 | set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr) |
8326f44d IM |
398 | { |
399 | unsigned int cache_type, cache_op, cache_result; | |
400 | u64 config, val; | |
401 | ||
402 | config = attr->config; | |
403 | ||
404 | cache_type = (config >> 0) & 0xff; | |
405 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) | |
406 | return -EINVAL; | |
407 | ||
408 | cache_op = (config >> 8) & 0xff; | |
409 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) | |
410 | return -EINVAL; | |
411 | ||
412 | cache_result = (config >> 16) & 0xff; | |
413 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) | |
414 | return -EINVAL; | |
415 | ||
416 | val = hw_cache_event_ids[cache_type][cache_op][cache_result]; | |
417 | ||
418 | if (val == 0) | |
419 | return -ENOENT; | |
420 | ||
421 | if (val == -1) | |
422 | return -EINVAL; | |
423 | ||
424 | hwc->config |= val; | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
c1726f34 RR |
429 | static int x86_setup_perfctr(struct perf_event *event) |
430 | { | |
431 | struct perf_event_attr *attr = &event->attr; | |
432 | struct hw_perf_event *hwc = &event->hw; | |
433 | u64 config; | |
434 | ||
435 | if (!hwc->sample_period) { | |
436 | hwc->sample_period = x86_pmu.max_period; | |
437 | hwc->last_period = hwc->sample_period; | |
438 | atomic64_set(&hwc->period_left, hwc->sample_period); | |
439 | } else { | |
440 | /* | |
441 | * If we have a PMU initialized but no APIC | |
442 | * interrupts, we cannot sample hardware | |
443 | * events (user-space has to fall back and | |
444 | * sample via a hrtimer based software event): | |
445 | */ | |
446 | if (!x86_pmu.apic) | |
447 | return -EOPNOTSUPP; | |
448 | } | |
449 | ||
450 | if (attr->type == PERF_TYPE_RAW) | |
451 | return 0; | |
452 | ||
453 | if (attr->type == PERF_TYPE_HW_CACHE) | |
454 | return set_ext_hw_attr(hwc, attr); | |
455 | ||
456 | if (attr->config >= x86_pmu.max_events) | |
457 | return -EINVAL; | |
458 | ||
459 | /* | |
460 | * The generic map: | |
461 | */ | |
462 | config = x86_pmu.event_map(attr->config); | |
463 | ||
464 | if (config == 0) | |
465 | return -ENOENT; | |
466 | ||
467 | if (config == -1LL) | |
468 | return -EINVAL; | |
469 | ||
470 | /* | |
471 | * Branch tracing: | |
472 | */ | |
473 | if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && | |
474 | (hwc->sample_period == 1)) { | |
475 | /* BTS is not supported by this architecture. */ | |
476 | if (!x86_pmu.bts) | |
477 | return -EOPNOTSUPP; | |
478 | ||
479 | /* BTS is currently only allowed for user-mode. */ | |
480 | if (!attr->exclude_kernel) | |
481 | return -EOPNOTSUPP; | |
482 | } | |
483 | ||
484 | hwc->config |= config; | |
485 | ||
486 | return 0; | |
487 | } | |
4261e0e0 | 488 | |
b4cdc5c2 | 489 | static int x86_pmu_hw_config(struct perf_event *event) |
a072738e CG |
490 | { |
491 | /* | |
492 | * Generate PMC IRQs: | |
493 | * (keep 'enabled' bit clear for now) | |
494 | */ | |
b4cdc5c2 | 495 | event->hw.config = ARCH_PERFMON_EVENTSEL_INT; |
a072738e CG |
496 | |
497 | /* | |
498 | * Count user and OS events unless requested not to | |
499 | */ | |
b4cdc5c2 PZ |
500 | if (!event->attr.exclude_user) |
501 | event->hw.config |= ARCH_PERFMON_EVENTSEL_USR; | |
502 | if (!event->attr.exclude_kernel) | |
503 | event->hw.config |= ARCH_PERFMON_EVENTSEL_OS; | |
a072738e | 504 | |
b4cdc5c2 PZ |
505 | if (event->attr.type == PERF_TYPE_RAW) |
506 | event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK; | |
a072738e | 507 | |
9d0fcba6 | 508 | return x86_setup_perfctr(event); |
a098f448 RR |
509 | } |
510 | ||
241771ef | 511 | /* |
0d48696f | 512 | * Setup the hardware configuration for a given attr_type |
241771ef | 513 | */ |
cdd6c482 | 514 | static int __hw_perf_event_init(struct perf_event *event) |
241771ef | 515 | { |
4e935e47 | 516 | int err; |
241771ef | 517 | |
85cf9dba RR |
518 | if (!x86_pmu_initialized()) |
519 | return -ENODEV; | |
241771ef | 520 | |
4e935e47 | 521 | err = 0; |
cdd6c482 | 522 | if (!atomic_inc_not_zero(&active_events)) { |
4e935e47 | 523 | mutex_lock(&pmc_reserve_mutex); |
cdd6c482 | 524 | if (atomic_read(&active_events) == 0) { |
30dd568c MM |
525 | if (!reserve_pmc_hardware()) |
526 | err = -EBUSY; | |
4b24a88b | 527 | else { |
ca037701 | 528 | err = reserve_ds_buffers(); |
4b24a88b SE |
529 | if (err) |
530 | release_pmc_hardware(); | |
531 | } | |
30dd568c MM |
532 | } |
533 | if (!err) | |
cdd6c482 | 534 | atomic_inc(&active_events); |
4e935e47 PZ |
535 | mutex_unlock(&pmc_reserve_mutex); |
536 | } | |
537 | if (err) | |
538 | return err; | |
539 | ||
cdd6c482 | 540 | event->destroy = hw_perf_event_destroy; |
a1792cda | 541 | |
4261e0e0 RR |
542 | event->hw.idx = -1; |
543 | event->hw.last_cpu = -1; | |
544 | event->hw.last_tag = ~0ULL; | |
b690081d | 545 | |
9d0fcba6 | 546 | return x86_pmu.hw_config(event); |
4261e0e0 RR |
547 | } |
548 | ||
8c48e444 | 549 | static void x86_pmu_disable_all(void) |
f87ad35d | 550 | { |
cdd6c482 | 551 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
9e35ad38 PZ |
552 | int idx; |
553 | ||
948b1bb8 | 554 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
b0f3f28e PZ |
555 | u64 val; |
556 | ||
43f6201a | 557 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 558 | continue; |
8c48e444 | 559 | rdmsrl(x86_pmu.eventsel + idx, val); |
bb1165d6 | 560 | if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE)) |
4295ee62 | 561 | continue; |
bb1165d6 | 562 | val &= ~ARCH_PERFMON_EVENTSEL_ENABLE; |
8c48e444 | 563 | wrmsrl(x86_pmu.eventsel + idx, val); |
f87ad35d | 564 | } |
f87ad35d JSR |
565 | } |
566 | ||
9e35ad38 | 567 | void hw_perf_disable(void) |
b56a3802 | 568 | { |
1da53e02 SE |
569 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
570 | ||
85cf9dba | 571 | if (!x86_pmu_initialized()) |
9e35ad38 | 572 | return; |
1da53e02 | 573 | |
1a6e21f7 PZ |
574 | if (!cpuc->enabled) |
575 | return; | |
576 | ||
577 | cpuc->n_added = 0; | |
578 | cpuc->enabled = 0; | |
579 | barrier(); | |
1da53e02 SE |
580 | |
581 | x86_pmu.disable_all(); | |
b56a3802 | 582 | } |
241771ef | 583 | |
11164cd4 | 584 | static void x86_pmu_enable_all(int added) |
f87ad35d | 585 | { |
cdd6c482 | 586 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
f87ad35d JSR |
587 | int idx; |
588 | ||
948b1bb8 | 589 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
cdd6c482 | 590 | struct perf_event *event = cpuc->events[idx]; |
4295ee62 | 591 | u64 val; |
b0f3f28e | 592 | |
43f6201a | 593 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 594 | continue; |
984b838c | 595 | |
cdd6c482 | 596 | val = event->hw.config; |
bb1165d6 | 597 | val |= ARCH_PERFMON_EVENTSEL_ENABLE; |
8c48e444 | 598 | wrmsrl(x86_pmu.eventsel + idx, val); |
f87ad35d JSR |
599 | } |
600 | } | |
601 | ||
1da53e02 SE |
602 | static const struct pmu pmu; |
603 | ||
604 | static inline int is_x86_event(struct perf_event *event) | |
605 | { | |
606 | return event->pmu == &pmu; | |
607 | } | |
608 | ||
609 | static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign) | |
610 | { | |
63b14649 | 611 | struct event_constraint *c, *constraints[X86_PMC_IDX_MAX]; |
1da53e02 | 612 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
c933c1a6 | 613 | int i, j, w, wmax, num = 0; |
1da53e02 SE |
614 | struct hw_perf_event *hwc; |
615 | ||
616 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); | |
617 | ||
618 | for (i = 0; i < n; i++) { | |
b622d644 PZ |
619 | c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]); |
620 | constraints[i] = c; | |
1da53e02 SE |
621 | } |
622 | ||
8113070d SE |
623 | /* |
624 | * fastpath, try to reuse previous register | |
625 | */ | |
c933c1a6 | 626 | for (i = 0; i < n; i++) { |
8113070d | 627 | hwc = &cpuc->event_list[i]->hw; |
81269a08 | 628 | c = constraints[i]; |
8113070d SE |
629 | |
630 | /* never assigned */ | |
631 | if (hwc->idx == -1) | |
632 | break; | |
633 | ||
634 | /* constraint still honored */ | |
63b14649 | 635 | if (!test_bit(hwc->idx, c->idxmsk)) |
8113070d SE |
636 | break; |
637 | ||
638 | /* not already used */ | |
639 | if (test_bit(hwc->idx, used_mask)) | |
640 | break; | |
641 | ||
34538ee7 | 642 | __set_bit(hwc->idx, used_mask); |
8113070d SE |
643 | if (assign) |
644 | assign[i] = hwc->idx; | |
645 | } | |
c933c1a6 | 646 | if (i == n) |
8113070d SE |
647 | goto done; |
648 | ||
649 | /* | |
650 | * begin slow path | |
651 | */ | |
652 | ||
653 | bitmap_zero(used_mask, X86_PMC_IDX_MAX); | |
654 | ||
1da53e02 SE |
655 | /* |
656 | * weight = number of possible counters | |
657 | * | |
658 | * 1 = most constrained, only works on one counter | |
659 | * wmax = least constrained, works on any counter | |
660 | * | |
661 | * assign events to counters starting with most | |
662 | * constrained events. | |
663 | */ | |
948b1bb8 | 664 | wmax = x86_pmu.num_counters; |
1da53e02 SE |
665 | |
666 | /* | |
667 | * when fixed event counters are present, | |
668 | * wmax is incremented by 1 to account | |
669 | * for one more choice | |
670 | */ | |
948b1bb8 | 671 | if (x86_pmu.num_counters_fixed) |
1da53e02 SE |
672 | wmax++; |
673 | ||
8113070d | 674 | for (w = 1, num = n; num && w <= wmax; w++) { |
1da53e02 | 675 | /* for each event */ |
8113070d | 676 | for (i = 0; num && i < n; i++) { |
81269a08 | 677 | c = constraints[i]; |
1da53e02 SE |
678 | hwc = &cpuc->event_list[i]->hw; |
679 | ||
272d30be | 680 | if (c->weight != w) |
1da53e02 SE |
681 | continue; |
682 | ||
984b3f57 | 683 | for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) { |
1da53e02 SE |
684 | if (!test_bit(j, used_mask)) |
685 | break; | |
686 | } | |
687 | ||
688 | if (j == X86_PMC_IDX_MAX) | |
689 | break; | |
1da53e02 | 690 | |
34538ee7 | 691 | __set_bit(j, used_mask); |
8113070d | 692 | |
1da53e02 SE |
693 | if (assign) |
694 | assign[i] = j; | |
695 | num--; | |
696 | } | |
697 | } | |
8113070d | 698 | done: |
1da53e02 SE |
699 | /* |
700 | * scheduling failed or is just a simulation, | |
701 | * free resources if necessary | |
702 | */ | |
703 | if (!assign || num) { | |
704 | for (i = 0; i < n; i++) { | |
705 | if (x86_pmu.put_event_constraints) | |
706 | x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]); | |
707 | } | |
708 | } | |
709 | return num ? -ENOSPC : 0; | |
710 | } | |
711 | ||
712 | /* | |
713 | * dogrp: true if must collect siblings events (group) | |
714 | * returns total number of events and error code | |
715 | */ | |
716 | static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp) | |
717 | { | |
718 | struct perf_event *event; | |
719 | int n, max_count; | |
720 | ||
948b1bb8 | 721 | max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed; |
1da53e02 SE |
722 | |
723 | /* current number of events already accepted */ | |
724 | n = cpuc->n_events; | |
725 | ||
726 | if (is_x86_event(leader)) { | |
727 | if (n >= max_count) | |
728 | return -ENOSPC; | |
729 | cpuc->event_list[n] = leader; | |
730 | n++; | |
731 | } | |
732 | if (!dogrp) | |
733 | return n; | |
734 | ||
735 | list_for_each_entry(event, &leader->sibling_list, group_entry) { | |
736 | if (!is_x86_event(event) || | |
8113070d | 737 | event->state <= PERF_EVENT_STATE_OFF) |
1da53e02 SE |
738 | continue; |
739 | ||
740 | if (n >= max_count) | |
741 | return -ENOSPC; | |
742 | ||
743 | cpuc->event_list[n] = event; | |
744 | n++; | |
745 | } | |
746 | return n; | |
747 | } | |
748 | ||
1da53e02 | 749 | static inline void x86_assign_hw_event(struct perf_event *event, |
447a194b | 750 | struct cpu_hw_events *cpuc, int i) |
1da53e02 | 751 | { |
447a194b SE |
752 | struct hw_perf_event *hwc = &event->hw; |
753 | ||
754 | hwc->idx = cpuc->assign[i]; | |
755 | hwc->last_cpu = smp_processor_id(); | |
756 | hwc->last_tag = ++cpuc->tags[i]; | |
1da53e02 SE |
757 | |
758 | if (hwc->idx == X86_PMC_IDX_FIXED_BTS) { | |
759 | hwc->config_base = 0; | |
760 | hwc->event_base = 0; | |
761 | } else if (hwc->idx >= X86_PMC_IDX_FIXED) { | |
762 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; | |
763 | /* | |
764 | * We set it so that event_base + idx in wrmsr/rdmsr maps to | |
765 | * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2: | |
766 | */ | |
767 | hwc->event_base = | |
768 | MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED; | |
769 | } else { | |
770 | hwc->config_base = x86_pmu.eventsel; | |
771 | hwc->event_base = x86_pmu.perfctr; | |
772 | } | |
773 | } | |
774 | ||
447a194b SE |
775 | static inline int match_prev_assignment(struct hw_perf_event *hwc, |
776 | struct cpu_hw_events *cpuc, | |
777 | int i) | |
778 | { | |
779 | return hwc->idx == cpuc->assign[i] && | |
780 | hwc->last_cpu == smp_processor_id() && | |
781 | hwc->last_tag == cpuc->tags[i]; | |
782 | } | |
783 | ||
c08053e6 | 784 | static int x86_pmu_start(struct perf_event *event); |
d76a0812 | 785 | static void x86_pmu_stop(struct perf_event *event); |
2e841873 | 786 | |
9e35ad38 | 787 | void hw_perf_enable(void) |
ee06094f | 788 | { |
1da53e02 SE |
789 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
790 | struct perf_event *event; | |
791 | struct hw_perf_event *hwc; | |
11164cd4 | 792 | int i, added = cpuc->n_added; |
1da53e02 | 793 | |
85cf9dba | 794 | if (!x86_pmu_initialized()) |
2b9ff0db | 795 | return; |
1a6e21f7 PZ |
796 | |
797 | if (cpuc->enabled) | |
798 | return; | |
799 | ||
1da53e02 | 800 | if (cpuc->n_added) { |
19925ce7 | 801 | int n_running = cpuc->n_events - cpuc->n_added; |
1da53e02 SE |
802 | /* |
803 | * apply assignment obtained either from | |
804 | * hw_perf_group_sched_in() or x86_pmu_enable() | |
805 | * | |
806 | * step1: save events moving to new counters | |
807 | * step2: reprogram moved events into new counters | |
808 | */ | |
19925ce7 | 809 | for (i = 0; i < n_running; i++) { |
1da53e02 SE |
810 | event = cpuc->event_list[i]; |
811 | hwc = &event->hw; | |
812 | ||
447a194b SE |
813 | /* |
814 | * we can avoid reprogramming counter if: | |
815 | * - assigned same counter as last time | |
816 | * - running on same CPU as last time | |
817 | * - no other event has used the counter since | |
818 | */ | |
819 | if (hwc->idx == -1 || | |
820 | match_prev_assignment(hwc, cpuc, i)) | |
1da53e02 SE |
821 | continue; |
822 | ||
d76a0812 | 823 | x86_pmu_stop(event); |
1da53e02 SE |
824 | } |
825 | ||
826 | for (i = 0; i < cpuc->n_events; i++) { | |
1da53e02 SE |
827 | event = cpuc->event_list[i]; |
828 | hwc = &event->hw; | |
829 | ||
45e16a68 | 830 | if (!match_prev_assignment(hwc, cpuc, i)) |
447a194b | 831 | x86_assign_hw_event(event, cpuc, i); |
45e16a68 PZ |
832 | else if (i < n_running) |
833 | continue; | |
1da53e02 | 834 | |
c08053e6 | 835 | x86_pmu_start(event); |
1da53e02 SE |
836 | } |
837 | cpuc->n_added = 0; | |
838 | perf_events_lapic_init(); | |
839 | } | |
1a6e21f7 PZ |
840 | |
841 | cpuc->enabled = 1; | |
842 | barrier(); | |
843 | ||
11164cd4 | 844 | x86_pmu.enable_all(added); |
ee06094f | 845 | } |
ee06094f | 846 | |
31fa58af RR |
847 | static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, |
848 | u64 enable_mask) | |
b0f3f28e | 849 | { |
31fa58af | 850 | wrmsrl(hwc->config_base + hwc->idx, hwc->config | enable_mask); |
b0f3f28e PZ |
851 | } |
852 | ||
aff3d91a | 853 | static inline void x86_pmu_disable_event(struct perf_event *event) |
b0f3f28e | 854 | { |
aff3d91a | 855 | struct hw_perf_event *hwc = &event->hw; |
7645a24c PZ |
856 | |
857 | wrmsrl(hwc->config_base + hwc->idx, hwc->config); | |
b0f3f28e PZ |
858 | } |
859 | ||
245b2e70 | 860 | static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left); |
241771ef | 861 | |
ee06094f IM |
862 | /* |
863 | * Set the next IRQ period, based on the hwc->period_left value. | |
cdd6c482 | 864 | * To be called with the event disabled in hw: |
ee06094f | 865 | */ |
e4abb5d4 | 866 | static int |
07088edb | 867 | x86_perf_event_set_period(struct perf_event *event) |
241771ef | 868 | { |
07088edb | 869 | struct hw_perf_event *hwc = &event->hw; |
2f18d1e8 | 870 | s64 left = atomic64_read(&hwc->period_left); |
e4abb5d4 | 871 | s64 period = hwc->sample_period; |
7645a24c | 872 | int ret = 0, idx = hwc->idx; |
ee06094f | 873 | |
30dd568c MM |
874 | if (idx == X86_PMC_IDX_FIXED_BTS) |
875 | return 0; | |
876 | ||
ee06094f | 877 | /* |
af901ca1 | 878 | * If we are way outside a reasonable range then just skip forward: |
ee06094f IM |
879 | */ |
880 | if (unlikely(left <= -period)) { | |
881 | left = period; | |
882 | atomic64_set(&hwc->period_left, left); | |
9e350de3 | 883 | hwc->last_period = period; |
e4abb5d4 | 884 | ret = 1; |
ee06094f IM |
885 | } |
886 | ||
887 | if (unlikely(left <= 0)) { | |
888 | left += period; | |
889 | atomic64_set(&hwc->period_left, left); | |
9e350de3 | 890 | hwc->last_period = period; |
e4abb5d4 | 891 | ret = 1; |
ee06094f | 892 | } |
1c80f4b5 | 893 | /* |
dfc65094 | 894 | * Quirk: certain CPUs dont like it if just 1 hw_event is left: |
1c80f4b5 IM |
895 | */ |
896 | if (unlikely(left < 2)) | |
897 | left = 2; | |
241771ef | 898 | |
e4abb5d4 PZ |
899 | if (left > x86_pmu.max_period) |
900 | left = x86_pmu.max_period; | |
901 | ||
245b2e70 | 902 | per_cpu(pmc_prev_left[idx], smp_processor_id()) = left; |
ee06094f IM |
903 | |
904 | /* | |
cdd6c482 | 905 | * The hw event starts counting from this event offset, |
ee06094f IM |
906 | * mark it to be able to extra future deltas: |
907 | */ | |
2f18d1e8 | 908 | atomic64_set(&hwc->prev_count, (u64)-left); |
ee06094f | 909 | |
7645a24c | 910 | wrmsrl(hwc->event_base + idx, |
948b1bb8 | 911 | (u64)(-left) & x86_pmu.cntval_mask); |
e4abb5d4 | 912 | |
cdd6c482 | 913 | perf_event_update_userpage(event); |
194002b2 | 914 | |
e4abb5d4 | 915 | return ret; |
2f18d1e8 IM |
916 | } |
917 | ||
aff3d91a | 918 | static void x86_pmu_enable_event(struct perf_event *event) |
7c90cc45 | 919 | { |
cdd6c482 | 920 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
7c90cc45 | 921 | if (cpuc->enabled) |
31fa58af RR |
922 | __x86_pmu_enable_event(&event->hw, |
923 | ARCH_PERFMON_EVENTSEL_ENABLE); | |
241771ef IM |
924 | } |
925 | ||
b690081d | 926 | /* |
1da53e02 SE |
927 | * activate a single event |
928 | * | |
929 | * The event is added to the group of enabled events | |
930 | * but only if it can be scehduled with existing events. | |
931 | * | |
932 | * Called with PMU disabled. If successful and return value 1, | |
933 | * then guaranteed to call perf_enable() and hw_perf_enable() | |
fe9081cc PZ |
934 | */ |
935 | static int x86_pmu_enable(struct perf_event *event) | |
936 | { | |
937 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | |
1da53e02 SE |
938 | struct hw_perf_event *hwc; |
939 | int assign[X86_PMC_IDX_MAX]; | |
940 | int n, n0, ret; | |
fe9081cc | 941 | |
1da53e02 | 942 | hwc = &event->hw; |
fe9081cc | 943 | |
1da53e02 SE |
944 | n0 = cpuc->n_events; |
945 | n = collect_events(cpuc, event, false); | |
946 | if (n < 0) | |
947 | return n; | |
53b441a5 | 948 | |
a072738e | 949 | ret = x86_pmu.schedule_events(cpuc, n, assign); |
1da53e02 SE |
950 | if (ret) |
951 | return ret; | |
952 | /* | |
953 | * copy new assignment, now we know it is possible | |
954 | * will be used by hw_perf_enable() | |
955 | */ | |
956 | memcpy(cpuc->assign, assign, n*sizeof(int)); | |
7e2ae347 | 957 | |
1da53e02 | 958 | cpuc->n_events = n; |
356e1f2e | 959 | cpuc->n_added += n - n0; |
95cdd2e7 IM |
960 | |
961 | return 0; | |
241771ef IM |
962 | } |
963 | ||
d76a0812 SE |
964 | static int x86_pmu_start(struct perf_event *event) |
965 | { | |
c08053e6 PZ |
966 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
967 | int idx = event->hw.idx; | |
968 | ||
969 | if (idx == -1) | |
d76a0812 SE |
970 | return -EAGAIN; |
971 | ||
07088edb | 972 | x86_perf_event_set_period(event); |
c08053e6 PZ |
973 | cpuc->events[idx] = event; |
974 | __set_bit(idx, cpuc->active_mask); | |
aff3d91a | 975 | x86_pmu.enable(event); |
c08053e6 | 976 | perf_event_update_userpage(event); |
d76a0812 SE |
977 | |
978 | return 0; | |
979 | } | |
980 | ||
cdd6c482 | 981 | static void x86_pmu_unthrottle(struct perf_event *event) |
a78ac325 | 982 | { |
71e2d282 PZ |
983 | int ret = x86_pmu_start(event); |
984 | WARN_ON_ONCE(ret); | |
a78ac325 PZ |
985 | } |
986 | ||
cdd6c482 | 987 | void perf_event_print_debug(void) |
241771ef | 988 | { |
2f18d1e8 | 989 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
ca037701 | 990 | u64 pebs; |
cdd6c482 | 991 | struct cpu_hw_events *cpuc; |
5bb9efe3 | 992 | unsigned long flags; |
1e125676 IM |
993 | int cpu, idx; |
994 | ||
948b1bb8 | 995 | if (!x86_pmu.num_counters) |
1e125676 | 996 | return; |
241771ef | 997 | |
5bb9efe3 | 998 | local_irq_save(flags); |
241771ef IM |
999 | |
1000 | cpu = smp_processor_id(); | |
cdd6c482 | 1001 | cpuc = &per_cpu(cpu_hw_events, cpu); |
241771ef | 1002 | |
faa28ae0 | 1003 | if (x86_pmu.version >= 2) { |
a1ef58f4 JSR |
1004 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
1005 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); | |
1006 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); | |
1007 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); | |
ca037701 | 1008 | rdmsrl(MSR_IA32_PEBS_ENABLE, pebs); |
a1ef58f4 JSR |
1009 | |
1010 | pr_info("\n"); | |
1011 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); | |
1012 | pr_info("CPU#%d: status: %016llx\n", cpu, status); | |
1013 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); | |
1014 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); | |
ca037701 | 1015 | pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs); |
f87ad35d | 1016 | } |
7645a24c | 1017 | pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask); |
241771ef | 1018 | |
948b1bb8 | 1019 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
4a06bd85 RR |
1020 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); |
1021 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); | |
241771ef | 1022 | |
245b2e70 | 1023 | prev_left = per_cpu(pmc_prev_left[idx], cpu); |
241771ef | 1024 | |
a1ef58f4 | 1025 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
241771ef | 1026 | cpu, idx, pmc_ctrl); |
a1ef58f4 | 1027 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
241771ef | 1028 | cpu, idx, pmc_count); |
a1ef58f4 | 1029 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
ee06094f | 1030 | cpu, idx, prev_left); |
241771ef | 1031 | } |
948b1bb8 | 1032 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { |
2f18d1e8 IM |
1033 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
1034 | ||
a1ef58f4 | 1035 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
2f18d1e8 IM |
1036 | cpu, idx, pmc_count); |
1037 | } | |
5bb9efe3 | 1038 | local_irq_restore(flags); |
241771ef IM |
1039 | } |
1040 | ||
d76a0812 | 1041 | static void x86_pmu_stop(struct perf_event *event) |
241771ef | 1042 | { |
d76a0812 | 1043 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
cdd6c482 | 1044 | struct hw_perf_event *hwc = &event->hw; |
2e841873 | 1045 | int idx = hwc->idx; |
241771ef | 1046 | |
71e2d282 PZ |
1047 | if (!__test_and_clear_bit(idx, cpuc->active_mask)) |
1048 | return; | |
1049 | ||
aff3d91a | 1050 | x86_pmu.disable(event); |
241771ef | 1051 | |
ee06094f | 1052 | /* |
cdd6c482 | 1053 | * Drain the remaining delta count out of a event |
ee06094f IM |
1054 | * that we are disabling: |
1055 | */ | |
cc2ad4ba | 1056 | x86_perf_event_update(event); |
30dd568c | 1057 | |
cdd6c482 | 1058 | cpuc->events[idx] = NULL; |
2e841873 PZ |
1059 | } |
1060 | ||
1061 | static void x86_pmu_disable(struct perf_event *event) | |
1062 | { | |
1063 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); | |
1064 | int i; | |
1065 | ||
d76a0812 | 1066 | x86_pmu_stop(event); |
194002b2 | 1067 | |
1da53e02 SE |
1068 | for (i = 0; i < cpuc->n_events; i++) { |
1069 | if (event == cpuc->event_list[i]) { | |
1070 | ||
1071 | if (x86_pmu.put_event_constraints) | |
1072 | x86_pmu.put_event_constraints(cpuc, event); | |
1073 | ||
1074 | while (++i < cpuc->n_events) | |
1075 | cpuc->event_list[i-1] = cpuc->event_list[i]; | |
1076 | ||
1077 | --cpuc->n_events; | |
6c9687ab | 1078 | break; |
1da53e02 SE |
1079 | } |
1080 | } | |
cdd6c482 | 1081 | perf_event_update_userpage(event); |
241771ef IM |
1082 | } |
1083 | ||
8c48e444 | 1084 | static int x86_pmu_handle_irq(struct pt_regs *regs) |
a29aa8a7 | 1085 | { |
df1a132b | 1086 | struct perf_sample_data data; |
cdd6c482 IM |
1087 | struct cpu_hw_events *cpuc; |
1088 | struct perf_event *event; | |
1089 | struct hw_perf_event *hwc; | |
11d1578f | 1090 | int idx, handled = 0; |
9029a5e3 IM |
1091 | u64 val; |
1092 | ||
dc1d628a | 1093 | perf_sample_data_init(&data, 0); |
df1a132b | 1094 | |
cdd6c482 | 1095 | cpuc = &__get_cpu_var(cpu_hw_events); |
962bf7a6 | 1096 | |
948b1bb8 | 1097 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
43f6201a | 1098 | if (!test_bit(idx, cpuc->active_mask)) |
a29aa8a7 | 1099 | continue; |
962bf7a6 | 1100 | |
cdd6c482 IM |
1101 | event = cpuc->events[idx]; |
1102 | hwc = &event->hw; | |
a4016a79 | 1103 | |
cc2ad4ba | 1104 | val = x86_perf_event_update(event); |
948b1bb8 | 1105 | if (val & (1ULL << (x86_pmu.cntval_bits - 1))) |
48e22d56 | 1106 | continue; |
962bf7a6 | 1107 | |
9e350de3 | 1108 | /* |
cdd6c482 | 1109 | * event overflow |
9e350de3 PZ |
1110 | */ |
1111 | handled = 1; | |
cdd6c482 | 1112 | data.period = event->hw.last_period; |
9e350de3 | 1113 | |
07088edb | 1114 | if (!x86_perf_event_set_period(event)) |
e4abb5d4 PZ |
1115 | continue; |
1116 | ||
cdd6c482 | 1117 | if (perf_event_overflow(event, 1, &data, regs)) |
71e2d282 | 1118 | x86_pmu_stop(event); |
a29aa8a7 | 1119 | } |
962bf7a6 | 1120 | |
9e350de3 PZ |
1121 | if (handled) |
1122 | inc_irq_stat(apic_perf_irqs); | |
1123 | ||
a29aa8a7 RR |
1124 | return handled; |
1125 | } | |
39d81eab | 1126 | |
b6276f35 PZ |
1127 | void smp_perf_pending_interrupt(struct pt_regs *regs) |
1128 | { | |
1129 | irq_enter(); | |
1130 | ack_APIC_irq(); | |
1131 | inc_irq_stat(apic_pending_irqs); | |
cdd6c482 | 1132 | perf_event_do_pending(); |
b6276f35 PZ |
1133 | irq_exit(); |
1134 | } | |
1135 | ||
cdd6c482 | 1136 | void set_perf_event_pending(void) |
b6276f35 | 1137 | { |
04da8a43 | 1138 | #ifdef CONFIG_X86_LOCAL_APIC |
7d428966 PZ |
1139 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
1140 | return; | |
1141 | ||
b6276f35 | 1142 | apic->send_IPI_self(LOCAL_PENDING_VECTOR); |
04da8a43 | 1143 | #endif |
b6276f35 PZ |
1144 | } |
1145 | ||
cdd6c482 | 1146 | void perf_events_lapic_init(void) |
241771ef | 1147 | { |
04da8a43 | 1148 | if (!x86_pmu.apic || !x86_pmu_initialized()) |
241771ef | 1149 | return; |
85cf9dba | 1150 | |
241771ef | 1151 | /* |
c323d95f | 1152 | * Always use NMI for PMU |
241771ef | 1153 | */ |
c323d95f | 1154 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
241771ef IM |
1155 | } |
1156 | ||
1157 | static int __kprobes | |
cdd6c482 | 1158 | perf_event_nmi_handler(struct notifier_block *self, |
241771ef IM |
1159 | unsigned long cmd, void *__args) |
1160 | { | |
1161 | struct die_args *args = __args; | |
1162 | struct pt_regs *regs; | |
b0f3f28e | 1163 | |
cdd6c482 | 1164 | if (!atomic_read(&active_events)) |
63a809a2 PZ |
1165 | return NOTIFY_DONE; |
1166 | ||
b0f3f28e PZ |
1167 | switch (cmd) { |
1168 | case DIE_NMI: | |
1169 | case DIE_NMI_IPI: | |
1170 | break; | |
241771ef | 1171 | |
b0f3f28e | 1172 | default: |
241771ef | 1173 | return NOTIFY_DONE; |
b0f3f28e | 1174 | } |
241771ef IM |
1175 | |
1176 | regs = args->regs; | |
1177 | ||
1178 | apic_write(APIC_LVTPC, APIC_DM_NMI); | |
a4016a79 PZ |
1179 | /* |
1180 | * Can't rely on the handled return value to say it was our NMI, two | |
cdd6c482 | 1181 | * events could trigger 'simultaneously' raising two back-to-back NMIs. |
a4016a79 PZ |
1182 | * |
1183 | * If the first NMI handles both, the latter will be empty and daze | |
1184 | * the CPU. | |
1185 | */ | |
a3288106 | 1186 | x86_pmu.handle_irq(regs); |
241771ef | 1187 | |
a4016a79 | 1188 | return NOTIFY_STOP; |
241771ef IM |
1189 | } |
1190 | ||
f22f54f4 PZ |
1191 | static __read_mostly struct notifier_block perf_event_nmi_notifier = { |
1192 | .notifier_call = perf_event_nmi_handler, | |
1193 | .next = NULL, | |
1194 | .priority = 1 | |
1195 | }; | |
1196 | ||
63b14649 | 1197 | static struct event_constraint unconstrained; |
38331f62 | 1198 | static struct event_constraint emptyconstraint; |
63b14649 | 1199 | |
63b14649 | 1200 | static struct event_constraint * |
f22f54f4 | 1201 | x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event) |
1da53e02 | 1202 | { |
63b14649 | 1203 | struct event_constraint *c; |
1da53e02 | 1204 | |
1da53e02 SE |
1205 | if (x86_pmu.event_constraints) { |
1206 | for_each_event_constraint(c, x86_pmu.event_constraints) { | |
63b14649 PZ |
1207 | if ((event->hw.config & c->cmask) == c->code) |
1208 | return c; | |
1da53e02 SE |
1209 | } |
1210 | } | |
63b14649 PZ |
1211 | |
1212 | return &unconstrained; | |
1da53e02 SE |
1213 | } |
1214 | ||
1da53e02 | 1215 | static int x86_event_sched_in(struct perf_event *event, |
6e37738a | 1216 | struct perf_cpu_context *cpuctx) |
1da53e02 SE |
1217 | { |
1218 | int ret = 0; | |
1219 | ||
1220 | event->state = PERF_EVENT_STATE_ACTIVE; | |
6e37738a | 1221 | event->oncpu = smp_processor_id(); |
1da53e02 SE |
1222 | event->tstamp_running += event->ctx->time - event->tstamp_stopped; |
1223 | ||
1224 | if (!is_x86_event(event)) | |
1225 | ret = event->pmu->enable(event); | |
1226 | ||
1227 | if (!ret && !is_software_event(event)) | |
1228 | cpuctx->active_oncpu++; | |
1229 | ||
1230 | if (!ret && event->attr.exclusive) | |
1231 | cpuctx->exclusive = 1; | |
1232 | ||
1233 | return ret; | |
1234 | } | |
1235 | ||
1236 | static void x86_event_sched_out(struct perf_event *event, | |
6e37738a | 1237 | struct perf_cpu_context *cpuctx) |
1da53e02 SE |
1238 | { |
1239 | event->state = PERF_EVENT_STATE_INACTIVE; | |
1240 | event->oncpu = -1; | |
1241 | ||
1242 | if (!is_x86_event(event)) | |
1243 | event->pmu->disable(event); | |
1244 | ||
1245 | event->tstamp_running -= event->ctx->time - event->tstamp_stopped; | |
1246 | ||
1247 | if (!is_software_event(event)) | |
1248 | cpuctx->active_oncpu--; | |
1249 | ||
1250 | if (event->attr.exclusive || !cpuctx->active_oncpu) | |
1251 | cpuctx->exclusive = 0; | |
1252 | } | |
1253 | ||
1254 | /* | |
1255 | * Called to enable a whole group of events. | |
1256 | * Returns 1 if the group was enabled, or -EAGAIN if it could not be. | |
1257 | * Assumes the caller has disabled interrupts and has | |
1258 | * frozen the PMU with hw_perf_save_disable. | |
1259 | * | |
1260 | * called with PMU disabled. If successful and return value 1, | |
1261 | * then guaranteed to call perf_enable() and hw_perf_enable() | |
1262 | */ | |
1263 | int hw_perf_group_sched_in(struct perf_event *leader, | |
1264 | struct perf_cpu_context *cpuctx, | |
6e37738a | 1265 | struct perf_event_context *ctx) |
1da53e02 | 1266 | { |
6e37738a | 1267 | struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events); |
1da53e02 SE |
1268 | struct perf_event *sub; |
1269 | int assign[X86_PMC_IDX_MAX]; | |
1270 | int n0, n1, ret; | |
1271 | ||
0b861225 CG |
1272 | if (!x86_pmu_initialized()) |
1273 | return 0; | |
1274 | ||
1da53e02 SE |
1275 | /* n0 = total number of events */ |
1276 | n0 = collect_events(cpuc, leader, true); | |
1277 | if (n0 < 0) | |
1278 | return n0; | |
1279 | ||
a072738e | 1280 | ret = x86_pmu.schedule_events(cpuc, n0, assign); |
1da53e02 SE |
1281 | if (ret) |
1282 | return ret; | |
1283 | ||
6e37738a | 1284 | ret = x86_event_sched_in(leader, cpuctx); |
1da53e02 SE |
1285 | if (ret) |
1286 | return ret; | |
1287 | ||
1288 | n1 = 1; | |
1289 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { | |
8113070d | 1290 | if (sub->state > PERF_EVENT_STATE_OFF) { |
6e37738a | 1291 | ret = x86_event_sched_in(sub, cpuctx); |
1da53e02 SE |
1292 | if (ret) |
1293 | goto undo; | |
1294 | ++n1; | |
1295 | } | |
1296 | } | |
1297 | /* | |
1298 | * copy new assignment, now we know it is possible | |
1299 | * will be used by hw_perf_enable() | |
1300 | */ | |
1301 | memcpy(cpuc->assign, assign, n0*sizeof(int)); | |
1302 | ||
1303 | cpuc->n_events = n0; | |
356e1f2e | 1304 | cpuc->n_added += n1; |
1da53e02 SE |
1305 | ctx->nr_active += n1; |
1306 | ||
1307 | /* | |
1308 | * 1 means successful and events are active | |
1309 | * This is not quite true because we defer | |
1310 | * actual activation until hw_perf_enable() but | |
1311 | * this way we* ensure caller won't try to enable | |
1312 | * individual events | |
1313 | */ | |
1314 | return 1; | |
1315 | undo: | |
6e37738a | 1316 | x86_event_sched_out(leader, cpuctx); |
1da53e02 SE |
1317 | n0 = 1; |
1318 | list_for_each_entry(sub, &leader->sibling_list, group_entry) { | |
1319 | if (sub->state == PERF_EVENT_STATE_ACTIVE) { | |
6e37738a | 1320 | x86_event_sched_out(sub, cpuctx); |
1da53e02 SE |
1321 | if (++n0 == n1) |
1322 | break; | |
1323 | } | |
1324 | } | |
1325 | return ret; | |
1326 | } | |
1327 | ||
f22f54f4 PZ |
1328 | #include "perf_event_amd.c" |
1329 | #include "perf_event_p6.c" | |
a072738e | 1330 | #include "perf_event_p4.c" |
caff2bef | 1331 | #include "perf_event_intel_lbr.c" |
ca037701 | 1332 | #include "perf_event_intel_ds.c" |
f22f54f4 | 1333 | #include "perf_event_intel.c" |
f87ad35d | 1334 | |
3f6da390 PZ |
1335 | static int __cpuinit |
1336 | x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu) | |
1337 | { | |
1338 | unsigned int cpu = (long)hcpu; | |
b38b24ea | 1339 | int ret = NOTIFY_OK; |
3f6da390 PZ |
1340 | |
1341 | switch (action & ~CPU_TASKS_FROZEN) { | |
1342 | case CPU_UP_PREPARE: | |
1343 | if (x86_pmu.cpu_prepare) | |
b38b24ea | 1344 | ret = x86_pmu.cpu_prepare(cpu); |
3f6da390 PZ |
1345 | break; |
1346 | ||
1347 | case CPU_STARTING: | |
1348 | if (x86_pmu.cpu_starting) | |
1349 | x86_pmu.cpu_starting(cpu); | |
1350 | break; | |
1351 | ||
1352 | case CPU_DYING: | |
1353 | if (x86_pmu.cpu_dying) | |
1354 | x86_pmu.cpu_dying(cpu); | |
1355 | break; | |
1356 | ||
b38b24ea | 1357 | case CPU_UP_CANCELED: |
3f6da390 PZ |
1358 | case CPU_DEAD: |
1359 | if (x86_pmu.cpu_dead) | |
1360 | x86_pmu.cpu_dead(cpu); | |
1361 | break; | |
1362 | ||
1363 | default: | |
1364 | break; | |
1365 | } | |
1366 | ||
b38b24ea | 1367 | return ret; |
3f6da390 PZ |
1368 | } |
1369 | ||
12558038 CG |
1370 | static void __init pmu_check_apic(void) |
1371 | { | |
1372 | if (cpu_has_apic) | |
1373 | return; | |
1374 | ||
1375 | x86_pmu.apic = 0; | |
1376 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); | |
1377 | pr_info("no hardware sampling interrupt available.\n"); | |
1378 | } | |
1379 | ||
cdd6c482 | 1380 | void __init init_hw_perf_events(void) |
b56a3802 | 1381 | { |
b622d644 | 1382 | struct event_constraint *c; |
72eae04d RR |
1383 | int err; |
1384 | ||
cdd6c482 | 1385 | pr_info("Performance Events: "); |
1123e3ad | 1386 | |
b56a3802 JSR |
1387 | switch (boot_cpu_data.x86_vendor) { |
1388 | case X86_VENDOR_INTEL: | |
72eae04d | 1389 | err = intel_pmu_init(); |
b56a3802 | 1390 | break; |
f87ad35d | 1391 | case X86_VENDOR_AMD: |
72eae04d | 1392 | err = amd_pmu_init(); |
f87ad35d | 1393 | break; |
4138960a RR |
1394 | default: |
1395 | return; | |
b56a3802 | 1396 | } |
1123e3ad | 1397 | if (err != 0) { |
cdd6c482 | 1398 | pr_cont("no PMU driver, software events only.\n"); |
b56a3802 | 1399 | return; |
1123e3ad | 1400 | } |
b56a3802 | 1401 | |
12558038 CG |
1402 | pmu_check_apic(); |
1403 | ||
1123e3ad | 1404 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
faa28ae0 | 1405 | |
3c44780b PZ |
1406 | if (x86_pmu.quirks) |
1407 | x86_pmu.quirks(); | |
1408 | ||
948b1bb8 | 1409 | if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) { |
cdd6c482 | 1410 | WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!", |
948b1bb8 RR |
1411 | x86_pmu.num_counters, X86_PMC_MAX_GENERIC); |
1412 | x86_pmu.num_counters = X86_PMC_MAX_GENERIC; | |
241771ef | 1413 | } |
948b1bb8 RR |
1414 | x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1; |
1415 | perf_max_events = x86_pmu.num_counters; | |
241771ef | 1416 | |
948b1bb8 | 1417 | if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) { |
cdd6c482 | 1418 | WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!", |
948b1bb8 RR |
1419 | x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED); |
1420 | x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED; | |
703e937c | 1421 | } |
862a1a5f | 1422 | |
d6dc0b4e | 1423 | x86_pmu.intel_ctrl |= |
948b1bb8 | 1424 | ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED; |
241771ef | 1425 | |
cdd6c482 IM |
1426 | perf_events_lapic_init(); |
1427 | register_die_notifier(&perf_event_nmi_notifier); | |
1123e3ad | 1428 | |
63b14649 | 1429 | unconstrained = (struct event_constraint) |
948b1bb8 RR |
1430 | __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1, |
1431 | 0, x86_pmu.num_counters); | |
63b14649 | 1432 | |
b622d644 PZ |
1433 | if (x86_pmu.event_constraints) { |
1434 | for_each_event_constraint(c, x86_pmu.event_constraints) { | |
a098f448 | 1435 | if (c->cmask != X86_RAW_EVENT_MASK) |
b622d644 PZ |
1436 | continue; |
1437 | ||
948b1bb8 RR |
1438 | c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1; |
1439 | c->weight += x86_pmu.num_counters; | |
b622d644 PZ |
1440 | } |
1441 | } | |
1442 | ||
57c0c15b | 1443 | pr_info("... version: %d\n", x86_pmu.version); |
948b1bb8 RR |
1444 | pr_info("... bit width: %d\n", x86_pmu.cntval_bits); |
1445 | pr_info("... generic registers: %d\n", x86_pmu.num_counters); | |
1446 | pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask); | |
57c0c15b | 1447 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); |
948b1bb8 | 1448 | pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed); |
d6dc0b4e | 1449 | pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl); |
3f6da390 PZ |
1450 | |
1451 | perf_cpu_notifier(x86_pmu_notifier); | |
241771ef | 1452 | } |
621a01ea | 1453 | |
cdd6c482 | 1454 | static inline void x86_pmu_read(struct perf_event *event) |
ee06094f | 1455 | { |
cc2ad4ba | 1456 | x86_perf_event_update(event); |
ee06094f IM |
1457 | } |
1458 | ||
4aeb0b42 RR |
1459 | static const struct pmu pmu = { |
1460 | .enable = x86_pmu_enable, | |
1461 | .disable = x86_pmu_disable, | |
d76a0812 SE |
1462 | .start = x86_pmu_start, |
1463 | .stop = x86_pmu_stop, | |
4aeb0b42 | 1464 | .read = x86_pmu_read, |
a78ac325 | 1465 | .unthrottle = x86_pmu_unthrottle, |
621a01ea IM |
1466 | }; |
1467 | ||
ca037701 PZ |
1468 | /* |
1469 | * validate that we can schedule this event | |
1470 | */ | |
1471 | static int validate_event(struct perf_event *event) | |
1472 | { | |
1473 | struct cpu_hw_events *fake_cpuc; | |
1474 | struct event_constraint *c; | |
1475 | int ret = 0; | |
1476 | ||
1477 | fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO); | |
1478 | if (!fake_cpuc) | |
1479 | return -ENOMEM; | |
1480 | ||
1481 | c = x86_pmu.get_event_constraints(fake_cpuc, event); | |
1482 | ||
1483 | if (!c || !c->weight) | |
1484 | ret = -ENOSPC; | |
1485 | ||
1486 | if (x86_pmu.put_event_constraints) | |
1487 | x86_pmu.put_event_constraints(fake_cpuc, event); | |
1488 | ||
1489 | kfree(fake_cpuc); | |
1490 | ||
1491 | return ret; | |
1492 | } | |
1493 | ||
1da53e02 SE |
1494 | /* |
1495 | * validate a single event group | |
1496 | * | |
1497 | * validation include: | |
184f412c IM |
1498 | * - check events are compatible which each other |
1499 | * - events do not compete for the same counter | |
1500 | * - number of events <= number of counters | |
1da53e02 SE |
1501 | * |
1502 | * validation ensures the group can be loaded onto the | |
1503 | * PMU if it was the only group available. | |
1504 | */ | |
fe9081cc PZ |
1505 | static int validate_group(struct perf_event *event) |
1506 | { | |
1da53e02 | 1507 | struct perf_event *leader = event->group_leader; |
502568d5 PZ |
1508 | struct cpu_hw_events *fake_cpuc; |
1509 | int ret, n; | |
fe9081cc | 1510 | |
502568d5 PZ |
1511 | ret = -ENOMEM; |
1512 | fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO); | |
1513 | if (!fake_cpuc) | |
1514 | goto out; | |
fe9081cc | 1515 | |
1da53e02 SE |
1516 | /* |
1517 | * the event is not yet connected with its | |
1518 | * siblings therefore we must first collect | |
1519 | * existing siblings, then add the new event | |
1520 | * before we can simulate the scheduling | |
1521 | */ | |
502568d5 PZ |
1522 | ret = -ENOSPC; |
1523 | n = collect_events(fake_cpuc, leader, true); | |
1da53e02 | 1524 | if (n < 0) |
502568d5 | 1525 | goto out_free; |
fe9081cc | 1526 | |
502568d5 PZ |
1527 | fake_cpuc->n_events = n; |
1528 | n = collect_events(fake_cpuc, event, false); | |
1da53e02 | 1529 | if (n < 0) |
502568d5 | 1530 | goto out_free; |
fe9081cc | 1531 | |
502568d5 | 1532 | fake_cpuc->n_events = n; |
1da53e02 | 1533 | |
a072738e | 1534 | ret = x86_pmu.schedule_events(fake_cpuc, n, NULL); |
502568d5 PZ |
1535 | |
1536 | out_free: | |
1537 | kfree(fake_cpuc); | |
1538 | out: | |
1539 | return ret; | |
fe9081cc PZ |
1540 | } |
1541 | ||
cdd6c482 | 1542 | const struct pmu *hw_perf_event_init(struct perf_event *event) |
621a01ea | 1543 | { |
8113070d | 1544 | const struct pmu *tmp; |
621a01ea IM |
1545 | int err; |
1546 | ||
cdd6c482 | 1547 | err = __hw_perf_event_init(event); |
fe9081cc | 1548 | if (!err) { |
8113070d SE |
1549 | /* |
1550 | * we temporarily connect event to its pmu | |
1551 | * such that validate_group() can classify | |
1552 | * it as an x86 event using is_x86_event() | |
1553 | */ | |
1554 | tmp = event->pmu; | |
1555 | event->pmu = &pmu; | |
1556 | ||
fe9081cc PZ |
1557 | if (event->group_leader != event) |
1558 | err = validate_group(event); | |
ca037701 PZ |
1559 | else |
1560 | err = validate_event(event); | |
8113070d SE |
1561 | |
1562 | event->pmu = tmp; | |
fe9081cc | 1563 | } |
a1792cda | 1564 | if (err) { |
cdd6c482 IM |
1565 | if (event->destroy) |
1566 | event->destroy(event); | |
9ea98e19 | 1567 | return ERR_PTR(err); |
a1792cda | 1568 | } |
621a01ea | 1569 | |
4aeb0b42 | 1570 | return &pmu; |
621a01ea | 1571 | } |
d7d59fb3 PZ |
1572 | |
1573 | /* | |
1574 | * callchain support | |
1575 | */ | |
1576 | ||
1577 | static inline | |
f9188e02 | 1578 | void callchain_store(struct perf_callchain_entry *entry, u64 ip) |
d7d59fb3 | 1579 | { |
f9188e02 | 1580 | if (entry->nr < PERF_MAX_STACK_DEPTH) |
d7d59fb3 PZ |
1581 | entry->ip[entry->nr++] = ip; |
1582 | } | |
1583 | ||
245b2e70 TH |
1584 | static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry); |
1585 | static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry); | |
d7d59fb3 PZ |
1586 | |
1587 | ||
1588 | static void | |
1589 | backtrace_warning_symbol(void *data, char *msg, unsigned long symbol) | |
1590 | { | |
1591 | /* Ignore warnings */ | |
1592 | } | |
1593 | ||
1594 | static void backtrace_warning(void *data, char *msg) | |
1595 | { | |
1596 | /* Ignore warnings */ | |
1597 | } | |
1598 | ||
1599 | static int backtrace_stack(void *data, char *name) | |
1600 | { | |
038e836e | 1601 | return 0; |
d7d59fb3 PZ |
1602 | } |
1603 | ||
1604 | static void backtrace_address(void *data, unsigned long addr, int reliable) | |
1605 | { | |
1606 | struct perf_callchain_entry *entry = data; | |
1607 | ||
6f4dee06 | 1608 | callchain_store(entry, addr); |
d7d59fb3 PZ |
1609 | } |
1610 | ||
1611 | static const struct stacktrace_ops backtrace_ops = { | |
1612 | .warning = backtrace_warning, | |
1613 | .warning_symbol = backtrace_warning_symbol, | |
1614 | .stack = backtrace_stack, | |
1615 | .address = backtrace_address, | |
06d65bda | 1616 | .walk_stack = print_context_stack_bp, |
d7d59fb3 PZ |
1617 | }; |
1618 | ||
038e836e IM |
1619 | #include "../dumpstack.h" |
1620 | ||
d7d59fb3 PZ |
1621 | static void |
1622 | perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
1623 | { | |
f9188e02 | 1624 | callchain_store(entry, PERF_CONTEXT_KERNEL); |
038e836e | 1625 | callchain_store(entry, regs->ip); |
d7d59fb3 | 1626 | |
48b5ba9c | 1627 | dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry); |
d7d59fb3 PZ |
1628 | } |
1629 | ||
257ef9d2 TE |
1630 | #ifdef CONFIG_COMPAT |
1631 | static inline int | |
1632 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
74193ef0 | 1633 | { |
257ef9d2 TE |
1634 | /* 32-bit process in 64-bit kernel. */ |
1635 | struct stack_frame_ia32 frame; | |
1636 | const void __user *fp; | |
74193ef0 | 1637 | |
257ef9d2 TE |
1638 | if (!test_thread_flag(TIF_IA32)) |
1639 | return 0; | |
1640 | ||
1641 | fp = compat_ptr(regs->bp); | |
1642 | while (entry->nr < PERF_MAX_STACK_DEPTH) { | |
1643 | unsigned long bytes; | |
1644 | frame.next_frame = 0; | |
1645 | frame.return_address = 0; | |
1646 | ||
1647 | bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); | |
1648 | if (bytes != sizeof(frame)) | |
1649 | break; | |
74193ef0 | 1650 | |
257ef9d2 TE |
1651 | if (fp < compat_ptr(regs->sp)) |
1652 | break; | |
74193ef0 | 1653 | |
257ef9d2 TE |
1654 | callchain_store(entry, frame.return_address); |
1655 | fp = compat_ptr(frame.next_frame); | |
1656 | } | |
1657 | return 1; | |
d7d59fb3 | 1658 | } |
257ef9d2 TE |
1659 | #else |
1660 | static inline int | |
1661 | perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
1662 | { | |
1663 | return 0; | |
1664 | } | |
1665 | #endif | |
d7d59fb3 PZ |
1666 | |
1667 | static void | |
1668 | perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
1669 | { | |
1670 | struct stack_frame frame; | |
1671 | const void __user *fp; | |
1672 | ||
5a6cec3a IM |
1673 | if (!user_mode(regs)) |
1674 | regs = task_pt_regs(current); | |
1675 | ||
74193ef0 | 1676 | fp = (void __user *)regs->bp; |
d7d59fb3 | 1677 | |
f9188e02 | 1678 | callchain_store(entry, PERF_CONTEXT_USER); |
d7d59fb3 PZ |
1679 | callchain_store(entry, regs->ip); |
1680 | ||
257ef9d2 TE |
1681 | if (perf_callchain_user32(regs, entry)) |
1682 | return; | |
1683 | ||
f9188e02 | 1684 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
257ef9d2 | 1685 | unsigned long bytes; |
038e836e | 1686 | frame.next_frame = NULL; |
d7d59fb3 PZ |
1687 | frame.return_address = 0; |
1688 | ||
257ef9d2 TE |
1689 | bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); |
1690 | if (bytes != sizeof(frame)) | |
d7d59fb3 PZ |
1691 | break; |
1692 | ||
5a6cec3a | 1693 | if ((unsigned long)fp < regs->sp) |
d7d59fb3 PZ |
1694 | break; |
1695 | ||
1696 | callchain_store(entry, frame.return_address); | |
038e836e | 1697 | fp = frame.next_frame; |
d7d59fb3 PZ |
1698 | } |
1699 | } | |
1700 | ||
1701 | static void | |
1702 | perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
1703 | { | |
1704 | int is_user; | |
1705 | ||
1706 | if (!regs) | |
1707 | return; | |
1708 | ||
1709 | is_user = user_mode(regs); | |
1710 | ||
d7d59fb3 PZ |
1711 | if (is_user && current->state != TASK_RUNNING) |
1712 | return; | |
1713 | ||
1714 | if (!is_user) | |
1715 | perf_callchain_kernel(regs, entry); | |
1716 | ||
1717 | if (current->mm) | |
1718 | perf_callchain_user(regs, entry); | |
1719 | } | |
1720 | ||
1721 | struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) | |
1722 | { | |
1723 | struct perf_callchain_entry *entry; | |
1724 | ||
39447b38 ZY |
1725 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
1726 | /* TODO: We don't support guest os callchain now */ | |
1727 | return NULL; | |
1728 | } | |
1729 | ||
d7d59fb3 | 1730 | if (in_nmi()) |
245b2e70 | 1731 | entry = &__get_cpu_var(pmc_nmi_entry); |
d7d59fb3 | 1732 | else |
245b2e70 | 1733 | entry = &__get_cpu_var(pmc_irq_entry); |
d7d59fb3 PZ |
1734 | |
1735 | entry->nr = 0; | |
1736 | ||
1737 | perf_do_callchain(regs, entry); | |
1738 | ||
1739 | return entry; | |
1740 | } | |
5331d7b8 FW |
1741 | |
1742 | void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip) | |
1743 | { | |
1744 | regs->ip = ip; | |
1745 | /* | |
1746 | * perf_arch_fetch_caller_regs adds another call, we need to increment | |
1747 | * the skip level | |
1748 | */ | |
1749 | regs->bp = rewind_frame_pointer(skip + 1); | |
1750 | regs->cs = __KERNEL_CS; | |
1751 | local_save_flags(regs->flags); | |
1752 | } | |
39447b38 ZY |
1753 | |
1754 | unsigned long perf_instruction_pointer(struct pt_regs *regs) | |
1755 | { | |
1756 | unsigned long ip; | |
dcf46b94 | 1757 | |
39447b38 ZY |
1758 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) |
1759 | ip = perf_guest_cbs->get_guest_ip(); | |
1760 | else | |
1761 | ip = instruction_pointer(regs); | |
dcf46b94 | 1762 | |
39447b38 ZY |
1763 | return ip; |
1764 | } | |
1765 | ||
1766 | unsigned long perf_misc_flags(struct pt_regs *regs) | |
1767 | { | |
1768 | int misc = 0; | |
dcf46b94 | 1769 | |
39447b38 | 1770 | if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) { |
dcf46b94 ZY |
1771 | if (perf_guest_cbs->is_user_mode()) |
1772 | misc |= PERF_RECORD_MISC_GUEST_USER; | |
1773 | else | |
1774 | misc |= PERF_RECORD_MISC_GUEST_KERNEL; | |
1775 | } else { | |
1776 | if (user_mode(regs)) | |
1777 | misc |= PERF_RECORD_MISC_USER; | |
1778 | else | |
1779 | misc |= PERF_RECORD_MISC_KERNEL; | |
1780 | } | |
1781 | ||
39447b38 ZY |
1782 | if (regs->flags & PERF_EFLAGS_EXACT) |
1783 | misc |= PERF_RECORD_MISC_EXACT; | |
1784 | ||
1785 | return misc; | |
1786 | } |