1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/slab.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_FULL_DATA(dev, func, type, idx) \
19 ((dev << 24) | (func << 16) | (type << 8) | idx)
20 #define UNCORE_PCI_DEV_DATA(type, idx) ((type << 8) | idx)
21 #define UNCORE_PCI_DEV_DEV(data) ((data >> 24) & 0xff)
22 #define UNCORE_PCI_DEV_FUNC(data) ((data >> 16) & 0xff)
23 #define UNCORE_PCI_DEV_TYPE(data) ((data >> 8) & 0xff)
24 #define UNCORE_PCI_DEV_IDX(data) (data & 0xff)
25 #define UNCORE_EXTRA_PCI_DEV 0xff
26 #define UNCORE_EXTRA_PCI_DEV_MAX 3
28 #define UNCORE_EVENT_CONSTRAINT(c, n) EVENT_CONSTRAINT(c, n, 0xff)
30 struct pci_extra_dev
{
31 struct pci_dev
*dev
[UNCORE_EXTRA_PCI_DEV_MAX
];
34 struct intel_uncore_ops
;
35 struct intel_uncore_pmu
;
36 struct intel_uncore_box
;
37 struct uncore_event_desc
;
39 struct intel_uncore_type
{
48 unsigned event_mask_ext
;
53 unsigned num_shared_regs
:8;
54 unsigned single_fixed
:1;
55 unsigned pair_ctr_ctl
:1;
56 unsigned *msr_offsets
;
57 struct event_constraint unconstrainted
;
58 struct event_constraint
*constraints
;
59 struct intel_uncore_pmu
*pmus
;
60 struct intel_uncore_ops
*ops
;
61 struct uncore_event_desc
*event_descs
;
62 const struct attribute_group
*attr_groups
[4];
63 struct pmu
*pmu
; /* for custom pmu ops */
66 #define pmu_group attr_groups[0]
67 #define format_group attr_groups[1]
68 #define events_group attr_groups[2]
70 struct intel_uncore_ops
{
71 void (*init_box
)(struct intel_uncore_box
*);
72 void (*exit_box
)(struct intel_uncore_box
*);
73 void (*disable_box
)(struct intel_uncore_box
*);
74 void (*enable_box
)(struct intel_uncore_box
*);
75 void (*disable_event
)(struct intel_uncore_box
*, struct perf_event
*);
76 void (*enable_event
)(struct intel_uncore_box
*, struct perf_event
*);
77 u64 (*read_counter
)(struct intel_uncore_box
*, struct perf_event
*);
78 int (*hw_config
)(struct intel_uncore_box
*, struct perf_event
*);
79 struct event_constraint
*(*get_constraint
)(struct intel_uncore_box
*,
81 void (*put_constraint
)(struct intel_uncore_box
*, struct perf_event
*);
84 struct intel_uncore_pmu
{
86 char name
[UNCORE_PMU_NAME_LEN
];
91 struct intel_uncore_type
*type
;
92 struct intel_uncore_box
**boxes
;
95 struct intel_uncore_extra_reg
{
97 u64 config
, config1
, config2
;
101 struct intel_uncore_box
{
104 int n_active
; /* number of active events */
106 int cpu
; /* cpu to collect events */
109 struct perf_event
*events
[UNCORE_PMC_IDX_MAX
];
110 struct perf_event
*event_list
[UNCORE_PMC_IDX_MAX
];
111 struct event_constraint
*event_constraint
[UNCORE_PMC_IDX_MAX
];
112 unsigned long active_mask
[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX
)];
113 u64 tags
[UNCORE_PMC_IDX_MAX
];
114 struct pci_dev
*pci_dev
;
115 struct intel_uncore_pmu
*pmu
;
116 u64 hrtimer_duration
; /* hrtimer timeout for this box */
117 struct hrtimer hrtimer
;
118 struct list_head list
;
119 struct list_head active_list
;
121 struct intel_uncore_extra_reg shared_regs
[0];
124 #define UNCORE_BOX_FLAG_INITIATED 0
125 #define UNCORE_BOX_FLAG_CTL_OFFS8 1 /* event config registers are 8-byte apart */
127 struct uncore_event_desc
{
128 struct kobj_attribute attr
;
133 struct list_head list
;
135 int pbus_to_physid
[256];
138 struct pci2phy_map
*__find_pci2phy_map(int segment
);
140 ssize_t
uncore_event_show(struct kobject
*kobj
,
141 struct kobj_attribute
*attr
, char *buf
);
143 #define INTEL_UNCORE_EVENT_DESC(_name, _config) \
145 .attr = __ATTR(_name, 0444, uncore_event_show, NULL), \
149 #define DEFINE_UNCORE_FORMAT_ATTR(_var, _name, _format) \
150 static ssize_t __uncore_##_var##_show(struct kobject *kobj, \
151 struct kobj_attribute *attr, \
154 BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE); \
155 return sprintf(page, _format "\n"); \
157 static struct kobj_attribute format_attr_##_var = \
158 __ATTR(_name, 0444, __uncore_##_var##_show, NULL)
160 static inline unsigned uncore_pci_box_ctl(struct intel_uncore_box
*box
)
162 return box
->pmu
->type
->box_ctl
;
165 static inline unsigned uncore_pci_fixed_ctl(struct intel_uncore_box
*box
)
167 return box
->pmu
->type
->fixed_ctl
;
170 static inline unsigned uncore_pci_fixed_ctr(struct intel_uncore_box
*box
)
172 return box
->pmu
->type
->fixed_ctr
;
176 unsigned uncore_pci_event_ctl(struct intel_uncore_box
*box
, int idx
)
178 if (test_bit(UNCORE_BOX_FLAG_CTL_OFFS8
, &box
->flags
))
179 return idx
* 8 + box
->pmu
->type
->event_ctl
;
181 return idx
* 4 + box
->pmu
->type
->event_ctl
;
185 unsigned uncore_pci_perf_ctr(struct intel_uncore_box
*box
, int idx
)
187 return idx
* 8 + box
->pmu
->type
->perf_ctr
;
190 static inline unsigned uncore_msr_box_offset(struct intel_uncore_box
*box
)
192 struct intel_uncore_pmu
*pmu
= box
->pmu
;
193 return pmu
->type
->msr_offsets
?
194 pmu
->type
->msr_offsets
[pmu
->pmu_idx
] :
195 pmu
->type
->msr_offset
* pmu
->pmu_idx
;
198 static inline unsigned uncore_msr_box_ctl(struct intel_uncore_box
*box
)
200 if (!box
->pmu
->type
->box_ctl
)
202 return box
->pmu
->type
->box_ctl
+ uncore_msr_box_offset(box
);
205 static inline unsigned uncore_msr_fixed_ctl(struct intel_uncore_box
*box
)
207 if (!box
->pmu
->type
->fixed_ctl
)
209 return box
->pmu
->type
->fixed_ctl
+ uncore_msr_box_offset(box
);
212 static inline unsigned uncore_msr_fixed_ctr(struct intel_uncore_box
*box
)
214 return box
->pmu
->type
->fixed_ctr
+ uncore_msr_box_offset(box
);
218 unsigned uncore_msr_event_ctl(struct intel_uncore_box
*box
, int idx
)
220 return box
->pmu
->type
->event_ctl
+
221 (box
->pmu
->type
->pair_ctr_ctl
? 2 * idx
: idx
) +
222 uncore_msr_box_offset(box
);
226 unsigned uncore_msr_perf_ctr(struct intel_uncore_box
*box
, int idx
)
228 return box
->pmu
->type
->perf_ctr
+
229 (box
->pmu
->type
->pair_ctr_ctl
? 2 * idx
: idx
) +
230 uncore_msr_box_offset(box
);
234 unsigned uncore_fixed_ctl(struct intel_uncore_box
*box
)
237 return uncore_pci_fixed_ctl(box
);
239 return uncore_msr_fixed_ctl(box
);
243 unsigned uncore_fixed_ctr(struct intel_uncore_box
*box
)
246 return uncore_pci_fixed_ctr(box
);
248 return uncore_msr_fixed_ctr(box
);
252 unsigned uncore_event_ctl(struct intel_uncore_box
*box
, int idx
)
255 return uncore_pci_event_ctl(box
, idx
);
257 return uncore_msr_event_ctl(box
, idx
);
261 unsigned uncore_perf_ctr(struct intel_uncore_box
*box
, int idx
)
264 return uncore_pci_perf_ctr(box
, idx
);
266 return uncore_msr_perf_ctr(box
, idx
);
269 static inline int uncore_perf_ctr_bits(struct intel_uncore_box
*box
)
271 return box
->pmu
->type
->perf_ctr_bits
;
274 static inline int uncore_fixed_ctr_bits(struct intel_uncore_box
*box
)
276 return box
->pmu
->type
->fixed_ctr_bits
;
279 static inline int uncore_num_counters(struct intel_uncore_box
*box
)
281 return box
->pmu
->type
->num_counters
;
284 static inline void uncore_disable_box(struct intel_uncore_box
*box
)
286 if (box
->pmu
->type
->ops
->disable_box
)
287 box
->pmu
->type
->ops
->disable_box(box
);
290 static inline void uncore_enable_box(struct intel_uncore_box
*box
)
292 if (box
->pmu
->type
->ops
->enable_box
)
293 box
->pmu
->type
->ops
->enable_box(box
);
296 static inline void uncore_disable_event(struct intel_uncore_box
*box
,
297 struct perf_event
*event
)
299 box
->pmu
->type
->ops
->disable_event(box
, event
);
302 static inline void uncore_enable_event(struct intel_uncore_box
*box
,
303 struct perf_event
*event
)
305 box
->pmu
->type
->ops
->enable_event(box
, event
);
308 static inline u64
uncore_read_counter(struct intel_uncore_box
*box
,
309 struct perf_event
*event
)
311 return box
->pmu
->type
->ops
->read_counter(box
, event
);
314 static inline void uncore_box_init(struct intel_uncore_box
*box
)
316 if (!test_and_set_bit(UNCORE_BOX_FLAG_INITIATED
, &box
->flags
)) {
317 if (box
->pmu
->type
->ops
->init_box
)
318 box
->pmu
->type
->ops
->init_box(box
);
322 static inline void uncore_box_exit(struct intel_uncore_box
*box
)
324 if (test_and_clear_bit(UNCORE_BOX_FLAG_INITIATED
, &box
->flags
)) {
325 if (box
->pmu
->type
->ops
->exit_box
)
326 box
->pmu
->type
->ops
->exit_box(box
);
330 static inline bool uncore_box_is_fake(struct intel_uncore_box
*box
)
332 return (box
->pkgid
< 0);
335 static inline struct intel_uncore_pmu
*uncore_event_to_pmu(struct perf_event
*event
)
337 return container_of(event
->pmu
, struct intel_uncore_pmu
, pmu
);
340 static inline struct intel_uncore_box
*uncore_event_to_box(struct perf_event
*event
)
342 return event
->pmu_private
;
345 struct intel_uncore_box
*uncore_pmu_to_box(struct intel_uncore_pmu
*pmu
, int cpu
);
346 u64
uncore_msr_read_counter(struct intel_uncore_box
*box
, struct perf_event
*event
);
347 void uncore_pmu_start_hrtimer(struct intel_uncore_box
*box
);
348 void uncore_pmu_cancel_hrtimer(struct intel_uncore_box
*box
);
349 void uncore_pmu_event_read(struct perf_event
*event
);
350 void uncore_perf_event_update(struct intel_uncore_box
*box
, struct perf_event
*event
);
351 struct event_constraint
*
352 uncore_get_constraint(struct intel_uncore_box
*box
, struct perf_event
*event
);
353 void uncore_put_constraint(struct intel_uncore_box
*box
, struct perf_event
*event
);
354 u64
uncore_shared_reg_config(struct intel_uncore_box
*box
, int idx
);
356 extern struct intel_uncore_type
**uncore_msr_uncores
;
357 extern struct intel_uncore_type
**uncore_pci_uncores
;
358 extern struct pci_driver
*uncore_pci_driver
;
359 extern raw_spinlock_t pci2phy_map_lock
;
360 extern struct list_head pci2phy_map_head
;
361 extern struct pci_extra_dev
*uncore_extra_pci_dev
;
362 extern struct event_constraint uncore_constraint_empty
;
365 int snb_uncore_pci_init(void);
366 int ivb_uncore_pci_init(void);
367 int hsw_uncore_pci_init(void);
368 int bdw_uncore_pci_init(void);
369 int skl_uncore_pci_init(void);
370 void snb_uncore_cpu_init(void);
371 void nhm_uncore_cpu_init(void);
372 void skl_uncore_cpu_init(void);
373 int snb_pci2phy_map_init(int devid
);
376 int snbep_uncore_pci_init(void);
377 void snbep_uncore_cpu_init(void);
378 int ivbep_uncore_pci_init(void);
379 void ivbep_uncore_cpu_init(void);
380 int hswep_uncore_pci_init(void);
381 void hswep_uncore_cpu_init(void);
382 int bdx_uncore_pci_init(void);
383 void bdx_uncore_cpu_init(void);
384 int knl_uncore_pci_init(void);
385 void knl_uncore_cpu_init(void);
386 int skx_uncore_pci_init(void);
387 void skx_uncore_cpu_init(void);
390 void nhmex_uncore_cpu_init(void);