Merge branch 'perf' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux-2...
[linux-2.6-block.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
b4cdc5c2 429static int x86_pmu_hw_config(struct perf_event *event)
a072738e
CG
430{
431 /*
432 * Generate PMC IRQs:
433 * (keep 'enabled' bit clear for now)
434 */
b4cdc5c2 435 event->hw.config = ARCH_PERFMON_EVENTSEL_INT;
a072738e
CG
436
437 /*
438 * Count user and OS events unless requested not to
439 */
b4cdc5c2
PZ
440 if (!event->attr.exclude_user)
441 event->hw.config |= ARCH_PERFMON_EVENTSEL_USR;
442 if (!event->attr.exclude_kernel)
443 event->hw.config |= ARCH_PERFMON_EVENTSEL_OS;
a072738e 444
b4cdc5c2
PZ
445 if (event->attr.type == PERF_TYPE_RAW)
446 event->hw.config |= event->attr.config & X86_RAW_EVENT_MASK;
a072738e 447
b4cdc5c2 448 return 0;
a098f448
RR
449}
450
241771ef 451/*
0d48696f 452 * Setup the hardware configuration for a given attr_type
241771ef 453 */
cdd6c482 454static int __hw_perf_event_init(struct perf_event *event)
241771ef 455{
cdd6c482
IM
456 struct perf_event_attr *attr = &event->attr;
457 struct hw_perf_event *hwc = &event->hw;
9c74fb50 458 u64 config;
4e935e47 459 int err;
241771ef 460
85cf9dba
RR
461 if (!x86_pmu_initialized())
462 return -ENODEV;
241771ef 463
4e935e47 464 err = 0;
cdd6c482 465 if (!atomic_inc_not_zero(&active_events)) {
4e935e47 466 mutex_lock(&pmc_reserve_mutex);
cdd6c482 467 if (atomic_read(&active_events) == 0) {
30dd568c
MM
468 if (!reserve_pmc_hardware())
469 err = -EBUSY;
4b24a88b 470 else {
ca037701 471 err = reserve_ds_buffers();
4b24a88b
SE
472 if (err)
473 release_pmc_hardware();
474 }
30dd568c
MM
475 }
476 if (!err)
cdd6c482 477 atomic_inc(&active_events);
4e935e47
PZ
478 mutex_unlock(&pmc_reserve_mutex);
479 }
480 if (err)
481 return err;
482
cdd6c482 483 event->destroy = hw_perf_event_destroy;
a1792cda 484
b690081d 485 hwc->idx = -1;
447a194b
SE
486 hwc->last_cpu = -1;
487 hwc->last_tag = ~0ULL;
b690081d 488
a072738e 489 /* Processor specifics */
b4cdc5c2 490 err = x86_pmu.hw_config(event);
984763cb
RR
491 if (err)
492 return err;
0475f9ea 493
bd2b5b12 494 if (!hwc->sample_period) {
b23f3325 495 hwc->sample_period = x86_pmu.max_period;
9e350de3 496 hwc->last_period = hwc->sample_period;
bd2b5b12 497 atomic64_set(&hwc->period_left, hwc->sample_period);
04da8a43
IM
498 } else {
499 /*
500 * If we have a PMU initialized but no APIC
501 * interrupts, we cannot sample hardware
cdd6c482
IM
502 * events (user-space has to fall back and
503 * sample via a hrtimer based software event):
04da8a43
IM
504 */
505 if (!x86_pmu.apic)
506 return -EOPNOTSUPP;
bd2b5b12 507 }
d2517a49 508
b4cdc5c2 509 if (attr->type == PERF_TYPE_RAW)
8326f44d 510 return 0;
241771ef 511
8326f44d
IM
512 if (attr->type == PERF_TYPE_HW_CACHE)
513 return set_ext_hw_attr(hwc, attr);
514
515 if (attr->config >= x86_pmu.max_events)
516 return -EINVAL;
9c74fb50 517
8326f44d
IM
518 /*
519 * The generic map:
520 */
9c74fb50
PZ
521 config = x86_pmu.event_map(attr->config);
522
523 if (config == 0)
524 return -ENOENT;
525
526 if (config == -1LL)
527 return -EINVAL;
528
747b50aa 529 /*
530 * Branch tracing:
531 */
532 if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1653192f 533 (hwc->sample_period == 1)) {
534 /* BTS is not supported by this architecture. */
ca037701 535 if (!x86_pmu.bts)
1653192f 536 return -EOPNOTSUPP;
537
538 /* BTS is currently only allowed for user-mode. */
a072738e 539 if (!attr->exclude_kernel)
1653192f 540 return -EOPNOTSUPP;
541 }
747b50aa 542
9c74fb50 543 hwc->config |= config;
4e935e47 544
241771ef
IM
545 return 0;
546}
547
8c48e444 548static void x86_pmu_disable_all(void)
f87ad35d 549{
cdd6c482 550 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
9e35ad38
PZ
551 int idx;
552
948b1bb8 553 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
b0f3f28e
PZ
554 u64 val;
555
43f6201a 556 if (!test_bit(idx, cpuc->active_mask))
4295ee62 557 continue;
8c48e444 558 rdmsrl(x86_pmu.eventsel + idx, val);
bb1165d6 559 if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
4295ee62 560 continue;
bb1165d6 561 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
8c48e444 562 wrmsrl(x86_pmu.eventsel + idx, val);
f87ad35d 563 }
f87ad35d
JSR
564}
565
9e35ad38 566void hw_perf_disable(void)
b56a3802 567{
1da53e02
SE
568 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
569
85cf9dba 570 if (!x86_pmu_initialized())
9e35ad38 571 return;
1da53e02 572
1a6e21f7
PZ
573 if (!cpuc->enabled)
574 return;
575
576 cpuc->n_added = 0;
577 cpuc->enabled = 0;
578 barrier();
1da53e02
SE
579
580 x86_pmu.disable_all();
b56a3802 581}
241771ef 582
11164cd4 583static void x86_pmu_enable_all(int added)
f87ad35d 584{
cdd6c482 585 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
f87ad35d
JSR
586 int idx;
587
948b1bb8 588 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
cdd6c482 589 struct perf_event *event = cpuc->events[idx];
4295ee62 590 u64 val;
b0f3f28e 591
43f6201a 592 if (!test_bit(idx, cpuc->active_mask))
4295ee62 593 continue;
984b838c 594
cdd6c482 595 val = event->hw.config;
bb1165d6 596 val |= ARCH_PERFMON_EVENTSEL_ENABLE;
8c48e444 597 wrmsrl(x86_pmu.eventsel + idx, val);
f87ad35d
JSR
598 }
599}
600
1da53e02
SE
601static const struct pmu pmu;
602
603static inline int is_x86_event(struct perf_event *event)
604{
605 return event->pmu == &pmu;
606}
607
608static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
609{
63b14649 610 struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
1da53e02 611 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
c933c1a6 612 int i, j, w, wmax, num = 0;
1da53e02
SE
613 struct hw_perf_event *hwc;
614
615 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
616
617 for (i = 0; i < n; i++) {
b622d644
PZ
618 c = x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
619 constraints[i] = c;
1da53e02
SE
620 }
621
8113070d
SE
622 /*
623 * fastpath, try to reuse previous register
624 */
c933c1a6 625 for (i = 0; i < n; i++) {
8113070d 626 hwc = &cpuc->event_list[i]->hw;
81269a08 627 c = constraints[i];
8113070d
SE
628
629 /* never assigned */
630 if (hwc->idx == -1)
631 break;
632
633 /* constraint still honored */
63b14649 634 if (!test_bit(hwc->idx, c->idxmsk))
8113070d
SE
635 break;
636
637 /* not already used */
638 if (test_bit(hwc->idx, used_mask))
639 break;
640
34538ee7 641 __set_bit(hwc->idx, used_mask);
8113070d
SE
642 if (assign)
643 assign[i] = hwc->idx;
644 }
c933c1a6 645 if (i == n)
8113070d
SE
646 goto done;
647
648 /*
649 * begin slow path
650 */
651
652 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
653
1da53e02
SE
654 /*
655 * weight = number of possible counters
656 *
657 * 1 = most constrained, only works on one counter
658 * wmax = least constrained, works on any counter
659 *
660 * assign events to counters starting with most
661 * constrained events.
662 */
948b1bb8 663 wmax = x86_pmu.num_counters;
1da53e02
SE
664
665 /*
666 * when fixed event counters are present,
667 * wmax is incremented by 1 to account
668 * for one more choice
669 */
948b1bb8 670 if (x86_pmu.num_counters_fixed)
1da53e02
SE
671 wmax++;
672
8113070d 673 for (w = 1, num = n; num && w <= wmax; w++) {
1da53e02 674 /* for each event */
8113070d 675 for (i = 0; num && i < n; i++) {
81269a08 676 c = constraints[i];
1da53e02
SE
677 hwc = &cpuc->event_list[i]->hw;
678
272d30be 679 if (c->weight != w)
1da53e02
SE
680 continue;
681
984b3f57 682 for_each_set_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
1da53e02
SE
683 if (!test_bit(j, used_mask))
684 break;
685 }
686
687 if (j == X86_PMC_IDX_MAX)
688 break;
1da53e02 689
34538ee7 690 __set_bit(j, used_mask);
8113070d 691
1da53e02
SE
692 if (assign)
693 assign[i] = j;
694 num--;
695 }
696 }
8113070d 697done:
1da53e02
SE
698 /*
699 * scheduling failed or is just a simulation,
700 * free resources if necessary
701 */
702 if (!assign || num) {
703 for (i = 0; i < n; i++) {
704 if (x86_pmu.put_event_constraints)
705 x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
706 }
707 }
708 return num ? -ENOSPC : 0;
709}
710
711/*
712 * dogrp: true if must collect siblings events (group)
713 * returns total number of events and error code
714 */
715static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
716{
717 struct perf_event *event;
718 int n, max_count;
719
948b1bb8 720 max_count = x86_pmu.num_counters + x86_pmu.num_counters_fixed;
1da53e02
SE
721
722 /* current number of events already accepted */
723 n = cpuc->n_events;
724
725 if (is_x86_event(leader)) {
726 if (n >= max_count)
727 return -ENOSPC;
728 cpuc->event_list[n] = leader;
729 n++;
730 }
731 if (!dogrp)
732 return n;
733
734 list_for_each_entry(event, &leader->sibling_list, group_entry) {
735 if (!is_x86_event(event) ||
8113070d 736 event->state <= PERF_EVENT_STATE_OFF)
1da53e02
SE
737 continue;
738
739 if (n >= max_count)
740 return -ENOSPC;
741
742 cpuc->event_list[n] = event;
743 n++;
744 }
745 return n;
746}
747
1da53e02 748static inline void x86_assign_hw_event(struct perf_event *event,
447a194b 749 struct cpu_hw_events *cpuc, int i)
1da53e02 750{
447a194b
SE
751 struct hw_perf_event *hwc = &event->hw;
752
753 hwc->idx = cpuc->assign[i];
754 hwc->last_cpu = smp_processor_id();
755 hwc->last_tag = ++cpuc->tags[i];
1da53e02
SE
756
757 if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
758 hwc->config_base = 0;
759 hwc->event_base = 0;
760 } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
761 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
762 /*
763 * We set it so that event_base + idx in wrmsr/rdmsr maps to
764 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
765 */
766 hwc->event_base =
767 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
768 } else {
769 hwc->config_base = x86_pmu.eventsel;
770 hwc->event_base = x86_pmu.perfctr;
771 }
772}
773
447a194b
SE
774static inline int match_prev_assignment(struct hw_perf_event *hwc,
775 struct cpu_hw_events *cpuc,
776 int i)
777{
778 return hwc->idx == cpuc->assign[i] &&
779 hwc->last_cpu == smp_processor_id() &&
780 hwc->last_tag == cpuc->tags[i];
781}
782
c08053e6 783static int x86_pmu_start(struct perf_event *event);
d76a0812 784static void x86_pmu_stop(struct perf_event *event);
2e841873 785
9e35ad38 786void hw_perf_enable(void)
ee06094f 787{
1da53e02
SE
788 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
789 struct perf_event *event;
790 struct hw_perf_event *hwc;
11164cd4 791 int i, added = cpuc->n_added;
1da53e02 792
85cf9dba 793 if (!x86_pmu_initialized())
2b9ff0db 794 return;
1a6e21f7
PZ
795
796 if (cpuc->enabled)
797 return;
798
1da53e02 799 if (cpuc->n_added) {
19925ce7 800 int n_running = cpuc->n_events - cpuc->n_added;
1da53e02
SE
801 /*
802 * apply assignment obtained either from
803 * hw_perf_group_sched_in() or x86_pmu_enable()
804 *
805 * step1: save events moving to new counters
806 * step2: reprogram moved events into new counters
807 */
19925ce7 808 for (i = 0; i < n_running; i++) {
1da53e02
SE
809 event = cpuc->event_list[i];
810 hwc = &event->hw;
811
447a194b
SE
812 /*
813 * we can avoid reprogramming counter if:
814 * - assigned same counter as last time
815 * - running on same CPU as last time
816 * - no other event has used the counter since
817 */
818 if (hwc->idx == -1 ||
819 match_prev_assignment(hwc, cpuc, i))
1da53e02
SE
820 continue;
821
d76a0812 822 x86_pmu_stop(event);
1da53e02
SE
823 }
824
825 for (i = 0; i < cpuc->n_events; i++) {
1da53e02
SE
826 event = cpuc->event_list[i];
827 hwc = &event->hw;
828
45e16a68 829 if (!match_prev_assignment(hwc, cpuc, i))
447a194b 830 x86_assign_hw_event(event, cpuc, i);
45e16a68
PZ
831 else if (i < n_running)
832 continue;
1da53e02 833
c08053e6 834 x86_pmu_start(event);
1da53e02
SE
835 }
836 cpuc->n_added = 0;
837 perf_events_lapic_init();
838 }
1a6e21f7
PZ
839
840 cpuc->enabled = 1;
841 barrier();
842
11164cd4 843 x86_pmu.enable_all(added);
ee06094f 844}
ee06094f 845
aff3d91a 846static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc)
b0f3f28e 847{
7645a24c 848 wrmsrl(hwc->config_base + hwc->idx,
bb1165d6 849 hwc->config | ARCH_PERFMON_EVENTSEL_ENABLE);
b0f3f28e
PZ
850}
851
aff3d91a 852static inline void x86_pmu_disable_event(struct perf_event *event)
b0f3f28e 853{
aff3d91a 854 struct hw_perf_event *hwc = &event->hw;
7645a24c
PZ
855
856 wrmsrl(hwc->config_base + hwc->idx, hwc->config);
b0f3f28e
PZ
857}
858
245b2e70 859static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
241771ef 860
ee06094f
IM
861/*
862 * Set the next IRQ period, based on the hwc->period_left value.
cdd6c482 863 * To be called with the event disabled in hw:
ee06094f 864 */
e4abb5d4 865static int
07088edb 866x86_perf_event_set_period(struct perf_event *event)
241771ef 867{
07088edb 868 struct hw_perf_event *hwc = &event->hw;
2f18d1e8 869 s64 left = atomic64_read(&hwc->period_left);
e4abb5d4 870 s64 period = hwc->sample_period;
7645a24c 871 int ret = 0, idx = hwc->idx;
ee06094f 872
30dd568c
MM
873 if (idx == X86_PMC_IDX_FIXED_BTS)
874 return 0;
875
ee06094f 876 /*
af901ca1 877 * If we are way outside a reasonable range then just skip forward:
ee06094f
IM
878 */
879 if (unlikely(left <= -period)) {
880 left = period;
881 atomic64_set(&hwc->period_left, left);
9e350de3 882 hwc->last_period = period;
e4abb5d4 883 ret = 1;
ee06094f
IM
884 }
885
886 if (unlikely(left <= 0)) {
887 left += period;
888 atomic64_set(&hwc->period_left, left);
9e350de3 889 hwc->last_period = period;
e4abb5d4 890 ret = 1;
ee06094f 891 }
1c80f4b5 892 /*
dfc65094 893 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
1c80f4b5
IM
894 */
895 if (unlikely(left < 2))
896 left = 2;
241771ef 897
e4abb5d4
PZ
898 if (left > x86_pmu.max_period)
899 left = x86_pmu.max_period;
900
245b2e70 901 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
ee06094f
IM
902
903 /*
cdd6c482 904 * The hw event starts counting from this event offset,
ee06094f
IM
905 * mark it to be able to extra future deltas:
906 */
2f18d1e8 907 atomic64_set(&hwc->prev_count, (u64)-left);
ee06094f 908
7645a24c 909 wrmsrl(hwc->event_base + idx,
948b1bb8 910 (u64)(-left) & x86_pmu.cntval_mask);
e4abb5d4 911
cdd6c482 912 perf_event_update_userpage(event);
194002b2 913
e4abb5d4 914 return ret;
2f18d1e8
IM
915}
916
aff3d91a 917static void x86_pmu_enable_event(struct perf_event *event)
7c90cc45 918{
cdd6c482 919 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
7c90cc45 920 if (cpuc->enabled)
aff3d91a 921 __x86_pmu_enable_event(&event->hw);
241771ef
IM
922}
923
b690081d 924/*
1da53e02
SE
925 * activate a single event
926 *
927 * The event is added to the group of enabled events
928 * but only if it can be scehduled with existing events.
929 *
930 * Called with PMU disabled. If successful and return value 1,
931 * then guaranteed to call perf_enable() and hw_perf_enable()
fe9081cc
PZ
932 */
933static int x86_pmu_enable(struct perf_event *event)
934{
935 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1da53e02
SE
936 struct hw_perf_event *hwc;
937 int assign[X86_PMC_IDX_MAX];
938 int n, n0, ret;
fe9081cc 939
1da53e02 940 hwc = &event->hw;
fe9081cc 941
1da53e02
SE
942 n0 = cpuc->n_events;
943 n = collect_events(cpuc, event, false);
944 if (n < 0)
945 return n;
53b441a5 946
a072738e 947 ret = x86_pmu.schedule_events(cpuc, n, assign);
1da53e02
SE
948 if (ret)
949 return ret;
950 /*
951 * copy new assignment, now we know it is possible
952 * will be used by hw_perf_enable()
953 */
954 memcpy(cpuc->assign, assign, n*sizeof(int));
7e2ae347 955
1da53e02 956 cpuc->n_events = n;
356e1f2e 957 cpuc->n_added += n - n0;
95cdd2e7
IM
958
959 return 0;
241771ef
IM
960}
961
d76a0812
SE
962static int x86_pmu_start(struct perf_event *event)
963{
c08053e6
PZ
964 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
965 int idx = event->hw.idx;
966
967 if (idx == -1)
d76a0812
SE
968 return -EAGAIN;
969
07088edb 970 x86_perf_event_set_period(event);
c08053e6
PZ
971 cpuc->events[idx] = event;
972 __set_bit(idx, cpuc->active_mask);
aff3d91a 973 x86_pmu.enable(event);
c08053e6 974 perf_event_update_userpage(event);
d76a0812
SE
975
976 return 0;
977}
978
cdd6c482 979static void x86_pmu_unthrottle(struct perf_event *event)
a78ac325 980{
71e2d282
PZ
981 int ret = x86_pmu_start(event);
982 WARN_ON_ONCE(ret);
a78ac325
PZ
983}
984
cdd6c482 985void perf_event_print_debug(void)
241771ef 986{
2f18d1e8 987 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
ca037701 988 u64 pebs;
cdd6c482 989 struct cpu_hw_events *cpuc;
5bb9efe3 990 unsigned long flags;
1e125676
IM
991 int cpu, idx;
992
948b1bb8 993 if (!x86_pmu.num_counters)
1e125676 994 return;
241771ef 995
5bb9efe3 996 local_irq_save(flags);
241771ef
IM
997
998 cpu = smp_processor_id();
cdd6c482 999 cpuc = &per_cpu(cpu_hw_events, cpu);
241771ef 1000
faa28ae0 1001 if (x86_pmu.version >= 2) {
a1ef58f4
JSR
1002 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
1003 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1004 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1005 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
ca037701 1006 rdmsrl(MSR_IA32_PEBS_ENABLE, pebs);
a1ef58f4
JSR
1007
1008 pr_info("\n");
1009 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1010 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1011 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1012 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
ca037701 1013 pr_info("CPU#%d: pebs: %016llx\n", cpu, pebs);
f87ad35d 1014 }
7645a24c 1015 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
241771ef 1016
948b1bb8 1017 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
4a06bd85
RR
1018 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
1019 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
241771ef 1020
245b2e70 1021 prev_left = per_cpu(pmc_prev_left[idx], cpu);
241771ef 1022
a1ef58f4 1023 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
241771ef 1024 cpu, idx, pmc_ctrl);
a1ef58f4 1025 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
241771ef 1026 cpu, idx, pmc_count);
a1ef58f4 1027 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
ee06094f 1028 cpu, idx, prev_left);
241771ef 1029 }
948b1bb8 1030 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
2f18d1e8
IM
1031 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
1032
a1ef58f4 1033 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
2f18d1e8
IM
1034 cpu, idx, pmc_count);
1035 }
5bb9efe3 1036 local_irq_restore(flags);
241771ef
IM
1037}
1038
d76a0812 1039static void x86_pmu_stop(struct perf_event *event)
241771ef 1040{
d76a0812 1041 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
cdd6c482 1042 struct hw_perf_event *hwc = &event->hw;
2e841873 1043 int idx = hwc->idx;
241771ef 1044
71e2d282
PZ
1045 if (!__test_and_clear_bit(idx, cpuc->active_mask))
1046 return;
1047
aff3d91a 1048 x86_pmu.disable(event);
241771ef 1049
ee06094f 1050 /*
cdd6c482 1051 * Drain the remaining delta count out of a event
ee06094f
IM
1052 * that we are disabling:
1053 */
cc2ad4ba 1054 x86_perf_event_update(event);
30dd568c 1055
cdd6c482 1056 cpuc->events[idx] = NULL;
2e841873
PZ
1057}
1058
1059static void x86_pmu_disable(struct perf_event *event)
1060{
1061 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1062 int i;
1063
d76a0812 1064 x86_pmu_stop(event);
194002b2 1065
1da53e02
SE
1066 for (i = 0; i < cpuc->n_events; i++) {
1067 if (event == cpuc->event_list[i]) {
1068
1069 if (x86_pmu.put_event_constraints)
1070 x86_pmu.put_event_constraints(cpuc, event);
1071
1072 while (++i < cpuc->n_events)
1073 cpuc->event_list[i-1] = cpuc->event_list[i];
1074
1075 --cpuc->n_events;
6c9687ab 1076 break;
1da53e02
SE
1077 }
1078 }
cdd6c482 1079 perf_event_update_userpage(event);
241771ef
IM
1080}
1081
8c48e444 1082static int x86_pmu_handle_irq(struct pt_regs *regs)
a29aa8a7 1083{
df1a132b 1084 struct perf_sample_data data;
cdd6c482
IM
1085 struct cpu_hw_events *cpuc;
1086 struct perf_event *event;
1087 struct hw_perf_event *hwc;
11d1578f 1088 int idx, handled = 0;
9029a5e3
IM
1089 u64 val;
1090
dc1d628a 1091 perf_sample_data_init(&data, 0);
df1a132b 1092
cdd6c482 1093 cpuc = &__get_cpu_var(cpu_hw_events);
962bf7a6 1094
948b1bb8 1095 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
43f6201a 1096 if (!test_bit(idx, cpuc->active_mask))
a29aa8a7 1097 continue;
962bf7a6 1098
cdd6c482
IM
1099 event = cpuc->events[idx];
1100 hwc = &event->hw;
a4016a79 1101
cc2ad4ba 1102 val = x86_perf_event_update(event);
948b1bb8 1103 if (val & (1ULL << (x86_pmu.cntval_bits - 1)))
48e22d56 1104 continue;
962bf7a6 1105
9e350de3 1106 /*
cdd6c482 1107 * event overflow
9e350de3
PZ
1108 */
1109 handled = 1;
cdd6c482 1110 data.period = event->hw.last_period;
9e350de3 1111
07088edb 1112 if (!x86_perf_event_set_period(event))
e4abb5d4
PZ
1113 continue;
1114
cdd6c482 1115 if (perf_event_overflow(event, 1, &data, regs))
71e2d282 1116 x86_pmu_stop(event);
a29aa8a7 1117 }
962bf7a6 1118
9e350de3
PZ
1119 if (handled)
1120 inc_irq_stat(apic_perf_irqs);
1121
a29aa8a7
RR
1122 return handled;
1123}
39d81eab 1124
b6276f35
PZ
1125void smp_perf_pending_interrupt(struct pt_regs *regs)
1126{
1127 irq_enter();
1128 ack_APIC_irq();
1129 inc_irq_stat(apic_pending_irqs);
cdd6c482 1130 perf_event_do_pending();
b6276f35
PZ
1131 irq_exit();
1132}
1133
cdd6c482 1134void set_perf_event_pending(void)
b6276f35 1135{
04da8a43 1136#ifdef CONFIG_X86_LOCAL_APIC
7d428966
PZ
1137 if (!x86_pmu.apic || !x86_pmu_initialized())
1138 return;
1139
b6276f35 1140 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
04da8a43 1141#endif
b6276f35
PZ
1142}
1143
cdd6c482 1144void perf_events_lapic_init(void)
241771ef 1145{
04da8a43 1146 if (!x86_pmu.apic || !x86_pmu_initialized())
241771ef 1147 return;
85cf9dba 1148
241771ef 1149 /*
c323d95f 1150 * Always use NMI for PMU
241771ef 1151 */
c323d95f 1152 apic_write(APIC_LVTPC, APIC_DM_NMI);
241771ef
IM
1153}
1154
1155static int __kprobes
cdd6c482 1156perf_event_nmi_handler(struct notifier_block *self,
241771ef
IM
1157 unsigned long cmd, void *__args)
1158{
1159 struct die_args *args = __args;
1160 struct pt_regs *regs;
b0f3f28e 1161
cdd6c482 1162 if (!atomic_read(&active_events))
63a809a2
PZ
1163 return NOTIFY_DONE;
1164
b0f3f28e
PZ
1165 switch (cmd) {
1166 case DIE_NMI:
1167 case DIE_NMI_IPI:
1168 break;
241771ef 1169
b0f3f28e 1170 default:
241771ef 1171 return NOTIFY_DONE;
b0f3f28e 1172 }
241771ef
IM
1173
1174 regs = args->regs;
1175
1176 apic_write(APIC_LVTPC, APIC_DM_NMI);
a4016a79
PZ
1177 /*
1178 * Can't rely on the handled return value to say it was our NMI, two
cdd6c482 1179 * events could trigger 'simultaneously' raising two back-to-back NMIs.
a4016a79
PZ
1180 *
1181 * If the first NMI handles both, the latter will be empty and daze
1182 * the CPU.
1183 */
a3288106 1184 x86_pmu.handle_irq(regs);
241771ef 1185
a4016a79 1186 return NOTIFY_STOP;
241771ef
IM
1187}
1188
f22f54f4
PZ
1189static __read_mostly struct notifier_block perf_event_nmi_notifier = {
1190 .notifier_call = perf_event_nmi_handler,
1191 .next = NULL,
1192 .priority = 1
1193};
1194
63b14649 1195static struct event_constraint unconstrained;
38331f62 1196static struct event_constraint emptyconstraint;
63b14649 1197
63b14649 1198static struct event_constraint *
f22f54f4 1199x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
1da53e02 1200{
63b14649 1201 struct event_constraint *c;
1da53e02 1202
1da53e02
SE
1203 if (x86_pmu.event_constraints) {
1204 for_each_event_constraint(c, x86_pmu.event_constraints) {
63b14649
PZ
1205 if ((event->hw.config & c->cmask) == c->code)
1206 return c;
1da53e02
SE
1207 }
1208 }
63b14649
PZ
1209
1210 return &unconstrained;
1da53e02
SE
1211}
1212
1da53e02 1213static int x86_event_sched_in(struct perf_event *event,
6e37738a 1214 struct perf_cpu_context *cpuctx)
1da53e02
SE
1215{
1216 int ret = 0;
1217
1218 event->state = PERF_EVENT_STATE_ACTIVE;
6e37738a 1219 event->oncpu = smp_processor_id();
1da53e02
SE
1220 event->tstamp_running += event->ctx->time - event->tstamp_stopped;
1221
1222 if (!is_x86_event(event))
1223 ret = event->pmu->enable(event);
1224
1225 if (!ret && !is_software_event(event))
1226 cpuctx->active_oncpu++;
1227
1228 if (!ret && event->attr.exclusive)
1229 cpuctx->exclusive = 1;
1230
1231 return ret;
1232}
1233
1234static void x86_event_sched_out(struct perf_event *event,
6e37738a 1235 struct perf_cpu_context *cpuctx)
1da53e02
SE
1236{
1237 event->state = PERF_EVENT_STATE_INACTIVE;
1238 event->oncpu = -1;
1239
1240 if (!is_x86_event(event))
1241 event->pmu->disable(event);
1242
1243 event->tstamp_running -= event->ctx->time - event->tstamp_stopped;
1244
1245 if (!is_software_event(event))
1246 cpuctx->active_oncpu--;
1247
1248 if (event->attr.exclusive || !cpuctx->active_oncpu)
1249 cpuctx->exclusive = 0;
1250}
1251
1252/*
1253 * Called to enable a whole group of events.
1254 * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
1255 * Assumes the caller has disabled interrupts and has
1256 * frozen the PMU with hw_perf_save_disable.
1257 *
1258 * called with PMU disabled. If successful and return value 1,
1259 * then guaranteed to call perf_enable() and hw_perf_enable()
1260 */
1261int hw_perf_group_sched_in(struct perf_event *leader,
1262 struct perf_cpu_context *cpuctx,
6e37738a 1263 struct perf_event_context *ctx)
1da53e02 1264{
6e37738a 1265 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1da53e02
SE
1266 struct perf_event *sub;
1267 int assign[X86_PMC_IDX_MAX];
1268 int n0, n1, ret;
1269
0b861225
CG
1270 if (!x86_pmu_initialized())
1271 return 0;
1272
1da53e02
SE
1273 /* n0 = total number of events */
1274 n0 = collect_events(cpuc, leader, true);
1275 if (n0 < 0)
1276 return n0;
1277
a072738e 1278 ret = x86_pmu.schedule_events(cpuc, n0, assign);
1da53e02
SE
1279 if (ret)
1280 return ret;
1281
6e37738a 1282 ret = x86_event_sched_in(leader, cpuctx);
1da53e02
SE
1283 if (ret)
1284 return ret;
1285
1286 n1 = 1;
1287 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
8113070d 1288 if (sub->state > PERF_EVENT_STATE_OFF) {
6e37738a 1289 ret = x86_event_sched_in(sub, cpuctx);
1da53e02
SE
1290 if (ret)
1291 goto undo;
1292 ++n1;
1293 }
1294 }
1295 /*
1296 * copy new assignment, now we know it is possible
1297 * will be used by hw_perf_enable()
1298 */
1299 memcpy(cpuc->assign, assign, n0*sizeof(int));
1300
1301 cpuc->n_events = n0;
356e1f2e 1302 cpuc->n_added += n1;
1da53e02
SE
1303 ctx->nr_active += n1;
1304
1305 /*
1306 * 1 means successful and events are active
1307 * This is not quite true because we defer
1308 * actual activation until hw_perf_enable() but
1309 * this way we* ensure caller won't try to enable
1310 * individual events
1311 */
1312 return 1;
1313undo:
6e37738a 1314 x86_event_sched_out(leader, cpuctx);
1da53e02
SE
1315 n0 = 1;
1316 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
1317 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
6e37738a 1318 x86_event_sched_out(sub, cpuctx);
1da53e02
SE
1319 if (++n0 == n1)
1320 break;
1321 }
1322 }
1323 return ret;
1324}
1325
f22f54f4
PZ
1326#include "perf_event_amd.c"
1327#include "perf_event_p6.c"
a072738e 1328#include "perf_event_p4.c"
caff2bef 1329#include "perf_event_intel_lbr.c"
ca037701 1330#include "perf_event_intel_ds.c"
f22f54f4 1331#include "perf_event_intel.c"
f87ad35d 1332
3f6da390
PZ
1333static int __cpuinit
1334x86_pmu_notifier(struct notifier_block *self, unsigned long action, void *hcpu)
1335{
1336 unsigned int cpu = (long)hcpu;
b38b24ea 1337 int ret = NOTIFY_OK;
3f6da390
PZ
1338
1339 switch (action & ~CPU_TASKS_FROZEN) {
1340 case CPU_UP_PREPARE:
1341 if (x86_pmu.cpu_prepare)
b38b24ea 1342 ret = x86_pmu.cpu_prepare(cpu);
3f6da390
PZ
1343 break;
1344
1345 case CPU_STARTING:
1346 if (x86_pmu.cpu_starting)
1347 x86_pmu.cpu_starting(cpu);
1348 break;
1349
1350 case CPU_DYING:
1351 if (x86_pmu.cpu_dying)
1352 x86_pmu.cpu_dying(cpu);
1353 break;
1354
b38b24ea 1355 case CPU_UP_CANCELED:
3f6da390
PZ
1356 case CPU_DEAD:
1357 if (x86_pmu.cpu_dead)
1358 x86_pmu.cpu_dead(cpu);
1359 break;
1360
1361 default:
1362 break;
1363 }
1364
b38b24ea 1365 return ret;
3f6da390
PZ
1366}
1367
12558038
CG
1368static void __init pmu_check_apic(void)
1369{
1370 if (cpu_has_apic)
1371 return;
1372
1373 x86_pmu.apic = 0;
1374 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1375 pr_info("no hardware sampling interrupt available.\n");
1376}
1377
cdd6c482 1378void __init init_hw_perf_events(void)
b56a3802 1379{
b622d644 1380 struct event_constraint *c;
72eae04d
RR
1381 int err;
1382
cdd6c482 1383 pr_info("Performance Events: ");
1123e3ad 1384
b56a3802
JSR
1385 switch (boot_cpu_data.x86_vendor) {
1386 case X86_VENDOR_INTEL:
72eae04d 1387 err = intel_pmu_init();
b56a3802 1388 break;
f87ad35d 1389 case X86_VENDOR_AMD:
72eae04d 1390 err = amd_pmu_init();
f87ad35d 1391 break;
4138960a
RR
1392 default:
1393 return;
b56a3802 1394 }
1123e3ad 1395 if (err != 0) {
cdd6c482 1396 pr_cont("no PMU driver, software events only.\n");
b56a3802 1397 return;
1123e3ad 1398 }
b56a3802 1399
12558038
CG
1400 pmu_check_apic();
1401
1123e3ad 1402 pr_cont("%s PMU driver.\n", x86_pmu.name);
faa28ae0 1403
3c44780b
PZ
1404 if (x86_pmu.quirks)
1405 x86_pmu.quirks();
1406
948b1bb8 1407 if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
cdd6c482 1408 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
948b1bb8
RR
1409 x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
1410 x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
241771ef 1411 }
948b1bb8
RR
1412 x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
1413 perf_max_events = x86_pmu.num_counters;
241771ef 1414
948b1bb8 1415 if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
cdd6c482 1416 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
948b1bb8
RR
1417 x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
1418 x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
703e937c 1419 }
862a1a5f 1420
d6dc0b4e 1421 x86_pmu.intel_ctrl |=
948b1bb8 1422 ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
241771ef 1423
cdd6c482
IM
1424 perf_events_lapic_init();
1425 register_die_notifier(&perf_event_nmi_notifier);
1123e3ad 1426
63b14649 1427 unconstrained = (struct event_constraint)
948b1bb8
RR
1428 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_counters) - 1,
1429 0, x86_pmu.num_counters);
63b14649 1430
b622d644
PZ
1431 if (x86_pmu.event_constraints) {
1432 for_each_event_constraint(c, x86_pmu.event_constraints) {
a098f448 1433 if (c->cmask != X86_RAW_EVENT_MASK)
b622d644
PZ
1434 continue;
1435
948b1bb8
RR
1436 c->idxmsk64 |= (1ULL << x86_pmu.num_counters) - 1;
1437 c->weight += x86_pmu.num_counters;
b622d644
PZ
1438 }
1439 }
1440
57c0c15b 1441 pr_info("... version: %d\n", x86_pmu.version);
948b1bb8
RR
1442 pr_info("... bit width: %d\n", x86_pmu.cntval_bits);
1443 pr_info("... generic registers: %d\n", x86_pmu.num_counters);
1444 pr_info("... value mask: %016Lx\n", x86_pmu.cntval_mask);
57c0c15b 1445 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
948b1bb8 1446 pr_info("... fixed-purpose events: %d\n", x86_pmu.num_counters_fixed);
d6dc0b4e 1447 pr_info("... event mask: %016Lx\n", x86_pmu.intel_ctrl);
3f6da390
PZ
1448
1449 perf_cpu_notifier(x86_pmu_notifier);
241771ef 1450}
621a01ea 1451
cdd6c482 1452static inline void x86_pmu_read(struct perf_event *event)
ee06094f 1453{
cc2ad4ba 1454 x86_perf_event_update(event);
ee06094f
IM
1455}
1456
4aeb0b42
RR
1457static const struct pmu pmu = {
1458 .enable = x86_pmu_enable,
1459 .disable = x86_pmu_disable,
d76a0812
SE
1460 .start = x86_pmu_start,
1461 .stop = x86_pmu_stop,
4aeb0b42 1462 .read = x86_pmu_read,
a78ac325 1463 .unthrottle = x86_pmu_unthrottle,
621a01ea
IM
1464};
1465
ca037701
PZ
1466/*
1467 * validate that we can schedule this event
1468 */
1469static int validate_event(struct perf_event *event)
1470{
1471 struct cpu_hw_events *fake_cpuc;
1472 struct event_constraint *c;
1473 int ret = 0;
1474
1475 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
1476 if (!fake_cpuc)
1477 return -ENOMEM;
1478
1479 c = x86_pmu.get_event_constraints(fake_cpuc, event);
1480
1481 if (!c || !c->weight)
1482 ret = -ENOSPC;
1483
1484 if (x86_pmu.put_event_constraints)
1485 x86_pmu.put_event_constraints(fake_cpuc, event);
1486
1487 kfree(fake_cpuc);
1488
1489 return ret;
1490}
1491
1da53e02
SE
1492/*
1493 * validate a single event group
1494 *
1495 * validation include:
184f412c
IM
1496 * - check events are compatible which each other
1497 * - events do not compete for the same counter
1498 * - number of events <= number of counters
1da53e02
SE
1499 *
1500 * validation ensures the group can be loaded onto the
1501 * PMU if it was the only group available.
1502 */
fe9081cc
PZ
1503static int validate_group(struct perf_event *event)
1504{
1da53e02 1505 struct perf_event *leader = event->group_leader;
502568d5
PZ
1506 struct cpu_hw_events *fake_cpuc;
1507 int ret, n;
fe9081cc 1508
502568d5
PZ
1509 ret = -ENOMEM;
1510 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
1511 if (!fake_cpuc)
1512 goto out;
fe9081cc 1513
1da53e02
SE
1514 /*
1515 * the event is not yet connected with its
1516 * siblings therefore we must first collect
1517 * existing siblings, then add the new event
1518 * before we can simulate the scheduling
1519 */
502568d5
PZ
1520 ret = -ENOSPC;
1521 n = collect_events(fake_cpuc, leader, true);
1da53e02 1522 if (n < 0)
502568d5 1523 goto out_free;
fe9081cc 1524
502568d5
PZ
1525 fake_cpuc->n_events = n;
1526 n = collect_events(fake_cpuc, event, false);
1da53e02 1527 if (n < 0)
502568d5 1528 goto out_free;
fe9081cc 1529
502568d5 1530 fake_cpuc->n_events = n;
1da53e02 1531
a072738e 1532 ret = x86_pmu.schedule_events(fake_cpuc, n, NULL);
502568d5
PZ
1533
1534out_free:
1535 kfree(fake_cpuc);
1536out:
1537 return ret;
fe9081cc
PZ
1538}
1539
cdd6c482 1540const struct pmu *hw_perf_event_init(struct perf_event *event)
621a01ea 1541{
8113070d 1542 const struct pmu *tmp;
621a01ea
IM
1543 int err;
1544
cdd6c482 1545 err = __hw_perf_event_init(event);
fe9081cc 1546 if (!err) {
8113070d
SE
1547 /*
1548 * we temporarily connect event to its pmu
1549 * such that validate_group() can classify
1550 * it as an x86 event using is_x86_event()
1551 */
1552 tmp = event->pmu;
1553 event->pmu = &pmu;
1554
fe9081cc
PZ
1555 if (event->group_leader != event)
1556 err = validate_group(event);
ca037701
PZ
1557 else
1558 err = validate_event(event);
8113070d
SE
1559
1560 event->pmu = tmp;
fe9081cc 1561 }
a1792cda 1562 if (err) {
cdd6c482
IM
1563 if (event->destroy)
1564 event->destroy(event);
9ea98e19 1565 return ERR_PTR(err);
a1792cda 1566 }
621a01ea 1567
4aeb0b42 1568 return &pmu;
621a01ea 1569}
d7d59fb3
PZ
1570
1571/*
1572 * callchain support
1573 */
1574
1575static inline
f9188e02 1576void callchain_store(struct perf_callchain_entry *entry, u64 ip)
d7d59fb3 1577{
f9188e02 1578 if (entry->nr < PERF_MAX_STACK_DEPTH)
d7d59fb3
PZ
1579 entry->ip[entry->nr++] = ip;
1580}
1581
245b2e70
TH
1582static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
1583static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry);
d7d59fb3
PZ
1584
1585
1586static void
1587backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1588{
1589 /* Ignore warnings */
1590}
1591
1592static void backtrace_warning(void *data, char *msg)
1593{
1594 /* Ignore warnings */
1595}
1596
1597static int backtrace_stack(void *data, char *name)
1598{
038e836e 1599 return 0;
d7d59fb3
PZ
1600}
1601
1602static void backtrace_address(void *data, unsigned long addr, int reliable)
1603{
1604 struct perf_callchain_entry *entry = data;
1605
6f4dee06 1606 callchain_store(entry, addr);
d7d59fb3
PZ
1607}
1608
1609static const struct stacktrace_ops backtrace_ops = {
1610 .warning = backtrace_warning,
1611 .warning_symbol = backtrace_warning_symbol,
1612 .stack = backtrace_stack,
1613 .address = backtrace_address,
06d65bda 1614 .walk_stack = print_context_stack_bp,
d7d59fb3
PZ
1615};
1616
038e836e
IM
1617#include "../dumpstack.h"
1618
d7d59fb3
PZ
1619static void
1620perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1621{
f9188e02 1622 callchain_store(entry, PERF_CONTEXT_KERNEL);
038e836e 1623 callchain_store(entry, regs->ip);
d7d59fb3 1624
48b5ba9c 1625 dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
d7d59fb3
PZ
1626}
1627
257ef9d2
TE
1628#ifdef CONFIG_COMPAT
1629static inline int
1630perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
74193ef0 1631{
257ef9d2
TE
1632 /* 32-bit process in 64-bit kernel. */
1633 struct stack_frame_ia32 frame;
1634 const void __user *fp;
74193ef0 1635
257ef9d2
TE
1636 if (!test_thread_flag(TIF_IA32))
1637 return 0;
1638
1639 fp = compat_ptr(regs->bp);
1640 while (entry->nr < PERF_MAX_STACK_DEPTH) {
1641 unsigned long bytes;
1642 frame.next_frame = 0;
1643 frame.return_address = 0;
1644
1645 bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
1646 if (bytes != sizeof(frame))
1647 break;
74193ef0 1648
257ef9d2
TE
1649 if (fp < compat_ptr(regs->sp))
1650 break;
74193ef0 1651
257ef9d2
TE
1652 callchain_store(entry, frame.return_address);
1653 fp = compat_ptr(frame.next_frame);
1654 }
1655 return 1;
d7d59fb3 1656}
257ef9d2
TE
1657#else
1658static inline int
1659perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
1660{
1661 return 0;
1662}
1663#endif
d7d59fb3
PZ
1664
1665static void
1666perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1667{
1668 struct stack_frame frame;
1669 const void __user *fp;
1670
5a6cec3a
IM
1671 if (!user_mode(regs))
1672 regs = task_pt_regs(current);
1673
74193ef0 1674 fp = (void __user *)regs->bp;
d7d59fb3 1675
f9188e02 1676 callchain_store(entry, PERF_CONTEXT_USER);
d7d59fb3
PZ
1677 callchain_store(entry, regs->ip);
1678
257ef9d2
TE
1679 if (perf_callchain_user32(regs, entry))
1680 return;
1681
f9188e02 1682 while (entry->nr < PERF_MAX_STACK_DEPTH) {
257ef9d2 1683 unsigned long bytes;
038e836e 1684 frame.next_frame = NULL;
d7d59fb3
PZ
1685 frame.return_address = 0;
1686
257ef9d2
TE
1687 bytes = copy_from_user_nmi(&frame, fp, sizeof(frame));
1688 if (bytes != sizeof(frame))
d7d59fb3
PZ
1689 break;
1690
5a6cec3a 1691 if ((unsigned long)fp < regs->sp)
d7d59fb3
PZ
1692 break;
1693
1694 callchain_store(entry, frame.return_address);
038e836e 1695 fp = frame.next_frame;
d7d59fb3
PZ
1696 }
1697}
1698
1699static void
1700perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1701{
1702 int is_user;
1703
1704 if (!regs)
1705 return;
1706
1707 is_user = user_mode(regs);
1708
d7d59fb3
PZ
1709 if (is_user && current->state != TASK_RUNNING)
1710 return;
1711
1712 if (!is_user)
1713 perf_callchain_kernel(regs, entry);
1714
1715 if (current->mm)
1716 perf_callchain_user(regs, entry);
1717}
1718
1719struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1720{
1721 struct perf_callchain_entry *entry;
1722
1723 if (in_nmi())
245b2e70 1724 entry = &__get_cpu_var(pmc_nmi_entry);
d7d59fb3 1725 else
245b2e70 1726 entry = &__get_cpu_var(pmc_irq_entry);
d7d59fb3
PZ
1727
1728 entry->nr = 0;
1729
1730 perf_do_callchain(regs, entry);
1731
1732 return entry;
1733}
5331d7b8
FW
1734
1735void perf_arch_fetch_caller_regs(struct pt_regs *regs, unsigned long ip, int skip)
1736{
1737 regs->ip = ip;
1738 /*
1739 * perf_arch_fetch_caller_regs adds another call, we need to increment
1740 * the skip level
1741 */
1742 regs->bp = rewind_frame_pointer(skip + 1);
1743 regs->cs = __KERNEL_CS;
1744 local_save_flags(regs->flags);
1745}