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
8 * For licencing details see kernel-base/COPYING
11 #include <linux/perf_counter.h>
12 #include <linux/capability.h>
13 #include <linux/notifier.h>
14 #include <linux/hardirq.h>
15 #include <linux/kprobes.h>
16 #include <linux/module.h>
17 #include <linux/kdebug.h>
18 #include <linux/sched.h>
19 #include <linux/uaccess.h>
22 #include <asm/stacktrace.h>
25 static bool perf_counters_initialized __read_mostly;
28 * Number of (generic) HW counters:
30 static int nr_counters_generic __read_mostly;
31 static u64 perf_counter_mask __read_mostly;
32 static u64 counter_value_mask __read_mostly;
33 static int counter_value_bits __read_mostly;
35 static int nr_counters_fixed __read_mostly;
37 struct cpu_hw_counters {
38 struct perf_counter *counters[X86_PMC_IDX_MAX];
39 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
40 unsigned long interrupts;
42 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
47 * struct pmc_x86_ops - performance counter x86 ops
50 u64 (*save_disable_all)(void);
51 void (*restore_all)(u64);
52 u64 (*get_status)(u64);
53 void (*ack_status)(u64);
54 void (*enable)(int, u64);
55 void (*disable)(int, u64);
58 u64 (*event_map)(int);
59 u64 (*raw_event)(u64);
63 static struct pmc_x86_ops *pmc_ops __read_mostly;
65 static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = {
69 static __read_mostly int intel_perfmon_version;
72 * Intel PerfMon v3. Used on Core2 and later.
74 static const u64 intel_perfmon_event_map[] =
76 [PERF_COUNT_CPU_CYCLES] = 0x003c,
77 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
78 [PERF_COUNT_CACHE_REFERENCES] = 0x4f2e,
79 [PERF_COUNT_CACHE_MISSES] = 0x412e,
80 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
81 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
82 [PERF_COUNT_BUS_CYCLES] = 0x013c,
85 static u64 pmc_intel_event_map(int event)
87 return intel_perfmon_event_map[event];
90 static u64 pmc_intel_raw_event(u64 event)
92 #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
93 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
94 #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
96 #define CORE_EVNTSEL_MASK \
97 (CORE_EVNTSEL_EVENT_MASK | \
98 CORE_EVNTSEL_UNIT_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 pmc_amd_event_map(int event)
119 return amd_perfmon_event_map[event];
122 static u64 pmc_amd_raw_event(u64 event)
124 #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
125 #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
126 #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
128 #define K7_EVNTSEL_MASK \
129 (K7_EVNTSEL_EVENT_MASK | \
130 K7_EVNTSEL_UNIT_MASK | \
131 K7_EVNTSEL_COUNTER_MASK)
133 return event & K7_EVNTSEL_MASK;
137 * Propagate counter elapsed time into the generic counter.
138 * Can only be executed on the CPU where the counter is active.
139 * Returns the delta events processed.
142 x86_perf_counter_update(struct perf_counter *counter,
143 struct hw_perf_counter *hwc, int idx)
145 u64 prev_raw_count, new_raw_count, delta;
148 * Careful: an NMI might modify the previous counter value.
150 * Our tactic to handle this is to first atomically read and
151 * exchange a new raw count - then add that new-prev delta
152 * count to the generic counter atomically:
155 prev_raw_count = atomic64_read(&hwc->prev_count);
156 rdmsrl(hwc->counter_base + idx, new_raw_count);
158 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
159 new_raw_count) != prev_raw_count)
163 * Now we have the new raw value and have updated the prev
164 * timestamp already. We can now calculate the elapsed delta
165 * (counter-)time and add that to the generic counter.
167 * Careful, not all hw sign-extends above the physical width
168 * of the count, so we do that by clipping the delta to 32 bits:
170 delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
172 atomic64_add(delta, &counter->count);
173 atomic64_sub(delta, &hwc->period_left);
176 static atomic_t num_counters;
177 static DEFINE_MUTEX(pmc_reserve_mutex);
179 static bool reserve_pmc_hardware(void)
183 if (nmi_watchdog == NMI_LOCAL_APIC)
184 disable_lapic_nmi_watchdog();
186 for (i = 0; i < nr_counters_generic; i++) {
187 if (!reserve_perfctr_nmi(pmc_ops->perfctr + i))
191 for (i = 0; i < nr_counters_generic; i++) {
192 if (!reserve_evntsel_nmi(pmc_ops->eventsel + i))
199 for (i--; i >= 0; i--)
200 release_evntsel_nmi(pmc_ops->eventsel + i);
202 i = nr_counters_generic;
205 for (i--; i >= 0; i--)
206 release_perfctr_nmi(pmc_ops->perfctr + i);
208 if (nmi_watchdog == NMI_LOCAL_APIC)
209 enable_lapic_nmi_watchdog();
214 static void release_pmc_hardware(void)
218 for (i = 0; i < nr_counters_generic; i++) {
219 release_perfctr_nmi(pmc_ops->perfctr + i);
220 release_evntsel_nmi(pmc_ops->eventsel + i);
223 if (nmi_watchdog == NMI_LOCAL_APIC)
224 enable_lapic_nmi_watchdog();
227 static void hw_perf_counter_destroy(struct perf_counter *counter)
229 if (atomic_dec_and_mutex_lock(&num_counters, &pmc_reserve_mutex)) {
230 release_pmc_hardware();
231 mutex_unlock(&pmc_reserve_mutex);
236 * Setup the hardware configuration for a given hw_event_type
238 static int __hw_perf_counter_init(struct perf_counter *counter)
240 struct perf_counter_hw_event *hw_event = &counter->hw_event;
241 struct hw_perf_counter *hwc = &counter->hw;
244 if (unlikely(!perf_counters_initialized))
248 if (atomic_inc_not_zero(&num_counters)) {
249 mutex_lock(&pmc_reserve_mutex);
250 if (atomic_read(&num_counters) == 0 && !reserve_pmc_hardware())
253 atomic_inc(&num_counters);
254 mutex_unlock(&pmc_reserve_mutex);
261 * (keep 'enabled' bit clear for now)
263 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
266 * Count user and OS events unless requested not to.
268 if (!hw_event->exclude_user)
269 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
270 if (!hw_event->exclude_kernel)
271 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
274 * If privileged enough, allow NMI events:
277 if (capable(CAP_SYS_ADMIN) && hw_event->nmi)
280 hwc->irq_period = hw_event->irq_period;
282 * Intel PMCs cannot be accessed sanely above 32 bit width,
283 * so we install an artificial 1<<31 period regardless of
284 * the generic counter period:
286 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
287 if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
288 hwc->irq_period = 0x7FFFFFFF;
290 atomic64_set(&hwc->period_left, hwc->irq_period);
293 * Raw event type provide the config in the event structure
295 if (perf_event_raw(hw_event)) {
296 hwc->config |= pmc_ops->raw_event(perf_event_config(hw_event));
298 if (perf_event_id(hw_event) >= pmc_ops->max_events)
303 hwc->config |= pmc_ops->event_map(perf_event_id(hw_event));
306 counter->destroy = hw_perf_counter_destroy;
311 static u64 pmc_intel_save_disable_all(void)
315 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
316 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
321 static u64 pmc_amd_save_disable_all(void)
323 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
326 enabled = cpuc->enabled;
329 * ensure we write the disable before we start disabling the
330 * counters proper, so that pcm_amd_enable() does the right thing.
334 for (idx = 0; idx < nr_counters_generic; idx++) {
337 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
338 if (val & ARCH_PERFMON_EVENTSEL0_ENABLE) {
339 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
340 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
347 u64 hw_perf_save_disable(void)
349 if (unlikely(!perf_counters_initialized))
352 return pmc_ops->save_disable_all();
355 * Exported because of ACPI idle
357 EXPORT_SYMBOL_GPL(hw_perf_save_disable);
359 static void pmc_intel_restore_all(u64 ctrl)
361 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
364 static void pmc_amd_restore_all(u64 ctrl)
366 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
369 cpuc->enabled = ctrl;
374 for (idx = 0; idx < nr_counters_generic; idx++) {
375 if (test_bit(idx, cpuc->active_mask)) {
378 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
379 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
380 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
385 void hw_perf_restore(u64 ctrl)
387 if (unlikely(!perf_counters_initialized))
390 pmc_ops->restore_all(ctrl);
393 * Exported because of ACPI idle
395 EXPORT_SYMBOL_GPL(hw_perf_restore);
397 static u64 pmc_intel_get_status(u64 mask)
401 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
406 static u64 pmc_amd_get_status(u64 mask)
411 for (idx = 0; idx < nr_counters_generic; idx++) {
414 if (!(mask & (1 << idx)))
417 rdmsrl(MSR_K7_PERFCTR0 + idx, val);
418 val <<= (64 - counter_value_bits);
420 status |= (1 << idx);
426 static u64 hw_perf_get_status(u64 mask)
428 if (unlikely(!perf_counters_initialized))
431 return pmc_ops->get_status(mask);
434 static void pmc_intel_ack_status(u64 ack)
436 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
439 static void pmc_amd_ack_status(u64 ack)
443 static void hw_perf_ack_status(u64 ack)
445 if (unlikely(!perf_counters_initialized))
448 pmc_ops->ack_status(ack);
451 static void pmc_intel_enable(int idx, u64 config)
453 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx,
454 config | ARCH_PERFMON_EVENTSEL0_ENABLE);
457 static void pmc_amd_enable(int idx, u64 config)
459 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
461 set_bit(idx, cpuc->active_mask);
463 config |= ARCH_PERFMON_EVENTSEL0_ENABLE;
465 wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
468 static void hw_perf_enable(int idx, u64 config)
470 if (unlikely(!perf_counters_initialized))
473 pmc_ops->enable(idx, config);
476 static void pmc_intel_disable(int idx, u64 config)
478 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config);
481 static void pmc_amd_disable(int idx, u64 config)
483 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
485 clear_bit(idx, cpuc->active_mask);
486 wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
490 static void hw_perf_disable(int idx, u64 config)
492 if (unlikely(!perf_counters_initialized))
495 pmc_ops->disable(idx, config);
499 __pmc_fixed_disable(struct perf_counter *counter,
500 struct hw_perf_counter *hwc, unsigned int __idx)
502 int idx = __idx - X86_PMC_IDX_FIXED;
506 mask = 0xfULL << (idx * 4);
508 rdmsrl(hwc->config_base, ctrl_val);
510 err = checking_wrmsrl(hwc->config_base, ctrl_val);
514 __pmc_generic_disable(struct perf_counter *counter,
515 struct hw_perf_counter *hwc, unsigned int idx)
517 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
518 __pmc_fixed_disable(counter, hwc, idx);
520 hw_perf_disable(idx, hwc->config);
523 static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
526 * Set the next IRQ period, based on the hwc->period_left value.
527 * To be called with the counter disabled in hw:
530 __hw_perf_counter_set_period(struct perf_counter *counter,
531 struct hw_perf_counter *hwc, int idx)
533 s64 left = atomic64_read(&hwc->period_left);
534 s64 period = hwc->irq_period;
538 * If we are way outside a reasoable range then just skip forward:
540 if (unlikely(left <= -period)) {
542 atomic64_set(&hwc->period_left, left);
545 if (unlikely(left <= 0)) {
547 atomic64_set(&hwc->period_left, left);
550 per_cpu(prev_left[idx], smp_processor_id()) = left;
553 * The hw counter starts counting from this counter offset,
554 * mark it to be able to extra future deltas:
556 atomic64_set(&hwc->prev_count, (u64)-left);
558 err = checking_wrmsrl(hwc->counter_base + idx,
559 (u64)(-left) & counter_value_mask);
563 __pmc_fixed_enable(struct perf_counter *counter,
564 struct hw_perf_counter *hwc, unsigned int __idx)
566 int idx = __idx - X86_PMC_IDX_FIXED;
567 u64 ctrl_val, bits, mask;
571 * Enable IRQ generation (0x8),
572 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
576 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
578 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
581 mask = 0xfULL << (idx * 4);
583 rdmsrl(hwc->config_base, ctrl_val);
586 err = checking_wrmsrl(hwc->config_base, ctrl_val);
590 __pmc_generic_enable(struct perf_counter *counter,
591 struct hw_perf_counter *hwc, int idx)
593 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
594 __pmc_fixed_enable(counter, hwc, idx);
596 hw_perf_enable(idx, hwc->config);
600 fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
604 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
607 if (unlikely(hwc->nmi))
610 event = hwc->config & ARCH_PERFMON_EVENT_MASK;
612 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_INSTRUCTIONS)))
613 return X86_PMC_IDX_FIXED_INSTRUCTIONS;
614 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_CPU_CYCLES)))
615 return X86_PMC_IDX_FIXED_CPU_CYCLES;
616 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_BUS_CYCLES)))
617 return X86_PMC_IDX_FIXED_BUS_CYCLES;
623 * Find a PMC slot for the freshly enabled / scheduled in counter:
625 static int pmc_generic_enable(struct perf_counter *counter)
627 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
628 struct hw_perf_counter *hwc = &counter->hw;
631 idx = fixed_mode_idx(counter, hwc);
634 * Try to get the fixed counter, if that is already taken
635 * then try to get a generic counter:
637 if (test_and_set_bit(idx, cpuc->used))
640 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
642 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
643 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
646 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
650 /* Try to get the previous generic counter again */
651 if (test_and_set_bit(idx, cpuc->used)) {
653 idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
654 if (idx == nr_counters_generic)
657 set_bit(idx, cpuc->used);
660 hwc->config_base = pmc_ops->eventsel;
661 hwc->counter_base = pmc_ops->perfctr;
664 perf_counters_lapic_init(hwc->nmi);
666 __pmc_generic_disable(counter, hwc, idx);
668 cpuc->counters[idx] = counter;
670 * Make it visible before enabling the hw:
674 __hw_perf_counter_set_period(counter, hwc, idx);
675 __pmc_generic_enable(counter, hwc, idx);
680 void perf_counter_print_debug(void)
682 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
683 struct cpu_hw_counters *cpuc;
686 if (!nr_counters_generic)
691 cpu = smp_processor_id();
692 cpuc = &per_cpu(cpu_hw_counters, cpu);
694 if (intel_perfmon_version >= 2) {
695 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
696 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
697 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
698 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
701 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
702 pr_info("CPU#%d: status: %016llx\n", cpu, status);
703 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
704 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
706 pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
708 for (idx = 0; idx < nr_counters_generic; idx++) {
709 rdmsrl(pmc_ops->eventsel + idx, pmc_ctrl);
710 rdmsrl(pmc_ops->perfctr + idx, pmc_count);
712 prev_left = per_cpu(prev_left[idx], cpu);
714 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
716 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
717 cpu, idx, pmc_count);
718 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
719 cpu, idx, prev_left);
721 for (idx = 0; idx < nr_counters_fixed; idx++) {
722 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
724 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
725 cpu, idx, pmc_count);
730 static void pmc_generic_disable(struct perf_counter *counter)
732 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
733 struct hw_perf_counter *hwc = &counter->hw;
734 unsigned int idx = hwc->idx;
736 __pmc_generic_disable(counter, hwc, idx);
738 clear_bit(idx, cpuc->used);
739 cpuc->counters[idx] = NULL;
741 * Make sure the cleared pointer becomes visible before we
742 * (potentially) free the counter:
747 * Drain the remaining delta count out of a counter
748 * that we are disabling:
750 x86_perf_counter_update(counter, hwc, idx);
754 * Save and restart an expired counter. Called by NMI contexts,
755 * so it has to be careful about preempting normal counter ops:
757 static void perf_save_and_restart(struct perf_counter *counter)
759 struct hw_perf_counter *hwc = &counter->hw;
762 x86_perf_counter_update(counter, hwc, idx);
763 __hw_perf_counter_set_period(counter, hwc, idx);
765 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
766 __pmc_generic_enable(counter, hwc, idx);
770 * Maximum interrupt frequency of 100KHz per CPU
772 #define PERFMON_MAX_INTERRUPTS (100000/HZ)
775 * This handler is triggered by the local APIC, so the APIC IRQ handling
778 static int __smp_perf_counter_interrupt(struct pt_regs *regs, int nmi)
780 int bit, cpu = smp_processor_id();
782 struct cpu_hw_counters *cpuc = &per_cpu(cpu_hw_counters, cpu);
785 cpuc->throttle_ctrl = hw_perf_save_disable();
787 status = hw_perf_get_status(cpuc->throttle_ctrl);
793 inc_irq_stat(apic_perf_irqs);
795 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
796 struct perf_counter *counter = cpuc->counters[bit];
798 clear_bit(bit, (unsigned long *) &status);
802 perf_save_and_restart(counter);
803 if (perf_counter_overflow(counter, nmi, regs, 0))
804 __pmc_generic_disable(counter, &counter->hw, bit);
807 hw_perf_ack_status(ack);
810 * Repeat if there is more work to be done:
812 status = hw_perf_get_status(cpuc->throttle_ctrl);
817 * Restore - do not reenable when global enable is off or throttled:
819 if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
820 hw_perf_restore(cpuc->throttle_ctrl);
825 void perf_counter_unthrottle(void)
827 struct cpu_hw_counters *cpuc;
829 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
832 if (unlikely(!perf_counters_initialized))
835 cpuc = &__get_cpu_var(cpu_hw_counters);
836 if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
837 if (printk_ratelimit())
838 printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
839 hw_perf_restore(cpuc->throttle_ctrl);
841 cpuc->interrupts = 0;
844 void smp_perf_counter_interrupt(struct pt_regs *regs)
847 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
849 __smp_perf_counter_interrupt(regs, 0);
853 void smp_perf_pending_interrupt(struct pt_regs *regs)
857 inc_irq_stat(apic_pending_irqs);
858 perf_counter_do_pending();
862 void set_perf_counter_pending(void)
864 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
867 void perf_counters_lapic_init(int nmi)
871 if (!perf_counters_initialized)
874 * Enable the performance counter vector in the APIC LVT:
876 apic_val = apic_read(APIC_LVTERR);
878 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
880 apic_write(APIC_LVTPC, APIC_DM_NMI);
882 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
883 apic_write(APIC_LVTERR, apic_val);
887 perf_counter_nmi_handler(struct notifier_block *self,
888 unsigned long cmd, void *__args)
890 struct die_args *args = __args;
891 struct pt_regs *regs;
905 apic_write(APIC_LVTPC, APIC_DM_NMI);
906 ret = __smp_perf_counter_interrupt(regs, 1);
908 return ret ? NOTIFY_STOP : NOTIFY_OK;
911 static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
912 .notifier_call = perf_counter_nmi_handler,
917 static struct pmc_x86_ops pmc_intel_ops = {
918 .save_disable_all = pmc_intel_save_disable_all,
919 .restore_all = pmc_intel_restore_all,
920 .get_status = pmc_intel_get_status,
921 .ack_status = pmc_intel_ack_status,
922 .enable = pmc_intel_enable,
923 .disable = pmc_intel_disable,
924 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
925 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
926 .event_map = pmc_intel_event_map,
927 .raw_event = pmc_intel_raw_event,
928 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
931 static struct pmc_x86_ops pmc_amd_ops = {
932 .save_disable_all = pmc_amd_save_disable_all,
933 .restore_all = pmc_amd_restore_all,
934 .get_status = pmc_amd_get_status,
935 .ack_status = pmc_amd_ack_status,
936 .enable = pmc_amd_enable,
937 .disable = pmc_amd_disable,
938 .eventsel = MSR_K7_EVNTSEL0,
939 .perfctr = MSR_K7_PERFCTR0,
940 .event_map = pmc_amd_event_map,
941 .raw_event = pmc_amd_raw_event,
942 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
945 static struct pmc_x86_ops *pmc_intel_init(void)
947 union cpuid10_edx edx;
948 union cpuid10_eax eax;
953 * Check whether the Architectural PerfMon supports
954 * Branch Misses Retired Event or not.
956 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
957 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
960 intel_perfmon_version = eax.split.version_id;
961 if (intel_perfmon_version < 2)
964 pr_info("Intel Performance Monitoring support detected.\n");
965 pr_info("... version: %d\n", intel_perfmon_version);
966 pr_info("... bit width: %d\n", eax.split.bit_width);
967 pr_info("... mask length: %d\n", eax.split.mask_length);
969 nr_counters_generic = eax.split.num_counters;
970 nr_counters_fixed = edx.split.num_counters_fixed;
971 counter_value_mask = (1ULL << eax.split.bit_width) - 1;
973 return &pmc_intel_ops;
976 static struct pmc_x86_ops *pmc_amd_init(void)
978 nr_counters_generic = 4;
979 nr_counters_fixed = 0;
980 counter_value_mask = 0x0000FFFFFFFFFFFFULL;
981 counter_value_bits = 48;
983 pr_info("AMD Performance Monitoring support detected.\n");
988 void __init init_hw_perf_counters(void)
990 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
993 switch (boot_cpu_data.x86_vendor) {
994 case X86_VENDOR_INTEL:
995 pmc_ops = pmc_intel_init();
998 pmc_ops = pmc_amd_init();
1004 pr_info("... num counters: %d\n", nr_counters_generic);
1005 if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
1006 nr_counters_generic = X86_PMC_MAX_GENERIC;
1007 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
1008 nr_counters_generic, X86_PMC_MAX_GENERIC);
1010 perf_counter_mask = (1 << nr_counters_generic) - 1;
1011 perf_max_counters = nr_counters_generic;
1013 pr_info("... value mask: %016Lx\n", counter_value_mask);
1015 if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
1016 nr_counters_fixed = X86_PMC_MAX_FIXED;
1017 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
1018 nr_counters_fixed, X86_PMC_MAX_FIXED);
1020 pr_info("... fixed counters: %d\n", nr_counters_fixed);
1022 perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
1024 pr_info("... counter mask: %016Lx\n", perf_counter_mask);
1025 perf_counters_initialized = true;
1027 perf_counters_lapic_init(0);
1028 register_die_notifier(&perf_counter_nmi_notifier);
1031 static void pmc_generic_read(struct perf_counter *counter)
1033 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
1036 static const struct hw_perf_counter_ops x86_perf_counter_ops = {
1037 .enable = pmc_generic_enable,
1038 .disable = pmc_generic_disable,
1039 .read = pmc_generic_read,
1042 const struct hw_perf_counter_ops *
1043 hw_perf_counter_init(struct perf_counter *counter)
1047 err = __hw_perf_counter_init(counter);
1049 return ERR_PTR(err);
1051 return &x86_perf_counter_ops;
1059 void callchain_store(struct perf_callchain_entry *entry, unsigned long ip)
1061 if (entry->nr < MAX_STACK_DEPTH)
1062 entry->ip[entry->nr++] = ip;
1065 static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry);
1066 static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry);
1070 backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
1072 /* Ignore warnings */
1075 static void backtrace_warning(void *data, char *msg)
1077 /* Ignore warnings */
1080 static int backtrace_stack(void *data, char *name)
1082 /* Don't bother with IRQ stacks for now */
1086 static void backtrace_address(void *data, unsigned long addr, int reliable)
1088 struct perf_callchain_entry *entry = data;
1091 callchain_store(entry, addr);
1094 static const struct stacktrace_ops backtrace_ops = {
1095 .warning = backtrace_warning,
1096 .warning_symbol = backtrace_warning_symbol,
1097 .stack = backtrace_stack,
1098 .address = backtrace_address,
1102 perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
1108 callchain_store(entry, instruction_pointer(regs));
1110 stack = ((char *)regs + sizeof(struct pt_regs));
1111 #ifdef CONFIG_FRAME_POINTER
1112 bp = frame_pointer(regs);
1117 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, entry);
1119 entry->kernel = entry->nr - nr;
1123 struct stack_frame {
1124 const void __user *next_fp;
1125 unsigned long return_address;
1128 static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
1132 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
1136 pagefault_disable();
1137 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
1145 perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
1147 struct stack_frame frame;
1148 const void __user *fp;
1151 regs = (struct pt_regs *)current->thread.sp0 - 1;
1152 fp = (void __user *)regs->bp;
1154 callchain_store(entry, regs->ip);
1156 while (entry->nr < MAX_STACK_DEPTH) {
1157 frame.next_fp = NULL;
1158 frame.return_address = 0;
1160 if (!copy_stack_frame(fp, &frame))
1163 if ((unsigned long)fp < user_stack_pointer(regs))
1166 callchain_store(entry, frame.return_address);
1170 entry->user = entry->nr - nr;
1174 perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
1181 is_user = user_mode(regs);
1183 if (!current || current->pid == 0)
1186 if (is_user && current->state != TASK_RUNNING)
1190 perf_callchain_kernel(regs, entry);
1193 perf_callchain_user(regs, entry);
1196 struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1198 struct perf_callchain_entry *entry;
1201 entry = &__get_cpu_var(nmi_entry);
1203 entry = &__get_cpu_var(irq_entry);
1210 perf_do_callchain(regs, entry);