2 * Performance counter 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>
10 * For licencing details see kernel-base/COPYING
13 #include <linux/perf_counter.h>
14 #include <linux/capability.h>
15 #include <linux/notifier.h>
16 #include <linux/hardirq.h>
17 #include <linux/kprobes.h>
18 #include <linux/module.h>
19 #include <linux/kdebug.h>
20 #include <linux/sched.h>
21 #include <linux/uaccess.h>
24 #include <asm/stacktrace.h>
27 static u64 perf_counter_mask __read_mostly;
29 struct cpu_hw_counters {
30 struct perf_counter *counters[X86_PMC_IDX_MAX];
31 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
32 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
33 unsigned long interrupts;
38 * struct x86_pmu - generic x86 pmu
43 int (*handle_irq)(struct pt_regs *, int);
44 void (*disable_all)(void);
45 void (*enable_all)(void);
46 void (*enable)(struct hw_perf_counter *, int);
47 void (*disable)(struct hw_perf_counter *, int);
50 u64 (*event_map)(int);
51 u64 (*raw_event)(u64);
54 int num_counters_fixed;
61 static struct x86_pmu x86_pmu __read_mostly;
63 static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
68 * Intel PerfMon v3. Used on Core2 and later.
70 static const u64 intel_perfmon_event_map[] =
72 [PERF_COUNT_CPU_CYCLES] = 0x003c,
73 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
74 [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
75 [PERF_COUNT_CACHE_MISSES] = 0x412e,
76 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
77 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
78 [PERF_COUNT_BUS_CYCLES] = 0x013c,
81 static u64 intel_pmu_event_map(int event)
83 return intel_perfmon_event_map[event];
86 static u64 intel_pmu_raw_event(u64 event)
88 #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
89 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
90 #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL
91 #define CORE_EVNTSEL_INV_MASK 0x00800000ULL
92 #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
94 #define CORE_EVNTSEL_MASK \
95 (CORE_EVNTSEL_EVENT_MASK | \
96 CORE_EVNTSEL_UNIT_MASK | \
97 CORE_EVNTSEL_EDGE_MASK | \
98 CORE_EVNTSEL_INV_MASK | \
99 CORE_EVNTSEL_COUNTER_MASK)
101 return event & CORE_EVNTSEL_MASK;
105 * AMD Performance Monitor K7 and later.
107 static const u64 amd_perfmon_event_map[] =
109 [PERF_COUNT_CPU_CYCLES] = 0x0076,
110 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
111 [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
112 [PERF_COUNT_CACHE_MISSES] = 0x0081,
113 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
114 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
117 static u64 amd_pmu_event_map(int event)
119 return amd_perfmon_event_map[event];
122 static u64 amd_pmu_raw_event(u64 event)
124 #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
125 #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
126 #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL
127 #define K7_EVNTSEL_INV_MASK 0x000800000ULL
128 #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
130 #define K7_EVNTSEL_MASK \
131 (K7_EVNTSEL_EVENT_MASK | \
132 K7_EVNTSEL_UNIT_MASK | \
133 K7_EVNTSEL_EDGE_MASK | \
134 K7_EVNTSEL_INV_MASK | \
135 K7_EVNTSEL_COUNTER_MASK)
137 return event & K7_EVNTSEL_MASK;
141 * Propagate counter elapsed time into the generic counter.
142 * Can only be executed on the CPU where the counter is active.
143 * Returns the delta events processed.
146 x86_perf_counter_update(struct perf_counter *counter,
147 struct hw_perf_counter *hwc, int idx)
149 int shift = 64 - x86_pmu.counter_bits;
150 u64 prev_raw_count, new_raw_count;
154 * Careful: an NMI might modify the previous counter value.
156 * Our tactic to handle this is to first atomically read and
157 * exchange a new raw count - then add that new-prev delta
158 * count to the generic counter atomically:
161 prev_raw_count = atomic64_read(&hwc->prev_count);
162 rdmsrl(hwc->counter_base + idx, new_raw_count);
164 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
165 new_raw_count) != prev_raw_count)
169 * Now we have the new raw value and have updated the prev
170 * timestamp already. We can now calculate the elapsed delta
171 * (counter-)time and add that to the generic counter.
173 * Careful, not all hw sign-extends above the physical width
176 delta = (new_raw_count << shift) - (prev_raw_count << shift);
179 atomic64_add(delta, &counter->count);
180 atomic64_sub(delta, &hwc->period_left);
182 return new_raw_count;
185 static atomic_t active_counters;
186 static DEFINE_MUTEX(pmc_reserve_mutex);
188 static bool reserve_pmc_hardware(void)
192 if (nmi_watchdog == NMI_LOCAL_APIC)
193 disable_lapic_nmi_watchdog();
195 for (i = 0; i < x86_pmu.num_counters; i++) {
196 if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
200 for (i = 0; i < x86_pmu.num_counters; i++) {
201 if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
208 for (i--; i >= 0; i--)
209 release_evntsel_nmi(x86_pmu.eventsel + i);
211 i = x86_pmu.num_counters;
214 for (i--; i >= 0; i--)
215 release_perfctr_nmi(x86_pmu.perfctr + i);
217 if (nmi_watchdog == NMI_LOCAL_APIC)
218 enable_lapic_nmi_watchdog();
223 static void release_pmc_hardware(void)
227 for (i = 0; i < x86_pmu.num_counters; i++) {
228 release_perfctr_nmi(x86_pmu.perfctr + i);
229 release_evntsel_nmi(x86_pmu.eventsel + i);
232 if (nmi_watchdog == NMI_LOCAL_APIC)
233 enable_lapic_nmi_watchdog();
236 static void hw_perf_counter_destroy(struct perf_counter *counter)
238 if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) {
239 release_pmc_hardware();
240 mutex_unlock(&pmc_reserve_mutex);
244 static inline int x86_pmu_initialized(void)
246 return x86_pmu.handle_irq != NULL;
250 * Setup the hardware configuration for a given hw_event_type
252 static int __hw_perf_counter_init(struct perf_counter *counter)
254 struct perf_counter_hw_event *hw_event = &counter->hw_event;
255 struct hw_perf_counter *hwc = &counter->hw;
258 if (!x86_pmu_initialized())
262 if (!atomic_inc_not_zero(&active_counters)) {
263 mutex_lock(&pmc_reserve_mutex);
264 if (atomic_read(&active_counters) == 0 && !reserve_pmc_hardware())
267 atomic_inc(&active_counters);
268 mutex_unlock(&pmc_reserve_mutex);
275 * (keep 'enabled' bit clear for now)
277 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
280 * Count user and OS events unless requested not to.
282 if (!hw_event->exclude_user)
283 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
284 if (!hw_event->exclude_kernel)
285 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
287 if (!hwc->sample_period)
288 hwc->sample_period = x86_pmu.max_period;
290 atomic64_set(&hwc->period_left, hwc->sample_period);
293 * Raw event type provide the config in the event structure
295 if (perf_event_raw(hw_event)) {
296 hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
298 if (perf_event_id(hw_event) >= x86_pmu.max_events)
303 hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
306 counter->destroy = hw_perf_counter_destroy;
311 static void intel_pmu_disable_all(void)
313 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
316 static void amd_pmu_disable_all(void)
318 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
326 * ensure we write the disable before we start disabling the
327 * counters proper, so that amd_pmu_enable_counter() does the
332 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
335 if (!test_bit(idx, cpuc->active_mask))
337 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
338 if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
340 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
341 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
345 void hw_perf_disable(void)
347 if (!x86_pmu_initialized())
349 return x86_pmu.disable_all();
352 static void intel_pmu_enable_all(void)
354 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
357 static void amd_pmu_enable_all(void)
359 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
368 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
371 if (!test_bit(idx, cpuc->active_mask))
373 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
374 if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
376 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
377 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
381 void hw_perf_enable(void)
383 if (!x86_pmu_initialized())
385 x86_pmu.enable_all();
388 static inline u64 intel_pmu_get_status(void)
392 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
397 static inline void intel_pmu_ack_status(u64 ack)
399 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
402 static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
405 err = checking_wrmsrl(hwc->config_base + idx,
406 hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
409 static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
412 err = checking_wrmsrl(hwc->config_base + idx,
417 intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
419 int idx = __idx - X86_PMC_IDX_FIXED;
423 mask = 0xfULL << (idx * 4);
425 rdmsrl(hwc->config_base, ctrl_val);
427 err = checking_wrmsrl(hwc->config_base, ctrl_val);
431 intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
433 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
434 intel_pmu_disable_fixed(hwc, idx);
438 x86_pmu_disable_counter(hwc, idx);
442 amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
444 x86_pmu_disable_counter(hwc, idx);
447 static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
450 * Set the next IRQ period, based on the hwc->period_left value.
451 * To be called with the counter disabled in hw:
454 x86_perf_counter_set_period(struct perf_counter *counter,
455 struct hw_perf_counter *hwc, int idx)
457 s64 left = atomic64_read(&hwc->period_left);
458 s64 period = hwc->sample_period;
462 * If we are way outside a reasoable range then just skip forward:
464 if (unlikely(left <= -period)) {
466 atomic64_set(&hwc->period_left, left);
470 if (unlikely(left <= 0)) {
472 atomic64_set(&hwc->period_left, left);
476 * Quirk: certain CPUs dont like it if just 1 event is left:
478 if (unlikely(left < 2))
481 if (left > x86_pmu.max_period)
482 left = x86_pmu.max_period;
484 per_cpu(prev_left[idx], smp_processor_id()) = left;
487 * The hw counter starts counting from this counter offset,
488 * mark it to be able to extra future deltas:
490 atomic64_set(&hwc->prev_count, (u64)-left);
492 err = checking_wrmsrl(hwc->counter_base + idx,
493 (u64)(-left) & x86_pmu.counter_mask);
499 intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
501 int idx = __idx - X86_PMC_IDX_FIXED;
502 u64 ctrl_val, bits, mask;
506 * Enable IRQ generation (0x8),
507 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
511 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
513 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
516 mask = 0xfULL << (idx * 4);
518 rdmsrl(hwc->config_base, ctrl_val);
521 err = checking_wrmsrl(hwc->config_base, ctrl_val);
524 static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
526 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
527 intel_pmu_enable_fixed(hwc, idx);
531 x86_pmu_enable_counter(hwc, idx);
534 static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
536 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
539 x86_pmu_enable_counter(hwc, idx);
541 x86_pmu_disable_counter(hwc, idx);
545 fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
549 if (!x86_pmu.num_counters_fixed)
552 event = hwc->config & ARCH_PERFMON_EVENT_MASK;
554 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
555 return X86_PMC_IDX_FIXED_INSTRUCTIONS;
556 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
557 return X86_PMC_IDX_FIXED_CPU_CYCLES;
558 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
559 return X86_PMC_IDX_FIXED_BUS_CYCLES;
565 * Find a PMC slot for the freshly enabled / scheduled in counter:
567 static int x86_pmu_enable(struct perf_counter *counter)
569 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
570 struct hw_perf_counter *hwc = &counter->hw;
573 idx = fixed_mode_idx(counter, hwc);
576 * Try to get the fixed counter, if that is already taken
577 * then try to get a generic counter:
579 if (test_and_set_bit(idx, cpuc->used_mask))
582 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
584 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
585 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
588 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
592 /* Try to get the previous generic counter again */
593 if (test_and_set_bit(idx, cpuc->used_mask)) {
595 idx = find_first_zero_bit(cpuc->used_mask,
596 x86_pmu.num_counters);
597 if (idx == x86_pmu.num_counters)
600 set_bit(idx, cpuc->used_mask);
603 hwc->config_base = x86_pmu.eventsel;
604 hwc->counter_base = x86_pmu.perfctr;
607 perf_counters_lapic_init();
609 x86_pmu.disable(hwc, idx);
611 cpuc->counters[idx] = counter;
612 set_bit(idx, cpuc->active_mask);
614 x86_perf_counter_set_period(counter, hwc, idx);
615 x86_pmu.enable(hwc, idx);
620 static void x86_pmu_unthrottle(struct perf_counter *counter)
622 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
623 struct hw_perf_counter *hwc = &counter->hw;
625 if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX ||
626 cpuc->counters[hwc->idx] != counter))
629 x86_pmu.enable(hwc, hwc->idx);
632 void perf_counter_print_debug(void)
634 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
635 struct cpu_hw_counters *cpuc;
639 if (!x86_pmu.num_counters)
642 local_irq_save(flags);
644 cpu = smp_processor_id();
645 cpuc = &per_cpu(cpu_hw_counters, cpu);
647 if (x86_pmu.version >= 2) {
648 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
649 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
650 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
651 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
654 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
655 pr_info("CPU#%d: status: %016llx\n", cpu, status);
656 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
657 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
659 pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask);
661 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
662 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
663 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
665 prev_left = per_cpu(prev_left[idx], cpu);
667 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
669 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
670 cpu, idx, pmc_count);
671 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
672 cpu, idx, prev_left);
674 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
675 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
677 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
678 cpu, idx, pmc_count);
680 local_irq_restore(flags);
683 static void x86_pmu_disable(struct perf_counter *counter)
685 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
686 struct hw_perf_counter *hwc = &counter->hw;
690 * Must be done before we disable, otherwise the nmi handler
691 * could reenable again:
693 clear_bit(idx, cpuc->active_mask);
694 x86_pmu.disable(hwc, idx);
697 * Make sure the cleared pointer becomes visible before we
698 * (potentially) free the counter:
703 * Drain the remaining delta count out of a counter
704 * that we are disabling:
706 x86_perf_counter_update(counter, hwc, idx);
707 cpuc->counters[idx] = NULL;
708 clear_bit(idx, cpuc->used_mask);
712 * Save and restart an expired counter. Called by NMI contexts,
713 * so it has to be careful about preempting normal counter ops:
715 static int intel_pmu_save_and_restart(struct perf_counter *counter)
717 struct hw_perf_counter *hwc = &counter->hw;
721 x86_perf_counter_update(counter, hwc, idx);
722 ret = x86_perf_counter_set_period(counter, hwc, idx);
724 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
725 intel_pmu_enable_counter(hwc, idx);
730 static void intel_pmu_reset(void)
735 if (!x86_pmu.num_counters)
738 local_irq_save(flags);
740 printk("clearing PMU state on CPU#%d\n", smp_processor_id());
742 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
743 checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
744 checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
746 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
747 checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
750 local_irq_restore(flags);
755 * This handler is triggered by the local APIC, so the APIC IRQ handling
758 static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
760 struct cpu_hw_counters *cpuc;
761 struct cpu_hw_counters;
765 cpu = smp_processor_id();
766 cpuc = &per_cpu(cpu_hw_counters, cpu);
769 status = intel_pmu_get_status();
778 WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
779 perf_counter_print_debug();
785 inc_irq_stat(apic_perf_irqs);
787 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
788 struct perf_counter *counter = cpuc->counters[bit];
790 clear_bit(bit, (unsigned long *) &status);
791 if (!test_bit(bit, cpuc->active_mask))
794 if (!intel_pmu_save_and_restart(counter))
797 if (perf_counter_overflow(counter, nmi, regs, 0))
798 intel_pmu_disable_counter(&counter->hw, bit);
801 intel_pmu_ack_status(ack);
804 * Repeat if there is more work to be done:
806 status = intel_pmu_get_status();
815 static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
817 int cpu, idx, handled = 0;
818 struct cpu_hw_counters *cpuc;
819 struct perf_counter *counter;
820 struct hw_perf_counter *hwc;
823 cpu = smp_processor_id();
824 cpuc = &per_cpu(cpu_hw_counters, cpu);
826 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
827 if (!test_bit(idx, cpuc->active_mask))
830 counter = cpuc->counters[idx];
833 val = x86_perf_counter_update(counter, hwc, idx);
834 if (val & (1ULL << (x86_pmu.counter_bits - 1)))
837 /* counter overflow */
839 inc_irq_stat(apic_perf_irqs);
840 if (!x86_perf_counter_set_period(counter, hwc, idx))
843 if (perf_counter_overflow(counter, nmi, regs, 0))
844 amd_pmu_disable_counter(hwc, idx);
850 void smp_perf_counter_interrupt(struct pt_regs *regs)
853 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
855 x86_pmu.handle_irq(regs, 0);
859 void smp_perf_pending_interrupt(struct pt_regs *regs)
863 inc_irq_stat(apic_pending_irqs);
864 perf_counter_do_pending();
868 void set_perf_counter_pending(void)
870 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
873 void perf_counters_lapic_init(void)
875 if (!x86_pmu_initialized())
879 * Always use NMI for PMU
881 apic_write(APIC_LVTPC, APIC_DM_NMI);
885 perf_counter_nmi_handler(struct notifier_block *self,
886 unsigned long cmd, void *__args)
888 struct die_args *args = __args;
889 struct pt_regs *regs;
891 if (!atomic_read(&active_counters))
905 apic_write(APIC_LVTPC, APIC_DM_NMI);
907 * Can't rely on the handled return value to say it was our NMI, two
908 * counters could trigger 'simultaneously' raising two back-to-back NMIs.
910 * If the first NMI handles both, the latter will be empty and daze
913 x86_pmu.handle_irq(regs, 1);
918 static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
919 .notifier_call = perf_counter_nmi_handler,
924 static struct x86_pmu intel_pmu = {
926 .handle_irq = intel_pmu_handle_irq,
927 .disable_all = intel_pmu_disable_all,
928 .enable_all = intel_pmu_enable_all,
929 .enable = intel_pmu_enable_counter,
930 .disable = intel_pmu_disable_counter,
931 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
932 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
933 .event_map = intel_pmu_event_map,
934 .raw_event = intel_pmu_raw_event,
935 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
937 * Intel PMCs cannot be accessed sanely above 32 bit width,
938 * so we install an artificial 1<<31 period regardless of
939 * the generic counter period:
941 .max_period = (1ULL << 31) - 1,
944 static struct x86_pmu amd_pmu = {
946 .handle_irq = amd_pmu_handle_irq,
947 .disable_all = amd_pmu_disable_all,
948 .enable_all = amd_pmu_enable_all,
949 .enable = amd_pmu_enable_counter,
950 .disable = amd_pmu_disable_counter,
951 .eventsel = MSR_K7_EVNTSEL0,
952 .perfctr = MSR_K7_PERFCTR0,
953 .event_map = amd_pmu_event_map,
954 .raw_event = amd_pmu_raw_event,
955 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
958 .counter_mask = (1ULL << 48) - 1,
959 /* use highest bit to detect overflow */
960 .max_period = (1ULL << 47) - 1,
963 static int intel_pmu_init(void)
965 union cpuid10_edx edx;
966 union cpuid10_eax eax;
971 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
975 * Check whether the Architectural PerfMon supports
976 * Branch Misses Retired Event or not.
978 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
979 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
982 version = eax.split.version_id;
987 x86_pmu.version = version;
988 x86_pmu.num_counters = eax.split.num_counters;
991 * Quirk: v2 perfmon does not report fixed-purpose counters, so
992 * assume at least 3 counters:
994 x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3);
996 x86_pmu.counter_bits = eax.split.bit_width;
997 x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
999 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
1004 static int amd_pmu_init(void)
1010 void __init init_hw_perf_counters(void)
1014 switch (boot_cpu_data.x86_vendor) {
1015 case X86_VENDOR_INTEL:
1016 err = intel_pmu_init();
1018 case X86_VENDOR_AMD:
1019 err = amd_pmu_init();
1027 pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
1028 pr_info("... version: %d\n", x86_pmu.version);
1029 pr_info("... bit width: %d\n", x86_pmu.counter_bits);
1031 pr_info("... num counters: %d\n", x86_pmu.num_counters);
1032 if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
1033 x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
1034 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
1035 x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
1037 perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
1038 perf_max_counters = x86_pmu.num_counters;
1040 pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
1041 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
1043 if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
1044 x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
1045 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
1046 x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
1048 pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
1050 perf_counter_mask |=
1051 ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
1053 pr_info("... counter mask: %016Lx\n", perf_counter_mask);
1055 perf_counters_lapic_init();
1056 register_die_notifier(&perf_counter_nmi_notifier);
1059 static inline void x86_pmu_read(struct perf_counter *counter)
1061 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
1064 static const struct pmu pmu = {
1065 .enable = x86_pmu_enable,
1066 .disable = x86_pmu_disable,
1067 .read = x86_pmu_read,
1068 .unthrottle = x86_pmu_unthrottle,
1071 const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
1075 err = __hw_perf_counter_init(counter);
1077 return ERR_PTR(err);
1087 void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
1089 if (entry->nr < MAX_STACK_DEPTH)
1090 entry->ip[entry->nr++] = ip;
1093 static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
1094 static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
1098 backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1100 /* Ignore warnings */
1103 static void backtrace_warning(void *data, char *msg)
1105 /* Ignore warnings */
1108 static int backtrace_stack(void *data, char *name)
1110 /* Don't bother with IRQ stacks for now */
1114 static void backtrace_address(void *data, unsigned long addr, int reliable)
1116 struct perf_callchain_entry *entry = data;
1119 callchain_store(entry, addr);
1122 static const struct stacktrace_ops backtrace_ops = {
1123 .warning = backtrace_warning,
1124 .warning_symbol = backtrace_warning_symbol,
1125 .stack = backtrace_stack,
1126 .address = backtrace_address,
1130 perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1136 callchain_store(entry, instruction_pointer(regs));
1138 stack = ((char *)regs + sizeof(struct pt_regs));
1139 #ifdef CONFIG_FRAME_POINTER
1140 bp = frame_pointer(regs);
1145 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
1147 entry->kernel = entry->nr - nr;
1151 struct stack_frame {
1152 const void __user *next_fp;
1153 unsigned long return_address;
1156 static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
1160 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
1164 pagefault_disable();
1165 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
1173 perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1175 struct stack_frame frame;
1176 const void __user *fp;
1179 regs = (struct pt_regs *)current->thread.sp0 - 1;
1180 fp = (void __user *)regs->bp;
1182 callchain_store(entry, regs->ip);
1184 while (entry->nr < MAX_STACK_DEPTH) {
1185 frame.next_fp = NULL;
1186 frame.return_address = 0;
1188 if (!copy_stack_frame(fp, &frame))
1191 if ((unsigned long)fp < user_stack_pointer(regs))
1194 callchain_store(entry, frame.return_address);
1198 entry->user = entry->nr - nr;
1202 perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1209 is_user = user_mode(regs);
1211 if (!current || current->pid == 0)
1214 if (is_user && current->state != TASK_RUNNING)
1218 perf_callchain_kernel(regs, entry);
1221 perf_callchain_user(regs, entry);
1224 struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1226 struct perf_callchain_entry *entry;
1229 entry = &__get_cpu_var(nmi_entry);
1231 entry = &__get_cpu_var(irq_entry);
1238 perf_do_callchain(regs, entry);