Linux 4.6-rc6
[linux/fpc-iii.git] / arch / x86 / events / intel / uncore.h
blob79766b9a35809fd072849f09b2b16ec0ea3ee901
1 #include <linux/module.h>
2 #include <linux/slab.h>
3 #include <linux/pci.h>
4 #include <asm/apicdef.h>
6 #include <linux/perf_event.h>
7 #include "../perf_event.h"
9 #define UNCORE_PMU_NAME_LEN 32
10 #define UNCORE_PMU_HRTIMER_INTERVAL (60LL * NSEC_PER_SEC)
11 #define UNCORE_SNB_IMC_HRTIMER_INTERVAL (5ULL * NSEC_PER_SEC)
13 #define UNCORE_FIXED_EVENT 0xff
14 #define UNCORE_PMC_IDX_MAX_GENERIC 8
15 #define UNCORE_PMC_IDX_FIXED UNCORE_PMC_IDX_MAX_GENERIC
16 #define UNCORE_PMC_IDX_MAX (UNCORE_PMC_IDX_FIXED + 1)
18 #define UNCORE_PCI_DEV_DATA(type, idx) ((type << 8) | idx)
19 #define UNCORE_PCI_DEV_TYPE(data) ((data >> 8) & 0xff)
20 #define UNCORE_PCI_DEV_IDX(data) (data & 0xff)
21 #define UNCORE_EXTRA_PCI_DEV 0xff
22 #define UNCORE_EXTRA_PCI_DEV_MAX 3
24 #define UNCORE_EVENT_CONSTRAINT(c, n) EVENT_CONSTRAINT(c, n, 0xff)
26 struct pci_extra_dev {
27 struct pci_dev *dev[UNCORE_EXTRA_PCI_DEV_MAX];
30 struct intel_uncore_ops;
31 struct intel_uncore_pmu;
32 struct intel_uncore_box;
33 struct uncore_event_desc;
35 struct intel_uncore_type {
36 const char *name;
37 int num_counters;
38 int num_boxes;
39 int perf_ctr_bits;
40 int fixed_ctr_bits;
41 unsigned perf_ctr;
42 unsigned event_ctl;
43 unsigned event_mask;
44 unsigned fixed_ctr;
45 unsigned fixed_ctl;
46 unsigned box_ctl;
47 unsigned msr_offset;
48 unsigned num_shared_regs:8;
49 unsigned single_fixed:1;
50 unsigned pair_ctr_ctl:1;
51 unsigned *msr_offsets;
52 struct event_constraint unconstrainted;
53 struct event_constraint *constraints;
54 struct intel_uncore_pmu *pmus;
55 struct intel_uncore_ops *ops;
56 struct uncore_event_desc *event_descs;
57 const struct attribute_group *attr_groups[4];
58 struct pmu *pmu; /* for custom pmu ops */
61 #define pmu_group attr_groups[0]
62 #define format_group attr_groups[1]
63 #define events_group attr_groups[2]
65 struct intel_uncore_ops {
66 void (*init_box)(struct intel_uncore_box *);
67 void (*exit_box)(struct intel_uncore_box *);
68 void (*disable_box)(struct intel_uncore_box *);
69 void (*enable_box)(struct intel_uncore_box *);
70 void (*disable_event)(struct intel_uncore_box *, struct perf_event *);
71 void (*enable_event)(struct intel_uncore_box *, struct perf_event *);
72 u64 (*read_counter)(struct intel_uncore_box *, struct perf_event *);
73 int (*hw_config)(struct intel_uncore_box *, struct perf_event *);
74 struct event_constraint *(*get_constraint)(struct intel_uncore_box *,
75 struct perf_event *);
76 void (*put_constraint)(struct intel_uncore_box *, struct perf_event *);
79 struct intel_uncore_pmu {
80 struct pmu pmu;
81 char name[UNCORE_PMU_NAME_LEN];
82 int pmu_idx;
83 int func_id;
84 bool registered;
85 atomic_t activeboxes;
86 struct intel_uncore_type *type;
87 struct intel_uncore_box **boxes;
90 struct intel_uncore_extra_reg {
91 raw_spinlock_t lock;
92 u64 config, config1, config2;
93 atomic_t ref;
96 struct intel_uncore_box {
97 int pci_phys_id;
98 int pkgid;
99 int n_active; /* number of active events */
100 int n_events;
101 int cpu; /* cpu to collect events */
102 unsigned long flags;
103 atomic_t refcnt;
104 struct perf_event *events[UNCORE_PMC_IDX_MAX];
105 struct perf_event *event_list[UNCORE_PMC_IDX_MAX];
106 struct event_constraint *event_constraint[UNCORE_PMC_IDX_MAX];
107 unsigned long active_mask[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX)];
108 u64 tags[UNCORE_PMC_IDX_MAX];
109 struct pci_dev *pci_dev;
110 struct intel_uncore_pmu *pmu;
111 u64 hrtimer_duration; /* hrtimer timeout for this box */
112 struct hrtimer hrtimer;
113 struct list_head list;
114 struct list_head active_list;
115 void *io_addr;
116 struct intel_uncore_extra_reg shared_regs[0];
119 #define UNCORE_BOX_FLAG_INITIATED 0
121 struct uncore_event_desc {
122 struct kobj_attribute attr;
123 const char *config;
126 struct pci2phy_map {
127 struct list_head list;
128 int segment;
129 int pbus_to_physid[256];
132 struct pci2phy_map *__find_pci2phy_map(int segment);
134 ssize_t uncore_event_show(struct kobject *kobj,
135 struct kobj_attribute *attr, char *buf);
137 #define INTEL_UNCORE_EVENT_DESC(_name, _config) \
139 .attr = __ATTR(_name, 0444, uncore_event_show, NULL), \
140 .config = _config, \
143 #define DEFINE_UNCORE_FORMAT_ATTR(_var, _name, _format) \
144 static ssize_t __uncore_##_var##_show(struct kobject *kobj, \
145 struct kobj_attribute *attr, \
146 char *page) \
148 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
149 return sprintf(page, _format "\n"); \
151 static struct kobj_attribute format_attr_##_var = \
152 __ATTR(_name, 0444, __uncore_##_var##_show, NULL)
154 static inline unsigned uncore_pci_box_ctl(struct intel_uncore_box *box)
156 return box->pmu->type->box_ctl;
159 static inline unsigned uncore_pci_fixed_ctl(struct intel_uncore_box *box)
161 return box->pmu->type->fixed_ctl;
164 static inline unsigned uncore_pci_fixed_ctr(struct intel_uncore_box *box)
166 return box->pmu->type->fixed_ctr;
169 static inline
170 unsigned uncore_pci_event_ctl(struct intel_uncore_box *box, int idx)
172 return idx * 4 + box->pmu->type->event_ctl;
175 static inline
176 unsigned uncore_pci_perf_ctr(struct intel_uncore_box *box, int idx)
178 return idx * 8 + box->pmu->type->perf_ctr;
181 static inline unsigned uncore_msr_box_offset(struct intel_uncore_box *box)
183 struct intel_uncore_pmu *pmu = box->pmu;
184 return pmu->type->msr_offsets ?
185 pmu->type->msr_offsets[pmu->pmu_idx] :
186 pmu->type->msr_offset * pmu->pmu_idx;
189 static inline unsigned uncore_msr_box_ctl(struct intel_uncore_box *box)
191 if (!box->pmu->type->box_ctl)
192 return 0;
193 return box->pmu->type->box_ctl + uncore_msr_box_offset(box);
196 static inline unsigned uncore_msr_fixed_ctl(struct intel_uncore_box *box)
198 if (!box->pmu->type->fixed_ctl)
199 return 0;
200 return box->pmu->type->fixed_ctl + uncore_msr_box_offset(box);
203 static inline unsigned uncore_msr_fixed_ctr(struct intel_uncore_box *box)
205 return box->pmu->type->fixed_ctr + uncore_msr_box_offset(box);
208 static inline
209 unsigned uncore_msr_event_ctl(struct intel_uncore_box *box, int idx)
211 return box->pmu->type->event_ctl +
212 (box->pmu->type->pair_ctr_ctl ? 2 * idx : idx) +
213 uncore_msr_box_offset(box);
216 static inline
217 unsigned uncore_msr_perf_ctr(struct intel_uncore_box *box, int idx)
219 return box->pmu->type->perf_ctr +
220 (box->pmu->type->pair_ctr_ctl ? 2 * idx : idx) +
221 uncore_msr_box_offset(box);
224 static inline
225 unsigned uncore_fixed_ctl(struct intel_uncore_box *box)
227 if (box->pci_dev)
228 return uncore_pci_fixed_ctl(box);
229 else
230 return uncore_msr_fixed_ctl(box);
233 static inline
234 unsigned uncore_fixed_ctr(struct intel_uncore_box *box)
236 if (box->pci_dev)
237 return uncore_pci_fixed_ctr(box);
238 else
239 return uncore_msr_fixed_ctr(box);
242 static inline
243 unsigned uncore_event_ctl(struct intel_uncore_box *box, int idx)
245 if (box->pci_dev)
246 return uncore_pci_event_ctl(box, idx);
247 else
248 return uncore_msr_event_ctl(box, idx);
251 static inline
252 unsigned uncore_perf_ctr(struct intel_uncore_box *box, int idx)
254 if (box->pci_dev)
255 return uncore_pci_perf_ctr(box, idx);
256 else
257 return uncore_msr_perf_ctr(box, idx);
260 static inline int uncore_perf_ctr_bits(struct intel_uncore_box *box)
262 return box->pmu->type->perf_ctr_bits;
265 static inline int uncore_fixed_ctr_bits(struct intel_uncore_box *box)
267 return box->pmu->type->fixed_ctr_bits;
270 static inline int uncore_num_counters(struct intel_uncore_box *box)
272 return box->pmu->type->num_counters;
275 static inline void uncore_disable_box(struct intel_uncore_box *box)
277 if (box->pmu->type->ops->disable_box)
278 box->pmu->type->ops->disable_box(box);
281 static inline void uncore_enable_box(struct intel_uncore_box *box)
283 if (box->pmu->type->ops->enable_box)
284 box->pmu->type->ops->enable_box(box);
287 static inline void uncore_disable_event(struct intel_uncore_box *box,
288 struct perf_event *event)
290 box->pmu->type->ops->disable_event(box, event);
293 static inline void uncore_enable_event(struct intel_uncore_box *box,
294 struct perf_event *event)
296 box->pmu->type->ops->enable_event(box, event);
299 static inline u64 uncore_read_counter(struct intel_uncore_box *box,
300 struct perf_event *event)
302 return box->pmu->type->ops->read_counter(box, event);
305 static inline void uncore_box_init(struct intel_uncore_box *box)
307 if (!test_and_set_bit(UNCORE_BOX_FLAG_INITIATED, &box->flags)) {
308 if (box->pmu->type->ops->init_box)
309 box->pmu->type->ops->init_box(box);
313 static inline void uncore_box_exit(struct intel_uncore_box *box)
315 if (test_and_clear_bit(UNCORE_BOX_FLAG_INITIATED, &box->flags)) {
316 if (box->pmu->type->ops->exit_box)
317 box->pmu->type->ops->exit_box(box);
321 static inline bool uncore_box_is_fake(struct intel_uncore_box *box)
323 return (box->pkgid < 0);
326 static inline struct intel_uncore_pmu *uncore_event_to_pmu(struct perf_event *event)
328 return container_of(event->pmu, struct intel_uncore_pmu, pmu);
331 static inline struct intel_uncore_box *uncore_event_to_box(struct perf_event *event)
333 return event->pmu_private;
336 struct intel_uncore_box *uncore_pmu_to_box(struct intel_uncore_pmu *pmu, int cpu);
337 u64 uncore_msr_read_counter(struct intel_uncore_box *box, struct perf_event *event);
338 void uncore_pmu_start_hrtimer(struct intel_uncore_box *box);
339 void uncore_pmu_cancel_hrtimer(struct intel_uncore_box *box);
340 void uncore_pmu_event_read(struct perf_event *event);
341 void uncore_perf_event_update(struct intel_uncore_box *box, struct perf_event *event);
342 struct event_constraint *
343 uncore_get_constraint(struct intel_uncore_box *box, struct perf_event *event);
344 void uncore_put_constraint(struct intel_uncore_box *box, struct perf_event *event);
345 u64 uncore_shared_reg_config(struct intel_uncore_box *box, int idx);
347 extern struct intel_uncore_type **uncore_msr_uncores;
348 extern struct intel_uncore_type **uncore_pci_uncores;
349 extern struct pci_driver *uncore_pci_driver;
350 extern raw_spinlock_t pci2phy_map_lock;
351 extern struct list_head pci2phy_map_head;
352 extern struct pci_extra_dev *uncore_extra_pci_dev;
353 extern struct event_constraint uncore_constraint_empty;
355 /* perf_event_intel_uncore_snb.c */
356 int snb_uncore_pci_init(void);
357 int ivb_uncore_pci_init(void);
358 int hsw_uncore_pci_init(void);
359 int bdw_uncore_pci_init(void);
360 int skl_uncore_pci_init(void);
361 void snb_uncore_cpu_init(void);
362 void nhm_uncore_cpu_init(void);
363 int snb_pci2phy_map_init(int devid);
365 /* perf_event_intel_uncore_snbep.c */
366 int snbep_uncore_pci_init(void);
367 void snbep_uncore_cpu_init(void);
368 int ivbep_uncore_pci_init(void);
369 void ivbep_uncore_cpu_init(void);
370 int hswep_uncore_pci_init(void);
371 void hswep_uncore_cpu_init(void);
372 int bdx_uncore_pci_init(void);
373 void bdx_uncore_cpu_init(void);
374 int knl_uncore_pci_init(void);
375 void knl_uncore_cpu_init(void);
377 /* perf_event_intel_uncore_nhmex.c */
378 void nhmex_uncore_cpu_init(void);