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