op_model_ppro.c 6.5 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2
 * @file op_model_ppro.h
3
 * Family 6 perfmon and architectural perfmon MSR operations
L
Linus Torvalds 已提交
4 5
 *
 * @remark Copyright 2002 OProfile authors
6
 * @remark Copyright 2008 Intel Corporation
L
Linus Torvalds 已提交
7 8 9 10 11
 * @remark Read the file COPYING
 *
 * @author John Levon
 * @author Philippe Elie
 * @author Graydon Hoare
12
 * @author Andi Kleen
L
Linus Torvalds 已提交
13 14 15
 */

#include <linux/oprofile.h>
16
#include <linux/slab.h>
L
Linus Torvalds 已提交
17 18 19
#include <asm/ptrace.h>
#include <asm/msr.h>
#include <asm/apic.h>
20
#include <asm/nmi.h>
21
#include <asm/intel_arch_perfmon.h>
22

L
Linus Torvalds 已提交
23 24 25
#include "op_x86_model.h"
#include "op_counter.h"

26 27
static int num_counters = 2;
static int counter_width = 32;
L
Linus Torvalds 已提交
28

29 30
#define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
#define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
31
#define CTR_OVERFLOWED(n) (!((n) & (1U<<(counter_width-1))))
L
Linus Torvalds 已提交
32

33 34 35
#define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
#define CTRL_READ(l, h, msrs, c) do {rdmsr((msrs->controls[(c)].addr), (l), (h)); } while (0)
#define CTRL_WRITE(l, h, msrs, c) do {wrmsr((msrs->controls[(c)].addr), (l), (h)); } while (0)
L
Linus Torvalds 已提交
36 37 38 39
#define CTRL_SET_ACTIVE(n) (n |= (1<<22))
#define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
#define CTRL_CLEAR(x) (x &= (1<<21))
#define CTRL_SET_ENABLE(val) (val |= 1<<20)
40 41
#define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
#define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
L
Linus Torvalds 已提交
42 43 44
#define CTRL_SET_UM(val, m) (val |= (m << 8))
#define CTRL_SET_EVENT(val, e) (val |= e)

45
static u64 *reset_value;
46

L
Linus Torvalds 已提交
47 48
static void ppro_fill_in_addresses(struct op_msrs * const msrs)
{
49 50
	int i;

51
	for (i = 0; i < num_counters; i++) {
52 53 54 55 56
		if (reserve_perfctr_nmi(MSR_P6_PERFCTR0 + i))
			msrs->counters[i].addr = MSR_P6_PERFCTR0 + i;
		else
			msrs->counters[i].addr = 0;
	}
57

58
	for (i = 0; i < num_counters; i++) {
59 60 61 62 63
		if (reserve_evntsel_nmi(MSR_P6_EVNTSEL0 + i))
			msrs->controls[i].addr = MSR_P6_EVNTSEL0 + i;
		else
			msrs->controls[i].addr = 0;
	}
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71
}


static void ppro_setup_ctrs(struct op_msrs const * const msrs)
{
	unsigned int low, high;
	int i;

72 73 74 75 76 77 78 79 80 81 82 83 84 85
	if (!reset_value) {
		reset_value = kmalloc(sizeof(unsigned) * num_counters,
					GFP_ATOMIC);
		if (!reset_value)
			return;
	}

	if (cpu_has_arch_perfmon) {
		union cpuid10_eax eax;
		eax.full = cpuid_eax(0xa);
		if (counter_width < eax.split.bit_width)
			counter_width = eax.split.bit_width;
	}

L
Linus Torvalds 已提交
86
	/* clear all counters */
87
	for (i = 0 ; i < num_counters; ++i) {
88
		if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
89
			continue;
L
Linus Torvalds 已提交
90 91 92 93
		CTRL_READ(low, high, msrs, i);
		CTRL_CLEAR(low);
		CTRL_WRITE(low, high, msrs, i);
	}
94

L
Linus Torvalds 已提交
95
	/* avoid a false detection of ctr overflows in NMI handler */
96
	for (i = 0; i < num_counters; ++i) {
97
		if (unlikely(!CTR_IS_RESERVED(msrs, i)))
98
			continue;
99
		wrmsrl(msrs->counters[i].addr, -1LL);
L
Linus Torvalds 已提交
100 101 102
	}

	/* enable active counters */
103
	for (i = 0; i < num_counters; ++i) {
104
		if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
L
Linus Torvalds 已提交
105 106
			reset_value[i] = counter_config[i].count;

107
			wrmsrl(msrs->counters[i].addr, -reset_value[i]);
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116

			CTRL_READ(low, high, msrs, i);
			CTRL_CLEAR(low);
			CTRL_SET_ENABLE(low);
			CTRL_SET_USR(low, counter_config[i].user);
			CTRL_SET_KERN(low, counter_config[i].kernel);
			CTRL_SET_UM(low, counter_config[i].unit_mask);
			CTRL_SET_EVENT(low, counter_config[i].event);
			CTRL_WRITE(low, high, msrs, i);
117 118
		} else {
			reset_value[i] = 0;
L
Linus Torvalds 已提交
119 120 121 122
		}
	}
}

123

L
Linus Torvalds 已提交
124 125 126 127 128
static int ppro_check_ctrs(struct pt_regs * const regs,
			   struct op_msrs const * const msrs)
{
	unsigned int low, high;
	int i;
129

130
	for (i = 0 ; i < num_counters; ++i) {
131 132
		if (!reset_value[i])
			continue;
L
Linus Torvalds 已提交
133 134 135
		CTR_READ(low, high, msrs, i);
		if (CTR_OVERFLOWED(low)) {
			oprofile_add_sample(regs, i);
136
			wrmsrl(msrs->counters[i].addr, -reset_value[i]);
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
		}
	}

	/* Only P6 based Pentium M need to re-unmask the apic vector but it
	 * doesn't hurt other P6 variant */
	apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);

	/* We can't work out if we really handled an interrupt. We
	 * might have caught a *second* counter just after overflowing
	 * the interrupt for this counter then arrives
	 * and we don't find a counter that's overflowed, so we
	 * would return 0 and get dazed + confused. Instead we always
	 * assume we found an overflow. This sucks.
	 */
	return 1;
}

154

L
Linus Torvalds 已提交
155 156
static void ppro_start(struct op_msrs const * const msrs)
{
157
	unsigned int low, high;
158
	int i;
159

160
	for (i = 0; i < num_counters; ++i) {
161 162 163 164 165
		if (reset_value[i]) {
			CTRL_READ(low, high, msrs, i);
			CTRL_SET_ACTIVE(low);
			CTRL_WRITE(low, high, msrs, i);
		}
166
	}
L
Linus Torvalds 已提交
167 168 169 170 171
}


static void ppro_stop(struct op_msrs const * const msrs)
{
172
	unsigned int low, high;
173
	int i;
174

175
	for (i = 0; i < num_counters; ++i) {
176 177 178
		if (!reset_value[i])
			continue;
		CTRL_READ(low, high, msrs, i);
179
		CTRL_SET_INACTIVE(low);
180
		CTRL_WRITE(low, high, msrs, i);
181 182 183 184 185 186 187
	}
}

static void ppro_shutdown(struct op_msrs const * const msrs)
{
	int i;

188
	for (i = 0 ; i < num_counters ; ++i) {
189
		if (CTR_IS_RESERVED(msrs, i))
190 191
			release_perfctr_nmi(MSR_P6_PERFCTR0 + i);
	}
192
	for (i = 0 ; i < num_counters ; ++i) {
193
		if (CTRL_IS_RESERVED(msrs, i))
194 195
			release_evntsel_nmi(MSR_P6_EVNTSEL0 + i);
	}
196 197 198 199
	if (reset_value) {
		kfree(reset_value);
		reset_value = NULL;
	}
L
Linus Torvalds 已提交
200 201 202
}


203
struct op_x86_model_spec op_ppro_spec = {
204 205 206 207 208 209 210 211
	.num_counters		= 2,	/* can be overriden */
	.num_controls		= 2,	/* dito */
	.fill_in_addresses	= &ppro_fill_in_addresses,
	.setup_ctrs		= &ppro_setup_ctrs,
	.check_ctrs		= &ppro_check_ctrs,
	.start			= &ppro_start,
	.stop			= &ppro_stop,
	.shutdown		= &ppro_shutdown
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
};

/*
 * Architectural performance monitoring.
 *
 * Newer Intel CPUs (Core1+) have support for architectural
 * events described in CPUID 0xA. See the IA32 SDM Vol3b.18 for details.
 * The advantage of this is that it can be done without knowing about
 * the specific CPU.
 */

void arch_perfmon_setup_counters(void)
{
	union cpuid10_eax eax;

	eax.full = cpuid_eax(0xa);

	/* Workaround for BIOS bugs in 6/15. Taken from perfmon2 */
	if (eax.split.version_id == 0 && current_cpu_data.x86 == 6 &&
		current_cpu_data.x86_model == 15) {
		eax.split.version_id = 2;
		eax.split.num_counters = 2;
		eax.split.bit_width = 40;
	}

	num_counters = eax.split.num_counters;

	op_arch_perfmon_spec.num_counters = num_counters;
	op_arch_perfmon_spec.num_controls = num_counters;
241 242
	op_ppro_spec.num_counters = num_counters;
	op_ppro_spec.num_controls = num_counters;
243 244 245 246
}

struct op_x86_model_spec op_arch_perfmon_spec = {
	/* num_counters/num_controls filled in at runtime */
R
Robert Richter 已提交
247
	.fill_in_addresses	= &ppro_fill_in_addresses,
248
	/* user space does the cpuid check for available events */
R
Robert Richter 已提交
249 250 251 252 253
	.setup_ctrs		= &ppro_setup_ctrs,
	.check_ctrs		= &ppro_check_ctrs,
	.start			= &ppro_start,
	.stop			= &ppro_stop,
	.shutdown		= &ppro_shutdown
L
Linus Torvalds 已提交
254
};