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