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>
20 #include <asm/perf_counter.h>
23 static bool perf_counters_initialized __read_mostly;
26 * Number of (generic) HW counters:
28 static int nr_counters_generic __read_mostly;
29 static u64 perf_counter_mask __read_mostly;
30 static u64 counter_value_mask __read_mostly;
31 static int counter_value_bits __read_mostly;
33 static int nr_counters_fixed __read_mostly;
35 struct cpu_hw_counters {
36 struct perf_counter *counters[X86_PMC_IDX_MAX];
37 unsigned long used[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
38 unsigned long interrupts;
45 * struct pmc_x86_ops - performance counter x86 ops
48 u64 (*save_disable_all)(void);
49 void (*restore_all)(u64);
50 u64 (*get_status)(u64);
51 void (*ack_status)(u64);
52 void (*enable)(int, u64);
53 void (*disable)(int, u64);
56 u64 (*event_map)(int);
57 u64 (*raw_event)(u64);
61 static struct pmc_x86_ops *pmc_ops;
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 pmc_intel_event_map(int event)
83 return intel_perfmon_event_map[event];
86 static u64 pmc_intel_raw_event(u64 event)
88 #define CORE_EVNTSEL_EVENT_MASK 0x000000FF
89 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00
90 #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000
92 #define CORE_EVNTSEL_MASK \
93 (CORE_EVNTSEL_EVENT_MASK | \
94 CORE_EVNTSEL_UNIT_MASK | \
95 CORE_EVNTSEL_COUNTER_MASK)
97 return event & CORE_EVNTSEL_MASK;
101 * AMD Performance Monitor K7 and later.
103 static const u64 amd_perfmon_event_map[] =
105 [PERF_COUNT_CPU_CYCLES] = 0x0076,
106 [PERF_COUNT_INSTRUCTIONS] = 0x00c0,
107 [PERF_COUNT_CACHE_REFERENCES] = 0x0080,
108 [PERF_COUNT_CACHE_MISSES] = 0x0081,
109 [PERF_COUNT_BRANCH_INSTRUCTIONS] = 0x00c4,
110 [PERF_COUNT_BRANCH_MISSES] = 0x00c5,
113 static u64 pmc_amd_event_map(int event)
115 return amd_perfmon_event_map[event];
118 static u64 pmc_amd_raw_event(u64 event)
120 #define K7_EVNTSEL_EVENT_MASK 0x7000000FF
121 #define K7_EVNTSEL_UNIT_MASK 0x00000FF00
122 #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000
124 #define K7_EVNTSEL_MASK \
125 (K7_EVNTSEL_EVENT_MASK | \
126 K7_EVNTSEL_UNIT_MASK | \
127 K7_EVNTSEL_COUNTER_MASK)
129 return event & K7_EVNTSEL_MASK;
133 * Propagate counter elapsed time into the generic counter.
134 * Can only be executed on the CPU where the counter is active.
135 * Returns the delta events processed.
138 x86_perf_counter_update(struct perf_counter *counter,
139 struct hw_perf_counter *hwc, int idx)
141 u64 prev_raw_count, new_raw_count, delta;
144 * Careful: an NMI might modify the previous counter value.
146 * Our tactic to handle this is to first atomically read and
147 * exchange a new raw count - then add that new-prev delta
148 * count to the generic counter atomically:
151 prev_raw_count = atomic64_read(&hwc->prev_count);
152 rdmsrl(hwc->counter_base + idx, new_raw_count);
154 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
155 new_raw_count) != prev_raw_count)
159 * Now we have the new raw value and have updated the prev
160 * timestamp already. We can now calculate the elapsed delta
161 * (counter-)time and add that to the generic counter.
163 * Careful, not all hw sign-extends above the physical width
164 * of the count, so we do that by clipping the delta to 32 bits:
166 delta = (u64)(u32)((s32)new_raw_count - (s32)prev_raw_count);
168 atomic64_add(delta, &counter->count);
169 atomic64_sub(delta, &hwc->period_left);
173 * Setup the hardware configuration for a given hw_event_type
175 static int __hw_perf_counter_init(struct perf_counter *counter)
177 struct perf_counter_hw_event *hw_event = &counter->hw_event;
178 struct hw_perf_counter *hwc = &counter->hw;
180 if (unlikely(!perf_counters_initialized))
185 * (keep 'enabled' bit clear for now)
187 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
190 * Count user and OS events unless requested not to.
192 if (!hw_event->exclude_user)
193 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
194 if (!hw_event->exclude_kernel)
195 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
198 * If privileged enough, allow NMI events:
201 if (capable(CAP_SYS_ADMIN) && hw_event->nmi)
204 hwc->irq_period = hw_event->irq_period;
206 * Intel PMCs cannot be accessed sanely above 32 bit width,
207 * so we install an artificial 1<<31 period regardless of
208 * the generic counter period:
210 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
211 if ((s64)hwc->irq_period <= 0 || hwc->irq_period > 0x7FFFFFFF)
212 hwc->irq_period = 0x7FFFFFFF;
214 atomic64_set(&hwc->period_left, hwc->irq_period);
217 * Raw event type provide the config in the event structure
220 hwc->config |= pmc_ops->raw_event(hw_event->type);
222 if (hw_event->type >= pmc_ops->max_events)
227 hwc->config |= pmc_ops->event_map(hw_event->type);
229 counter->wakeup_pending = 0;
234 static u64 pmc_intel_save_disable_all(void)
238 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
239 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
244 static u64 pmc_amd_save_disable_all(void)
246 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
249 enabled = cpuc->enabled;
253 for (idx = 0; idx < nr_counters_generic; idx++) {
256 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
257 if (val & ARCH_PERFMON_EVENTSEL0_ENABLE) {
258 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
259 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
266 u64 hw_perf_save_disable(void)
268 if (unlikely(!perf_counters_initialized))
271 return pmc_ops->save_disable_all();
274 * Exported because of ACPI idle
276 EXPORT_SYMBOL_GPL(hw_perf_save_disable);
278 static void pmc_intel_restore_all(u64 ctrl)
280 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
283 static void pmc_amd_restore_all(u64 ctrl)
285 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
288 cpuc->enabled = ctrl;
293 for (idx = 0; idx < nr_counters_generic; idx++) {
294 if (test_bit(idx, (unsigned long *)&cpuc->active_mask)) {
297 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
298 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
299 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
304 void hw_perf_restore(u64 ctrl)
306 if (unlikely(!perf_counters_initialized))
309 pmc_ops->restore_all(ctrl);
312 * Exported because of ACPI idle
314 EXPORT_SYMBOL_GPL(hw_perf_restore);
316 static u64 pmc_intel_get_status(u64 mask)
320 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
325 static u64 pmc_amd_get_status(u64 mask)
330 for (idx = 0; idx < nr_counters_generic; idx++) {
333 if (!(mask & (1 << idx)))
336 rdmsrl(MSR_K7_PERFCTR0 + idx, val);
337 val <<= (64 - counter_value_bits);
339 status |= (1 << idx);
345 static u64 hw_perf_get_status(u64 mask)
347 if (unlikely(!perf_counters_initialized))
350 return pmc_ops->get_status(mask);
353 static void pmc_intel_ack_status(u64 ack)
355 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
358 static void pmc_amd_ack_status(u64 ack)
362 static void hw_perf_ack_status(u64 ack)
364 if (unlikely(!perf_counters_initialized))
367 pmc_ops->ack_status(ack);
370 static void pmc_intel_enable(int idx, u64 config)
372 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx,
373 config | ARCH_PERFMON_EVENTSEL0_ENABLE);
376 static void pmc_amd_enable(int idx, u64 config)
378 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
380 set_bit(idx, (unsigned long *)&cpuc->active_mask);
382 config |= ARCH_PERFMON_EVENTSEL0_ENABLE;
384 wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
387 static void hw_perf_enable(int idx, u64 config)
389 if (unlikely(!perf_counters_initialized))
392 pmc_ops->enable(idx, config);
395 static void pmc_intel_disable(int idx, u64 config)
397 wrmsrl(MSR_ARCH_PERFMON_EVENTSEL0 + idx, config);
400 static void pmc_amd_disable(int idx, u64 config)
402 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
404 clear_bit(idx, (unsigned long *)&cpuc->active_mask);
405 wrmsrl(MSR_K7_EVNTSEL0 + idx, config);
409 static void hw_perf_disable(int idx, u64 config)
411 if (unlikely(!perf_counters_initialized))
414 pmc_ops->disable(idx, config);
418 __pmc_fixed_disable(struct perf_counter *counter,
419 struct hw_perf_counter *hwc, unsigned int __idx)
421 int idx = __idx - X86_PMC_IDX_FIXED;
425 mask = 0xfULL << (idx * 4);
427 rdmsrl(hwc->config_base, ctrl_val);
429 err = checking_wrmsrl(hwc->config_base, ctrl_val);
433 __pmc_generic_disable(struct perf_counter *counter,
434 struct hw_perf_counter *hwc, unsigned int idx)
436 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
437 __pmc_fixed_disable(counter, hwc, idx);
439 hw_perf_disable(idx, hwc->config);
442 static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]);
445 * Set the next IRQ period, based on the hwc->period_left value.
446 * To be called with the counter disabled in hw:
449 __hw_perf_counter_set_period(struct perf_counter *counter,
450 struct hw_perf_counter *hwc, int idx)
452 s64 left = atomic64_read(&hwc->period_left);
453 s32 period = hwc->irq_period;
457 * If we are way outside a reasoable range then just skip forward:
459 if (unlikely(left <= -period)) {
461 atomic64_set(&hwc->period_left, left);
464 if (unlikely(left <= 0)) {
466 atomic64_set(&hwc->period_left, left);
469 per_cpu(prev_left[idx], smp_processor_id()) = left;
472 * The hw counter starts counting from this counter offset,
473 * mark it to be able to extra future deltas:
475 atomic64_set(&hwc->prev_count, (u64)-left);
477 err = checking_wrmsrl(hwc->counter_base + idx,
478 (u64)(-left) & counter_value_mask);
482 __pmc_fixed_enable(struct perf_counter *counter,
483 struct hw_perf_counter *hwc, unsigned int __idx)
485 int idx = __idx - X86_PMC_IDX_FIXED;
486 u64 ctrl_val, bits, mask;
490 * Enable IRQ generation (0x8),
491 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
495 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
497 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
500 mask = 0xfULL << (idx * 4);
502 rdmsrl(hwc->config_base, ctrl_val);
505 err = checking_wrmsrl(hwc->config_base, ctrl_val);
509 __pmc_generic_enable(struct perf_counter *counter,
510 struct hw_perf_counter *hwc, int idx)
512 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL))
513 __pmc_fixed_enable(counter, hwc, idx);
515 hw_perf_enable(idx, hwc->config);
519 fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc)
523 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
526 if (unlikely(hwc->nmi))
529 event = hwc->config & ARCH_PERFMON_EVENT_MASK;
531 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_INSTRUCTIONS)))
532 return X86_PMC_IDX_FIXED_INSTRUCTIONS;
533 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_CPU_CYCLES)))
534 return X86_PMC_IDX_FIXED_CPU_CYCLES;
535 if (unlikely(event == pmc_ops->event_map(PERF_COUNT_BUS_CYCLES)))
536 return X86_PMC_IDX_FIXED_BUS_CYCLES;
542 * Find a PMC slot for the freshly enabled / scheduled in counter:
544 static int pmc_generic_enable(struct perf_counter *counter)
546 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
547 struct hw_perf_counter *hwc = &counter->hw;
550 idx = fixed_mode_idx(counter, hwc);
553 * Try to get the fixed counter, if that is already taken
554 * then try to get a generic counter:
556 if (test_and_set_bit(idx, cpuc->used))
559 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
561 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
562 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
565 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
569 /* Try to get the previous generic counter again */
570 if (test_and_set_bit(idx, cpuc->used)) {
572 idx = find_first_zero_bit(cpuc->used, nr_counters_generic);
573 if (idx == nr_counters_generic)
576 set_bit(idx, cpuc->used);
579 hwc->config_base = pmc_ops->eventsel;
580 hwc->counter_base = pmc_ops->perfctr;
583 perf_counters_lapic_init(hwc->nmi);
585 __pmc_generic_disable(counter, hwc, idx);
587 cpuc->counters[idx] = counter;
589 * Make it visible before enabling the hw:
593 __hw_perf_counter_set_period(counter, hwc, idx);
594 __pmc_generic_enable(counter, hwc, idx);
599 void perf_counter_print_debug(void)
601 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
602 struct cpu_hw_counters *cpuc;
605 if (!nr_counters_generic)
610 cpu = smp_processor_id();
611 cpuc = &per_cpu(cpu_hw_counters, cpu);
613 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
614 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
615 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
616 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
617 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
620 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
621 pr_info("CPU#%d: status: %016llx\n", cpu, status);
622 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
623 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
625 pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used);
627 for (idx = 0; idx < nr_counters_generic; idx++) {
628 rdmsrl(pmc_ops->eventsel + idx, pmc_ctrl);
629 rdmsrl(pmc_ops->perfctr + idx, pmc_count);
631 prev_left = per_cpu(prev_left[idx], cpu);
633 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
635 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
636 cpu, idx, pmc_count);
637 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
638 cpu, idx, prev_left);
640 for (idx = 0; idx < nr_counters_fixed; idx++) {
641 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
643 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
644 cpu, idx, pmc_count);
649 static void pmc_generic_disable(struct perf_counter *counter)
651 struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters);
652 struct hw_perf_counter *hwc = &counter->hw;
653 unsigned int idx = hwc->idx;
655 __pmc_generic_disable(counter, hwc, idx);
657 clear_bit(idx, cpuc->used);
658 cpuc->counters[idx] = NULL;
660 * Make sure the cleared pointer becomes visible before we
661 * (potentially) free the counter:
666 * Drain the remaining delta count out of a counter
667 * that we are disabling:
669 x86_perf_counter_update(counter, hwc, idx);
672 static void perf_store_irq_data(struct perf_counter *counter, u64 data)
674 struct perf_data *irqdata = counter->irqdata;
676 if (irqdata->len > PERF_DATA_BUFLEN - sizeof(u64)) {
679 u64 *p = (u64 *) &irqdata->data[irqdata->len];
682 irqdata->len += sizeof(u64);
687 * Save and restart an expired counter. Called by NMI contexts,
688 * so it has to be careful about preempting normal counter ops:
690 static void perf_save_and_restart(struct perf_counter *counter)
692 struct hw_perf_counter *hwc = &counter->hw;
695 x86_perf_counter_update(counter, hwc, idx);
696 __hw_perf_counter_set_period(counter, hwc, idx);
698 if (counter->state == PERF_COUNTER_STATE_ACTIVE)
699 __pmc_generic_enable(counter, hwc, idx);
703 perf_handle_group(struct perf_counter *sibling, u64 *status, u64 *overflown)
705 struct perf_counter *counter, *group_leader = sibling->group_leader;
708 * Store sibling timestamps (if any):
710 list_for_each_entry(counter, &group_leader->sibling_list, list_entry) {
712 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
713 perf_store_irq_data(sibling, counter->hw_event.type);
714 perf_store_irq_data(sibling, atomic64_read(&counter->count));
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 __smp_perf_counter_interrupt(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 = hw_perf_save_disable();
736 status = hw_perf_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);
751 perf_save_and_restart(counter);
753 switch (counter->hw_event.record_type) {
754 case PERF_RECORD_SIMPLE:
756 case PERF_RECORD_IRQ:
757 perf_store_irq_data(counter, instruction_pointer(regs));
759 case PERF_RECORD_GROUP:
760 perf_handle_group(counter, &status, &ack);
764 * From NMI context we cannot call into the scheduler to
765 * do a task wakeup - but we mark these generic as
766 * wakeup_pending and initate a wakeup callback:
769 counter->wakeup_pending = 1;
770 set_tsk_thread_flag(current, TIF_PERF_COUNTERS);
772 wake_up(&counter->waitq);
776 hw_perf_ack_status(ack);
779 * Repeat if there is more work to be done:
781 status = hw_perf_get_status(cpuc->throttle_ctrl);
786 * Restore - do not reenable when global enable is off or throttled:
788 if (++cpuc->interrupts < PERFMON_MAX_INTERRUPTS)
789 hw_perf_restore(cpuc->throttle_ctrl);
794 void perf_counter_unthrottle(void)
796 struct cpu_hw_counters *cpuc;
798 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
801 if (unlikely(!perf_counters_initialized))
804 cpuc = &__get_cpu_var(cpu_hw_counters);
805 if (cpuc->interrupts >= PERFMON_MAX_INTERRUPTS) {
806 if (printk_ratelimit())
807 printk(KERN_WARNING "PERFMON: max interrupts exceeded!\n");
808 hw_perf_restore(cpuc->throttle_ctrl);
810 cpuc->interrupts = 0;
813 void smp_perf_counter_interrupt(struct pt_regs *regs)
816 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
818 __smp_perf_counter_interrupt(regs, 0);
823 * This handler is triggered by NMI contexts:
825 void perf_counter_notify(struct pt_regs *regs)
827 struct cpu_hw_counters *cpuc;
831 local_irq_save(flags);
832 cpu = smp_processor_id();
833 cpuc = &per_cpu(cpu_hw_counters, cpu);
835 for_each_bit(bit, cpuc->used, X86_PMC_IDX_MAX) {
836 struct perf_counter *counter = cpuc->counters[bit];
841 if (counter->wakeup_pending) {
842 counter->wakeup_pending = 0;
843 wake_up(&counter->waitq);
847 local_irq_restore(flags);
850 void perf_counters_lapic_init(int nmi)
854 if (!perf_counters_initialized)
857 * Enable the performance counter vector in the APIC LVT:
859 apic_val = apic_read(APIC_LVTERR);
861 apic_write(APIC_LVTERR, apic_val | APIC_LVT_MASKED);
863 apic_write(APIC_LVTPC, APIC_DM_NMI);
865 apic_write(APIC_LVTPC, LOCAL_PERF_VECTOR);
866 apic_write(APIC_LVTERR, apic_val);
870 perf_counter_nmi_handler(struct notifier_block *self,
871 unsigned long cmd, void *__args)
873 struct die_args *args = __args;
874 struct pt_regs *regs;
888 apic_write(APIC_LVTPC, APIC_DM_NMI);
889 ret = __smp_perf_counter_interrupt(regs, 1);
891 return ret ? NOTIFY_STOP : NOTIFY_OK;
894 static __read_mostly struct notifier_block perf_counter_nmi_notifier = {
895 .notifier_call = perf_counter_nmi_handler,
900 static struct pmc_x86_ops pmc_intel_ops = {
901 .save_disable_all = pmc_intel_save_disable_all,
902 .restore_all = pmc_intel_restore_all,
903 .get_status = pmc_intel_get_status,
904 .ack_status = pmc_intel_ack_status,
905 .enable = pmc_intel_enable,
906 .disable = pmc_intel_disable,
907 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
908 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
909 .event_map = pmc_intel_event_map,
910 .raw_event = pmc_intel_raw_event,
911 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
914 static struct pmc_x86_ops pmc_amd_ops = {
915 .save_disable_all = pmc_amd_save_disable_all,
916 .restore_all = pmc_amd_restore_all,
917 .get_status = pmc_amd_get_status,
918 .ack_status = pmc_amd_ack_status,
919 .enable = pmc_amd_enable,
920 .disable = pmc_amd_disable,
921 .eventsel = MSR_K7_EVNTSEL0,
922 .perfctr = MSR_K7_PERFCTR0,
923 .event_map = pmc_amd_event_map,
924 .raw_event = pmc_amd_raw_event,
925 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
928 static struct pmc_x86_ops *pmc_intel_init(void)
930 union cpuid10_eax eax;
933 union cpuid10_edx edx;
936 * Check whether the Architectural PerfMon supports
937 * Branch Misses Retired Event or not.
939 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
940 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
943 pr_info("Intel Performance Monitoring support detected.\n");
944 pr_info("... version: %d\n", eax.split.version_id);
945 pr_info("... bit width: %d\n", eax.split.bit_width);
946 pr_info("... mask length: %d\n", eax.split.mask_length);
948 nr_counters_generic = eax.split.num_counters;
949 nr_counters_fixed = edx.split.num_counters_fixed;
950 counter_value_mask = (1ULL << eax.split.bit_width) - 1;
952 return &pmc_intel_ops;
955 static struct pmc_x86_ops *pmc_amd_init(void)
960 nr_counters_generic = 4;
961 nr_counters_fixed = 0;
962 counter_value_mask = ~0ULL;
964 rdmsrl(MSR_K7_PERFCTR0, old);
965 wrmsrl(MSR_K7_PERFCTR0, counter_value_mask);
967 * read the truncated mask
969 rdmsrl(MSR_K7_PERFCTR0, counter_value_mask);
970 wrmsrl(MSR_K7_PERFCTR0, old);
972 bits = 32 + fls(counter_value_mask >> 32);
974 bits = fls((u32)counter_value_mask);
975 counter_value_bits = bits;
977 pr_info("AMD Performance Monitoring support detected.\n");
982 void __init init_hw_perf_counters(void)
984 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON))
987 switch (boot_cpu_data.x86_vendor) {
988 case X86_VENDOR_INTEL:
989 pmc_ops = pmc_intel_init();
992 pmc_ops = pmc_amd_init();
998 pr_info("... num counters: %d\n", nr_counters_generic);
999 if (nr_counters_generic > X86_PMC_MAX_GENERIC) {
1000 nr_counters_generic = X86_PMC_MAX_GENERIC;
1001 WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!",
1002 nr_counters_generic, X86_PMC_MAX_GENERIC);
1004 perf_counter_mask = (1 << nr_counters_generic) - 1;
1005 perf_max_counters = nr_counters_generic;
1007 pr_info("... value mask: %016Lx\n", counter_value_mask);
1009 if (nr_counters_fixed > X86_PMC_MAX_FIXED) {
1010 nr_counters_fixed = X86_PMC_MAX_FIXED;
1011 WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!",
1012 nr_counters_fixed, X86_PMC_MAX_FIXED);
1014 pr_info("... fixed counters: %d\n", nr_counters_fixed);
1016 perf_counter_mask |= ((1LL << nr_counters_fixed)-1) << X86_PMC_IDX_FIXED;
1018 pr_info("... counter mask: %016Lx\n", perf_counter_mask);
1019 perf_counters_initialized = true;
1021 perf_counters_lapic_init(0);
1022 register_die_notifier(&perf_counter_nmi_notifier);
1025 static void pmc_generic_read(struct perf_counter *counter)
1027 x86_perf_counter_update(counter, &counter->hw, counter->hw.idx);
1030 static const struct hw_perf_counter_ops x86_perf_counter_ops = {
1031 .enable = pmc_generic_enable,
1032 .disable = pmc_generic_disable,
1033 .read = pmc_generic_read,
1036 const struct hw_perf_counter_ops *
1037 hw_perf_counter_init(struct perf_counter *counter)
1041 err = __hw_perf_counter_init(counter);
1045 return &x86_perf_counter_ops;