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