1 #ifdef CONFIG_CPU_SUP_INTEL
4 * Intel PerfMon v3. Used on Core2 and later.
6 static const u64 intel_perfmon_event_map
[] =
8 [PERF_COUNT_HW_CPU_CYCLES
] = 0x003c,
9 [PERF_COUNT_HW_INSTRUCTIONS
] = 0x00c0,
10 [PERF_COUNT_HW_CACHE_REFERENCES
] = 0x4f2e,
11 [PERF_COUNT_HW_CACHE_MISSES
] = 0x412e,
12 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = 0x00c4,
13 [PERF_COUNT_HW_BRANCH_MISSES
] = 0x00c5,
14 [PERF_COUNT_HW_BUS_CYCLES
] = 0x013c,
17 static struct event_constraint intel_core_event_constraints
[] =
19 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
20 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
21 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
22 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
23 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
24 INTEL_EVENT_CONSTRAINT(0xc1, 0x1), /* FP_COMP_INSTR_RET */
28 static struct event_constraint intel_core2_event_constraints
[] =
30 FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
31 FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
32 INTEL_EVENT_CONSTRAINT(0x10, 0x1), /* FP_COMP_OPS_EXE */
33 INTEL_EVENT_CONSTRAINT(0x11, 0x2), /* FP_ASSIST */
34 INTEL_EVENT_CONSTRAINT(0x12, 0x2), /* MUL */
35 INTEL_EVENT_CONSTRAINT(0x13, 0x2), /* DIV */
36 INTEL_EVENT_CONSTRAINT(0x14, 0x1), /* CYCLES_DIV_BUSY */
37 INTEL_EVENT_CONSTRAINT(0x18, 0x1), /* IDLE_DURING_DIV */
38 INTEL_EVENT_CONSTRAINT(0x19, 0x2), /* DELAYED_BYPASS */
39 INTEL_EVENT_CONSTRAINT(0xa1, 0x1), /* RS_UOPS_DISPATCH_CYCLES */
40 INTEL_EVENT_CONSTRAINT(0xcb, 0x1), /* MEM_LOAD_RETIRED */
44 static struct event_constraint intel_nehalem_event_constraints
[] =
46 FIXED_EVENT_CONSTRAINT(0xc0, (0xf|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
47 FIXED_EVENT_CONSTRAINT(0x3c, (0xf|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
48 INTEL_EVENT_CONSTRAINT(0x40, 0x3), /* L1D_CACHE_LD */
49 INTEL_EVENT_CONSTRAINT(0x41, 0x3), /* L1D_CACHE_ST */
50 INTEL_EVENT_CONSTRAINT(0x42, 0x3), /* L1D_CACHE_LOCK */
51 INTEL_EVENT_CONSTRAINT(0x43, 0x3), /* L1D_ALL_REF */
52 INTEL_EVENT_CONSTRAINT(0x48, 0x3), /* L1D_PEND_MISS */
53 INTEL_EVENT_CONSTRAINT(0x4e, 0x3), /* L1D_PREFETCH */
54 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
55 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
59 static struct event_constraint intel_westmere_event_constraints
[] =
61 FIXED_EVENT_CONSTRAINT(0xc0, (0xf|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
62 FIXED_EVENT_CONSTRAINT(0x3c, (0xf|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
63 INTEL_EVENT_CONSTRAINT(0x51, 0x3), /* L1D */
64 INTEL_EVENT_CONSTRAINT(0x60, 0x1), /* OFFCORE_REQUESTS_OUTSTANDING */
65 INTEL_EVENT_CONSTRAINT(0x63, 0x3), /* CACHE_LOCK_CYCLES */
69 static struct event_constraint intel_gen_event_constraints
[] =
71 FIXED_EVENT_CONSTRAINT(0xc0, (0x3|(1ULL<<32))), /* INSTRUCTIONS_RETIRED */
72 FIXED_EVENT_CONSTRAINT(0x3c, (0x3|(1ULL<<33))), /* UNHALTED_CORE_CYCLES */
76 static u64
intel_pmu_event_map(int hw_event
)
78 return intel_perfmon_event_map
[hw_event
];
81 static __initconst u64 westmere_hw_cache_event_ids
82 [PERF_COUNT_HW_CACHE_MAX
]
83 [PERF_COUNT_HW_CACHE_OP_MAX
]
84 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
88 [ C(RESULT_ACCESS
) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
89 [ C(RESULT_MISS
) ] = 0x0151, /* L1D.REPL */
92 [ C(RESULT_ACCESS
) ] = 0x020b, /* MEM_INST_RETURED.STORES */
93 [ C(RESULT_MISS
) ] = 0x0251, /* L1D.M_REPL */
95 [ C(OP_PREFETCH
) ] = {
96 [ C(RESULT_ACCESS
) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
97 [ C(RESULT_MISS
) ] = 0x024e, /* L1D_PREFETCH.MISS */
102 [ C(RESULT_ACCESS
) ] = 0x0380, /* L1I.READS */
103 [ C(RESULT_MISS
) ] = 0x0280, /* L1I.MISSES */
106 [ C(RESULT_ACCESS
) ] = -1,
107 [ C(RESULT_MISS
) ] = -1,
109 [ C(OP_PREFETCH
) ] = {
110 [ C(RESULT_ACCESS
) ] = 0x0,
111 [ C(RESULT_MISS
) ] = 0x0,
116 [ C(RESULT_ACCESS
) ] = 0x0324, /* L2_RQSTS.LOADS */
117 [ C(RESULT_MISS
) ] = 0x0224, /* L2_RQSTS.LD_MISS */
120 [ C(RESULT_ACCESS
) ] = 0x0c24, /* L2_RQSTS.RFOS */
121 [ C(RESULT_MISS
) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
123 [ C(OP_PREFETCH
) ] = {
124 [ C(RESULT_ACCESS
) ] = 0x4f2e, /* LLC Reference */
125 [ C(RESULT_MISS
) ] = 0x412e, /* LLC Misses */
130 [ C(RESULT_ACCESS
) ] = 0x010b, /* MEM_INST_RETIRED.LOADS */
131 [ C(RESULT_MISS
) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
134 [ C(RESULT_ACCESS
) ] = 0x020b, /* MEM_INST_RETURED.STORES */
135 [ C(RESULT_MISS
) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
137 [ C(OP_PREFETCH
) ] = {
138 [ C(RESULT_ACCESS
) ] = 0x0,
139 [ C(RESULT_MISS
) ] = 0x0,
144 [ C(RESULT_ACCESS
) ] = 0x01c0, /* INST_RETIRED.ANY_P */
145 [ C(RESULT_MISS
) ] = 0x0185, /* ITLB_MISSES.ANY */
148 [ C(RESULT_ACCESS
) ] = -1,
149 [ C(RESULT_MISS
) ] = -1,
151 [ C(OP_PREFETCH
) ] = {
152 [ C(RESULT_ACCESS
) ] = -1,
153 [ C(RESULT_MISS
) ] = -1,
158 [ C(RESULT_ACCESS
) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
159 [ C(RESULT_MISS
) ] = 0x03e8, /* BPU_CLEARS.ANY */
162 [ C(RESULT_ACCESS
) ] = -1,
163 [ C(RESULT_MISS
) ] = -1,
165 [ C(OP_PREFETCH
) ] = {
166 [ C(RESULT_ACCESS
) ] = -1,
167 [ C(RESULT_MISS
) ] = -1,
172 static __initconst u64 nehalem_hw_cache_event_ids
173 [PERF_COUNT_HW_CACHE_MAX
]
174 [PERF_COUNT_HW_CACHE_OP_MAX
]
175 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
179 [ C(RESULT_ACCESS
) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
180 [ C(RESULT_MISS
) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
183 [ C(RESULT_ACCESS
) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
184 [ C(RESULT_MISS
) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
186 [ C(OP_PREFETCH
) ] = {
187 [ C(RESULT_ACCESS
) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */
188 [ C(RESULT_MISS
) ] = 0x024e, /* L1D_PREFETCH.MISS */
193 [ C(RESULT_ACCESS
) ] = 0x0380, /* L1I.READS */
194 [ C(RESULT_MISS
) ] = 0x0280, /* L1I.MISSES */
197 [ C(RESULT_ACCESS
) ] = -1,
198 [ C(RESULT_MISS
) ] = -1,
200 [ C(OP_PREFETCH
) ] = {
201 [ C(RESULT_ACCESS
) ] = 0x0,
202 [ C(RESULT_MISS
) ] = 0x0,
207 [ C(RESULT_ACCESS
) ] = 0x0324, /* L2_RQSTS.LOADS */
208 [ C(RESULT_MISS
) ] = 0x0224, /* L2_RQSTS.LD_MISS */
211 [ C(RESULT_ACCESS
) ] = 0x0c24, /* L2_RQSTS.RFOS */
212 [ C(RESULT_MISS
) ] = 0x0824, /* L2_RQSTS.RFO_MISS */
214 [ C(OP_PREFETCH
) ] = {
215 [ C(RESULT_ACCESS
) ] = 0x4f2e, /* LLC Reference */
216 [ C(RESULT_MISS
) ] = 0x412e, /* LLC Misses */
221 [ C(RESULT_ACCESS
) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
222 [ C(RESULT_MISS
) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */
225 [ C(RESULT_ACCESS
) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
226 [ C(RESULT_MISS
) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */
228 [ C(OP_PREFETCH
) ] = {
229 [ C(RESULT_ACCESS
) ] = 0x0,
230 [ C(RESULT_MISS
) ] = 0x0,
235 [ C(RESULT_ACCESS
) ] = 0x01c0, /* INST_RETIRED.ANY_P */
236 [ C(RESULT_MISS
) ] = 0x20c8, /* ITLB_MISS_RETIRED */
239 [ C(RESULT_ACCESS
) ] = -1,
240 [ C(RESULT_MISS
) ] = -1,
242 [ C(OP_PREFETCH
) ] = {
243 [ C(RESULT_ACCESS
) ] = -1,
244 [ C(RESULT_MISS
) ] = -1,
249 [ C(RESULT_ACCESS
) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */
250 [ C(RESULT_MISS
) ] = 0x03e8, /* BPU_CLEARS.ANY */
253 [ C(RESULT_ACCESS
) ] = -1,
254 [ C(RESULT_MISS
) ] = -1,
256 [ C(OP_PREFETCH
) ] = {
257 [ C(RESULT_ACCESS
) ] = -1,
258 [ C(RESULT_MISS
) ] = -1,
263 static __initconst u64 core2_hw_cache_event_ids
264 [PERF_COUNT_HW_CACHE_MAX
]
265 [PERF_COUNT_HW_CACHE_OP_MAX
]
266 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
270 [ C(RESULT_ACCESS
) ] = 0x0f40, /* L1D_CACHE_LD.MESI */
271 [ C(RESULT_MISS
) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */
274 [ C(RESULT_ACCESS
) ] = 0x0f41, /* L1D_CACHE_ST.MESI */
275 [ C(RESULT_MISS
) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */
277 [ C(OP_PREFETCH
) ] = {
278 [ C(RESULT_ACCESS
) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */
279 [ C(RESULT_MISS
) ] = 0,
284 [ C(RESULT_ACCESS
) ] = 0x0080, /* L1I.READS */
285 [ C(RESULT_MISS
) ] = 0x0081, /* L1I.MISSES */
288 [ C(RESULT_ACCESS
) ] = -1,
289 [ C(RESULT_MISS
) ] = -1,
291 [ C(OP_PREFETCH
) ] = {
292 [ C(RESULT_ACCESS
) ] = 0,
293 [ C(RESULT_MISS
) ] = 0,
298 [ C(RESULT_ACCESS
) ] = 0x4f29, /* L2_LD.MESI */
299 [ C(RESULT_MISS
) ] = 0x4129, /* L2_LD.ISTATE */
302 [ C(RESULT_ACCESS
) ] = 0x4f2A, /* L2_ST.MESI */
303 [ C(RESULT_MISS
) ] = 0x412A, /* L2_ST.ISTATE */
305 [ C(OP_PREFETCH
) ] = {
306 [ C(RESULT_ACCESS
) ] = 0,
307 [ C(RESULT_MISS
) ] = 0,
312 [ C(RESULT_ACCESS
) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */
313 [ C(RESULT_MISS
) ] = 0x0208, /* DTLB_MISSES.MISS_LD */
316 [ C(RESULT_ACCESS
) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */
317 [ C(RESULT_MISS
) ] = 0x0808, /* DTLB_MISSES.MISS_ST */
319 [ C(OP_PREFETCH
) ] = {
320 [ C(RESULT_ACCESS
) ] = 0,
321 [ C(RESULT_MISS
) ] = 0,
326 [ C(RESULT_ACCESS
) ] = 0x00c0, /* INST_RETIRED.ANY_P */
327 [ C(RESULT_MISS
) ] = 0x1282, /* ITLBMISSES */
330 [ C(RESULT_ACCESS
) ] = -1,
331 [ C(RESULT_MISS
) ] = -1,
333 [ C(OP_PREFETCH
) ] = {
334 [ C(RESULT_ACCESS
) ] = -1,
335 [ C(RESULT_MISS
) ] = -1,
340 [ C(RESULT_ACCESS
) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
341 [ C(RESULT_MISS
) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
344 [ C(RESULT_ACCESS
) ] = -1,
345 [ C(RESULT_MISS
) ] = -1,
347 [ C(OP_PREFETCH
) ] = {
348 [ C(RESULT_ACCESS
) ] = -1,
349 [ C(RESULT_MISS
) ] = -1,
354 static __initconst u64 atom_hw_cache_event_ids
355 [PERF_COUNT_HW_CACHE_MAX
]
356 [PERF_COUNT_HW_CACHE_OP_MAX
]
357 [PERF_COUNT_HW_CACHE_RESULT_MAX
] =
361 [ C(RESULT_ACCESS
) ] = 0x2140, /* L1D_CACHE.LD */
362 [ C(RESULT_MISS
) ] = 0,
365 [ C(RESULT_ACCESS
) ] = 0x2240, /* L1D_CACHE.ST */
366 [ C(RESULT_MISS
) ] = 0,
368 [ C(OP_PREFETCH
) ] = {
369 [ C(RESULT_ACCESS
) ] = 0x0,
370 [ C(RESULT_MISS
) ] = 0,
375 [ C(RESULT_ACCESS
) ] = 0x0380, /* L1I.READS */
376 [ C(RESULT_MISS
) ] = 0x0280, /* L1I.MISSES */
379 [ C(RESULT_ACCESS
) ] = -1,
380 [ C(RESULT_MISS
) ] = -1,
382 [ C(OP_PREFETCH
) ] = {
383 [ C(RESULT_ACCESS
) ] = 0,
384 [ C(RESULT_MISS
) ] = 0,
389 [ C(RESULT_ACCESS
) ] = 0x4f29, /* L2_LD.MESI */
390 [ C(RESULT_MISS
) ] = 0x4129, /* L2_LD.ISTATE */
393 [ C(RESULT_ACCESS
) ] = 0x4f2A, /* L2_ST.MESI */
394 [ C(RESULT_MISS
) ] = 0x412A, /* L2_ST.ISTATE */
396 [ C(OP_PREFETCH
) ] = {
397 [ C(RESULT_ACCESS
) ] = 0,
398 [ C(RESULT_MISS
) ] = 0,
403 [ C(RESULT_ACCESS
) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */
404 [ C(RESULT_MISS
) ] = 0x0508, /* DTLB_MISSES.MISS_LD */
407 [ C(RESULT_ACCESS
) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */
408 [ C(RESULT_MISS
) ] = 0x0608, /* DTLB_MISSES.MISS_ST */
410 [ C(OP_PREFETCH
) ] = {
411 [ C(RESULT_ACCESS
) ] = 0,
412 [ C(RESULT_MISS
) ] = 0,
417 [ C(RESULT_ACCESS
) ] = 0x00c0, /* INST_RETIRED.ANY_P */
418 [ C(RESULT_MISS
) ] = 0x0282, /* ITLB.MISSES */
421 [ C(RESULT_ACCESS
) ] = -1,
422 [ C(RESULT_MISS
) ] = -1,
424 [ C(OP_PREFETCH
) ] = {
425 [ C(RESULT_ACCESS
) ] = -1,
426 [ C(RESULT_MISS
) ] = -1,
431 [ C(RESULT_ACCESS
) ] = 0x00c4, /* BR_INST_RETIRED.ANY */
432 [ C(RESULT_MISS
) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */
435 [ C(RESULT_ACCESS
) ] = -1,
436 [ C(RESULT_MISS
) ] = -1,
438 [ C(OP_PREFETCH
) ] = {
439 [ C(RESULT_ACCESS
) ] = -1,
440 [ C(RESULT_MISS
) ] = -1,
445 static u64
intel_pmu_raw_event(u64 hw_event
)
447 #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL
448 #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL
449 #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL
450 #define CORE_EVNTSEL_INV_MASK 0x00800000ULL
451 #define CORE_EVNTSEL_REG_MASK 0xFF000000ULL
453 #define CORE_EVNTSEL_MASK \
454 (INTEL_ARCH_EVTSEL_MASK | \
455 INTEL_ARCH_UNIT_MASK | \
456 INTEL_ARCH_EDGE_MASK | \
457 INTEL_ARCH_INV_MASK | \
460 return hw_event
& CORE_EVNTSEL_MASK
;
463 static void intel_pmu_enable_bts(u64 config
)
465 unsigned long debugctlmsr
;
467 debugctlmsr
= get_debugctlmsr();
469 debugctlmsr
|= X86_DEBUGCTL_TR
;
470 debugctlmsr
|= X86_DEBUGCTL_BTS
;
471 debugctlmsr
|= X86_DEBUGCTL_BTINT
;
473 if (!(config
& ARCH_PERFMON_EVENTSEL_OS
))
474 debugctlmsr
|= X86_DEBUGCTL_BTS_OFF_OS
;
476 if (!(config
& ARCH_PERFMON_EVENTSEL_USR
))
477 debugctlmsr
|= X86_DEBUGCTL_BTS_OFF_USR
;
479 update_debugctlmsr(debugctlmsr
);
482 static void intel_pmu_disable_bts(void)
484 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
485 unsigned long debugctlmsr
;
490 debugctlmsr
= get_debugctlmsr();
493 ~(X86_DEBUGCTL_TR
| X86_DEBUGCTL_BTS
| X86_DEBUGCTL_BTINT
|
494 X86_DEBUGCTL_BTS_OFF_OS
| X86_DEBUGCTL_BTS_OFF_USR
);
496 update_debugctlmsr(debugctlmsr
);
499 static void intel_pmu_disable_all(void)
501 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
503 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, 0);
505 if (test_bit(X86_PMC_IDX_FIXED_BTS
, cpuc
->active_mask
))
506 intel_pmu_disable_bts();
509 static void intel_pmu_enable_all(void)
511 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
513 wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL
, x86_pmu
.intel_ctrl
);
515 if (test_bit(X86_PMC_IDX_FIXED_BTS
, cpuc
->active_mask
)) {
516 struct perf_event
*event
=
517 cpuc
->events
[X86_PMC_IDX_FIXED_BTS
];
519 if (WARN_ON_ONCE(!event
))
522 intel_pmu_enable_bts(event
->hw
.config
);
526 static inline u64
intel_pmu_get_status(void)
530 rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS
, status
);
535 static inline void intel_pmu_ack_status(u64 ack
)
537 wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL
, ack
);
541 intel_pmu_disable_fixed(struct hw_perf_event
*hwc
, int __idx
)
543 int idx
= __idx
- X86_PMC_IDX_FIXED
;
546 mask
= 0xfULL
<< (idx
* 4);
548 rdmsrl(hwc
->config_base
, ctrl_val
);
550 (void)checking_wrmsrl(hwc
->config_base
, ctrl_val
);
553 static void intel_pmu_drain_bts_buffer(void)
555 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
556 struct debug_store
*ds
= cpuc
->ds
;
562 struct perf_event
*event
= cpuc
->events
[X86_PMC_IDX_FIXED_BTS
];
563 struct bts_record
*at
, *top
;
564 struct perf_output_handle handle
;
565 struct perf_event_header header
;
566 struct perf_sample_data data
;
575 at
= (struct bts_record
*)(unsigned long)ds
->bts_buffer_base
;
576 top
= (struct bts_record
*)(unsigned long)ds
->bts_index
;
581 ds
->bts_index
= ds
->bts_buffer_base
;
584 data
.period
= event
->hw
.last_period
;
590 * Prepare a generic sample, i.e. fill in the invariant fields.
591 * We will overwrite the from and to address before we output
594 perf_prepare_sample(&header
, &data
, event
, ®s
);
596 if (perf_output_begin(&handle
, event
,
597 header
.size
* (top
- at
), 1, 1))
600 for (; at
< top
; at
++) {
604 perf_output_sample(&handle
, &header
, &data
, event
);
607 perf_output_end(&handle
);
609 /* There's new data available. */
610 event
->hw
.interrupts
++;
611 event
->pending_kill
= POLL_IN
;
615 intel_pmu_disable_event(struct hw_perf_event
*hwc
, int idx
)
617 if (unlikely(idx
== X86_PMC_IDX_FIXED_BTS
)) {
618 intel_pmu_disable_bts();
619 intel_pmu_drain_bts_buffer();
623 if (unlikely(hwc
->config_base
== MSR_ARCH_PERFMON_FIXED_CTR_CTRL
)) {
624 intel_pmu_disable_fixed(hwc
, idx
);
628 x86_pmu_disable_event(hwc
, idx
);
632 intel_pmu_enable_fixed(struct hw_perf_event
*hwc
, int __idx
)
634 int idx
= __idx
- X86_PMC_IDX_FIXED
;
635 u64 ctrl_val
, bits
, mask
;
639 * Enable IRQ generation (0x8),
640 * and enable ring-3 counting (0x2) and ring-0 counting (0x1)
644 if (hwc
->config
& ARCH_PERFMON_EVENTSEL_USR
)
646 if (hwc
->config
& ARCH_PERFMON_EVENTSEL_OS
)
650 * ANY bit is supported in v3 and up
652 if (x86_pmu
.version
> 2 && hwc
->config
& ARCH_PERFMON_EVENTSEL_ANY
)
656 mask
= 0xfULL
<< (idx
* 4);
658 rdmsrl(hwc
->config_base
, ctrl_val
);
661 err
= checking_wrmsrl(hwc
->config_base
, ctrl_val
);
664 static void intel_pmu_enable_event(struct hw_perf_event
*hwc
, int idx
)
666 if (unlikely(idx
== X86_PMC_IDX_FIXED_BTS
)) {
667 if (!__get_cpu_var(cpu_hw_events
).enabled
)
670 intel_pmu_enable_bts(hwc
->config
);
674 if (unlikely(hwc
->config_base
== MSR_ARCH_PERFMON_FIXED_CTR_CTRL
)) {
675 intel_pmu_enable_fixed(hwc
, idx
);
679 __x86_pmu_enable_event(hwc
, idx
);
683 * Save and restart an expired event. Called by NMI contexts,
684 * so it has to be careful about preempting normal event ops:
686 static int intel_pmu_save_and_restart(struct perf_event
*event
)
688 struct hw_perf_event
*hwc
= &event
->hw
;
692 x86_perf_event_update(event
, hwc
, idx
);
693 ret
= x86_perf_event_set_period(event
, hwc
, idx
);
698 static void intel_pmu_reset(void)
700 struct debug_store
*ds
= __get_cpu_var(cpu_hw_events
).ds
;
704 if (!x86_pmu
.num_events
)
707 local_irq_save(flags
);
709 printk("clearing PMU state on CPU#%d\n", smp_processor_id());
711 for (idx
= 0; idx
< x86_pmu
.num_events
; idx
++) {
712 checking_wrmsrl(x86_pmu
.eventsel
+ idx
, 0ull);
713 checking_wrmsrl(x86_pmu
.perfctr
+ idx
, 0ull);
715 for (idx
= 0; idx
< x86_pmu
.num_events_fixed
; idx
++) {
716 checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0
+ idx
, 0ull);
719 ds
->bts_index
= ds
->bts_buffer_base
;
721 local_irq_restore(flags
);
725 * This handler is triggered by the local APIC, so the APIC IRQ handling
728 static int intel_pmu_handle_irq(struct pt_regs
*regs
)
730 struct perf_sample_data data
;
731 struct cpu_hw_events
*cpuc
;
738 cpuc
= &__get_cpu_var(cpu_hw_events
);
741 intel_pmu_drain_bts_buffer();
742 status
= intel_pmu_get_status();
751 WARN_ONCE(1, "perfevents: irq loop stuck!\n");
752 perf_event_print_debug();
758 inc_irq_stat(apic_perf_irqs
);
760 for_each_bit(bit
, (unsigned long *)&status
, X86_PMC_IDX_MAX
) {
761 struct perf_event
*event
= cpuc
->events
[bit
];
763 clear_bit(bit
, (unsigned long *) &status
);
764 if (!test_bit(bit
, cpuc
->active_mask
))
767 if (!intel_pmu_save_and_restart(event
))
770 data
.period
= event
->hw
.last_period
;
772 if (perf_event_overflow(event
, 1, &data
, regs
))
773 intel_pmu_disable_event(&event
->hw
, bit
);
776 intel_pmu_ack_status(ack
);
779 * Repeat if there is more work to be done:
781 status
= intel_pmu_get_status();
790 static struct event_constraint bts_constraint
=
791 EVENT_CONSTRAINT(0, 1ULL << X86_PMC_IDX_FIXED_BTS
, 0);
793 static struct event_constraint
*
794 intel_special_constraints(struct perf_event
*event
)
796 unsigned int hw_event
;
798 hw_event
= event
->hw
.config
& INTEL_ARCH_EVENT_MASK
;
800 if (unlikely((hw_event
==
801 x86_pmu
.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS
)) &&
802 (event
->hw
.sample_period
== 1))) {
804 return &bts_constraint
;
809 static struct event_constraint
*
810 intel_get_event_constraints(struct cpu_hw_events
*cpuc
, struct perf_event
*event
)
812 struct event_constraint
*c
;
814 c
= intel_special_constraints(event
);
818 return x86_get_event_constraints(cpuc
, event
);
821 static __initconst
struct x86_pmu core_pmu
= {
823 .handle_irq
= x86_pmu_handle_irq
,
824 .disable_all
= x86_pmu_disable_all
,
825 .enable_all
= x86_pmu_enable_all
,
826 .enable
= x86_pmu_enable_event
,
827 .disable
= x86_pmu_disable_event
,
828 .eventsel
= MSR_ARCH_PERFMON_EVENTSEL0
,
829 .perfctr
= MSR_ARCH_PERFMON_PERFCTR0
,
830 .event_map
= intel_pmu_event_map
,
831 .raw_event
= intel_pmu_raw_event
,
832 .max_events
= ARRAY_SIZE(intel_perfmon_event_map
),
835 * Intel PMCs cannot be accessed sanely above 32 bit width,
836 * so we install an artificial 1<<31 period regardless of
837 * the generic event period:
839 .max_period
= (1ULL << 31) - 1,
840 .get_event_constraints
= intel_get_event_constraints
,
841 .event_constraints
= intel_core_event_constraints
,
844 static __initconst
struct x86_pmu intel_pmu
= {
846 .handle_irq
= intel_pmu_handle_irq
,
847 .disable_all
= intel_pmu_disable_all
,
848 .enable_all
= intel_pmu_enable_all
,
849 .enable
= intel_pmu_enable_event
,
850 .disable
= intel_pmu_disable_event
,
851 .eventsel
= MSR_ARCH_PERFMON_EVENTSEL0
,
852 .perfctr
= MSR_ARCH_PERFMON_PERFCTR0
,
853 .event_map
= intel_pmu_event_map
,
854 .raw_event
= intel_pmu_raw_event
,
855 .max_events
= ARRAY_SIZE(intel_perfmon_event_map
),
858 * Intel PMCs cannot be accessed sanely above 32 bit width,
859 * so we install an artificial 1<<31 period regardless of
860 * the generic event period:
862 .max_period
= (1ULL << 31) - 1,
863 .enable_bts
= intel_pmu_enable_bts
,
864 .disable_bts
= intel_pmu_disable_bts
,
865 .get_event_constraints
= intel_get_event_constraints
868 static __init
int intel_pmu_init(void)
870 union cpuid10_edx edx
;
871 union cpuid10_eax eax
;
876 if (!cpu_has(&boot_cpu_data
, X86_FEATURE_ARCH_PERFMON
)) {
877 /* check for P6 processor family */
878 if (boot_cpu_data
.x86
== 6) {
879 return p6_pmu_init();
886 * Check whether the Architectural PerfMon supports
887 * Branch Misses Retired hw_event or not.
889 cpuid(10, &eax
.full
, &ebx
, &unused
, &edx
.full
);
890 if (eax
.split
.mask_length
<= ARCH_PERFMON_BRANCH_MISSES_RETIRED
)
893 version
= eax
.split
.version_id
;
899 x86_pmu
.version
= version
;
900 x86_pmu
.num_events
= eax
.split
.num_events
;
901 x86_pmu
.event_bits
= eax
.split
.bit_width
;
902 x86_pmu
.event_mask
= (1ULL << eax
.split
.bit_width
) - 1;
905 * Quirk: v2 perfmon does not report fixed-purpose events, so
906 * assume at least 3 events:
909 x86_pmu
.num_events_fixed
= max((int)edx
.split
.num_events_fixed
, 3);
912 * Install the hw-cache-events table:
914 switch (boot_cpu_data
.x86_model
) {
915 case 14: /* 65 nm core solo/duo, "Yonah" */
916 pr_cont("Core events, ");
919 case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */
920 case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */
921 case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */
922 case 29: /* six-core 45 nm xeon "Dunnington" */
923 memcpy(hw_cache_event_ids
, core2_hw_cache_event_ids
,
924 sizeof(hw_cache_event_ids
));
926 x86_pmu
.event_constraints
= intel_core2_event_constraints
;
927 pr_cont("Core2 events, ");
930 case 26: /* 45 nm nehalem, "Bloomfield" */
931 case 30: /* 45 nm nehalem, "Lynnfield" */
932 memcpy(hw_cache_event_ids
, nehalem_hw_cache_event_ids
,
933 sizeof(hw_cache_event_ids
));
935 x86_pmu
.event_constraints
= intel_nehalem_event_constraints
;
936 pr_cont("Nehalem/Corei7 events, ");
939 memcpy(hw_cache_event_ids
, atom_hw_cache_event_ids
,
940 sizeof(hw_cache_event_ids
));
942 x86_pmu
.event_constraints
= intel_gen_event_constraints
;
943 pr_cont("Atom events, ");
946 case 37: /* 32 nm nehalem, "Clarkdale" */
947 case 44: /* 32 nm nehalem, "Gulftown" */
948 memcpy(hw_cache_event_ids
, westmere_hw_cache_event_ids
,
949 sizeof(hw_cache_event_ids
));
951 x86_pmu
.event_constraints
= intel_westmere_event_constraints
;
952 pr_cont("Westmere events, ");
956 * default constraints for v2 and up
958 x86_pmu
.event_constraints
= intel_gen_event_constraints
;
959 pr_cont("generic architected perfmon, ");
964 #else /* CONFIG_CPU_SUP_INTEL */
966 static int intel_pmu_init(void)
971 #endif /* CONFIG_CPU_SUP_INTEL */