2 * Performance events x86 architecture code
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
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 u64 tags[X86_PMC_IDX_MAX];
94 struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
97 #define __EVENT_CONSTRAINT(c, n, m, w) {\
98 { .idxmsk64[0] = (n) }, \
104 #define EVENT_CONSTRAINT(c, n, m) \
105 __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
107 #define INTEL_EVENT_CONSTRAINT(c, n) \
108 EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVTSEL_MASK)
110 #define FIXED_EVENT_CONSTRAINT(c, n) \
111 EVENT_CONSTRAINT(c, n, INTEL_ARCH_FIXED_MASK)
113 #define EVENT_CONSTRAINT_END \
114 EVENT_CONSTRAINT(0, 0, 0)
116 #define for_each_event_constraint(e, c) \
117 for ((e) = (c); (e)->cmask; (e)++)
120 * struct x86_pmu - generic x86 pmu
125 int (*handle_irq)(struct pt_regs *);
126 void (*disable_all)(void);
127 void (*enable_all)(void);
128 void (*enable)(struct hw_perf_event *, int);
129 void (*disable)(struct hw_perf_event *, int);
132 u64 (*event_map)(int);
133 u64 (*raw_event)(u64);
136 int num_events_fixed;
142 void (*enable_bts)(u64 config);
143 void (*disable_bts)(void);
145 struct event_constraint *
146 (*get_event_constraints)(struct cpu_hw_events *cpuc,
147 struct perf_event *event);
149 void (*put_event_constraints)(struct cpu_hw_events *cpuc,
150 struct perf_event *event);
151 struct event_constraint *event_constraints;
154 static struct x86_pmu x86_pmu __read_mostly;
156 static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
160 static int x86_perf_event_set_period(struct perf_event *event,
161 struct hw_perf_event *hwc, int idx);
164 * Not sure about some of these
166 static const u64 p6_perfmon_event_map[] =
168 [PERF_COUNT_HW_CPU_CYCLES] = 0x0079,
169 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
170 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0f2e,
171 [PERF_COUNT_HW_CACHE_MISSES] = 0x012e,
172 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
173 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
174 [PERF_COUNT_HW_BUS_CYCLES] = 0x0062,
177 static u64 p6_pmu_event_map(int hw_event)
179 return p6_perfmon_event_map[hw_event];
183 * Event setting that is specified not to count anything.
184 * We use this to effectively disable a counter.
186 * L2_RQSTS with 0 MESI unit mask.
188 #define P6_NOP_EVENT 0x0000002EULL
190 static u64 p6_pmu_raw_event(u64 hw_event)
192 #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL
193 #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL
194 #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL
195 #define P6_EVNTSEL_INV_MASK 0x00800000ULL
196 #define P6_EVNTSEL_REG_MASK 0xFF000000ULL
198 #define P6_EVNTSEL_MASK \
199 (P6_EVNTSEL_EVENT_MASK | \
200 P6_EVNTSEL_UNIT_MASK | \
201 P6_EVNTSEL_EDGE_MASK | \
202 P6_EVNTSEL_INV_MASK | \
205 return hw_event & P6_EVNTSEL_MASK;
208 static struct event_constraint intel_p6_event_constraints[] =
210 INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FLOPS */
211 INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
212 INTEL_EVENT_CONSTRAINT(0x11, 0x1), /* FP_ASSIST */
213 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
214 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
215 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
220 * Intel PerfMon v3. Used on Core2 and later.
222 static const u64 intel_perfmon_event_map[] =
224 [PERF_COUNT_HW_CPU_CYCLES] = 0x003c,
225 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
226 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e,
227 [PERF_COUNT_HW_CACHE_MISSES] = 0x412e,
228 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
229 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
230 [PERF_COUNT_HW_BUS_CYCLES] = 0x013c,
233 static struct event_constraint intel_core_event_constraints[] =
235 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
236 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
237 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
238 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
239 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
240 INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
244 static struct event_constraint intel_core2_event_constraints[] =
246 FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
247 FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
248 INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
249 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
250 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
251 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
252 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
253 INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
254 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
255 INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
256 INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
260 static struct event_constraint intel_nehalem_event_constraints[] =
262 FIXED_EVENT_CONSTRAINT(0xc0, (0xf|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
263 FIXED_EVENT_CONSTRAINT(0x3c, (0xf|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
264 INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
265 INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
266 INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
267 INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
268 INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
269 INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
270 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
271 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
275 static struct event_constraint intel_westmere_event_constraints[] =
277 FIXED_EVENT_CONSTRAINT(0xc0, (0xf|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
278 FIXED_EVENT_CONSTRAINT(0x3c, (0xf|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
279 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
280 INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
281 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
285 static struct event_constraint intel_gen_event_constraints[] =
287 FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
288 FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
292 static u64 intel_pmu_event_map(int hw_event)
294 return intel_perfmon_event_map[hw_event];
298 * Generalized hw caching related hw_event table, filled
299 * in on a per model basis. A value of 0 means
300 * 'not supported', -1 means 'hw_event makes no sense on
301 * this CPU', any other value means the raw hw_event
305 #define C(x) PERF_COUNT_HW_CACHE_##x
307 static u64 __read_mostly hw_cache_event_ids
308 [PERF_COUNT_HW_CACHE_MAX]
309 [PERF_COUNT_HW_CACHE_OP_MAX]
310 [PERF_COUNT_HW_CACHE_RESULT_MAX];
312 static __initconst u64 westmere_hw_cache_event_ids
313 [PERF_COUNT_HW_CACHE_MAX]
314 [PERF_COUNT_HW_CACHE_OP_MAX]
315 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
319 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
320 [ C(RESULT_MISS) ] = 0x0151, /* L1D.REPL */
323 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
324 [ C(RESULT_MISS) ] = 0x0251, /* L1D.M_REPL */
326 [ C(OP_PREFETCH) ] = {
327 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
328 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
333 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
334 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
337 [ C(RESULT_ACCESS) ] = -1,
338 [ C(RESULT_MISS) ] = -1,
340 [ C(OP_PREFETCH) ] = {
341 [ C(RESULT_ACCESS) ] = 0x0,
342 [ C(RESULT_MISS) ] = 0x0,
347 [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */
348 [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */
351 [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */
352 [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
354 [ C(OP_PREFETCH) ] = {
355 [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */
356 [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */
361 [ C(RESULT_ACCESS) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
362 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
365 [ C(RESULT_ACCESS) ] = 0x020b, /* MEM_INST_RETURED.STORES */
366 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
368 [ C(OP_PREFETCH) ] = {
369 [ C(RESULT_ACCESS) ] = 0x0,
370 [ C(RESULT_MISS) ] = 0x0,
375 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
376 [ C(RESULT_MISS) ] = 0x0185, /* ITLB_MISSES.ANY */
379 [ C(RESULT_ACCESS) ] = -1,
380 [ C(RESULT_MISS) ] = -1,
382 [ C(OP_PREFETCH) ] = {
383 [ C(RESULT_ACCESS) ] = -1,
384 [ C(RESULT_MISS) ] = -1,
389 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
390 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
393 [ C(RESULT_ACCESS) ] = -1,
394 [ C(RESULT_MISS) ] = -1,
396 [ C(OP_PREFETCH) ] = {
397 [ C(RESULT_ACCESS) ] = -1,
398 [ C(RESULT_MISS) ] = -1,
403 static __initconst u64 nehalem_hw_cache_event_ids
404 [PERF_COUNT_HW_CACHE_MAX]
405 [PERF_COUNT_HW_CACHE_OP_MAX]
406 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
410 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
411 [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
414 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
415 [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
417 [ C(OP_PREFETCH) ] = {
418 [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
419 [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */
424 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
425 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
428 [ C(RESULT_ACCESS) ] = -1,
429 [ C(RESULT_MISS) ] = -1,
431 [ C(OP_PREFETCH) ] = {
432 [ C(RESULT_ACCESS) ] = 0x0,
433 [ C(RESULT_MISS) ] = 0x0,
438 [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */
439 [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */
442 [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */
443 [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
445 [ C(OP_PREFETCH) ] = {
446 [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */
447 [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */
452 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
453 [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
456 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
457 [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
459 [ C(OP_PREFETCH) ] = {
460 [ C(RESULT_ACCESS) ] = 0x0,
461 [ C(RESULT_MISS) ] = 0x0,
466 [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */
467 [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */
470 [ C(RESULT_ACCESS) ] = -1,
471 [ C(RESULT_MISS) ] = -1,
473 [ C(OP_PREFETCH) ] = {
474 [ C(RESULT_ACCESS) ] = -1,
475 [ C(RESULT_MISS) ] = -1,
480 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
481 [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */
484 [ C(RESULT_ACCESS) ] = -1,
485 [ C(RESULT_MISS) ] = -1,
487 [ C(OP_PREFETCH) ] = {
488 [ C(RESULT_ACCESS) ] = -1,
489 [ C(RESULT_MISS) ] = -1,
494 static __initconst u64 core2_hw_cache_event_ids
495 [PERF_COUNT_HW_CACHE_MAX]
496 [PERF_COUNT_HW_CACHE_OP_MAX]
497 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
501 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
502 [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
505 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
506 [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
508 [ C(OP_PREFETCH) ] = {
509 [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
510 [ C(RESULT_MISS) ] = 0,
515 [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */
516 [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */
519 [ C(RESULT_ACCESS) ] = -1,
520 [ C(RESULT_MISS) ] = -1,
522 [ C(OP_PREFETCH) ] = {
523 [ C(RESULT_ACCESS) ] = 0,
524 [ C(RESULT_MISS) ] = 0,
529 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
530 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
533 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
534 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
536 [ C(OP_PREFETCH) ] = {
537 [ C(RESULT_ACCESS) ] = 0,
538 [ C(RESULT_MISS) ] = 0,
543 [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
544 [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
547 [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
548 [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
550 [ C(OP_PREFETCH) ] = {
551 [ C(RESULT_ACCESS) ] = 0,
552 [ C(RESULT_MISS) ] = 0,
557 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
558 [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */
561 [ C(RESULT_ACCESS) ] = -1,
562 [ C(RESULT_MISS) ] = -1,
564 [ C(OP_PREFETCH) ] = {
565 [ C(RESULT_ACCESS) ] = -1,
566 [ C(RESULT_MISS) ] = -1,
571 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
572 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
575 [ C(RESULT_ACCESS) ] = -1,
576 [ C(RESULT_MISS) ] = -1,
578 [ C(OP_PREFETCH) ] = {
579 [ C(RESULT_ACCESS) ] = -1,
580 [ C(RESULT_MISS) ] = -1,
585 static __initconst u64 atom_hw_cache_event_ids
586 [PERF_COUNT_HW_CACHE_MAX]
587 [PERF_COUNT_HW_CACHE_OP_MAX]
588 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
592 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */
593 [ C(RESULT_MISS) ] = 0,
596 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */
597 [ C(RESULT_MISS) ] = 0,
599 [ C(OP_PREFETCH) ] = {
600 [ C(RESULT_ACCESS) ] = 0x0,
601 [ C(RESULT_MISS) ] = 0,
606 [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */
607 [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */
610 [ C(RESULT_ACCESS) ] = -1,
611 [ C(RESULT_MISS) ] = -1,
613 [ C(OP_PREFETCH) ] = {
614 [ C(RESULT_ACCESS) ] = 0,
615 [ C(RESULT_MISS) ] = 0,
620 [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */
621 [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */
624 [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */
625 [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */
627 [ C(OP_PREFETCH) ] = {
628 [ C(RESULT_ACCESS) ] = 0,
629 [ C(RESULT_MISS) ] = 0,
634 [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
635 [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
638 [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
639 [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
641 [ C(OP_PREFETCH) ] = {
642 [ C(RESULT_ACCESS) ] = 0,
643 [ C(RESULT_MISS) ] = 0,
648 [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */
649 [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */
652 [ C(RESULT_ACCESS) ] = -1,
653 [ C(RESULT_MISS) ] = -1,
655 [ C(OP_PREFETCH) ] = {
656 [ C(RESULT_ACCESS) ] = -1,
657 [ C(RESULT_MISS) ] = -1,
662 [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
663 [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
666 [ C(RESULT_ACCESS) ] = -1,
667 [ C(RESULT_MISS) ] = -1,
669 [ C(OP_PREFETCH) ] = {
670 [ C(RESULT_ACCESS) ] = -1,
671 [ C(RESULT_MISS) ] = -1,
676 static u64 intel_pmu_raw_event(u64 hw_event)
678 #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
679 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
680 #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL
681 #define CORE_EVNTSEL_INV_MASK 0x00800000ULL
682 #define CORE_EVNTSEL_REG_MASK 0xFF000000ULL
684 #define CORE_EVNTSEL_MASK \
685 (INTEL_ARCH_EVTSEL_MASK | \
686 INTEL_ARCH_UNIT_MASK | \
687 INTEL_ARCH_EDGE_MASK | \
688 INTEL_ARCH_INV_MASK | \
691 return hw_event & CORE_EVNTSEL_MASK;
694 static __initconst u64 amd_hw_cache_event_ids
695 [PERF_COUNT_HW_CACHE_MAX]
696 [PERF_COUNT_HW_CACHE_OP_MAX]
697 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
701 [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
702 [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */
705 [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */
706 [ C(RESULT_MISS) ] = 0,
708 [ C(OP_PREFETCH) ] = {
709 [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */
710 [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */
715 [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */
716 [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */
719 [ C(RESULT_ACCESS) ] = -1,
720 [ C(RESULT_MISS) ] = -1,
722 [ C(OP_PREFETCH) ] = {
723 [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */
724 [ C(RESULT_MISS) ] = 0,
729 [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */
730 [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */
733 [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */
734 [ C(RESULT_MISS) ] = 0,
736 [ C(OP_PREFETCH) ] = {
737 [ C(RESULT_ACCESS) ] = 0,
738 [ C(RESULT_MISS) ] = 0,
743 [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */
744 [ C(RESULT_MISS) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */
747 [ C(RESULT_ACCESS) ] = 0,
748 [ C(RESULT_MISS) ] = 0,
750 [ C(OP_PREFETCH) ] = {
751 [ C(RESULT_ACCESS) ] = 0,
752 [ C(RESULT_MISS) ] = 0,
757 [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */
758 [ C(RESULT_MISS) ] = 0x0085, /* Instr. fetch ITLB misses */
761 [ C(RESULT_ACCESS) ] = -1,
762 [ C(RESULT_MISS) ] = -1,
764 [ C(OP_PREFETCH) ] = {
765 [ C(RESULT_ACCESS) ] = -1,
766 [ C(RESULT_MISS) ] = -1,
771 [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */
772 [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */
775 [ C(RESULT_ACCESS) ] = -1,
776 [ C(RESULT_MISS) ] = -1,
778 [ C(OP_PREFETCH) ] = {
779 [ C(RESULT_ACCESS) ] = -1,
780 [ C(RESULT_MISS) ] = -1,
786 * AMD Performance Monitor K7 and later.
788 static const u64 amd_perfmon_event_map[] =
790 [PERF_COUNT_HW_CPU_CYCLES] = 0x0076,
791 [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0,
792 [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080,
793 [PERF_COUNT_HW_CACHE_MISSES] = 0x0081,
794 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4,
795 [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5,
798 static u64 amd_pmu_event_map(int hw_event)
800 return amd_perfmon_event_map[hw_event];
803 static u64 amd_pmu_raw_event(u64 hw_event)
805 #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL
806 #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL
807 #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL
808 #define K7_EVNTSEL_INV_MASK 0x000800000ULL
809 #define K7_EVNTSEL_REG_MASK 0x0FF000000ULL
811 #define K7_EVNTSEL_MASK \
812 (K7_EVNTSEL_EVENT_MASK | \
813 K7_EVNTSEL_UNIT_MASK | \
814 K7_EVNTSEL_EDGE_MASK | \
815 K7_EVNTSEL_INV_MASK | \
818 return hw_event & K7_EVNTSEL_MASK;
822 * Propagate event elapsed time into the generic event.
823 * Can only be executed on the CPU where the event is active.
824 * Returns the delta events processed.
827 x86_perf_event_update(struct perf_event *event,
828 struct hw_perf_event *hwc, int idx)
830 int shift = 64 - x86_pmu.event_bits;
831 u64 prev_raw_count, new_raw_count;
834 if (idx == X86_PMC_IDX_FIXED_BTS)
838 * Careful: an NMI might modify the previous event value.
840 * Our tactic to handle this is to first atomically read and
841 * exchange a new raw count - then add that new-prev delta
842 * count to the generic event atomically:
845 prev_raw_count = atomic64_read(&hwc->prev_count);
846 rdmsrl(hwc->event_base + idx, new_raw_count);
848 if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count,
849 new_raw_count) != prev_raw_count)
853 * Now we have the new raw value and have updated the prev
854 * timestamp already. We can now calculate the elapsed delta
855 * (event-)time and add that to the generic event.
857 * Careful, not all hw sign-extends above the physical width
860 delta = (new_raw_count << shift) - (prev_raw_count << shift);
863 atomic64_add(delta, &event->count);
864 atomic64_sub(delta, &hwc->period_left);
866 return new_raw_count;
869 static atomic_t active_events;
870 static DEFINE_MUTEX(pmc_reserve_mutex);
872 static bool reserve_pmc_hardware(void)
874 #ifdef CONFIG_X86_LOCAL_APIC
877 if (nmi_watchdog == NMI_LOCAL_APIC)
878 disable_lapic_nmi_watchdog();
880 for (i = 0; i < x86_pmu.num_events; i++) {
881 if (!reserve_perfctr_nmi(x86_pmu.perfctr + i))
885 for (i = 0; i < x86_pmu.num_events; i++) {
886 if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
893 #ifdef CONFIG_X86_LOCAL_APIC
895 for (i--; i >= 0; i--)
896 release_evntsel_nmi(x86_pmu.eventsel + i);
898 i = x86_pmu.num_events;
901 for (i--; i >= 0; i--)
902 release_perfctr_nmi(x86_pmu.perfctr + i);
904 if (nmi_watchdog == NMI_LOCAL_APIC)
905 enable_lapic_nmi_watchdog();
911 static void release_pmc_hardware(void)
913 #ifdef CONFIG_X86_LOCAL_APIC
916 for (i = 0; i < x86_pmu.num_events; i++) {
917 release_perfctr_nmi(x86_pmu.perfctr + i);
918 release_evntsel_nmi(x86_pmu.eventsel + i);
921 if (nmi_watchdog == NMI_LOCAL_APIC)
922 enable_lapic_nmi_watchdog();
926 static inline bool bts_available(void)
928 return x86_pmu.enable_bts != NULL;
931 static inline void init_debug_store_on_cpu(int cpu)
933 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
938 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA,
939 (u32)((u64)(unsigned long)ds),
940 (u32)((u64)(unsigned long)ds >> 32));
943 static inline void fini_debug_store_on_cpu(int cpu)
945 if (!per_cpu(cpu_hw_events, cpu).ds)
948 wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0);
951 static void release_bts_hardware(void)
955 if (!bts_available())
960 for_each_online_cpu(cpu)
961 fini_debug_store_on_cpu(cpu);
963 for_each_possible_cpu(cpu) {
964 struct debug_store *ds = per_cpu(cpu_hw_events, cpu).ds;
969 per_cpu(cpu_hw_events, cpu).ds = NULL;
971 kfree((void *)(unsigned long)ds->bts_buffer_base);
978 static int reserve_bts_hardware(void)
982 if (!bts_available())
987 for_each_possible_cpu(cpu) {
988 struct debug_store *ds;
992 buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL);
993 if (unlikely(!buffer))
996 ds = kzalloc(sizeof(*ds), GFP_KERNEL);
1002 ds->bts_buffer_base = (u64)(unsigned long)buffer;
1003 ds->bts_index = ds->bts_buffer_base;
1004 ds->bts_absolute_maximum =
1005 ds->bts_buffer_base + BTS_BUFFER_SIZE;
1006 ds->bts_interrupt_threshold =
1007 ds->bts_absolute_maximum - BTS_OVFL_TH;
1009 per_cpu(cpu_hw_events, cpu).ds = ds;
1014 release_bts_hardware();
1016 for_each_online_cpu(cpu)
1017 init_debug_store_on_cpu(cpu);
1025 static void hw_perf_event_destroy(struct perf_event *event)
1027 if (atomic_dec_and_mutex_lock(&active_events, &pmc_reserve_mutex)) {
1028 release_pmc_hardware();
1029 release_bts_hardware();
1030 mutex_unlock(&pmc_reserve_mutex);
1034 static inline int x86_pmu_initialized(void)
1036 return x86_pmu.handle_irq != NULL;
1040 set_ext_hw_attr(struct hw_perf_event *hwc, struct perf_event_attr *attr)
1042 unsigned int cache_type, cache_op, cache_result;
1045 config = attr->config;
1047 cache_type = (config >> 0) & 0xff;
1048 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
1051 cache_op = (config >> 8) & 0xff;
1052 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
1055 cache_result = (config >> 16) & 0xff;
1056 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
1059 val = hw_cache_event_ids[cache_type][cache_op][cache_result];
1072 static void intel_pmu_enable_bts(u64 config)
1074 unsigned long debugctlmsr;
1076 debugctlmsr = get_debugctlmsr();
1078 debugctlmsr |= X86_DEBUGCTL_TR;
1079 debugctlmsr |= X86_DEBUGCTL_BTS;
1080 debugctlmsr |= X86_DEBUGCTL_BTINT;
1082 if (!(config & ARCH_PERFMON_EVENTSEL_OS))
1083 debugctlmsr |= X86_DEBUGCTL_BTS_OFF_OS;
1085 if (!(config & ARCH_PERFMON_EVENTSEL_USR))
1086 debugctlmsr |= X86_DEBUGCTL_BTS_OFF_USR;
1088 update_debugctlmsr(debugctlmsr);
1091 static void intel_pmu_disable_bts(void)
1093 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1094 unsigned long debugctlmsr;
1099 debugctlmsr = get_debugctlmsr();
1102 ~(X86_DEBUGCTL_TR | X86_DEBUGCTL_BTS | X86_DEBUGCTL_BTINT |
1103 X86_DEBUGCTL_BTS_OFF_OS | X86_DEBUGCTL_BTS_OFF_USR);
1105 update_debugctlmsr(debugctlmsr);
1109 * Setup the hardware configuration for a given attr_type
1111 static int __hw_perf_event_init(struct perf_event *event)
1113 struct perf_event_attr *attr = &event->attr;
1114 struct hw_perf_event *hwc = &event->hw;
1118 if (!x86_pmu_initialized())
1122 if (!atomic_inc_not_zero(&active_events)) {
1123 mutex_lock(&pmc_reserve_mutex);
1124 if (atomic_read(&active_events) == 0) {
1125 if (!reserve_pmc_hardware())
1128 err = reserve_bts_hardware();
1131 atomic_inc(&active_events);
1132 mutex_unlock(&pmc_reserve_mutex);
1137 event->destroy = hw_perf_event_destroy;
1140 * Generate PMC IRQs:
1141 * (keep 'enabled' bit clear for now)
1143 hwc->config = ARCH_PERFMON_EVENTSEL_INT;
1147 hwc->last_tag = ~0ULL;
1150 * Count user and OS events unless requested not to.
1152 if (!attr->exclude_user)
1153 hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
1154 if (!attr->exclude_kernel)
1155 hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
1157 if (!hwc->sample_period) {
1158 hwc->sample_period = x86_pmu.max_period;
1159 hwc->last_period = hwc->sample_period;
1160 atomic64_set(&hwc->period_left, hwc->sample_period);
1163 * If we have a PMU initialized but no APIC
1164 * interrupts, we cannot sample hardware
1165 * events (user-space has to fall back and
1166 * sample via a hrtimer based software event):
1173 * Raw hw_event type provide the config in the hw_event structure
1175 if (attr->type == PERF_TYPE_RAW) {
1176 hwc->config |= x86_pmu.raw_event(attr->config);
1180 if (attr->type == PERF_TYPE_HW_CACHE)
1181 return set_ext_hw_attr(hwc, attr);
1183 if (attr->config >= x86_pmu.max_events)
1189 config = x86_pmu.event_map(attr->config);
1200 if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) &&
1201 (hwc->sample_period == 1)) {
1202 /* BTS is not supported by this architecture. */
1203 if (!bts_available())
1206 /* BTS is currently only allowed for user-mode. */
1207 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
1211 hwc->config |= config;
1216 static void p6_pmu_disable_all(void)
1220 /* p6 only has one enable register */
1221 rdmsrl(MSR_P6_EVNTSEL0, val);
1222 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
1223 wrmsrl(MSR_P6_EVNTSEL0, val);
1226 static void intel_pmu_disable_all(void)
1228 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1230 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0);
1232 if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask))
1233 intel_pmu_disable_bts();
1236 static void x86_pmu_disable_all(void)
1238 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1241 for (idx = 0; idx < x86_pmu.num_events; idx++) {
1244 if (!test_bit(idx, cpuc->active_mask))
1246 rdmsrl(x86_pmu.eventsel + idx, val);
1247 if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
1249 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
1250 wrmsrl(x86_pmu.eventsel + idx, val);
1254 void hw_perf_disable(void)
1256 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1258 if (!x86_pmu_initialized())
1268 x86_pmu.disable_all();
1271 static void p6_pmu_enable_all(void)
1275 /* p6 only has one enable register */
1276 rdmsrl(MSR_P6_EVNTSEL0, val);
1277 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
1278 wrmsrl(MSR_P6_EVNTSEL0, val);
1281 static void intel_pmu_enable_all(void)
1283 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1285 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl);
1287 if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) {
1288 struct perf_event *event =
1289 cpuc->events[X86_PMC_IDX_FIXED_BTS];
1291 if (WARN_ON_ONCE(!event))
1294 intel_pmu_enable_bts(event->hw.config);
1298 static void x86_pmu_enable_all(void)
1300 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1303 for (idx = 0; idx < x86_pmu.num_events; idx++) {
1304 struct perf_event *event = cpuc->events[idx];
1307 if (!test_bit(idx, cpuc->active_mask))
1310 val = event->hw.config;
1311 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
1312 wrmsrl(x86_pmu.eventsel + idx, val);
1316 static const struct pmu pmu;
1318 static inline int is_x86_event(struct perf_event *event)
1320 return event->pmu == &pmu;
1323 static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
1325 struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
1326 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
1327 int i, j, w, wmax, num = 0;
1328 struct hw_perf_event *hwc;
1330 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
1332 for (i = 0; i < n; i++) {
1334 x86_pmu.get_event_constraints(cpuc, cpuc->event_list[i]);
1338 * fastpath, try to reuse previous register
1340 for (i = 0; i < n; i++) {
1341 hwc = &cpuc->event_list[i]->hw;
1344 /* never assigned */
1348 /* constraint still honored */
1349 if (!test_bit(hwc->idx, c->idxmsk))
1352 /* not already used */
1353 if (test_bit(hwc->idx, used_mask))
1356 set_bit(hwc->idx, used_mask);
1358 assign[i] = hwc->idx;
1367 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
1370 * weight = number of possible counters
1372 * 1 = most constrained, only works on one counter
1373 * wmax = least constrained, works on any counter
1375 * assign events to counters starting with most
1376 * constrained events.
1378 wmax = x86_pmu.num_events;
1381 * when fixed event counters are present,
1382 * wmax is incremented by 1 to account
1383 * for one more choice
1385 if (x86_pmu.num_events_fixed)
1388 for (w = 1, num = n; num && w <= wmax; w++) {
1389 /* for each event */
1390 for (i = 0; num && i < n; i++) {
1392 hwc = &cpuc->event_list[i]->hw;
1397 for_each_bit(j, c->idxmsk, X86_PMC_IDX_MAX) {
1398 if (!test_bit(j, used_mask))
1402 if (j == X86_PMC_IDX_MAX)
1405 set_bit(j, used_mask);
1414 * scheduling failed or is just a simulation,
1415 * free resources if necessary
1417 if (!assign || num) {
1418 for (i = 0; i < n; i++) {
1419 if (x86_pmu.put_event_constraints)
1420 x86_pmu.put_event_constraints(cpuc, cpuc->event_list[i]);
1423 return num ? -ENOSPC : 0;
1427 * dogrp: true if must collect siblings events (group)
1428 * returns total number of events and error code
1430 static int collect_events(struct cpu_hw_events *cpuc, struct perf_event *leader, bool dogrp)
1432 struct perf_event *event;
1435 max_count = x86_pmu.num_events + x86_pmu.num_events_fixed;
1437 /* current number of events already accepted */
1440 if (is_x86_event(leader)) {
1443 cpuc->event_list[n] = leader;
1449 list_for_each_entry(event, &leader->sibling_list, group_entry) {
1450 if (!is_x86_event(event) ||
1451 event->state <= PERF_EVENT_STATE_OFF)
1457 cpuc->event_list[n] = event;
1463 static inline void x86_assign_hw_event(struct perf_event *event,
1464 struct cpu_hw_events *cpuc, int i)
1466 struct hw_perf_event *hwc = &event->hw;
1468 hwc->idx = cpuc->assign[i];
1469 hwc->last_cpu = smp_processor_id();
1470 hwc->last_tag = ++cpuc->tags[i];
1472 if (hwc->idx == X86_PMC_IDX_FIXED_BTS) {
1473 hwc->config_base = 0;
1474 hwc->event_base = 0;
1475 } else if (hwc->idx >= X86_PMC_IDX_FIXED) {
1476 hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL;
1478 * We set it so that event_base + idx in wrmsr/rdmsr maps to
1479 * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2:
1482 MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED;
1484 hwc->config_base = x86_pmu.eventsel;
1485 hwc->event_base = x86_pmu.perfctr;
1489 static inline int match_prev_assignment(struct hw_perf_event *hwc,
1490 struct cpu_hw_events *cpuc,
1493 return hwc->idx == cpuc->assign[i] &&
1494 hwc->last_cpu == smp_processor_id() &&
1495 hwc->last_tag == cpuc->tags[i];
1498 static void x86_pmu_stop(struct perf_event *event);
1500 void hw_perf_enable(void)
1502 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1503 struct perf_event *event;
1504 struct hw_perf_event *hwc;
1507 if (!x86_pmu_initialized())
1513 if (cpuc->n_added) {
1515 * apply assignment obtained either from
1516 * hw_perf_group_sched_in() or x86_pmu_enable()
1518 * step1: save events moving to new counters
1519 * step2: reprogram moved events into new counters
1521 for (i = 0; i < cpuc->n_events; i++) {
1523 event = cpuc->event_list[i];
1527 * we can avoid reprogramming counter if:
1528 * - assigned same counter as last time
1529 * - running on same CPU as last time
1530 * - no other event has used the counter since
1532 if (hwc->idx == -1 ||
1533 match_prev_assignment(hwc, cpuc, i))
1536 x86_pmu_stop(event);
1541 for (i = 0; i < cpuc->n_events; i++) {
1543 event = cpuc->event_list[i];
1546 if (hwc->idx == -1) {
1547 x86_assign_hw_event(event, cpuc, i);
1548 x86_perf_event_set_period(event, hwc, hwc->idx);
1551 * need to mark as active because x86_pmu_disable()
1552 * clear active_mask and events[] yet it preserves
1555 set_bit(hwc->idx, cpuc->active_mask);
1556 cpuc->events[hwc->idx] = event;
1558 x86_pmu.enable(hwc, hwc->idx);
1559 perf_event_update_userpage(event);
1562 perf_events_lapic_init();
1568 x86_pmu.enable_all();
1571 static inline u64 intel_pmu_get_status(void)
1575 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1580 static inline void intel_pmu_ack_status(u64 ack)
1582 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack);
1585 static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, int idx)
1587 (void)checking_wrmsrl(hwc->config_base + idx,
1588 hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
1591 static inline void x86_pmu_disable_event(struct hw_perf_event *hwc, int idx)
1593 (void)checking_wrmsrl(hwc->config_base + idx, hwc->config);
1597 intel_pmu_disable_fixed(struct hw_perf_event *hwc, int __idx)
1599 int idx = __idx - X86_PMC_IDX_FIXED;
1602 mask = 0xfULL << (idx * 4);
1604 rdmsrl(hwc->config_base, ctrl_val);
1606 (void)checking_wrmsrl(hwc->config_base, ctrl_val);
1610 p6_pmu_disable_event(struct hw_perf_event *hwc, int idx)
1612 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1613 u64 val = P6_NOP_EVENT;
1616 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
1618 (void)checking_wrmsrl(hwc->config_base + idx, val);
1622 intel_pmu_disable_event(struct hw_perf_event *hwc, int idx)
1624 if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
1625 intel_pmu_disable_bts();
1629 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
1630 intel_pmu_disable_fixed(hwc, idx);
1634 x86_pmu_disable_event(hwc, idx);
1637 static DEFINE_PER_CPU(u64 [X86_PMC_IDX_MAX], pmc_prev_left);
1640 * Set the next IRQ period, based on the hwc->period_left value.
1641 * To be called with the event disabled in hw:
1644 x86_perf_event_set_period(struct perf_event *event,
1645 struct hw_perf_event *hwc, int idx)
1647 s64 left = atomic64_read(&hwc->period_left);
1648 s64 period = hwc->sample_period;
1651 if (idx == X86_PMC_IDX_FIXED_BTS)
1655 * If we are way outside a reasonable range then just skip forward:
1657 if (unlikely(left <= -period)) {
1659 atomic64_set(&hwc->period_left, left);
1660 hwc->last_period = period;
1664 if (unlikely(left <= 0)) {
1666 atomic64_set(&hwc->period_left, left);
1667 hwc->last_period = period;
1671 * Quirk: certain CPUs dont like it if just 1 hw_event is left:
1673 if (unlikely(left < 2))
1676 if (left > x86_pmu.max_period)
1677 left = x86_pmu.max_period;
1679 per_cpu(pmc_prev_left[idx], smp_processor_id()) = left;
1682 * The hw event starts counting from this event offset,
1683 * mark it to be able to extra future deltas:
1685 atomic64_set(&hwc->prev_count, (u64)-left);
1687 err = checking_wrmsrl(hwc->event_base + idx,
1688 (u64)(-left) & x86_pmu.event_mask);
1690 perf_event_update_userpage(event);
1696 intel_pmu_enable_fixed(struct hw_perf_event *hwc, int __idx)
1698 int idx = __idx - X86_PMC_IDX_FIXED;
1699 u64 ctrl_val, bits, mask;
1703 * Enable IRQ generation (0x8),
1704 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
1708 if (hwc->config & ARCH_PERFMON_EVENTSEL_USR)
1710 if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
1714 * ANY bit is supported in v3 and up
1716 if (x86_pmu.version > 2 && hwc->config & ARCH_PERFMON_EVENTSEL_ANY)
1720 mask = 0xfULL << (idx * 4);
1722 rdmsrl(hwc->config_base, ctrl_val);
1725 err = checking_wrmsrl(hwc->config_base, ctrl_val);
1728 static void p6_pmu_enable_event(struct hw_perf_event *hwc, int idx)
1730 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1735 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
1737 (void)checking_wrmsrl(hwc->config_base + idx, val);
1741 static void intel_pmu_enable_event(struct hw_perf_event *hwc, int idx)
1743 if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) {
1744 if (!__get_cpu_var(cpu_hw_events).enabled)
1747 intel_pmu_enable_bts(hwc->config);
1751 if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) {
1752 intel_pmu_enable_fixed(hwc, idx);
1756 __x86_pmu_enable_event(hwc, idx);
1759 static void x86_pmu_enable_event(struct hw_perf_event *hwc, int idx)
1761 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1763 __x86_pmu_enable_event(hwc, idx);
1767 * activate a single event
1769 * The event is added to the group of enabled events
1770 * but only if it can be scehduled with existing events.
1772 * Called with PMU disabled. If successful and return value 1,
1773 * then guaranteed to call perf_enable() and hw_perf_enable()
1775 static int x86_pmu_enable(struct perf_event *event)
1777 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1778 struct hw_perf_event *hwc;
1779 int assign[X86_PMC_IDX_MAX];
1784 n0 = cpuc->n_events;
1785 n = collect_events(cpuc, event, false);
1789 ret = x86_schedule_events(cpuc, n, assign);
1793 * copy new assignment, now we know it is possible
1794 * will be used by hw_perf_enable()
1796 memcpy(cpuc->assign, assign, n*sizeof(int));
1799 cpuc->n_added = n - n0;
1804 static int x86_pmu_start(struct perf_event *event)
1806 struct hw_perf_event *hwc = &event->hw;
1811 x86_perf_event_set_period(event, hwc, hwc->idx);
1812 x86_pmu.enable(hwc, hwc->idx);
1817 static void x86_pmu_unthrottle(struct perf_event *event)
1819 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1820 struct hw_perf_event *hwc = &event->hw;
1822 if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX ||
1823 cpuc->events[hwc->idx] != event))
1826 x86_pmu.enable(hwc, hwc->idx);
1829 void perf_event_print_debug(void)
1831 u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed;
1832 struct cpu_hw_events *cpuc;
1833 unsigned long flags;
1836 if (!x86_pmu.num_events)
1839 local_irq_save(flags);
1841 cpu = smp_processor_id();
1842 cpuc = &per_cpu(cpu_hw_events, cpu);
1844 if (x86_pmu.version >= 2) {
1845 rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl);
1846 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status);
1847 rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow);
1848 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed);
1851 pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl);
1852 pr_info("CPU#%d: status: %016llx\n", cpu, status);
1853 pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow);
1854 pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed);
1856 pr_info("CPU#%d: active: %016llx\n", cpu, *(u64 *)cpuc->active_mask);
1858 for (idx = 0; idx < x86_pmu.num_events; idx++) {
1859 rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl);
1860 rdmsrl(x86_pmu.perfctr + idx, pmc_count);
1862 prev_left = per_cpu(pmc_prev_left[idx], cpu);
1864 pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n",
1865 cpu, idx, pmc_ctrl);
1866 pr_info("CPU#%d: gen-PMC%d count: %016llx\n",
1867 cpu, idx, pmc_count);
1868 pr_info("CPU#%d: gen-PMC%d left: %016llx\n",
1869 cpu, idx, prev_left);
1871 for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
1872 rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count);
1874 pr_info("CPU#%d: fixed-PMC%d count: %016llx\n",
1875 cpu, idx, pmc_count);
1877 local_irq_restore(flags);
1880 static void intel_pmu_drain_bts_buffer(struct cpu_hw_events *cpuc)
1882 struct debug_store *ds = cpuc->ds;
1888 struct perf_event *event = cpuc->events[X86_PMC_IDX_FIXED_BTS];
1889 struct bts_record *at, *top;
1890 struct perf_output_handle handle;
1891 struct perf_event_header header;
1892 struct perf_sample_data data;
1893 struct pt_regs regs;
1901 at = (struct bts_record *)(unsigned long)ds->bts_buffer_base;
1902 top = (struct bts_record *)(unsigned long)ds->bts_index;
1907 ds->bts_index = ds->bts_buffer_base;
1910 data.period = event->hw.last_period;
1916 * Prepare a generic sample, i.e. fill in the invariant fields.
1917 * We will overwrite the from and to address before we output
1920 perf_prepare_sample(&header, &data, event, ®s);
1922 if (perf_output_begin(&handle, event,
1923 header.size * (top - at), 1, 1))
1926 for (; at < top; at++) {
1930 perf_output_sample(&handle, &header, &data, event);
1933 perf_output_end(&handle);
1935 /* There's new data available. */
1936 event->hw.interrupts++;
1937 event->pending_kill = POLL_IN;
1940 static void x86_pmu_stop(struct perf_event *event)
1942 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1943 struct hw_perf_event *hwc = &event->hw;
1947 * Must be done before we disable, otherwise the nmi handler
1948 * could reenable again:
1950 clear_bit(idx, cpuc->active_mask);
1951 x86_pmu.disable(hwc, idx);
1954 * Drain the remaining delta count out of a event
1955 * that we are disabling:
1957 x86_perf_event_update(event, hwc, idx);
1959 /* Drain the remaining BTS records. */
1960 if (unlikely(idx == X86_PMC_IDX_FIXED_BTS))
1961 intel_pmu_drain_bts_buffer(cpuc);
1963 cpuc->events[idx] = NULL;
1966 static void x86_pmu_disable(struct perf_event *event)
1968 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
1971 x86_pmu_stop(event);
1973 for (i = 0; i < cpuc->n_events; i++) {
1974 if (event == cpuc->event_list[i]) {
1976 if (x86_pmu.put_event_constraints)
1977 x86_pmu.put_event_constraints(cpuc, event);
1979 while (++i < cpuc->n_events)
1980 cpuc->event_list[i-1] = cpuc->event_list[i];
1986 perf_event_update_userpage(event);
1990 * Save and restart an expired event. Called by NMI contexts,
1991 * so it has to be careful about preempting normal event ops:
1993 static int intel_pmu_save_and_restart(struct perf_event *event)
1995 struct hw_perf_event *hwc = &event->hw;
1999 x86_perf_event_update(event, hwc, idx);
2000 ret = x86_perf_event_set_period(event, hwc, idx);
2002 if (event->state == PERF_EVENT_STATE_ACTIVE)
2003 intel_pmu_enable_event(hwc, idx);
2008 static void intel_pmu_reset(void)
2010 struct debug_store *ds = __get_cpu_var(cpu_hw_events).ds;
2011 unsigned long flags;
2014 if (!x86_pmu.num_events)
2017 local_irq_save(flags);
2019 printk("clearing PMU state on CPU#%d\n", smp_processor_id());
2021 for (idx = 0; idx < x86_pmu.num_events; idx++) {
2022 checking_wrmsrl(x86_pmu.eventsel + idx, 0ull);
2023 checking_wrmsrl(x86_pmu.perfctr + idx, 0ull);
2025 for (idx = 0; idx < x86_pmu.num_events_fixed; idx++) {
2026 checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull);
2029 ds->bts_index = ds->bts_buffer_base;
2031 local_irq_restore(flags);
2035 * This handler is triggered by the local APIC, so the APIC IRQ handling
2038 static int intel_pmu_handle_irq(struct pt_regs *regs)
2040 struct perf_sample_data data;
2041 struct cpu_hw_events *cpuc;
2048 cpuc = &__get_cpu_var(cpu_hw_events);
2051 intel_pmu_drain_bts_buffer(cpuc);
2052 status = intel_pmu_get_status();
2060 if (++loops > 100) {
2061 WARN_ONCE(1, "perfevents: irq loop stuck!\n");
2062 perf_event_print_debug();
2068 inc_irq_stat(apic_perf_irqs);
2070 for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) {
2071 struct perf_event *event = cpuc->events[bit];
2073 clear_bit(bit, (unsigned long *) &status);
2074 if (!test_bit(bit, cpuc->active_mask))
2077 if (!intel_pmu_save_and_restart(event))
2080 data.period = event->hw.last_period;
2082 if (perf_event_overflow(event, 1, &data, regs))
2083 intel_pmu_disable_event(&event->hw, bit);
2086 intel_pmu_ack_status(ack);
2089 * Repeat if there is more work to be done:
2091 status = intel_pmu_get_status();
2100 static int x86_pmu_handle_irq(struct pt_regs *regs)
2102 struct perf_sample_data data;
2103 struct cpu_hw_events *cpuc;
2104 struct perf_event *event;
2105 struct hw_perf_event *hwc;
2106 int idx, handled = 0;
2112 cpuc = &__get_cpu_var(cpu_hw_events);
2114 for (idx = 0; idx < x86_pmu.num_events; idx++) {
2115 if (!test_bit(idx, cpuc->active_mask))
2118 event = cpuc->events[idx];
2121 val = x86_perf_event_update(event, hwc, idx);
2122 if (val & (1ULL << (x86_pmu.event_bits - 1)))
2129 data.period = event->hw.last_period;
2131 if (!x86_perf_event_set_period(event, hwc, idx))
2134 if (perf_event_overflow(event, 1, &data, regs))
2135 x86_pmu.disable(hwc, idx);
2139 inc_irq_stat(apic_perf_irqs);
2144 void smp_perf_pending_interrupt(struct pt_regs *regs)
2148 inc_irq_stat(apic_pending_irqs);
2149 perf_event_do_pending();
2153 void set_perf_event_pending(void)
2155 #ifdef CONFIG_X86_LOCAL_APIC
2156 if (!x86_pmu.apic || !x86_pmu_initialized())
2159 apic->send_IPI_self(LOCAL_PENDING_VECTOR);
2163 void perf_events_lapic_init(void)
2165 #ifdef CONFIG_X86_LOCAL_APIC
2166 if (!x86_pmu.apic || !x86_pmu_initialized())
2170 * Always use NMI for PMU
2172 apic_write(APIC_LVTPC, APIC_DM_NMI);
2176 static int __kprobes
2177 perf_event_nmi_handler(struct notifier_block *self,
2178 unsigned long cmd, void *__args)
2180 struct die_args *args = __args;
2181 struct pt_regs *regs;
2183 if (!atomic_read(&active_events))
2197 #ifdef CONFIG_X86_LOCAL_APIC
2198 apic_write(APIC_LVTPC, APIC_DM_NMI);
2201 * Can't rely on the handled return value to say it was our NMI, two
2202 * events could trigger 'simultaneously' raising two back-to-back NMIs.
2204 * If the first NMI handles both, the latter will be empty and daze
2207 x86_pmu.handle_irq(regs);
2212 static struct event_constraint unconstrained;
2214 static struct event_constraint bts_constraint =
2215 EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS, 0);
2217 static struct event_constraint *
2218 intel_special_constraints(struct perf_event *event)
2220 unsigned int hw_event;
2222 hw_event = event->hw.config & INTEL_ARCH_EVENT_MASK;
2224 if (unlikely((hw_event ==
2225 x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) &&
2226 (event->hw.sample_period == 1))) {
2228 return &bts_constraint;
2233 static struct event_constraint *
2234 intel_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
2236 struct event_constraint *c;
2238 c = intel_special_constraints(event);
2242 if (x86_pmu.event_constraints) {
2243 for_each_event_constraint(c, x86_pmu.event_constraints) {
2244 if ((event->hw.config & c->cmask) == c->code)
2249 return &unconstrained;
2252 static struct event_constraint *
2253 amd_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event)
2255 return &unconstrained;
2258 static int x86_event_sched_in(struct perf_event *event,
2259 struct perf_cpu_context *cpuctx, int cpu)
2263 event->state = PERF_EVENT_STATE_ACTIVE;
2265 event->tstamp_running += event->ctx->time - event->tstamp_stopped;
2267 if (!is_x86_event(event))
2268 ret = event->pmu->enable(event);
2270 if (!ret && !is_software_event(event))
2271 cpuctx->active_oncpu++;
2273 if (!ret && event->attr.exclusive)
2274 cpuctx->exclusive = 1;
2279 static void x86_event_sched_out(struct perf_event *event,
2280 struct perf_cpu_context *cpuctx, int cpu)
2282 event->state = PERF_EVENT_STATE_INACTIVE;
2285 if (!is_x86_event(event))
2286 event->pmu->disable(event);
2288 event->tstamp_running -= event->ctx->time - event->tstamp_stopped;
2290 if (!is_software_event(event))
2291 cpuctx->active_oncpu--;
2293 if (event->attr.exclusive || !cpuctx->active_oncpu)
2294 cpuctx->exclusive = 0;
2298 * Called to enable a whole group of events.
2299 * Returns 1 if the group was enabled, or -EAGAIN if it could not be.
2300 * Assumes the caller has disabled interrupts and has
2301 * frozen the PMU with hw_perf_save_disable.
2303 * called with PMU disabled. If successful and return value 1,
2304 * then guaranteed to call perf_enable() and hw_perf_enable()
2306 int hw_perf_group_sched_in(struct perf_event *leader,
2307 struct perf_cpu_context *cpuctx,
2308 struct perf_event_context *ctx, int cpu)
2310 struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
2311 struct perf_event *sub;
2312 int assign[X86_PMC_IDX_MAX];
2315 /* n0 = total number of events */
2316 n0 = collect_events(cpuc, leader, true);
2320 ret = x86_schedule_events(cpuc, n0, assign);
2324 ret = x86_event_sched_in(leader, cpuctx, cpu);
2329 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2330 if (sub->state > PERF_EVENT_STATE_OFF) {
2331 ret = x86_event_sched_in(sub, cpuctx, cpu);
2338 * copy new assignment, now we know it is possible
2339 * will be used by hw_perf_enable()
2341 memcpy(cpuc->assign, assign, n0*sizeof(int));
2343 cpuc->n_events = n0;
2345 ctx->nr_active += n1;
2348 * 1 means successful and events are active
2349 * This is not quite true because we defer
2350 * actual activation until hw_perf_enable() but
2351 * this way we* ensure caller won't try to enable
2356 x86_event_sched_out(leader, cpuctx, cpu);
2358 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2359 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
2360 x86_event_sched_out(sub, cpuctx, cpu);
2368 static __read_mostly struct notifier_block perf_event_nmi_notifier = {
2369 .notifier_call = perf_event_nmi_handler,
2374 static __initconst struct x86_pmu p6_pmu = {
2376 .handle_irq = x86_pmu_handle_irq,
2377 .disable_all = p6_pmu_disable_all,
2378 .enable_all = p6_pmu_enable_all,
2379 .enable = p6_pmu_enable_event,
2380 .disable = p6_pmu_disable_event,
2381 .eventsel = MSR_P6_EVNTSEL0,
2382 .perfctr = MSR_P6_PERFCTR0,
2383 .event_map = p6_pmu_event_map,
2384 .raw_event = p6_pmu_raw_event,
2385 .max_events = ARRAY_SIZE(p6_perfmon_event_map),
2387 .max_period = (1ULL << 31) - 1,
2391 * Events have 40 bits implemented. However they are designed such
2392 * that bits [32-39] are sign extensions of bit 31. As such the
2393 * effective width of a event for P6-like PMU is 32 bits only.
2395 * See IA-32 Intel Architecture Software developer manual Vol 3B
2398 .event_mask = (1ULL << 32) - 1,
2399 .get_event_constraints = intel_get_event_constraints,
2400 .event_constraints = intel_p6_event_constraints
2403 static __initconst struct x86_pmu core_pmu = {
2405 .handle_irq = x86_pmu_handle_irq,
2406 .disable_all = x86_pmu_disable_all,
2407 .enable_all = x86_pmu_enable_all,
2408 .enable = x86_pmu_enable_event,
2409 .disable = x86_pmu_disable_event,
2410 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
2411 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
2412 .event_map = intel_pmu_event_map,
2413 .raw_event = intel_pmu_raw_event,
2414 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
2417 * Intel PMCs cannot be accessed sanely above 32 bit width,
2418 * so we install an artificial 1<<31 period regardless of
2419 * the generic event period:
2421 .max_period = (1ULL << 31) - 1,
2422 .get_event_constraints = intel_get_event_constraints,
2423 .event_constraints = intel_core_event_constraints,
2426 static __initconst struct x86_pmu intel_pmu = {
2428 .handle_irq = intel_pmu_handle_irq,
2429 .disable_all = intel_pmu_disable_all,
2430 .enable_all = intel_pmu_enable_all,
2431 .enable = intel_pmu_enable_event,
2432 .disable = intel_pmu_disable_event,
2433 .eventsel = MSR_ARCH_PERFMON_EVENTSEL0,
2434 .perfctr = MSR_ARCH_PERFMON_PERFCTR0,
2435 .event_map = intel_pmu_event_map,
2436 .raw_event = intel_pmu_raw_event,
2437 .max_events = ARRAY_SIZE(intel_perfmon_event_map),
2440 * Intel PMCs cannot be accessed sanely above 32 bit width,
2441 * so we install an artificial 1<<31 period regardless of
2442 * the generic event period:
2444 .max_period = (1ULL << 31) - 1,
2445 .enable_bts = intel_pmu_enable_bts,
2446 .disable_bts = intel_pmu_disable_bts,
2447 .get_event_constraints = intel_get_event_constraints
2450 static __initconst struct x86_pmu amd_pmu = {
2452 .handle_irq = x86_pmu_handle_irq,
2453 .disable_all = x86_pmu_disable_all,
2454 .enable_all = x86_pmu_enable_all,
2455 .enable = x86_pmu_enable_event,
2456 .disable = x86_pmu_disable_event,
2457 .eventsel = MSR_K7_EVNTSEL0,
2458 .perfctr = MSR_K7_PERFCTR0,
2459 .event_map = amd_pmu_event_map,
2460 .raw_event = amd_pmu_raw_event,
2461 .max_events = ARRAY_SIZE(amd_perfmon_event_map),
2464 .event_mask = (1ULL << 48) - 1,
2466 /* use highest bit to detect overflow */
2467 .max_period = (1ULL << 47) - 1,
2468 .get_event_constraints = amd_get_event_constraints
2471 static __init int p6_pmu_init(void)
2473 switch (boot_cpu_data.x86_model) {
2475 case 3: /* Pentium Pro */
2477 case 6: /* Pentium II */
2480 case 11: /* Pentium III */
2486 pr_cont("unsupported p6 CPU model %d ",
2487 boot_cpu_data.x86_model);
2496 static __init int intel_pmu_init(void)
2498 union cpuid10_edx edx;
2499 union cpuid10_eax eax;
2500 unsigned int unused;
2504 if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) {
2505 /* check for P6 processor family */
2506 if (boot_cpu_data.x86 == 6) {
2507 return p6_pmu_init();
2514 * Check whether the Architectural PerfMon supports
2515 * Branch Misses Retired hw_event or not.
2517 cpuid(10, &eax.full, &ebx, &unused, &edx.full);
2518 if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED)
2521 version = eax.split.version_id;
2525 x86_pmu = intel_pmu;
2527 x86_pmu.version = version;
2528 x86_pmu.num_events = eax.split.num_events;
2529 x86_pmu.event_bits = eax.split.bit_width;
2530 x86_pmu.event_mask = (1ULL << eax.split.bit_width) - 1;
2533 * Quirk: v2 perfmon does not report fixed-purpose events, so
2534 * assume at least 3 events:
2537 x86_pmu.num_events_fixed = max((int)edx.split.num_events_fixed, 3);
2540 * Install the hw-cache-events table:
2542 switch (boot_cpu_data.x86_model) {
2543 case 14: /* 65 nm core solo/duo, "Yonah" */
2544 pr_cont("Core events, ");
2547 case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
2548 case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
2549 case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
2550 case 29: /* six-core 45 nm xeon "Dunnington" */
2551 memcpy(hw_cache_event_ids, core2_hw_cache_event_ids,
2552 sizeof(hw_cache_event_ids));
2554 x86_pmu.event_constraints = intel_core2_event_constraints;
2555 pr_cont("Core2 events, ");
2558 case 26: /* 45 nm nehalem, "Bloomfield" */
2559 case 30: /* 45 nm nehalem, "Lynnfield" */
2560 memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
2561 sizeof(hw_cache_event_ids));
2563 x86_pmu.event_constraints = intel_nehalem_event_constraints;
2564 pr_cont("Nehalem/Corei7 events, ");
2567 memcpy(hw_cache_event_ids, atom_hw_cache_event_ids,
2568 sizeof(hw_cache_event_ids));
2570 x86_pmu.event_constraints = intel_gen_event_constraints;
2571 pr_cont("Atom events, ");
2574 case 37: /* 32 nm nehalem, "Clarkdale" */
2575 case 44: /* 32 nm nehalem, "Gulftown" */
2576 memcpy(hw_cache_event_ids, westmere_hw_cache_event_ids,
2577 sizeof(hw_cache_event_ids));
2579 x86_pmu.event_constraints = intel_westmere_event_constraints;
2580 pr_cont("Westmere events, ");
2584 * default constraints for v2 and up
2586 x86_pmu.event_constraints = intel_gen_event_constraints;
2587 pr_cont("generic architected perfmon, ");
2592 static __init int amd_pmu_init(void)
2594 /* Performance-monitoring supported from K7 and later: */
2595 if (boot_cpu_data.x86 < 6)
2600 /* Events are common for all AMDs */
2601 memcpy(hw_cache_event_ids, amd_hw_cache_event_ids,
2602 sizeof(hw_cache_event_ids));
2607 static void __init pmu_check_apic(void)
2613 pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n");
2614 pr_info("no hardware sampling interrupt available.\n");
2617 void __init init_hw_perf_events(void)
2621 pr_info("Performance Events: ");
2623 switch (boot_cpu_data.x86_vendor) {
2624 case X86_VENDOR_INTEL:
2625 err = intel_pmu_init();
2627 case X86_VENDOR_AMD:
2628 err = amd_pmu_init();
2634 pr_cont("no PMU driver, software events only.\n");
2640 pr_cont("%s PMU driver.\n", x86_pmu.name);
2642 if (x86_pmu.num_events > X86_PMC_MAX_GENERIC) {
2643 WARN(1, KERN_ERR "hw perf events %d > max(%d), clipping!",
2644 x86_pmu.num_events, X86_PMC_MAX_GENERIC);
2645 x86_pmu.num_events = X86_PMC_MAX_GENERIC;
2647 perf_event_mask = (1 << x86_pmu.num_events) - 1;
2648 perf_max_events = x86_pmu.num_events;
2650 if (x86_pmu.num_events_fixed > X86_PMC_MAX_FIXED) {
2651 WARN(1, KERN_ERR "hw perf events fixed %d > max(%d), clipping!",
2652 x86_pmu.num_events_fixed, X86_PMC_MAX_FIXED);
2653 x86_pmu.num_events_fixed = X86_PMC_MAX_FIXED;
2657 ((1LL << x86_pmu.num_events_fixed)-1) << X86_PMC_IDX_FIXED;
2658 x86_pmu.intel_ctrl = perf_event_mask;
2660 perf_events_lapic_init();
2661 register_die_notifier(&perf_event_nmi_notifier);
2663 unconstrained = (struct event_constraint)
2664 __EVENT_CONSTRAINT(0, (1ULL << x86_pmu.num_events) - 1,
2665 0, x86_pmu.num_events);
2667 pr_info("... version: %d\n", x86_pmu.version);
2668 pr_info("... bit width: %d\n", x86_pmu.event_bits);
2669 pr_info("... generic registers: %d\n", x86_pmu.num_events);
2670 pr_info("... value mask: %016Lx\n", x86_pmu.event_mask);
2671 pr_info("... max period: %016Lx\n", x86_pmu.max_period);
2672 pr_info("... fixed-purpose events: %d\n", x86_pmu.num_events_fixed);
2673 pr_info("... event mask: %016Lx\n", perf_event_mask);
2676 static inline void x86_pmu_read(struct perf_event *event)
2678 x86_perf_event_update(event, &event->hw, event->hw.idx);
2681 static const struct pmu pmu = {
2682 .enable = x86_pmu_enable,
2683 .disable = x86_pmu_disable,
2684 .start = x86_pmu_start,
2685 .stop = x86_pmu_stop,
2686 .read = x86_pmu_read,
2687 .unthrottle = x86_pmu_unthrottle,
2691 * validate a single event group
2693 * validation include:
2694 * - check events are compatible which each other
2695 * - events do not compete for the same counter
2696 * - number of events <= number of counters
2698 * validation ensures the group can be loaded onto the
2699 * PMU if it was the only group available.
2701 static int validate_group(struct perf_event *event)
2703 struct perf_event *leader = event->group_leader;
2704 struct cpu_hw_events *fake_cpuc;
2708 fake_cpuc = kmalloc(sizeof(*fake_cpuc), GFP_KERNEL | __GFP_ZERO);
2713 * the event is not yet connected with its
2714 * siblings therefore we must first collect
2715 * existing siblings, then add the new event
2716 * before we can simulate the scheduling
2719 n = collect_events(fake_cpuc, leader, true);
2723 fake_cpuc->n_events = n;
2724 n = collect_events(fake_cpuc, event, false);
2728 fake_cpuc->n_events = n;
2730 ret = x86_schedule_events(fake_cpuc, n, NULL);
2738 const struct pmu *hw_perf_event_init(struct perf_event *event)
2740 const struct pmu *tmp;
2743 err = __hw_perf_event_init(event);
2746 * we temporarily connect event to its pmu
2747 * such that validate_group() can classify
2748 * it as an x86 event using is_x86_event()
2753 if (event->group_leader != event)
2754 err = validate_group(event);
2760 event->destroy(event);
2761 return ERR_PTR(err);
2772 void callchain_store(struct perf_callchain_entry *entry, u64 ip)
2774 if (entry->nr < PERF_MAX_STACK_DEPTH)
2775 entry->ip[entry->nr++] = ip;
2778 static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_irq_entry);
2779 static DEFINE_PER_CPU(struct perf_callchain_entry, pmc_nmi_entry);
2783 backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
2785 /* Ignore warnings */
2788 static void backtrace_warning(void *data, char *msg)
2790 /* Ignore warnings */
2793 static int backtrace_stack(void *data, char *name)
2798 static void backtrace_address(void *data, unsigned long addr, int reliable)
2800 struct perf_callchain_entry *entry = data;
2803 callchain_store(entry, addr);
2806 static const struct stacktrace_ops backtrace_ops = {
2807 .warning = backtrace_warning,
2808 .warning_symbol = backtrace_warning_symbol,
2809 .stack = backtrace_stack,
2810 .address = backtrace_address,
2811 .walk_stack = print_context_stack_bp,
2814 #include "../dumpstack.h"
2817 perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry)
2819 callchain_store(entry, PERF_CONTEXT_KERNEL);
2820 callchain_store(entry, regs->ip);
2822 dump_trace(NULL, regs, NULL, regs->bp, &backtrace_ops, entry);
2826 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
2828 static unsigned long
2829 copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
2831 unsigned long offset, addr = (unsigned long)from;
2832 int type = in_nmi() ? KM_NMI : KM_IRQ0;
2833 unsigned long size, len = 0;
2839 ret = __get_user_pages_fast(addr, 1, 0, &page);
2843 offset = addr & (PAGE_SIZE - 1);
2844 size = min(PAGE_SIZE - offset, n - len);
2846 map = kmap_atomic(page, type);
2847 memcpy(to, map+offset, size);
2848 kunmap_atomic(map, type);
2860 static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
2862 unsigned long bytes;
2864 bytes = copy_from_user_nmi(frame, fp, sizeof(*frame));
2866 return bytes == sizeof(*frame);
2870 perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry)
2872 struct stack_frame frame;
2873 const void __user *fp;
2875 if (!user_mode(regs))
2876 regs = task_pt_regs(current);
2878 fp = (void __user *)regs->bp;
2880 callchain_store(entry, PERF_CONTEXT_USER);
2881 callchain_store(entry, regs->ip);
2883 while (entry->nr < PERF_MAX_STACK_DEPTH) {
2884 frame.next_frame = NULL;
2885 frame.return_address = 0;
2887 if (!copy_stack_frame(fp, &frame))
2890 if ((unsigned long)fp < regs->sp)
2893 callchain_store(entry, frame.return_address);
2894 fp = frame.next_frame;
2899 perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry)
2906 is_user = user_mode(regs);
2908 if (is_user && current->state != TASK_RUNNING)
2912 perf_callchain_kernel(regs, entry);
2915 perf_callchain_user(regs, entry);
2918 struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2920 struct perf_callchain_entry *entry;
2923 entry = &__get_cpu_var(pmc_nmi_entry);
2925 entry = &__get_cpu_var(pmc_irq_entry);
2929 perf_do_callchain(regs, entry);
2934 void hw_perf_event_setup_online(int cpu)
2936 init_debug_store_on_cpu(cpu);