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