perf_event_p4.c 19.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Netburst Perfomance Events (P4, old Xeon)
 *
 *  Copyright (C) 2010 Parallels, Inc., Cyrill Gorcunov <gorcunov@openvz.org>
 *  Copyright (C) 2010 Intel Corporation, Lin Ming <ming.m.lin@intel.com>
 *
 *  For licencing details see kernel-base/COPYING
 */

#ifdef CONFIG_CPU_SUP_INTEL

#include <asm/perf_event_p4.h>

/*
 * array indices: 0,1 - HT threads, used with HT enabled cpu
 */
struct p4_event_template {
	u32 opcode;			/* ESCR event + CCCR selector */
	u64 config;			/* packed predefined bits */
	int dep;			/* upstream dependency event index */
21
	int key;			/* index into p4_templates */
22 23 24 25 26
	u64 msr;			/*
					 * the high 32 bits set into MSR_IA32_PEBS_ENABLE and
					 * the low 32 bits set into MSR_P4_PEBS_MATRIX_VERT
					 * for cache events
					 */
27 28 29 30 31 32 33 34 35 36 37 38
	unsigned int emask;		/* ESCR EventMask */
	unsigned int escr_msr[2];	/* ESCR MSR for this event */
	unsigned int cntr[2];		/* counter index (offset) */
};

struct p4_pmu_res {
	/* maps hw_conf::idx into template for ESCR sake */
	struct p4_event_template *tpl[ARCH_P4_MAX_CCCR];
};

static DEFINE_PER_CPU(struct p4_pmu_res, p4_pmu_config);

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#define P4_CACHE_EVENT_CONFIG(event, bit) \
	p4_config_pack_escr(P4_EVENT_UNPACK_EVENT(event) << P4_EVNTSEL_EVENT_SHIFT) | \
	p4_config_pack_escr((event##_##bit) << P4_EVNTSEL_EVENTMASK_SHIFT) | \
	p4_config_pack_cccr(P4_EVENT_UNPACK_SELECTOR(event) << P4_CCCR_ESCR_SELECT_SHIFT)

static __initconst u64 p4_hw_cache_event_ids
				[PERF_COUNT_HW_CACHE_MAX]
				[PERF_COUNT_HW_CACHE_OP_MAX]
				[PERF_COUNT_HW_CACHE_RESULT_MAX] =
{
 [ C(L1D ) ] = {
	[ C(OP_READ) ] = {
		[ C(RESULT_ACCESS) ] = 0x0,
					/* 1stL_cache_load_miss_retired */
		[ C(RESULT_MISS)   ] = P4_CACHE_EVENT_CONFIG(P4_REPLAY_EVENT, NBOGUS)
					| KEY_P4_L1D_OP_READ_RESULT_MISS,
	},
 },
 [ C(LL  ) ] = {
	[ C(OP_READ) ] = {
		[ C(RESULT_ACCESS) ] = 0x0,
					/* 2ndL_cache_load_miss_retired */
		[ C(RESULT_MISS)   ] = P4_CACHE_EVENT_CONFIG(P4_REPLAY_EVENT, NBOGUS)
					| KEY_P4_LL_OP_READ_RESULT_MISS,
	},
 },
 [ C(DTLB) ] = {
	[ C(OP_READ) ] = {
		[ C(RESULT_ACCESS) ] = 0x0,
					/* DTLB_load_miss_retired */
		[ C(RESULT_MISS)   ] = P4_CACHE_EVENT_CONFIG(P4_REPLAY_EVENT, NBOGUS)
					| KEY_P4_DTLB_OP_READ_RESULT_MISS,
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = 0x0,
					/* DTLB_store_miss_retired */
		[ C(RESULT_MISS)   ] = P4_CACHE_EVENT_CONFIG(P4_REPLAY_EVENT, NBOGUS)
					| KEY_P4_DTLB_OP_WRITE_RESULT_MISS,
	},
 },
 [ C(ITLB) ] = {
	[ C(OP_READ) ] = {
					/* ITLB_reference.HIT */
		[ C(RESULT_ACCESS) ] = P4_CACHE_EVENT_CONFIG(P4_ITLB_REFERENCE, HIT)
					| KEY_P4_ITLB_OP_READ_RESULT_ACCESS,

					/* ITLB_reference.MISS */
		[ C(RESULT_MISS)   ] = P4_CACHE_EVENT_CONFIG(P4_ITLB_REFERENCE, MISS)
					| KEY_P4_ITLB_OP_READ_RESULT_MISS,
	},
	[ C(OP_WRITE) ] = {
		[ C(RESULT_ACCESS) ] = -1,
		[ C(RESULT_MISS)   ] = -1,
	},
	[ C(OP_PREFETCH) ] = {
		[ C(RESULT_ACCESS) ] = -1,
		[ C(RESULT_MISS)   ] = -1,
	},
 },
};

100 101 102 103 104 105 106 107 108 109 110 111
/*
 * WARN: CCCR1 doesn't have a working enable bit so try to not
 * use it if possible
 *
 * Also as only we start to support raw events we will need to
 * append _all_ P4_EVENT_PACK'ed events here
 */
struct p4_event_template p4_templates[] = {
	[0] = {
		.opcode	= P4_GLOBAL_POWER_EVENTS,
		.config	= 0,
		.dep	= -1,
112
		.key	= 0,
113 114 115 116 117
		.emask	=
			P4_EVENT_ATTR(P4_GLOBAL_POWER_EVENTS, RUNNING),
		.escr_msr	= { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
		.cntr		= { 0, 2 },
	},
118
	[1] = {
119 120
		.opcode	= P4_INSTR_RETIRED,
		.config	= 0,
121
		.dep	= -1, /* needs front-end tagging */
122
		.key	= 1,
123 124
		.emask	=
			P4_EVENT_ATTR(P4_INSTR_RETIRED, NBOGUSNTAG)	|
125 126
			P4_EVENT_ATTR(P4_INSTR_RETIRED, BOGUSNTAG),
		.escr_msr	= { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
127 128
		.cntr		= { 12, 14 },
	},
129
	[2] = {
130 131 132
		.opcode	= P4_BSQ_CACHE_REFERENCE,
		.config	= 0,
		.dep	= -1,
133
		.key	= 2,
134 135 136 137 138 139 140 141 142 143
		.emask	=
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITS)	|
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITE)	|
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_HITM)	|
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITS)	|
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITE)	|
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_HITM),
		.escr_msr	= { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
		.cntr		= { 0, 2 },
	},
144
	[3] = {
145 146 147
		.opcode	= P4_BSQ_CACHE_REFERENCE,
		.config	= 0,
		.dep	= -1,
148
		.key	= 3,
149 150 151 152 153 154 155
		.emask	=
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_2ndL_MISS)	|
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, RD_3rdL_MISS)	|
			P4_EVENT_ATTR(P4_BSQ_CACHE_REFERENCE, WR_2ndL_MISS),
		.escr_msr	= { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
		.cntr		= { 0, 3 },
	},
156
	[4] = {
157 158 159
		.opcode	= P4_RETIRED_BRANCH_TYPE,
		.config	= 0,
		.dep	= -1,
160
		.key	= 4,
161 162 163 164 165 166 167 168
		.emask	=
			P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, CONDITIONAL)	|
			P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, CALL)		|
			P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, RETURN)		|
			P4_EVENT_ATTR(P4_RETIRED_BRANCH_TYPE, INDIRECT),
		.escr_msr	= { MSR_P4_TBPU_ESCR0, MSR_P4_TBPU_ESCR1 },
		.cntr		= { 4, 6 },
	},
169
	[5] = {
170 171 172
		.opcode	= P4_MISPRED_BRANCH_RETIRED,
		.config	= 0,
		.dep	= -1,
173
		.key	= 5,
174 175 176 177 178
		.emask	=
			P4_EVENT_ATTR(P4_MISPRED_BRANCH_RETIRED, NBOGUS),
		.escr_msr	= { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
		.cntr		= { 12, 14 },
	},
179
	[6] = {
180 181 182
		.opcode	= P4_FSB_DATA_ACTIVITY,
		.config	= p4_config_pack_cccr(P4_CCCR_EDGE | P4_CCCR_COMPARE),
		.dep	= -1,
183
		.key	= 6,
184 185 186 187 188 189
		.emask	=
			P4_EVENT_ATTR(P4_FSB_DATA_ACTIVITY, DRDY_DRV)	|
			P4_EVENT_ATTR(P4_FSB_DATA_ACTIVITY, DRDY_OWN),
		.escr_msr	= { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
		.cntr		= { 0, 2 },
	},
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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
	[KEY_P4_L1D_OP_READ_RESULT_MISS] = {
		.opcode	= P4_REPLAY_EVENT,
		.config	= 0,
		.dep	= -1,
		.msr	= (u64)(1 << 0 | 1 << 24) << 32 | (1 << 0),
		.key	= KEY_P4_L1D_OP_READ_RESULT_MISS,
		.emask	=
			P4_EVENT_ATTR(P4_REPLAY_EVENT, NBOGUS),
		.escr_msr	= { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR2 },
		.cntr		= { 16, 17 },
	},
	[KEY_P4_LL_OP_READ_RESULT_MISS] = {
		.opcode	= P4_REPLAY_EVENT,
		.config	= 0,
		.dep	= -1,
		.msr	= (u64)(1 << 1 | 1 << 24) << 32 | (1 << 0),
		.key	= KEY_P4_LL_OP_READ_RESULT_MISS,
		.emask	=
			P4_EVENT_ATTR(P4_REPLAY_EVENT, NBOGUS),
		.escr_msr	= { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR2 },
		.cntr		= { 16, 17 },
	},
	[KEY_P4_DTLB_OP_READ_RESULT_MISS] = {
		.opcode	= P4_REPLAY_EVENT,
		.config	= 0,
		.dep	= -1,
		.msr	= (u64)(1 << 2 | 1 << 24) << 32 | (1 << 0),
		.key	= KEY_P4_DTLB_OP_READ_RESULT_MISS,
		.emask	=
			P4_EVENT_ATTR(P4_REPLAY_EVENT, NBOGUS),
		.escr_msr	= { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR2 },
		.cntr		= { 16, 17 },
	},
	[KEY_P4_DTLB_OP_WRITE_RESULT_MISS] = {
		.opcode	= P4_REPLAY_EVENT,
		.config	= 0,
		.dep	= -1,
		.msr	= (u64)(1 << 2 | 1 << 24) << 32 | (1 << 1),
		.key	= KEY_P4_DTLB_OP_WRITE_RESULT_MISS,
		.emask	=
			P4_EVENT_ATTR(P4_REPLAY_EVENT, NBOGUS),
		.escr_msr	= { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR2 },
		.cntr		= { 16, 17 },
	},
	[KEY_P4_ITLB_OP_READ_RESULT_ACCESS] = {
		.opcode	= P4_ITLB_REFERENCE,
		.config	= 0,
		.dep	= -1,
		.msr	= 0,
		.key	= KEY_P4_ITLB_OP_READ_RESULT_ACCESS,
		.emask	=
			P4_EVENT_ATTR(P4_ITLB_REFERENCE, HIT),
		.escr_msr	= { MSR_P4_ITLB_ESCR0, MSR_P4_ITLB_ESCR1 },
		.cntr		= { 0, 2 },
	},
	[KEY_P4_ITLB_OP_READ_RESULT_MISS] = {
		.opcode	= P4_ITLB_REFERENCE,
		.config	= 0,
		.dep	= -1,
		.msr	= 0,
		.key	= KEY_P4_ITLB_OP_READ_RESULT_MISS,
		.emask	=
			P4_EVENT_ATTR(P4_ITLB_REFERENCE, MISS),
		.escr_msr	= { MSR_P4_ITLB_ESCR0, MSR_P4_ITLB_ESCR1 },
		.cntr		= { 0, 2 },
	},
	[KEY_P4_UOP_TYPE] = {
257 258 259
		.opcode	= P4_UOP_TYPE,
		.config	= 0,
		.dep	= -1,
260
		.key	= KEY_P4_UOP_TYPE,
261 262 263 264 265 266
		.emask	=
			P4_EVENT_ATTR(P4_UOP_TYPE, TAGLOADS)	|
			P4_EVENT_ATTR(P4_UOP_TYPE, TAGSTORES),
		.escr_msr	= { MSR_P4_RAT_ESCR0, MSR_P4_RAT_ESCR1 },
		.cntr		= { 16, 17 },
	},
267 268 269 270 271 272 273
};

static u64 p4_pmu_event_map(int hw_event)
{
	struct p4_event_template *tpl;
	u64 config;

274
	if (hw_event > ARRAY_SIZE(p4_templates)) {
275 276 277
		printk_once(KERN_ERR "PMU: Incorrect event index\n");
		return 0;
	}
278
	tpl = &p4_templates[hw_event];
279 280 281 282 283 284 285 286 287

	/*
	 * fill config up according to
	 * a predefined event template
	 */
	config  = tpl->config;
	config |= p4_config_pack_escr(P4_EVENT_UNPACK_EVENT(tpl->opcode) << P4_EVNTSEL_EVENT_SHIFT);
	config |= p4_config_pack_escr(tpl->emask << P4_EVNTSEL_EVENTMASK_SHIFT);
	config |= p4_config_pack_cccr(P4_EVENT_UNPACK_SELECTOR(tpl->opcode) << P4_CCCR_ESCR_SELECT_SHIFT);
288
	config |= p4_config_pack_cccr(hw_event & P4_CCCR_RESERVED);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

	return config;
}

/*
 * Note that we still have 5 events (from global events SDM list)
 * intersected in opcode+emask bits so we will need another
 * scheme there do distinguish templates.
 */
static inline int p4_pmu_emask_match(unsigned int dst, unsigned int src)
{
	return dst & src;
}

static struct p4_event_template *p4_pmu_template_lookup(u64 config)
{
305
	int key = p4_config_unpack_key(config);
306

307 308 309 310
	if (key < ARRAY_SIZE(p4_templates))
		return &p4_templates[key];
	else
		return NULL;
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
}

/*
 * We don't control raw events so it's up to the caller
 * to pass sane values (and we don't count the thread number
 * on HT machine but allow HT-compatible specifics to be
 * passed on)
 */
static u64 p4_pmu_raw_event(u64 hw_event)
{
	return hw_event &
		(p4_config_pack_escr(P4_EVNTSEL_MASK_HT) |
		 p4_config_pack_cccr(P4_CCCR_MASK_HT));
}

static int p4_hw_config(struct perf_event_attr *attr, struct hw_perf_event *hwc)
{
	int cpu = raw_smp_processor_id();

	/*
	 * the reason we use cpu that early is that: if we get scheduled
	 * first time on the same cpu -- we will not need swap thread
	 * specific flags in config (and will save some cpu cycles)
	 */

	/* CCCR by default */
	hwc->config = p4_config_pack_cccr(p4_default_cccr_conf(cpu));

	/* Count user and OS events unless not requested to */
	hwc->config |= p4_config_pack_escr(p4_default_escr_conf(cpu, attr->exclude_kernel,
								attr->exclude_user));
342 343 344 345
	/* on HT machine we need a special bit */
	if (p4_ht_active() && p4_ht_thread(cpu))
		hwc->config = p4_set_ht_bit(hwc->config);

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	return 0;
}

static inline void p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc)
{
	unsigned long dummy;

	rdmsrl(hwc->config_base + hwc->idx, dummy);
	if (dummy & P4_CCCR_OVF) {
		(void)checking_wrmsrl(hwc->config_base + hwc->idx,
			((u64)dummy) & ~P4_CCCR_OVF);
	}
}

static inline void p4_pmu_disable_event(struct perf_event *event)
{
	struct hw_perf_event *hwc = &event->hw;

	/*
	 * If event gets disabled while counter is in overflowed
	 * state we need to clear P4_CCCR_OVF, otherwise interrupt get
	 * asserted again and again
	 */
	(void)checking_wrmsrl(hwc->config_base + hwc->idx,
		(u64)(p4_config_unpack_cccr(hwc->config)) &
			~P4_CCCR_ENABLE & ~P4_CCCR_OVF);
}

static void p4_pmu_disable_all(void)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	int idx;

	for (idx = 0; idx < x86_pmu.num_events; idx++) {
		struct perf_event *event = cpuc->events[idx];
		if (!test_bit(idx, cpuc->active_mask))
			continue;
		p4_pmu_disable_event(event);
	}
}

static void p4_pmu_enable_event(struct perf_event *event)
{
	struct hw_perf_event *hwc = &event->hw;
	int thread = p4_ht_config_thread(hwc->config);
	u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config));
	u64 escr_base;
	struct p4_event_template *tpl;
	struct p4_pmu_res *c;

	/*
	 * some preparation work from per-cpu private fields
	 * since we need to find out which ESCR to use
	 */
	c = &__get_cpu_var(p4_pmu_config);
	tpl = c->tpl[hwc->idx];
	if (!tpl) {
		pr_crit("%s: Wrong index: %d\n", __func__, hwc->idx);
		return;
	}
406 407 408 409 410 411

	if (tpl->msr) {
		(void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, tpl->msr >> 32);
		(void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, tpl->msr & 0xffffffff);
	}

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	escr_base = (u64)tpl->escr_msr[thread];

	/*
	 * - we dont support cascaded counters yet
	 * - and counter 1 is broken (erratum)
	 */
	WARN_ON_ONCE(p4_is_event_cascaded(hwc->config));
	WARN_ON_ONCE(hwc->idx == 1);

	(void)checking_wrmsrl(escr_base, escr_conf);
	(void)checking_wrmsrl(hwc->config_base + hwc->idx,
		(u64)(p4_config_unpack_cccr(hwc->config)) | P4_CCCR_ENABLE);
}

static void p4_pmu_enable_all(void)
{
	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
	int idx;

	for (idx = 0; idx < x86_pmu.num_events; idx++) {
		struct perf_event *event = cpuc->events[idx];
		if (!test_bit(idx, cpuc->active_mask))
			continue;
		p4_pmu_enable_event(event);
	}
}

static int p4_pmu_handle_irq(struct pt_regs *regs)
{
	struct perf_sample_data data;
	struct cpu_hw_events *cpuc;
	struct perf_event *event;
	struct hw_perf_event *hwc;
	int idx, handled = 0;
	u64 val;

	data.addr = 0;
	data.raw = NULL;

	cpuc = &__get_cpu_var(cpu_hw_events);

	for (idx = 0; idx < x86_pmu.num_events; idx++) {

		if (!test_bit(idx, cpuc->active_mask))
			continue;

		event = cpuc->events[idx];
		hwc = &event->hw;

		WARN_ON_ONCE(hwc->idx != idx);

		/*
		 * FIXME: Redundant call, actually not needed
		 * but just to check if we're screwed
		 */
		p4_pmu_clear_cccr_ovf(hwc);

		val = x86_perf_event_update(event);
		if (val & (1ULL << (x86_pmu.event_bits - 1)))
			continue;

		/*
		 * event overflow
		 */
		handled		= 1;
		data.period	= event->hw.last_period;

		if (!x86_perf_event_set_period(event))
			continue;
		if (perf_event_overflow(event, 1, &data, regs))
			p4_pmu_disable_event(event);
	}

	if (handled) {
		/* p4 quirk: unmask it again */
		apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
		inc_irq_stat(apic_perf_irqs);
	}

	return handled;
}

/*
 * swap thread specific fields according to a thread
 * we are going to run on
 */
static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu)
{
	u32 escr, cccr;

	/*
	 * we either lucky and continue on same cpu or no HT support
	 */
	if (!p4_should_swap_ts(hwc->config, cpu))
		return;

	/*
	 * the event is migrated from an another logical
	 * cpu, so we need to swap thread specific flags
	 */

	escr = p4_config_unpack_escr(hwc->config);
	cccr = p4_config_unpack_cccr(hwc->config);

	if (p4_ht_thread(cpu)) {
		cccr &= ~P4_CCCR_OVF_PMI_T0;
		cccr |= P4_CCCR_OVF_PMI_T1;
		if (escr & P4_EVNTSEL_T0_OS) {
			escr &= ~P4_EVNTSEL_T0_OS;
			escr |= P4_EVNTSEL_T1_OS;
		}
		if (escr & P4_EVNTSEL_T0_USR) {
			escr &= ~P4_EVNTSEL_T0_USR;
			escr |= P4_EVNTSEL_T1_USR;
		}
		hwc->config  = p4_config_pack_escr(escr);
		hwc->config |= p4_config_pack_cccr(cccr);
		hwc->config |= P4_CONFIG_HT;
	} else {
		cccr &= ~P4_CCCR_OVF_PMI_T1;
		cccr |= P4_CCCR_OVF_PMI_T0;
		if (escr & P4_EVNTSEL_T1_OS) {
			escr &= ~P4_EVNTSEL_T1_OS;
			escr |= P4_EVNTSEL_T0_OS;
		}
		if (escr & P4_EVNTSEL_T1_USR) {
			escr &= ~P4_EVNTSEL_T1_USR;
			escr |= P4_EVNTSEL_T0_USR;
		}
		hwc->config  = p4_config_pack_escr(escr);
		hwc->config |= p4_config_pack_cccr(cccr);
		hwc->config &= ~P4_CONFIG_HT;
	}
}

/* ESCRs are not sequential in memory so we need a map */
548
static const unsigned int p4_escr_map[ARCH_P4_TOTAL_ESCR] = {
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
	MSR_P4_ALF_ESCR0,	/*  0 */
	MSR_P4_ALF_ESCR1,	/*  1 */
	MSR_P4_BPU_ESCR0,	/*  2 */
	MSR_P4_BPU_ESCR1,	/*  3 */
	MSR_P4_BSU_ESCR0,	/*  4 */
	MSR_P4_BSU_ESCR1,	/*  5 */
	MSR_P4_CRU_ESCR0,	/*  6 */
	MSR_P4_CRU_ESCR1,	/*  7 */
	MSR_P4_CRU_ESCR2,	/*  8 */
	MSR_P4_CRU_ESCR3,	/*  9 */
	MSR_P4_CRU_ESCR4,	/* 10 */
	MSR_P4_CRU_ESCR5,	/* 11 */
	MSR_P4_DAC_ESCR0,	/* 12 */
	MSR_P4_DAC_ESCR1,	/* 13 */
	MSR_P4_FIRM_ESCR0,	/* 14 */
	MSR_P4_FIRM_ESCR1,	/* 15 */
	MSR_P4_FLAME_ESCR0,	/* 16 */
	MSR_P4_FLAME_ESCR1,	/* 17 */
	MSR_P4_FSB_ESCR0,	/* 18 */
	MSR_P4_FSB_ESCR1,	/* 19 */
	MSR_P4_IQ_ESCR0,	/* 20 */
	MSR_P4_IQ_ESCR1,	/* 21 */
	MSR_P4_IS_ESCR0,	/* 22 */
	MSR_P4_IS_ESCR1,	/* 23 */
	MSR_P4_ITLB_ESCR0,	/* 24 */
	MSR_P4_ITLB_ESCR1,	/* 25 */
	MSR_P4_IX_ESCR0,	/* 26 */
	MSR_P4_IX_ESCR1,	/* 27 */
	MSR_P4_MOB_ESCR0,	/* 28 */
	MSR_P4_MOB_ESCR1,	/* 29 */
	MSR_P4_MS_ESCR0,	/* 30 */
	MSR_P4_MS_ESCR1,	/* 31 */
	MSR_P4_PMH_ESCR0,	/* 32 */
	MSR_P4_PMH_ESCR1,	/* 33 */
	MSR_P4_RAT_ESCR0,	/* 34 */
	MSR_P4_RAT_ESCR1,	/* 35 */
	MSR_P4_SAAT_ESCR0,	/* 36 */
	MSR_P4_SAAT_ESCR1,	/* 37 */
	MSR_P4_SSU_ESCR0,	/* 38 */
	MSR_P4_SSU_ESCR1,	/* 39 */
	MSR_P4_TBPU_ESCR0,	/* 40 */
	MSR_P4_TBPU_ESCR1,	/* 41 */
	MSR_P4_TC_ESCR0,	/* 42 */
	MSR_P4_TC_ESCR1,	/* 43 */
	MSR_P4_U2L_ESCR0,	/* 44 */
	MSR_P4_U2L_ESCR1,	/* 45 */
};

static int p4_get_escr_idx(unsigned int addr)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(p4_escr_map); i++) {
		if (addr == p4_escr_map[i])
			return i;
	}

	return -1;
}

static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
{
	unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
	unsigned long escr_mask[BITS_TO_LONGS(ARCH_P4_TOTAL_ESCR)];

	struct hw_perf_event *hwc;
	struct p4_event_template *tpl;
	struct p4_pmu_res *c;
	int cpu = raw_smp_processor_id();
	int escr_idx, thread, i, num;

	bitmap_zero(used_mask, X86_PMC_IDX_MAX);
	bitmap_zero(escr_mask, ARCH_P4_TOTAL_ESCR);

	c = &__get_cpu_var(p4_pmu_config);
	/*
	 * Firstly find out which resource events are going
	 * to use, if ESCR+CCCR tuple is already borrowed
	 * then get out of here
	 */
	for (i = 0, num = n; i < n; i++, num--) {
		hwc = &cpuc->event_list[i]->hw;
		tpl = p4_pmu_template_lookup(hwc->config);
		if (!tpl)
			goto done;
		thread = p4_ht_thread(cpu);
		escr_idx = p4_get_escr_idx(tpl->escr_msr[thread]);
		if (escr_idx == -1)
			goto done;

		/* already allocated and remains on the same cpu */
		if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) {
			if (assign)
				assign[i] = hwc->idx;
			/* upstream dependent event */
			if (unlikely(tpl->dep != -1))
				printk_once(KERN_WARNING "PMU: Dep events are "
					"not implemented yet\n");
			goto reserve;
		}

		/* it may be already borrowed */
		if (test_bit(tpl->cntr[thread], used_mask) ||
			test_bit(escr_idx, escr_mask))
			goto done;

		/*
		 * ESCR+CCCR+COUNTERs are available to use lets swap
		 * thread specific bits, push assigned bits
		 * back and save template into per-cpu
		 * area (which will allow us to find out the ESCR
		 * to be used at moment of "enable event via real MSR")
		 */
		p4_pmu_swap_config_ts(hwc, cpu);
		if (assign) {
			assign[i] = tpl->cntr[thread];
			c->tpl[assign[i]] = tpl;
		}
reserve:
		set_bit(tpl->cntr[thread], used_mask);
		set_bit(escr_idx, escr_mask);
	}

done:
	return num ? -ENOSPC : 0;
}

static __initconst struct x86_pmu p4_pmu = {
	.name			= "Netburst P4/Xeon",
	.handle_irq		= p4_pmu_handle_irq,
	.disable_all		= p4_pmu_disable_all,
	.enable_all		= p4_pmu_enable_all,
	.enable			= p4_pmu_enable_event,
	.disable		= p4_pmu_disable_event,
	.eventsel		= MSR_P4_BPU_CCCR0,
	.perfctr		= MSR_P4_BPU_PERFCTR0,
	.event_map		= p4_pmu_event_map,
	.raw_event		= p4_pmu_raw_event,
687
	.max_events		= ARRAY_SIZE(p4_templates),
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
	.get_event_constraints	= x86_get_event_constraints,
	/*
	 * IF HT disabled we may need to use all
	 * ARCH_P4_MAX_CCCR counters simulaneously
	 * though leave it restricted at moment assuming
	 * HT is on
	 */
	.num_events		= ARCH_P4_MAX_CCCR,
	.apic			= 1,
	.event_bits		= 40,
	.event_mask		= (1ULL << 40) - 1,
	.max_period		= (1ULL << 39) - 1,
	.hw_config		= p4_hw_config,
	.schedule_events	= p4_pmu_schedule_events,
};

static __init int p4_pmu_init(void)
{
	unsigned int low, high;

	/* If we get stripped -- indexig fails */
	BUILD_BUG_ON(ARCH_P4_MAX_CCCR > X86_PMC_MAX_GENERIC);

	rdmsr(MSR_IA32_MISC_ENABLE, low, high);
	if (!(low & (1 << 7))) {
		pr_cont("unsupported Netburst CPU model %d ",
			boot_cpu_data.x86_model);
		return -ENODEV;
	}

718 719 720
	memcpy(hw_cache_event_ids, p4_hw_cache_event_ids,
	       sizeof(hw_cache_event_ids));

721 722 723 724 725 726 727 728
	pr_cont("Netburst events, ");

	x86_pmu = p4_pmu;

	return 0;
}

#endif /* CONFIG_CPU_SUP_INTEL */