2 * Performance counter x86 architecture code
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2009 Jaswinder Singh Rajput
7 * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter
8 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
9 * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com>
11 * For licencing details see kernel-base/COPYING
14 #include <linux/perf_counter.h>
15 #include <linux/capability.h>
16 #include <linux/notifier.h>
17 #include <linux/hardirq.h>
18 #include <linux/kprobes.h>
19 #include <linux/module.h>
20 #include <linux/kdebug.h>
21 #include <linux/sched.h>
22 #include <linux/uaccess.h>
23 #include <linux/highmem.h>
24 #include <linux/cpu.h>
27 #include <asm/stacktrace.h>
30 static u64 perf_counter_mask __read_mostly
;
32 /* The maximal number of PEBS counters: */
33 #define MAX_PEBS_COUNTERS 4
35 /* The size of a BTS record in bytes: */
36 #define BTS_RECORD_SIZE 24
38 /* The size of a per-cpu BTS buffer in bytes: */
39 #define BTS_BUFFER_SIZE (BTS_RECORD_SIZE * 1024)
41 /* The BTS overflow threshold in bytes from the end of the buffer: */
42 #define BTS_OVFL_TH (BTS_RECORD_SIZE * 64)
46 * Bits in the debugctlmsr controlling branch tracing.
48 #define X86_DEBUGCTL_TR (1 << 6)
49 #define X86_DEBUGCTL_BTS (1 << 7)
50 #define X86_DEBUGCTL_BTINT (1 << 8)
51 #define X86_DEBUGCTL_BTS_OFF_OS (1 << 9)
52 #define X86_DEBUGCTL_BTS_OFF_USR (1 << 10)
55 * A debug store configuration.
57 * We only support architectures that use 64bit fields.
62 u64 bts_absolute_maximum
;
63 u64 bts_interrupt_threshold
;
66 u64 pebs_absolute_maximum
;
67 u64 pebs_interrupt_threshold
;
68 u64 pebs_counter_reset
[MAX_PEBS_COUNTERS
];
71 struct cpu_hw_counters
{
72 struct perf_counter
*counters
[X86_PMC_IDX_MAX
];
73 unsigned long used_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
74 unsigned long active_mask
[BITS_TO_LONGS(X86_PMC_IDX_MAX
)];
75 unsigned long interrupts
;
77 struct debug_store
*ds
;
81 * struct x86_pmu - generic x86 pmu
86 int (*handle_irq
)(struct pt_regs
*);
87 void (*disable_all
)(void);
88 void (*enable_all
)(void);
89 void (*enable
)(struct hw_perf_counter
*, int);
90 void (*disable
)(struct hw_perf_counter
*, int);
93 u64 (*event_map
)(int);
94 u64 (*raw_event
)(u64
);
97 int num_counters_fixed
;
103 void (*enable_bts
)(u64 config
);
104 void (*disable_bts
)(void);
107 static struct x86_pmu x86_pmu __read_mostly
;
109 static DEFINE_PER_CPU(struct cpu_hw_counters
, cpu_hw_counters
) = {
114 * Not sure about some of these
116 static const u64 p6_perfmon_event_map
[] =
118 [PERF_COUNT_HW_CPU_CYCLES
] = 0x0079,
119 [PERF_COUNT_HW_INSTRUCTIONS
] = 0x00c0,
120 [PERF_COUNT_HW_CACHE_REFERENCES
] = 0x0f2e,
121 [PERF_COUNT_HW_CACHE_MISSES
] = 0x012e,
122 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = 0x00c4,
123 [PERF_COUNT_HW_BRANCH_MISSES
] = 0x00c5,
124 [PERF_COUNT_HW_BUS_CYCLES
] = 0x0062,
127 static u64
p6_pmu_event_map(int event
)
129 return p6_perfmon_event_map
[event
];
133 * Counter setting that is specified not to count anything.
134 * We use this to effectively disable a counter.
136 * L2_RQSTS with 0 MESI unit mask.
138 #define P6_NOP_COUNTER 0x0000002EULL
140 static u64
p6_pmu_raw_event(u64 event
)
142 #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL
143 #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL
144 #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL
145 #define P6_EVNTSEL_INV_MASK 0x00800000ULL
146 #define P6_EVNTSEL_COUNTER_MASK 0xFF000000ULL
148 #define P6_EVNTSEL_MASK \
149 (P6_EVNTSEL_EVENT_MASK | \
150 P6_EVNTSEL_UNIT_MASK | \
151 P6_EVNTSEL_EDGE_MASK | \
152 P6_EVNTSEL_INV_MASK | \
153 P6_EVNTSEL_COUNTER_MASK)
155 return event
& P6_EVNTSEL_MASK
;
160 * Intel PerfMon v3. Used on Core2 and later.
162 static const u64 intel_perfmon_event_map
[] =
164 [PERF_COUNT_HW_CPU_CYCLES
] = 0x003c,
165 [PERF_COUNT_HW_INSTRUCTIONS
] = 0x00c0,
166 [PERF_COUNT_HW_CACHE_REFERENCES
] = 0x4f2e,
167 [PERF_COUNT_HW_CACHE_MISSES
] = 0x412e,
168 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = 0x00c4,
169 [PERF_COUNT_HW_BRANCH_MISSES
] = 0x00c5,
170 [PERF_COUNT_HW_BUS_CYCLES
] = 0x013c,
173 static u64
intel_pmu_event_map(int event
)
175 return intel_perfmon_event_map
[event
];
179 * Generalized hw caching related event table, filled
180 * in on a per model basis. A value of 0 means
181 * 'not supported', -1 means 'event makes no sense on
182 * this CPU', any other value means the raw event
186 #define C(x) PERF_COUNT_HW_CACHE_##x
188 static u64 __read_mostly hw_cache_event_ids
189 [PERF_COUNT_HW_CACHE_MAX
]
190 [PERF_COUNT_HW_CACHE_OP_MAX
]
191 [PERF_COUNT_HW_CACHE_RESULT_MAX
];
193 static const u64 nehalem_hw_cache_event_ids
194 [PERF_COUNT_HW_CACHE_MAX
]
195 [PERF_COUNT_HW_CACHE_OP_MAX
]
196 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
200 [ C(RESULT_ACCESS
) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
201 [ C(RESULT_MISS
) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
204 [ C(RESULT_ACCESS
) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
205 [ C(RESULT_MISS
) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
207 [ C(OP_PREFETCH
) ] = {
208 [ C(RESULT_ACCESS
) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
209 [ C(RESULT_MISS
) ] = 0x024e, /* L1D_PREFETCH.MISS */
214 [ C(RESULT_ACCESS
) ] = 0x0380, /* L1I.READS */
215 [ C(RESULT_MISS
) ] = 0x0280, /* L1I.MISSES */
218 [ C(RESULT_ACCESS
) ] = -1,
219 [ C(RESULT_MISS
) ] = -1,
221 [ C(OP_PREFETCH
) ] = {
222 [ C(RESULT_ACCESS
) ] = 0x0,
223 [ C(RESULT_MISS
) ] = 0x0,
228 [ C(RESULT_ACCESS
) ] = 0x0324, /* L2_RQSTS.LOADS */
229 [ C(RESULT_MISS
) ] = 0x0224, /* L2_RQSTS.LD_MISS */
232 [ C(RESULT_ACCESS
) ] = 0x0c24, /* L2_RQSTS.RFOS */
233 [ C(RESULT_MISS
) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
235 [ C(OP_PREFETCH
) ] = {
236 [ C(RESULT_ACCESS
) ] = 0x4f2e, /* LLC Reference */
237 [ C(RESULT_MISS
) ] = 0x412e, /* LLC Misses */
242 [ C(RESULT_ACCESS
) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
243 [ C(RESULT_MISS
) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
246 [ C(RESULT_ACCESS
) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
247 [ C(RESULT_MISS
) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
249 [ C(OP_PREFETCH
) ] = {
250 [ C(RESULT_ACCESS
) ] = 0x0,
251 [ C(RESULT_MISS
) ] = 0x0,
256 [ C(RESULT_ACCESS
) ] = 0x01c0, /* INST_RETIRED.ANY_P */
257 [ C(RESULT_MISS
) ] = 0x20c8, /* ITLB_MISS_RETIRED */
260 [ C(RESULT_ACCESS
) ] = -1,
261 [ C(RESULT_MISS
) ] = -1,
263 [ C(OP_PREFETCH
) ] = {
264 [ C(RESULT_ACCESS
) ] = -1,
265 [ C(RESULT_MISS
) ] = -1,
270 [ C(RESULT_ACCESS
) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
271 [ C(RESULT_MISS
) ] = 0x03e8, /* BPU_CLEARS.ANY */
274 [ C(RESULT_ACCESS
) ] = -1,
275 [ C(RESULT_MISS
) ] = -1,
277 [ C(OP_PREFETCH
) ] = {
278 [ C(RESULT_ACCESS
) ] = -1,
279 [ C(RESULT_MISS
) ] = -1,
284 static const u64 core2_hw_cache_event_ids
285 [PERF_COUNT_HW_CACHE_MAX
]
286 [PERF_COUNT_HW_CACHE_OP_MAX
]
287 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
291 [ C(RESULT_ACCESS
) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
292 [ C(RESULT_MISS
) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
295 [ C(RESULT_ACCESS
) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
296 [ C(RESULT_MISS
) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
298 [ C(OP_PREFETCH
) ] = {
299 [ C(RESULT_ACCESS
) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
300 [ C(RESULT_MISS
) ] = 0,
305 [ C(RESULT_ACCESS
) ] = 0x0080, /* L1I.READS */
306 [ C(RESULT_MISS
) ] = 0x0081, /* L1I.MISSES */
309 [ C(RESULT_ACCESS
) ] = -1,
310 [ C(RESULT_MISS
) ] = -1,
312 [ C(OP_PREFETCH
) ] = {
313 [ C(RESULT_ACCESS
) ] = 0,
314 [ C(RESULT_MISS
) ] = 0,
319 [ C(RESULT_ACCESS
) ] = 0x4f29, /* L2_LD.MESI */
320 [ C(RESULT_MISS
) ] = 0x4129, /* L2_LD.ISTATE */
323 [ C(RESULT_ACCESS
) ] = 0x4f2A, /* L2_ST.MESI */
324 [ C(RESULT_MISS
) ] = 0x412A, /* L2_ST.ISTATE */
326 [ C(OP_PREFETCH
) ] = {
327 [ C(RESULT_ACCESS
) ] = 0,
328 [ C(RESULT_MISS
) ] = 0,
333 [ C(RESULT_ACCESS
) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
334 [ C(RESULT_MISS
) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
337 [ C(RESULT_ACCESS
) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
338 [ C(RESULT_MISS
) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
340 [ C(OP_PREFETCH
) ] = {
341 [ C(RESULT_ACCESS
) ] = 0,
342 [ C(RESULT_MISS
) ] = 0,
347 [ C(RESULT_ACCESS
) ] = 0x00c0, /* INST_RETIRED.ANY_P */
348 [ C(RESULT_MISS
) ] = 0x1282, /* ITLBMISSES */
351 [ C(RESULT_ACCESS
) ] = -1,
352 [ C(RESULT_MISS
) ] = -1,
354 [ C(OP_PREFETCH
) ] = {
355 [ C(RESULT_ACCESS
) ] = -1,
356 [ C(RESULT_MISS
) ] = -1,
361 [ C(RESULT_ACCESS
) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
362 [ C(RESULT_MISS
) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
365 [ C(RESULT_ACCESS
) ] = -1,
366 [ C(RESULT_MISS
) ] = -1,
368 [ C(OP_PREFETCH
) ] = {
369 [ C(RESULT_ACCESS
) ] = -1,
370 [ C(RESULT_MISS
) ] = -1,
375 static const u64 atom_hw_cache_event_ids
376 [PERF_COUNT_HW_CACHE_MAX
]
377 [PERF_COUNT_HW_CACHE_OP_MAX
]
378 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
382 [ C(RESULT_ACCESS
) ] = 0x2140, /* L1D_CACHE.LD */
383 [ C(RESULT_MISS
) ] = 0,
386 [ C(RESULT_ACCESS
) ] = 0x2240, /* L1D_CACHE.ST */
387 [ C(RESULT_MISS
) ] = 0,
389 [ C(OP_PREFETCH
) ] = {
390 [ C(RESULT_ACCESS
) ] = 0x0,
391 [ C(RESULT_MISS
) ] = 0,
396 [ C(RESULT_ACCESS
) ] = 0x0380, /* L1I.READS */
397 [ C(RESULT_MISS
) ] = 0x0280, /* L1I.MISSES */
400 [ C(RESULT_ACCESS
) ] = -1,
401 [ C(RESULT_MISS
) ] = -1,
403 [ C(OP_PREFETCH
) ] = {
404 [ C(RESULT_ACCESS
) ] = 0,
405 [ C(RESULT_MISS
) ] = 0,
410 [ C(RESULT_ACCESS
) ] = 0x4f29, /* L2_LD.MESI */
411 [ C(RESULT_MISS
) ] = 0x4129, /* L2_LD.ISTATE */
414 [ C(RESULT_ACCESS
) ] = 0x4f2A, /* L2_ST.MESI */
415 [ C(RESULT_MISS
) ] = 0x412A, /* L2_ST.ISTATE */
417 [ C(OP_PREFETCH
) ] = {
418 [ C(RESULT_ACCESS
) ] = 0,
419 [ C(RESULT_MISS
) ] = 0,
424 [ C(RESULT_ACCESS
) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
425 [ C(RESULT_MISS
) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
428 [ C(RESULT_ACCESS
) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
429 [ C(RESULT_MISS
) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
431 [ C(OP_PREFETCH
) ] = {
432 [ C(RESULT_ACCESS
) ] = 0,
433 [ C(RESULT_MISS
) ] = 0,
438 [ C(RESULT_ACCESS
) ] = 0x00c0, /* INST_RETIRED.ANY_P */
439 [ C(RESULT_MISS
) ] = 0x0282, /* ITLB.MISSES */
442 [ C(RESULT_ACCESS
) ] = -1,
443 [ C(RESULT_MISS
) ] = -1,
445 [ C(OP_PREFETCH
) ] = {
446 [ C(RESULT_ACCESS
) ] = -1,
447 [ C(RESULT_MISS
) ] = -1,
452 [ C(RESULT_ACCESS
) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
453 [ C(RESULT_MISS
) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
456 [ C(RESULT_ACCESS
) ] = -1,
457 [ C(RESULT_MISS
) ] = -1,
459 [ C(OP_PREFETCH
) ] = {
460 [ C(RESULT_ACCESS
) ] = -1,
461 [ C(RESULT_MISS
) ] = -1,
466 static u64
intel_pmu_raw_event(u64 event
)
468 #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
469 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
470 #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL
471 #define CORE_EVNTSEL_INV_MASK 0x00800000ULL
472 #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL
474 #define CORE_EVNTSEL_MASK \
475 (CORE_EVNTSEL_EVENT_MASK | \
476 CORE_EVNTSEL_UNIT_MASK | \
477 CORE_EVNTSEL_EDGE_MASK | \
478 CORE_EVNTSEL_INV_MASK | \
479 CORE_EVNTSEL_COUNTER_MASK)
481 return event
& CORE_EVNTSEL_MASK
;
484 static const u64 amd_hw_cache_event_ids
485 [PERF_COUNT_HW_CACHE_MAX
]
486 [PERF_COUNT_HW_CACHE_OP_MAX
]
487 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
491 [ C(RESULT_ACCESS
) ] = 0x0040, /* Data Cache Accesses */
492 [ C(RESULT_MISS
) ] = 0x0041, /* Data Cache Misses */
495 [ C(RESULT_ACCESS
) ] = 0x0142, /* Data Cache Refills :system */
496 [ C(RESULT_MISS
) ] = 0,
498 [ C(OP_PREFETCH
) ] = {
499 [ C(RESULT_ACCESS
) ] = 0x0267, /* Data Prefetcher :attempts */
500 [ C(RESULT_MISS
) ] = 0x0167, /* Data Prefetcher :cancelled */
505 [ C(RESULT_ACCESS
) ] = 0x0080, /* Instruction cache fetches */
506 [ C(RESULT_MISS
) ] = 0x0081, /* Instruction cache misses */
509 [ C(RESULT_ACCESS
) ] = -1,
510 [ C(RESULT_MISS
) ] = -1,
512 [ C(OP_PREFETCH
) ] = {
513 [ C(RESULT_ACCESS
) ] = 0x014B, /* Prefetch Instructions :Load */
514 [ C(RESULT_MISS
) ] = 0,
519 [ C(RESULT_ACCESS
) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
520 [ C(RESULT_MISS
) ] = 0x037E, /* L2 Cache Misses : IC+DC */
523 [ C(RESULT_ACCESS
) ] = 0x017F, /* L2 Fill/Writeback */
524 [ C(RESULT_MISS
) ] = 0,
526 [ C(OP_PREFETCH
) ] = {
527 [ C(RESULT_ACCESS
) ] = 0,
528 [ C(RESULT_MISS
) ] = 0,
533 [ C(RESULT_ACCESS
) ] = 0x0040, /* Data Cache Accesses */
534 [ C(RESULT_MISS
) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */
537 [ C(RESULT_ACCESS
) ] = 0,
538 [ C(RESULT_MISS
) ] = 0,
540 [ C(OP_PREFETCH
) ] = {
541 [ C(RESULT_ACCESS
) ] = 0,
542 [ C(RESULT_MISS
) ] = 0,
547 [ C(RESULT_ACCESS
) ] = 0x0080, /* Instruction fecthes */
548 [ C(RESULT_MISS
) ] = 0x0085, /* Instr. fetch ITLB misses */
551 [ C(RESULT_ACCESS
) ] = -1,
552 [ C(RESULT_MISS
) ] = -1,
554 [ C(OP_PREFETCH
) ] = {
555 [ C(RESULT_ACCESS
) ] = -1,
556 [ C(RESULT_MISS
) ] = -1,
561 [ C(RESULT_ACCESS
) ] = 0x00c2, /* Retired Branch Instr. */
562 [ C(RESULT_MISS
) ] = 0x00c3, /* Retired Mispredicted BI */
565 [ C(RESULT_ACCESS
) ] = -1,
566 [ C(RESULT_MISS
) ] = -1,
568 [ C(OP_PREFETCH
) ] = {
569 [ C(RESULT_ACCESS
) ] = -1,
570 [ C(RESULT_MISS
) ] = -1,
576 * AMD Performance Monitor K7 and later.
578 static const u64 amd_perfmon_event_map
[] =
580 [PERF_COUNT_HW_CPU_CYCLES
] = 0x0076,
581 [PERF_COUNT_HW_INSTRUCTIONS
] = 0x00c0,
582 [PERF_COUNT_HW_CACHE_REFERENCES
] = 0x0080,
583 [PERF_COUNT_HW_CACHE_MISSES
] = 0x0081,
584 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = 0x00c4,
585 [PERF_COUNT_HW_BRANCH_MISSES
] = 0x00c5,
588 static u64
amd_pmu_event_map(int event
)
590 return amd_perfmon_event_map
[event
];
593 static u64
amd_pmu_raw_event(u64 event
)
595 #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
596 #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
597 #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL
598 #define K7_EVNTSEL_INV_MASK 0x000800000ULL
599 #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL
601 #define K7_EVNTSEL_MASK \
602 (K7_EVNTSEL_EVENT_MASK | \
603 K7_EVNTSEL_UNIT_MASK | \
604 K7_EVNTSEL_EDGE_MASK | \
605 K7_EVNTSEL_INV_MASK | \
606 K7_EVNTSEL_COUNTER_MASK)
608 return event
& K7_EVNTSEL_MASK
;
612 * Propagate counter elapsed time into the generic counter.
613 * Can only be executed on the CPU where the counter is active.
614 * Returns the delta events processed.
617 x86_perf_counter_update(struct perf_counter
*counter
,
618 struct hw_perf_counter
*hwc
, int idx
)
620 int shift
= 64 - x86_pmu
.counter_bits
;
621 u64 prev_raw_count
, new_raw_count
;
624 if (idx
== X86_PMC_IDX_FIXED_BTS
)
628 * Careful: an NMI might modify the previous counter value.
630 * Our tactic to handle this is to first atomically read and
631 * exchange a new raw count - then add that new-prev delta
632 * count to the generic counter atomically:
635 prev_raw_count
= atomic64_read(&hwc
->prev_count
);
636 rdmsrl(hwc
->counter_base
+ idx
, new_raw_count
);
638 if (atomic64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
639 new_raw_count
) != prev_raw_count
)
643 * Now we have the new raw value and have updated the prev
644 * timestamp already. We can now calculate the elapsed delta
645 * (counter-)time and add that to the generic counter.
647 * Careful, not all hw sign-extends above the physical width
650 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
653 atomic64_add(delta
, &counter
->count
);
654 atomic64_sub(delta
, &hwc
->period_left
);
656 return new_raw_count
;
659 static atomic_t active_counters
;
660 static DEFINE_MUTEX(pmc_reserve_mutex
);
662 static bool reserve_pmc_hardware(void)
664 #ifdef CONFIG_X86_LOCAL_APIC
667 if (nmi_watchdog
== NMI_LOCAL_APIC
)
668 disable_lapic_nmi_watchdog();
670 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
671 if (!reserve_perfctr_nmi(x86_pmu
.perfctr
+ i
))
675 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
676 if (!reserve_evntsel_nmi(x86_pmu
.eventsel
+ i
))
683 #ifdef CONFIG_X86_LOCAL_APIC
685 for (i
--; i
>= 0; i
--)
686 release_evntsel_nmi(x86_pmu
.eventsel
+ i
);
688 i
= x86_pmu
.num_counters
;
691 for (i
--; i
>= 0; i
--)
692 release_perfctr_nmi(x86_pmu
.perfctr
+ i
);
694 if (nmi_watchdog
== NMI_LOCAL_APIC
)
695 enable_lapic_nmi_watchdog();
701 static void release_pmc_hardware(void)
703 #ifdef CONFIG_X86_LOCAL_APIC
706 for (i
= 0; i
< x86_pmu
.num_counters
; i
++) {
707 release_perfctr_nmi(x86_pmu
.perfctr
+ i
);
708 release_evntsel_nmi(x86_pmu
.eventsel
+ i
);
711 if (nmi_watchdog
== NMI_LOCAL_APIC
)
712 enable_lapic_nmi_watchdog();
716 static inline bool bts_available(void)
718 return x86_pmu
.enable_bts
!= NULL
;
721 static inline void init_debug_store_on_cpu(int cpu
)
723 struct debug_store
*ds
= per_cpu(cpu_hw_counters
, cpu
).ds
;
728 wrmsr_on_cpu(cpu
, MSR_IA32_DS_AREA
,
729 (u32
)((u64
)(long)ds
), (u32
)((u64
)(long)ds
>> 32));
732 static inline void fini_debug_store_on_cpu(int cpu
)
734 if (!per_cpu(cpu_hw_counters
, cpu
).ds
)
737 wrmsr_on_cpu(cpu
, MSR_IA32_DS_AREA
, 0, 0);
740 static void release_bts_hardware(void)
744 if (!bts_available())
749 for_each_online_cpu(cpu
)
750 fini_debug_store_on_cpu(cpu
);
752 for_each_possible_cpu(cpu
) {
753 struct debug_store
*ds
= per_cpu(cpu_hw_counters
, cpu
).ds
;
758 per_cpu(cpu_hw_counters
, cpu
).ds
= NULL
;
760 kfree((void *)(long)ds
->bts_buffer_base
);
767 static int reserve_bts_hardware(void)
771 if (!bts_available())
776 for_each_possible_cpu(cpu
) {
777 struct debug_store
*ds
;
781 buffer
= kzalloc(BTS_BUFFER_SIZE
, GFP_KERNEL
);
782 if (unlikely(!buffer
))
785 ds
= kzalloc(sizeof(*ds
), GFP_KERNEL
);
791 ds
->bts_buffer_base
= (u64
)(long)buffer
;
792 ds
->bts_index
= ds
->bts_buffer_base
;
793 ds
->bts_absolute_maximum
=
794 ds
->bts_buffer_base
+ BTS_BUFFER_SIZE
;
795 ds
->bts_interrupt_threshold
=
796 ds
->bts_absolute_maximum
- BTS_OVFL_TH
;
798 per_cpu(cpu_hw_counters
, cpu
).ds
= ds
;
803 release_bts_hardware();
805 for_each_online_cpu(cpu
)
806 init_debug_store_on_cpu(cpu
);
814 static void hw_perf_counter_destroy(struct perf_counter
*counter
)
816 if (atomic_dec_and_mutex_lock(&active_counters
, &pmc_reserve_mutex
)) {
817 release_pmc_hardware();
818 release_bts_hardware();
819 mutex_unlock(&pmc_reserve_mutex
);
823 static inline int x86_pmu_initialized(void)
825 return x86_pmu
.handle_irq
!= NULL
;
829 set_ext_hw_attr(struct hw_perf_counter
*hwc
, struct perf_counter_attr
*attr
)
831 unsigned int cache_type
, cache_op
, cache_result
;
834 config
= attr
->config
;
836 cache_type
= (config
>> 0) & 0xff;
837 if (cache_type
>= PERF_COUNT_HW_CACHE_MAX
)
840 cache_op
= (config
>> 8) & 0xff;
841 if (cache_op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
844 cache_result
= (config
>> 16) & 0xff;
845 if (cache_result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
848 val
= hw_cache_event_ids
[cache_type
][cache_op
][cache_result
];
861 static void intel_pmu_enable_bts(u64 config
)
863 unsigned long debugctlmsr
;
865 debugctlmsr
= get_debugctlmsr();
867 debugctlmsr
|= X86_DEBUGCTL_TR
;
868 debugctlmsr
|= X86_DEBUGCTL_BTS
;
869 debugctlmsr
|= X86_DEBUGCTL_BTINT
;
871 if (!(config
& ARCH_PERFMON_EVENTSEL_OS
))
872 debugctlmsr
|= X86_DEBUGCTL_BTS_OFF_OS
;
874 if (!(config
& ARCH_PERFMON_EVENTSEL_USR
))
875 debugctlmsr
|= X86_DEBUGCTL_BTS_OFF_USR
;
877 update_debugctlmsr(debugctlmsr
);
880 static void intel_pmu_disable_bts(void)
882 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
883 unsigned long debugctlmsr
;
888 debugctlmsr
= get_debugctlmsr();
891 ~(X86_DEBUGCTL_TR
| X86_DEBUGCTL_BTS
| X86_DEBUGCTL_BTINT
|
892 X86_DEBUGCTL_BTS_OFF_OS
| X86_DEBUGCTL_BTS_OFF_USR
);
894 update_debugctlmsr(debugctlmsr
);
898 * Setup the hardware configuration for a given attr_type
900 static int __hw_perf_counter_init(struct perf_counter
*counter
)
902 struct perf_counter_attr
*attr
= &counter
->attr
;
903 struct hw_perf_counter
*hwc
= &counter
->hw
;
907 if (!x86_pmu_initialized())
911 if (!atomic_inc_not_zero(&active_counters
)) {
912 mutex_lock(&pmc_reserve_mutex
);
913 if (atomic_read(&active_counters
) == 0) {
914 if (!reserve_pmc_hardware())
917 reserve_bts_hardware();
920 atomic_inc(&active_counters
);
921 mutex_unlock(&pmc_reserve_mutex
);
928 * (keep 'enabled' bit clear for now)
930 hwc
->config
= ARCH_PERFMON_EVENTSEL_INT
;
933 * Count user and OS events unless requested not to.
935 if (!attr
->exclude_user
)
936 hwc
->config
|= ARCH_PERFMON_EVENTSEL_USR
;
937 if (!attr
->exclude_kernel
)
938 hwc
->config
|= ARCH_PERFMON_EVENTSEL_OS
;
940 if (!hwc
->sample_period
) {
941 hwc
->sample_period
= x86_pmu
.max_period
;
942 hwc
->last_period
= hwc
->sample_period
;
943 atomic64_set(&hwc
->period_left
, hwc
->sample_period
);
946 * If we have a PMU initialized but no APIC
947 * interrupts, we cannot sample hardware
948 * counters (user-space has to fall back and
949 * sample via a hrtimer based software counter):
955 counter
->destroy
= hw_perf_counter_destroy
;
958 * Raw event type provide the config in the event structure
960 if (attr
->type
== PERF_TYPE_RAW
) {
961 hwc
->config
|= x86_pmu
.raw_event(attr
->config
);
965 if (attr
->type
== PERF_TYPE_HW_CACHE
)
966 return set_ext_hw_attr(hwc
, attr
);
968 if (attr
->config
>= x86_pmu
.max_events
)
974 config
= x86_pmu
.event_map(attr
->config
);
982 hwc
->config
|= config
;
987 static void p6_pmu_disable_all(void)
989 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
998 /* p6 only has one enable register */
999 rdmsrl(MSR_P6_EVNTSEL0
, val
);
1000 val
&= ~ARCH_PERFMON_EVENTSEL0_ENABLE
;
1001 wrmsrl(MSR_P6_EVNTSEL0
, val
);
1004 static void intel_pmu_disable_all(void)
1006 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1014 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, 0);
1016 if (test_bit(X86_PMC_IDX_FIXED_BTS
, cpuc
->active_mask
))
1017 intel_pmu_disable_bts();
1020 static void amd_pmu_disable_all(void)
1022 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1030 * ensure we write the disable before we start disabling the
1031 * counters proper, so that amd_pmu_enable_counter() does the
1036 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1039 if (!test_bit(idx
, cpuc
->active_mask
))
1041 rdmsrl(MSR_K7_EVNTSEL0
+ idx
, val
);
1042 if (!(val
& ARCH_PERFMON_EVENTSEL0_ENABLE
))
1044 val
&= ~ARCH_PERFMON_EVENTSEL0_ENABLE
;
1045 wrmsrl(MSR_K7_EVNTSEL0
+ idx
, val
);
1049 void hw_perf_disable(void)
1051 if (!x86_pmu_initialized())
1053 return x86_pmu
.disable_all();
1056 static void p6_pmu_enable_all(void)
1058 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1067 /* p6 only has one enable register */
1068 rdmsrl(MSR_P6_EVNTSEL0
, val
);
1069 val
|= ARCH_PERFMON_EVENTSEL0_ENABLE
;
1070 wrmsrl(MSR_P6_EVNTSEL0
, val
);
1073 static void intel_pmu_enable_all(void)
1075 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1083 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, x86_pmu
.intel_ctrl
);
1085 if (test_bit(X86_PMC_IDX_FIXED_BTS
, cpuc
->active_mask
)) {
1086 struct perf_counter
*counter
=
1087 cpuc
->counters
[X86_PMC_IDX_FIXED_BTS
];
1089 if (WARN_ON_ONCE(!counter
))
1092 intel_pmu_enable_bts(counter
->hw
.config
);
1096 static void amd_pmu_enable_all(void)
1098 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1107 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1108 struct perf_counter
*counter
= cpuc
->counters
[idx
];
1111 if (!test_bit(idx
, cpuc
->active_mask
))
1114 val
= counter
->hw
.config
;
1115 val
|= ARCH_PERFMON_EVENTSEL0_ENABLE
;
1116 wrmsrl(MSR_K7_EVNTSEL0
+ idx
, val
);
1120 void hw_perf_enable(void)
1122 if (!x86_pmu_initialized())
1124 x86_pmu
.enable_all();
1127 static inline u64
intel_pmu_get_status(void)
1131 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS
, status
);
1136 static inline void intel_pmu_ack_status(u64 ack
)
1138 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL
, ack
);
1141 static inline void x86_pmu_enable_counter(struct hw_perf_counter
*hwc
, int idx
)
1143 (void)checking_wrmsrl(hwc
->config_base
+ idx
,
1144 hwc
->config
| ARCH_PERFMON_EVENTSEL0_ENABLE
);
1147 static inline void x86_pmu_disable_counter(struct hw_perf_counter
*hwc
, int idx
)
1149 (void)checking_wrmsrl(hwc
->config_base
+ idx
, hwc
->config
);
1153 intel_pmu_disable_fixed(struct hw_perf_counter
*hwc
, int __idx
)
1155 int idx
= __idx
- X86_PMC_IDX_FIXED
;
1158 mask
= 0xfULL
<< (idx
* 4);
1160 rdmsrl(hwc
->config_base
, ctrl_val
);
1162 (void)checking_wrmsrl(hwc
->config_base
, ctrl_val
);
1166 p6_pmu_disable_counter(struct hw_perf_counter
*hwc
, int idx
)
1168 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1169 u64 val
= P6_NOP_COUNTER
;
1172 val
|= ARCH_PERFMON_EVENTSEL0_ENABLE
;
1174 (void)checking_wrmsrl(hwc
->config_base
+ idx
, val
);
1178 intel_pmu_disable_counter(struct hw_perf_counter
*hwc
, int idx
)
1180 if (unlikely(idx
== X86_PMC_IDX_FIXED_BTS
)) {
1181 intel_pmu_disable_bts();
1185 if (unlikely(hwc
->config_base
== MSR_ARCH_PERFMON_FIXED_CTR_CTRL
)) {
1186 intel_pmu_disable_fixed(hwc
, idx
);
1190 x86_pmu_disable_counter(hwc
, idx
);
1194 amd_pmu_disable_counter(struct hw_perf_counter
*hwc
, int idx
)
1196 x86_pmu_disable_counter(hwc
, idx
);
1199 static DEFINE_PER_CPU(u64
, prev_left
[X86_PMC_IDX_MAX
]);
1202 * Set the next IRQ period, based on the hwc->period_left value.
1203 * To be called with the counter disabled in hw:
1206 x86_perf_counter_set_period(struct perf_counter
*counter
,
1207 struct hw_perf_counter
*hwc
, int idx
)
1209 s64 left
= atomic64_read(&hwc
->period_left
);
1210 s64 period
= hwc
->sample_period
;
1213 if (idx
== X86_PMC_IDX_FIXED_BTS
)
1217 * If we are way outside a reasoable range then just skip forward:
1219 if (unlikely(left
<= -period
)) {
1221 atomic64_set(&hwc
->period_left
, left
);
1222 hwc
->last_period
= period
;
1226 if (unlikely(left
<= 0)) {
1228 atomic64_set(&hwc
->period_left
, left
);
1229 hwc
->last_period
= period
;
1233 * Quirk: certain CPUs dont like it if just 1 event is left:
1235 if (unlikely(left
< 2))
1238 if (left
> x86_pmu
.max_period
)
1239 left
= x86_pmu
.max_period
;
1241 per_cpu(prev_left
[idx
], smp_processor_id()) = left
;
1244 * The hw counter starts counting from this counter offset,
1245 * mark it to be able to extra future deltas:
1247 atomic64_set(&hwc
->prev_count
, (u64
)-left
);
1249 err
= checking_wrmsrl(hwc
->counter_base
+ idx
,
1250 (u64
)(-left
) & x86_pmu
.counter_mask
);
1252 perf_counter_update_userpage(counter
);
1258 intel_pmu_enable_fixed(struct hw_perf_counter
*hwc
, int __idx
)
1260 int idx
= __idx
- X86_PMC_IDX_FIXED
;
1261 u64 ctrl_val
, bits
, mask
;
1265 * Enable IRQ generation (0x8),
1266 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
1270 if (hwc
->config
& ARCH_PERFMON_EVENTSEL_USR
)
1272 if (hwc
->config
& ARCH_PERFMON_EVENTSEL_OS
)
1275 mask
= 0xfULL
<< (idx
* 4);
1277 rdmsrl(hwc
->config_base
, ctrl_val
);
1280 err
= checking_wrmsrl(hwc
->config_base
, ctrl_val
);
1283 static void p6_pmu_enable_counter(struct hw_perf_counter
*hwc
, int idx
)
1285 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1290 val
|= ARCH_PERFMON_EVENTSEL0_ENABLE
;
1292 (void)checking_wrmsrl(hwc
->config_base
+ idx
, val
);
1296 static void intel_pmu_enable_counter(struct hw_perf_counter
*hwc
, int idx
)
1298 if (unlikely(idx
== X86_PMC_IDX_FIXED_BTS
)) {
1299 if (!__get_cpu_var(cpu_hw_counters
).enabled
)
1302 intel_pmu_enable_bts(hwc
->config
);
1306 if (unlikely(hwc
->config_base
== MSR_ARCH_PERFMON_FIXED_CTR_CTRL
)) {
1307 intel_pmu_enable_fixed(hwc
, idx
);
1311 x86_pmu_enable_counter(hwc
, idx
);
1314 static void amd_pmu_enable_counter(struct hw_perf_counter
*hwc
, int idx
)
1316 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1319 x86_pmu_enable_counter(hwc
, idx
);
1323 fixed_mode_idx(struct perf_counter
*counter
, struct hw_perf_counter
*hwc
)
1327 event
= hwc
->config
& ARCH_PERFMON_EVENT_MASK
;
1329 if (unlikely((event
==
1330 x86_pmu
.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS
)) &&
1331 (hwc
->sample_period
== 1)))
1332 return X86_PMC_IDX_FIXED_BTS
;
1334 if (!x86_pmu
.num_counters_fixed
)
1337 if (unlikely(event
== x86_pmu
.event_map(PERF_COUNT_HW_INSTRUCTIONS
)))
1338 return X86_PMC_IDX_FIXED_INSTRUCTIONS
;
1339 if (unlikely(event
== x86_pmu
.event_map(PERF_COUNT_HW_CPU_CYCLES
)))
1340 return X86_PMC_IDX_FIXED_CPU_CYCLES
;
1341 if (unlikely(event
== x86_pmu
.event_map(PERF_COUNT_HW_BUS_CYCLES
)))
1342 return X86_PMC_IDX_FIXED_BUS_CYCLES
;
1348 * Find a PMC slot for the freshly enabled / scheduled in counter:
1350 static int x86_pmu_enable(struct perf_counter
*counter
)
1352 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1353 struct hw_perf_counter
*hwc
= &counter
->hw
;
1356 idx
= fixed_mode_idx(counter
, hwc
);
1357 if (idx
== X86_PMC_IDX_FIXED_BTS
) {
1359 * Try to use BTS for branch tracing. If that is not
1360 * available, try to get a generic counter.
1362 if (unlikely(!cpuc
->ds
))
1366 * Try to get the fixed counter, if that is already taken
1367 * then try to get a generic counter:
1369 if (test_and_set_bit(idx
, cpuc
->used_mask
))
1372 hwc
->config_base
= 0;
1373 hwc
->counter_base
= 0;
1375 } else if (idx
>= 0) {
1377 * Try to get the fixed counter, if that is already taken
1378 * then try to get a generic counter:
1380 if (test_and_set_bit(idx
, cpuc
->used_mask
))
1383 hwc
->config_base
= MSR_ARCH_PERFMON_FIXED_CTR_CTRL
;
1385 * We set it so that counter_base + idx in wrmsr/rdmsr maps to
1386 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
1389 MSR_ARCH_PERFMON_FIXED_CTR0
- X86_PMC_IDX_FIXED
;
1393 /* Try to get the previous generic counter again */
1394 if (test_and_set_bit(idx
, cpuc
->used_mask
)) {
1396 idx
= find_first_zero_bit(cpuc
->used_mask
,
1397 x86_pmu
.num_counters
);
1398 if (idx
== x86_pmu
.num_counters
)
1401 set_bit(idx
, cpuc
->used_mask
);
1404 hwc
->config_base
= x86_pmu
.eventsel
;
1405 hwc
->counter_base
= x86_pmu
.perfctr
;
1408 perf_counters_lapic_init();
1410 x86_pmu
.disable(hwc
, idx
);
1412 cpuc
->counters
[idx
] = counter
;
1413 set_bit(idx
, cpuc
->active_mask
);
1415 x86_perf_counter_set_period(counter
, hwc
, idx
);
1416 x86_pmu
.enable(hwc
, idx
);
1418 perf_counter_update_userpage(counter
);
1423 static void x86_pmu_unthrottle(struct perf_counter
*counter
)
1425 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1426 struct hw_perf_counter
*hwc
= &counter
->hw
;
1428 if (WARN_ON_ONCE(hwc
->idx
>= X86_PMC_IDX_MAX
||
1429 cpuc
->counters
[hwc
->idx
] != counter
))
1432 x86_pmu
.enable(hwc
, hwc
->idx
);
1435 void perf_counter_print_debug(void)
1437 u64 ctrl
, status
, overflow
, pmc_ctrl
, pmc_count
, prev_left
, fixed
;
1438 struct cpu_hw_counters
*cpuc
;
1439 unsigned long flags
;
1442 if (!x86_pmu
.num_counters
)
1445 local_irq_save(flags
);
1447 cpu
= smp_processor_id();
1448 cpuc
= &per_cpu(cpu_hw_counters
, cpu
);
1450 if (x86_pmu
.version
>= 2) {
1451 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, ctrl
);
1452 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS
, status
);
1453 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL
, overflow
);
1454 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL
, fixed
);
1457 pr_info("CPU#%d: ctrl: %016llx\n", cpu
, ctrl
);
1458 pr_info("CPU#%d: status: %016llx\n", cpu
, status
);
1459 pr_info("CPU#%d: overflow: %016llx\n", cpu
, overflow
);
1460 pr_info("CPU#%d: fixed: %016llx\n", cpu
, fixed
);
1462 pr_info("CPU#%d: used: %016llx\n", cpu
, *(u64
*)cpuc
->used_mask
);
1464 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1465 rdmsrl(x86_pmu
.eventsel
+ idx
, pmc_ctrl
);
1466 rdmsrl(x86_pmu
.perfctr
+ idx
, pmc_count
);
1468 prev_left
= per_cpu(prev_left
[idx
], cpu
);
1470 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
1471 cpu
, idx
, pmc_ctrl
);
1472 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
1473 cpu
, idx
, pmc_count
);
1474 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
1475 cpu
, idx
, prev_left
);
1477 for (idx
= 0; idx
< x86_pmu
.num_counters_fixed
; idx
++) {
1478 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0
+ idx
, pmc_count
);
1480 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
1481 cpu
, idx
, pmc_count
);
1483 local_irq_restore(flags
);
1486 static void intel_pmu_drain_bts_buffer(struct cpu_hw_counters
*cpuc
,
1487 struct perf_sample_data
*data
)
1489 struct debug_store
*ds
= cpuc
->ds
;
1495 struct perf_counter
*counter
= cpuc
->counters
[X86_PMC_IDX_FIXED_BTS
];
1496 unsigned long orig_ip
= data
->regs
->ip
;
1505 for (at
= ds
->bts_buffer_base
;
1507 at
+= sizeof(struct bts_record
)) {
1508 struct bts_record
*rec
= (struct bts_record
*)(long)at
;
1510 data
->regs
->ip
= rec
->from
;
1511 data
->addr
= rec
->to
;
1513 perf_counter_output(counter
, 1, data
);
1516 ds
->bts_index
= ds
->bts_buffer_base
;
1518 data
->regs
->ip
= orig_ip
;
1521 /* There's new data available. */
1522 counter
->pending_kill
= POLL_IN
;
1525 static void x86_pmu_disable(struct perf_counter
*counter
)
1527 struct cpu_hw_counters
*cpuc
= &__get_cpu_var(cpu_hw_counters
);
1528 struct hw_perf_counter
*hwc
= &counter
->hw
;
1532 * Must be done before we disable, otherwise the nmi handler
1533 * could reenable again:
1535 clear_bit(idx
, cpuc
->active_mask
);
1536 x86_pmu
.disable(hwc
, idx
);
1539 * Make sure the cleared pointer becomes visible before we
1540 * (potentially) free the counter:
1545 * Drain the remaining delta count out of a counter
1546 * that we are disabling:
1548 x86_perf_counter_update(counter
, hwc
, idx
);
1550 /* Drain the remaining BTS records. */
1551 if (unlikely(idx
== X86_PMC_IDX_FIXED_BTS
)) {
1552 struct perf_sample_data data
;
1553 struct pt_regs regs
;
1556 intel_pmu_drain_bts_buffer(cpuc
, &data
);
1558 cpuc
->counters
[idx
] = NULL
;
1559 clear_bit(idx
, cpuc
->used_mask
);
1561 perf_counter_update_userpage(counter
);
1565 * Save and restart an expired counter. Called by NMI contexts,
1566 * so it has to be careful about preempting normal counter ops:
1568 static int intel_pmu_save_and_restart(struct perf_counter
*counter
)
1570 struct hw_perf_counter
*hwc
= &counter
->hw
;
1574 x86_perf_counter_update(counter
, hwc
, idx
);
1575 ret
= x86_perf_counter_set_period(counter
, hwc
, idx
);
1577 if (counter
->state
== PERF_COUNTER_STATE_ACTIVE
)
1578 intel_pmu_enable_counter(hwc
, idx
);
1583 static void intel_pmu_reset(void)
1585 struct debug_store
*ds
= __get_cpu_var(cpu_hw_counters
).ds
;
1586 unsigned long flags
;
1589 if (!x86_pmu
.num_counters
)
1592 local_irq_save(flags
);
1594 printk("clearing PMU state on CPU#%d\n", smp_processor_id());
1596 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1597 checking_wrmsrl(x86_pmu
.eventsel
+ idx
, 0ull);
1598 checking_wrmsrl(x86_pmu
.perfctr
+ idx
, 0ull);
1600 for (idx
= 0; idx
< x86_pmu
.num_counters_fixed
; idx
++) {
1601 checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0
+ idx
, 0ull);
1604 ds
->bts_index
= ds
->bts_buffer_base
;
1606 local_irq_restore(flags
);
1609 static int p6_pmu_handle_irq(struct pt_regs
*regs
)
1611 struct perf_sample_data data
;
1612 struct cpu_hw_counters
*cpuc
;
1613 struct perf_counter
*counter
;
1614 struct hw_perf_counter
*hwc
;
1615 int idx
, handled
= 0;
1621 cpuc
= &__get_cpu_var(cpu_hw_counters
);
1623 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1624 if (!test_bit(idx
, cpuc
->active_mask
))
1627 counter
= cpuc
->counters
[idx
];
1630 val
= x86_perf_counter_update(counter
, hwc
, idx
);
1631 if (val
& (1ULL << (x86_pmu
.counter_bits
- 1)))
1638 data
.period
= counter
->hw
.last_period
;
1640 if (!x86_perf_counter_set_period(counter
, hwc
, idx
))
1643 if (perf_counter_overflow(counter
, 1, &data
))
1644 p6_pmu_disable_counter(hwc
, idx
);
1648 inc_irq_stat(apic_perf_irqs
);
1654 * This handler is triggered by the local APIC, so the APIC IRQ handling
1657 static int intel_pmu_handle_irq(struct pt_regs
*regs
)
1659 struct perf_sample_data data
;
1660 struct cpu_hw_counters
*cpuc
;
1667 cpuc
= &__get_cpu_var(cpu_hw_counters
);
1670 intel_pmu_drain_bts_buffer(cpuc
, &data
);
1671 status
= intel_pmu_get_status();
1679 if (++loops
> 100) {
1680 WARN_ONCE(1, "perfcounters: irq loop stuck!\n");
1681 perf_counter_print_debug();
1687 inc_irq_stat(apic_perf_irqs
);
1689 for_each_bit(bit
, (unsigned long *)&status
, X86_PMC_IDX_MAX
) {
1690 struct perf_counter
*counter
= cpuc
->counters
[bit
];
1692 clear_bit(bit
, (unsigned long *) &status
);
1693 if (!test_bit(bit
, cpuc
->active_mask
))
1696 if (!intel_pmu_save_and_restart(counter
))
1699 data
.period
= counter
->hw
.last_period
;
1701 if (perf_counter_overflow(counter
, 1, &data
))
1702 intel_pmu_disable_counter(&counter
->hw
, bit
);
1705 intel_pmu_ack_status(ack
);
1708 * Repeat if there is more work to be done:
1710 status
= intel_pmu_get_status();
1719 static int amd_pmu_handle_irq(struct pt_regs
*regs
)
1721 struct perf_sample_data data
;
1722 struct cpu_hw_counters
*cpuc
;
1723 struct perf_counter
*counter
;
1724 struct hw_perf_counter
*hwc
;
1725 int idx
, handled
= 0;
1731 cpuc
= &__get_cpu_var(cpu_hw_counters
);
1733 for (idx
= 0; idx
< x86_pmu
.num_counters
; idx
++) {
1734 if (!test_bit(idx
, cpuc
->active_mask
))
1737 counter
= cpuc
->counters
[idx
];
1740 val
= x86_perf_counter_update(counter
, hwc
, idx
);
1741 if (val
& (1ULL << (x86_pmu
.counter_bits
- 1)))
1748 data
.period
= counter
->hw
.last_period
;
1750 if (!x86_perf_counter_set_period(counter
, hwc
, idx
))
1753 if (perf_counter_overflow(counter
, 1, &data
))
1754 amd_pmu_disable_counter(hwc
, idx
);
1758 inc_irq_stat(apic_perf_irqs
);
1763 void smp_perf_pending_interrupt(struct pt_regs
*regs
)
1767 inc_irq_stat(apic_pending_irqs
);
1768 perf_counter_do_pending();
1772 void set_perf_counter_pending(void)
1774 #ifdef CONFIG_X86_LOCAL_APIC
1775 apic
->send_IPI_self(LOCAL_PENDING_VECTOR
);
1779 void perf_counters_lapic_init(void)
1781 #ifdef CONFIG_X86_LOCAL_APIC
1782 if (!x86_pmu
.apic
|| !x86_pmu_initialized())
1786 * Always use NMI for PMU
1788 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1792 static int __kprobes
1793 perf_counter_nmi_handler(struct notifier_block
*self
,
1794 unsigned long cmd
, void *__args
)
1796 struct die_args
*args
= __args
;
1797 struct pt_regs
*regs
;
1799 if (!atomic_read(&active_counters
))
1813 #ifdef CONFIG_X86_LOCAL_APIC
1814 apic_write(APIC_LVTPC
, APIC_DM_NMI
);
1817 * Can't rely on the handled return value to say it was our NMI, two
1818 * counters could trigger 'simultaneously' raising two back-to-back NMIs.
1820 * If the first NMI handles both, the latter will be empty and daze
1823 x86_pmu
.handle_irq(regs
);
1828 static __read_mostly
struct notifier_block perf_counter_nmi_notifier
= {
1829 .notifier_call
= perf_counter_nmi_handler
,
1834 static struct x86_pmu p6_pmu
= {
1836 .handle_irq
= p6_pmu_handle_irq
,
1837 .disable_all
= p6_pmu_disable_all
,
1838 .enable_all
= p6_pmu_enable_all
,
1839 .enable
= p6_pmu_enable_counter
,
1840 .disable
= p6_pmu_disable_counter
,
1841 .eventsel
= MSR_P6_EVNTSEL0
,
1842 .perfctr
= MSR_P6_PERFCTR0
,
1843 .event_map
= p6_pmu_event_map
,
1844 .raw_event
= p6_pmu_raw_event
,
1845 .max_events
= ARRAY_SIZE(p6_perfmon_event_map
),
1847 .max_period
= (1ULL << 31) - 1,
1851 * Counters have 40 bits implemented. However they are designed such
1852 * that bits [32-39] are sign extensions of bit 31. As such the
1853 * effective width of a counter for P6-like PMU is 32 bits only.
1855 * See IA-32 Intel Architecture Software developer manual Vol 3B
1858 .counter_mask
= (1ULL << 32) - 1,
1861 static struct x86_pmu intel_pmu
= {
1863 .handle_irq
= intel_pmu_handle_irq
,
1864 .disable_all
= intel_pmu_disable_all
,
1865 .enable_all
= intel_pmu_enable_all
,
1866 .enable
= intel_pmu_enable_counter
,
1867 .disable
= intel_pmu_disable_counter
,
1868 .eventsel
= MSR_ARCH_PERFMON_EVENTSEL0
,
1869 .perfctr
= MSR_ARCH_PERFMON_PERFCTR0
,
1870 .event_map
= intel_pmu_event_map
,
1871 .raw_event
= intel_pmu_raw_event
,
1872 .max_events
= ARRAY_SIZE(intel_perfmon_event_map
),
1875 * Intel PMCs cannot be accessed sanely above 32 bit width,
1876 * so we install an artificial 1<<31 period regardless of
1877 * the generic counter period:
1879 .max_period
= (1ULL << 31) - 1,
1880 .enable_bts
= intel_pmu_enable_bts
,
1881 .disable_bts
= intel_pmu_disable_bts
,
1884 static struct x86_pmu amd_pmu
= {
1886 .handle_irq
= amd_pmu_handle_irq
,
1887 .disable_all
= amd_pmu_disable_all
,
1888 .enable_all
= amd_pmu_enable_all
,
1889 .enable
= amd_pmu_enable_counter
,
1890 .disable
= amd_pmu_disable_counter
,
1891 .eventsel
= MSR_K7_EVNTSEL0
,
1892 .perfctr
= MSR_K7_PERFCTR0
,
1893 .event_map
= amd_pmu_event_map
,
1894 .raw_event
= amd_pmu_raw_event
,
1895 .max_events
= ARRAY_SIZE(amd_perfmon_event_map
),
1898 .counter_mask
= (1ULL << 48) - 1,
1900 /* use highest bit to detect overflow */
1901 .max_period
= (1ULL << 47) - 1,
1904 static int p6_pmu_init(void)
1906 switch (boot_cpu_data
.x86_model
) {
1908 case 3: /* Pentium Pro */
1910 case 6: /* Pentium II */
1913 case 11: /* Pentium III */
1920 pr_cont("unsupported p6 CPU model %d ",
1921 boot_cpu_data
.x86_model
);
1927 if (!cpu_has_apic
) {
1928 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
1929 pr_info("no hardware sampling interrupt available.\n");
1936 static int intel_pmu_init(void)
1938 union cpuid10_edx edx
;
1939 union cpuid10_eax eax
;
1940 unsigned int unused
;
1944 if (!cpu_has(&boot_cpu_data
, X86_FEATURE_ARCH_PERFMON
)) {
1945 /* check for P6 processor family */
1946 if (boot_cpu_data
.x86
== 6) {
1947 return p6_pmu_init();
1954 * Check whether the Architectural PerfMon supports
1955 * Branch Misses Retired Event or not.
1957 cpuid(10, &eax
.full
, &ebx
, &unused
, &edx
.full
);
1958 if (eax
.split
.mask_length
<= ARCH_PERFMON_BRANCH_MISSES_RETIRED
)
1961 version
= eax
.split
.version_id
;
1965 x86_pmu
= intel_pmu
;
1966 x86_pmu
.version
= version
;
1967 x86_pmu
.num_counters
= eax
.split
.num_counters
;
1968 x86_pmu
.counter_bits
= eax
.split
.bit_width
;
1969 x86_pmu
.counter_mask
= (1ULL << eax
.split
.bit_width
) - 1;
1972 * Quirk: v2 perfmon does not report fixed-purpose counters, so
1973 * assume at least 3 counters:
1975 x86_pmu
.num_counters_fixed
= max((int)edx
.split
.num_counters_fixed
, 3);
1978 * Install the hw-cache-events table:
1980 switch (boot_cpu_data
.x86_model
) {
1981 case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
1982 case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
1983 case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
1984 case 29: /* six-core 45 nm xeon "Dunnington" */
1985 memcpy(hw_cache_event_ids
, core2_hw_cache_event_ids
,
1986 sizeof(hw_cache_event_ids
));
1988 pr_cont("Core2 events, ");
1992 memcpy(hw_cache_event_ids
, nehalem_hw_cache_event_ids
,
1993 sizeof(hw_cache_event_ids
));
1995 pr_cont("Nehalem/Corei7 events, ");
1998 memcpy(hw_cache_event_ids
, atom_hw_cache_event_ids
,
1999 sizeof(hw_cache_event_ids
));
2001 pr_cont("Atom events, ");
2007 static int amd_pmu_init(void)
2009 /* Performance-monitoring supported from K7 and later: */
2010 if (boot_cpu_data
.x86
< 6)
2015 /* Events are common for all AMDs */
2016 memcpy(hw_cache_event_ids
, amd_hw_cache_event_ids
,
2017 sizeof(hw_cache_event_ids
));
2022 void __init
init_hw_perf_counters(void)
2026 pr_info("Performance Counters: ");
2028 switch (boot_cpu_data
.x86_vendor
) {
2029 case X86_VENDOR_INTEL
:
2030 err
= intel_pmu_init();
2032 case X86_VENDOR_AMD
:
2033 err
= amd_pmu_init();
2039 pr_cont("no PMU driver, software counters only.\n");
2043 pr_cont("%s PMU driver.\n", x86_pmu
.name
);
2045 if (x86_pmu
.num_counters
> X86_PMC_MAX_GENERIC
) {
2046 WARN(1, KERN_ERR
"hw perf counters %d > max(%d), clipping!",
2047 x86_pmu
.num_counters
, X86_PMC_MAX_GENERIC
);
2048 x86_pmu
.num_counters
= X86_PMC_MAX_GENERIC
;
2050 perf_counter_mask
= (1 << x86_pmu
.num_counters
) - 1;
2051 perf_max_counters
= x86_pmu
.num_counters
;
2053 if (x86_pmu
.num_counters_fixed
> X86_PMC_MAX_FIXED
) {
2054 WARN(1, KERN_ERR
"hw perf counters fixed %d > max(%d), clipping!",
2055 x86_pmu
.num_counters_fixed
, X86_PMC_MAX_FIXED
);
2056 x86_pmu
.num_counters_fixed
= X86_PMC_MAX_FIXED
;
2059 perf_counter_mask
|=
2060 ((1LL << x86_pmu
.num_counters_fixed
)-1) << X86_PMC_IDX_FIXED
;
2061 x86_pmu
.intel_ctrl
= perf_counter_mask
;
2063 perf_counters_lapic_init();
2064 register_die_notifier(&perf_counter_nmi_notifier
);
2066 pr_info("... version: %d\n", x86_pmu
.version
);
2067 pr_info("... bit width: %d\n", x86_pmu
.counter_bits
);
2068 pr_info("... generic counters: %d\n", x86_pmu
.num_counters
);
2069 pr_info("... value mask: %016Lx\n", x86_pmu
.counter_mask
);
2070 pr_info("... max period: %016Lx\n", x86_pmu
.max_period
);
2071 pr_info("... fixed-purpose counters: %d\n", x86_pmu
.num_counters_fixed
);
2072 pr_info("... counter mask: %016Lx\n", perf_counter_mask
);
2075 static inline void x86_pmu_read(struct perf_counter
*counter
)
2077 x86_perf_counter_update(counter
, &counter
->hw
, counter
->hw
.idx
);
2080 static const struct pmu pmu
= {
2081 .enable
= x86_pmu_enable
,
2082 .disable
= x86_pmu_disable
,
2083 .read
= x86_pmu_read
,
2084 .unthrottle
= x86_pmu_unthrottle
,
2087 const struct pmu
*hw_perf_counter_init(struct perf_counter
*counter
)
2091 err
= __hw_perf_counter_init(counter
);
2093 return ERR_PTR(err
);
2103 void callchain_store(struct perf_callchain_entry
*entry
, u64 ip
)
2105 if (entry
->nr
< PERF_MAX_STACK_DEPTH
)
2106 entry
->ip
[entry
->nr
++] = ip
;
2109 static DEFINE_PER_CPU(struct perf_callchain_entry
, irq_entry
);
2110 static DEFINE_PER_CPU(struct perf_callchain_entry
, nmi_entry
);
2111 static DEFINE_PER_CPU(int, in_nmi_frame
);
2115 backtrace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
2117 /* Ignore warnings */
2120 static void backtrace_warning(void *data
, char *msg
)
2122 /* Ignore warnings */
2125 static int backtrace_stack(void *data
, char *name
)
2127 per_cpu(in_nmi_frame
, smp_processor_id()) =
2128 x86_is_stack_id(NMI_STACK
, name
);
2133 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
2135 struct perf_callchain_entry
*entry
= data
;
2137 if (per_cpu(in_nmi_frame
, smp_processor_id()))
2141 callchain_store(entry
, addr
);
2144 static const struct stacktrace_ops backtrace_ops
= {
2145 .warning
= backtrace_warning
,
2146 .warning_symbol
= backtrace_warning_symbol
,
2147 .stack
= backtrace_stack
,
2148 .address
= backtrace_address
,
2151 #include "../dumpstack.h"
2154 perf_callchain_kernel(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
2156 callchain_store(entry
, PERF_CONTEXT_KERNEL
);
2157 callchain_store(entry
, regs
->ip
);
2159 dump_trace(NULL
, regs
, NULL
, 0, &backtrace_ops
, entry
);
2163 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
2165 static unsigned long
2166 copy_from_user_nmi(void *to
, const void __user
*from
, unsigned long n
)
2168 unsigned long offset
, addr
= (unsigned long)from
;
2169 int type
= in_nmi() ? KM_NMI
: KM_IRQ0
;
2170 unsigned long size
, len
= 0;
2176 ret
= __get_user_pages_fast(addr
, 1, 0, &page
);
2180 offset
= addr
& (PAGE_SIZE
- 1);
2181 size
= min(PAGE_SIZE
- offset
, n
- len
);
2183 map
= kmap_atomic(page
, type
);
2184 memcpy(to
, map
+offset
, size
);
2185 kunmap_atomic(map
, type
);
2197 static int copy_stack_frame(const void __user
*fp
, struct stack_frame
*frame
)
2199 unsigned long bytes
;
2201 bytes
= copy_from_user_nmi(frame
, fp
, sizeof(*frame
));
2203 return bytes
== sizeof(*frame
);
2207 perf_callchain_user(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
2209 struct stack_frame frame
;
2210 const void __user
*fp
;
2212 if (!user_mode(regs
))
2213 regs
= task_pt_regs(current
);
2215 fp
= (void __user
*)regs
->bp
;
2217 callchain_store(entry
, PERF_CONTEXT_USER
);
2218 callchain_store(entry
, regs
->ip
);
2220 while (entry
->nr
< PERF_MAX_STACK_DEPTH
) {
2221 frame
.next_frame
= NULL
;
2222 frame
.return_address
= 0;
2224 if (!copy_stack_frame(fp
, &frame
))
2227 if ((unsigned long)fp
< regs
->sp
)
2230 callchain_store(entry
, frame
.return_address
);
2231 fp
= frame
.next_frame
;
2236 perf_do_callchain(struct pt_regs
*regs
, struct perf_callchain_entry
*entry
)
2243 is_user
= user_mode(regs
);
2245 if (!current
|| current
->pid
== 0)
2248 if (is_user
&& current
->state
!= TASK_RUNNING
)
2252 perf_callchain_kernel(regs
, entry
);
2255 perf_callchain_user(regs
, entry
);
2258 struct perf_callchain_entry
*perf_callchain(struct pt_regs
*regs
)
2260 struct perf_callchain_entry
*entry
;
2263 entry
= &__get_cpu_var(nmi_entry
);
2265 entry
= &__get_cpu_var(irq_entry
);
2269 perf_do_callchain(regs
, entry
);
2274 void hw_perf_counter_setup_online(int cpu
)
2276 init_debug_store_on_cpu(cpu
);