]>
Commit | Line | Data |
---|---|---|
241771ef IM |
1 | /* |
2 | * Performance counter x86 architecture code | |
3 | * | |
98144511 IM |
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> | |
30dd568c | 9 | * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com> |
241771ef IM |
10 | * |
11 | * For licencing details see kernel-base/COPYING | |
12 | */ | |
13 | ||
14 | #include <linux/perf_counter.h> | |
15 | #include <linux/capability.h> | |
16 | #include <linux/notifier.h> | |
17 | #include <linux/hardirq.h> | |
18 | #include <linux/kprobes.h> | |
4ac13294 | 19 | #include <linux/module.h> |
241771ef IM |
20 | #include <linux/kdebug.h> |
21 | #include <linux/sched.h> | |
d7d59fb3 | 22 | #include <linux/uaccess.h> |
74193ef0 | 23 | #include <linux/highmem.h> |
30dd568c | 24 | #include <linux/cpu.h> |
241771ef | 25 | |
241771ef | 26 | #include <asm/apic.h> |
d7d59fb3 | 27 | #include <asm/stacktrace.h> |
4e935e47 | 28 | #include <asm/nmi.h> |
241771ef | 29 | |
862a1a5f | 30 | static u64 perf_counter_mask __read_mostly; |
703e937c | 31 | |
30dd568c MM |
32 | /* The maximal number of PEBS counters: */ |
33 | #define MAX_PEBS_COUNTERS 4 | |
34 | ||
35 | /* The size of a BTS record in bytes: */ | |
36 | #define BTS_RECORD_SIZE 24 | |
37 | ||
38 | /* The size of a per-cpu BTS buffer in bytes: */ | |
39 | #define BTS_BUFFER_SIZE (BTS_RECORD_SIZE * 1024) | |
40 | ||
41 | /* The BTS overflow threshold in bytes from the end of the buffer: */ | |
42 | #define BTS_OVFL_TH (BTS_RECORD_SIZE * 64) | |
43 | ||
44 | ||
45 | /* | |
46 | * Bits in the debugctlmsr controlling branch tracing. | |
47 | */ | |
48 | #define X86_DEBUGCTL_TR (1 << 6) | |
49 | #define X86_DEBUGCTL_BTS (1 << 7) | |
50 | #define X86_DEBUGCTL_BTINT (1 << 8) | |
51 | #define X86_DEBUGCTL_BTS_OFF_OS (1 << 9) | |
52 | #define X86_DEBUGCTL_BTS_OFF_USR (1 << 10) | |
53 | ||
54 | /* | |
55 | * A debug store configuration. | |
56 | * | |
57 | * We only support architectures that use 64bit fields. | |
58 | */ | |
59 | struct debug_store { | |
60 | u64 bts_buffer_base; | |
61 | u64 bts_index; | |
62 | u64 bts_absolute_maximum; | |
63 | u64 bts_interrupt_threshold; | |
64 | u64 pebs_buffer_base; | |
65 | u64 pebs_index; | |
66 | u64 pebs_absolute_maximum; | |
67 | u64 pebs_interrupt_threshold; | |
68 | u64 pebs_counter_reset[MAX_PEBS_COUNTERS]; | |
69 | }; | |
70 | ||
241771ef | 71 | struct cpu_hw_counters { |
862a1a5f | 72 | struct perf_counter *counters[X86_PMC_IDX_MAX]; |
43f6201a RR |
73 | unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; |
74 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | |
4b39fd96 | 75 | unsigned long interrupts; |
b0f3f28e | 76 | int enabled; |
30dd568c | 77 | struct debug_store *ds; |
241771ef IM |
78 | }; |
79 | ||
80 | /* | |
5f4ec28f | 81 | * struct x86_pmu - generic x86 pmu |
241771ef | 82 | */ |
5f4ec28f | 83 | struct x86_pmu { |
faa28ae0 RR |
84 | const char *name; |
85 | int version; | |
a3288106 | 86 | int (*handle_irq)(struct pt_regs *); |
9e35ad38 PZ |
87 | void (*disable_all)(void); |
88 | void (*enable_all)(void); | |
7c90cc45 | 89 | void (*enable)(struct hw_perf_counter *, int); |
d4369891 | 90 | void (*disable)(struct hw_perf_counter *, int); |
169e41eb JSR |
91 | unsigned eventsel; |
92 | unsigned perfctr; | |
b0f3f28e PZ |
93 | u64 (*event_map)(int); |
94 | u64 (*raw_event)(u64); | |
169e41eb | 95 | int max_events; |
0933e5c6 RR |
96 | int num_counters; |
97 | int num_counters_fixed; | |
98 | int counter_bits; | |
99 | u64 counter_mask; | |
04da8a43 | 100 | int apic; |
c619b8ff | 101 | u64 max_period; |
9e35ad38 | 102 | u64 intel_ctrl; |
30dd568c MM |
103 | void (*enable_bts)(u64 config); |
104 | void (*disable_bts)(void); | |
b56a3802 JSR |
105 | }; |
106 | ||
4a06bd85 | 107 | static struct x86_pmu x86_pmu __read_mostly; |
b56a3802 | 108 | |
b0f3f28e PZ |
109 | static DEFINE_PER_CPU(struct cpu_hw_counters, cpu_hw_counters) = { |
110 | .enabled = 1, | |
111 | }; | |
241771ef | 112 | |
11d1578f VW |
113 | /* |
114 | * Not sure about some of these | |
115 | */ | |
116 | static const u64 p6_perfmon_event_map[] = | |
117 | { | |
118 | [PERF_COUNT_HW_CPU_CYCLES] = 0x0079, | |
119 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, | |
f64ccccb IM |
120 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0f2e, |
121 | [PERF_COUNT_HW_CACHE_MISSES] = 0x012e, | |
11d1578f VW |
122 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, |
123 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, | |
124 | [PERF_COUNT_HW_BUS_CYCLES] = 0x0062, | |
125 | }; | |
126 | ||
127 | static u64 p6_pmu_event_map(int event) | |
128 | { | |
129 | return p6_perfmon_event_map[event]; | |
130 | } | |
131 | ||
9c74fb50 PZ |
132 | /* |
133 | * Counter setting that is specified not to count anything. | |
134 | * We use this to effectively disable a counter. | |
135 | * | |
136 | * L2_RQSTS with 0 MESI unit mask. | |
137 | */ | |
138 | #define P6_NOP_COUNTER 0x0000002EULL | |
139 | ||
11d1578f VW |
140 | static u64 p6_pmu_raw_event(u64 event) |
141 | { | |
142 | #define P6_EVNTSEL_EVENT_MASK 0x000000FFULL | |
143 | #define P6_EVNTSEL_UNIT_MASK 0x0000FF00ULL | |
144 | #define P6_EVNTSEL_EDGE_MASK 0x00040000ULL | |
145 | #define P6_EVNTSEL_INV_MASK 0x00800000ULL | |
146 | #define P6_EVNTSEL_COUNTER_MASK 0xFF000000ULL | |
147 | ||
148 | #define P6_EVNTSEL_MASK \ | |
149 | (P6_EVNTSEL_EVENT_MASK | \ | |
150 | P6_EVNTSEL_UNIT_MASK | \ | |
151 | P6_EVNTSEL_EDGE_MASK | \ | |
152 | P6_EVNTSEL_INV_MASK | \ | |
153 | P6_EVNTSEL_COUNTER_MASK) | |
154 | ||
155 | return event & P6_EVNTSEL_MASK; | |
156 | } | |
157 | ||
158 | ||
b56a3802 JSR |
159 | /* |
160 | * Intel PerfMon v3. Used on Core2 and later. | |
161 | */ | |
b0f3f28e | 162 | static const u64 intel_perfmon_event_map[] = |
241771ef | 163 | { |
f4dbfa8f PZ |
164 | [PERF_COUNT_HW_CPU_CYCLES] = 0x003c, |
165 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, | |
166 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x4f2e, | |
167 | [PERF_COUNT_HW_CACHE_MISSES] = 0x412e, | |
168 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, | |
169 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, | |
170 | [PERF_COUNT_HW_BUS_CYCLES] = 0x013c, | |
241771ef IM |
171 | }; |
172 | ||
5f4ec28f | 173 | static u64 intel_pmu_event_map(int event) |
b56a3802 JSR |
174 | { |
175 | return intel_perfmon_event_map[event]; | |
176 | } | |
241771ef | 177 | |
8326f44d IM |
178 | /* |
179 | * Generalized hw caching related event table, filled | |
180 | * in on a per model basis. A value of 0 means | |
181 | * 'not supported', -1 means 'event makes no sense on | |
182 | * this CPU', any other value means the raw event | |
183 | * ID. | |
184 | */ | |
185 | ||
186 | #define C(x) PERF_COUNT_HW_CACHE_##x | |
187 | ||
188 | static u64 __read_mostly hw_cache_event_ids | |
189 | [PERF_COUNT_HW_CACHE_MAX] | |
190 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
191 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; | |
192 | ||
193 | static const u64 nehalem_hw_cache_event_ids | |
194 | [PERF_COUNT_HW_CACHE_MAX] | |
195 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
196 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | |
197 | { | |
198 | [ C(L1D) ] = { | |
199 | [ C(OP_READ) ] = { | |
200 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */ | |
201 | [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */ | |
202 | }, | |
203 | [ C(OP_WRITE) ] = { | |
204 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */ | |
205 | [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */ | |
206 | }, | |
207 | [ C(OP_PREFETCH) ] = { | |
208 | [ C(RESULT_ACCESS) ] = 0x014e, /* L1D_PREFETCH.REQUESTS */ | |
209 | [ C(RESULT_MISS) ] = 0x024e, /* L1D_PREFETCH.MISS */ | |
210 | }, | |
211 | }, | |
212 | [ C(L1I ) ] = { | |
213 | [ C(OP_READ) ] = { | |
fecc8ac8 | 214 | [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ |
8326f44d IM |
215 | [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ |
216 | }, | |
217 | [ C(OP_WRITE) ] = { | |
218 | [ C(RESULT_ACCESS) ] = -1, | |
219 | [ C(RESULT_MISS) ] = -1, | |
220 | }, | |
221 | [ C(OP_PREFETCH) ] = { | |
222 | [ C(RESULT_ACCESS) ] = 0x0, | |
223 | [ C(RESULT_MISS) ] = 0x0, | |
224 | }, | |
225 | }, | |
8be6e8f3 | 226 | [ C(LL ) ] = { |
8326f44d IM |
227 | [ C(OP_READ) ] = { |
228 | [ C(RESULT_ACCESS) ] = 0x0324, /* L2_RQSTS.LOADS */ | |
229 | [ C(RESULT_MISS) ] = 0x0224, /* L2_RQSTS.LD_MISS */ | |
230 | }, | |
231 | [ C(OP_WRITE) ] = { | |
232 | [ C(RESULT_ACCESS) ] = 0x0c24, /* L2_RQSTS.RFOS */ | |
233 | [ C(RESULT_MISS) ] = 0x0824, /* L2_RQSTS.RFO_MISS */ | |
234 | }, | |
235 | [ C(OP_PREFETCH) ] = { | |
8be6e8f3 PZ |
236 | [ C(RESULT_ACCESS) ] = 0x4f2e, /* LLC Reference */ |
237 | [ C(RESULT_MISS) ] = 0x412e, /* LLC Misses */ | |
8326f44d IM |
238 | }, |
239 | }, | |
240 | [ C(DTLB) ] = { | |
241 | [ C(OP_READ) ] = { | |
242 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */ | |
243 | [ C(RESULT_MISS) ] = 0x0108, /* DTLB_LOAD_MISSES.ANY */ | |
244 | }, | |
245 | [ C(OP_WRITE) ] = { | |
246 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */ | |
247 | [ C(RESULT_MISS) ] = 0x010c, /* MEM_STORE_RETIRED.DTLB_MISS */ | |
248 | }, | |
249 | [ C(OP_PREFETCH) ] = { | |
250 | [ C(RESULT_ACCESS) ] = 0x0, | |
251 | [ C(RESULT_MISS) ] = 0x0, | |
252 | }, | |
253 | }, | |
254 | [ C(ITLB) ] = { | |
255 | [ C(OP_READ) ] = { | |
256 | [ C(RESULT_ACCESS) ] = 0x01c0, /* INST_RETIRED.ANY_P */ | |
fecc8ac8 | 257 | [ C(RESULT_MISS) ] = 0x20c8, /* ITLB_MISS_RETIRED */ |
8326f44d IM |
258 | }, |
259 | [ C(OP_WRITE) ] = { | |
260 | [ C(RESULT_ACCESS) ] = -1, | |
261 | [ C(RESULT_MISS) ] = -1, | |
262 | }, | |
263 | [ C(OP_PREFETCH) ] = { | |
264 | [ C(RESULT_ACCESS) ] = -1, | |
265 | [ C(RESULT_MISS) ] = -1, | |
266 | }, | |
267 | }, | |
268 | [ C(BPU ) ] = { | |
269 | [ C(OP_READ) ] = { | |
270 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ALL_BRANCHES */ | |
271 | [ C(RESULT_MISS) ] = 0x03e8, /* BPU_CLEARS.ANY */ | |
272 | }, | |
273 | [ C(OP_WRITE) ] = { | |
274 | [ C(RESULT_ACCESS) ] = -1, | |
275 | [ C(RESULT_MISS) ] = -1, | |
276 | }, | |
277 | [ C(OP_PREFETCH) ] = { | |
278 | [ C(RESULT_ACCESS) ] = -1, | |
279 | [ C(RESULT_MISS) ] = -1, | |
280 | }, | |
281 | }, | |
282 | }; | |
283 | ||
284 | static const u64 core2_hw_cache_event_ids | |
285 | [PERF_COUNT_HW_CACHE_MAX] | |
286 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
287 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | |
288 | { | |
0312af84 TG |
289 | [ C(L1D) ] = { |
290 | [ C(OP_READ) ] = { | |
291 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI */ | |
292 | [ C(RESULT_MISS) ] = 0x0140, /* L1D_CACHE_LD.I_STATE */ | |
293 | }, | |
294 | [ C(OP_WRITE) ] = { | |
295 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI */ | |
296 | [ C(RESULT_MISS) ] = 0x0141, /* L1D_CACHE_ST.I_STATE */ | |
297 | }, | |
298 | [ C(OP_PREFETCH) ] = { | |
299 | [ C(RESULT_ACCESS) ] = 0x104e, /* L1D_PREFETCH.REQUESTS */ | |
300 | [ C(RESULT_MISS) ] = 0, | |
301 | }, | |
302 | }, | |
303 | [ C(L1I ) ] = { | |
304 | [ C(OP_READ) ] = { | |
305 | [ C(RESULT_ACCESS) ] = 0x0080, /* L1I.READS */ | |
306 | [ C(RESULT_MISS) ] = 0x0081, /* L1I.MISSES */ | |
307 | }, | |
308 | [ C(OP_WRITE) ] = { | |
309 | [ C(RESULT_ACCESS) ] = -1, | |
310 | [ C(RESULT_MISS) ] = -1, | |
311 | }, | |
312 | [ C(OP_PREFETCH) ] = { | |
313 | [ C(RESULT_ACCESS) ] = 0, | |
314 | [ C(RESULT_MISS) ] = 0, | |
315 | }, | |
316 | }, | |
8be6e8f3 | 317 | [ C(LL ) ] = { |
0312af84 TG |
318 | [ C(OP_READ) ] = { |
319 | [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */ | |
320 | [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */ | |
321 | }, | |
322 | [ C(OP_WRITE) ] = { | |
323 | [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */ | |
324 | [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */ | |
325 | }, | |
326 | [ C(OP_PREFETCH) ] = { | |
327 | [ C(RESULT_ACCESS) ] = 0, | |
328 | [ C(RESULT_MISS) ] = 0, | |
329 | }, | |
330 | }, | |
331 | [ C(DTLB) ] = { | |
332 | [ C(OP_READ) ] = { | |
333 | [ C(RESULT_ACCESS) ] = 0x0f40, /* L1D_CACHE_LD.MESI (alias) */ | |
334 | [ C(RESULT_MISS) ] = 0x0208, /* DTLB_MISSES.MISS_LD */ | |
335 | }, | |
336 | [ C(OP_WRITE) ] = { | |
337 | [ C(RESULT_ACCESS) ] = 0x0f41, /* L1D_CACHE_ST.MESI (alias) */ | |
338 | [ C(RESULT_MISS) ] = 0x0808, /* DTLB_MISSES.MISS_ST */ | |
339 | }, | |
340 | [ C(OP_PREFETCH) ] = { | |
341 | [ C(RESULT_ACCESS) ] = 0, | |
342 | [ C(RESULT_MISS) ] = 0, | |
343 | }, | |
344 | }, | |
345 | [ C(ITLB) ] = { | |
346 | [ C(OP_READ) ] = { | |
347 | [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ | |
348 | [ C(RESULT_MISS) ] = 0x1282, /* ITLBMISSES */ | |
349 | }, | |
350 | [ C(OP_WRITE) ] = { | |
351 | [ C(RESULT_ACCESS) ] = -1, | |
352 | [ C(RESULT_MISS) ] = -1, | |
353 | }, | |
354 | [ C(OP_PREFETCH) ] = { | |
355 | [ C(RESULT_ACCESS) ] = -1, | |
356 | [ C(RESULT_MISS) ] = -1, | |
357 | }, | |
358 | }, | |
359 | [ C(BPU ) ] = { | |
360 | [ C(OP_READ) ] = { | |
361 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ | |
362 | [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ | |
363 | }, | |
364 | [ C(OP_WRITE) ] = { | |
365 | [ C(RESULT_ACCESS) ] = -1, | |
366 | [ C(RESULT_MISS) ] = -1, | |
367 | }, | |
368 | [ C(OP_PREFETCH) ] = { | |
369 | [ C(RESULT_ACCESS) ] = -1, | |
370 | [ C(RESULT_MISS) ] = -1, | |
371 | }, | |
372 | }, | |
8326f44d IM |
373 | }; |
374 | ||
375 | static const u64 atom_hw_cache_event_ids | |
376 | [PERF_COUNT_HW_CACHE_MAX] | |
377 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
378 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | |
379 | { | |
ad689220 TG |
380 | [ C(L1D) ] = { |
381 | [ C(OP_READ) ] = { | |
382 | [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE.LD */ | |
383 | [ C(RESULT_MISS) ] = 0, | |
384 | }, | |
385 | [ C(OP_WRITE) ] = { | |
fecc8ac8 | 386 | [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE.ST */ |
ad689220 TG |
387 | [ C(RESULT_MISS) ] = 0, |
388 | }, | |
389 | [ C(OP_PREFETCH) ] = { | |
390 | [ C(RESULT_ACCESS) ] = 0x0, | |
391 | [ C(RESULT_MISS) ] = 0, | |
392 | }, | |
393 | }, | |
394 | [ C(L1I ) ] = { | |
395 | [ C(OP_READ) ] = { | |
fecc8ac8 YW |
396 | [ C(RESULT_ACCESS) ] = 0x0380, /* L1I.READS */ |
397 | [ C(RESULT_MISS) ] = 0x0280, /* L1I.MISSES */ | |
ad689220 TG |
398 | }, |
399 | [ C(OP_WRITE) ] = { | |
400 | [ C(RESULT_ACCESS) ] = -1, | |
401 | [ C(RESULT_MISS) ] = -1, | |
402 | }, | |
403 | [ C(OP_PREFETCH) ] = { | |
404 | [ C(RESULT_ACCESS) ] = 0, | |
405 | [ C(RESULT_MISS) ] = 0, | |
406 | }, | |
407 | }, | |
8be6e8f3 | 408 | [ C(LL ) ] = { |
ad689220 TG |
409 | [ C(OP_READ) ] = { |
410 | [ C(RESULT_ACCESS) ] = 0x4f29, /* L2_LD.MESI */ | |
411 | [ C(RESULT_MISS) ] = 0x4129, /* L2_LD.ISTATE */ | |
412 | }, | |
413 | [ C(OP_WRITE) ] = { | |
414 | [ C(RESULT_ACCESS) ] = 0x4f2A, /* L2_ST.MESI */ | |
415 | [ C(RESULT_MISS) ] = 0x412A, /* L2_ST.ISTATE */ | |
416 | }, | |
417 | [ C(OP_PREFETCH) ] = { | |
418 | [ C(RESULT_ACCESS) ] = 0, | |
419 | [ C(RESULT_MISS) ] = 0, | |
420 | }, | |
421 | }, | |
422 | [ C(DTLB) ] = { | |
423 | [ C(OP_READ) ] = { | |
fecc8ac8 | 424 | [ C(RESULT_ACCESS) ] = 0x2140, /* L1D_CACHE_LD.MESI (alias) */ |
ad689220 TG |
425 | [ C(RESULT_MISS) ] = 0x0508, /* DTLB_MISSES.MISS_LD */ |
426 | }, | |
427 | [ C(OP_WRITE) ] = { | |
fecc8ac8 | 428 | [ C(RESULT_ACCESS) ] = 0x2240, /* L1D_CACHE_ST.MESI (alias) */ |
ad689220 TG |
429 | [ C(RESULT_MISS) ] = 0x0608, /* DTLB_MISSES.MISS_ST */ |
430 | }, | |
431 | [ C(OP_PREFETCH) ] = { | |
432 | [ C(RESULT_ACCESS) ] = 0, | |
433 | [ C(RESULT_MISS) ] = 0, | |
434 | }, | |
435 | }, | |
436 | [ C(ITLB) ] = { | |
437 | [ C(OP_READ) ] = { | |
438 | [ C(RESULT_ACCESS) ] = 0x00c0, /* INST_RETIRED.ANY_P */ | |
439 | [ C(RESULT_MISS) ] = 0x0282, /* ITLB.MISSES */ | |
440 | }, | |
441 | [ C(OP_WRITE) ] = { | |
442 | [ C(RESULT_ACCESS) ] = -1, | |
443 | [ C(RESULT_MISS) ] = -1, | |
444 | }, | |
445 | [ C(OP_PREFETCH) ] = { | |
446 | [ C(RESULT_ACCESS) ] = -1, | |
447 | [ C(RESULT_MISS) ] = -1, | |
448 | }, | |
449 | }, | |
450 | [ C(BPU ) ] = { | |
451 | [ C(OP_READ) ] = { | |
452 | [ C(RESULT_ACCESS) ] = 0x00c4, /* BR_INST_RETIRED.ANY */ | |
453 | [ C(RESULT_MISS) ] = 0x00c5, /* BP_INST_RETIRED.MISPRED */ | |
454 | }, | |
455 | [ C(OP_WRITE) ] = { | |
456 | [ C(RESULT_ACCESS) ] = -1, | |
457 | [ C(RESULT_MISS) ] = -1, | |
458 | }, | |
459 | [ C(OP_PREFETCH) ] = { | |
460 | [ C(RESULT_ACCESS) ] = -1, | |
461 | [ C(RESULT_MISS) ] = -1, | |
462 | }, | |
463 | }, | |
8326f44d IM |
464 | }; |
465 | ||
5f4ec28f | 466 | static u64 intel_pmu_raw_event(u64 event) |
b0f3f28e | 467 | { |
82bae4f8 PZ |
468 | #define CORE_EVNTSEL_EVENT_MASK 0x000000FFULL |
469 | #define CORE_EVNTSEL_UNIT_MASK 0x0000FF00ULL | |
ff99be57 PZ |
470 | #define CORE_EVNTSEL_EDGE_MASK 0x00040000ULL |
471 | #define CORE_EVNTSEL_INV_MASK 0x00800000ULL | |
82bae4f8 | 472 | #define CORE_EVNTSEL_COUNTER_MASK 0xFF000000ULL |
b0f3f28e | 473 | |
128f048f | 474 | #define CORE_EVNTSEL_MASK \ |
b0f3f28e PZ |
475 | (CORE_EVNTSEL_EVENT_MASK | \ |
476 | CORE_EVNTSEL_UNIT_MASK | \ | |
ff99be57 PZ |
477 | CORE_EVNTSEL_EDGE_MASK | \ |
478 | CORE_EVNTSEL_INV_MASK | \ | |
b0f3f28e PZ |
479 | CORE_EVNTSEL_COUNTER_MASK) |
480 | ||
481 | return event & CORE_EVNTSEL_MASK; | |
482 | } | |
483 | ||
f4db43a3 | 484 | static const u64 amd_hw_cache_event_ids |
f86748e9 TG |
485 | [PERF_COUNT_HW_CACHE_MAX] |
486 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
487 | [PERF_COUNT_HW_CACHE_RESULT_MAX] = | |
488 | { | |
489 | [ C(L1D) ] = { | |
490 | [ C(OP_READ) ] = { | |
f4db43a3 JSR |
491 | [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */ |
492 | [ C(RESULT_MISS) ] = 0x0041, /* Data Cache Misses */ | |
f86748e9 TG |
493 | }, |
494 | [ C(OP_WRITE) ] = { | |
d9f2a5ec | 495 | [ C(RESULT_ACCESS) ] = 0x0142, /* Data Cache Refills :system */ |
f86748e9 TG |
496 | [ C(RESULT_MISS) ] = 0, |
497 | }, | |
498 | [ C(OP_PREFETCH) ] = { | |
f4db43a3 JSR |
499 | [ C(RESULT_ACCESS) ] = 0x0267, /* Data Prefetcher :attempts */ |
500 | [ C(RESULT_MISS) ] = 0x0167, /* Data Prefetcher :cancelled */ | |
f86748e9 TG |
501 | }, |
502 | }, | |
503 | [ C(L1I ) ] = { | |
504 | [ C(OP_READ) ] = { | |
505 | [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction cache fetches */ | |
506 | [ C(RESULT_MISS) ] = 0x0081, /* Instruction cache misses */ | |
507 | }, | |
508 | [ C(OP_WRITE) ] = { | |
509 | [ C(RESULT_ACCESS) ] = -1, | |
510 | [ C(RESULT_MISS) ] = -1, | |
511 | }, | |
512 | [ C(OP_PREFETCH) ] = { | |
f4db43a3 | 513 | [ C(RESULT_ACCESS) ] = 0x014B, /* Prefetch Instructions :Load */ |
f86748e9 TG |
514 | [ C(RESULT_MISS) ] = 0, |
515 | }, | |
516 | }, | |
8be6e8f3 | 517 | [ C(LL ) ] = { |
f86748e9 | 518 | [ C(OP_READ) ] = { |
f4db43a3 JSR |
519 | [ C(RESULT_ACCESS) ] = 0x037D, /* Requests to L2 Cache :IC+DC */ |
520 | [ C(RESULT_MISS) ] = 0x037E, /* L2 Cache Misses : IC+DC */ | |
f86748e9 TG |
521 | }, |
522 | [ C(OP_WRITE) ] = { | |
f4db43a3 | 523 | [ C(RESULT_ACCESS) ] = 0x017F, /* L2 Fill/Writeback */ |
f86748e9 TG |
524 | [ C(RESULT_MISS) ] = 0, |
525 | }, | |
526 | [ C(OP_PREFETCH) ] = { | |
527 | [ C(RESULT_ACCESS) ] = 0, | |
528 | [ C(RESULT_MISS) ] = 0, | |
529 | }, | |
530 | }, | |
531 | [ C(DTLB) ] = { | |
532 | [ C(OP_READ) ] = { | |
f4db43a3 JSR |
533 | [ C(RESULT_ACCESS) ] = 0x0040, /* Data Cache Accesses */ |
534 | [ C(RESULT_MISS) ] = 0x0046, /* L1 DTLB and L2 DLTB Miss */ | |
f86748e9 TG |
535 | }, |
536 | [ C(OP_WRITE) ] = { | |
537 | [ C(RESULT_ACCESS) ] = 0, | |
538 | [ C(RESULT_MISS) ] = 0, | |
539 | }, | |
540 | [ C(OP_PREFETCH) ] = { | |
541 | [ C(RESULT_ACCESS) ] = 0, | |
542 | [ C(RESULT_MISS) ] = 0, | |
543 | }, | |
544 | }, | |
545 | [ C(ITLB) ] = { | |
546 | [ C(OP_READ) ] = { | |
547 | [ C(RESULT_ACCESS) ] = 0x0080, /* Instruction fecthes */ | |
548 | [ C(RESULT_MISS) ] = 0x0085, /* Instr. fetch ITLB misses */ | |
549 | }, | |
550 | [ C(OP_WRITE) ] = { | |
551 | [ C(RESULT_ACCESS) ] = -1, | |
552 | [ C(RESULT_MISS) ] = -1, | |
553 | }, | |
554 | [ C(OP_PREFETCH) ] = { | |
555 | [ C(RESULT_ACCESS) ] = -1, | |
556 | [ C(RESULT_MISS) ] = -1, | |
557 | }, | |
558 | }, | |
559 | [ C(BPU ) ] = { | |
560 | [ C(OP_READ) ] = { | |
561 | [ C(RESULT_ACCESS) ] = 0x00c2, /* Retired Branch Instr. */ | |
562 | [ C(RESULT_MISS) ] = 0x00c3, /* Retired Mispredicted BI */ | |
563 | }, | |
564 | [ C(OP_WRITE) ] = { | |
565 | [ C(RESULT_ACCESS) ] = -1, | |
566 | [ C(RESULT_MISS) ] = -1, | |
567 | }, | |
568 | [ C(OP_PREFETCH) ] = { | |
569 | [ C(RESULT_ACCESS) ] = -1, | |
570 | [ C(RESULT_MISS) ] = -1, | |
571 | }, | |
572 | }, | |
573 | }; | |
574 | ||
f87ad35d JSR |
575 | /* |
576 | * AMD Performance Monitor K7 and later. | |
577 | */ | |
b0f3f28e | 578 | static const u64 amd_perfmon_event_map[] = |
f87ad35d | 579 | { |
f4dbfa8f PZ |
580 | [PERF_COUNT_HW_CPU_CYCLES] = 0x0076, |
581 | [PERF_COUNT_HW_INSTRUCTIONS] = 0x00c0, | |
582 | [PERF_COUNT_HW_CACHE_REFERENCES] = 0x0080, | |
583 | [PERF_COUNT_HW_CACHE_MISSES] = 0x0081, | |
584 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = 0x00c4, | |
585 | [PERF_COUNT_HW_BRANCH_MISSES] = 0x00c5, | |
f87ad35d JSR |
586 | }; |
587 | ||
5f4ec28f | 588 | static u64 amd_pmu_event_map(int event) |
f87ad35d JSR |
589 | { |
590 | return amd_perfmon_event_map[event]; | |
591 | } | |
592 | ||
5f4ec28f | 593 | static u64 amd_pmu_raw_event(u64 event) |
b0f3f28e | 594 | { |
82bae4f8 PZ |
595 | #define K7_EVNTSEL_EVENT_MASK 0x7000000FFULL |
596 | #define K7_EVNTSEL_UNIT_MASK 0x00000FF00ULL | |
ff99be57 PZ |
597 | #define K7_EVNTSEL_EDGE_MASK 0x000040000ULL |
598 | #define K7_EVNTSEL_INV_MASK 0x000800000ULL | |
82bae4f8 | 599 | #define K7_EVNTSEL_COUNTER_MASK 0x0FF000000ULL |
b0f3f28e PZ |
600 | |
601 | #define K7_EVNTSEL_MASK \ | |
602 | (K7_EVNTSEL_EVENT_MASK | \ | |
603 | K7_EVNTSEL_UNIT_MASK | \ | |
ff99be57 PZ |
604 | K7_EVNTSEL_EDGE_MASK | \ |
605 | K7_EVNTSEL_INV_MASK | \ | |
b0f3f28e PZ |
606 | K7_EVNTSEL_COUNTER_MASK) |
607 | ||
608 | return event & K7_EVNTSEL_MASK; | |
609 | } | |
610 | ||
ee06094f IM |
611 | /* |
612 | * Propagate counter elapsed time into the generic counter. | |
613 | * Can only be executed on the CPU where the counter is active. | |
614 | * Returns the delta events processed. | |
615 | */ | |
4b7bfd0d | 616 | static u64 |
ee06094f IM |
617 | x86_perf_counter_update(struct perf_counter *counter, |
618 | struct hw_perf_counter *hwc, int idx) | |
619 | { | |
ec3232bd PZ |
620 | int shift = 64 - x86_pmu.counter_bits; |
621 | u64 prev_raw_count, new_raw_count; | |
622 | s64 delta; | |
ee06094f | 623 | |
30dd568c MM |
624 | if (idx == X86_PMC_IDX_FIXED_BTS) |
625 | return 0; | |
626 | ||
ee06094f IM |
627 | /* |
628 | * Careful: an NMI might modify the previous counter value. | |
629 | * | |
630 | * Our tactic to handle this is to first atomically read and | |
631 | * exchange a new raw count - then add that new-prev delta | |
632 | * count to the generic counter atomically: | |
633 | */ | |
634 | again: | |
635 | prev_raw_count = atomic64_read(&hwc->prev_count); | |
636 | rdmsrl(hwc->counter_base + idx, new_raw_count); | |
637 | ||
638 | if (atomic64_cmpxchg(&hwc->prev_count, prev_raw_count, | |
639 | new_raw_count) != prev_raw_count) | |
640 | goto again; | |
641 | ||
642 | /* | |
643 | * Now we have the new raw value and have updated the prev | |
644 | * timestamp already. We can now calculate the elapsed delta | |
645 | * (counter-)time and add that to the generic counter. | |
646 | * | |
647 | * Careful, not all hw sign-extends above the physical width | |
ec3232bd | 648 | * of the count. |
ee06094f | 649 | */ |
ec3232bd PZ |
650 | delta = (new_raw_count << shift) - (prev_raw_count << shift); |
651 | delta >>= shift; | |
ee06094f IM |
652 | |
653 | atomic64_add(delta, &counter->count); | |
654 | atomic64_sub(delta, &hwc->period_left); | |
4b7bfd0d RR |
655 | |
656 | return new_raw_count; | |
ee06094f IM |
657 | } |
658 | ||
ba77813a | 659 | static atomic_t active_counters; |
4e935e47 PZ |
660 | static DEFINE_MUTEX(pmc_reserve_mutex); |
661 | ||
662 | static bool reserve_pmc_hardware(void) | |
663 | { | |
04da8a43 | 664 | #ifdef CONFIG_X86_LOCAL_APIC |
4e935e47 PZ |
665 | int i; |
666 | ||
667 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
668 | disable_lapic_nmi_watchdog(); | |
669 | ||
0933e5c6 | 670 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 | 671 | if (!reserve_perfctr_nmi(x86_pmu.perfctr + i)) |
4e935e47 PZ |
672 | goto perfctr_fail; |
673 | } | |
674 | ||
0933e5c6 | 675 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 | 676 | if (!reserve_evntsel_nmi(x86_pmu.eventsel + i)) |
4e935e47 PZ |
677 | goto eventsel_fail; |
678 | } | |
04da8a43 | 679 | #endif |
4e935e47 PZ |
680 | |
681 | return true; | |
682 | ||
04da8a43 | 683 | #ifdef CONFIG_X86_LOCAL_APIC |
4e935e47 PZ |
684 | eventsel_fail: |
685 | for (i--; i >= 0; i--) | |
4a06bd85 | 686 | release_evntsel_nmi(x86_pmu.eventsel + i); |
4e935e47 | 687 | |
0933e5c6 | 688 | i = x86_pmu.num_counters; |
4e935e47 PZ |
689 | |
690 | perfctr_fail: | |
691 | for (i--; i >= 0; i--) | |
4a06bd85 | 692 | release_perfctr_nmi(x86_pmu.perfctr + i); |
4e935e47 PZ |
693 | |
694 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
695 | enable_lapic_nmi_watchdog(); | |
696 | ||
697 | return false; | |
04da8a43 | 698 | #endif |
4e935e47 PZ |
699 | } |
700 | ||
701 | static void release_pmc_hardware(void) | |
702 | { | |
04da8a43 | 703 | #ifdef CONFIG_X86_LOCAL_APIC |
4e935e47 PZ |
704 | int i; |
705 | ||
0933e5c6 | 706 | for (i = 0; i < x86_pmu.num_counters; i++) { |
4a06bd85 RR |
707 | release_perfctr_nmi(x86_pmu.perfctr + i); |
708 | release_evntsel_nmi(x86_pmu.eventsel + i); | |
4e935e47 PZ |
709 | } |
710 | ||
711 | if (nmi_watchdog == NMI_LOCAL_APIC) | |
712 | enable_lapic_nmi_watchdog(); | |
04da8a43 | 713 | #endif |
4e935e47 PZ |
714 | } |
715 | ||
30dd568c MM |
716 | static inline bool bts_available(void) |
717 | { | |
718 | return x86_pmu.enable_bts != NULL; | |
719 | } | |
720 | ||
721 | static inline void init_debug_store_on_cpu(int cpu) | |
722 | { | |
723 | struct debug_store *ds = per_cpu(cpu_hw_counters, cpu).ds; | |
724 | ||
725 | if (!ds) | |
726 | return; | |
727 | ||
728 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, | |
729 | (u32)((u64)(long)ds), (u32)((u64)(long)ds >> 32)); | |
730 | } | |
731 | ||
732 | static inline void fini_debug_store_on_cpu(int cpu) | |
733 | { | |
734 | if (!per_cpu(cpu_hw_counters, cpu).ds) | |
735 | return; | |
736 | ||
737 | wrmsr_on_cpu(cpu, MSR_IA32_DS_AREA, 0, 0); | |
738 | } | |
739 | ||
740 | static void release_bts_hardware(void) | |
741 | { | |
742 | int cpu; | |
743 | ||
744 | if (!bts_available()) | |
745 | return; | |
746 | ||
747 | get_online_cpus(); | |
748 | ||
749 | for_each_online_cpu(cpu) | |
750 | fini_debug_store_on_cpu(cpu); | |
751 | ||
752 | for_each_possible_cpu(cpu) { | |
753 | struct debug_store *ds = per_cpu(cpu_hw_counters, cpu).ds; | |
754 | ||
755 | if (!ds) | |
756 | continue; | |
757 | ||
758 | per_cpu(cpu_hw_counters, cpu).ds = NULL; | |
759 | ||
760 | kfree((void *)(long)ds->bts_buffer_base); | |
761 | kfree(ds); | |
762 | } | |
763 | ||
764 | put_online_cpus(); | |
765 | } | |
766 | ||
767 | static int reserve_bts_hardware(void) | |
768 | { | |
769 | int cpu, err = 0; | |
770 | ||
771 | if (!bts_available()) | |
747b50aa | 772 | return 0; |
30dd568c MM |
773 | |
774 | get_online_cpus(); | |
775 | ||
776 | for_each_possible_cpu(cpu) { | |
777 | struct debug_store *ds; | |
778 | void *buffer; | |
779 | ||
780 | err = -ENOMEM; | |
781 | buffer = kzalloc(BTS_BUFFER_SIZE, GFP_KERNEL); | |
782 | if (unlikely(!buffer)) | |
783 | break; | |
784 | ||
785 | ds = kzalloc(sizeof(*ds), GFP_KERNEL); | |
786 | if (unlikely(!ds)) { | |
787 | kfree(buffer); | |
788 | break; | |
789 | } | |
790 | ||
791 | ds->bts_buffer_base = (u64)(long)buffer; | |
792 | ds->bts_index = ds->bts_buffer_base; | |
793 | ds->bts_absolute_maximum = | |
794 | ds->bts_buffer_base + BTS_BUFFER_SIZE; | |
795 | ds->bts_interrupt_threshold = | |
796 | ds->bts_absolute_maximum - BTS_OVFL_TH; | |
797 | ||
798 | per_cpu(cpu_hw_counters, cpu).ds = ds; | |
799 | err = 0; | |
800 | } | |
801 | ||
802 | if (err) | |
803 | release_bts_hardware(); | |
804 | else { | |
805 | for_each_online_cpu(cpu) | |
806 | init_debug_store_on_cpu(cpu); | |
807 | } | |
808 | ||
809 | put_online_cpus(); | |
810 | ||
811 | return err; | |
812 | } | |
813 | ||
4e935e47 PZ |
814 | static void hw_perf_counter_destroy(struct perf_counter *counter) |
815 | { | |
ba77813a | 816 | if (atomic_dec_and_mutex_lock(&active_counters, &pmc_reserve_mutex)) { |
4e935e47 | 817 | release_pmc_hardware(); |
30dd568c | 818 | release_bts_hardware(); |
4e935e47 PZ |
819 | mutex_unlock(&pmc_reserve_mutex); |
820 | } | |
821 | } | |
822 | ||
85cf9dba RR |
823 | static inline int x86_pmu_initialized(void) |
824 | { | |
825 | return x86_pmu.handle_irq != NULL; | |
826 | } | |
827 | ||
8326f44d IM |
828 | static inline int |
829 | set_ext_hw_attr(struct hw_perf_counter *hwc, struct perf_counter_attr *attr) | |
830 | { | |
831 | unsigned int cache_type, cache_op, cache_result; | |
832 | u64 config, val; | |
833 | ||
834 | config = attr->config; | |
835 | ||
836 | cache_type = (config >> 0) & 0xff; | |
837 | if (cache_type >= PERF_COUNT_HW_CACHE_MAX) | |
838 | return -EINVAL; | |
839 | ||
840 | cache_op = (config >> 8) & 0xff; | |
841 | if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX) | |
842 | return -EINVAL; | |
843 | ||
844 | cache_result = (config >> 16) & 0xff; | |
845 | if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX) | |
846 | return -EINVAL; | |
847 | ||
848 | val = hw_cache_event_ids[cache_type][cache_op][cache_result]; | |
849 | ||
850 | if (val == 0) | |
851 | return -ENOENT; | |
852 | ||
853 | if (val == -1) | |
854 | return -EINVAL; | |
855 | ||
856 | hwc->config |= val; | |
857 | ||
858 | return 0; | |
859 | } | |
860 | ||
30dd568c MM |
861 | static void intel_pmu_enable_bts(u64 config) |
862 | { | |
863 | unsigned long debugctlmsr; | |
864 | ||
865 | debugctlmsr = get_debugctlmsr(); | |
866 | ||
867 | debugctlmsr |= X86_DEBUGCTL_TR; | |
868 | debugctlmsr |= X86_DEBUGCTL_BTS; | |
869 | debugctlmsr |= X86_DEBUGCTL_BTINT; | |
870 | ||
871 | if (!(config & ARCH_PERFMON_EVENTSEL_OS)) | |
872 | debugctlmsr |= X86_DEBUGCTL_BTS_OFF_OS; | |
873 | ||
874 | if (!(config & ARCH_PERFMON_EVENTSEL_USR)) | |
875 | debugctlmsr |= X86_DEBUGCTL_BTS_OFF_USR; | |
876 | ||
877 | update_debugctlmsr(debugctlmsr); | |
878 | } | |
879 | ||
880 | static void intel_pmu_disable_bts(void) | |
881 | { | |
882 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
883 | unsigned long debugctlmsr; | |
884 | ||
885 | if (!cpuc->ds) | |
886 | return; | |
887 | ||
888 | debugctlmsr = get_debugctlmsr(); | |
889 | ||
890 | debugctlmsr &= | |
891 | ~(X86_DEBUGCTL_TR | X86_DEBUGCTL_BTS | X86_DEBUGCTL_BTINT | | |
892 | X86_DEBUGCTL_BTS_OFF_OS | X86_DEBUGCTL_BTS_OFF_USR); | |
893 | ||
894 | update_debugctlmsr(debugctlmsr); | |
895 | } | |
896 | ||
241771ef | 897 | /* |
0d48696f | 898 | * Setup the hardware configuration for a given attr_type |
241771ef | 899 | */ |
621a01ea | 900 | static int __hw_perf_counter_init(struct perf_counter *counter) |
241771ef | 901 | { |
0d48696f | 902 | struct perf_counter_attr *attr = &counter->attr; |
241771ef | 903 | struct hw_perf_counter *hwc = &counter->hw; |
9c74fb50 | 904 | u64 config; |
4e935e47 | 905 | int err; |
241771ef | 906 | |
85cf9dba RR |
907 | if (!x86_pmu_initialized()) |
908 | return -ENODEV; | |
241771ef | 909 | |
4e935e47 | 910 | err = 0; |
ba77813a | 911 | if (!atomic_inc_not_zero(&active_counters)) { |
4e935e47 | 912 | mutex_lock(&pmc_reserve_mutex); |
30dd568c MM |
913 | if (atomic_read(&active_counters) == 0) { |
914 | if (!reserve_pmc_hardware()) | |
915 | err = -EBUSY; | |
916 | else | |
747b50aa | 917 | err = reserve_bts_hardware(); |
30dd568c MM |
918 | } |
919 | if (!err) | |
ba77813a | 920 | atomic_inc(&active_counters); |
4e935e47 PZ |
921 | mutex_unlock(&pmc_reserve_mutex); |
922 | } | |
923 | if (err) | |
924 | return err; | |
925 | ||
241771ef | 926 | /* |
0475f9ea | 927 | * Generate PMC IRQs: |
241771ef IM |
928 | * (keep 'enabled' bit clear for now) |
929 | */ | |
0475f9ea | 930 | hwc->config = ARCH_PERFMON_EVENTSEL_INT; |
241771ef IM |
931 | |
932 | /* | |
0475f9ea | 933 | * Count user and OS events unless requested not to. |
241771ef | 934 | */ |
0d48696f | 935 | if (!attr->exclude_user) |
0475f9ea | 936 | hwc->config |= ARCH_PERFMON_EVENTSEL_USR; |
0d48696f | 937 | if (!attr->exclude_kernel) |
241771ef | 938 | hwc->config |= ARCH_PERFMON_EVENTSEL_OS; |
0475f9ea | 939 | |
bd2b5b12 | 940 | if (!hwc->sample_period) { |
b23f3325 | 941 | hwc->sample_period = x86_pmu.max_period; |
9e350de3 | 942 | hwc->last_period = hwc->sample_period; |
bd2b5b12 | 943 | atomic64_set(&hwc->period_left, hwc->sample_period); |
04da8a43 IM |
944 | } else { |
945 | /* | |
946 | * If we have a PMU initialized but no APIC | |
947 | * interrupts, we cannot sample hardware | |
948 | * counters (user-space has to fall back and | |
949 | * sample via a hrtimer based software counter): | |
950 | */ | |
951 | if (!x86_pmu.apic) | |
952 | return -EOPNOTSUPP; | |
bd2b5b12 | 953 | } |
d2517a49 | 954 | |
8326f44d | 955 | counter->destroy = hw_perf_counter_destroy; |
241771ef IM |
956 | |
957 | /* | |
dfa7c899 | 958 | * Raw event type provide the config in the event structure |
241771ef | 959 | */ |
a21ca2ca IM |
960 | if (attr->type == PERF_TYPE_RAW) { |
961 | hwc->config |= x86_pmu.raw_event(attr->config); | |
8326f44d | 962 | return 0; |
241771ef | 963 | } |
241771ef | 964 | |
8326f44d IM |
965 | if (attr->type == PERF_TYPE_HW_CACHE) |
966 | return set_ext_hw_attr(hwc, attr); | |
967 | ||
968 | if (attr->config >= x86_pmu.max_events) | |
969 | return -EINVAL; | |
9c74fb50 | 970 | |
8326f44d IM |
971 | /* |
972 | * The generic map: | |
973 | */ | |
9c74fb50 PZ |
974 | config = x86_pmu.event_map(attr->config); |
975 | ||
976 | if (config == 0) | |
977 | return -ENOENT; | |
978 | ||
979 | if (config == -1LL) | |
980 | return -EINVAL; | |
981 | ||
747b50aa | 982 | /* |
983 | * Branch tracing: | |
984 | */ | |
985 | if ((attr->config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) && | |
986 | (hwc->sample_period == 1) && !bts_available()) | |
987 | return -EOPNOTSUPP; | |
988 | ||
9c74fb50 | 989 | hwc->config |= config; |
4e935e47 | 990 | |
241771ef IM |
991 | return 0; |
992 | } | |
993 | ||
11d1578f VW |
994 | static void p6_pmu_disable_all(void) |
995 | { | |
996 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
9c74fb50 | 997 | u64 val; |
11d1578f VW |
998 | |
999 | if (!cpuc->enabled) | |
1000 | return; | |
1001 | ||
1002 | cpuc->enabled = 0; | |
1003 | barrier(); | |
1004 | ||
1005 | /* p6 only has one enable register */ | |
1006 | rdmsrl(MSR_P6_EVNTSEL0, val); | |
1007 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; | |
1008 | wrmsrl(MSR_P6_EVNTSEL0, val); | |
1009 | } | |
1010 | ||
9e35ad38 | 1011 | static void intel_pmu_disable_all(void) |
4ac13294 | 1012 | { |
30dd568c MM |
1013 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
1014 | ||
1015 | if (!cpuc->enabled) | |
1016 | return; | |
1017 | ||
1018 | cpuc->enabled = 0; | |
1019 | barrier(); | |
1020 | ||
862a1a5f | 1021 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, 0); |
30dd568c MM |
1022 | |
1023 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) | |
1024 | intel_pmu_disable_bts(); | |
241771ef | 1025 | } |
b56a3802 | 1026 | |
9e35ad38 | 1027 | static void amd_pmu_disable_all(void) |
f87ad35d | 1028 | { |
b0f3f28e | 1029 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
9e35ad38 PZ |
1030 | int idx; |
1031 | ||
1032 | if (!cpuc->enabled) | |
1033 | return; | |
b0f3f28e | 1034 | |
b0f3f28e | 1035 | cpuc->enabled = 0; |
60b3df9c PZ |
1036 | /* |
1037 | * ensure we write the disable before we start disabling the | |
5f4ec28f RR |
1038 | * counters proper, so that amd_pmu_enable_counter() does the |
1039 | * right thing. | |
60b3df9c | 1040 | */ |
b0f3f28e | 1041 | barrier(); |
f87ad35d | 1042 | |
0933e5c6 | 1043 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
b0f3f28e PZ |
1044 | u64 val; |
1045 | ||
43f6201a | 1046 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 1047 | continue; |
f87ad35d | 1048 | rdmsrl(MSR_K7_EVNTSEL0 + idx, val); |
4295ee62 RR |
1049 | if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE)) |
1050 | continue; | |
1051 | val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE; | |
1052 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); | |
f87ad35d | 1053 | } |
f87ad35d JSR |
1054 | } |
1055 | ||
9e35ad38 | 1056 | void hw_perf_disable(void) |
b56a3802 | 1057 | { |
85cf9dba | 1058 | if (!x86_pmu_initialized()) |
9e35ad38 PZ |
1059 | return; |
1060 | return x86_pmu.disable_all(); | |
b56a3802 | 1061 | } |
241771ef | 1062 | |
11d1578f VW |
1063 | static void p6_pmu_enable_all(void) |
1064 | { | |
1065 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
1066 | unsigned long val; | |
1067 | ||
1068 | if (cpuc->enabled) | |
1069 | return; | |
1070 | ||
1071 | cpuc->enabled = 1; | |
1072 | barrier(); | |
1073 | ||
1074 | /* p6 only has one enable register */ | |
1075 | rdmsrl(MSR_P6_EVNTSEL0, val); | |
1076 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; | |
1077 | wrmsrl(MSR_P6_EVNTSEL0, val); | |
1078 | } | |
1079 | ||
9e35ad38 | 1080 | static void intel_pmu_enable_all(void) |
b56a3802 | 1081 | { |
30dd568c MM |
1082 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
1083 | ||
1084 | if (cpuc->enabled) | |
1085 | return; | |
1086 | ||
1087 | cpuc->enabled = 1; | |
1088 | barrier(); | |
1089 | ||
9e35ad38 | 1090 | wrmsrl(MSR_CORE_PERF_GLOBAL_CTRL, x86_pmu.intel_ctrl); |
30dd568c MM |
1091 | |
1092 | if (test_bit(X86_PMC_IDX_FIXED_BTS, cpuc->active_mask)) { | |
1093 | struct perf_counter *counter = | |
1094 | cpuc->counters[X86_PMC_IDX_FIXED_BTS]; | |
1095 | ||
1096 | if (WARN_ON_ONCE(!counter)) | |
1097 | return; | |
1098 | ||
1099 | intel_pmu_enable_bts(counter->hw.config); | |
1100 | } | |
b56a3802 JSR |
1101 | } |
1102 | ||
9e35ad38 | 1103 | static void amd_pmu_enable_all(void) |
f87ad35d | 1104 | { |
b0f3f28e | 1105 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); |
f87ad35d JSR |
1106 | int idx; |
1107 | ||
9e35ad38 | 1108 | if (cpuc->enabled) |
b0f3f28e PZ |
1109 | return; |
1110 | ||
9e35ad38 PZ |
1111 | cpuc->enabled = 1; |
1112 | barrier(); | |
1113 | ||
0933e5c6 | 1114 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
984b838c | 1115 | struct perf_counter *counter = cpuc->counters[idx]; |
4295ee62 | 1116 | u64 val; |
b0f3f28e | 1117 | |
43f6201a | 1118 | if (!test_bit(idx, cpuc->active_mask)) |
4295ee62 | 1119 | continue; |
984b838c PZ |
1120 | |
1121 | val = counter->hw.config; | |
4295ee62 RR |
1122 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
1123 | wrmsrl(MSR_K7_EVNTSEL0 + idx, val); | |
f87ad35d JSR |
1124 | } |
1125 | } | |
1126 | ||
9e35ad38 | 1127 | void hw_perf_enable(void) |
ee06094f | 1128 | { |
85cf9dba | 1129 | if (!x86_pmu_initialized()) |
2b9ff0db | 1130 | return; |
9e35ad38 | 1131 | x86_pmu.enable_all(); |
ee06094f | 1132 | } |
ee06094f | 1133 | |
19d84dab | 1134 | static inline u64 intel_pmu_get_status(void) |
b0f3f28e PZ |
1135 | { |
1136 | u64 status; | |
1137 | ||
b7f8859a | 1138 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); |
b0f3f28e | 1139 | |
b7f8859a | 1140 | return status; |
b0f3f28e PZ |
1141 | } |
1142 | ||
dee5d906 | 1143 | static inline void intel_pmu_ack_status(u64 ack) |
b0f3f28e PZ |
1144 | { |
1145 | wrmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, ack); | |
1146 | } | |
1147 | ||
7c90cc45 | 1148 | static inline void x86_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) |
b0f3f28e | 1149 | { |
11d1578f | 1150 | (void)checking_wrmsrl(hwc->config_base + idx, |
7c90cc45 | 1151 | hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE); |
b0f3f28e PZ |
1152 | } |
1153 | ||
d4369891 | 1154 | static inline void x86_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) |
b0f3f28e | 1155 | { |
11d1578f | 1156 | (void)checking_wrmsrl(hwc->config_base + idx, hwc->config); |
b0f3f28e PZ |
1157 | } |
1158 | ||
2f18d1e8 | 1159 | static inline void |
d4369891 | 1160 | intel_pmu_disable_fixed(struct hw_perf_counter *hwc, int __idx) |
2f18d1e8 IM |
1161 | { |
1162 | int idx = __idx - X86_PMC_IDX_FIXED; | |
1163 | u64 ctrl_val, mask; | |
2f18d1e8 IM |
1164 | |
1165 | mask = 0xfULL << (idx * 4); | |
1166 | ||
1167 | rdmsrl(hwc->config_base, ctrl_val); | |
1168 | ctrl_val &= ~mask; | |
11d1578f VW |
1169 | (void)checking_wrmsrl(hwc->config_base, ctrl_val); |
1170 | } | |
1171 | ||
1172 | static inline void | |
1173 | p6_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) | |
1174 | { | |
1175 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
9c74fb50 | 1176 | u64 val = P6_NOP_COUNTER; |
11d1578f | 1177 | |
9c74fb50 PZ |
1178 | if (cpuc->enabled) |
1179 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; | |
11d1578f VW |
1180 | |
1181 | (void)checking_wrmsrl(hwc->config_base + idx, val); | |
2f18d1e8 IM |
1182 | } |
1183 | ||
7e2ae347 | 1184 | static inline void |
d4369891 | 1185 | intel_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) |
7e2ae347 | 1186 | { |
30dd568c MM |
1187 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) { |
1188 | intel_pmu_disable_bts(); | |
1189 | return; | |
1190 | } | |
1191 | ||
d4369891 RR |
1192 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
1193 | intel_pmu_disable_fixed(hwc, idx); | |
1194 | return; | |
1195 | } | |
1196 | ||
1197 | x86_pmu_disable_counter(hwc, idx); | |
1198 | } | |
1199 | ||
1200 | static inline void | |
1201 | amd_pmu_disable_counter(struct hw_perf_counter *hwc, int idx) | |
1202 | { | |
1203 | x86_pmu_disable_counter(hwc, idx); | |
7e2ae347 IM |
1204 | } |
1205 | ||
2f18d1e8 | 1206 | static DEFINE_PER_CPU(u64, prev_left[X86_PMC_IDX_MAX]); |
241771ef | 1207 | |
ee06094f IM |
1208 | /* |
1209 | * Set the next IRQ period, based on the hwc->period_left value. | |
1210 | * To be called with the counter disabled in hw: | |
1211 | */ | |
e4abb5d4 | 1212 | static int |
26816c28 | 1213 | x86_perf_counter_set_period(struct perf_counter *counter, |
ee06094f | 1214 | struct hw_perf_counter *hwc, int idx) |
241771ef | 1215 | { |
2f18d1e8 | 1216 | s64 left = atomic64_read(&hwc->period_left); |
e4abb5d4 PZ |
1217 | s64 period = hwc->sample_period; |
1218 | int err, ret = 0; | |
ee06094f | 1219 | |
30dd568c MM |
1220 | if (idx == X86_PMC_IDX_FIXED_BTS) |
1221 | return 0; | |
1222 | ||
ee06094f IM |
1223 | /* |
1224 | * If we are way outside a reasoable range then just skip forward: | |
1225 | */ | |
1226 | if (unlikely(left <= -period)) { | |
1227 | left = period; | |
1228 | atomic64_set(&hwc->period_left, left); | |
9e350de3 | 1229 | hwc->last_period = period; |
e4abb5d4 | 1230 | ret = 1; |
ee06094f IM |
1231 | } |
1232 | ||
1233 | if (unlikely(left <= 0)) { | |
1234 | left += period; | |
1235 | atomic64_set(&hwc->period_left, left); | |
9e350de3 | 1236 | hwc->last_period = period; |
e4abb5d4 | 1237 | ret = 1; |
ee06094f | 1238 | } |
1c80f4b5 IM |
1239 | /* |
1240 | * Quirk: certain CPUs dont like it if just 1 event is left: | |
1241 | */ | |
1242 | if (unlikely(left < 2)) | |
1243 | left = 2; | |
241771ef | 1244 | |
e4abb5d4 PZ |
1245 | if (left > x86_pmu.max_period) |
1246 | left = x86_pmu.max_period; | |
1247 | ||
ee06094f IM |
1248 | per_cpu(prev_left[idx], smp_processor_id()) = left; |
1249 | ||
1250 | /* | |
1251 | * The hw counter starts counting from this counter offset, | |
1252 | * mark it to be able to extra future deltas: | |
1253 | */ | |
2f18d1e8 | 1254 | atomic64_set(&hwc->prev_count, (u64)-left); |
ee06094f | 1255 | |
2f18d1e8 | 1256 | err = checking_wrmsrl(hwc->counter_base + idx, |
0933e5c6 | 1257 | (u64)(-left) & x86_pmu.counter_mask); |
e4abb5d4 | 1258 | |
194002b2 PZ |
1259 | perf_counter_update_userpage(counter); |
1260 | ||
e4abb5d4 | 1261 | return ret; |
2f18d1e8 IM |
1262 | } |
1263 | ||
1264 | static inline void | |
7c90cc45 | 1265 | intel_pmu_enable_fixed(struct hw_perf_counter *hwc, int __idx) |
2f18d1e8 IM |
1266 | { |
1267 | int idx = __idx - X86_PMC_IDX_FIXED; | |
1268 | u64 ctrl_val, bits, mask; | |
1269 | int err; | |
1270 | ||
1271 | /* | |
0475f9ea PM |
1272 | * Enable IRQ generation (0x8), |
1273 | * and enable ring-3 counting (0x2) and ring-0 counting (0x1) | |
1274 | * if requested: | |
2f18d1e8 | 1275 | */ |
0475f9ea PM |
1276 | bits = 0x8ULL; |
1277 | if (hwc->config & ARCH_PERFMON_EVENTSEL_USR) | |
1278 | bits |= 0x2; | |
2f18d1e8 IM |
1279 | if (hwc->config & ARCH_PERFMON_EVENTSEL_OS) |
1280 | bits |= 0x1; | |
1281 | bits <<= (idx * 4); | |
1282 | mask = 0xfULL << (idx * 4); | |
1283 | ||
1284 | rdmsrl(hwc->config_base, ctrl_val); | |
1285 | ctrl_val &= ~mask; | |
1286 | ctrl_val |= bits; | |
1287 | err = checking_wrmsrl(hwc->config_base, ctrl_val); | |
7e2ae347 IM |
1288 | } |
1289 | ||
11d1578f VW |
1290 | static void p6_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) |
1291 | { | |
1292 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
984b838c | 1293 | u64 val; |
11d1578f | 1294 | |
984b838c | 1295 | val = hwc->config; |
11d1578f | 1296 | if (cpuc->enabled) |
984b838c PZ |
1297 | val |= ARCH_PERFMON_EVENTSEL0_ENABLE; |
1298 | ||
1299 | (void)checking_wrmsrl(hwc->config_base + idx, val); | |
11d1578f VW |
1300 | } |
1301 | ||
1302 | ||
7c90cc45 | 1303 | static void intel_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) |
7e2ae347 | 1304 | { |
30dd568c MM |
1305 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) { |
1306 | if (!__get_cpu_var(cpu_hw_counters).enabled) | |
1307 | return; | |
1308 | ||
1309 | intel_pmu_enable_bts(hwc->config); | |
1310 | return; | |
1311 | } | |
1312 | ||
7c90cc45 RR |
1313 | if (unlikely(hwc->config_base == MSR_ARCH_PERFMON_FIXED_CTR_CTRL)) { |
1314 | intel_pmu_enable_fixed(hwc, idx); | |
1315 | return; | |
1316 | } | |
1317 | ||
1318 | x86_pmu_enable_counter(hwc, idx); | |
1319 | } | |
1320 | ||
1321 | static void amd_pmu_enable_counter(struct hw_perf_counter *hwc, int idx) | |
1322 | { | |
1323 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
1324 | ||
1325 | if (cpuc->enabled) | |
1326 | x86_pmu_enable_counter(hwc, idx); | |
241771ef IM |
1327 | } |
1328 | ||
2f18d1e8 IM |
1329 | static int |
1330 | fixed_mode_idx(struct perf_counter *counter, struct hw_perf_counter *hwc) | |
862a1a5f | 1331 | { |
2f18d1e8 IM |
1332 | unsigned int event; |
1333 | ||
30dd568c MM |
1334 | event = hwc->config & ARCH_PERFMON_EVENT_MASK; |
1335 | ||
1336 | if (unlikely((event == | |
1337 | x86_pmu.event_map(PERF_COUNT_HW_BRANCH_INSTRUCTIONS)) && | |
1338 | (hwc->sample_period == 1))) | |
1339 | return X86_PMC_IDX_FIXED_BTS; | |
1340 | ||
ef7b3e09 | 1341 | if (!x86_pmu.num_counters_fixed) |
f87ad35d JSR |
1342 | return -1; |
1343 | ||
f4dbfa8f | 1344 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_INSTRUCTIONS))) |
2f18d1e8 | 1345 | return X86_PMC_IDX_FIXED_INSTRUCTIONS; |
f4dbfa8f | 1346 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_CPU_CYCLES))) |
2f18d1e8 | 1347 | return X86_PMC_IDX_FIXED_CPU_CYCLES; |
f4dbfa8f | 1348 | if (unlikely(event == x86_pmu.event_map(PERF_COUNT_HW_BUS_CYCLES))) |
2f18d1e8 IM |
1349 | return X86_PMC_IDX_FIXED_BUS_CYCLES; |
1350 | ||
862a1a5f IM |
1351 | return -1; |
1352 | } | |
1353 | ||
ee06094f IM |
1354 | /* |
1355 | * Find a PMC slot for the freshly enabled / scheduled in counter: | |
1356 | */ | |
4aeb0b42 | 1357 | static int x86_pmu_enable(struct perf_counter *counter) |
241771ef IM |
1358 | { |
1359 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
1360 | struct hw_perf_counter *hwc = &counter->hw; | |
2f18d1e8 | 1361 | int idx; |
241771ef | 1362 | |
2f18d1e8 | 1363 | idx = fixed_mode_idx(counter, hwc); |
30dd568c | 1364 | if (idx == X86_PMC_IDX_FIXED_BTS) { |
747b50aa | 1365 | /* BTS is already occupied. */ |
30dd568c | 1366 | if (test_and_set_bit(idx, cpuc->used_mask)) |
747b50aa | 1367 | return -EAGAIN; |
30dd568c MM |
1368 | |
1369 | hwc->config_base = 0; | |
1370 | hwc->counter_base = 0; | |
1371 | hwc->idx = idx; | |
1372 | } else if (idx >= 0) { | |
2f18d1e8 IM |
1373 | /* |
1374 | * Try to get the fixed counter, if that is already taken | |
1375 | * then try to get a generic counter: | |
1376 | */ | |
43f6201a | 1377 | if (test_and_set_bit(idx, cpuc->used_mask)) |
2f18d1e8 | 1378 | goto try_generic; |
0dff86aa | 1379 | |
2f18d1e8 IM |
1380 | hwc->config_base = MSR_ARCH_PERFMON_FIXED_CTR_CTRL; |
1381 | /* | |
1382 | * We set it so that counter_base + idx in wrmsr/rdmsr maps to | |
1383 | * MSR_ARCH_PERFMON_FIXED_CTR0 ... CTR2: | |
1384 | */ | |
1385 | hwc->counter_base = | |
1386 | MSR_ARCH_PERFMON_FIXED_CTR0 - X86_PMC_IDX_FIXED; | |
241771ef | 1387 | hwc->idx = idx; |
2f18d1e8 IM |
1388 | } else { |
1389 | idx = hwc->idx; | |
1390 | /* Try to get the previous generic counter again */ | |
43f6201a | 1391 | if (test_and_set_bit(idx, cpuc->used_mask)) { |
2f18d1e8 | 1392 | try_generic: |
43f6201a | 1393 | idx = find_first_zero_bit(cpuc->used_mask, |
0933e5c6 RR |
1394 | x86_pmu.num_counters); |
1395 | if (idx == x86_pmu.num_counters) | |
2f18d1e8 IM |
1396 | return -EAGAIN; |
1397 | ||
43f6201a | 1398 | set_bit(idx, cpuc->used_mask); |
2f18d1e8 IM |
1399 | hwc->idx = idx; |
1400 | } | |
4a06bd85 RR |
1401 | hwc->config_base = x86_pmu.eventsel; |
1402 | hwc->counter_base = x86_pmu.perfctr; | |
241771ef IM |
1403 | } |
1404 | ||
c323d95f | 1405 | perf_counters_lapic_init(); |
53b441a5 | 1406 | |
d4369891 | 1407 | x86_pmu.disable(hwc, idx); |
241771ef | 1408 | |
862a1a5f | 1409 | cpuc->counters[idx] = counter; |
43f6201a | 1410 | set_bit(idx, cpuc->active_mask); |
7e2ae347 | 1411 | |
26816c28 | 1412 | x86_perf_counter_set_period(counter, hwc, idx); |
7c90cc45 | 1413 | x86_pmu.enable(hwc, idx); |
95cdd2e7 | 1414 | |
194002b2 PZ |
1415 | perf_counter_update_userpage(counter); |
1416 | ||
95cdd2e7 | 1417 | return 0; |
241771ef IM |
1418 | } |
1419 | ||
a78ac325 PZ |
1420 | static void x86_pmu_unthrottle(struct perf_counter *counter) |
1421 | { | |
1422 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
1423 | struct hw_perf_counter *hwc = &counter->hw; | |
1424 | ||
1425 | if (WARN_ON_ONCE(hwc->idx >= X86_PMC_IDX_MAX || | |
1426 | cpuc->counters[hwc->idx] != counter)) | |
1427 | return; | |
1428 | ||
1429 | x86_pmu.enable(hwc, hwc->idx); | |
1430 | } | |
1431 | ||
241771ef IM |
1432 | void perf_counter_print_debug(void) |
1433 | { | |
2f18d1e8 | 1434 | u64 ctrl, status, overflow, pmc_ctrl, pmc_count, prev_left, fixed; |
0dff86aa | 1435 | struct cpu_hw_counters *cpuc; |
5bb9efe3 | 1436 | unsigned long flags; |
1e125676 IM |
1437 | int cpu, idx; |
1438 | ||
0933e5c6 | 1439 | if (!x86_pmu.num_counters) |
1e125676 | 1440 | return; |
241771ef | 1441 | |
5bb9efe3 | 1442 | local_irq_save(flags); |
241771ef IM |
1443 | |
1444 | cpu = smp_processor_id(); | |
0dff86aa | 1445 | cpuc = &per_cpu(cpu_hw_counters, cpu); |
241771ef | 1446 | |
faa28ae0 | 1447 | if (x86_pmu.version >= 2) { |
a1ef58f4 JSR |
1448 | rdmsrl(MSR_CORE_PERF_GLOBAL_CTRL, ctrl); |
1449 | rdmsrl(MSR_CORE_PERF_GLOBAL_STATUS, status); | |
1450 | rdmsrl(MSR_CORE_PERF_GLOBAL_OVF_CTRL, overflow); | |
1451 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR_CTRL, fixed); | |
1452 | ||
1453 | pr_info("\n"); | |
1454 | pr_info("CPU#%d: ctrl: %016llx\n", cpu, ctrl); | |
1455 | pr_info("CPU#%d: status: %016llx\n", cpu, status); | |
1456 | pr_info("CPU#%d: overflow: %016llx\n", cpu, overflow); | |
1457 | pr_info("CPU#%d: fixed: %016llx\n", cpu, fixed); | |
f87ad35d | 1458 | } |
43f6201a | 1459 | pr_info("CPU#%d: used: %016llx\n", cpu, *(u64 *)cpuc->used_mask); |
241771ef | 1460 | |
0933e5c6 | 1461 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
4a06bd85 RR |
1462 | rdmsrl(x86_pmu.eventsel + idx, pmc_ctrl); |
1463 | rdmsrl(x86_pmu.perfctr + idx, pmc_count); | |
241771ef | 1464 | |
ee06094f | 1465 | prev_left = per_cpu(prev_left[idx], cpu); |
241771ef | 1466 | |
a1ef58f4 | 1467 | pr_info("CPU#%d: gen-PMC%d ctrl: %016llx\n", |
241771ef | 1468 | cpu, idx, pmc_ctrl); |
a1ef58f4 | 1469 | pr_info("CPU#%d: gen-PMC%d count: %016llx\n", |
241771ef | 1470 | cpu, idx, pmc_count); |
a1ef58f4 | 1471 | pr_info("CPU#%d: gen-PMC%d left: %016llx\n", |
ee06094f | 1472 | cpu, idx, prev_left); |
241771ef | 1473 | } |
0933e5c6 | 1474 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { |
2f18d1e8 IM |
1475 | rdmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, pmc_count); |
1476 | ||
a1ef58f4 | 1477 | pr_info("CPU#%d: fixed-PMC%d count: %016llx\n", |
2f18d1e8 IM |
1478 | cpu, idx, pmc_count); |
1479 | } | |
5bb9efe3 | 1480 | local_irq_restore(flags); |
241771ef IM |
1481 | } |
1482 | ||
30dd568c MM |
1483 | static void intel_pmu_drain_bts_buffer(struct cpu_hw_counters *cpuc, |
1484 | struct perf_sample_data *data) | |
1485 | { | |
1486 | struct debug_store *ds = cpuc->ds; | |
1487 | struct bts_record { | |
1488 | u64 from; | |
1489 | u64 to; | |
1490 | u64 flags; | |
1491 | }; | |
1492 | struct perf_counter *counter = cpuc->counters[X86_PMC_IDX_FIXED_BTS]; | |
1493 | unsigned long orig_ip = data->regs->ip; | |
1494 | u64 at; | |
1495 | ||
1496 | if (!counter) | |
1497 | return; | |
1498 | ||
1499 | if (!ds) | |
1500 | return; | |
1501 | ||
1502 | for (at = ds->bts_buffer_base; | |
1503 | at < ds->bts_index; | |
1504 | at += sizeof(struct bts_record)) { | |
1505 | struct bts_record *rec = (struct bts_record *)(long)at; | |
1506 | ||
1507 | data->regs->ip = rec->from; | |
1508 | data->addr = rec->to; | |
1509 | ||
1510 | perf_counter_output(counter, 1, data); | |
1511 | } | |
1512 | ||
1513 | ds->bts_index = ds->bts_buffer_base; | |
1514 | ||
1515 | data->regs->ip = orig_ip; | |
1516 | data->addr = 0; | |
1517 | ||
1518 | /* There's new data available. */ | |
1519 | counter->pending_kill = POLL_IN; | |
1520 | } | |
1521 | ||
4aeb0b42 | 1522 | static void x86_pmu_disable(struct perf_counter *counter) |
241771ef IM |
1523 | { |
1524 | struct cpu_hw_counters *cpuc = &__get_cpu_var(cpu_hw_counters); | |
1525 | struct hw_perf_counter *hwc = &counter->hw; | |
6f00cada | 1526 | int idx = hwc->idx; |
241771ef | 1527 | |
09534238 RR |
1528 | /* |
1529 | * Must be done before we disable, otherwise the nmi handler | |
1530 | * could reenable again: | |
1531 | */ | |
43f6201a | 1532 | clear_bit(idx, cpuc->active_mask); |
d4369891 | 1533 | x86_pmu.disable(hwc, idx); |
241771ef | 1534 | |
2f18d1e8 IM |
1535 | /* |
1536 | * Make sure the cleared pointer becomes visible before we | |
1537 | * (potentially) free the counter: | |
1538 | */ | |
527e26af | 1539 | barrier(); |
241771ef | 1540 | |
ee06094f IM |
1541 | /* |
1542 | * Drain the remaining delta count out of a counter | |
1543 | * that we are disabling: | |
1544 | */ | |
1545 | x86_perf_counter_update(counter, hwc, idx); | |
30dd568c MM |
1546 | |
1547 | /* Drain the remaining BTS records. */ | |
1548 | if (unlikely(idx == X86_PMC_IDX_FIXED_BTS)) { | |
1549 | struct perf_sample_data data; | |
1550 | struct pt_regs regs; | |
1551 | ||
1552 | data.regs = ®s; | |
1553 | intel_pmu_drain_bts_buffer(cpuc, &data); | |
1554 | } | |
09534238 | 1555 | cpuc->counters[idx] = NULL; |
43f6201a | 1556 | clear_bit(idx, cpuc->used_mask); |
194002b2 PZ |
1557 | |
1558 | perf_counter_update_userpage(counter); | |
241771ef IM |
1559 | } |
1560 | ||
7e2ae347 | 1561 | /* |
ee06094f IM |
1562 | * Save and restart an expired counter. Called by NMI contexts, |
1563 | * so it has to be careful about preempting normal counter ops: | |
7e2ae347 | 1564 | */ |
e4abb5d4 | 1565 | static int intel_pmu_save_and_restart(struct perf_counter *counter) |
241771ef IM |
1566 | { |
1567 | struct hw_perf_counter *hwc = &counter->hw; | |
1568 | int idx = hwc->idx; | |
e4abb5d4 | 1569 | int ret; |
241771ef | 1570 | |
ee06094f | 1571 | x86_perf_counter_update(counter, hwc, idx); |
e4abb5d4 | 1572 | ret = x86_perf_counter_set_period(counter, hwc, idx); |
7e2ae347 | 1573 | |
2f18d1e8 | 1574 | if (counter->state == PERF_COUNTER_STATE_ACTIVE) |
7c90cc45 | 1575 | intel_pmu_enable_counter(hwc, idx); |
e4abb5d4 PZ |
1576 | |
1577 | return ret; | |
241771ef IM |
1578 | } |
1579 | ||
aaba9801 IM |
1580 | static void intel_pmu_reset(void) |
1581 | { | |
30dd568c | 1582 | struct debug_store *ds = __get_cpu_var(cpu_hw_counters).ds; |
aaba9801 IM |
1583 | unsigned long flags; |
1584 | int idx; | |
1585 | ||
1586 | if (!x86_pmu.num_counters) | |
1587 | return; | |
1588 | ||
1589 | local_irq_save(flags); | |
1590 | ||
1591 | printk("clearing PMU state on CPU#%d\n", smp_processor_id()); | |
1592 | ||
1593 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { | |
1594 | checking_wrmsrl(x86_pmu.eventsel + idx, 0ull); | |
1595 | checking_wrmsrl(x86_pmu.perfctr + idx, 0ull); | |
1596 | } | |
1597 | for (idx = 0; idx < x86_pmu.num_counters_fixed; idx++) { | |
1598 | checking_wrmsrl(MSR_ARCH_PERFMON_FIXED_CTR0 + idx, 0ull); | |
1599 | } | |
30dd568c MM |
1600 | if (ds) |
1601 | ds->bts_index = ds->bts_buffer_base; | |
aaba9801 IM |
1602 | |
1603 | local_irq_restore(flags); | |
1604 | } | |
1605 | ||
11d1578f VW |
1606 | static int p6_pmu_handle_irq(struct pt_regs *regs) |
1607 | { | |
1608 | struct perf_sample_data data; | |
1609 | struct cpu_hw_counters *cpuc; | |
1610 | struct perf_counter *counter; | |
1611 | struct hw_perf_counter *hwc; | |
1612 | int idx, handled = 0; | |
1613 | u64 val; | |
1614 | ||
1615 | data.regs = regs; | |
1616 | data.addr = 0; | |
1617 | ||
1618 | cpuc = &__get_cpu_var(cpu_hw_counters); | |
1619 | ||
1620 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { | |
1621 | if (!test_bit(idx, cpuc->active_mask)) | |
1622 | continue; | |
1623 | ||
1624 | counter = cpuc->counters[idx]; | |
1625 | hwc = &counter->hw; | |
1626 | ||
1627 | val = x86_perf_counter_update(counter, hwc, idx); | |
1628 | if (val & (1ULL << (x86_pmu.counter_bits - 1))) | |
1629 | continue; | |
1630 | ||
1631 | /* | |
1632 | * counter overflow | |
1633 | */ | |
1634 | handled = 1; | |
1635 | data.period = counter->hw.last_period; | |
1636 | ||
1637 | if (!x86_perf_counter_set_period(counter, hwc, idx)) | |
1638 | continue; | |
1639 | ||
1640 | if (perf_counter_overflow(counter, 1, &data)) | |
1641 | p6_pmu_disable_counter(hwc, idx); | |
1642 | } | |
1643 | ||
1644 | if (handled) | |
1645 | inc_irq_stat(apic_perf_irqs); | |
1646 | ||
1647 | return handled; | |
1648 | } | |
aaba9801 | 1649 | |
241771ef IM |
1650 | /* |
1651 | * This handler is triggered by the local APIC, so the APIC IRQ handling | |
1652 | * rules apply: | |
1653 | */ | |
a3288106 | 1654 | static int intel_pmu_handle_irq(struct pt_regs *regs) |
241771ef | 1655 | { |
df1a132b | 1656 | struct perf_sample_data data; |
9029a5e3 | 1657 | struct cpu_hw_counters *cpuc; |
11d1578f | 1658 | int bit, loops; |
4b39fd96 | 1659 | u64 ack, status; |
9029a5e3 | 1660 | |
df1a132b PZ |
1661 | data.regs = regs; |
1662 | data.addr = 0; | |
1663 | ||
11d1578f | 1664 | cpuc = &__get_cpu_var(cpu_hw_counters); |
241771ef | 1665 | |
9e35ad38 | 1666 | perf_disable(); |
30dd568c | 1667 | intel_pmu_drain_bts_buffer(cpuc, &data); |
19d84dab | 1668 | status = intel_pmu_get_status(); |
9e35ad38 PZ |
1669 | if (!status) { |
1670 | perf_enable(); | |
1671 | return 0; | |
1672 | } | |
87b9cf46 | 1673 | |
9029a5e3 | 1674 | loops = 0; |
241771ef | 1675 | again: |
9029a5e3 IM |
1676 | if (++loops > 100) { |
1677 | WARN_ONCE(1, "perfcounters: irq loop stuck!\n"); | |
34adc806 | 1678 | perf_counter_print_debug(); |
aaba9801 IM |
1679 | intel_pmu_reset(); |
1680 | perf_enable(); | |
9029a5e3 IM |
1681 | return 1; |
1682 | } | |
1683 | ||
d278c484 | 1684 | inc_irq_stat(apic_perf_irqs); |
241771ef | 1685 | ack = status; |
2f18d1e8 | 1686 | for_each_bit(bit, (unsigned long *)&status, X86_PMC_IDX_MAX) { |
862a1a5f | 1687 | struct perf_counter *counter = cpuc->counters[bit]; |
241771ef IM |
1688 | |
1689 | clear_bit(bit, (unsigned long *) &status); | |
43f6201a | 1690 | if (!test_bit(bit, cpuc->active_mask)) |
241771ef IM |
1691 | continue; |
1692 | ||
e4abb5d4 PZ |
1693 | if (!intel_pmu_save_and_restart(counter)) |
1694 | continue; | |
1695 | ||
60f916de PZ |
1696 | data.period = counter->hw.last_period; |
1697 | ||
df1a132b | 1698 | if (perf_counter_overflow(counter, 1, &data)) |
d4369891 | 1699 | intel_pmu_disable_counter(&counter->hw, bit); |
241771ef IM |
1700 | } |
1701 | ||
dee5d906 | 1702 | intel_pmu_ack_status(ack); |
241771ef IM |
1703 | |
1704 | /* | |
1705 | * Repeat if there is more work to be done: | |
1706 | */ | |
19d84dab | 1707 | status = intel_pmu_get_status(); |
241771ef IM |
1708 | if (status) |
1709 | goto again; | |
b0f3f28e | 1710 | |
48e22d56 | 1711 | perf_enable(); |
9e35ad38 PZ |
1712 | |
1713 | return 1; | |
1b023a96 MG |
1714 | } |
1715 | ||
a3288106 | 1716 | static int amd_pmu_handle_irq(struct pt_regs *regs) |
a29aa8a7 | 1717 | { |
df1a132b | 1718 | struct perf_sample_data data; |
9029a5e3 | 1719 | struct cpu_hw_counters *cpuc; |
a29aa8a7 RR |
1720 | struct perf_counter *counter; |
1721 | struct hw_perf_counter *hwc; | |
11d1578f | 1722 | int idx, handled = 0; |
9029a5e3 IM |
1723 | u64 val; |
1724 | ||
df1a132b PZ |
1725 | data.regs = regs; |
1726 | data.addr = 0; | |
1727 | ||
11d1578f | 1728 | cpuc = &__get_cpu_var(cpu_hw_counters); |
962bf7a6 | 1729 | |
a29aa8a7 | 1730 | for (idx = 0; idx < x86_pmu.num_counters; idx++) { |
43f6201a | 1731 | if (!test_bit(idx, cpuc->active_mask)) |
a29aa8a7 | 1732 | continue; |
962bf7a6 | 1733 | |
a29aa8a7 RR |
1734 | counter = cpuc->counters[idx]; |
1735 | hwc = &counter->hw; | |
a4016a79 | 1736 | |
4b7bfd0d | 1737 | val = x86_perf_counter_update(counter, hwc, idx); |
a29aa8a7 | 1738 | if (val & (1ULL << (x86_pmu.counter_bits - 1))) |
48e22d56 | 1739 | continue; |
962bf7a6 | 1740 | |
9e350de3 PZ |
1741 | /* |
1742 | * counter overflow | |
1743 | */ | |
1744 | handled = 1; | |
1745 | data.period = counter->hw.last_period; | |
1746 | ||
e4abb5d4 PZ |
1747 | if (!x86_perf_counter_set_period(counter, hwc, idx)) |
1748 | continue; | |
1749 | ||
df1a132b | 1750 | if (perf_counter_overflow(counter, 1, &data)) |
a29aa8a7 | 1751 | amd_pmu_disable_counter(hwc, idx); |
a29aa8a7 | 1752 | } |
962bf7a6 | 1753 | |
9e350de3 PZ |
1754 | if (handled) |
1755 | inc_irq_stat(apic_perf_irqs); | |
1756 | ||
a29aa8a7 RR |
1757 | return handled; |
1758 | } | |
39d81eab | 1759 | |
b6276f35 PZ |
1760 | void smp_perf_pending_interrupt(struct pt_regs *regs) |
1761 | { | |
1762 | irq_enter(); | |
1763 | ack_APIC_irq(); | |
1764 | inc_irq_stat(apic_pending_irqs); | |
1765 | perf_counter_do_pending(); | |
1766 | irq_exit(); | |
1767 | } | |
1768 | ||
1769 | void set_perf_counter_pending(void) | |
1770 | { | |
04da8a43 | 1771 | #ifdef CONFIG_X86_LOCAL_APIC |
b6276f35 | 1772 | apic->send_IPI_self(LOCAL_PENDING_VECTOR); |
04da8a43 | 1773 | #endif |
b6276f35 PZ |
1774 | } |
1775 | ||
c323d95f | 1776 | void perf_counters_lapic_init(void) |
241771ef | 1777 | { |
04da8a43 IM |
1778 | #ifdef CONFIG_X86_LOCAL_APIC |
1779 | if (!x86_pmu.apic || !x86_pmu_initialized()) | |
241771ef | 1780 | return; |
85cf9dba | 1781 | |
241771ef | 1782 | /* |
c323d95f | 1783 | * Always use NMI for PMU |
241771ef | 1784 | */ |
c323d95f | 1785 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
04da8a43 | 1786 | #endif |
241771ef IM |
1787 | } |
1788 | ||
1789 | static int __kprobes | |
1790 | perf_counter_nmi_handler(struct notifier_block *self, | |
1791 | unsigned long cmd, void *__args) | |
1792 | { | |
1793 | struct die_args *args = __args; | |
1794 | struct pt_regs *regs; | |
b0f3f28e | 1795 | |
ba77813a | 1796 | if (!atomic_read(&active_counters)) |
63a809a2 PZ |
1797 | return NOTIFY_DONE; |
1798 | ||
b0f3f28e PZ |
1799 | switch (cmd) { |
1800 | case DIE_NMI: | |
1801 | case DIE_NMI_IPI: | |
1802 | break; | |
241771ef | 1803 | |
b0f3f28e | 1804 | default: |
241771ef | 1805 | return NOTIFY_DONE; |
b0f3f28e | 1806 | } |
241771ef IM |
1807 | |
1808 | regs = args->regs; | |
1809 | ||
04da8a43 | 1810 | #ifdef CONFIG_X86_LOCAL_APIC |
241771ef | 1811 | apic_write(APIC_LVTPC, APIC_DM_NMI); |
04da8a43 | 1812 | #endif |
a4016a79 PZ |
1813 | /* |
1814 | * Can't rely on the handled return value to say it was our NMI, two | |
1815 | * counters could trigger 'simultaneously' raising two back-to-back NMIs. | |
1816 | * | |
1817 | * If the first NMI handles both, the latter will be empty and daze | |
1818 | * the CPU. | |
1819 | */ | |
a3288106 | 1820 | x86_pmu.handle_irq(regs); |
241771ef | 1821 | |
a4016a79 | 1822 | return NOTIFY_STOP; |
241771ef IM |
1823 | } |
1824 | ||
1825 | static __read_mostly struct notifier_block perf_counter_nmi_notifier = { | |
5b75af0a MG |
1826 | .notifier_call = perf_counter_nmi_handler, |
1827 | .next = NULL, | |
1828 | .priority = 1 | |
241771ef IM |
1829 | }; |
1830 | ||
11d1578f VW |
1831 | static struct x86_pmu p6_pmu = { |
1832 | .name = "p6", | |
1833 | .handle_irq = p6_pmu_handle_irq, | |
1834 | .disable_all = p6_pmu_disable_all, | |
1835 | .enable_all = p6_pmu_enable_all, | |
1836 | .enable = p6_pmu_enable_counter, | |
1837 | .disable = p6_pmu_disable_counter, | |
1838 | .eventsel = MSR_P6_EVNTSEL0, | |
1839 | .perfctr = MSR_P6_PERFCTR0, | |
1840 | .event_map = p6_pmu_event_map, | |
1841 | .raw_event = p6_pmu_raw_event, | |
1842 | .max_events = ARRAY_SIZE(p6_perfmon_event_map), | |
04da8a43 | 1843 | .apic = 1, |
11d1578f VW |
1844 | .max_period = (1ULL << 31) - 1, |
1845 | .version = 0, | |
1846 | .num_counters = 2, | |
1847 | /* | |
1848 | * Counters have 40 bits implemented. However they are designed such | |
1849 | * that bits [32-39] are sign extensions of bit 31. As such the | |
1850 | * effective width of a counter for P6-like PMU is 32 bits only. | |
1851 | * | |
1852 | * See IA-32 Intel Architecture Software developer manual Vol 3B | |
1853 | */ | |
1854 | .counter_bits = 32, | |
1855 | .counter_mask = (1ULL << 32) - 1, | |
1856 | }; | |
1857 | ||
5f4ec28f | 1858 | static struct x86_pmu intel_pmu = { |
faa28ae0 | 1859 | .name = "Intel", |
39d81eab | 1860 | .handle_irq = intel_pmu_handle_irq, |
9e35ad38 PZ |
1861 | .disable_all = intel_pmu_disable_all, |
1862 | .enable_all = intel_pmu_enable_all, | |
5f4ec28f RR |
1863 | .enable = intel_pmu_enable_counter, |
1864 | .disable = intel_pmu_disable_counter, | |
b56a3802 JSR |
1865 | .eventsel = MSR_ARCH_PERFMON_EVENTSEL0, |
1866 | .perfctr = MSR_ARCH_PERFMON_PERFCTR0, | |
5f4ec28f RR |
1867 | .event_map = intel_pmu_event_map, |
1868 | .raw_event = intel_pmu_raw_event, | |
b56a3802 | 1869 | .max_events = ARRAY_SIZE(intel_perfmon_event_map), |
04da8a43 | 1870 | .apic = 1, |
c619b8ff RR |
1871 | /* |
1872 | * Intel PMCs cannot be accessed sanely above 32 bit width, | |
1873 | * so we install an artificial 1<<31 period regardless of | |
1874 | * the generic counter period: | |
1875 | */ | |
1876 | .max_period = (1ULL << 31) - 1, | |
30dd568c MM |
1877 | .enable_bts = intel_pmu_enable_bts, |
1878 | .disable_bts = intel_pmu_disable_bts, | |
b56a3802 JSR |
1879 | }; |
1880 | ||
5f4ec28f | 1881 | static struct x86_pmu amd_pmu = { |
faa28ae0 | 1882 | .name = "AMD", |
39d81eab | 1883 | .handle_irq = amd_pmu_handle_irq, |
9e35ad38 PZ |
1884 | .disable_all = amd_pmu_disable_all, |
1885 | .enable_all = amd_pmu_enable_all, | |
5f4ec28f RR |
1886 | .enable = amd_pmu_enable_counter, |
1887 | .disable = amd_pmu_disable_counter, | |
f87ad35d JSR |
1888 | .eventsel = MSR_K7_EVNTSEL0, |
1889 | .perfctr = MSR_K7_PERFCTR0, | |
5f4ec28f RR |
1890 | .event_map = amd_pmu_event_map, |
1891 | .raw_event = amd_pmu_raw_event, | |
f87ad35d | 1892 | .max_events = ARRAY_SIZE(amd_perfmon_event_map), |
0933e5c6 RR |
1893 | .num_counters = 4, |
1894 | .counter_bits = 48, | |
1895 | .counter_mask = (1ULL << 48) - 1, | |
04da8a43 | 1896 | .apic = 1, |
c619b8ff RR |
1897 | /* use highest bit to detect overflow */ |
1898 | .max_period = (1ULL << 47) - 1, | |
f87ad35d JSR |
1899 | }; |
1900 | ||
11d1578f VW |
1901 | static int p6_pmu_init(void) |
1902 | { | |
11d1578f VW |
1903 | switch (boot_cpu_data.x86_model) { |
1904 | case 1: | |
1905 | case 3: /* Pentium Pro */ | |
1906 | case 5: | |
1907 | case 6: /* Pentium II */ | |
1908 | case 7: | |
1909 | case 8: | |
1910 | case 11: /* Pentium III */ | |
1911 | break; | |
1912 | case 9: | |
1913 | case 13: | |
f1c6a581 DQ |
1914 | /* Pentium M */ |
1915 | break; | |
11d1578f VW |
1916 | default: |
1917 | pr_cont("unsupported p6 CPU model %d ", | |
1918 | boot_cpu_data.x86_model); | |
1919 | return -ENODEV; | |
1920 | } | |
1921 | ||
04da8a43 IM |
1922 | x86_pmu = p6_pmu; |
1923 | ||
11d1578f | 1924 | if (!cpu_has_apic) { |
3c581a7f | 1925 | pr_info("no APIC, boot with the \"lapic\" boot parameter to force-enable it.\n"); |
04da8a43 IM |
1926 | pr_info("no hardware sampling interrupt available.\n"); |
1927 | x86_pmu.apic = 0; | |
11d1578f VW |
1928 | } |
1929 | ||
11d1578f VW |
1930 | return 0; |
1931 | } | |
1932 | ||
72eae04d | 1933 | static int intel_pmu_init(void) |
241771ef | 1934 | { |
7bb497bd | 1935 | union cpuid10_edx edx; |
241771ef | 1936 | union cpuid10_eax eax; |
703e937c | 1937 | unsigned int unused; |
7bb497bd | 1938 | unsigned int ebx; |
faa28ae0 | 1939 | int version; |
241771ef | 1940 | |
11d1578f VW |
1941 | if (!cpu_has(&boot_cpu_data, X86_FEATURE_ARCH_PERFMON)) { |
1942 | /* check for P6 processor family */ | |
1943 | if (boot_cpu_data.x86 == 6) { | |
1944 | return p6_pmu_init(); | |
1945 | } else { | |
72eae04d | 1946 | return -ENODEV; |
11d1578f VW |
1947 | } |
1948 | } | |
da1a776b | 1949 | |
241771ef IM |
1950 | /* |
1951 | * Check whether the Architectural PerfMon supports | |
1952 | * Branch Misses Retired Event or not. | |
1953 | */ | |
703e937c | 1954 | cpuid(10, &eax.full, &ebx, &unused, &edx.full); |
241771ef | 1955 | if (eax.split.mask_length <= ARCH_PERFMON_BRANCH_MISSES_RETIRED) |
72eae04d | 1956 | return -ENODEV; |
241771ef | 1957 | |
faa28ae0 RR |
1958 | version = eax.split.version_id; |
1959 | if (version < 2) | |
72eae04d | 1960 | return -ENODEV; |
7bb497bd | 1961 | |
1123e3ad IM |
1962 | x86_pmu = intel_pmu; |
1963 | x86_pmu.version = version; | |
1964 | x86_pmu.num_counters = eax.split.num_counters; | |
1965 | x86_pmu.counter_bits = eax.split.bit_width; | |
1966 | x86_pmu.counter_mask = (1ULL << eax.split.bit_width) - 1; | |
066d7dea IM |
1967 | |
1968 | /* | |
1969 | * Quirk: v2 perfmon does not report fixed-purpose counters, so | |
1970 | * assume at least 3 counters: | |
1971 | */ | |
1123e3ad | 1972 | x86_pmu.num_counters_fixed = max((int)edx.split.num_counters_fixed, 3); |
b56a3802 | 1973 | |
8326f44d | 1974 | /* |
1123e3ad | 1975 | * Install the hw-cache-events table: |
8326f44d IM |
1976 | */ |
1977 | switch (boot_cpu_data.x86_model) { | |
dc81081b YW |
1978 | case 15: /* original 65 nm celeron/pentium/core2/xeon, "Merom"/"Conroe" */ |
1979 | case 22: /* single-core 65 nm celeron/core2solo "Merom-L"/"Conroe-L" */ | |
1980 | case 23: /* current 45 nm celeron/core2/xeon "Penryn"/"Wolfdale" */ | |
1981 | case 29: /* six-core 45 nm xeon "Dunnington" */ | |
8326f44d | 1982 | memcpy(hw_cache_event_ids, core2_hw_cache_event_ids, |
820a6442 | 1983 | sizeof(hw_cache_event_ids)); |
8326f44d | 1984 | |
1123e3ad | 1985 | pr_cont("Core2 events, "); |
8326f44d IM |
1986 | break; |
1987 | default: | |
1988 | case 26: | |
1989 | memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids, | |
820a6442 | 1990 | sizeof(hw_cache_event_ids)); |
8326f44d | 1991 | |
1123e3ad | 1992 | pr_cont("Nehalem/Corei7 events, "); |
8326f44d IM |
1993 | break; |
1994 | case 28: | |
1995 | memcpy(hw_cache_event_ids, atom_hw_cache_event_ids, | |
820a6442 | 1996 | sizeof(hw_cache_event_ids)); |
8326f44d | 1997 | |
1123e3ad | 1998 | pr_cont("Atom events, "); |
8326f44d IM |
1999 | break; |
2000 | } | |
72eae04d | 2001 | return 0; |
b56a3802 JSR |
2002 | } |
2003 | ||
72eae04d | 2004 | static int amd_pmu_init(void) |
f87ad35d | 2005 | { |
4d2be126 JSR |
2006 | /* Performance-monitoring supported from K7 and later: */ |
2007 | if (boot_cpu_data.x86 < 6) | |
2008 | return -ENODEV; | |
2009 | ||
4a06bd85 | 2010 | x86_pmu = amd_pmu; |
f86748e9 | 2011 | |
f4db43a3 JSR |
2012 | /* Events are common for all AMDs */ |
2013 | memcpy(hw_cache_event_ids, amd_hw_cache_event_ids, | |
2014 | sizeof(hw_cache_event_ids)); | |
f86748e9 | 2015 | |
72eae04d | 2016 | return 0; |
f87ad35d JSR |
2017 | } |
2018 | ||
b56a3802 JSR |
2019 | void __init init_hw_perf_counters(void) |
2020 | { | |
72eae04d RR |
2021 | int err; |
2022 | ||
1123e3ad IM |
2023 | pr_info("Performance Counters: "); |
2024 | ||
b56a3802 JSR |
2025 | switch (boot_cpu_data.x86_vendor) { |
2026 | case X86_VENDOR_INTEL: | |
72eae04d | 2027 | err = intel_pmu_init(); |
b56a3802 | 2028 | break; |
f87ad35d | 2029 | case X86_VENDOR_AMD: |
72eae04d | 2030 | err = amd_pmu_init(); |
f87ad35d | 2031 | break; |
4138960a RR |
2032 | default: |
2033 | return; | |
b56a3802 | 2034 | } |
1123e3ad IM |
2035 | if (err != 0) { |
2036 | pr_cont("no PMU driver, software counters only.\n"); | |
b56a3802 | 2037 | return; |
1123e3ad | 2038 | } |
b56a3802 | 2039 | |
1123e3ad | 2040 | pr_cont("%s PMU driver.\n", x86_pmu.name); |
faa28ae0 | 2041 | |
0933e5c6 | 2042 | if (x86_pmu.num_counters > X86_PMC_MAX_GENERIC) { |
241771ef | 2043 | WARN(1, KERN_ERR "hw perf counters %d > max(%d), clipping!", |
0933e5c6 | 2044 | x86_pmu.num_counters, X86_PMC_MAX_GENERIC); |
4078c444 | 2045 | x86_pmu.num_counters = X86_PMC_MAX_GENERIC; |
241771ef | 2046 | } |
0933e5c6 RR |
2047 | perf_counter_mask = (1 << x86_pmu.num_counters) - 1; |
2048 | perf_max_counters = x86_pmu.num_counters; | |
241771ef | 2049 | |
0933e5c6 | 2050 | if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) { |
703e937c | 2051 | WARN(1, KERN_ERR "hw perf counters fixed %d > max(%d), clipping!", |
0933e5c6 | 2052 | x86_pmu.num_counters_fixed, X86_PMC_MAX_FIXED); |
4078c444 | 2053 | x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED; |
703e937c | 2054 | } |
862a1a5f | 2055 | |
0933e5c6 RR |
2056 | perf_counter_mask |= |
2057 | ((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED; | |
c14dab5c | 2058 | x86_pmu.intel_ctrl = perf_counter_mask; |
241771ef | 2059 | |
c323d95f | 2060 | perf_counters_lapic_init(); |
241771ef | 2061 | register_die_notifier(&perf_counter_nmi_notifier); |
1123e3ad IM |
2062 | |
2063 | pr_info("... version: %d\n", x86_pmu.version); | |
2064 | pr_info("... bit width: %d\n", x86_pmu.counter_bits); | |
2065 | pr_info("... generic counters: %d\n", x86_pmu.num_counters); | |
2066 | pr_info("... value mask: %016Lx\n", x86_pmu.counter_mask); | |
2067 | pr_info("... max period: %016Lx\n", x86_pmu.max_period); | |
2068 | pr_info("... fixed-purpose counters: %d\n", x86_pmu.num_counters_fixed); | |
2069 | pr_info("... counter mask: %016Lx\n", perf_counter_mask); | |
241771ef | 2070 | } |
621a01ea | 2071 | |
bb775fc2 | 2072 | static inline void x86_pmu_read(struct perf_counter *counter) |
ee06094f IM |
2073 | { |
2074 | x86_perf_counter_update(counter, &counter->hw, counter->hw.idx); | |
2075 | } | |
2076 | ||
4aeb0b42 RR |
2077 | static const struct pmu pmu = { |
2078 | .enable = x86_pmu_enable, | |
2079 | .disable = x86_pmu_disable, | |
2080 | .read = x86_pmu_read, | |
a78ac325 | 2081 | .unthrottle = x86_pmu_unthrottle, |
621a01ea IM |
2082 | }; |
2083 | ||
4aeb0b42 | 2084 | const struct pmu *hw_perf_counter_init(struct perf_counter *counter) |
621a01ea IM |
2085 | { |
2086 | int err; | |
2087 | ||
2088 | err = __hw_perf_counter_init(counter); | |
2089 | if (err) | |
9ea98e19 | 2090 | return ERR_PTR(err); |
621a01ea | 2091 | |
4aeb0b42 | 2092 | return &pmu; |
621a01ea | 2093 | } |
d7d59fb3 PZ |
2094 | |
2095 | /* | |
2096 | * callchain support | |
2097 | */ | |
2098 | ||
2099 | static inline | |
f9188e02 | 2100 | void callchain_store(struct perf_callchain_entry *entry, u64 ip) |
d7d59fb3 | 2101 | { |
f9188e02 | 2102 | if (entry->nr < PERF_MAX_STACK_DEPTH) |
d7d59fb3 PZ |
2103 | entry->ip[entry->nr++] = ip; |
2104 | } | |
2105 | ||
2106 | static DEFINE_PER_CPU(struct perf_callchain_entry, irq_entry); | |
2107 | static DEFINE_PER_CPU(struct perf_callchain_entry, nmi_entry); | |
0406ca6d | 2108 | static DEFINE_PER_CPU(int, in_nmi_frame); |
d7d59fb3 PZ |
2109 | |
2110 | ||
2111 | static void | |
2112 | backtrace_warning_symbol(void *data, char *msg, unsigned long symbol) | |
2113 | { | |
2114 | /* Ignore warnings */ | |
2115 | } | |
2116 | ||
2117 | static void backtrace_warning(void *data, char *msg) | |
2118 | { | |
2119 | /* Ignore warnings */ | |
2120 | } | |
2121 | ||
2122 | static int backtrace_stack(void *data, char *name) | |
2123 | { | |
0406ca6d FW |
2124 | per_cpu(in_nmi_frame, smp_processor_id()) = |
2125 | x86_is_stack_id(NMI_STACK, name); | |
2126 | ||
038e836e | 2127 | return 0; |
d7d59fb3 PZ |
2128 | } |
2129 | ||
2130 | static void backtrace_address(void *data, unsigned long addr, int reliable) | |
2131 | { | |
2132 | struct perf_callchain_entry *entry = data; | |
2133 | ||
0406ca6d FW |
2134 | if (per_cpu(in_nmi_frame, smp_processor_id())) |
2135 | return; | |
2136 | ||
d7d59fb3 PZ |
2137 | if (reliable) |
2138 | callchain_store(entry, addr); | |
2139 | } | |
2140 | ||
2141 | static const struct stacktrace_ops backtrace_ops = { | |
2142 | .warning = backtrace_warning, | |
2143 | .warning_symbol = backtrace_warning_symbol, | |
2144 | .stack = backtrace_stack, | |
2145 | .address = backtrace_address, | |
2146 | }; | |
2147 | ||
038e836e IM |
2148 | #include "../dumpstack.h" |
2149 | ||
d7d59fb3 PZ |
2150 | static void |
2151 | perf_callchain_kernel(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
2152 | { | |
f9188e02 | 2153 | callchain_store(entry, PERF_CONTEXT_KERNEL); |
038e836e | 2154 | callchain_store(entry, regs->ip); |
d7d59fb3 | 2155 | |
f9188e02 | 2156 | dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry); |
d7d59fb3 PZ |
2157 | } |
2158 | ||
74193ef0 PZ |
2159 | /* |
2160 | * best effort, GUP based copy_from_user() that assumes IRQ or NMI context | |
2161 | */ | |
2162 | static unsigned long | |
2163 | copy_from_user_nmi(void *to, const void __user *from, unsigned long n) | |
d7d59fb3 | 2164 | { |
74193ef0 PZ |
2165 | unsigned long offset, addr = (unsigned long)from; |
2166 | int type = in_nmi() ? KM_NMI : KM_IRQ0; | |
2167 | unsigned long size, len = 0; | |
2168 | struct page *page; | |
2169 | void *map; | |
d7d59fb3 PZ |
2170 | int ret; |
2171 | ||
74193ef0 PZ |
2172 | do { |
2173 | ret = __get_user_pages_fast(addr, 1, 0, &page); | |
2174 | if (!ret) | |
2175 | break; | |
d7d59fb3 | 2176 | |
74193ef0 PZ |
2177 | offset = addr & (PAGE_SIZE - 1); |
2178 | size = min(PAGE_SIZE - offset, n - len); | |
d7d59fb3 | 2179 | |
74193ef0 PZ |
2180 | map = kmap_atomic(page, type); |
2181 | memcpy(to, map+offset, size); | |
2182 | kunmap_atomic(map, type); | |
2183 | put_page(page); | |
2184 | ||
2185 | len += size; | |
2186 | to += size; | |
2187 | addr += size; | |
2188 | ||
2189 | } while (len < n); | |
2190 | ||
2191 | return len; | |
2192 | } | |
2193 | ||
2194 | static int copy_stack_frame(const void __user *fp, struct stack_frame *frame) | |
2195 | { | |
2196 | unsigned long bytes; | |
2197 | ||
2198 | bytes = copy_from_user_nmi(frame, fp, sizeof(*frame)); | |
2199 | ||
2200 | return bytes == sizeof(*frame); | |
d7d59fb3 PZ |
2201 | } |
2202 | ||
2203 | static void | |
2204 | perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
2205 | { | |
2206 | struct stack_frame frame; | |
2207 | const void __user *fp; | |
2208 | ||
5a6cec3a IM |
2209 | if (!user_mode(regs)) |
2210 | regs = task_pt_regs(current); | |
2211 | ||
74193ef0 | 2212 | fp = (void __user *)regs->bp; |
d7d59fb3 | 2213 | |
f9188e02 | 2214 | callchain_store(entry, PERF_CONTEXT_USER); |
d7d59fb3 PZ |
2215 | callchain_store(entry, regs->ip); |
2216 | ||
f9188e02 | 2217 | while (entry->nr < PERF_MAX_STACK_DEPTH) { |
038e836e | 2218 | frame.next_frame = NULL; |
d7d59fb3 PZ |
2219 | frame.return_address = 0; |
2220 | ||
2221 | if (!copy_stack_frame(fp, &frame)) | |
2222 | break; | |
2223 | ||
5a6cec3a | 2224 | if ((unsigned long)fp < regs->sp) |
d7d59fb3 PZ |
2225 | break; |
2226 | ||
2227 | callchain_store(entry, frame.return_address); | |
038e836e | 2228 | fp = frame.next_frame; |
d7d59fb3 PZ |
2229 | } |
2230 | } | |
2231 | ||
2232 | static void | |
2233 | perf_do_callchain(struct pt_regs *regs, struct perf_callchain_entry *entry) | |
2234 | { | |
2235 | int is_user; | |
2236 | ||
2237 | if (!regs) | |
2238 | return; | |
2239 | ||
2240 | is_user = user_mode(regs); | |
2241 | ||
2242 | if (!current || current->pid == 0) | |
2243 | return; | |
2244 | ||
2245 | if (is_user && current->state != TASK_RUNNING) | |
2246 | return; | |
2247 | ||
2248 | if (!is_user) | |
2249 | perf_callchain_kernel(regs, entry); | |
2250 | ||
2251 | if (current->mm) | |
2252 | perf_callchain_user(regs, entry); | |
2253 | } | |
2254 | ||
2255 | struct perf_callchain_entry *perf_callchain(struct pt_regs *regs) | |
2256 | { | |
2257 | struct perf_callchain_entry *entry; | |
2258 | ||
2259 | if (in_nmi()) | |
2260 | entry = &__get_cpu_var(nmi_entry); | |
2261 | else | |
2262 | entry = &__get_cpu_var(irq_entry); | |
2263 | ||
2264 | entry->nr = 0; | |
2265 | ||
2266 | perf_do_callchain(regs, entry); | |
2267 | ||
2268 | return entry; | |
2269 | } | |
30dd568c MM |
2270 | |
2271 | void hw_perf_counter_setup_online(int cpu) | |
2272 | { | |
2273 | init_debug_store_on_cpu(cpu); | |
2274 | } |