op_model_amd.c 12.6 KB
Newer Older
1
/*
2
 * @file op_model_amd.c
3
 * athlon / K7 / K8 / Family 10h model-specific MSR operations
L
Linus Torvalds 已提交
4
 *
5
 * @remark Copyright 2002-2009 OProfile authors
L
Linus Torvalds 已提交
6 7 8 9 10
 * @remark Read the file COPYING
 *
 * @author John Levon
 * @author Philippe Elie
 * @author Graydon Hoare
11
 * @author Robert Richter <robert.richter@amd.com>
12 13 14
 * @author Barry Kasindorf <barry.kasindorf@amd.com>
 * @author Jason Yeh <jason.yeh@amd.com>
 * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
15
 */
L
Linus Torvalds 已提交
16 17

#include <linux/oprofile.h>
18 19
#include <linux/device.h>
#include <linux/pci.h>
20
#include <linux/percpu.h>
21

L
Linus Torvalds 已提交
22 23
#include <asm/ptrace.h>
#include <asm/msr.h>
24
#include <asm/nmi.h>
25

L
Linus Torvalds 已提交
26 27 28
#include "op_x86_model.h"
#include "op_counter.h"

29 30
#define NUM_COUNTERS 4
#define NUM_CONTROLS 4
31 32 33 34 35 36 37 38
#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
#define NUM_VIRT_COUNTERS 32
#define NUM_VIRT_CONTROLS 32
#else
#define NUM_VIRT_COUNTERS NUM_COUNTERS
#define NUM_VIRT_CONTROLS NUM_CONTROLS
#endif

39
#define OP_EVENT_MASK			0x0FFF
40
#define OP_CTR_OVERFLOW			(1ULL<<31)
41 42

#define MSR_AMD_EVENTSEL_RESERVED	((0xFFFFFCF0ULL<<32)|(1ULL<<21))
L
Linus Torvalds 已提交
43

44 45 46 47
static unsigned long reset_value[NUM_VIRT_COUNTERS];
#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
DECLARE_PER_CPU(int, switch_index);
#endif
48 49 50

#ifdef CONFIG_OPROFILE_IBS

51
/* IbsFetchCtl bits/masks */
52 53 54 55
#define IBS_FETCH_RAND_EN		(1ULL<<57)
#define IBS_FETCH_VAL			(1ULL<<49)
#define IBS_FETCH_ENABLE		(1ULL<<48)
#define IBS_FETCH_CNT_MASK		0xFFFF0000ULL
56

57
/*IbsOpCtl bits */
58 59 60
#define IBS_OP_CNT_CTL			(1ULL<<19)
#define IBS_OP_VAL			(1ULL<<18)
#define IBS_OP_ENABLE			(1ULL<<17)
61

62 63
#define IBS_FETCH_SIZE			6
#define IBS_OP_SIZE			12
64

65
static int has_ibs;	/* AMD Family10h and later */
66 67 68 69 70 71 72 73 74 75 76

struct op_ibs_config {
	unsigned long op_enabled;
	unsigned long fetch_enabled;
	unsigned long max_cnt_fetch;
	unsigned long max_cnt_op;
	unsigned long rand_en;
	unsigned long dispatched_ops;
};

static struct op_ibs_config ibs_config;
77

78 79
#endif

80
/* functions for op_amd_spec */
81

82
static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
L
Linus Torvalds 已提交
83
{
84 85
	int i;

86
	for (i = 0; i < NUM_COUNTERS; i++) {
87 88
		if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
			msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
89 90 91 92
		else
			msrs->counters[i].addr = 0;
	}

93
	for (i = 0; i < NUM_CONTROLS; i++) {
94 95
		if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
			msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
96 97 98
		else
			msrs->controls[i].addr = 0;
	}
99 100 101 102 103 104 105 106 107 108

#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
	for (i = 0; i < NUM_VIRT_COUNTERS; i++) {
		int hw_counter = i % NUM_CONTROLS;
		if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
			msrs->multiplex[i].addr = MSR_K7_PERFCTR0 + hw_counter;
		else
			msrs->multiplex[i].addr = 0;
	}
#endif
L
Linus Torvalds 已提交
109 110
}

111 112
static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
			      struct op_msrs const * const msrs)
L
Linus Torvalds 已提交
113
{
114
	u64 val;
L
Linus Torvalds 已提交
115
	int i;
116

117 118 119 120 121 122 123 124 125
	/* setup reset_value */
	for (i = 0; i < NUM_VIRT_COUNTERS; ++i) {
		if (counter_config[i].enabled) {
			reset_value[i] = counter_config[i].count;
		} else {
			reset_value[i] = 0;
		}
	}

L
Linus Torvalds 已提交
126
	/* clear all counters */
127
	for (i = 0; i < NUM_CONTROLS; ++i) {
128
		if (unlikely(!msrs->controls[i].addr))
129
			continue;
130 131 132
		rdmsrl(msrs->controls[i].addr, val);
		val &= model->reserved;
		wrmsrl(msrs->controls[i].addr, val);
L
Linus Torvalds 已提交
133
	}
134

L
Linus Torvalds 已提交
135
	/* avoid a false detection of ctr overflows in NMI handler */
136
	for (i = 0; i < NUM_COUNTERS; ++i) {
137
		if (unlikely(!msrs->counters[i].addr))
138
			continue;
139
		wrmsrl(msrs->counters[i].addr, -1LL);
L
Linus Torvalds 已提交
140 141 142
	}

	/* enable active counters */
143
	for (i = 0; i < NUM_COUNTERS; ++i) {
144 145 146 147 148 149 150 151 152 153
#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
		int offset = i + __get_cpu_var(switch_index);
#else
		int offset = i;
#endif
		if (counter_config[offset].enabled && msrs->counters[i].addr) {
			/* setup counter registers */
			wrmsrl(msrs->counters[i].addr, -(u64)reset_value[offset]);

			/* setup control registers */
154 155
			rdmsrl(msrs->controls[i].addr, val);
			val &= model->reserved;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
			val |= op_x86_get_ctrl(model, &counter_config[offset]);
			wrmsrl(msrs->controls[i].addr, val);
		}
	}
}


#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX

static void op_amd_switch_ctrl(struct op_x86_model_spec const *model,
			       struct op_msrs const * const msrs)
{
	u64 val;
	int i;

	/* enable active counters */
	for (i = 0; i < NUM_COUNTERS; ++i) {
		int offset = i + __get_cpu_var(switch_index);
		if (counter_config[offset].enabled) {
			/* setup control registers */
			rdmsrl(msrs->controls[i].addr, val);
			val &= model->reserved;
			val |= op_x86_get_ctrl(model, &counter_config[offset]);
179
			wrmsrl(msrs->controls[i].addr, val);
L
Linus Torvalds 已提交
180 181 182 183
		}
	}
}

184 185 186
#endif


187 188
#ifdef CONFIG_OPROFILE_IBS

189 190 191
static inline int
op_amd_handle_ibs(struct pt_regs * const regs,
		  struct op_msrs const * const msrs)
L
Linus Torvalds 已提交
192
{
193
	u64 val, ctl;
194
	struct op_entry entry;
L
Linus Torvalds 已提交
195

196
	if (!has_ibs)
197
		return 0;
L
Linus Torvalds 已提交
198

199
	if (ibs_config.fetch_enabled) {
200 201 202 203
		rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
		if (ctl & IBS_FETCH_VAL) {
			rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
			oprofile_write_reserve(&entry, regs, val,
204
					       IBS_FETCH_CODE, IBS_FETCH_SIZE);
205 206
			oprofile_add_data64(&entry, val);
			oprofile_add_data64(&entry, ctl);
207
			rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
208
			oprofile_add_data64(&entry, val);
209
			oprofile_write_commit(&entry);
210

R
Robert Richter 已提交
211
			/* reenable the IRQ */
212 213 214
			ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT_MASK);
			ctl |= IBS_FETCH_ENABLE;
			wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
215 216 217
		}
	}

218
	if (ibs_config.op_enabled) {
219 220 221 222
		rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
		if (ctl & IBS_OP_VAL) {
			rdmsrl(MSR_AMD64_IBSOPRIP, val);
			oprofile_write_reserve(&entry, regs, val,
223
					       IBS_OP_CODE, IBS_OP_SIZE);
224
			oprofile_add_data64(&entry, val);
225
			rdmsrl(MSR_AMD64_IBSOPDATA, val);
226
			oprofile_add_data64(&entry, val);
227
			rdmsrl(MSR_AMD64_IBSOPDATA2, val);
228
			oprofile_add_data64(&entry, val);
229
			rdmsrl(MSR_AMD64_IBSOPDATA3, val);
230
			oprofile_add_data64(&entry, val);
231
			rdmsrl(MSR_AMD64_IBSDCLINAD, val);
232
			oprofile_add_data64(&entry, val);
233
			rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
234
			oprofile_add_data64(&entry, val);
235
			oprofile_write_commit(&entry);
236 237

			/* reenable the IRQ */
238 239 240
			ctl &= ~IBS_OP_VAL & 0xFFFFFFFF;
			ctl |= IBS_OP_ENABLE;
			wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
241 242 243
		}
	}

L
Linus Torvalds 已提交
244 245 246
	return 1;
}

247 248
static inline void op_amd_start_ibs(void)
{
249
	u64 val;
250
	if (has_ibs && ibs_config.fetch_enabled) {
251 252 253 254
		val = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
		val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
		val |= IBS_FETCH_ENABLE;
		wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
255 256 257
	}

	if (has_ibs && ibs_config.op_enabled) {
258 259 260 261
		val = (ibs_config.max_cnt_op >> 4) & 0xFFFF;
		val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
		val |= IBS_OP_ENABLE;
		wrmsrl(MSR_AMD64_IBSOPCTL, val);
262 263 264 265 266
	}
}

static void op_amd_stop_ibs(void)
{
267
	if (has_ibs && ibs_config.fetch_enabled)
268
		/* clear max count and enable */
269
		wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
270

271
	if (has_ibs && ibs_config.op_enabled)
272
		/* clear max count and enable */
273
		wrmsrl(MSR_AMD64_IBSOPCTL, 0);
274 275 276 277 278
}

#else

static inline int op_amd_handle_ibs(struct pt_regs * const regs,
279 280 281 282
				    struct op_msrs const * const msrs)
{
	return 0;
}
283 284 285
static inline void op_amd_start_ibs(void) { }
static inline void op_amd_stop_ibs(void) { }

286 287
#endif

288 289 290
static int op_amd_check_ctrs(struct pt_regs * const regs,
			     struct op_msrs const * const msrs)
{
291
	u64 val;
292 293
	int i;

294
	for (i = 0; i < NUM_COUNTERS; ++i) {
295 296 297 298 299 300
#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
		int offset = i + __get_cpu_var(switch_index);
#else
		int offset = i;
#endif
		if (!reset_value[offset])
301
			continue;
302 303 304 305
		rdmsrl(msrs->counters[i].addr, val);
		/* bit is clear if overflowed: */
		if (val & OP_CTR_OVERFLOW)
			continue;
306 307
		oprofile_add_sample(regs, offset);
		wrmsrl(msrs->counters[i].addr, -(u64)reset_value[offset]);
308 309 310 311 312 313 314
	}

	op_amd_handle_ibs(regs, msrs);

	/* See op_model_ppro.c */
	return 1;
}
315

316
static void op_amd_start(struct op_msrs const * const msrs)
L
Linus Torvalds 已提交
317
{
318
	u64 val;
L
Linus Torvalds 已提交
319
	int i;
320

321
	for (i = 0; i < NUM_COUNTERS; ++i) {
322 323 324 325 326 327
#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
		int offset = i + __get_cpu_var(switch_index);
#else
		int offset = i;
#endif
		if (reset_value[offset]) {
328 329 330
			rdmsrl(msrs->controls[i].addr, val);
			val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
			wrmsrl(msrs->controls[i].addr, val);
L
Linus Torvalds 已提交
331 332
		}
	}
333

334
	op_amd_start_ibs();
L
Linus Torvalds 已提交
335 336
}

337
static void op_amd_stop(struct op_msrs const * const msrs)
L
Linus Torvalds 已提交
338
{
339
	u64 val;
L
Linus Torvalds 已提交
340 341
	int i;

R
Robert Richter 已提交
342 343 344 345
	/*
	 * Subtle: stop on all counters to avoid race with setting our
	 * pm callback
	 */
346
	for (i = 0; i < NUM_COUNTERS; ++i) {
347 348 349
#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
		if (!reset_value[i + per_cpu(switch_index, smp_processor_id())])
#else
350
		if (!reset_value[i])
351
#endif
352
			continue;
353 354 355
		rdmsrl(msrs->controls[i].addr, val);
		val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
		wrmsrl(msrs->controls[i].addr, val);
L
Linus Torvalds 已提交
356
	}
357

358
	op_amd_stop_ibs();
L
Linus Torvalds 已提交
359 360
}

361
static void op_amd_shutdown(struct op_msrs const * const msrs)
362 363 364
{
	int i;

365
	for (i = 0; i < NUM_COUNTERS; ++i) {
366
		if (msrs->counters[i].addr)
367 368
			release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
	}
369
	for (i = 0; i < NUM_COUNTERS; ++i) {
370
		if (msrs->controls[i].addr)
371 372 373
			release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
	}
}
L
Linus Torvalds 已提交
374

375
#ifdef CONFIG_OPROFILE_IBS
376

377 378
static u8 ibs_eilvt_off;

379 380
static inline void apic_init_ibs_nmi_per_cpu(void *arg)
{
381
	ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
382 383 384 385 386 387 388
}

static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
{
	setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
}

389
static int init_ibs_nmi(void)
390 391 392 393 394 395 396 397
{
#define IBSCTL_LVTOFFSETVAL		(1 << 8)
#define IBSCTL				0x1cc
	struct pci_dev *cpu_cfg;
	int nodes;
	u32 value = 0;

	/* per CPU setup */
398
	on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
399 400 401 402 403 404 405 406 407 408 409 410 411 412

	nodes = 0;
	cpu_cfg = NULL;
	do {
		cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
					 PCI_DEVICE_ID_AMD_10H_NB_MISC,
					 cpu_cfg);
		if (!cpu_cfg)
			break;
		++nodes;
		pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
				       | IBSCTL_LVTOFFSETVAL);
		pci_read_config_dword(cpu_cfg, IBSCTL, &value);
		if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
413
			pci_dev_put(cpu_cfg);
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
			printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
				"IBSCTL = 0x%08x", value);
			return 1;
		}
	} while (1);

	if (!nodes) {
		printk(KERN_DEBUG "No CPU node configured for IBS");
		return 1;
	}

#ifdef CONFIG_NUMA
	/* Sanity check */
	/* Works only for 64bit with proper numa implementation. */
	if (nodes != num_possible_nodes()) {
		printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
			"found: %d, expected %d",
			nodes, num_possible_nodes());
		return 1;
	}
#endif
	return 0;
}

438 439 440
/* uninitialize the APIC for the IBS interrupts if needed */
static void clear_ibs_nmi(void)
{
441
	if (has_ibs)
442 443 444
		on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
}

R
Robert Richter 已提交
445
/* initialize the APIC for the IBS interrupts if available */
446
static void ibs_init(void)
447
{
448
	has_ibs = boot_cpu_has(X86_FEATURE_IBS);
449

450
	if (!has_ibs)
451 452
		return;

453
	if (init_ibs_nmi()) {
454
		has_ibs = 0;
455 456 457 458
		return;
	}

	printk(KERN_INFO "oprofile: AMD IBS detected\n");
459 460
}

461
static void ibs_exit(void)
462
{
463
	if (!has_ibs)
464 465 466
		return;

	clear_ibs_nmi();
467 468
}

R
Robert Richter 已提交
469
static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
470

R
Robert Richter 已提交
471
static int setup_ibs_files(struct super_block *sb, struct dentry *root)
472 473
{
	struct dentry *dir;
474 475 476 477 478 479 480 481
	int ret = 0;

	/* architecture specific files */
	if (create_arch_files)
		ret = create_arch_files(sb, root);

	if (ret)
		return ret;
482

483
	if (!has_ibs)
484 485 486
		return ret;

	/* model specific files */
487 488 489 490 491 492 493

	/* setup some reasonable defaults */
	ibs_config.max_cnt_fetch = 250000;
	ibs_config.fetch_enabled = 0;
	ibs_config.max_cnt_op = 250000;
	ibs_config.op_enabled = 0;
	ibs_config.dispatched_ops = 1;
494 495

	dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
496
	oprofilefs_create_ulong(sb, dir, "enable",
497
				&ibs_config.fetch_enabled);
498
	oprofilefs_create_ulong(sb, dir, "max_count",
499 500 501 502
				&ibs_config.max_cnt_fetch);
	oprofilefs_create_ulong(sb, dir, "rand_enable",
				&ibs_config.rand_en);

503
	dir = oprofilefs_mkdir(sb, root, "ibs_op");
504
	oprofilefs_create_ulong(sb, dir, "enable",
505
				&ibs_config.op_enabled);
506
	oprofilefs_create_ulong(sb, dir, "max_count",
507
				&ibs_config.max_cnt_op);
508
	oprofilefs_create_ulong(sb, dir, "dispatched_ops",
509
				&ibs_config.dispatched_ops);
510 511

	return 0;
512 513
}

514 515
static int op_amd_init(struct oprofile_operations *ops)
{
516
	ibs_init();
517 518
	create_arch_files = ops->create_files;
	ops->create_files = setup_ibs_files;
519 520 521 522 523
	return 0;
}

static void op_amd_exit(void)
{
524
	ibs_exit();
525 526
}

527 528 529 530 531 532 533 534 535 536 537 538
#else

/* no IBS support */

static int op_amd_init(struct oprofile_operations *ops)
{
	return 0;
}

static void op_amd_exit(void) {}

#endif /* CONFIG_OPROFILE_IBS */
539

540
struct op_x86_model_spec const op_amd_spec = {
R
Robert Richter 已提交
541 542
	.num_counters		= NUM_COUNTERS,
	.num_controls		= NUM_CONTROLS,
543 544
	.num_virt_counters	= NUM_VIRT_COUNTERS,
	.num_virt_controls	= NUM_VIRT_CONTROLS,
545 546 547 548
	.reserved		= MSR_AMD_EVENTSEL_RESERVED,
	.event_mask		= OP_EVENT_MASK,
	.init			= op_amd_init,
	.exit			= op_amd_exit,
R
Robert Richter 已提交
549 550 551 552 553
	.fill_in_addresses	= &op_amd_fill_in_addresses,
	.setup_ctrs		= &op_amd_setup_ctrs,
	.check_ctrs		= &op_amd_check_ctrs,
	.start			= &op_amd_start,
	.stop			= &op_amd_stop,
554
	.shutdown		= &op_amd_shutdown,
555 556 557
#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
	.switch_ctrl		= &op_amd_switch_ctrl,
#endif
L
Linus Torvalds 已提交
558
};