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