x86: Simplify code by removing a !SMP #ifdefs from 'struct cpuinfo_x86'
[pandora-kernel.git] / arch / x86 / kernel / cpu / perf_event.h
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
17 /*
18  *          |   NHM/WSM    |      SNB     |
19  * register -------------------------------
20  *          |  HT  | no HT |  HT  | no HT |
21  *-----------------------------------------
22  * offcore  | core | core  | cpu  | core  |
23  * lbr_sel  | core | core  | cpu  | core  |
24  * ld_lat   | cpu  | core  | cpu  | core  |
25  *-----------------------------------------
26  *
27  * Given that there is a small number of shared regs,
28  * we can pre-allocate their slot in the per-cpu
29  * per-core reg tables.
30  */
31 enum extra_reg_type {
32         EXTRA_REG_NONE  = -1,   /* not used */
33
34         EXTRA_REG_RSP_0 = 0,    /* offcore_response_0 */
35         EXTRA_REG_RSP_1 = 1,    /* offcore_response_1 */
36
37         EXTRA_REG_MAX           /* number of entries needed */
38 };
39
40 struct event_constraint {
41         union {
42                 unsigned long   idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
43                 u64             idxmsk64;
44         };
45         u64     code;
46         u64     cmask;
47         int     weight;
48 };
49
50 struct amd_nb {
51         int nb_id;  /* NorthBridge id */
52         int refcnt; /* reference count */
53         struct perf_event *owners[X86_PMC_IDX_MAX];
54         struct event_constraint event_constraints[X86_PMC_IDX_MAX];
55 };
56
57 /* The maximal number of PEBS events: */
58 #define MAX_PEBS_EVENTS         4
59
60 /*
61  * A debug store configuration.
62  *
63  * We only support architectures that use 64bit fields.
64  */
65 struct debug_store {
66         u64     bts_buffer_base;
67         u64     bts_index;
68         u64     bts_absolute_maximum;
69         u64     bts_interrupt_threshold;
70         u64     pebs_buffer_base;
71         u64     pebs_index;
72         u64     pebs_absolute_maximum;
73         u64     pebs_interrupt_threshold;
74         u64     pebs_event_reset[MAX_PEBS_EVENTS];
75 };
76
77 /*
78  * Per register state.
79  */
80 struct er_account {
81         raw_spinlock_t          lock;   /* per-core: protect structure */
82         u64                 config;     /* extra MSR config */
83         u64                 reg;        /* extra MSR number */
84         atomic_t            ref;        /* reference count */
85 };
86
87 /*
88  * Per core/cpu state
89  *
90  * Used to coordinate shared registers between HT threads or
91  * among events on a single PMU.
92  */
93 struct intel_shared_regs {
94         struct er_account       regs[EXTRA_REG_MAX];
95         int                     refcnt;         /* per-core: #HT threads */
96         unsigned                core_id;        /* per-core: core id */
97 };
98
99 #define MAX_LBR_ENTRIES         16
100
101 struct cpu_hw_events {
102         /*
103          * Generic x86 PMC bits
104          */
105         struct perf_event       *events[X86_PMC_IDX_MAX]; /* in counter order */
106         unsigned long           active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
107         unsigned long           running[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
108         int                     enabled;
109
110         int                     n_events;
111         int                     n_added;
112         int                     n_txn;
113         int                     assign[X86_PMC_IDX_MAX]; /* event to counter assignment */
114         u64                     tags[X86_PMC_IDX_MAX];
115         struct perf_event       *event_list[X86_PMC_IDX_MAX]; /* in enabled order */
116
117         unsigned int            group_flag;
118
119         /*
120          * Intel DebugStore bits
121          */
122         struct debug_store      *ds;
123         u64                     pebs_enabled;
124
125         /*
126          * Intel LBR bits
127          */
128         int                             lbr_users;
129         void                            *lbr_context;
130         struct perf_branch_stack        lbr_stack;
131         struct perf_branch_entry        lbr_entries[MAX_LBR_ENTRIES];
132
133         /*
134          * Intel host/guest exclude bits
135          */
136         u64                             intel_ctrl_guest_mask;
137         u64                             intel_ctrl_host_mask;
138         struct perf_guest_switch_msr    guest_switch_msrs[X86_PMC_IDX_MAX];
139
140         /*
141          * manage shared (per-core, per-cpu) registers
142          * used on Intel NHM/WSM/SNB
143          */
144         struct intel_shared_regs        *shared_regs;
145
146         /*
147          * AMD specific bits
148          */
149         struct amd_nb                   *amd_nb;
150         /* Inverted mask of bits to clear in the perf_ctr ctrl registers */
151         u64                             perf_ctr_virt_mask;
152
153         void                            *kfree_on_online;
154 };
155
156 #define __EVENT_CONSTRAINT(c, n, m, w) {\
157         { .idxmsk64 = (n) },            \
158         .code = (c),                    \
159         .cmask = (m),                   \
160         .weight = (w),                  \
161 }
162
163 #define EVENT_CONSTRAINT(c, n, m)       \
164         __EVENT_CONSTRAINT(c, n, m, HWEIGHT(n))
165
166 /*
167  * Constraint on the Event code.
168  */
169 #define INTEL_EVENT_CONSTRAINT(c, n)    \
170         EVENT_CONSTRAINT(c, n, ARCH_PERFMON_EVENTSEL_EVENT)
171
172 /*
173  * Constraint on the Event code + UMask + fixed-mask
174  *
175  * filter mask to validate fixed counter events.
176  * the following filters disqualify for fixed counters:
177  *  - inv
178  *  - edge
179  *  - cnt-mask
180  *  The other filters are supported by fixed counters.
181  *  The any-thread option is supported starting with v3.
182  */
183 #define FIXED_EVENT_CONSTRAINT(c, n)    \
184         EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
185
186 /*
187  * Constraint on the Event code + UMask
188  */
189 #define INTEL_UEVENT_CONSTRAINT(c, n)   \
190         EVENT_CONSTRAINT(c, n, INTEL_ARCH_EVENT_MASK)
191
192 #define EVENT_CONSTRAINT_END            \
193         EVENT_CONSTRAINT(0, 0, 0)
194
195 #define for_each_event_constraint(e, c) \
196         for ((e) = (c); (e)->weight; (e)++)
197
198 /*
199  * Extra registers for specific events.
200  *
201  * Some events need large masks and require external MSRs.
202  * Those extra MSRs end up being shared for all events on
203  * a PMU and sometimes between PMU of sibling HT threads.
204  * In either case, the kernel needs to handle conflicting
205  * accesses to those extra, shared, regs. The data structure
206  * to manage those registers is stored in cpu_hw_event.
207  */
208 struct extra_reg {
209         unsigned int            event;
210         unsigned int            msr;
211         u64                     config_mask;
212         u64                     valid_mask;
213         int                     idx;  /* per_xxx->regs[] reg index */
214 };
215
216 #define EVENT_EXTRA_REG(e, ms, m, vm, i) {      \
217         .event = (e),           \
218         .msr = (ms),            \
219         .config_mask = (m),     \
220         .valid_mask = (vm),     \
221         .idx = EXTRA_REG_##i    \
222         }
223
224 #define INTEL_EVENT_EXTRA_REG(event, msr, vm, idx)      \
225         EVENT_EXTRA_REG(event, msr, ARCH_PERFMON_EVENTSEL_EVENT, vm, idx)
226
227 #define EVENT_EXTRA_END EVENT_EXTRA_REG(0, 0, 0, 0, RSP_0)
228
229 union perf_capabilities {
230         struct {
231                 u64     lbr_format:6;
232                 u64     pebs_trap:1;
233                 u64     pebs_arch_reg:1;
234                 u64     pebs_format:4;
235                 u64     smm_freeze:1;
236         };
237         u64     capabilities;
238 };
239
240 /*
241  * struct x86_pmu - generic x86 pmu
242  */
243 struct x86_pmu {
244         /*
245          * Generic x86 PMC bits
246          */
247         const char      *name;
248         int             version;
249         int             (*handle_irq)(struct pt_regs *);
250         void            (*disable_all)(void);
251         void            (*enable_all)(int added);
252         void            (*enable)(struct perf_event *);
253         void            (*disable)(struct perf_event *);
254         int             (*hw_config)(struct perf_event *event);
255         int             (*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign);
256         unsigned        eventsel;
257         unsigned        perfctr;
258         u64             (*event_map)(int);
259         int             max_events;
260         int             num_counters;
261         int             num_counters_fixed;
262         int             cntval_bits;
263         u64             cntval_mask;
264         int             apic;
265         u64             max_period;
266         struct event_constraint *
267                         (*get_event_constraints)(struct cpu_hw_events *cpuc,
268                                                  struct perf_event *event);
269
270         void            (*put_event_constraints)(struct cpu_hw_events *cpuc,
271                                                  struct perf_event *event);
272         struct event_constraint *event_constraints;
273         void            (*quirks)(void);
274         int             perfctr_second_write;
275
276         int             (*cpu_prepare)(int cpu);
277         void            (*cpu_starting)(int cpu);
278         void            (*cpu_dying)(int cpu);
279         void            (*cpu_dead)(int cpu);
280
281         /*
282          * Intel Arch Perfmon v2+
283          */
284         u64                     intel_ctrl;
285         union perf_capabilities intel_cap;
286
287         /*
288          * Intel DebugStore bits
289          */
290         int             bts, pebs;
291         int             bts_active, pebs_active;
292         int             pebs_record_size;
293         void            (*drain_pebs)(struct pt_regs *regs);
294         struct event_constraint *pebs_constraints;
295
296         /*
297          * Intel LBR
298          */
299         unsigned long   lbr_tos, lbr_from, lbr_to; /* MSR base regs       */
300         int             lbr_nr;                    /* hardware stack size */
301
302         /*
303          * Extra registers for events
304          */
305         struct extra_reg *extra_regs;
306         unsigned int er_flags;
307
308         /*
309          * Intel host/guest support (KVM)
310          */
311         struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr);
312 };
313
314 #define ERF_NO_HT_SHARING       1
315 #define ERF_HAS_RSP_1           2
316
317 extern struct x86_pmu x86_pmu __read_mostly;
318
319 DECLARE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
320
321 int x86_perf_event_set_period(struct perf_event *event);
322
323 /*
324  * Generalized hw caching related hw_event table, filled
325  * in on a per model basis. A value of 0 means
326  * 'not supported', -1 means 'hw_event makes no sense on
327  * this CPU', any other value means the raw hw_event
328  * ID.
329  */
330
331 #define C(x) PERF_COUNT_HW_CACHE_##x
332
333 extern u64 __read_mostly hw_cache_event_ids
334                                 [PERF_COUNT_HW_CACHE_MAX]
335                                 [PERF_COUNT_HW_CACHE_OP_MAX]
336                                 [PERF_COUNT_HW_CACHE_RESULT_MAX];
337 extern u64 __read_mostly hw_cache_extra_regs
338                                 [PERF_COUNT_HW_CACHE_MAX]
339                                 [PERF_COUNT_HW_CACHE_OP_MAX]
340                                 [PERF_COUNT_HW_CACHE_RESULT_MAX];
341
342 u64 x86_perf_event_update(struct perf_event *event);
343
344 static inline int x86_pmu_addr_offset(int index)
345 {
346         int offset;
347
348         /* offset = X86_FEATURE_PERFCTR_CORE ? index << 1 : index */
349         alternative_io(ASM_NOP2,
350                        "shll $1, %%eax",
351                        X86_FEATURE_PERFCTR_CORE,
352                        "=a" (offset),
353                        "a"  (index));
354
355         return offset;
356 }
357
358 static inline unsigned int x86_pmu_config_addr(int index)
359 {
360         return x86_pmu.eventsel + x86_pmu_addr_offset(index);
361 }
362
363 static inline unsigned int x86_pmu_event_addr(int index)
364 {
365         return x86_pmu.perfctr + x86_pmu_addr_offset(index);
366 }
367
368 int x86_setup_perfctr(struct perf_event *event);
369
370 int x86_pmu_hw_config(struct perf_event *event);
371
372 void x86_pmu_disable_all(void);
373
374 static inline void __x86_pmu_enable_event(struct hw_perf_event *hwc,
375                                           u64 enable_mask)
376 {
377         u64 disable_mask = __this_cpu_read(cpu_hw_events.perf_ctr_virt_mask);
378
379         if (hwc->extra_reg.reg)
380                 wrmsrl(hwc->extra_reg.reg, hwc->extra_reg.config);
381         wrmsrl(hwc->config_base, (hwc->config | enable_mask) & ~disable_mask);
382 }
383
384 void x86_pmu_enable_all(int added);
385
386 int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign);
387
388 void x86_pmu_stop(struct perf_event *event, int flags);
389
390 static inline void x86_pmu_disable_event(struct perf_event *event)
391 {
392         struct hw_perf_event *hwc = &event->hw;
393
394         wrmsrl(hwc->config_base, hwc->config);
395 }
396
397 void x86_pmu_enable_event(struct perf_event *event);
398
399 int x86_pmu_handle_irq(struct pt_regs *regs);
400
401 extern struct event_constraint emptyconstraint;
402
403 extern struct event_constraint unconstrained;
404
405 #ifdef CONFIG_CPU_SUP_AMD
406
407 int amd_pmu_init(void);
408
409 #else /* CONFIG_CPU_SUP_AMD */
410
411 static inline int amd_pmu_init(void)
412 {
413         return 0;
414 }
415
416 #endif /* CONFIG_CPU_SUP_AMD */
417
418 #ifdef CONFIG_CPU_SUP_INTEL
419
420 int intel_pmu_save_and_restart(struct perf_event *event);
421
422 struct event_constraint *
423 x86_get_event_constraints(struct cpu_hw_events *cpuc, struct perf_event *event);
424
425 struct intel_shared_regs *allocate_shared_regs(int cpu);
426
427 int intel_pmu_init(void);
428
429 void init_debug_store_on_cpu(int cpu);
430
431 void fini_debug_store_on_cpu(int cpu);
432
433 void release_ds_buffers(void);
434
435 void reserve_ds_buffers(void);
436
437 extern struct event_constraint bts_constraint;
438
439 void intel_pmu_enable_bts(u64 config);
440
441 void intel_pmu_disable_bts(void);
442
443 int intel_pmu_drain_bts_buffer(void);
444
445 extern struct event_constraint intel_core2_pebs_event_constraints[];
446
447 extern struct event_constraint intel_atom_pebs_event_constraints[];
448
449 extern struct event_constraint intel_nehalem_pebs_event_constraints[];
450
451 extern struct event_constraint intel_westmere_pebs_event_constraints[];
452
453 extern struct event_constraint intel_snb_pebs_event_constraints[];
454
455 struct event_constraint *intel_pebs_constraints(struct perf_event *event);
456
457 void intel_pmu_pebs_enable(struct perf_event *event);
458
459 void intel_pmu_pebs_disable(struct perf_event *event);
460
461 void intel_pmu_pebs_enable_all(void);
462
463 void intel_pmu_pebs_disable_all(void);
464
465 void intel_ds_init(void);
466
467 void intel_pmu_lbr_reset(void);
468
469 void intel_pmu_lbr_enable(struct perf_event *event);
470
471 void intel_pmu_lbr_disable(struct perf_event *event);
472
473 void intel_pmu_lbr_enable_all(void);
474
475 void intel_pmu_lbr_disable_all(void);
476
477 void intel_pmu_lbr_read(void);
478
479 void intel_pmu_lbr_init_core(void);
480
481 void intel_pmu_lbr_init_nhm(void);
482
483 void intel_pmu_lbr_init_atom(void);
484
485 int p4_pmu_init(void);
486
487 int p6_pmu_init(void);
488
489 #else /* CONFIG_CPU_SUP_INTEL */
490
491 static inline void reserve_ds_buffers(void)
492 {
493 }
494
495 static inline void release_ds_buffers(void)
496 {
497 }
498
499 static inline int intel_pmu_init(void)
500 {
501         return 0;
502 }
503
504 static inline struct intel_shared_regs *allocate_shared_regs(int cpu)
505 {
506         return NULL;
507 }
508
509 #endif /* CONFIG_CPU_SUP_INTEL */