2 * Performance events x86 architecture code
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>
9 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
10 * Copyright (C) 2009 Google, Inc., Stephane Eranian
12 * For licencing details see kernel-base/COPYING
15 #include <linux/perf_event.h>
16 #include <linux/capability.h>
17 #include <linux/notifier.h>
18 #include <linux/hardirq.h>
19 #include <linux/kprobes.h>
20 #include <linux/module.h>
21 #include <linux/kdebug.h>
22 #include <linux/sched.h>
23 #include <linux/uaccess.h>
24 #include <linux/highmem.h>
25 #include <linux/cpu.h>
26 #include <linux/bitops.h>
29 #include <asm/stacktrace.h>
34 #define wrmsrl(msr, val) \
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)); \
44 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
47 copy_from_user_nmi(void *to
, const void __user
*from
, unsigned long n
)
49 unsigned long offset
, addr
= (unsigned long)from
;
50 int type
= in_nmi() ? KM_NMI
: KM_IRQ0
;
51 unsigned long size
, len
= 0;
57 ret
= __get_user_pages_fast(addr
, 1, 0, &page
);
61 offset
= addr
& (PAGE_SIZE
- 1);
62 size
= min(PAGE_SIZE
- offset
, n
- len
);
64 map
= kmap_atomic(page
, type
);
65 memcpy(to
, map
+offset
, size
);
66 kunmap_atomic(map
, type
);
78 static u64 perf_event_mask __read_mostly
;
80 struct event_constraint
{
82 unsigned long idxmsk
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
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
];
97 #define MAX_LBR_ENTRIES 16
99 struct cpu_hw_events
{
101 * Generic x86 PMC bits
103 struct perf_event
*events
[X86_PMC_IDX_MAX
]; /* in counter order */
104 unsigned long active_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
109 int assign
[X86_PMC_IDX_MAX
]; /* event to counter assignment */
110 u64 tags
[X86_PMC_IDX_MAX
];
111 struct perf_event
*event_list
[X86_PMC_IDX_MAX
]; /* in enabled order */
114 * Intel DebugStore bits
116 struct debug_store
*ds
;
124 struct perf_branch_stack lbr_stack
;
125 struct perf_branch_entry lbr_entries
[MAX_LBR_ENTRIES
];
130 struct amd_nb
*amd_nb
;
133 #define __EVENT_CONSTRAINT(c, n, m, w) {\
134 { .idxmsk64 = (n) }, \
140 #define EVENT_CONSTRAINT(c, n, m) \
141 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
144 * Constraint on the Event code.
146 #define INTEL_EVENT_CONSTRAINT(c, n) \
147 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVTSEL_MASK)
150 * Constraint on the Event code + UMask + fixed-mask
152 #define FIXED_EVENT_CONSTRAINT(c, n) \
153 EVENT_CONSTRAINT(c, (1ULL << (32+n)), INTEL_ARCH_FIXED_MASK)
156 * Constraint on the Event code + UMask
158 #define PEBS_EVENT_CONSTRAINT(c, n) \
159 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
161 #define EVENT_CONSTRAINT_END \
162 EVENT_CONSTRAINT(0, 0, 0)
164 #define for_each_event_constraint(e, c) \
165 for ((e) = (c); (e)->cmask; (e)++)
167 union perf_capabilities
{
171 u64 pebs_arch_reg
: 1;
179 * struct x86_pmu - generic x86 pmu
183 * Generic x86 PMC bits
187 int (*handle_irq
)(struct pt_regs
*);
188 void (*disable_all
)(void);
189 void (*enable_all
)(void);
190 void (*enable
)(struct perf_event
*);
191 void (*disable
)(struct perf_event
*);
192 int (*hw_config
)(struct perf_event_attr
*attr
, struct hw_perf_event
*hwc
);
193 int (*schedule_events
)(struct cpu_hw_events
*cpuc
, int n
, int *assign
);
196 u64 (*event_map
)(int);
197 u64 (*raw_event
)(u64
);
200 int num_events_fixed
;
205 struct event_constraint
*
206 (*get_event_constraints
)(struct cpu_hw_events
*cpuc
,
207 struct perf_event
*event
);
209 void (*put_event_constraints
)(struct cpu_hw_events
*cpuc
,
210 struct perf_event
*event
);
211 struct event_constraint
*event_constraints
;
212 void (*quirks
)(void);
214 void (*cpu_prepare
)(int cpu
);
215 void (*cpu_starting
)(int cpu
);
216 void (*cpu_dying
)(int cpu
);
217 void (*cpu_dead
)(int cpu
);
220 * Intel Arch Perfmon v2+
223 union perf_capabilities intel_cap
;
226 * Intel DebugStore bits
229 int pebs_record_size
;
230 void (*drain_pebs
)(struct pt_regs
*regs
);
231 struct event_constraint
*pebs_constraints
;
236 unsigned long lbr_tos
, lbr_from
, lbr_to
; /* MSR base regs */
237 int lbr_nr
; /* hardware stack size */
240 static struct x86_pmu x86_pmu __read_mostly
;
242 static DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
) = {
246 static int x86_perf_event_set_period(struct perf_event
*event
);
249 * Generalized hw caching related hw_event table, filled
250 * in on a per model basis. A value of 0 means
251 * 'not supported', -1 means 'hw_event makes no sense on
252 * this CPU', any other value means the raw hw_event
256 #define C(x) PERF_COUNT_HW_CACHE_##x
258 static u64 __read_mostly hw_cache_event_ids
259 [PERF_COUNT_HW_CACHE_MAX
]
260 [PERF_COUNT_HW_CACHE_OP_MAX
]
261 [PERF_COUNT_HW_CACHE_RESULT_MAX
];
264 * Propagate event elapsed time into the generic event.
265 * Can only be executed on the CPU where the event is active.
266 * Returns the delta events processed.
269 x86_perf_event_update(struct perf_event
*event
)
271 struct hw_perf_event
*hwc
= &event
->hw
;
272 int shift
= 64 - x86_pmu
.event_bits
;
273 u64 prev_raw_count
, new_raw_count
;
277 if (idx
== X86_PMC_IDX_FIXED_BTS
)
281 * Careful: an NMI might modify the previous event value.
283 * Our tactic to handle this is to first atomically read and
284 * exchange a new raw count - then add that new-prev delta
285 * count to the generic event atomically:
288 prev_raw_count
= atomic64_read(&hwc
->prev_count
);
289 rdmsrl(hwc
->event_base
+ idx
, new_raw_count
);
291 if (atomic64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
292 new_raw_count
) != prev_raw_count
)
296 * Now we have the new raw value and have updated the prev
297 * timestamp already. We can now calculate the elapsed delta
298 * (event-)time and add that to the generic event.
300 * Careful, not all hw sign-extends above the physical width
303 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
306 atomic64_add(delta
, &event
->count
);
307 atomic64_sub(delta
, &hwc
->period_left
);
309 return new_raw_count
;
312 static atomic_t active_events
;
313 static DEFINE_MUTEX(pmc_reserve_mutex
);
315 #ifdef CONFIG_X86_LOCAL_APIC
317 static bool reserve_pmc_hardware(void)
321 if (nmi_watchdog
== NMI_LOCAL_APIC
)
322 disable_lapic_nmi_watchdog();
324 for (i
= 0; i
< x86_pmu
.num_events
; i
++) {
325 if (!reserve_perfctr_nmi(x86_pmu
.perfctr
+ i
))
329 for (i
= 0; i
< x86_pmu
.num_events
; i
++) {
330 if (!reserve_evntsel_nmi(x86_pmu
.eventsel
+ i
))
337 for (i
--; i
>= 0; i
--)
338 release_evntsel_nmi(x86_pmu
.eventsel
+ i
);
340 i
= x86_pmu
.num_events
;
343 for (i
--; i
>= 0; i
--)
344 release_perfctr_nmi(x86_pmu
.perfctr
+ i
);
346 if (nmi_watchdog
== NMI_LOCAL_APIC
)
347 enable_lapic_nmi_watchdog();
352 static void release_pmc_hardware(void)
356 for (i
= 0; i
< x86_pmu
.num_events
; i
++) {
357 release_perfctr_nmi(x86_pmu
.perfctr
+ i
);
358 release_evntsel_nmi(x86_pmu
.eventsel
+ i
);
361 if (nmi_watchdog
== NMI_LOCAL_APIC
)
362 enable_lapic_nmi_watchdog();
367 static bool reserve_pmc_hardware(void) { return true; }
368 static void release_pmc_hardware(void) {}
372 static int reserve_ds_buffers(void);
373 static void release_ds_buffers(void);
375 static void hw_perf_event_destroy(struct perf_event
*event
)
377 if (atomic_dec_and_mutex_lock(&active_events
, &pmc_reserve_mutex
)) {
378 release_pmc_hardware();
379 release_ds_buffers();
380 mutex_unlock(&pmc_reserve_mutex
);
384 static inline int x86_pmu_initialized(void)
386 return x86_pmu
.handle_irq
!= NULL
;
390 set_ext_hw_attr(struct hw_perf_event
*hwc
, struct perf_event_attr
*attr
)
392 unsigned int cache_type
, cache_op
, cache_result
;
395 config
= attr
->config
;
397 cache_type
= (config
>> 0) & 0xff;
398 if (cache_type
>= PERF_COUNT_HW_CACHE_MAX
)
401 cache_op
= (config
>> 8) & 0xff;
402 if (cache_op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
405 cache_result
= (config
>> 16) & 0xff;
406 if (cache_result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
409 val
= hw_cache_event_ids
[cache_type
][cache_op
][cache_result
];
422 static int x86_hw_config(struct perf_event_attr
*attr
, struct hw_perf_event
*hwc
)
426 * (keep 'enabled' bit clear for now)
428 hwc
->config
= ARCH_PERFMON_EVENTSEL_INT
;
431 * Count user and OS events unless requested not to
433 if (!attr
->exclude_user
)
434 hwc
->config
|= ARCH_PERFMON_EVENTSEL_USR
;
435 if (!attr
->exclude_kernel
)
436 hwc
->config
|= ARCH_PERFMON_EVENTSEL_OS
;
442 * Setup the hardware configuration for a given attr_type
444 static int __hw_perf_event_init(struct perf_event
*event
)
446 struct perf_event_attr
*attr
= &event
->attr
;
447 struct hw_perf_event
*hwc
= &event
->hw
;
451 if (!x86_pmu_initialized())
455 if (!atomic_inc_not_zero(&active_events
)) {
456 mutex_lock(&pmc_reserve_mutex
);
457 if (atomic_read(&active_events
) == 0) {
458 if (!reserve_pmc_hardware())
461 err
= reserve_ds_buffers();
464 atomic_inc(&active_events
);
465 mutex_unlock(&pmc_reserve_mutex
);
470 event
->destroy
= hw_perf_event_destroy
;
474 hwc
->last_tag
= ~0ULL;
476 /* Processor specifics */
477 err
= x86_pmu
.hw_config(attr
, hwc
);
481 if (!hwc
->sample_period
) {
482 hwc
->sample_period
= x86_pmu
.max_period
;
483 hwc
->last_period
= hwc
->sample_period
;
484 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
487 * If we have a PMU initialized but no APIC
488 * interrupts, we cannot sample hardware
489 * events (user-space has to fall back and
490 * sample via a hrtimer based software event):
497 * Raw hw_event type provide the config in the hw_event structure
499 if (attr
->type
== PERF_TYPE_RAW
) {
500 hwc
->config
|= x86_pmu
.raw_event(attr
->config
);
501 if ((hwc
->config
& ARCH_PERFMON_EVENTSEL_ANY
) &&
502 perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN
))
507 if (attr
->type
== PERF_TYPE_HW_CACHE
)
508 return set_ext_hw_attr(hwc
, attr
);
510 if (attr
->config
>= x86_pmu
.max_events
)
516 config
= x86_pmu
.event_map(attr
->config
);
527 if ((attr
->config
== PERF_COUNT_HW_BRANCH_INSTRUCTIONS
) &&
528 (hwc
->sample_period
== 1)) {
529 /* BTS is not supported by this architecture. */
533 /* BTS is currently only allowed for user-mode. */
534 if (!attr
->exclude_kernel
)
538 hwc
->config
|= config
;
543 static void x86_pmu_disable_all(void)
545 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
548 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
551 if (!test_bit(idx
, cpuc
->active_mask
))
553 rdmsrl(x86_pmu
.eventsel
+ idx
, val
);
554 if (!(val
& ARCH_PERFMON_EVENTSEL_ENABLE
))
556 val
&= ~ARCH_PERFMON_EVENTSEL_ENABLE
;
557 wrmsrl(x86_pmu
.eventsel
+ idx
, val
);
561 void hw_perf_disable(void)
563 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
565 if (!x86_pmu_initialized())
575 x86_pmu
.disable_all();
578 static void x86_pmu_enable_all(void)
580 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
583 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
584 struct perf_event
*event
= cpuc
->events
[idx
];
587 if (!test_bit(idx
, cpuc
->active_mask
))
590 val
= event
->hw
.config
;
591 val
|= ARCH_PERFMON_EVENTSEL_ENABLE
;
592 wrmsrl(x86_pmu
.eventsel
+ idx
, val
);
596 static const struct pmu pmu
;
598 static inline int is_x86_event(struct perf_event
*event
)
600 return event
->pmu
== &pmu
;
603 static int x86_schedule_events(struct cpu_hw_events
*cpuc
, int n
, int *assign
)
605 struct event_constraint
*c
, *constraints
[X86_PMC_IDX_MAX
];
606 unsigned long used_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
607 int i
, j
, w
, wmax
, num
= 0;
608 struct hw_perf_event
*hwc
;
610 bitmap_zero(used_mask
, X86_PMC_IDX_MAX
);
612 for (i
= 0; i
< n
; i
++) {
613 c
= x86_pmu
.get_event_constraints(cpuc
, cpuc
->event_list
[i
]);
618 * fastpath, try to reuse previous register
620 for (i
= 0; i
< n
; i
++) {
621 hwc
= &cpuc
->event_list
[i
]->hw
;
628 /* constraint still honored */
629 if (!test_bit(hwc
->idx
, c
->idxmsk
))
632 /* not already used */
633 if (test_bit(hwc
->idx
, used_mask
))
636 __set_bit(hwc
->idx
, used_mask
);
638 assign
[i
] = hwc
->idx
;
647 bitmap_zero(used_mask
, X86_PMC_IDX_MAX
);
650 * weight = number of possible counters
652 * 1 = most constrained, only works on one counter
653 * wmax = least constrained, works on any counter
655 * assign events to counters starting with most
656 * constrained events.
658 wmax
= x86_pmu
.num_events
;
661 * when fixed event counters are present,
662 * wmax is incremented by 1 to account
663 * for one more choice
665 if (x86_pmu
.num_events_fixed
)
668 for (w
= 1, num
= n
; num
&& w
<= wmax
; w
++) {
670 for (i
= 0; num
&& i
< n
; i
++) {
672 hwc
= &cpuc
->event_list
[i
]->hw
;
677 for_each_set_bit(j
, c
->idxmsk
, X86_PMC_IDX_MAX
) {
678 if (!test_bit(j
, used_mask
))
682 if (j
== X86_PMC_IDX_MAX
)
685 __set_bit(j
, used_mask
);
694 * scheduling failed or is just a simulation,
695 * free resources if necessary
697 if (!assign
|| num
) {
698 for (i
= 0; i
< n
; i
++) {
699 if (x86_pmu
.put_event_constraints
)
700 x86_pmu
.put_event_constraints(cpuc
, cpuc
->event_list
[i
]);
703 return num
? -ENOSPC
: 0;
707 * dogrp: true if must collect siblings events (group)
708 * returns total number of events and error code
710 static int collect_events(struct cpu_hw_events
*cpuc
, struct perf_event
*leader
, bool dogrp
)
712 struct perf_event
*event
;
715 max_count
= x86_pmu
.num_events
+ x86_pmu
.num_events_fixed
;
717 /* current number of events already accepted */
720 if (is_x86_event(leader
)) {
723 cpuc
->event_list
[n
] = leader
;
729 list_for_each_entry(event
, &leader
->sibling_list
, group_entry
) {
730 if (!is_x86_event(event
) ||
731 event
->state
<= PERF_EVENT_STATE_OFF
)
737 cpuc
->event_list
[n
] = event
;
743 static inline void x86_assign_hw_event(struct perf_event
*event
,
744 struct cpu_hw_events
*cpuc
, int i
)
746 struct hw_perf_event
*hwc
= &event
->hw
;
748 hwc
->idx
= cpuc
->assign
[i
];
749 hwc
->last_cpu
= smp_processor_id();
750 hwc
->last_tag
= ++cpuc
->tags
[i
];
752 if (hwc
->idx
== X86_PMC_IDX_FIXED_BTS
) {
753 hwc
->config_base
= 0;
755 } else if (hwc
->idx
>= X86_PMC_IDX_FIXED
) {
756 hwc
->config_base
= MSR_ARCH_PERFMON_FIXED_CTR_CTRL
;
758 * We set it so that event_base + idx in wrmsr/rdmsr maps to
759 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
762 MSR_ARCH_PERFMON_FIXED_CTR0
- X86_PMC_IDX_FIXED
;
764 hwc
->config_base
= x86_pmu
.eventsel
;
765 hwc
->event_base
= x86_pmu
.perfctr
;
769 static inline int match_prev_assignment(struct hw_perf_event
*hwc
,
770 struct cpu_hw_events
*cpuc
,
773 return hwc
->idx
== cpuc
->assign
[i
] &&
774 hwc
->last_cpu
== smp_processor_id() &&
775 hwc
->last_tag
== cpuc
->tags
[i
];
778 static int x86_pmu_start(struct perf_event
*event
);
779 static void x86_pmu_stop(struct perf_event
*event
);
781 void hw_perf_enable(void)
783 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
784 struct perf_event
*event
;
785 struct hw_perf_event
*hwc
;
788 if (!x86_pmu_initialized())
795 int n_running
= cpuc
->n_events
- cpuc
->n_added
;
797 * apply assignment obtained either from
798 * hw_perf_group_sched_in() or x86_pmu_enable()
800 * step1: save events moving to new counters
801 * step2: reprogram moved events into new counters
803 for (i
= 0; i
< n_running
; i
++) {
804 event
= cpuc
->event_list
[i
];
808 * we can avoid reprogramming counter if:
809 * - assigned same counter as last time
810 * - running on same CPU as last time
811 * - no other event has used the counter since
813 if (hwc
->idx
== -1 ||
814 match_prev_assignment(hwc
, cpuc
, i
))
820 for (i
= 0; i
< cpuc
->n_events
; i
++) {
821 event
= cpuc
->event_list
[i
];
824 if (!match_prev_assignment(hwc
, cpuc
, i
))
825 x86_assign_hw_event(event
, cpuc
, i
);
826 else if (i
< n_running
)
829 x86_pmu_start(event
);
832 perf_events_lapic_init();
838 x86_pmu
.enable_all();
841 static inline void __x86_pmu_enable_event(struct hw_perf_event
*hwc
)
843 wrmsrl(hwc
->config_base
+ hwc
->idx
,
844 hwc
->config
| ARCH_PERFMON_EVENTSEL_ENABLE
);
847 static inline void x86_pmu_disable_event(struct perf_event
*event
)
849 struct hw_perf_event
*hwc
= &event
->hw
;
851 wrmsrl(hwc
->config_base
+ hwc
->idx
, hwc
->config
);
854 static DEFINE_PER_CPU(u64
[X86_PMC_IDX_MAX
], pmc_prev_left
);
857 * Set the next IRQ period, based on the hwc->period_left value.
858 * To be called with the event disabled in hw:
861 x86_perf_event_set_period(struct perf_event
*event
)
863 struct hw_perf_event
*hwc
= &event
->hw
;
864 s64 left
= atomic64_read(&hwc
->period_left
);
865 s64 period
= hwc
->sample_period
;
866 int ret
= 0, idx
= hwc
->idx
;
868 if (idx
== X86_PMC_IDX_FIXED_BTS
)
872 * If we are way outside a reasonable range then just skip forward:
874 if (unlikely(left
<= -period
)) {
876 atomic64_set(&hwc
->period_left
, left
);
877 hwc
->last_period
= period
;
881 if (unlikely(left
<= 0)) {
883 atomic64_set(&hwc
->period_left
, left
);
884 hwc
->last_period
= period
;
888 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
890 if (unlikely(left
< 2))
893 if (left
> x86_pmu
.max_period
)
894 left
= x86_pmu
.max_period
;
896 per_cpu(pmc_prev_left
[idx
], smp_processor_id()) = left
;
899 * The hw event starts counting from this event offset,
900 * mark it to be able to extra future deltas:
902 atomic64_set(&hwc
->prev_count
, (u64
)-left
);
904 wrmsrl(hwc
->event_base
+ idx
,
905 (u64
)(-left
) & x86_pmu
.event_mask
);
907 perf_event_update_userpage(event
);
912 static void x86_pmu_enable_event(struct perf_event
*event
)
914 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
916 __x86_pmu_enable_event(&event
->hw
);
920 * activate a single event
922 * The event is added to the group of enabled events
923 * but only if it can be scehduled with existing events.
925 * Called with PMU disabled. If successful and return value 1,
926 * then guaranteed to call perf_enable() and hw_perf_enable()
928 static int x86_pmu_enable(struct perf_event
*event
)
930 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
931 struct hw_perf_event
*hwc
;
932 int assign
[X86_PMC_IDX_MAX
];
938 n
= collect_events(cpuc
, event
, false);
942 ret
= x86_pmu
.schedule_events(cpuc
, n
, assign
);
946 * copy new assignment, now we know it is possible
947 * will be used by hw_perf_enable()
949 memcpy(cpuc
->assign
, assign
, n
*sizeof(int));
952 cpuc
->n_added
+= n
- n0
;
957 static int x86_pmu_start(struct perf_event
*event
)
959 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
960 int idx
= event
->hw
.idx
;
965 x86_perf_event_set_period(event
);
966 cpuc
->events
[idx
] = event
;
967 __set_bit(idx
, cpuc
->active_mask
);
968 x86_pmu
.enable(event
);
969 perf_event_update_userpage(event
);
974 static void x86_pmu_unthrottle(struct perf_event
*event
)
976 int ret
= x86_pmu_start(event
);
980 void perf_event_print_debug(void)
982 u64 ctrl
, status
, overflow
, pmc_ctrl
, pmc_count
, prev_left
, fixed
;
984 struct cpu_hw_events
*cpuc
;
988 if (!x86_pmu
.num_events
)
991 local_irq_save(flags
);
993 cpu
= smp_processor_id();
994 cpuc
= &per_cpu(cpu_hw_events
, cpu
);
996 if (x86_pmu
.version
>= 2) {
997 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, ctrl
);
998 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS
, status
);
999 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL
, overflow
);
1000 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL
, fixed
);
1001 rdmsrl(MSR_IA32_PEBS_ENABLE
, pebs
);
1004 pr_info("CPU#%d: ctrl: %016llx\n", cpu
, ctrl
);
1005 pr_info("CPU#%d: status: %016llx\n", cpu
, status
);
1006 pr_info("CPU#%d: overflow: %016llx\n", cpu
, overflow
);
1007 pr_info("CPU#%d: fixed: %016llx\n", cpu
, fixed
);
1008 pr_info("CPU#%d: pebs: %016llx\n", cpu
, pebs
);
1010 pr_info("CPU#%d: active: %016llx\n", cpu
, *(u64
*)cpuc
->active_mask
);
1012 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
1013 rdmsrl(x86_pmu
.eventsel
+ idx
, pmc_ctrl
);
1014 rdmsrl(x86_pmu
.perfctr
+ idx
, pmc_count
);
1016 prev_left
= per_cpu(pmc_prev_left
[idx
], cpu
);
1018 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
1019 cpu
, idx
, pmc_ctrl
);
1020 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
1021 cpu
, idx
, pmc_count
);
1022 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
1023 cpu
, idx
, prev_left
);
1025 for (idx
= 0; idx
< x86_pmu
.num_events_fixed
; idx
++) {
1026 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0
+ idx
, pmc_count
);
1028 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
1029 cpu
, idx
, pmc_count
);
1031 local_irq_restore(flags
);
1034 static void x86_pmu_stop(struct perf_event
*event
)
1036 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1037 struct hw_perf_event
*hwc
= &event
->hw
;
1040 if (!__test_and_clear_bit(idx
, cpuc
->active_mask
))
1043 x86_pmu
.disable(event
);
1046 * Drain the remaining delta count out of a event
1047 * that we are disabling:
1049 x86_perf_event_update(event
);
1051 cpuc
->events
[idx
] = NULL
;
1054 static void x86_pmu_disable(struct perf_event
*event
)
1056 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1059 x86_pmu_stop(event
);
1061 for (i
= 0; i
< cpuc
->n_events
; i
++) {
1062 if (event
== cpuc
->event_list
[i
]) {
1064 if (x86_pmu
.put_event_constraints
)
1065 x86_pmu
.put_event_constraints(cpuc
, event
);
1067 while (++i
< cpuc
->n_events
)
1068 cpuc
->event_list
[i
-1] = cpuc
->event_list
[i
];
1074 perf_event_update_userpage(event
);
1077 static int x86_pmu_handle_irq(struct pt_regs
*regs
)
1079 struct perf_sample_data data
;
1080 struct cpu_hw_events
*cpuc
;
1081 struct perf_event
*event
;
1082 struct hw_perf_event
*hwc
;
1083 int idx
, handled
= 0;
1086 perf_sample_data_init(&data
, 0);
1088 cpuc
= &__get_cpu_var(cpu_hw_events
);
1090 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
1091 if (!test_bit(idx
, cpuc
->active_mask
))
1094 event
= cpuc
->events
[idx
];
1097 val
= x86_perf_event_update(event
);
1098 if (val
& (1ULL << (x86_pmu
.event_bits
- 1)))
1105 data
.period
= event
->hw
.last_period
;
1107 if (!x86_perf_event_set_period(event
))
1110 if (perf_event_overflow(event
, 1, &data
, regs
))
1111 x86_pmu_stop(event
);
1115 inc_irq_stat(apic_perf_irqs
);
1120 void smp_perf_pending_interrupt(struct pt_regs
*regs
)
1124 inc_irq_stat(apic_pending_irqs
);
1125 perf_event_do_pending();
1129 void set_perf_event_pending(void)
1131 #ifdef CONFIG_X86_LOCAL_APIC
1132 if (!x86_pmu
.apic
|| !x86_pmu_initialized())
1135 apic
->send_IPI_self(LOCAL_PENDING_VECTOR
);
1139 void perf_events_lapic_init(void)
1141 #ifdef CONFIG_X86_LOCAL_APIC
1142 if (!x86_pmu
.apic
|| !x86_pmu_initialized())
1146 * Always use NMI for PMU
1148 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1152 static int __kprobes
1153 perf_event_nmi_handler(struct notifier_block
*self
,
1154 unsigned long cmd
, void *__args
)
1156 struct die_args
*args
= __args
;
1157 struct pt_regs
*regs
;
1159 if (!atomic_read(&active_events
))
1173 #ifdef CONFIG_X86_LOCAL_APIC
1174 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1177 * Can't rely on the handled return value to say it was our NMI, two
1178 * events could trigger 'simultaneously' raising two back-to-back NMIs.
1180 * If the first NMI handles both, the latter will be empty and daze
1183 x86_pmu
.handle_irq(regs
);
1188 static __read_mostly
struct notifier_block perf_event_nmi_notifier
= {
1189 .notifier_call
= perf_event_nmi_handler
,
1194 static struct event_constraint unconstrained
;
1195 static struct event_constraint emptyconstraint
;
1197 static struct event_constraint
*
1198 x86_get_event_constraints(struct cpu_hw_events
*cpuc
, struct perf_event
*event
)
1200 struct event_constraint
*c
;
1202 if (x86_pmu
.event_constraints
) {
1203 for_each_event_constraint(c
, x86_pmu
.event_constraints
) {
1204 if ((event
->hw
.config
& c
->cmask
) == c
->code
)
1209 return &unconstrained
;
1212 static int x86_event_sched_in(struct perf_event
*event
,
1213 struct perf_cpu_context
*cpuctx
)
1217 event
->state
= PERF_EVENT_STATE_ACTIVE
;
1218 event
->oncpu
= smp_processor_id();
1219 event
->tstamp_running
+= event
->ctx
->time
- event
->tstamp_stopped
;
1221 if (!is_x86_event(event
))
1222 ret
= event
->pmu
->enable(event
);
1224 if (!ret
&& !is_software_event(event
))
1225 cpuctx
->active_oncpu
++;
1227 if (!ret
&& event
->attr
.exclusive
)
1228 cpuctx
->exclusive
= 1;
1233 static void x86_event_sched_out(struct perf_event
*event
,
1234 struct perf_cpu_context
*cpuctx
)
1236 event
->state
= PERF_EVENT_STATE_INACTIVE
;
1239 if (!is_x86_event(event
))
1240 event
->pmu
->disable(event
);
1242 event
->tstamp_running
-= event
->ctx
->time
- event
->tstamp_stopped
;
1244 if (!is_software_event(event
))
1245 cpuctx
->active_oncpu
--;
1247 if (event
->attr
.exclusive
|| !cpuctx
->active_oncpu
)
1248 cpuctx
->exclusive
= 0;
1252 * Called to enable a whole group of events.
1253 * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
1254 * Assumes the caller has disabled interrupts and has
1255 * frozen the PMU with hw_perf_save_disable.
1257 * called with PMU disabled. If successful and return value 1,
1258 * then guaranteed to call perf_enable() and hw_perf_enable()
1260 int hw_perf_group_sched_in(struct perf_event
*leader
,
1261 struct perf_cpu_context
*cpuctx
,
1262 struct perf_event_context
*ctx
)
1264 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
1265 struct perf_event
*sub
;
1266 int assign
[X86_PMC_IDX_MAX
];
1269 if (!x86_pmu_initialized())
1272 /* n0 = total number of events */
1273 n0
= collect_events(cpuc
, leader
, true);
1277 ret
= x86_pmu
.schedule_events(cpuc
, n0
, assign
);
1281 ret
= x86_event_sched_in(leader
, cpuctx
);
1286 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
1287 if (sub
->state
> PERF_EVENT_STATE_OFF
) {
1288 ret
= x86_event_sched_in(sub
, cpuctx
);
1295 * copy new assignment, now we know it is possible
1296 * will be used by hw_perf_enable()
1298 memcpy(cpuc
->assign
, assign
, n0
*sizeof(int));
1300 cpuc
->n_events
= n0
;
1301 cpuc
->n_added
+= n1
;
1302 ctx
->nr_active
+= n1
;
1305 * 1 means successful and events are active
1306 * This is not quite true because we defer
1307 * actual activation until hw_perf_enable() but
1308 * this way we* ensure caller won't try to enable
1313 x86_event_sched_out(leader
, cpuctx
);
1315 list_for_each_entry(sub
, &leader
->sibling_list
, group_entry
) {
1316 if (sub
->state
== PERF_EVENT_STATE_ACTIVE
) {
1317 x86_event_sched_out(sub
, cpuctx
);
1325 #include "perf_event_amd.c"
1326 #include "perf_event_p6.c"
1327 #include "perf_event_p4.c"
1328 #include "perf_event_intel_lbr.c"
1329 #include "perf_event_intel_ds.c"
1330 #include "perf_event_intel.c"
1332 static int __cpuinit
1333 x86_pmu_notifier(struct notifier_block
*self
, unsigned long action
, void *hcpu
)
1335 unsigned int cpu
= (long)hcpu
;
1337 switch (action
& ~CPU_TASKS_FROZEN
) {
1338 case CPU_UP_PREPARE
:
1339 if (x86_pmu
.cpu_prepare
)
1340 x86_pmu
.cpu_prepare(cpu
);
1344 if (x86_pmu
.cpu_starting
)
1345 x86_pmu
.cpu_starting(cpu
);
1349 if (x86_pmu
.cpu_dying
)
1350 x86_pmu
.cpu_dying(cpu
);
1354 if (x86_pmu
.cpu_dead
)
1355 x86_pmu
.cpu_dead(cpu
);
1365 static void __init
pmu_check_apic(void)
1371 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1372 pr_info("no hardware sampling interrupt available.\n");
1375 void __init
init_hw_perf_events(void)
1377 struct event_constraint
*c
;
1380 pr_info("Performance Events: ");
1382 switch (boot_cpu_data
.x86_vendor
) {
1383 case X86_VENDOR_INTEL
:
1384 err
= intel_pmu_init();
1386 case X86_VENDOR_AMD
:
1387 err
= amd_pmu_init();
1393 pr_cont("no PMU driver, software events only.\n");
1399 pr_cont("%s PMU driver.\n", x86_pmu
.name
);
1404 if (x86_pmu
.num_events
> X86_PMC_MAX_GENERIC
) {
1405 WARN(1, KERN_ERR
"hw perf events %d > max(%d), clipping!",
1406 x86_pmu
.num_events
, X86_PMC_MAX_GENERIC
);
1407 x86_pmu
.num_events
= X86_PMC_MAX_GENERIC
;
1409 perf_event_mask
= (1 << x86_pmu
.num_events
) - 1;
1410 perf_max_events
= x86_pmu
.num_events
;
1412 if (x86_pmu
.num_events_fixed
> X86_PMC_MAX_FIXED
) {
1413 WARN(1, KERN_ERR
"hw perf events fixed %d > max(%d), clipping!",
1414 x86_pmu
.num_events_fixed
, X86_PMC_MAX_FIXED
);
1415 x86_pmu
.num_events_fixed
= X86_PMC_MAX_FIXED
;
1419 ((1LL << x86_pmu
.num_events_fixed
)-1) << X86_PMC_IDX_FIXED
;
1420 x86_pmu
.intel_ctrl
= perf_event_mask
;
1422 perf_events_lapic_init();
1423 register_die_notifier(&perf_event_nmi_notifier
);
1425 unconstrained
= (struct event_constraint
)
1426 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu
.num_events
) - 1,
1427 0, x86_pmu
.num_events
);
1429 if (x86_pmu
.event_constraints
) {
1430 for_each_event_constraint(c
, x86_pmu
.event_constraints
) {
1431 if (c
->cmask
!= INTEL_ARCH_FIXED_MASK
)
1434 c
->idxmsk64
|= (1ULL << x86_pmu
.num_events
) - 1;
1435 c
->weight
+= x86_pmu
.num_events
;
1439 pr_info("... version: %d\n", x86_pmu
.version
);
1440 pr_info("... bit width: %d\n", x86_pmu
.event_bits
);
1441 pr_info("... generic registers: %d\n", x86_pmu
.num_events
);
1442 pr_info("... value mask: %016Lx\n", x86_pmu
.event_mask
);
1443 pr_info("... max period: %016Lx\n", x86_pmu
.max_period
);
1444 pr_info("... fixed-purpose events: %d\n", x86_pmu
.num_events_fixed
);
1445 pr_info("... event mask: %016Lx\n", perf_event_mask
);
1447 perf_cpu_notifier(x86_pmu_notifier
);
1450 static inline void x86_pmu_read(struct perf_event
*event
)
1452 x86_perf_event_update(event
);
1455 static const struct pmu pmu
= {
1456 .enable
= x86_pmu_enable
,
1457 .disable
= x86_pmu_disable
,
1458 .start
= x86_pmu_start
,
1459 .stop
= x86_pmu_stop
,
1460 .read
= x86_pmu_read
,
1461 .unthrottle
= x86_pmu_unthrottle
,
1465 * validate that we can schedule this event
1467 static int validate_event(struct perf_event
*event
)
1469 struct cpu_hw_events
*fake_cpuc
;
1470 struct event_constraint
*c
;
1473 fake_cpuc
= kmalloc(sizeof(*fake_cpuc
), GFP_KERNEL
| __GFP_ZERO
);
1477 c
= x86_pmu
.get_event_constraints(fake_cpuc
, event
);
1479 if (!c
|| !c
->weight
)
1482 if (x86_pmu
.put_event_constraints
)
1483 x86_pmu
.put_event_constraints(fake_cpuc
, event
);
1491 * validate a single event group
1493 * validation include:
1494 * - check events are compatible which each other
1495 * - events do not compete for the same counter
1496 * - number of events <= number of counters
1498 * validation ensures the group can be loaded onto the
1499 * PMU if it was the only group available.
1501 static int validate_group(struct perf_event
*event
)
1503 struct perf_event
*leader
= event
->group_leader
;
1504 struct cpu_hw_events
*fake_cpuc
;
1508 fake_cpuc
= kmalloc(sizeof(*fake_cpuc
), GFP_KERNEL
| __GFP_ZERO
);
1513 * the event is not yet connected with its
1514 * siblings therefore we must first collect
1515 * existing siblings, then add the new event
1516 * before we can simulate the scheduling
1519 n
= collect_events(fake_cpuc
, leader
, true);
1523 fake_cpuc
->n_events
= n
;
1524 n
= collect_events(fake_cpuc
, event
, false);
1528 fake_cpuc
->n_events
= n
;
1530 ret
= x86_pmu
.schedule_events(fake_cpuc
, n
, NULL
);
1538 const struct pmu
*hw_perf_event_init(struct perf_event
*event
)
1540 const struct pmu
*tmp
;
1543 err
= __hw_perf_event_init(event
);
1546 * we temporarily connect event to its pmu
1547 * such that validate_group() can classify
1548 * it as an x86 event using is_x86_event()
1553 if (event
->group_leader
!= event
)
1554 err
= validate_group(event
);
1556 err
= validate_event(event
);
1562 event
->destroy(event
);
1563 return ERR_PTR(err
);
1574 void callchain_store(struct perf_callchain_entry
*entry
, u64 ip
)
1576 if (entry
->nr
< PERF_MAX_STACK_DEPTH
)
1577 entry
->ip
[entry
->nr
++] = ip
;
1580 static DEFINE_PER_CPU(struct perf_callchain_entry
, pmc_irq_entry
);
1581 static DEFINE_PER_CPU(struct perf_callchain_entry
, pmc_nmi_entry
);
1585 backtrace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
1587 /* Ignore warnings */
1590 static void backtrace_warning(void *data
, char *msg
)
1592 /* Ignore warnings */
1595 static int backtrace_stack(void *data
, char *name
)
1600 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
1602 struct perf_callchain_entry
*entry
= data
;
1605 callchain_store(entry
, addr
);
1608 static const struct stacktrace_ops backtrace_ops
= {
1609 .warning
= backtrace_warning
,
1610 .warning_symbol
= backtrace_warning_symbol
,
1611 .stack
= backtrace_stack
,
1612 .address
= backtrace_address
,
1613 .walk_stack
= print_context_stack_bp
,
1616 #include "../dumpstack.h"
1619 perf_callchain_kernel(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1621 callchain_store(entry
, PERF_CONTEXT_KERNEL
);
1622 callchain_store(entry
, regs
->ip
);
1624 dump_trace(NULL
, regs
, NULL
, regs
->bp
, &backtrace_ops
, entry
);
1627 static int copy_stack_frame(const void __user
*fp
, struct stack_frame
*frame
)
1629 unsigned long bytes
;
1631 bytes
= copy_from_user_nmi(frame
, fp
, sizeof(*frame
));
1633 return bytes
== sizeof(*frame
);
1637 perf_callchain_user(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1639 struct stack_frame frame
;
1640 const void __user
*fp
;
1642 if (!user_mode(regs
))
1643 regs
= task_pt_regs(current
);
1645 fp
= (void __user
*)regs
->bp
;
1647 callchain_store(entry
, PERF_CONTEXT_USER
);
1648 callchain_store(entry
, regs
->ip
);
1650 while (entry
->nr
< PERF_MAX_STACK_DEPTH
) {
1651 frame
.next_frame
= NULL
;
1652 frame
.return_address
= 0;
1654 if (!copy_stack_frame(fp
, &frame
))
1657 if ((unsigned long)fp
< regs
->sp
)
1660 callchain_store(entry
, frame
.return_address
);
1661 fp
= frame
.next_frame
;
1666 perf_do_callchain(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1673 is_user
= user_mode(regs
);
1675 if (is_user
&& current
->state
!= TASK_RUNNING
)
1679 perf_callchain_kernel(regs
, entry
);
1682 perf_callchain_user(regs
, entry
);
1685 struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1687 struct perf_callchain_entry
*entry
;
1690 entry
= &__get_cpu_var(pmc_nmi_entry
);
1692 entry
= &__get_cpu_var(pmc_irq_entry
);
1696 perf_do_callchain(regs
, entry
);
1701 #ifdef CONFIG_EVENT_TRACING
1702 void perf_arch_fetch_caller_regs(struct pt_regs
*regs
, unsigned long ip
, int skip
)
1706 * perf_arch_fetch_caller_regs adds another call, we need to increment
1709 regs
->bp
= rewind_frame_pointer(skip
+ 1);
1710 regs
->cs
= __KERNEL_CS
;
1711 local_save_flags(regs
->flags
);