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