2 * Performance events 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>
10 * Copyright (C) 2009 Google, Inc., Stephane Eranian
12 * For licencing details see kernel-base/COPYING
15 #include <linux/perf_event.h>
16 #include <linux/capability.h>
17 #include <linux/notifier.h>
18 #include <linux/hardirq.h>
19 #include <linux/kprobes.h>
20 #include <linux/module.h>
21 #include <linux/kdebug.h>
22 #include <linux/sched.h>
23 #include <linux/uaccess.h>
24 #include <linux/highmem.h>
25 #include <linux/cpu.h>
26 #include <linux/bitops.h>
29 #include <asm/stacktrace.h>
32 static u64 perf_event_mask __read_mostly;
34 /* The maximal number of PEBS events: */
35 #define MAX_PEBS_EVENTS 4
37 /* The size of a BTS record in bytes: */
38 #define BTS_RECORD_SIZE 24
40 /* The size of a per-cpu BTS buffer in bytes: */
41 #define BTS_BUFFER_SIZE (BTS_RECORD_SIZE * 2048)
43 /* The BTS overflow threshold in bytes from the end of the buffer: */
44 #define BTS_OVFL_TH (BTS_RECORD_SIZE * 128)
48 * Bits in the debugctlmsr controlling branch tracing.
50 #define X86_DEBUGCTL_TR (1 << 6)
51 #define X86_DEBUGCTL_BTS (1 << 7)
52 #define X86_DEBUGCTL_BTINT (1 << 8)
53 #define X86_DEBUGCTL_BTS_OFF_OS (1 << 9)
54 #define X86_DEBUGCTL_BTS_OFF_USR (1 << 10)
57 * A debug store configuration.
59 * We only support architectures that use 64bit fields.
64 u64 bts_absolute_maximum;
65 u64 bts_interrupt_threshold;
68 u64 pebs_absolute_maximum;
69 u64 pebs_interrupt_threshold;
70 u64 pebs_event_reset[MAX_PEBS_EVENTS];
73 struct event_constraint {
75 unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
83 struct cpu_hw_events {
84 struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */
85 unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
86 unsigned long interrupts;
88 struct debug_store *ds;
92 int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
93 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
96 #define EVENT_CONSTRAINT(c, n, m) { \
97 { .idxmsk64[0] = (n) }, \
100 .weight = HWEIGHT64((u64)(n)), \
103 #define INTEL_EVENT_CONSTRAINT(c, n) \
104 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVTSEL_MASK)
106 #define FIXED_EVENT_CONSTRAINT(c, n) \
107 EVENT_CONSTRAINT(c, n, INTEL_ARCH_FIXED_MASK)
109 #define EVENT_CONSTRAINT_END \
110 EVENT_CONSTRAINT(0, 0, 0)
112 #define for_each_event_constraint(e, c) \
113 for ((e) = (c); (e)->cmask; (e)++)
116 * struct x86_pmu - generic x86 pmu
121 int (*handle_irq)(struct pt_regs *);
122 void (*disable_all)(void);
123 void (*enable_all)(void);
124 void (*enable)(struct hw_perf_event *, int);
125 void (*disable)(struct hw_perf_event *, int);
128 u64 (*event_map)(int);
129 u64 (*raw_event)(u64);
132 int num_events_fixed;
138 void (*enable_bts)(u64 config);
139 void (*disable_bts)(void);
141 struct event_constraint *
142 (*get_event_constraints)(struct cpu_hw_events *cpuc,
143 struct perf_event *event);
145 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
146 struct perf_event *event);
147 struct event_constraint *event_constraints;
150 static struct x86_pmu x86_pmu __read_mostly;
152 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
156 static int x86_perf_event_set_period(struct perf_event *event,
157 struct hw_perf_event *hwc, int idx);
160 * Not sure about some of these
162 static const u64 p6_perfmon_event_map[] =
164 [PERF_COUNT_HW_CPU_CYCLES] = 0x0079,
165 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
166 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0f2e,
167 [PERF_COUNT_HW_CACHE_MISSES] = 0x012e,
168 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
169 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
170 [PERF_COUNT_HW_BUS_CYCLES] = 0x0062,
173 static u64 p6_pmu_event_map(int hw_event)
175 return p6_perfmon_event_map[hw_event];
179 * Event setting that is specified not to count anything.
180 * We use this to effectively disable a counter.
182 * L2_RQSTS with 0 MESI unit mask.
184 #define P6_NOP_EVENT 0x0000002EULL
186 static u64 p6_pmu_raw_event(u64 hw_event)
188 #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL
189 #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL
190 #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL
191 #define P6_EVNTSEL_INV_MASK 0x00800000ULL
192 #define P6_EVNTSEL_REG_MASK 0xFF000000ULL
194 #define P6_EVNTSEL_MASK \
195 (P6_EVNTSEL_EVENT_MASK | \
196 P6_EVNTSEL_UNIT_MASK | \
197 P6_EVNTSEL_EDGE_MASK | \
198 P6_EVNTSEL_INV_MASK | \
201 return hw_event & P6_EVNTSEL_MASK;
204 static struct event_constraint intel_p6_event_constraints[] =
206 INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FLOPS */
207 INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
208 INTEL_EVENT_CONSTRAINT(0x11, 0x1), /* FP_ASSIST */
209 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
210 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
211 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
216 * Intel PerfMon v3. Used on Core2 and later.
218 static const u64 intel_perfmon_event_map[] =
220 [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
221 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
222 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
223 [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
224 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
225 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
226 [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
229 static struct event_constraint intel_core_event_constraints[] =
231 FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
232 FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
233 INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
234 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
235 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
236 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
237 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
238 INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
239 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
240 INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
241 INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
245 static struct event_constraint intel_nehalem_event_constraints[] =
247 FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
248 FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
249 INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
250 INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
251 INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
252 INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
253 INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
254 INTEL_EVENT_CONSTRAINT(0x4c, 0x3), /* LOAD_HIT_PRE */
255 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
256 INTEL_EVENT_CONSTRAINT(0x52, 0x3), /* L1D_CACHE_PREFETCH_LOCK_FB_HIT */
257 INTEL_EVENT_CONSTRAINT(0x53, 0x3), /* L1D_CACHE_LOCK_FB_HIT */
258 INTEL_EVENT_CONSTRAINT(0xc5, 0x3), /* CACHE_LOCK_CYCLES */
262 static struct event_constraint intel_gen_event_constraints[] =
264 FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
265 FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
269 static u64 intel_pmu_event_map(int hw_event)
271 return intel_perfmon_event_map[hw_event];
275 * Generalized hw caching related hw_event table, filled
276 * in on a per model basis. A value of 0 means
277 * 'not supported', -1 means 'hw_event makes no sense on
278 * this CPU', any other value means the raw hw_event
282 #define C(x) PERF_COUNT_HW_CACHE_##x
284 static u64 __read_mostly hw_cache_event_ids
285 [PERF_COUNT_HW_CACHE_MAX]
286 [PERF_COUNT_HW_CACHE_OP_MAX]
287 [PERF_COUNT_HW_CACHE_RESULT_MAX];
289 static __initconst u64 nehalem_hw_cache_event_ids
290 [PERF_COUNT_HW_CACHE_MAX]
291 [PERF_COUNT_HW_CACHE_OP_MAX]
292 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
296 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
297 [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
300 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
301 [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
303 [ C(OP_PREFETCH) ] = {
304 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
305 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
310 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
311 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
314 [ C(RESULT_ACCESS) ] = -1,
315 [ C(RESULT_MISS) ] = -1,
317 [ C(OP_PREFETCH) ] = {
318 [ C(RESULT_ACCESS) ] = 0x0,
319 [ C(RESULT_MISS) ] = 0x0,
324 [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */
325 [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */
328 [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */
329 [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
331 [ C(OP_PREFETCH) ] = {
332 [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */
333 [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */
338 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
339 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
342 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
343 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
345 [ C(OP_PREFETCH) ] = {
346 [ C(RESULT_ACCESS) ] = 0x0,
347 [ C(RESULT_MISS) ] = 0x0,
352 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
353 [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
356 [ C(RESULT_ACCESS) ] = -1,
357 [ C(RESULT_MISS) ] = -1,
359 [ C(OP_PREFETCH) ] = {
360 [ C(RESULT_ACCESS) ] = -1,
361 [ C(RESULT_MISS) ] = -1,
366 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
367 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
370 [ C(RESULT_ACCESS) ] = -1,
371 [ C(RESULT_MISS) ] = -1,
373 [ C(OP_PREFETCH) ] = {
374 [ C(RESULT_ACCESS) ] = -1,
375 [ C(RESULT_MISS) ] = -1,
380 static __initconst u64 core2_hw_cache_event_ids
381 [PERF_COUNT_HW_CACHE_MAX]
382 [PERF_COUNT_HW_CACHE_OP_MAX]
383 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
387 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
388 [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
391 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
392 [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
394 [ C(OP_PREFETCH) ] = {
395 [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
396 [ C(RESULT_MISS) ] = 0,
401 [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
402 [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
405 [ C(RESULT_ACCESS) ] = -1,
406 [ C(RESULT_MISS) ] = -1,
408 [ C(OP_PREFETCH) ] = {
409 [ C(RESULT_ACCESS) ] = 0,
410 [ C(RESULT_MISS) ] = 0,
415 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
416 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
419 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
420 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
422 [ C(OP_PREFETCH) ] = {
423 [ C(RESULT_ACCESS) ] = 0,
424 [ C(RESULT_MISS) ] = 0,
429 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
430 [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
433 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
434 [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
436 [ C(OP_PREFETCH) ] = {
437 [ C(RESULT_ACCESS) ] = 0,
438 [ C(RESULT_MISS) ] = 0,
443 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
444 [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
447 [ C(RESULT_ACCESS) ] = -1,
448 [ C(RESULT_MISS) ] = -1,
450 [ C(OP_PREFETCH) ] = {
451 [ C(RESULT_ACCESS) ] = -1,
452 [ C(RESULT_MISS) ] = -1,
457 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
458 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
461 [ C(RESULT_ACCESS) ] = -1,
462 [ C(RESULT_MISS) ] = -1,
464 [ C(OP_PREFETCH) ] = {
465 [ C(RESULT_ACCESS) ] = -1,
466 [ C(RESULT_MISS) ] = -1,
471 static __initconst u64 atom_hw_cache_event_ids
472 [PERF_COUNT_HW_CACHE_MAX]
473 [PERF_COUNT_HW_CACHE_OP_MAX]
474 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
478 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
479 [ C(RESULT_MISS) ] = 0,
482 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
483 [ C(RESULT_MISS) ] = 0,
485 [ C(OP_PREFETCH) ] = {
486 [ C(RESULT_ACCESS) ] = 0x0,
487 [ C(RESULT_MISS) ] = 0,
492 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
493 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
496 [ C(RESULT_ACCESS) ] = -1,
497 [ C(RESULT_MISS) ] = -1,
499 [ C(OP_PREFETCH) ] = {
500 [ C(RESULT_ACCESS) ] = 0,
501 [ C(RESULT_MISS) ] = 0,
506 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
507 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
510 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
511 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
513 [ C(OP_PREFETCH) ] = {
514 [ C(RESULT_ACCESS) ] = 0,
515 [ C(RESULT_MISS) ] = 0,
520 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
521 [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
524 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
525 [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
527 [ C(OP_PREFETCH) ] = {
528 [ C(RESULT_ACCESS) ] = 0,
529 [ C(RESULT_MISS) ] = 0,
534 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
535 [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
538 [ C(RESULT_ACCESS) ] = -1,
539 [ C(RESULT_MISS) ] = -1,
541 [ C(OP_PREFETCH) ] = {
542 [ C(RESULT_ACCESS) ] = -1,
543 [ C(RESULT_MISS) ] = -1,
548 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
549 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
552 [ C(RESULT_ACCESS) ] = -1,
553 [ C(RESULT_MISS) ] = -1,
555 [ C(OP_PREFETCH) ] = {
556 [ C(RESULT_ACCESS) ] = -1,
557 [ C(RESULT_MISS) ] = -1,
562 static u64 intel_pmu_raw_event(u64 hw_event)
564 #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
565 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
566 #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL
567 #define CORE_EVNTSEL_INV_MASK 0x00800000ULL
568 #define CORE_EVNTSEL_REG_MASK 0xFF000000ULL
570 #define CORE_EVNTSEL_MASK \
571 (INTEL_ARCH_EVTSEL_MASK | \
572 INTEL_ARCH_UNIT_MASK | \
573 INTEL_ARCH_EDGE_MASK | \
574 INTEL_ARCH_INV_MASK | \
577 return hw_event & CORE_EVNTSEL_MASK;
580 static __initconst u64 amd_hw_cache_event_ids
581 [PERF_COUNT_HW_CACHE_MAX]
582 [PERF_COUNT_HW_CACHE_OP_MAX]
583 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
587 [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
588 [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */
591 [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */
592 [ C(RESULT_MISS) ] = 0,
594 [ C(OP_PREFETCH) ] = {
595 [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
596 [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
601 [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
602 [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
605 [ C(RESULT_ACCESS) ] = -1,
606 [ C(RESULT_MISS) ] = -1,
608 [ C(OP_PREFETCH) ] = {
609 [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
610 [ C(RESULT_MISS) ] = 0,
615 [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
616 [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
619 [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
620 [ C(RESULT_MISS) ] = 0,
622 [ C(OP_PREFETCH) ] = {
623 [ C(RESULT_ACCESS) ] = 0,
624 [ C(RESULT_MISS) ] = 0,
629 [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
630 [ C(RESULT_MISS) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */
633 [ C(RESULT_ACCESS) ] = 0,
634 [ C(RESULT_MISS) ] = 0,
636 [ C(OP_PREFETCH) ] = {
637 [ C(RESULT_ACCESS) ] = 0,
638 [ C(RESULT_MISS) ] = 0,
643 [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
644 [ C(RESULT_MISS) ] = 0x0085, /* Instr. fetch ITLB misses */
647 [ C(RESULT_ACCESS) ] = -1,
648 [ C(RESULT_MISS) ] = -1,
650 [ C(OP_PREFETCH) ] = {
651 [ C(RESULT_ACCESS) ] = -1,
652 [ C(RESULT_MISS) ] = -1,
657 [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
658 [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
661 [ C(RESULT_ACCESS) ] = -1,
662 [ C(RESULT_MISS) ] = -1,
664 [ C(OP_PREFETCH) ] = {
665 [ C(RESULT_ACCESS) ] = -1,
666 [ C(RESULT_MISS) ] = -1,
672 * AMD Performance Monitor K7 and later.
674 static const u64 amd_perfmon_event_map[] =
676 [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
677 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
678 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080,
679 [PERF_COUNT_HW_CACHE_MISSES] = 0x0081,
680 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
681 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
684 static u64 amd_pmu_event_map(int hw_event)
686 return amd_perfmon_event_map[hw_event];
689 static u64 amd_pmu_raw_event(u64 hw_event)
691 #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
692 #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
693 #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL
694 #define K7_EVNTSEL_INV_MASK 0x000800000ULL
695 #define K7_EVNTSEL_REG_MASK 0x0FF000000ULL
697 #define K7_EVNTSEL_MASK \
698 (K7_EVNTSEL_EVENT_MASK | \
699 K7_EVNTSEL_UNIT_MASK | \
700 K7_EVNTSEL_EDGE_MASK | \
701 K7_EVNTSEL_INV_MASK | \
704 return hw_event & K7_EVNTSEL_MASK;
708 * Propagate event elapsed time into the generic event.
709 * Can only be executed on the CPU where the event is active.
710 * Returns the delta events processed.
713 x86_perf_event_update(struct perf_event *event,
714 struct hw_perf_event *hwc, int idx)
716 int shift = 64 - x86_pmu.event_bits;
717 u64 prev_raw_count, new_raw_count;
720 if (idx == X86_PMC_IDX_FIXED_BTS)
724 * Careful: an NMI might modify the previous event value.
726 * Our tactic to handle this is to first atomically read and
727 * exchange a new raw count - then add that new-prev delta
728 * count to the generic event atomically:
731 prev_raw_count = atomic64_read(&hwc->prev_count);
732 rdmsrl(hwc->event_base + idx, new_raw_count);
734 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
735 new_raw_count) != prev_raw_count)
739 * Now we have the new raw value and have updated the prev
740 * timestamp already. We can now calculate the elapsed delta
741 * (event-)time and add that to the generic event.
743 * Careful, not all hw sign-extends above the physical width
746 delta = (new_raw_count << shift) - (prev_raw_count << shift);
749 atomic64_add(delta, &event->count);
750 atomic64_sub(delta, &hwc->period_left);
752 return new_raw_count;
755 static atomic_t active_events;
756 static DEFINE_MUTEX(pmc_reserve_mutex);
758 static bool reserve_pmc_hardware(void)
760 #ifdef CONFIG_X86_LOCAL_APIC
763 if (nmi_watchdog == NMI_LOCAL_APIC)
764 disable_lapic_nmi_watchdog();
766 for (i = 0; i < x86_pmu.num_events; i++) {
767 if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
771 for (i = 0; i < x86_pmu.num_events; i++) {
772 if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
779 #ifdef CONFIG_X86_LOCAL_APIC
781 for (i--; i >= 0; i--)
782 release_evntsel_nmi(x86_pmu.eventsel + i);
784 i = x86_pmu.num_events;
787 for (i--; i >= 0; i--)
788 release_perfctr_nmi(x86_pmu.perfctr + i);
790 if (nmi_watchdog == NMI_LOCAL_APIC)
791 enable_lapic_nmi_watchdog();
797 static void release_pmc_hardware(void)
799 #ifdef CONFIG_X86_LOCAL_APIC
802 for (i = 0; i < x86_pmu.num_events; i++) {
803 release_perfctr_nmi(x86_pmu.perfctr + i);
804 release_evntsel_nmi(x86_pmu.eventsel + i);
807 if (nmi_watchdog == NMI_LOCAL_APIC)
808 enable_lapic_nmi_watchdog();
812 static inline bool bts_available(void)
814 return x86_pmu.enable_bts != NULL;
817 static inline void init_debug_store_on_cpu(int cpu)
819 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
824 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
825 (u32)((u64)(unsigned long)ds),
826 (u32)((u64)(unsigned long)ds >> 32));
829 static inline void fini_debug_store_on_cpu(int cpu)
831 if (!per_cpu(cpu_hw_events, cpu).ds)
834 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
837 static void release_bts_hardware(void)
841 if (!bts_available())
846 for_each_online_cpu(cpu)
847 fini_debug_store_on_cpu(cpu);
849 for_each_possible_cpu(cpu) {
850 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
855 per_cpu(cpu_hw_events, cpu).ds = NULL;
857 kfree((void *)(unsigned long)ds->bts_buffer_base);
864 static int reserve_bts_hardware(void)
868 if (!bts_available())
873 for_each_possible_cpu(cpu) {
874 struct debug_store *ds;
878 buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL);
879 if (unlikely(!buffer))
882 ds = kzalloc(sizeof(*ds), GFP_KERNEL);
888 ds->bts_buffer_base = (u64)(unsigned long)buffer;
889 ds->bts_index = ds->bts_buffer_base;
890 ds->bts_absolute_maximum =
891 ds->bts_buffer_base + BTS_BUFFER_SIZE;
892 ds->bts_interrupt_threshold =
893 ds->bts_absolute_maximum - BTS_OVFL_TH;
895 per_cpu(cpu_hw_events, cpu).ds = ds;
900 release_bts_hardware();
902 for_each_online_cpu(cpu)
903 init_debug_store_on_cpu(cpu);
911 static void hw_perf_event_destroy(struct perf_event *event)
913 if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
914 release_pmc_hardware();
915 release_bts_hardware();
916 mutex_unlock(&pmc_reserve_mutex);
920 static inline int x86_pmu_initialized(void)
922 return x86_pmu.handle_irq != NULL;
926 set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr)
928 unsigned int cache_type, cache_op, cache_result;
931 config = attr->config;
933 cache_type = (config >> 0) & 0xff;
934 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
937 cache_op = (config >> 8) & 0xff;
938 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
941 cache_result = (config >> 16) & 0xff;
942 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
945 val = hw_cache_event_ids[cache_type][cache_op][cache_result];
958 static void intel_pmu_enable_bts(u64 config)
960 unsigned long debugctlmsr;
962 debugctlmsr = get_debugctlmsr();
964 debugctlmsr |= X86_DEBUGCTL_TR;
965 debugctlmsr |= X86_DEBUGCTL_BTS;
966 debugctlmsr |= X86_DEBUGCTL_BTINT;
968 if (!(config & ARCH_PERFMON_EVENTSEL_OS))
969 debugctlmsr |= X86_DEBUGCTL_BTS_OFF_OS;
971 if (!(config & ARCH_PERFMON_EVENTSEL_USR))
972 debugctlmsr |= X86_DEBUGCTL_BTS_OFF_USR;
974 update_debugctlmsr(debugctlmsr);
977 static void intel_pmu_disable_bts(void)
979 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
980 unsigned long debugctlmsr;
985 debugctlmsr = get_debugctlmsr();
988 ~(X86_DEBUGCTL_TR | X86_DEBUGCTL_BTS | X86_DEBUGCTL_BTINT |
989 X86_DEBUGCTL_BTS_OFF_OS | X86_DEBUGCTL_BTS_OFF_USR);
991 update_debugctlmsr(debugctlmsr);
995 * Setup the hardware configuration for a given attr_type
997 static int __hw_perf_event_init(struct perf_event *event)
999 struct perf_event_attr *attr = &event->attr;
1000 struct hw_perf_event *hwc = &event->hw;
1004 if (!x86_pmu_initialized())
1008 if (!atomic_inc_not_zero(&active_events)) {
1009 mutex_lock(&pmc_reserve_mutex);
1010 if (atomic_read(&active_events) == 0) {
1011 if (!reserve_pmc_hardware())
1014 err = reserve_bts_hardware();
1017 atomic_inc(&active_events);
1018 mutex_unlock(&pmc_reserve_mutex);
1023 event->destroy = hw_perf_event_destroy;
1026 * Generate PMC IRQs:
1027 * (keep 'enabled' bit clear for now)
1029 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
1034 * Count user and OS events unless requested not to.
1036 if (!attr->exclude_user)
1037 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
1038 if (!attr->exclude_kernel)
1039 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
1041 if (!hwc->sample_period) {
1042 hwc->sample_period = x86_pmu.max_period;
1043 hwc->last_period = hwc->sample_period;
1044 atomic64_set(&hwc->period_left, hwc->sample_period);
1047 * If we have a PMU initialized but no APIC
1048 * interrupts, we cannot sample hardware
1049 * events (user-space has to fall back and
1050 * sample via a hrtimer based software event):
1057 * Raw hw_event type provide the config in the hw_event structure
1059 if (attr->type == PERF_TYPE_RAW) {
1060 hwc->config |= x86_pmu.raw_event(attr->config);
1064 if (attr->type == PERF_TYPE_HW_CACHE)
1065 return set_ext_hw_attr(hwc, attr);
1067 if (attr->config >= x86_pmu.max_events)
1073 config = x86_pmu.event_map(attr->config);
1084 if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1085 (hwc->sample_period == 1)) {
1086 /* BTS is not supported by this architecture. */
1087 if (!bts_available())
1090 /* BTS is currently only allowed for user-mode. */
1091 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
1095 hwc->config |= config;
1100 static void p6_pmu_disable_all(void)
1102 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1111 /* p6 only has one enable register */
1112 rdmsrl(MSR_P6_EVNTSEL0, val);
1113 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
1114 wrmsrl(MSR_P6_EVNTSEL0, val);
1117 static void intel_pmu_disable_all(void)
1119 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1127 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
1129 if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask))
1130 intel_pmu_disable_bts();
1133 static void amd_pmu_disable_all(void)
1135 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1143 * ensure we write the disable before we start disabling the
1144 * events proper, so that amd_pmu_enable_event() does the
1149 for (idx = 0; idx < x86_pmu.num_events; idx++) {
1152 if (!test_bit(idx, cpuc->active_mask))
1154 rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
1155 if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
1157 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
1158 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
1162 void hw_perf_disable(void)
1164 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1166 if (!x86_pmu_initialized())
1172 x86_pmu.disable_all();
1175 static void p6_pmu_enable_all(void)
1177 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1186 /* p6 only has one enable register */
1187 rdmsrl(MSR_P6_EVNTSEL0, val);
1188 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
1189 wrmsrl(MSR_P6_EVNTSEL0, val);
1192 static void intel_pmu_enable_all(void)
1194 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1202 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
1204 if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
1205 struct perf_event *event =
1206 cpuc->events[X86_PMC_IDX_FIXED_BTS];
1208 if (WARN_ON_ONCE(!event))
1211 intel_pmu_enable_bts(event->hw.config);
1215 static void amd_pmu_enable_all(void)
1217 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1226 for (idx = 0; idx < x86_pmu.num_events; idx++) {
1227 struct perf_event *event = cpuc->events[idx];
1230 if (!test_bit(idx, cpuc->active_mask))
1233 val = event->hw.config;
1234 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
1235 wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
1239 static const struct pmu pmu;
1241 static inline int is_x86_event(struct perf_event *event)
1243 return event->pmu == &pmu;
1246 static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
1248 struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
1249 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
1250 int i, j, w, wmax, num = 0;
1251 struct hw_perf_event *hwc;
1253 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
1255 for (i = 0; i < n; i++) {
1257 x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
1261 * fastpath, try to reuse previous register
1263 for (i = 0; i < n; i++) {
1264 hwc = &cpuc->event_list[i]->hw;
1267 /* never assigned */
1271 /* constraint still honored */
1272 if (!test_bit(hwc->idx, c->idxmsk))
1275 /* not already used */
1276 if (test_bit(hwc->idx, used_mask))
1279 set_bit(hwc->idx, used_mask);
1281 assign[i] = hwc->idx;
1290 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
1293 * weight = number of possible counters
1295 * 1 = most constrained, only works on one counter
1296 * wmax = least constrained, works on any counter
1298 * assign events to counters starting with most
1299 * constrained events.
1301 wmax = x86_pmu.num_events;
1304 * when fixed event counters are present,
1305 * wmax is incremented by 1 to account
1306 * for one more choice
1308 if (x86_pmu.num_events_fixed)
1311 for (w = 1, num = n; num && w <= wmax; w++) {
1312 /* for each event */
1313 for (i = 0; num && i < n; i++) {
1315 hwc = &cpuc->event_list[i]->hw;
1320 for_each_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
1321 if (!test_bit(j, used_mask))
1325 if (j == X86_PMC_IDX_MAX)
1328 set_bit(j, used_mask);
1337 * scheduling failed or is just a simulation,
1338 * free resources if necessary
1340 if (!assign || num) {
1341 for (i = 0; i < n; i++) {
1342 if (x86_pmu.put_event_constraints)
1343 x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
1346 return num ? -ENOSPC : 0;
1350 * dogrp: true if must collect siblings events (group)
1351 * returns total number of events and error code
1353 static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
1355 struct perf_event *event;
1358 max_count = x86_pmu.num_events + x86_pmu.num_events_fixed;
1360 /* current number of events already accepted */
1363 if (is_x86_event(leader)) {
1366 cpuc->event_list[n] = leader;
1372 list_for_each_entry(event, &leader->sibling_list, group_entry) {
1373 if (!is_x86_event(event) ||
1374 event->state <= PERF_EVENT_STATE_OFF)
1380 cpuc->event_list[n] = event;
1387 static inline void x86_assign_hw_event(struct perf_event *event,
1388 struct hw_perf_event *hwc, int idx)
1392 if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
1393 hwc->config_base = 0;
1394 hwc->event_base = 0;
1395 } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
1396 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
1398 * We set it so that event_base + idx in wrmsr/rdmsr maps to
1399 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
1402 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
1404 hwc->config_base = x86_pmu.eventsel;
1405 hwc->event_base = x86_pmu.perfctr;
1409 static void __x86_pmu_disable(struct perf_event *event, struct cpu_hw_events *cpuc);
1411 void hw_perf_enable(void)
1413 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1414 struct perf_event *event;
1415 struct hw_perf_event *hwc;
1418 if (!x86_pmu_initialized())
1420 if (cpuc->n_added) {
1422 * apply assignment obtained either from
1423 * hw_perf_group_sched_in() or x86_pmu_enable()
1425 * step1: save events moving to new counters
1426 * step2: reprogram moved events into new counters
1428 for (i = 0; i < cpuc->n_events; i++) {
1430 event = cpuc->event_list[i];
1433 if (hwc->idx == -1 || hwc->idx == cpuc->assign[i])
1436 __x86_pmu_disable(event, cpuc);
1441 for (i = 0; i < cpuc->n_events; i++) {
1443 event = cpuc->event_list[i];
1446 if (hwc->idx == -1) {
1447 x86_assign_hw_event(event, hwc, cpuc->assign[i]);
1448 x86_perf_event_set_period(event, hwc, hwc->idx);
1451 * need to mark as active because x86_pmu_disable()
1452 * clear active_mask and eventsp[] yet it preserves
1455 set_bit(hwc->idx, cpuc->active_mask);
1456 cpuc->events[hwc->idx] = event;
1458 x86_pmu.enable(hwc, hwc->idx);
1459 perf_event_update_userpage(event);
1462 perf_events_lapic_init();
1464 x86_pmu.enable_all();
1467 static inline u64 intel_pmu_get_status(void)
1471 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1476 static inline void intel_pmu_ack_status(u64 ack)
1478 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
1481 static inline void x86_pmu_enable_event(struct hw_perf_event *hwc, int idx)
1483 (void)checking_wrmsrl(hwc->config_base + idx,
1484 hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
1487 static inline void x86_pmu_disable_event(struct hw_perf_event *hwc, int idx)
1489 (void)checking_wrmsrl(hwc->config_base + idx, hwc->config);
1493 intel_pmu_disable_fixed(struct hw_perf_event *hwc, int __idx)
1495 int idx = __idx - X86_PMC_IDX_FIXED;
1498 mask = 0xfULL << (idx * 4);
1500 rdmsrl(hwc->config_base, ctrl_val);
1502 (void)checking_wrmsrl(hwc->config_base, ctrl_val);
1506 p6_pmu_disable_event(struct hw_perf_event *hwc, int idx)
1508 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1509 u64 val = P6_NOP_EVENT;
1512 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
1514 (void)checking_wrmsrl(hwc->config_base + idx, val);
1518 intel_pmu_disable_event(struct hw_perf_event *hwc, int idx)
1520 if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
1521 intel_pmu_disable_bts();
1525 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
1526 intel_pmu_disable_fixed(hwc, idx);
1530 x86_pmu_disable_event(hwc, idx);
1534 amd_pmu_disable_event(struct hw_perf_event *hwc, int idx)
1536 x86_pmu_disable_event(hwc, idx);
1539 static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
1542 * Set the next IRQ period, based on the hwc->period_left value.
1543 * To be called with the event disabled in hw:
1546 x86_perf_event_set_period(struct perf_event *event,
1547 struct hw_perf_event *hwc, int idx)
1549 s64 left = atomic64_read(&hwc->period_left);
1550 s64 period = hwc->sample_period;
1553 if (idx == X86_PMC_IDX_FIXED_BTS)
1557 * If we are way outside a reasonable range then just skip forward:
1559 if (unlikely(left <= -period)) {
1561 atomic64_set(&hwc->period_left, left);
1562 hwc->last_period = period;
1566 if (unlikely(left <= 0)) {
1568 atomic64_set(&hwc->period_left, left);
1569 hwc->last_period = period;
1573 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
1575 if (unlikely(left < 2))
1578 if (left > x86_pmu.max_period)
1579 left = x86_pmu.max_period;
1581 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
1584 * The hw event starts counting from this event offset,
1585 * mark it to be able to extra future deltas:
1587 atomic64_set(&hwc->prev_count, (u64)-left);
1589 err = checking_wrmsrl(hwc->event_base + idx,
1590 (u64)(-left) & x86_pmu.event_mask);
1592 perf_event_update_userpage(event);
1598 intel_pmu_enable_fixed(struct hw_perf_event *hwc, int __idx)
1600 int idx = __idx - X86_PMC_IDX_FIXED;
1601 u64 ctrl_val, bits, mask;
1605 * Enable IRQ generation (0x8),
1606 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
1610 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
1612 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
1615 mask = 0xfULL << (idx * 4);
1617 rdmsrl(hwc->config_base, ctrl_val);
1620 err = checking_wrmsrl(hwc->config_base, ctrl_val);
1623 static void p6_pmu_enable_event(struct hw_perf_event *hwc, int idx)
1625 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1630 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
1632 (void)checking_wrmsrl(hwc->config_base + idx, val);
1636 static void intel_pmu_enable_event(struct hw_perf_event *hwc, int idx)
1638 if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
1639 if (!__get_cpu_var(cpu_hw_events).enabled)
1642 intel_pmu_enable_bts(hwc->config);
1646 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
1647 intel_pmu_enable_fixed(hwc, idx);
1651 x86_pmu_enable_event(hwc, idx);
1654 static void amd_pmu_enable_event(struct hw_perf_event *hwc, int idx)
1656 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1659 x86_pmu_enable_event(hwc, idx);
1663 * activate a single event
1665 * The event is added to the group of enabled events
1666 * but only if it can be scehduled with existing events.
1668 * Called with PMU disabled. If successful and return value 1,
1669 * then guaranteed to call perf_enable() and hw_perf_enable()
1671 static int x86_pmu_enable(struct perf_event *event)
1673 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1674 struct hw_perf_event *hwc;
1675 int assign[X86_PMC_IDX_MAX];
1680 n0 = cpuc->n_events;
1681 n = collect_events(cpuc, event, false);
1685 ret = x86_schedule_events(cpuc, n, assign);
1689 * copy new assignment, now we know it is possible
1690 * will be used by hw_perf_enable()
1692 memcpy(cpuc->assign, assign, n*sizeof(int));
1695 cpuc->n_added = n - n0;
1698 x86_perf_event_set_period(event, hwc, hwc->idx);
1703 static void x86_pmu_unthrottle(struct perf_event *event)
1705 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1706 struct hw_perf_event *hwc = &event->hw;
1708 if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX ||
1709 cpuc->events[hwc->idx] != event))
1712 x86_pmu.enable(hwc, hwc->idx);
1715 void perf_event_print_debug(void)
1717 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
1718 struct cpu_hw_events *cpuc;
1719 unsigned long flags;
1722 if (!x86_pmu.num_events)
1725 local_irq_save(flags);
1727 cpu = smp_processor_id();
1728 cpuc = &per_cpu(cpu_hw_events, cpu);
1730 if (x86_pmu.version >= 2) {
1731 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
1732 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1733 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1734 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
1737 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1738 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1739 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1740 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
1742 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
1744 for (idx = 0; idx < x86_pmu.num_events; idx++) {
1745 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
1746 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
1748 prev_left = per_cpu(pmc_prev_left[idx], cpu);
1750 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
1751 cpu, idx, pmc_ctrl);
1752 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
1753 cpu, idx, pmc_count);
1754 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
1755 cpu, idx, prev_left);
1757 for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
1758 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
1760 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
1761 cpu, idx, pmc_count);
1763 local_irq_restore(flags);
1766 static void intel_pmu_drain_bts_buffer(struct cpu_hw_events *cpuc)
1768 struct debug_store *ds = cpuc->ds;
1774 struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS];
1775 struct bts_record *at, *top;
1776 struct perf_output_handle handle;
1777 struct perf_event_header header;
1778 struct perf_sample_data data;
1779 struct pt_regs regs;
1787 at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
1788 top = (struct bts_record *)(unsigned long)ds->bts_index;
1793 ds->bts_index = ds->bts_buffer_base;
1796 data.period = event->hw.last_period;
1802 * Prepare a generic sample, i.e. fill in the invariant fields.
1803 * We will overwrite the from and to address before we output
1806 perf_prepare_sample(&header, &data, event, ®s);
1808 if (perf_output_begin(&handle, event,
1809 header.size * (top - at), 1, 1))
1812 for (; at < top; at++) {
1816 perf_output_sample(&handle, &header, &data, event);
1819 perf_output_end(&handle);
1821 /* There's new data available. */
1822 event->hw.interrupts++;
1823 event->pending_kill = POLL_IN;
1826 static void __x86_pmu_disable(struct perf_event *event, struct cpu_hw_events *cpuc)
1828 struct hw_perf_event *hwc = &event->hw;
1832 * Must be done before we disable, otherwise the nmi handler
1833 * could reenable again:
1835 clear_bit(idx, cpuc->active_mask);
1836 x86_pmu.disable(hwc, idx);
1839 * Drain the remaining delta count out of a event
1840 * that we are disabling:
1842 x86_perf_event_update(event, hwc, idx);
1844 /* Drain the remaining BTS records. */
1845 if (unlikely(idx == X86_PMC_IDX_FIXED_BTS))
1846 intel_pmu_drain_bts_buffer(cpuc);
1848 cpuc->events[idx] = NULL;
1851 static void x86_pmu_disable(struct perf_event *event)
1853 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1856 __x86_pmu_disable(event, cpuc);
1858 for (i = 0; i < cpuc->n_events; i++) {
1859 if (event == cpuc->event_list[i]) {
1861 if (x86_pmu.put_event_constraints)
1862 x86_pmu.put_event_constraints(cpuc, event);
1864 while (++i < cpuc->n_events)
1865 cpuc->event_list[i-1] = cpuc->event_list[i];
1871 perf_event_update_userpage(event);
1875 * Save and restart an expired event. Called by NMI contexts,
1876 * so it has to be careful about preempting normal event ops:
1878 static int intel_pmu_save_and_restart(struct perf_event *event)
1880 struct hw_perf_event *hwc = &event->hw;
1884 x86_perf_event_update(event, hwc, idx);
1885 ret = x86_perf_event_set_period(event, hwc, idx);
1887 if (event->state == PERF_EVENT_STATE_ACTIVE)
1888 intel_pmu_enable_event(hwc, idx);
1893 static void intel_pmu_reset(void)
1895 struct debug_store *ds = __get_cpu_var(cpu_hw_events).ds;
1896 unsigned long flags;
1899 if (!x86_pmu.num_events)
1902 local_irq_save(flags);
1904 printk("clearing PMU state on CPU#%d\n", smp_processor_id());
1906 for (idx = 0; idx < x86_pmu.num_events; idx++) {
1907 checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
1908 checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
1910 for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
1911 checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
1914 ds->bts_index = ds->bts_buffer_base;
1916 local_irq_restore(flags);
1919 static int p6_pmu_handle_irq(struct pt_regs *regs)
1921 struct perf_sample_data data;
1922 struct cpu_hw_events *cpuc;
1923 struct perf_event *event;
1924 struct hw_perf_event *hwc;
1925 int idx, handled = 0;
1931 cpuc = &__get_cpu_var(cpu_hw_events);
1933 for (idx = 0; idx < x86_pmu.num_events; idx++) {
1934 if (!test_bit(idx, cpuc->active_mask))
1937 event = cpuc->events[idx];
1940 val = x86_perf_event_update(event, hwc, idx);
1941 if (val & (1ULL << (x86_pmu.event_bits - 1)))
1948 data.period = event->hw.last_period;
1950 if (!x86_perf_event_set_period(event, hwc, idx))
1953 if (perf_event_overflow(event, 1, &data, regs))
1954 p6_pmu_disable_event(hwc, idx);
1958 inc_irq_stat(apic_perf_irqs);
1964 * This handler is triggered by the local APIC, so the APIC IRQ handling
1967 static int intel_pmu_handle_irq(struct pt_regs *regs)
1969 struct perf_sample_data data;
1970 struct cpu_hw_events *cpuc;
1977 cpuc = &__get_cpu_var(cpu_hw_events);
1980 intel_pmu_drain_bts_buffer(cpuc);
1981 status = intel_pmu_get_status();
1989 if (++loops > 100) {
1990 WARN_ONCE(1, "perfevents: irq loop stuck!\n");
1991 perf_event_print_debug();
1997 inc_irq_stat(apic_perf_irqs);
1999 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
2000 struct perf_event *event = cpuc->events[bit];
2002 clear_bit(bit, (unsigned long *) &status);
2003 if (!test_bit(bit, cpuc->active_mask))
2006 if (!intel_pmu_save_and_restart(event))
2009 data.period = event->hw.last_period;
2011 if (perf_event_overflow(event, 1, &data, regs))
2012 intel_pmu_disable_event(&event->hw, bit);
2015 intel_pmu_ack_status(ack);
2018 * Repeat if there is more work to be done:
2020 status = intel_pmu_get_status();
2029 static int amd_pmu_handle_irq(struct pt_regs *regs)
2031 struct perf_sample_data data;
2032 struct cpu_hw_events *cpuc;
2033 struct perf_event *event;
2034 struct hw_perf_event *hwc;
2035 int idx, handled = 0;
2041 cpuc = &__get_cpu_var(cpu_hw_events);
2043 for (idx = 0; idx < x86_pmu.num_events; idx++) {
2044 if (!test_bit(idx, cpuc->active_mask))
2047 event = cpuc->events[idx];
2050 val = x86_perf_event_update(event, hwc, idx);
2051 if (val & (1ULL << (x86_pmu.event_bits - 1)))
2058 data.period = event->hw.last_period;
2060 if (!x86_perf_event_set_period(event, hwc, idx))
2063 if (perf_event_overflow(event, 1, &data, regs))
2064 amd_pmu_disable_event(hwc, idx);
2068 inc_irq_stat(apic_perf_irqs);
2073 void smp_perf_pending_interrupt(struct pt_regs *regs)
2077 inc_irq_stat(apic_pending_irqs);
2078 perf_event_do_pending();
2082 void set_perf_event_pending(void)
2084 #ifdef CONFIG_X86_LOCAL_APIC
2085 if (!x86_pmu.apic || !x86_pmu_initialized())
2088 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
2092 void perf_events_lapic_init(void)
2094 #ifdef CONFIG_X86_LOCAL_APIC
2095 if (!x86_pmu.apic || !x86_pmu_initialized())
2099 * Always use NMI for PMU
2101 apic_write(APIC_LVTPC, APIC_DM_NMI);
2105 static int __kprobes
2106 perf_event_nmi_handler(struct notifier_block *self,
2107 unsigned long cmd, void *__args)
2109 struct die_args *args = __args;
2110 struct pt_regs *regs;
2112 if (!atomic_read(&active_events))
2126 #ifdef CONFIG_X86_LOCAL_APIC
2127 apic_write(APIC_LVTPC, APIC_DM_NMI);
2130 * Can't rely on the handled return value to say it was our NMI, two
2131 * events could trigger 'simultaneously' raising two back-to-back NMIs.
2133 * If the first NMI handles both, the latter will be empty and daze
2136 x86_pmu.handle_irq(regs);
2141 static struct event_constraint unconstrained;
2143 static struct event_constraint bts_constraint =
2144 EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS, 0);
2146 static struct event_constraint *
2147 intel_special_constraints(struct perf_event *event)
2149 unsigned int hw_event;
2151 hw_event = event->hw.config & INTEL_ARCH_EVENT_MASK;
2153 if (unlikely((hw_event ==
2154 x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) &&
2155 (event->hw.sample_period == 1))) {
2157 return &bts_constraint;
2162 static struct event_constraint *
2163 intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
2165 struct event_constraint *c;
2167 c = intel_special_constraints(event);
2171 if (x86_pmu.event_constraints) {
2172 for_each_event_constraint(c, x86_pmu.event_constraints) {
2173 if ((event->hw.config & c->cmask) == c->code)
2178 return &unconstrained;
2181 static struct event_constraint *
2182 amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
2184 return &unconstrained;
2187 static int x86_event_sched_in(struct perf_event *event,
2188 struct perf_cpu_context *cpuctx, int cpu)
2192 event->state = PERF_EVENT_STATE_ACTIVE;
2194 event->tstamp_running += event->ctx->time - event->tstamp_stopped;
2196 if (!is_x86_event(event))
2197 ret = event->pmu->enable(event);
2199 if (!ret && !is_software_event(event))
2200 cpuctx->active_oncpu++;
2202 if (!ret && event->attr.exclusive)
2203 cpuctx->exclusive = 1;
2208 static void x86_event_sched_out(struct perf_event *event,
2209 struct perf_cpu_context *cpuctx, int cpu)
2211 event->state = PERF_EVENT_STATE_INACTIVE;
2214 if (!is_x86_event(event))
2215 event->pmu->disable(event);
2217 event->tstamp_running -= event->ctx->time - event->tstamp_stopped;
2219 if (!is_software_event(event))
2220 cpuctx->active_oncpu--;
2222 if (event->attr.exclusive || !cpuctx->active_oncpu)
2223 cpuctx->exclusive = 0;
2227 * Called to enable a whole group of events.
2228 * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
2229 * Assumes the caller has disabled interrupts and has
2230 * frozen the PMU with hw_perf_save_disable.
2232 * called with PMU disabled. If successful and return value 1,
2233 * then guaranteed to call perf_enable() and hw_perf_enable()
2235 int hw_perf_group_sched_in(struct perf_event *leader,
2236 struct perf_cpu_context *cpuctx,
2237 struct perf_event_context *ctx, int cpu)
2239 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
2240 struct perf_event *sub;
2241 int assign[X86_PMC_IDX_MAX];
2244 /* n0 = total number of events */
2245 n0 = collect_events(cpuc, leader, true);
2249 ret = x86_schedule_events(cpuc, n0, assign);
2253 ret = x86_event_sched_in(leader, cpuctx, cpu);
2258 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2259 if (sub->state > PERF_EVENT_STATE_OFF) {
2260 ret = x86_event_sched_in(sub, cpuctx, cpu);
2267 * copy new assignment, now we know it is possible
2268 * will be used by hw_perf_enable()
2270 memcpy(cpuc->assign, assign, n0*sizeof(int));
2272 cpuc->n_events = n0;
2274 ctx->nr_active += n1;
2277 * 1 means successful and events are active
2278 * This is not quite true because we defer
2279 * actual activation until hw_perf_enable() but
2280 * this way we* ensure caller won't try to enable
2285 x86_event_sched_out(leader, cpuctx, cpu);
2287 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2288 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
2289 x86_event_sched_out(sub, cpuctx, cpu);
2297 static __read_mostly struct notifier_block perf_event_nmi_notifier = {
2298 .notifier_call = perf_event_nmi_handler,
2303 static __initconst struct x86_pmu p6_pmu = {
2305 .handle_irq = p6_pmu_handle_irq,
2306 .disable_all = p6_pmu_disable_all,
2307 .enable_all = p6_pmu_enable_all,
2308 .enable = p6_pmu_enable_event,
2309 .disable = p6_pmu_disable_event,
2310 .eventsel = MSR_P6_EVNTSEL0,
2311 .perfctr = MSR_P6_PERFCTR0,
2312 .event_map = p6_pmu_event_map,
2313 .raw_event = p6_pmu_raw_event,
2314 .max_events = ARRAY_SIZE(p6_perfmon_event_map),
2316 .max_period = (1ULL << 31) - 1,
2320 * Events have 40 bits implemented. However they are designed such
2321 * that bits [32-39] are sign extensions of bit 31. As such the
2322 * effective width of a event for P6-like PMU is 32 bits only.
2324 * See IA-32 Intel Architecture Software developer manual Vol 3B
2327 .event_mask = (1ULL << 32) - 1,
2328 .get_event_constraints = intel_get_event_constraints,
2329 .event_constraints = intel_p6_event_constraints
2332 static __initconst struct x86_pmu intel_pmu = {
2334 .handle_irq = intel_pmu_handle_irq,
2335 .disable_all = intel_pmu_disable_all,
2336 .enable_all = intel_pmu_enable_all,
2337 .enable = intel_pmu_enable_event,
2338 .disable = intel_pmu_disable_event,
2339 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
2340 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
2341 .event_map = intel_pmu_event_map,
2342 .raw_event = intel_pmu_raw_event,
2343 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
2346 * Intel PMCs cannot be accessed sanely above 32 bit width,
2347 * so we install an artificial 1<<31 period regardless of
2348 * the generic event period:
2350 .max_period = (1ULL << 31) - 1,
2351 .enable_bts = intel_pmu_enable_bts,
2352 .disable_bts = intel_pmu_disable_bts,
2353 .get_event_constraints = intel_get_event_constraints
2356 static __initconst struct x86_pmu amd_pmu = {
2358 .handle_irq = amd_pmu_handle_irq,
2359 .disable_all = amd_pmu_disable_all,
2360 .enable_all = amd_pmu_enable_all,
2361 .enable = amd_pmu_enable_event,
2362 .disable = amd_pmu_disable_event,
2363 .eventsel = MSR_K7_EVNTSEL0,
2364 .perfctr = MSR_K7_PERFCTR0,
2365 .event_map = amd_pmu_event_map,
2366 .raw_event = amd_pmu_raw_event,
2367 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
2370 .event_mask = (1ULL << 48) - 1,
2372 /* use highest bit to detect overflow */
2373 .max_period = (1ULL << 47) - 1,
2374 .get_event_constraints = amd_get_event_constraints
2377 static __init int p6_pmu_init(void)
2379 switch (boot_cpu_data.x86_model) {
2381 case 3: /* Pentium Pro */
2383 case 6: /* Pentium II */
2386 case 11: /* Pentium III */
2392 pr_cont("unsupported p6 CPU model %d ",
2393 boot_cpu_data.x86_model);
2402 static __init int intel_pmu_init(void)
2404 union cpuid10_edx edx;
2405 union cpuid10_eax eax;
2406 unsigned int unused;
2410 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
2411 /* check for P6 processor family */
2412 if (boot_cpu_data.x86 == 6) {
2413 return p6_pmu_init();
2420 * Check whether the Architectural PerfMon supports
2421 * Branch Misses Retired hw_event or not.
2423 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
2424 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
2427 version = eax.split.version_id;
2431 x86_pmu = intel_pmu;
2432 x86_pmu.version = version;
2433 x86_pmu.num_events = eax.split.num_events;
2434 x86_pmu.event_bits = eax.split.bit_width;
2435 x86_pmu.event_mask = (1ULL << eax.split.bit_width) - 1;
2438 * Quirk: v2 perfmon does not report fixed-purpose events, so
2439 * assume at least 3 events:
2441 x86_pmu.num_events_fixed = max((int)edx.split.num_events_fixed, 3);
2444 * Install the hw-cache-events table:
2446 switch (boot_cpu_data.x86_model) {
2447 case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
2448 case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
2449 case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
2450 case 29: /* six-core 45 nm xeon "Dunnington" */
2451 memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
2452 sizeof(hw_cache_event_ids));
2454 x86_pmu.event_constraints = intel_core_event_constraints;
2455 pr_cont("Core2 events, ");
2458 memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
2459 sizeof(hw_cache_event_ids));
2461 x86_pmu.event_constraints = intel_nehalem_event_constraints;
2462 pr_cont("Nehalem/Corei7 events, ");
2465 memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
2466 sizeof(hw_cache_event_ids));
2468 x86_pmu.event_constraints = intel_gen_event_constraints;
2469 pr_cont("Atom events, ");
2473 * default constraints for v2 and up
2475 x86_pmu.event_constraints = intel_gen_event_constraints;
2476 pr_cont("generic architected perfmon, ");
2481 static __init int amd_pmu_init(void)
2483 /* Performance-monitoring supported from K7 and later: */
2484 if (boot_cpu_data.x86 < 6)
2489 /* Events are common for all AMDs */
2490 memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
2491 sizeof(hw_cache_event_ids));
2496 static void __init pmu_check_apic(void)
2502 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
2503 pr_info("no hardware sampling interrupt available.\n");
2506 void __init init_hw_perf_events(void)
2510 pr_info("Performance Events: ");
2512 switch (boot_cpu_data.x86_vendor) {
2513 case X86_VENDOR_INTEL:
2514 err = intel_pmu_init();
2516 case X86_VENDOR_AMD:
2517 err = amd_pmu_init();
2523 pr_cont("no PMU driver, software events only.\n");
2529 pr_cont("%s PMU driver.\n", x86_pmu.name);
2531 if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) {
2532 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
2533 x86_pmu.num_events, X86_PMC_MAX_GENERIC);
2534 x86_pmu.num_events = X86_PMC_MAX_GENERIC;
2536 perf_event_mask = (1 << x86_pmu.num_events) - 1;
2537 perf_max_events = x86_pmu.num_events;
2539 if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) {
2540 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
2541 x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED);
2542 x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED;
2546 ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED;
2547 x86_pmu.intel_ctrl = perf_event_mask;
2549 perf_events_lapic_init();
2550 register_die_notifier(&perf_event_nmi_notifier);
2552 unconstrained = (struct event_constraint)
2553 EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_events) - 1, 0);
2555 pr_info("... version: %d\n", x86_pmu.version);
2556 pr_info("... bit width: %d\n", x86_pmu.event_bits);
2557 pr_info("... generic registers: %d\n", x86_pmu.num_events);
2558 pr_info("... value mask: %016Lx\n", x86_pmu.event_mask);
2559 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
2560 pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed);
2561 pr_info("... event mask: %016Lx\n", perf_event_mask);
2564 static inline void x86_pmu_read(struct perf_event *event)
2566 x86_perf_event_update(event, &event->hw, event->hw.idx);
2569 static const struct pmu pmu = {
2570 .enable = x86_pmu_enable,
2571 .disable = x86_pmu_disable,
2572 .read = x86_pmu_read,
2573 .unthrottle = x86_pmu_unthrottle,
2577 * validate a single event group
2579 * validation include:
2580 * - check events are compatible which each other
2581 * - events do not compete for the same counter
2582 * - number of events <= number of counters
2584 * validation ensures the group can be loaded onto the
2585 * PMU if it was the only group available.
2587 static int validate_group(struct perf_event *event)
2589 struct perf_event *leader = event->group_leader;
2590 struct cpu_hw_events *fake_cpuc;
2594 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
2599 * the event is not yet connected with its
2600 * siblings therefore we must first collect
2601 * existing siblings, then add the new event
2602 * before we can simulate the scheduling
2605 n = collect_events(fake_cpuc, leader, true);
2609 fake_cpuc->n_events = n;
2610 n = collect_events(fake_cpuc, event, false);
2614 fake_cpuc->n_events = n;
2616 ret = x86_schedule_events(fake_cpuc, n, NULL);
2624 const struct pmu *hw_perf_event_init(struct perf_event *event)
2626 const struct pmu *tmp;
2629 err = __hw_perf_event_init(event);
2632 * we temporarily connect event to its pmu
2633 * such that validate_group() can classify
2634 * it as an x86 event using is_x86_event()
2639 if (event->group_leader != event)
2640 err = validate_group(event);
2646 event->destroy(event);
2647 return ERR_PTR(err);
2658 void callchain_store(struct perf_callchain_entry *entry, u64 ip)
2660 if (entry->nr < PERF_MAX_STACK_DEPTH)
2661 entry->ip[entry->nr++] = ip;
2664 static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
2665 static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry);
2669 backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
2671 /* Ignore warnings */
2674 static void backtrace_warning(void *data, char *msg)
2676 /* Ignore warnings */
2679 static int backtrace_stack(void *data, char *name)
2684 static void backtrace_address(void *data, unsigned long addr, int reliable)
2686 struct perf_callchain_entry *entry = data;
2689 callchain_store(entry, addr);
2692 static const struct stacktrace_ops backtrace_ops = {
2693 .warning = backtrace_warning,
2694 .warning_symbol = backtrace_warning_symbol,
2695 .stack = backtrace_stack,
2696 .address = backtrace_address,
2697 .walk_stack = print_context_stack_bp,
2700 #include "../dumpstack.h"
2703 perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
2705 callchain_store(entry, PERF_CONTEXT_KERNEL);
2706 callchain_store(entry, regs->ip);
2708 dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
2712 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
2714 static unsigned long
2715 copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
2717 unsigned long offset, addr = (unsigned long)from;
2718 int type = in_nmi() ? KM_NMI : KM_IRQ0;
2719 unsigned long size, len = 0;
2725 ret = __get_user_pages_fast(addr, 1, 0, &page);
2729 offset = addr & (PAGE_SIZE - 1);
2730 size = min(PAGE_SIZE - offset, n - len);
2732 map = kmap_atomic(page, type);
2733 memcpy(to, map+offset, size);
2734 kunmap_atomic(map, type);
2746 static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
2748 unsigned long bytes;
2750 bytes = copy_from_user_nmi(frame, fp, sizeof(*frame));
2752 return bytes == sizeof(*frame);
2756 perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
2758 struct stack_frame frame;
2759 const void __user *fp;
2761 if (!user_mode(regs))
2762 regs = task_pt_regs(current);
2764 fp = (void __user *)regs->bp;
2766 callchain_store(entry, PERF_CONTEXT_USER);
2767 callchain_store(entry, regs->ip);
2769 while (entry->nr < PERF_MAX_STACK_DEPTH) {
2770 frame.next_frame = NULL;
2771 frame.return_address = 0;
2773 if (!copy_stack_frame(fp, &frame))
2776 if ((unsigned long)fp < regs->sp)
2779 callchain_store(entry, frame.return_address);
2780 fp = frame.next_frame;
2785 perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
2792 is_user = user_mode(regs);
2794 if (is_user && current->state != TASK_RUNNING)
2798 perf_callchain_kernel(regs, entry);
2801 perf_callchain_user(regs, entry);
2804 struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2806 struct perf_callchain_entry *entry;
2809 entry = &__get_cpu_var(pmc_nmi_entry);
2811 entry = &__get_cpu_var(pmc_irq_entry);
2815 perf_do_callchain(regs, entry);
2820 void hw_perf_event_setup_online(int cpu)
2822 init_debug_store_on_cpu(cpu);