]>
Commit | Line | Data |
---|---|---|
de0428a7 KW |
1 | /* |
2 | * Performance events x86 architecture header | |
3 | * | |
4 | * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> | |
5 | * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar | |
6 | * Copyright (C) 2009 Jaswinder Singh Rajput | |
7 | * Copyright (C) 2009 Advanced Micro Devices, Inc., Robert Richter | |
8 | * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> | |
9 | * Copyright (C) 2009 Intel Corporation, <markus.t.metzger@intel.com> | |
10 | * Copyright (C) 2009 Google, Inc., Stephane Eranian | |
11 | * | |
12 | * For licencing details see kernel-base/COPYING | |
13 | */ | |
14 | ||
15 | #include <linux/perf_event.h> | |
16 | ||
1c2ac3fd PZ |
17 | #if 0 |
18 | #undef wrmsrl | |
19 | #define wrmsrl(msr, val) \ | |
20 | do { \ | |
21 | unsigned int _msr = (msr); \ | |
22 | u64 _val = (val); \ | |
23 | trace_printk("wrmsrl(%x, %Lx)\n", (unsigned int)(_msr), \ | |
24 | (unsigned long long)(_val)); \ | |
25 | native_write_msr((_msr), (u32)(_val), (u32)(_val >> 32)); \ | |
26 | } while (0) | |
27 | #endif | |
28 | ||
de0428a7 KW |
29 | /* |
30 | * | NHM/WSM | SNB | | |
31 | * register ------------------------------- | |
32 | * | HT | no HT | HT | no HT | | |
33 | *----------------------------------------- | |
34 | * offcore | core | core | cpu | core | | |
35 | * lbr_sel | core | core | cpu | core | | |
36 | * ld_lat | cpu | core | cpu | core | | |
37 | *----------------------------------------- | |
38 | * | |
39 | * Given that there is a small number of shared regs, | |
40 | * we can pre-allocate their slot in the per-cpu | |
41 | * per-core reg tables. | |
42 | */ | |
43 | enum extra_reg_type { | |
44 | EXTRA_REG_NONE = -1, /* not used */ | |
45 | ||
46 | EXTRA_REG_RSP_0 = 0, /* offcore_response_0 */ | |
47 | EXTRA_REG_RSP_1 = 1, /* offcore_response_1 */ | |
b36817e8 | 48 | EXTRA_REG_LBR = 2, /* lbr_select */ |
f20093ee | 49 | EXTRA_REG_LDLAT = 3, /* ld_lat_threshold */ |
de0428a7 KW |
50 | |
51 | EXTRA_REG_MAX /* number of entries needed */ | |
52 | }; | |
53 | ||
54 | struct event_constraint { | |
55 | union { | |
56 | unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | |
57 | u64 idxmsk64; | |
58 | }; | |
59 | u64 code; | |
60 | u64 cmask; | |
61 | int weight; | |
bc1738f6 | 62 | int overlap; |
9fac2cf3 | 63 | int flags; |
de0428a7 | 64 | }; |
f20093ee | 65 | /* |
2f7f73a5 | 66 | * struct hw_perf_event.flags flags |
f20093ee SE |
67 | */ |
68 | #define PERF_X86_EVENT_PEBS_LDLAT 0x1 /* ld+ldlat data address sampling */ | |
9ad64c0f | 69 | #define PERF_X86_EVENT_PEBS_ST 0x2 /* st data address sampling */ |
86a04461 | 70 | #define PERF_X86_EVENT_PEBS_ST_HSW 0x4 /* haswell style datala, store */ |
2f7f73a5 | 71 | #define PERF_X86_EVENT_COMMITTED 0x8 /* event passed commit_txn */ |
86a04461 AK |
72 | #define PERF_X86_EVENT_PEBS_LD_HSW 0x10 /* haswell style datala, load */ |
73 | #define PERF_X86_EVENT_PEBS_NA_HSW 0x20 /* haswell style datala, unknown */ | |
6f6539ca | 74 | #define PERF_X86_EVENT_EXCL 0x40 /* HT exclusivity on counter */ |
e979121b | 75 | #define PERF_X86_EVENT_DYNAMIC 0x80 /* dynamic alloc'd constraint */ |
7911d3f7 AL |
76 | #define PERF_X86_EVENT_RDPMC_ALLOWED 0x40 /* grant rdpmc permission */ |
77 | ||
de0428a7 KW |
78 | |
79 | struct amd_nb { | |
80 | int nb_id; /* NorthBridge id */ | |
81 | int refcnt; /* reference count */ | |
82 | struct perf_event *owners[X86_PMC_IDX_MAX]; | |
83 | struct event_constraint event_constraints[X86_PMC_IDX_MAX]; | |
84 | }; | |
85 | ||
86 | /* The maximal number of PEBS events: */ | |
70ab7003 | 87 | #define MAX_PEBS_EVENTS 8 |
de0428a7 KW |
88 | |
89 | /* | |
90 | * A debug store configuration. | |
91 | * | |
92 | * We only support architectures that use 64bit fields. | |
93 | */ | |
94 | struct debug_store { | |
95 | u64 bts_buffer_base; | |
96 | u64 bts_index; | |
97 | u64 bts_absolute_maximum; | |
98 | u64 bts_interrupt_threshold; | |
99 | u64 pebs_buffer_base; | |
100 | u64 pebs_index; | |
101 | u64 pebs_absolute_maximum; | |
102 | u64 pebs_interrupt_threshold; | |
103 | u64 pebs_event_reset[MAX_PEBS_EVENTS]; | |
104 | }; | |
105 | ||
106 | /* | |
107 | * Per register state. | |
108 | */ | |
109 | struct er_account { | |
110 | raw_spinlock_t lock; /* per-core: protect structure */ | |
111 | u64 config; /* extra MSR config */ | |
112 | u64 reg; /* extra MSR number */ | |
113 | atomic_t ref; /* reference count */ | |
114 | }; | |
115 | ||
116 | /* | |
117 | * Per core/cpu state | |
118 | * | |
119 | * Used to coordinate shared registers between HT threads or | |
120 | * among events on a single PMU. | |
121 | */ | |
122 | struct intel_shared_regs { | |
123 | struct er_account regs[EXTRA_REG_MAX]; | |
124 | int refcnt; /* per-core: #HT threads */ | |
125 | unsigned core_id; /* per-core: core id */ | |
126 | }; | |
127 | ||
6f6539ca MD |
128 | enum intel_excl_state_type { |
129 | INTEL_EXCL_UNUSED = 0, /* counter is unused */ | |
130 | INTEL_EXCL_SHARED = 1, /* counter can be used by both threads */ | |
131 | INTEL_EXCL_EXCLUSIVE = 2, /* counter can be used by one thread only */ | |
132 | }; | |
133 | ||
134 | struct intel_excl_states { | |
135 | enum intel_excl_state_type init_state[X86_PMC_IDX_MAX]; | |
136 | enum intel_excl_state_type state[X86_PMC_IDX_MAX]; | |
e979121b | 137 | bool sched_started; /* true if scheduling has started */ |
6f6539ca MD |
138 | }; |
139 | ||
140 | struct intel_excl_cntrs { | |
141 | raw_spinlock_t lock; | |
142 | ||
143 | struct intel_excl_states states[2]; | |
144 | ||
145 | int refcnt; /* per-core: #HT threads */ | |
146 | unsigned core_id; /* per-core: core id */ | |
147 | }; | |
148 | ||
de0428a7 KW |
149 | #define MAX_LBR_ENTRIES 16 |
150 | ||
90413464 SE |
151 | enum { |
152 | X86_PERF_KFREE_SHARED = 0, | |
153 | X86_PERF_KFREE_EXCL = 1, | |
154 | X86_PERF_KFREE_MAX | |
155 | }; | |
156 | ||
de0428a7 KW |
157 | struct cpu_hw_events { |
158 | /* | |
159 | * Generic x86 PMC bits | |
160 | */ | |
161 | struct perf_event *events[X86_PMC_IDX_MAX]; /* in counter order */ | |
162 | unsigned long active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | |
163 | unsigned long running[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; | |
164 | int enabled; | |
165 | ||
c347a2f1 PZ |
166 | int n_events; /* the # of events in the below arrays */ |
167 | int n_added; /* the # last events in the below arrays; | |
168 | they've never been enabled yet */ | |
169 | int n_txn; /* the # last events in the below arrays; | |
170 | added in the current transaction */ | |
de0428a7 KW |
171 | int assign[X86_PMC_IDX_MAX]; /* event to counter assignment */ |
172 | u64 tags[X86_PMC_IDX_MAX]; | |
173 | struct perf_event *event_list[X86_PMC_IDX_MAX]; /* in enabled order */ | |
174 | ||
175 | unsigned int group_flag; | |
5a425294 | 176 | int is_fake; |
de0428a7 KW |
177 | |
178 | /* | |
179 | * Intel DebugStore bits | |
180 | */ | |
181 | struct debug_store *ds; | |
182 | u64 pebs_enabled; | |
183 | ||
184 | /* | |
185 | * Intel LBR bits | |
186 | */ | |
187 | int lbr_users; | |
188 | void *lbr_context; | |
189 | struct perf_branch_stack lbr_stack; | |
190 | struct perf_branch_entry lbr_entries[MAX_LBR_ENTRIES]; | |
b36817e8 | 191 | struct er_account *lbr_sel; |
3e702ff6 | 192 | u64 br_sel; |
de0428a7 | 193 | |
144d31e6 GN |
194 | /* |
195 | * Intel host/guest exclude bits | |
196 | */ | |
197 | u64 intel_ctrl_guest_mask; | |
198 | u64 intel_ctrl_host_mask; | |
199 | struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX]; | |
200 | ||
2b9e344d PZ |
201 | /* |
202 | * Intel checkpoint mask | |
203 | */ | |
204 | u64 intel_cp_status; | |
205 | ||
de0428a7 KW |
206 | /* |
207 | * manage shared (per-core, per-cpu) registers | |
208 | * used on Intel NHM/WSM/SNB | |
209 | */ | |
210 | struct intel_shared_regs *shared_regs; | |
6f6539ca MD |
211 | /* |
212 | * manage exclusive counter access between hyperthread | |
213 | */ | |
214 | struct event_constraint *constraint_list; /* in enable order */ | |
215 | struct intel_excl_cntrs *excl_cntrs; | |
216 | int excl_thread_id; /* 0 or 1 */ | |
de0428a7 KW |
217 | |
218 | /* | |
219 | * AMD specific bits | |
220 | */ | |
1018faa6 JR |
221 | struct amd_nb *amd_nb; |
222 | /* Inverted mask of bits to clear in the perf_ctr ctrl registers */ | |
223 | u64 perf_ctr_virt_mask; | |
de0428a7 | 224 | |
90413464 | 225 | void *kfree_on_online[X86_PERF_KFREE_MAX]; |
de0428a7 KW |
226 | }; |
227 | ||
9fac2cf3 | 228 | #define __EVENT_CONSTRAINT(c, n, m, w, o, f) {\ |
de0428a7 KW |
229 | { .idxmsk64 = (n) }, \ |
230 | .code = (c), \ | |
231 | .cmask = (m), \ | |
232 | .weight = (w), \ | |
bc1738f6 | 233 | .overlap = (o), \ |
9fac2cf3 | 234 | .flags = f, \ |
de0428a7 KW |
235 | } |
236 | ||
237 | #define EVENT_CONSTRAINT(c, n, m) \ | |
9fac2cf3 | 238 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 0, 0) |
bc1738f6 | 239 | |
6f6539ca MD |
240 | #define INTEL_EXCLEVT_CONSTRAINT(c, n) \ |
241 | __EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT, HWEIGHT(n),\ | |
242 | 0, PERF_X86_EVENT_EXCL) | |
243 | ||
bc1738f6 RR |
244 | /* |
245 | * The overlap flag marks event constraints with overlapping counter | |
246 | * masks. This is the case if the counter mask of such an event is not | |
247 | * a subset of any other counter mask of a constraint with an equal or | |
248 | * higher weight, e.g.: | |
249 | * | |
250 | * c_overlaps = EVENT_CONSTRAINT_OVERLAP(0, 0x09, 0); | |
251 | * c_another1 = EVENT_CONSTRAINT(0, 0x07, 0); | |
252 | * c_another2 = EVENT_CONSTRAINT(0, 0x38, 0); | |
253 | * | |
254 | * The event scheduler may not select the correct counter in the first | |
255 | * cycle because it needs to know which subsequent events will be | |
256 | * scheduled. It may fail to schedule the events then. So we set the | |
257 | * overlap flag for such constraints to give the scheduler a hint which | |
258 | * events to select for counter rescheduling. | |
259 | * | |
260 | * Care must be taken as the rescheduling algorithm is O(n!) which | |
261 | * will increase scheduling cycles for an over-commited system | |
262 | * dramatically. The number of such EVENT_CONSTRAINT_OVERLAP() macros | |
263 | * and its counter masks must be kept at a minimum. | |
264 | */ | |
265 | #define EVENT_CONSTRAINT_OVERLAP(c, n, m) \ | |
9fac2cf3 | 266 | __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n), 1, 0) |
de0428a7 KW |
267 | |
268 | /* | |
269 | * Constraint on the Event code. | |
270 | */ | |
271 | #define INTEL_EVENT_CONSTRAINT(c, n) \ | |
272 | EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT) | |
273 | ||
274 | /* | |
275 | * Constraint on the Event code + UMask + fixed-mask | |
276 | * | |
277 | * filter mask to validate fixed counter events. | |
278 | * the following filters disqualify for fixed counters: | |
279 | * - inv | |
280 | * - edge | |
281 | * - cnt-mask | |
3a632cb2 AK |
282 | * - in_tx |
283 | * - in_tx_checkpointed | |
de0428a7 KW |
284 | * The other filters are supported by fixed counters. |
285 | * The any-thread option is supported starting with v3. | |
286 | */ | |
3a632cb2 | 287 | #define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_IN_TX|HSW_IN_TX_CHECKPOINTED) |
de0428a7 | 288 | #define FIXED_EVENT_CONSTRAINT(c, n) \ |
3a632cb2 | 289 | EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS) |
de0428a7 KW |
290 | |
291 | /* | |
292 | * Constraint on the Event code + UMask | |
293 | */ | |
294 | #define INTEL_UEVENT_CONSTRAINT(c, n) \ | |
295 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK) | |
296 | ||
7550ddff AK |
297 | /* Like UEVENT_CONSTRAINT, but match flags too */ |
298 | #define INTEL_FLAGS_UEVENT_CONSTRAINT(c, n) \ | |
299 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS) | |
300 | ||
e979121b MD |
301 | #define INTEL_EXCLUEVT_CONSTRAINT(c, n) \ |
302 | __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK, \ | |
303 | HWEIGHT(n), 0, PERF_X86_EVENT_EXCL) | |
304 | ||
f20093ee | 305 | #define INTEL_PLD_CONSTRAINT(c, n) \ |
86a04461 | 306 | __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ |
f20093ee SE |
307 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LDLAT) |
308 | ||
9ad64c0f | 309 | #define INTEL_PST_CONSTRAINT(c, n) \ |
86a04461 | 310 | __EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ |
9ad64c0f SE |
311 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST) |
312 | ||
86a04461 AK |
313 | /* Event constraint, but match on all event flags too. */ |
314 | #define INTEL_FLAGS_EVENT_CONSTRAINT(c, n) \ | |
315 | EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS) | |
316 | ||
317 | /* Check only flags, but allow all event/umask */ | |
318 | #define INTEL_ALL_EVENT_CONSTRAINT(code, n) \ | |
319 | EVENT_CONSTRAINT(code, n, X86_ALL_EVENT_FLAGS) | |
320 | ||
321 | /* Check flags and event code, and set the HSW store flag */ | |
322 | #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_ST(code, n) \ | |
323 | __EVENT_CONSTRAINT(code, n, \ | |
324 | ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \ | |
325 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW) | |
326 | ||
327 | /* Check flags and event code, and set the HSW load flag */ | |
328 | #define INTEL_FLAGS_EVENT_CONSTRAINT_DATALA_LD(code, n) \ | |
329 | __EVENT_CONSTRAINT(code, n, \ | |
330 | ARCH_PERFMON_EVENTSEL_EVENT|X86_ALL_EVENT_FLAGS, \ | |
331 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW) | |
332 | ||
333 | /* Check flags and event code/umask, and set the HSW store flag */ | |
334 | #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_ST(code, n) \ | |
335 | __EVENT_CONSTRAINT(code, n, \ | |
336 | INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ | |
f9134f36 AK |
337 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_ST_HSW) |
338 | ||
86a04461 AK |
339 | /* Check flags and event code/umask, and set the HSW load flag */ |
340 | #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_LD(code, n) \ | |
341 | __EVENT_CONSTRAINT(code, n, \ | |
342 | INTEL_ARCH_EVENT_MASK|X86_ALL_EVENT_FLAGS, \ | |
343 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_LD_HSW) | |
344 | ||
345 | /* Check flags and event code/umask, and set the HSW N/A flag */ | |
346 | #define INTEL_FLAGS_UEVENT_CONSTRAINT_DATALA_NA(code, n) \ | |
347 | __EVENT_CONSTRAINT(code, n, \ | |
348 | INTEL_ARCH_EVENT_MASK|INTEL_ARCH_EVENT_MASK, \ | |
349 | HWEIGHT(n), 0, PERF_X86_EVENT_PEBS_NA_HSW) | |
350 | ||
351 | ||
cf30d52e MD |
352 | /* |
353 | * We define the end marker as having a weight of -1 | |
354 | * to enable blacklisting of events using a counter bitmask | |
355 | * of zero and thus a weight of zero. | |
356 | * The end marker has a weight that cannot possibly be | |
357 | * obtained from counting the bits in the bitmask. | |
358 | */ | |
359 | #define EVENT_CONSTRAINT_END { .weight = -1 } | |
de0428a7 | 360 | |
cf30d52e MD |
361 | /* |
362 | * Check for end marker with weight == -1 | |
363 | */ | |
de0428a7 | 364 | #define for_each_event_constraint(e, c) \ |
cf30d52e | 365 | for ((e) = (c); (e)->weight != -1; (e)++) |
de0428a7 KW |
366 | |
367 | /* | |
368 | * Extra registers for specific events. | |
369 | * | |
370 | * Some events need large masks and require external MSRs. | |
371 | * Those extra MSRs end up being shared for all events on | |
372 | * a PMU and sometimes between PMU of sibling HT threads. | |
373 | * In either case, the kernel needs to handle conflicting | |
374 | * accesses to those extra, shared, regs. The data structure | |
375 | * to manage those registers is stored in cpu_hw_event. | |
376 | */ | |
377 | struct extra_reg { | |
378 | unsigned int event; | |
379 | unsigned int msr; | |
380 | u64 config_mask; | |
381 | u64 valid_mask; | |
382 | int idx; /* per_xxx->regs[] reg index */ | |
338b522c | 383 | bool extra_msr_access; |
de0428a7 KW |
384 | }; |
385 | ||
386 | #define EVENT_EXTRA_REG(e, ms, m, vm, i) { \ | |
338b522c KL |
387 | .event = (e), \ |
388 | .msr = (ms), \ | |
389 | .config_mask = (m), \ | |
390 | .valid_mask = (vm), \ | |
391 | .idx = EXTRA_REG_##i, \ | |
392 | .extra_msr_access = true, \ | |
de0428a7 KW |
393 | } |
394 | ||
395 | #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx) \ | |
396 | EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx) | |
397 | ||
f20093ee SE |
398 | #define INTEL_UEVENT_EXTRA_REG(event, msr, vm, idx) \ |
399 | EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT | \ | |
400 | ARCH_PERFMON_EVENTSEL_UMASK, vm, idx) | |
401 | ||
402 | #define INTEL_UEVENT_PEBS_LDLAT_EXTRA_REG(c) \ | |
403 | INTEL_UEVENT_EXTRA_REG(c, \ | |
404 | MSR_PEBS_LD_LAT_THRESHOLD, \ | |
405 | 0xffff, \ | |
406 | LDLAT) | |
407 | ||
de0428a7 KW |
408 | #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0) |
409 | ||
410 | union perf_capabilities { | |
411 | struct { | |
412 | u64 lbr_format:6; | |
413 | u64 pebs_trap:1; | |
414 | u64 pebs_arch_reg:1; | |
415 | u64 pebs_format:4; | |
416 | u64 smm_freeze:1; | |
069e0c3c AK |
417 | /* |
418 | * PMU supports separate counter range for writing | |
419 | * values > 32bit. | |
420 | */ | |
421 | u64 full_width_write:1; | |
de0428a7 KW |
422 | }; |
423 | u64 capabilities; | |
424 | }; | |
425 | ||
c1d6f42f PZ |
426 | struct x86_pmu_quirk { |
427 | struct x86_pmu_quirk *next; | |
428 | void (*func)(void); | |
429 | }; | |
430 | ||
f9b4eeb8 PZ |
431 | union x86_pmu_config { |
432 | struct { | |
433 | u64 event:8, | |
434 | umask:8, | |
435 | usr:1, | |
436 | os:1, | |
437 | edge:1, | |
438 | pc:1, | |
439 | interrupt:1, | |
440 | __reserved1:1, | |
441 | en:1, | |
442 | inv:1, | |
443 | cmask:8, | |
444 | event2:4, | |
445 | __reserved2:4, | |
446 | go:1, | |
447 | ho:1; | |
448 | } bits; | |
449 | u64 value; | |
450 | }; | |
451 | ||
452 | #define X86_CONFIG(args...) ((union x86_pmu_config){.bits = {args}}).value | |
453 | ||
48070342 AS |
454 | enum { |
455 | x86_lbr_exclusive_lbr, | |
8062382c | 456 | x86_lbr_exclusive_bts, |
48070342 AS |
457 | x86_lbr_exclusive_pt, |
458 | x86_lbr_exclusive_max, | |
459 | }; | |
460 | ||
de0428a7 KW |
461 | /* |
462 | * struct x86_pmu - generic x86 pmu | |
463 | */ | |
464 | struct x86_pmu { | |
465 | /* | |
466 | * Generic x86 PMC bits | |
467 | */ | |
468 | const char *name; | |
469 | int version; | |
470 | int (*handle_irq)(struct pt_regs *); | |
471 | void (*disable_all)(void); | |
472 | void (*enable_all)(int added); | |
473 | void (*enable)(struct perf_event *); | |
474 | void (*disable)(struct perf_event *); | |
475 | int (*hw_config)(struct perf_event *event); | |
476 | int (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign); | |
477 | unsigned eventsel; | |
478 | unsigned perfctr; | |
4c1fd17a | 479 | int (*addr_offset)(int index, bool eventsel); |
0fbdad07 | 480 | int (*rdpmc_index)(int index); |
de0428a7 KW |
481 | u64 (*event_map)(int); |
482 | int max_events; | |
483 | int num_counters; | |
484 | int num_counters_fixed; | |
485 | int cntval_bits; | |
486 | u64 cntval_mask; | |
ffb871bc GN |
487 | union { |
488 | unsigned long events_maskl; | |
489 | unsigned long events_mask[BITS_TO_LONGS(ARCH_PERFMON_EVENTS_COUNT)]; | |
490 | }; | |
491 | int events_mask_len; | |
de0428a7 KW |
492 | int apic; |
493 | u64 max_period; | |
494 | struct event_constraint * | |
495 | (*get_event_constraints)(struct cpu_hw_events *cpuc, | |
79cba822 | 496 | int idx, |
de0428a7 KW |
497 | struct perf_event *event); |
498 | ||
499 | void (*put_event_constraints)(struct cpu_hw_events *cpuc, | |
500 | struct perf_event *event); | |
c5362c0c MD |
501 | |
502 | void (*commit_scheduling)(struct cpu_hw_events *cpuc, | |
503 | struct perf_event *event, | |
504 | int cntr); | |
505 | ||
506 | void (*start_scheduling)(struct cpu_hw_events *cpuc); | |
507 | ||
508 | void (*stop_scheduling)(struct cpu_hw_events *cpuc); | |
509 | ||
de0428a7 | 510 | struct event_constraint *event_constraints; |
c1d6f42f | 511 | struct x86_pmu_quirk *quirks; |
de0428a7 | 512 | int perfctr_second_write; |
72db5596 | 513 | bool late_ack; |
294fe0f5 | 514 | unsigned (*limit_period)(struct perf_event *event, unsigned l); |
de0428a7 | 515 | |
0c9d42ed PZ |
516 | /* |
517 | * sysfs attrs | |
518 | */ | |
e97df763 | 519 | int attr_rdpmc_broken; |
0c9d42ed | 520 | int attr_rdpmc; |
641cc938 | 521 | struct attribute **format_attrs; |
f20093ee | 522 | struct attribute **event_attrs; |
0c9d42ed | 523 | |
a4747393 | 524 | ssize_t (*events_sysfs_show)(char *page, u64 config); |
1a6461b1 | 525 | struct attribute **cpu_events; |
a4747393 | 526 | |
0c9d42ed PZ |
527 | /* |
528 | * CPU Hotplug hooks | |
529 | */ | |
de0428a7 KW |
530 | int (*cpu_prepare)(int cpu); |
531 | void (*cpu_starting)(int cpu); | |
532 | void (*cpu_dying)(int cpu); | |
533 | void (*cpu_dead)(int cpu); | |
c93dc84c PZ |
534 | |
535 | void (*check_microcode)(void); | |
ba532500 YZ |
536 | void (*sched_task)(struct perf_event_context *ctx, |
537 | bool sched_in); | |
de0428a7 KW |
538 | |
539 | /* | |
540 | * Intel Arch Perfmon v2+ | |
541 | */ | |
542 | u64 intel_ctrl; | |
543 | union perf_capabilities intel_cap; | |
544 | ||
545 | /* | |
546 | * Intel DebugStore bits | |
547 | */ | |
597ed953 | 548 | unsigned int bts :1, |
3e0091e2 PZ |
549 | bts_active :1, |
550 | pebs :1, | |
551 | pebs_active :1, | |
552 | pebs_broken :1; | |
de0428a7 KW |
553 | int pebs_record_size; |
554 | void (*drain_pebs)(struct pt_regs *regs); | |
555 | struct event_constraint *pebs_constraints; | |
0780c927 | 556 | void (*pebs_aliases)(struct perf_event *event); |
70ab7003 | 557 | int max_pebs_events; |
de0428a7 KW |
558 | |
559 | /* | |
560 | * Intel LBR | |
561 | */ | |
562 | unsigned long lbr_tos, lbr_from, lbr_to; /* MSR base regs */ | |
563 | int lbr_nr; /* hardware stack size */ | |
b36817e8 SE |
564 | u64 lbr_sel_mask; /* LBR_SELECT valid bits */ |
565 | const int *lbr_sel_map; /* lbr_select mappings */ | |
b7af41a1 | 566 | bool lbr_double_abort; /* duplicated lbr aborts */ |
de0428a7 | 567 | |
48070342 AS |
568 | /* |
569 | * Intel PT/LBR/BTS are exclusive | |
570 | */ | |
571 | atomic_t lbr_exclusive[x86_lbr_exclusive_max]; | |
572 | ||
de0428a7 KW |
573 | /* |
574 | * Extra registers for events | |
575 | */ | |
576 | struct extra_reg *extra_regs; | |
9a5e3fb5 | 577 | unsigned int flags; |
144d31e6 GN |
578 | |
579 | /* | |
580 | * Intel host/guest support (KVM) | |
581 | */ | |
582 | struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr); | |
de0428a7 KW |
583 | }; |
584 | ||
e18bf526 YZ |
585 | struct x86_perf_task_context { |
586 | u64 lbr_from[MAX_LBR_ENTRIES]; | |
587 | u64 lbr_to[MAX_LBR_ENTRIES]; | |
588 | int lbr_callstack_users; | |
589 | int lbr_stack_state; | |
590 | }; | |
591 | ||
c1d6f42f PZ |
592 | #define x86_add_quirk(func_) \ |
593 | do { \ | |
594 | static struct x86_pmu_quirk __quirk __initdata = { \ | |
595 | .func = func_, \ | |
596 | }; \ | |
597 | __quirk.next = x86_pmu.quirks; \ | |
598 | x86_pmu.quirks = &__quirk; \ | |
599 | } while (0) | |
600 | ||
9a5e3fb5 SE |
601 | /* |
602 | * x86_pmu flags | |
603 | */ | |
604 | #define PMU_FL_NO_HT_SHARING 0x1 /* no hyper-threading resource sharing */ | |
605 | #define PMU_FL_HAS_RSP_1 0x2 /* has 2 equivalent offcore_rsp regs */ | |
6f6539ca | 606 | #define PMU_FL_EXCL_CNTRS 0x4 /* has exclusive counter requirements */ |
de0428a7 | 607 | |
3a54aaa0 SE |
608 | #define EVENT_VAR(_id) event_attr_##_id |
609 | #define EVENT_PTR(_id) &event_attr_##_id.attr.attr | |
610 | ||
611 | #define EVENT_ATTR(_name, _id) \ | |
612 | static struct perf_pmu_events_attr EVENT_VAR(_id) = { \ | |
613 | .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \ | |
614 | .id = PERF_COUNT_HW_##_id, \ | |
615 | .event_str = NULL, \ | |
616 | }; | |
617 | ||
618 | #define EVENT_ATTR_STR(_name, v, str) \ | |
619 | static struct perf_pmu_events_attr event_attr_##v = { \ | |
620 | .attr = __ATTR(_name, 0444, events_sysfs_show, NULL), \ | |
621 | .id = 0, \ | |
622 | .event_str = str, \ | |
623 | }; | |
624 | ||
de0428a7 KW |
625 | extern struct x86_pmu x86_pmu __read_mostly; |
626 | ||
e9d7f7cd YZ |
627 | static inline bool x86_pmu_has_lbr_callstack(void) |
628 | { | |
629 | return x86_pmu.lbr_sel_map && | |
630 | x86_pmu.lbr_sel_map[PERF_SAMPLE_BRANCH_CALL_STACK_SHIFT] > 0; | |
631 | } | |
632 | ||
de0428a7 KW |
633 | DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events); |
634 | ||
635 | int x86_perf_event_set_period(struct perf_event *event); | |
636 | ||
637 | /* | |
638 | * Generalized hw caching related hw_event table, filled | |
639 | * in on a per model basis. A value of 0 means | |
640 | * 'not supported', -1 means 'hw_event makes no sense on | |
641 | * this CPU', any other value means the raw hw_event | |
642 | * ID. | |
643 | */ | |
644 | ||
645 | #define C(x) PERF_COUNT_HW_CACHE_##x | |
646 | ||
647 | extern u64 __read_mostly hw_cache_event_ids | |
648 | [PERF_COUNT_HW_CACHE_MAX] | |
649 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
650 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; | |
651 | extern u64 __read_mostly hw_cache_extra_regs | |
652 | [PERF_COUNT_HW_CACHE_MAX] | |
653 | [PERF_COUNT_HW_CACHE_OP_MAX] | |
654 | [PERF_COUNT_HW_CACHE_RESULT_MAX]; | |
655 | ||
656 | u64 x86_perf_event_update(struct perf_event *event); | |
657 | ||
de0428a7 KW |
658 | static inline unsigned int x86_pmu_config_addr(int index) |
659 | { | |
4c1fd17a JS |
660 | return x86_pmu.eventsel + (x86_pmu.addr_offset ? |
661 | x86_pmu.addr_offset(index, true) : index); | |
de0428a7 KW |
662 | } |
663 | ||
664 | static inline unsigned int x86_pmu_event_addr(int index) | |
665 | { | |
4c1fd17a JS |
666 | return x86_pmu.perfctr + (x86_pmu.addr_offset ? |
667 | x86_pmu.addr_offset(index, false) : index); | |
de0428a7 KW |
668 | } |
669 | ||
0fbdad07 JS |
670 | static inline int x86_pmu_rdpmc_index(int index) |
671 | { | |
672 | return x86_pmu.rdpmc_index ? x86_pmu.rdpmc_index(index) : index; | |
673 | } | |
674 | ||
48070342 AS |
675 | int x86_add_exclusive(unsigned int what); |
676 | ||
677 | void x86_del_exclusive(unsigned int what); | |
678 | ||
679 | void hw_perf_lbr_event_destroy(struct perf_event *event); | |
680 | ||
de0428a7 KW |
681 | int x86_setup_perfctr(struct perf_event *event); |
682 | ||
683 | int x86_pmu_hw_config(struct perf_event *event); | |
684 | ||
685 | void x86_pmu_disable_all(void); | |
686 | ||
687 | static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc, | |
688 | u64 enable_mask) | |
689 | { | |
1018faa6 JR |
690 | u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask); |
691 | ||
de0428a7 KW |
692 | if (hwc->extra_reg.reg) |
693 | wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config); | |
1018faa6 | 694 | wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask); |
de0428a7 KW |
695 | } |
696 | ||
697 | void x86_pmu_enable_all(int added); | |
698 | ||
43b45780 | 699 | int perf_assign_events(struct perf_event **events, int n, |
4b4969b1 | 700 | int wmin, int wmax, int *assign); |
de0428a7 KW |
701 | int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign); |
702 | ||
703 | void x86_pmu_stop(struct perf_event *event, int flags); | |
704 | ||
705 | static inline void x86_pmu_disable_event(struct perf_event *event) | |
706 | { | |
707 | struct hw_perf_event *hwc = &event->hw; | |
708 | ||
709 | wrmsrl(hwc->config_base, hwc->config); | |
710 | } | |
711 | ||
712 | void x86_pmu_enable_event(struct perf_event *event); | |
713 | ||
714 | int x86_pmu_handle_irq(struct pt_regs *regs); | |
715 | ||
716 | extern struct event_constraint emptyconstraint; | |
717 | ||
718 | extern struct event_constraint unconstrained; | |
719 | ||
3e702ff6 SE |
720 | static inline bool kernel_ip(unsigned long ip) |
721 | { | |
722 | #ifdef CONFIG_X86_32 | |
723 | return ip > PAGE_OFFSET; | |
724 | #else | |
725 | return (long)ip < 0; | |
726 | #endif | |
727 | } | |
728 | ||
d07bdfd3 PZ |
729 | /* |
730 | * Not all PMUs provide the right context information to place the reported IP | |
731 | * into full context. Specifically segment registers are typically not | |
732 | * supplied. | |
733 | * | |
734 | * Assuming the address is a linear address (it is for IBS), we fake the CS and | |
735 | * vm86 mode using the known zero-based code segment and 'fix up' the registers | |
736 | * to reflect this. | |
737 | * | |
738 | * Intel PEBS/LBR appear to typically provide the effective address, nothing | |
739 | * much we can do about that but pray and treat it like a linear address. | |
740 | */ | |
741 | static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip) | |
742 | { | |
743 | regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS; | |
744 | if (regs->flags & X86_VM_MASK) | |
745 | regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK); | |
746 | regs->ip = ip; | |
747 | } | |
748 | ||
0bf79d44 | 749 | ssize_t x86_event_sysfs_show(char *page, u64 config, u64 event); |
20550a43 | 750 | ssize_t intel_event_sysfs_show(char *page, u64 config); |
43c032fe | 751 | |
de0428a7 KW |
752 | #ifdef CONFIG_CPU_SUP_AMD |
753 | ||
754 | int amd_pmu_init(void); | |
755 | ||
756 | #else /* CONFIG_CPU_SUP_AMD */ | |
757 | ||
758 | static inline int amd_pmu_init(void) | |
759 | { | |
760 | return 0; | |
761 | } | |
762 | ||
763 | #endif /* CONFIG_CPU_SUP_AMD */ | |
764 | ||
765 | #ifdef CONFIG_CPU_SUP_INTEL | |
766 | ||
48070342 AS |
767 | static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event) |
768 | { | |
769 | /* user explicitly requested branch sampling */ | |
770 | if (has_branch_stack(event)) | |
771 | return true; | |
772 | ||
773 | /* implicit branch sampling to correct PEBS skid */ | |
774 | if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1 && | |
775 | x86_pmu.intel_cap.pebs_format < 2) | |
776 | return true; | |
777 | ||
778 | return false; | |
779 | } | |
780 | ||
781 | static inline bool intel_pmu_has_bts(struct perf_event *event) | |
782 | { | |
783 | if (event->attr.config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS && | |
784 | !event->attr.freq && event->hw.sample_period == 1) | |
785 | return true; | |
786 | ||
787 | return false; | |
788 | } | |
789 | ||
de0428a7 KW |
790 | int intel_pmu_save_and_restart(struct perf_event *event); |
791 | ||
792 | struct event_constraint * | |
79cba822 SE |
793 | x86_get_event_constraints(struct cpu_hw_events *cpuc, int idx, |
794 | struct perf_event *event); | |
de0428a7 KW |
795 | |
796 | struct intel_shared_regs *allocate_shared_regs(int cpu); | |
797 | ||
798 | int intel_pmu_init(void); | |
799 | ||
800 | void init_debug_store_on_cpu(int cpu); | |
801 | ||
802 | void fini_debug_store_on_cpu(int cpu); | |
803 | ||
804 | void release_ds_buffers(void); | |
805 | ||
806 | void reserve_ds_buffers(void); | |
807 | ||
808 | extern struct event_constraint bts_constraint; | |
809 | ||
810 | void intel_pmu_enable_bts(u64 config); | |
811 | ||
812 | void intel_pmu_disable_bts(void); | |
813 | ||
814 | int intel_pmu_drain_bts_buffer(void); | |
815 | ||
816 | extern struct event_constraint intel_core2_pebs_event_constraints[]; | |
817 | ||
818 | extern struct event_constraint intel_atom_pebs_event_constraints[]; | |
819 | ||
1fa64180 YZ |
820 | extern struct event_constraint intel_slm_pebs_event_constraints[]; |
821 | ||
de0428a7 KW |
822 | extern struct event_constraint intel_nehalem_pebs_event_constraints[]; |
823 | ||
824 | extern struct event_constraint intel_westmere_pebs_event_constraints[]; | |
825 | ||
826 | extern struct event_constraint intel_snb_pebs_event_constraints[]; | |
827 | ||
20a36e39 SE |
828 | extern struct event_constraint intel_ivb_pebs_event_constraints[]; |
829 | ||
3044318f AK |
830 | extern struct event_constraint intel_hsw_pebs_event_constraints[]; |
831 | ||
de0428a7 KW |
832 | struct event_constraint *intel_pebs_constraints(struct perf_event *event); |
833 | ||
834 | void intel_pmu_pebs_enable(struct perf_event *event); | |
835 | ||
836 | void intel_pmu_pebs_disable(struct perf_event *event); | |
837 | ||
838 | void intel_pmu_pebs_enable_all(void); | |
839 | ||
840 | void intel_pmu_pebs_disable_all(void); | |
841 | ||
842 | void intel_ds_init(void); | |
843 | ||
2a0ad3b3 YZ |
844 | void intel_pmu_lbr_sched_task(struct perf_event_context *ctx, bool sched_in); |
845 | ||
de0428a7 KW |
846 | void intel_pmu_lbr_reset(void); |
847 | ||
848 | void intel_pmu_lbr_enable(struct perf_event *event); | |
849 | ||
850 | void intel_pmu_lbr_disable(struct perf_event *event); | |
851 | ||
852 | void intel_pmu_lbr_enable_all(void); | |
853 | ||
854 | void intel_pmu_lbr_disable_all(void); | |
855 | ||
856 | void intel_pmu_lbr_read(void); | |
857 | ||
858 | void intel_pmu_lbr_init_core(void); | |
859 | ||
860 | void intel_pmu_lbr_init_nhm(void); | |
861 | ||
862 | void intel_pmu_lbr_init_atom(void); | |
863 | ||
c5cc2cd9 SE |
864 | void intel_pmu_lbr_init_snb(void); |
865 | ||
e9d7f7cd YZ |
866 | void intel_pmu_lbr_init_hsw(void); |
867 | ||
60ce0fbd SE |
868 | int intel_pmu_setup_lbr_filter(struct perf_event *event); |
869 | ||
52ca9ced AS |
870 | void intel_pt_interrupt(void); |
871 | ||
8062382c AS |
872 | int intel_bts_interrupt(void); |
873 | ||
874 | void intel_bts_enable_local(void); | |
875 | ||
876 | void intel_bts_disable_local(void); | |
877 | ||
de0428a7 KW |
878 | int p4_pmu_init(void); |
879 | ||
880 | int p6_pmu_init(void); | |
881 | ||
e717bf4e VW |
882 | int knc_pmu_init(void); |
883 | ||
f20093ee SE |
884 | ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, |
885 | char *page); | |
886 | ||
de0428a7 KW |
887 | #else /* CONFIG_CPU_SUP_INTEL */ |
888 | ||
889 | static inline void reserve_ds_buffers(void) | |
890 | { | |
891 | } | |
892 | ||
893 | static inline void release_ds_buffers(void) | |
894 | { | |
895 | } | |
896 | ||
897 | static inline int intel_pmu_init(void) | |
898 | { | |
899 | return 0; | |
900 | } | |
901 | ||
902 | static inline struct intel_shared_regs *allocate_shared_regs(int cpu) | |
903 | { | |
904 | return NULL; | |
905 | } | |
906 | ||
907 | #endif /* CONFIG_CPU_SUP_INTEL */ |