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 perf_counter_output(counter
, nmi
, regs
);
806 hw_perf_ack_status(ack
);
809 * Repeat if there is more work to be done:
811 status
= hw_perf_get_status(cpuc
->throttle_ctrl
);
816 * Restore - do not reenable when global enable is off or throttled:
818 if (++cpuc
->interrupts
< PERFMON_MAX_INTERRUPTS
)
819 hw_perf_restore(cpuc
->throttle_ctrl
);
824 void perf_counter_unthrottle(void)
826 struct cpu_hw_counters
*cpuc
;
828 if (!cpu_has(&boot_cpu_data
, X86_FEATURE_ARCH_PERFMON
))
831 if (unlikely(!perf_counters_initialized
))
834 cpuc
= &__get_cpu_var(cpu_hw_counters
);
835 if (cpuc
->interrupts
>= PERFMON_MAX_INTERRUPTS
) {
836 if (printk_ratelimit())
837 printk(KERN_WARNING
"PERFMON: max interrupts exceeded!\n");
838 hw_perf_restore(cpuc
->throttle_ctrl
);
840 cpuc
->interrupts
= 0;
843 void smp_perf_counter_interrupt(struct pt_regs
*regs
)
846 apic_write(APIC_LVTPC
, LOCAL_PERF_VECTOR
);
848 __smp_perf_counter_interrupt(regs
, 0);
852 void perf_counters_lapic_init(int nmi
)
856 if (!perf_counters_initialized
)
859 * Enable the performance counter vector in the APIC LVT:
861 apic_val
= apic_read(APIC_LVTERR
);
863 apic_write(APIC_LVTERR
, apic_val
| APIC_LVT_MASKED
);
865 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
867 apic_write(APIC_LVTPC
, LOCAL_PERF_VECTOR
);
868 apic_write(APIC_LVTERR
, apic_val
);
872 perf_counter_nmi_handler(struct notifier_block
*self
,
873 unsigned long cmd
, void *__args
)
875 struct die_args
*args
= __args
;
876 struct pt_regs
*regs
;
890 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
891 ret
= __smp_perf_counter_interrupt(regs
, 1);
893 return ret
? NOTIFY_STOP
: NOTIFY_OK
;
896 static __read_mostly
struct notifier_block perf_counter_nmi_notifier
= {
897 .notifier_call
= perf_counter_nmi_handler
,
902 static struct pmc_x86_ops pmc_intel_ops
= {
903 .save_disable_all
= pmc_intel_save_disable_all
,
904 .restore_all
= pmc_intel_restore_all
,
905 .get_status
= pmc_intel_get_status
,
906 .ack_status
= pmc_intel_ack_status
,
907 .enable
= pmc_intel_enable
,
908 .disable
= pmc_intel_disable
,
909 .eventsel
= MSR_ARCH_PERFMON_EVENTSEL0
,
910 .perfctr
= MSR_ARCH_PERFMON_PERFCTR0
,
911 .event_map
= pmc_intel_event_map
,
912 .raw_event
= pmc_intel_raw_event
,
913 .max_events
= ARRAY_SIZE(intel_perfmon_event_map
),
916 static struct pmc_x86_ops pmc_amd_ops
= {
917 .save_disable_all
= pmc_amd_save_disable_all
,
918 .restore_all
= pmc_amd_restore_all
,
919 .get_status
= pmc_amd_get_status
,
920 .ack_status
= pmc_amd_ack_status
,
921 .enable
= pmc_amd_enable
,
922 .disable
= pmc_amd_disable
,
923 .eventsel
= MSR_K7_EVNTSEL0
,
924 .perfctr
= MSR_K7_PERFCTR0
,
925 .event_map
= pmc_amd_event_map
,
926 .raw_event
= pmc_amd_raw_event
,
927 .max_events
= ARRAY_SIZE(amd_perfmon_event_map
),
930 static struct pmc_x86_ops
*pmc_intel_init(void)
932 union cpuid10_edx edx
;
933 union cpuid10_eax eax
;
938 * Check whether the Architectural PerfMon supports
939 * Branch Misses Retired Event or not.
941 cpuid(10, &eax
.full
, &ebx
, &unused
, &edx
.full
);
942 if (eax
.split
.mask_length
<= ARCH_PERFMON_BRANCH_MISSES_RETIRED
)
945 intel_perfmon_version
= eax
.split
.version_id
;
946 if (intel_perfmon_version
< 2)
949 pr_info("Intel Performance Monitoring support detected.\n");
950 pr_info("... version: %d\n", intel_perfmon_version
);
951 pr_info("... bit width: %d\n", eax
.split
.bit_width
);
952 pr_info("... mask length: %d\n", eax
.split
.mask_length
);
954 nr_counters_generic
= eax
.split
.num_counters
;
955 nr_counters_fixed
= edx
.split
.num_counters_fixed
;
956 counter_value_mask
= (1ULL << eax
.split
.bit_width
) - 1;
958 return &pmc_intel_ops
;
961 static struct pmc_x86_ops
*pmc_amd_init(void)
963 nr_counters_generic
= 4;
964 nr_counters_fixed
= 0;
965 counter_value_mask
= 0x0000FFFFFFFFFFFFULL
;
966 counter_value_bits
= 48;
968 pr_info("AMD Performance Monitoring support detected.\n");
973 void __init
init_hw_perf_counters(void)
975 if (!cpu_has(&boot_cpu_data
, X86_FEATURE_ARCH_PERFMON
))
978 switch (boot_cpu_data
.x86_vendor
) {
979 case X86_VENDOR_INTEL
:
980 pmc_ops
= pmc_intel_init();
983 pmc_ops
= pmc_amd_init();
989 pr_info("... num counters: %d\n", nr_counters_generic
);
990 if (nr_counters_generic
> X86_PMC_MAX_GENERIC
) {
991 nr_counters_generic
= X86_PMC_MAX_GENERIC
;
992 WARN(1, KERN_ERR
"hw perf counters %d > max(%d), clipping!",
993 nr_counters_generic
, X86_PMC_MAX_GENERIC
);
995 perf_counter_mask
= (1 << nr_counters_generic
) - 1;
996 perf_max_counters
= nr_counters_generic
;
998 pr_info("... value mask: %016Lx\n", counter_value_mask
);
1000 if (nr_counters_fixed
> X86_PMC_MAX_FIXED
) {
1001 nr_counters_fixed
= X86_PMC_MAX_FIXED
;
1002 WARN(1, KERN_ERR
"hw perf counters fixed %d > max(%d), clipping!",
1003 nr_counters_fixed
, X86_PMC_MAX_FIXED
);
1005 pr_info("... fixed counters: %d\n", nr_counters_fixed
);
1007 perf_counter_mask
|= ((1LL << nr_counters_fixed
)-1) << X86_PMC_IDX_FIXED
;
1009 pr_info("... counter mask: %016Lx\n", perf_counter_mask
);
1010 perf_counters_initialized
= true;
1012 perf_counters_lapic_init(0);
1013 register_die_notifier(&perf_counter_nmi_notifier
);
1016 static void pmc_generic_read(struct perf_counter
*counter
)
1018 x86_perf_counter_update(counter
, &counter
->hw
, counter
->hw
.idx
);
1021 static const struct hw_perf_counter_ops x86_perf_counter_ops
= {
1022 .enable
= pmc_generic_enable
,
1023 .disable
= pmc_generic_disable
,
1024 .read
= pmc_generic_read
,
1027 const struct hw_perf_counter_ops
*
1028 hw_perf_counter_init(struct perf_counter
*counter
)
1032 err
= __hw_perf_counter_init(counter
);
1034 return ERR_PTR(err
);
1036 return &x86_perf_counter_ops
;
1044 void callchain_store(struct perf_callchain_entry
*entry
, unsigned long ip
)
1046 if (entry
->nr
< MAX_STACK_DEPTH
)
1047 entry
->ip
[entry
->nr
++] = ip
;
1050 static DEFINE_PER_CPU(struct perf_callchain_entry
, irq_entry
);
1051 static DEFINE_PER_CPU(struct perf_callchain_entry
, nmi_entry
);
1055 backtrace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
1057 /* Ignore warnings */
1060 static void backtrace_warning(void *data
, char *msg
)
1062 /* Ignore warnings */
1065 static int backtrace_stack(void *data
, char *name
)
1067 /* Don't bother with IRQ stacks for now */
1071 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
1073 struct perf_callchain_entry
*entry
= data
;
1076 callchain_store(entry
, addr
);
1079 static const struct stacktrace_ops backtrace_ops
= {
1080 .warning
= backtrace_warning
,
1081 .warning_symbol
= backtrace_warning_symbol
,
1082 .stack
= backtrace_stack
,
1083 .address
= backtrace_address
,
1087 perf_callchain_kernel(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1092 callchain_store(entry
, instruction_pointer(regs
));
1094 stack
= ((char *)regs
+ sizeof(struct pt_regs
));
1095 #ifdef CONFIG_FRAME_POINTER
1096 bp
= frame_pointer(regs
);
1101 dump_trace(NULL
, regs
, (void *)stack
, bp
, &backtrace_ops
, entry
);
1105 struct stack_frame
{
1106 const void __user
*next_fp
;
1107 unsigned long return_address
;
1110 static int copy_stack_frame(const void __user
*fp
, struct stack_frame
*frame
)
1114 if (!access_ok(VERIFY_READ
, fp
, sizeof(*frame
)))
1118 pagefault_disable();
1119 if (__copy_from_user_inatomic(frame
, fp
, sizeof(*frame
)))
1127 perf_callchain_user(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1129 struct stack_frame frame
;
1130 const void __user
*fp
;
1132 regs
= (struct pt_regs
*)current
->thread
.sp0
- 1;
1133 fp
= (void __user
*)regs
->bp
;
1135 callchain_store(entry
, regs
->ip
);
1137 while (entry
->nr
< MAX_STACK_DEPTH
) {
1138 frame
.next_fp
= NULL
;
1139 frame
.return_address
= 0;
1141 if (!copy_stack_frame(fp
, &frame
))
1144 if ((unsigned long)fp
< user_stack_pointer(regs
))
1147 callchain_store(entry
, frame
.return_address
);
1153 perf_do_callchain(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
1160 is_user
= user_mode(regs
);
1162 if (!current
|| current
->pid
== 0)
1165 if (is_user
&& current
->state
!= TASK_RUNNING
)
1169 perf_callchain_kernel(regs
, entry
);
1172 perf_callchain_user(regs
, entry
);
1175 struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
1177 struct perf_callchain_entry
*entry
;
1180 entry
= &__get_cpu_var(nmi_entry
);
1182 entry
= &__get_cpu_var(irq_entry
);
1186 perf_do_callchain(regs
, entry
);