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