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