uncore.h 10.8 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
2
#include <linux/slab.h>
3
#include <linux/pci.h>
4 5
#include <asm/apicdef.h>

6
#include <linux/perf_event.h>
7
#include "../perf_event.h"
8 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)
12

13
#define UNCORE_FIXED_EVENT		0xff
14 15 16 17
#define UNCORE_PMC_IDX_MAX_GENERIC	8
#define UNCORE_PMC_IDX_FIXED		UNCORE_PMC_IDX_MAX_GENERIC
#define UNCORE_PMC_IDX_MAX		(UNCORE_PMC_IDX_FIXED + 1)

18 19
#define UNCORE_PCI_DEV_FULL_DATA(dev, func, type, idx)	\
		((dev << 24) | (func << 16) | (type << 8) | idx)
20
#define UNCORE_PCI_DEV_DATA(type, idx)	((type << 8) | idx)
21 22
#define UNCORE_PCI_DEV_DEV(data)	((data >> 24) & 0xff)
#define UNCORE_PCI_DEV_FUNC(data)	((data >> 16) & 0xff)
23 24 25
#define UNCORE_PCI_DEV_TYPE(data)	((data >> 8) & 0xff)
#define UNCORE_PCI_DEV_IDX(data)	(data & 0xff)
#define UNCORE_EXTRA_PCI_DEV		0xff
26
#define UNCORE_EXTRA_PCI_DEV_MAX	3
27

28 29
#define UNCORE_EVENT_CONSTRAINT(c, n) EVENT_CONSTRAINT(c, n, 0xff)

30 31 32 33
struct pci_extra_dev {
	struct pci_dev *dev[UNCORE_EXTRA_PCI_DEV_MAX];
};

34 35 36 37 38 39 40 41 42 43 44 45 46 47
struct intel_uncore_ops;
struct intel_uncore_pmu;
struct intel_uncore_box;
struct uncore_event_desc;

struct intel_uncore_type {
	const char *name;
	int num_counters;
	int num_boxes;
	int perf_ctr_bits;
	int fixed_ctr_bits;
	unsigned perf_ctr;
	unsigned event_ctl;
	unsigned event_mask;
48
	unsigned event_mask_ext;
49 50 51 52
	unsigned fixed_ctr;
	unsigned fixed_ctl;
	unsigned box_ctl;
	unsigned msr_offset;
53 54
	unsigned num_shared_regs:8;
	unsigned single_fixed:1;
55
	unsigned pair_ctr_ctl:1;
56
	unsigned *msr_offsets;
57 58 59 60 61
	struct event_constraint unconstrainted;
	struct event_constraint *constraints;
	struct intel_uncore_pmu *pmus;
	struct intel_uncore_ops *ops;
	struct uncore_event_desc *event_descs;
62
	const struct attribute_group *attr_groups[4];
63
	struct pmu *pmu; /* for custom pmu ops */
64 65
};

66 67 68
#define pmu_group attr_groups[0]
#define format_group attr_groups[1]
#define events_group attr_groups[2]
69 70 71

struct intel_uncore_ops {
	void (*init_box)(struct intel_uncore_box *);
72
	void (*exit_box)(struct intel_uncore_box *);
73 74 75 76 77
	void (*disable_box)(struct intel_uncore_box *);
	void (*enable_box)(struct intel_uncore_box *);
	void (*disable_event)(struct intel_uncore_box *, struct perf_event *);
	void (*enable_event)(struct intel_uncore_box *, struct perf_event *);
	u64 (*read_counter)(struct intel_uncore_box *, struct perf_event *);
78 79 80 81
	int (*hw_config)(struct intel_uncore_box *, struct perf_event *);
	struct event_constraint *(*get_constraint)(struct intel_uncore_box *,
						   struct perf_event *);
	void (*put_constraint)(struct intel_uncore_box *, struct perf_event *);
82 83 84
};

struct intel_uncore_pmu {
85 86 87 88 89
	struct pmu			pmu;
	char				name[UNCORE_PMU_NAME_LEN];
	int				pmu_idx;
	int				func_id;
	bool				registered;
90
	atomic_t			activeboxes;
91
	struct intel_uncore_type	*type;
92
	struct intel_uncore_box		**boxes;
93 94
};

95 96
struct intel_uncore_extra_reg {
	raw_spinlock_t lock;
97
	u64 config, config1, config2;
98 99 100
	atomic_t ref;
};

101
struct intel_uncore_box {
102
	int pci_phys_id;
103
	int pkgid;	/* Logical package ID */
104 105 106 107 108 109 110
	int n_active;	/* number of active events */
	int n_events;
	int cpu;	/* cpu to collect events */
	unsigned long flags;
	atomic_t refcnt;
	struct perf_event *events[UNCORE_PMC_IDX_MAX];
	struct perf_event *event_list[UNCORE_PMC_IDX_MAX];
111
	struct event_constraint *event_constraint[UNCORE_PMC_IDX_MAX];
112 113
	unsigned long active_mask[BITS_TO_LONGS(UNCORE_PMC_IDX_MAX)];
	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 118
	struct hrtimer hrtimer;
	struct list_head list;
119
	struct list_head active_list;
120
	void *io_addr;
121
	struct intel_uncore_extra_reg shared_regs[0];
122 123 124
};

#define UNCORE_BOX_FLAG_INITIATED	0
125
#define UNCORE_BOX_FLAG_CTL_OFFS8	1 /* event config registers are 8-byte apart */
126 127 128 129 130 131

struct uncore_event_desc {
	struct kobj_attribute attr;
	const char *config;
};

132 133 134 135 136 137 138 139
struct pci2phy_map {
	struct list_head list;
	int segment;
	int pbus_to_physid[256];
};

struct pci2phy_map *__find_pci2phy_map(int segment);

140 141 142
ssize_t uncore_event_show(struct kobject *kobj,
			  struct kobj_attribute *attr, char *buf);

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#define INTEL_UNCORE_EVENT_DESC(_name, _config)			\
{								\
	.attr	= __ATTR(_name, 0444, uncore_event_show, NULL),	\
	.config	= _config,					\
}

#define DEFINE_UNCORE_FORMAT_ATTR(_var, _name, _format)			\
static ssize_t __uncore_##_var##_show(struct kobject *kobj,		\
				struct kobj_attribute *attr,		\
				char *page)				\
{									\
	BUILD_BUG_ON(sizeof(_format) >= PAGE_SIZE);			\
	return sprintf(page, _format "\n");				\
}									\
static struct kobj_attribute format_attr_##_var =			\
	__ATTR(_name, 0444, __uncore_##_var##_show, NULL)

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static inline unsigned uncore_pci_box_ctl(struct intel_uncore_box *box)
{
	return box->pmu->type->box_ctl;
}

static inline unsigned uncore_pci_fixed_ctl(struct intel_uncore_box *box)
{
	return box->pmu->type->fixed_ctl;
}

static inline unsigned uncore_pci_fixed_ctr(struct intel_uncore_box *box)
{
	return box->pmu->type->fixed_ctr;
}

static inline
unsigned uncore_pci_event_ctl(struct intel_uncore_box *box, int idx)
{
178 179 180
	if (test_bit(UNCORE_BOX_FLAG_CTL_OFFS8, &box->flags))
		return idx * 8 + box->pmu->type->event_ctl;

181 182 183 184 185 186 187 188 189
	return idx * 4 + box->pmu->type->event_ctl;
}

static inline
unsigned uncore_pci_perf_ctr(struct intel_uncore_box *box, int idx)
{
	return idx * 8 + box->pmu->type->perf_ctr;
}

190 191 192 193 194 195 196 197 198
static inline unsigned uncore_msr_box_offset(struct intel_uncore_box *box)
{
	struct intel_uncore_pmu *pmu = box->pmu;
	return pmu->type->msr_offsets ?
		pmu->type->msr_offsets[pmu->pmu_idx] :
		pmu->type->msr_offset * pmu->pmu_idx;
}

static inline unsigned uncore_msr_box_ctl(struct intel_uncore_box *box)
199 200 201
{
	if (!box->pmu->type->box_ctl)
		return 0;
202
	return box->pmu->type->box_ctl + uncore_msr_box_offset(box);
203 204
}

205
static inline unsigned uncore_msr_fixed_ctl(struct intel_uncore_box *box)
206 207 208
{
	if (!box->pmu->type->fixed_ctl)
		return 0;
209
	return box->pmu->type->fixed_ctl + uncore_msr_box_offset(box);
210 211
}

212
static inline unsigned uncore_msr_fixed_ctr(struct intel_uncore_box *box)
213
{
214
	return box->pmu->type->fixed_ctr + uncore_msr_box_offset(box);
215 216 217 218 219
}

static inline
unsigned uncore_msr_event_ctl(struct intel_uncore_box *box, int idx)
{
220 221
	return box->pmu->type->event_ctl +
		(box->pmu->type->pair_ctr_ctl ? 2 * idx : idx) +
222
		uncore_msr_box_offset(box);
223 224 225 226 227
}

static inline
unsigned uncore_msr_perf_ctr(struct intel_uncore_box *box, int idx)
{
228 229
	return box->pmu->type->perf_ctr +
		(box->pmu->type->pair_ctr_ctl ? 2 * idx : idx) +
230
		uncore_msr_box_offset(box);
231 232
}

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static inline
unsigned uncore_fixed_ctl(struct intel_uncore_box *box)
{
	if (box->pci_dev)
		return uncore_pci_fixed_ctl(box);
	else
		return uncore_msr_fixed_ctl(box);
}

static inline
unsigned uncore_fixed_ctr(struct intel_uncore_box *box)
{
	if (box->pci_dev)
		return uncore_pci_fixed_ctr(box);
	else
		return uncore_msr_fixed_ctr(box);
}

static inline
unsigned uncore_event_ctl(struct intel_uncore_box *box, int idx)
{
	if (box->pci_dev)
		return uncore_pci_event_ctl(box, idx);
	else
		return uncore_msr_event_ctl(box, idx);
}

static inline
unsigned uncore_perf_ctr(struct intel_uncore_box *box, int idx)
{
	if (box->pci_dev)
		return uncore_pci_perf_ctr(box, idx);
	else
		return uncore_msr_perf_ctr(box, idx);
}

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
static inline int uncore_perf_ctr_bits(struct intel_uncore_box *box)
{
	return box->pmu->type->perf_ctr_bits;
}

static inline int uncore_fixed_ctr_bits(struct intel_uncore_box *box)
{
	return box->pmu->type->fixed_ctr_bits;
}

static inline int uncore_num_counters(struct intel_uncore_box *box)
{
	return box->pmu->type->num_counters;
}

static inline void uncore_disable_box(struct intel_uncore_box *box)
{
	if (box->pmu->type->ops->disable_box)
		box->pmu->type->ops->disable_box(box);
}

static inline void uncore_enable_box(struct intel_uncore_box *box)
{
	if (box->pmu->type->ops->enable_box)
		box->pmu->type->ops->enable_box(box);
}

static inline void uncore_disable_event(struct intel_uncore_box *box,
				struct perf_event *event)
{
	box->pmu->type->ops->disable_event(box, event);
}

static inline void uncore_enable_event(struct intel_uncore_box *box,
				struct perf_event *event)
{
	box->pmu->type->ops->enable_event(box, event);
}

static inline u64 uncore_read_counter(struct intel_uncore_box *box,
				struct perf_event *event)
{
	return box->pmu->type->ops->read_counter(box, event);
}

314 315 316 317 318 319 320 321
static inline void uncore_box_init(struct intel_uncore_box *box)
{
	if (!test_and_set_bit(UNCORE_BOX_FLAG_INITIATED, &box->flags)) {
		if (box->pmu->type->ops->init_box)
			box->pmu->type->ops->init_box(box);
	}
}

322 323 324 325 326 327 328 329
static inline void uncore_box_exit(struct intel_uncore_box *box)
{
	if (test_and_clear_bit(UNCORE_BOX_FLAG_INITIATED, &box->flags)) {
		if (box->pmu->type->ops->exit_box)
			box->pmu->type->ops->exit_box(box);
	}
}

330 331
static inline bool uncore_box_is_fake(struct intel_uncore_box *box)
{
332
	return (box->pkgid < 0);
333
}
334

335 336 337 338 339 340 341 342 343 344
static inline struct intel_uncore_pmu *uncore_event_to_pmu(struct perf_event *event)
{
	return container_of(event->pmu, struct intel_uncore_pmu, pmu);
}

static inline struct intel_uncore_box *uncore_event_to_box(struct perf_event *event)
{
	return event->pmu_private;
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358
struct intel_uncore_box *uncore_pmu_to_box(struct intel_uncore_pmu *pmu, int cpu);
u64 uncore_msr_read_counter(struct intel_uncore_box *box, struct perf_event *event);
void uncore_pmu_start_hrtimer(struct intel_uncore_box *box);
void uncore_pmu_cancel_hrtimer(struct intel_uncore_box *box);
void uncore_pmu_event_read(struct perf_event *event);
void uncore_perf_event_update(struct intel_uncore_box *box, struct perf_event *event);
struct event_constraint *
uncore_get_constraint(struct intel_uncore_box *box, struct perf_event *event);
void uncore_put_constraint(struct intel_uncore_box *box, struct perf_event *event);
u64 uncore_shared_reg_config(struct intel_uncore_box *box, int idx);

extern struct intel_uncore_type **uncore_msr_uncores;
extern struct intel_uncore_type **uncore_pci_uncores;
extern struct pci_driver *uncore_pci_driver;
359 360
extern raw_spinlock_t pci2phy_map_lock;
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;
363

364
/* uncore_snb.c */
365 366 367
int snb_uncore_pci_init(void);
int ivb_uncore_pci_init(void);
int hsw_uncore_pci_init(void);
368
int bdw_uncore_pci_init(void);
369
int skl_uncore_pci_init(void);
370 371
void snb_uncore_cpu_init(void);
void nhm_uncore_cpu_init(void);
372
void skl_uncore_cpu_init(void);
373
int snb_pci2phy_map_init(int devid);
374

375
/* uncore_snbep.c */
376 377
int snbep_uncore_pci_init(void);
void snbep_uncore_cpu_init(void);
378 379
int ivbep_uncore_pci_init(void);
void ivbep_uncore_cpu_init(void);
380 381
int hswep_uncore_pci_init(void);
void hswep_uncore_cpu_init(void);
382 383
int bdx_uncore_pci_init(void);
void bdx_uncore_cpu_init(void);
384 385
int knl_uncore_pci_init(void);
void knl_uncore_cpu_init(void);
386 387
int skx_uncore_pci_init(void);
void skx_uncore_cpu_init(void);
388

389
/* uncore_nhmex.c */
390
void nhmex_uncore_cpu_init(void);