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