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