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