2 * Performance counter x86 architecture code
4 * Copyright(C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2008 Red Hat, Inc., Ingo Molnar
6 * Copyright(C) 2009 Jaswinder Singh Rajput
7 * Copyright(C) 2009 Advanced Micro Devices, Inc., Robert Richter
9 * For licencing details see kernel-base/COPYING
12 #include <linux/perf_counter.h>
13 #include <linux/capability.h>
14 #include <linux/notifier.h>
15 #include <linux/hardirq.h>
16 #include <linux/kprobes.h>
17 #include <linux/module.h>
18 #include <linux/kdebug.h>
19 #include <linux/sched.h>
20 #include <linux/uaccess.h>
23 #include <asm/stacktrace.h>
26 static u64 perf_counter_mask __read_mostly;
28 struct cpu_hw_counters {
29 struct perf_counter *counters[X86_PMC_IDX_MAX];
30 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
31 unsigned long active[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
32 unsigned long interrupts;
38 * struct x86_pmu - generic x86 pmu
43 int (*handle_irq)(struct pt_regs *, int);
44 u64 (*save_disable_all)(void);
45 void (*restore_all)(u64);
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;
59 static struct x86_pmu x86_pmu __read_mostly;
61 static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
66 * Intel PerfMon v3. Used on Core2 and later.
68 static const u64 intel_perfmon_event_map[] =
70 [PERF_COUNT_CPU_CYCLES] = 0x003c,
71 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
72 [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
73 [PERF_COUNT_CACHE_MISSES] = 0x412e,
74 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
75 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
76 [PERF_COUNT_BUS_CYCLES] = 0x013c,
79 static u64 intel_pmu_event_map(int event)
81 return intel_perfmon_event_map[event];
84 static u64 intel_pmu_raw_event(u64 event)
86 #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
87 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
88 #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
90 #define CORE_EVNTSEL_MASK \
91 (CORE_EVNTSEL_EVENT_MASK | \
92 CORE_EVNTSEL_UNIT_MASK | \
93 CORE_EVNTSEL_COUNTER_MASK)
95 return event & CORE_EVNTSEL_MASK;
99 * AMD Performance Monitor K7 and later.
101 static const u64 amd_perfmon_event_map[] =
103 [PERF_COUNT_CPU_CYCLES] = 0x0076,
104 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
105 [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
106 [PERF_COUNT_CACHE_MISSES] = 0x0081,
107 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
108 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
111 static u64 amd_pmu_event_map(int event)
113 return amd_perfmon_event_map[event];
116 static u64 amd_pmu_raw_event(u64 event)
118 #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
119 #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
120 #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
122 #define K7_EVNTSEL_MASK \
123 (K7_EVNTSEL_EVENT_MASK | \
124 K7_EVNTSEL_UNIT_MASK | \
125 K7_EVNTSEL_COUNTER_MASK)
127 return event & K7_EVNTSEL_MASK;
131 * Propagate counter elapsed time into the generic counter.
132 * Can only be executed on the CPU where the counter is active.
133 * Returns the delta events processed.
136 x86_perf_counter_update(struct perf_counter *counter,
137 struct hw_perf_counter *hwc, int idx)
139 u64 prev_raw_count, new_raw_count, delta;
142 * Careful: an NMI might modify the previous counter value.
144 * Our tactic to handle this is to first atomically read and
145 * exchange a new raw count - then add that new-prev delta
146 * count to the generic counter atomically:
149 prev_raw_count = atomic64_read(&hwc->prev_count);
150 rdmsrl(hwc->counter_base + idx, new_raw_count);
152 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
153 new_raw_count) != prev_raw_count)
157 * Now we have the new raw value and have updated the prev
158 * timestamp already. We can now calculate the elapsed delta
159 * (counter-)time and add that to the generic counter.
161 * Careful, not all hw sign-extends above the physical width
162 * of the count, so we do that by clipping the delta to 32 bits:
164 delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
166 atomic64_add(delta, &counter->count);
167 atomic64_sub(delta, &hwc->period_left);
169 return new_raw_count;
172 static atomic_t num_counters;
173 static DEFINE_MUTEX(pmc_reserve_mutex);
175 static bool reserve_pmc_hardware(void)
179 if (nmi_watchdog == NMI_LOCAL_APIC)
180 disable_lapic_nmi_watchdog();
182 for (i = 0; i < x86_pmu.num_counters; i++) {
183 if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
187 for (i = 0; i < x86_pmu.num_counters; i++) {
188 if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
195 for (i--; i >= 0; i--)
196 release_evntsel_nmi(x86_pmu.eventsel + i);
198 i = x86_pmu.num_counters;
201 for (i--; i >= 0; i--)
202 release_perfctr_nmi(x86_pmu.perfctr + i);
204 if (nmi_watchdog == NMI_LOCAL_APIC)
205 enable_lapic_nmi_watchdog();
210 static void release_pmc_hardware(void)
214 for (i = 0; i < x86_pmu.num_counters; i++) {
215 release_perfctr_nmi(x86_pmu.perfctr + i);
216 release_evntsel_nmi(x86_pmu.eventsel + i);
219 if (nmi_watchdog == NMI_LOCAL_APIC)
220 enable_lapic_nmi_watchdog();
223 static void hw_perf_counter_destroy(struct perf_counter *counter)
225 if (atomic_dec_and_mutex_lock(&num_counters, &pmc_reserve_mutex)) {
226 release_pmc_hardware();
227 mutex_unlock(&pmc_reserve_mutex);
231 static inline int x86_pmu_initialized(void)
233 return x86_pmu.handle_irq != NULL;
237 * Setup the hardware configuration for a given hw_event_type
239 static int __hw_perf_counter_init(struct perf_counter *counter)
241 struct perf_counter_hw_event *hw_event = &counter->hw_event;
242 struct hw_perf_counter *hwc = &counter->hw;
245 if (!x86_pmu_initialized())
249 if (atomic_inc_not_zero(&num_counters)) {
250 mutex_lock(&pmc_reserve_mutex);
251 if (atomic_read(&num_counters) == 0 && !reserve_pmc_hardware())
254 atomic_inc(&num_counters);
255 mutex_unlock(&pmc_reserve_mutex);
262 * (keep 'enabled' bit clear for now)
264 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
267 * Count user and OS events unless requested not to.
269 if (!hw_event->exclude_user)
270 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
271 if (!hw_event->exclude_kernel)
272 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
275 * If privileged enough, allow NMI events:
278 if (capable(CAP_SYS_ADMIN) && hw_event->nmi)
281 hwc->irq_period = hw_event->irq_period;
283 * Intel PMCs cannot be accessed sanely above 32 bit width,
284 * so we install an artificial 1<<31 period regardless of
285 * the generic counter period:
287 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
288 if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
289 hwc->irq_period = 0x7FFFFFFF;
291 atomic64_set(&hwc->period_left, hwc->irq_period);
294 * Raw event type provide the config in the event structure
296 if (perf_event_raw(hw_event)) {
297 hwc->config |= x86_pmu.raw_event(perf_event_config(hw_event));
299 if (perf_event_id(hw_event) >= x86_pmu.max_events)
304 hwc->config |= x86_pmu.event_map(perf_event_id(hw_event));
307 counter->destroy = hw_perf_counter_destroy;
312 static u64 intel_pmu_save_disable_all(void)
316 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
317 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
322 static u64 amd_pmu_save_disable_all(void)
324 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
327 enabled = cpuc->enabled;
330 * ensure we write the disable before we start disabling the
331 * counters proper, so that amd_pmu_enable_counter() does the
336 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
339 if (!test_bit(idx, cpuc->active))
341 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
342 if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
344 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
345 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
351 u64 hw_perf_save_disable(void)
353 if (!x86_pmu_initialized())
355 return x86_pmu.save_disable_all();
358 * Exported because of ACPI idle
360 EXPORT_SYMBOL_GPL(hw_perf_save_disable);
362 static void intel_pmu_restore_all(u64 ctrl)
364 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
367 static void amd_pmu_restore_all(u64 ctrl)
369 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
372 cpuc->enabled = ctrl;
377 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
380 if (!test_bit(idx, cpuc->active))
382 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
383 if (val & ARCH_PERFMON_EVENTSEL0_ENABLE)
385 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
386 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
390 void hw_perf_restore(u64 ctrl)
392 if (!x86_pmu_initialized())
394 x86_pmu.restore_all(ctrl);
397 * Exported because of ACPI idle
399 EXPORT_SYMBOL_GPL(hw_perf_restore);
401 static inline u64 intel_pmu_get_status(u64 mask)
405 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
410 static inline void intel_pmu_ack_status(u64 ack)
412 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
415 static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
418 err = checking_wrmsrl(hwc->config_base + idx,
419 hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
422 static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
425 err = checking_wrmsrl(hwc->config_base + idx,
430 intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx)
432 int idx = __idx - X86_PMC_IDX_FIXED;
436 mask = 0xfULL << (idx * 4);
438 rdmsrl(hwc->config_base, ctrl_val);
440 err = checking_wrmsrl(hwc->config_base, ctrl_val);
444 intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
446 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
447 intel_pmu_disable_fixed(hwc, idx);
451 x86_pmu_disable_counter(hwc, idx);
455 amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx)
457 x86_pmu_disable_counter(hwc, idx);
460 static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
463 * Set the next IRQ period, based on the hwc->period_left value.
464 * To be called with the counter disabled in hw:
467 x86_perf_counter_set_period(struct perf_counter *counter,
468 struct hw_perf_counter *hwc, int idx)
470 s64 left = atomic64_read(&hwc->period_left);
471 s64 period = hwc->irq_period;
475 * If we are way outside a reasoable range then just skip forward:
477 if (unlikely(left <= -period)) {
479 atomic64_set(&hwc->period_left, left);
482 if (unlikely(left <= 0)) {
484 atomic64_set(&hwc->period_left, left);
487 per_cpu(prev_left[idx], smp_processor_id()) = left;
490 * The hw counter starts counting from this counter offset,
491 * mark it to be able to extra future deltas:
493 atomic64_set(&hwc->prev_count, (u64)-left);
495 err = checking_wrmsrl(hwc->counter_base + idx,
496 (u64)(-left) & x86_pmu.counter_mask);
500 intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx)
502 int idx = __idx - X86_PMC_IDX_FIXED;
503 u64 ctrl_val, bits, mask;
507 * Enable IRQ generation (0x8),
508 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
512 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
514 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
517 mask = 0xfULL << (idx * 4);
519 rdmsrl(hwc->config_base, ctrl_val);
522 err = checking_wrmsrl(hwc->config_base, ctrl_val);
525 static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
527 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
528 intel_pmu_enable_fixed(hwc, idx);
532 x86_pmu_enable_counter(hwc, idx);
535 static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx)
537 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
540 x86_pmu_enable_counter(hwc, idx);
542 x86_pmu_disable_counter(hwc, idx);
546 fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
550 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
553 if (unlikely(hwc->nmi))
556 event = hwc->config & ARCH_PERFMON_EVENT_MASK;
558 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_INSTRUCTIONS)))
559 return X86_PMC_IDX_FIXED_INSTRUCTIONS;
560 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_CPU_CYCLES)))
561 return X86_PMC_IDX_FIXED_CPU_CYCLES;
562 if (unlikely(event == x86_pmu.event_map(PERF_COUNT_BUS_CYCLES)))
563 return X86_PMC_IDX_FIXED_BUS_CYCLES;
569 * Find a PMC slot for the freshly enabled / scheduled in counter:
571 static int x86_pmu_enable(struct perf_counter *counter)
573 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
574 struct hw_perf_counter *hwc = &counter->hw;
577 idx = fixed_mode_idx(counter, hwc);
580 * Try to get the fixed counter, if that is already taken
581 * then try to get a generic counter:
583 if (test_and_set_bit(idx, cpuc->used))
586 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
588 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
589 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
592 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
596 /* Try to get the previous generic counter again */
597 if (test_and_set_bit(idx, cpuc->used)) {
599 idx = find_first_zero_bit(cpuc->used,
600 x86_pmu.num_counters);
601 if (idx == x86_pmu.num_counters)
604 set_bit(idx, cpuc->used);
607 hwc->config_base = x86_pmu.eventsel;
608 hwc->counter_base = x86_pmu.perfctr;
611 perf_counters_lapic_init(hwc->nmi);
613 x86_pmu.disable(hwc, idx);
615 cpuc->counters[idx] = counter;
616 set_bit(idx, cpuc->active);
618 x86_perf_counter_set_period(counter, hwc, idx);
619 x86_pmu.enable(hwc, idx);
624 void perf_counter_print_debug(void)
626 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
627 struct cpu_hw_counters *cpuc;
630 if (!x86_pmu.num_counters)
635 cpu = smp_processor_id();
636 cpuc = &per_cpu(cpu_hw_counters, cpu);
638 if (x86_pmu.version >= 2) {
639 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
640 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
641 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
642 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
645 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
646 pr_info("CPU#%d: status: %016llx\n", cpu, status);
647 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
648 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
650 pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
652 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
653 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
654 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
656 prev_left = per_cpu(prev_left[idx], cpu);
658 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
660 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
661 cpu, idx, pmc_count);
662 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
663 cpu, idx, prev_left);
665 for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) {
666 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
668 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
669 cpu, idx, pmc_count);
674 static void x86_pmu_disable(struct perf_counter *counter)
676 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
677 struct hw_perf_counter *hwc = &counter->hw;
681 * Must be done before we disable, otherwise the nmi handler
682 * could reenable again:
684 clear_bit(idx, cpuc->active);
685 x86_pmu.disable(hwc, idx);
688 * Make sure the cleared pointer becomes visible before we
689 * (potentially) free the counter:
694 * Drain the remaining delta count out of a counter
695 * that we are disabling:
697 x86_perf_counter_update(counter, hwc, idx);
698 cpuc->counters[idx] = NULL;
699 clear_bit(idx, cpuc->used);
703 * Save and restart an expired counter. Called by NMI contexts,
704 * so it has to be careful about preempting normal counter ops:
706 static void intel_pmu_save_and_restart(struct perf_counter *counter)
708 struct hw_perf_counter *hwc = &counter->hw;
711 x86_perf_counter_update(counter, hwc, idx);
712 x86_perf_counter_set_period(counter, hwc, idx);
714 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
715 intel_pmu_enable_counter(hwc, idx);
719 * Maximum interrupt frequency of 100KHz per CPU
721 #define PERFMON_MAX_INTERRUPTS (100000/HZ)
724 * This handler is triggered by the local APIC, so the APIC IRQ handling
727 static int intel_pmu_handle_irq(struct pt_regs *regs, int nmi)
729 int bit, cpu = smp_processor_id();
731 struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
734 cpuc->throttle_ctrl = intel_pmu_save_disable_all();
736 status = intel_pmu_get_status(cpuc->throttle_ctrl);
742 inc_irq_stat(apic_perf_irqs);
744 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
745 struct perf_counter *counter = cpuc->counters[bit];
747 clear_bit(bit, (unsigned long *) &status);
748 if (!test_bit(bit, cpuc->active))
751 intel_pmu_save_and_restart(counter);
752 if (perf_counter_overflow(counter, nmi, regs, 0))
753 intel_pmu_disable_counter(&counter->hw, bit);
756 intel_pmu_ack_status(ack);
759 * Repeat if there is more work to be done:
761 status = intel_pmu_get_status(cpuc->throttle_ctrl);
766 * Restore - do not reenable when global enable is off or throttled:
768 if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
769 intel_pmu_restore_all(cpuc->throttle_ctrl);
774 static int amd_pmu_handle_irq(struct pt_regs *regs, int nmi)
776 int cpu = smp_processor_id();
777 struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
780 struct perf_counter *counter;
781 struct hw_perf_counter *hwc;
785 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
786 if (!test_bit(idx, cpuc->active))
788 counter = cpuc->counters[idx];
790 val = x86_perf_counter_update(counter, hwc, idx);
791 if (val & (1ULL << (x86_pmu.counter_bits - 1)))
793 /* counter overflow */
794 x86_perf_counter_set_period(counter, hwc, idx);
796 inc_irq_stat(apic_perf_irqs);
797 if (perf_counter_overflow(counter, nmi, regs, 0))
798 amd_pmu_disable_counter(hwc, idx);
799 else if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS)
801 * do not reenable when throttled, but reload
804 amd_pmu_disable_counter(hwc, idx);
805 else if (counter->state == PERF_COUNTER_STATE_ACTIVE)
806 amd_pmu_enable_counter(hwc, idx);
811 void perf_counter_unthrottle(void)
813 struct cpu_hw_counters *cpuc;
815 if (!x86_pmu_initialized())
818 cpuc = &__get_cpu_var(cpu_hw_counters);
819 if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
820 if (printk_ratelimit())
821 printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
822 hw_perf_restore(cpuc->throttle_ctrl);
824 cpuc->interrupts = 0;
827 void smp_perf_counter_interrupt(struct pt_regs *regs)
830 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
832 x86_pmu.handle_irq(regs, 0);
836 void smp_perf_pending_interrupt(struct pt_regs *regs)
840 inc_irq_stat(apic_pending_irqs);
841 perf_counter_do_pending();
845 void set_perf_counter_pending(void)
847 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
850 void perf_counters_lapic_init(int nmi)
854 if (!x86_pmu_initialized())
858 * Enable the performance counter vector in the APIC LVT:
860 apic_val = apic_read(APIC_LVTERR);
862 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
864 apic_write(APIC_LVTPC, APIC_DM_NMI);
866 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
867 apic_write(APIC_LVTERR, apic_val);
871 perf_counter_nmi_handler(struct notifier_block *self,
872 unsigned long cmd, void *__args)
874 struct die_args *args = __args;
875 struct pt_regs *regs;
889 apic_write(APIC_LVTPC, APIC_DM_NMI);
890 ret = x86_pmu.handle_irq(regs, 1);
892 return ret ? NOTIFY_STOP : NOTIFY_OK;
895 static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
896 .notifier_call = perf_counter_nmi_handler,
901 static struct x86_pmu intel_pmu = {
903 .handle_irq = intel_pmu_handle_irq,
904 .save_disable_all = intel_pmu_save_disable_all,
905 .restore_all = intel_pmu_restore_all,
906 .enable = intel_pmu_enable_counter,
907 .disable = intel_pmu_disable_counter,
908 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
909 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
910 .event_map = intel_pmu_event_map,
911 .raw_event = intel_pmu_raw_event,
912 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
915 static struct x86_pmu amd_pmu = {
917 .handle_irq = amd_pmu_handle_irq,
918 .save_disable_all = amd_pmu_save_disable_all,
919 .restore_all = amd_pmu_restore_all,
920 .enable = amd_pmu_enable_counter,
921 .disable = amd_pmu_disable_counter,
922 .eventsel = MSR_K7_EVNTSEL0,
923 .perfctr = MSR_K7_PERFCTR0,
924 .event_map = amd_pmu_event_map,
925 .raw_event = amd_pmu_raw_event,
926 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
929 .counter_mask = (1ULL << 48) - 1,
932 static int intel_pmu_init(void)
934 union cpuid10_edx edx;
935 union cpuid10_eax eax;
940 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
944 * Check whether the Architectural PerfMon supports
945 * Branch Misses Retired Event or not.
947 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
948 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
951 version = eax.split.version_id;
956 x86_pmu.version = version;
957 x86_pmu.num_counters = eax.split.num_counters;
958 x86_pmu.num_counters_fixed = edx.split.num_counters_fixed;
959 x86_pmu.counter_bits = eax.split.bit_width;
960 x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1;
965 static int amd_pmu_init(void)
971 void __init init_hw_perf_counters(void)
975 switch (boot_cpu_data.x86_vendor) {
976 case X86_VENDOR_INTEL:
977 err = intel_pmu_init();
980 err = amd_pmu_init();
988 pr_info("%s Performance Monitoring support detected.\n", x86_pmu.name);
989 pr_info("... version: %d\n", x86_pmu.version);
990 pr_info("... bit width: %d\n", x86_pmu.counter_bits);
992 pr_info("... num counters: %d\n", x86_pmu.num_counters);
993 if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) {
994 x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
995 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
996 x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
998 perf_counter_mask = (1 << x86_pmu.num_counters) - 1;
999 perf_max_counters = x86_pmu.num_counters;
1001 pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask);
1003 if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
1004 x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
1005 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
1006 x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED);
1008 pr_info("... fixed counters: %d\n", x86_pmu.num_counters_fixed);
1010 perf_counter_mask |=
1011 ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
1013 pr_info("... counter mask: %016Lx\n", perf_counter_mask);
1015 perf_counters_lapic_init(0);
1016 register_die_notifier(&perf_counter_nmi_notifier);
1019 static inline void x86_pmu_read(struct perf_counter *counter)
1021 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
1024 static const struct pmu pmu = {
1025 .enable = x86_pmu_enable,
1026 .disable = x86_pmu_disable,
1027 .read = x86_pmu_read,
1030 const struct pmu *hw_perf_counter_init(struct perf_counter *counter)
1034 err = __hw_perf_counter_init(counter);
1036 return ERR_PTR(err);
1046 void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
1048 if (entry->nr < MAX_STACK_DEPTH)
1049 entry->ip[entry->nr++] = ip;
1052 static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
1053 static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
1057 backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1059 /* Ignore warnings */
1062 static void backtrace_warning(void *data, char *msg)
1064 /* Ignore warnings */
1067 static int backtrace_stack(void *data, char *name)
1069 /* Don't bother with IRQ stacks for now */
1073 static void backtrace_address(void *data, unsigned long addr, int reliable)
1075 struct perf_callchain_entry *entry = data;
1078 callchain_store(entry, addr);
1081 static const struct stacktrace_ops backtrace_ops = {
1082 .warning = backtrace_warning,
1083 .warning_symbol = backtrace_warning_symbol,
1084 .stack = backtrace_stack,
1085 .address = backtrace_address,
1089 perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1095 callchain_store(entry, instruction_pointer(regs));
1097 stack = ((char *)regs + sizeof(struct pt_regs));
1098 #ifdef CONFIG_FRAME_POINTER
1099 bp = frame_pointer(regs);
1104 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
1106 entry->kernel = entry->nr - nr;
1110 struct stack_frame {
1111 const void __user *next_fp;
1112 unsigned long return_address;
1115 static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
1119 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
1123 pagefault_disable();
1124 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
1132 perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1134 struct stack_frame frame;
1135 const void __user *fp;
1138 regs = (struct pt_regs *)current->thread.sp0 - 1;
1139 fp = (void __user *)regs->bp;
1141 callchain_store(entry, regs->ip);
1143 while (entry->nr < MAX_STACK_DEPTH) {
1144 frame.next_fp = NULL;
1145 frame.return_address = 0;
1147 if (!copy_stack_frame(fp, &frame))
1150 if ((unsigned long)fp < user_stack_pointer(regs))
1153 callchain_store(entry, frame.return_address);
1157 entry->user = entry->nr - nr;
1161 perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1168 is_user = user_mode(regs);
1170 if (!current || current->pid == 0)
1173 if (is_user && current->state != TASK_RUNNING)
1177 perf_callchain_kernel(regs, entry);
1180 perf_callchain_user(regs, entry);
1183 struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1185 struct perf_callchain_entry *entry;
1188 entry = &__get_cpu_var(nmi_entry);
1190 entry = &__get_cpu_var(irq_entry);
1197 perf_do_callchain(regs, entry);