op_model_ppro.c 6.4 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/perf_counter.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
#define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
30
#define CTR_OVERFLOWED(n) (!((n) & (1ULL<<(counter_width-1))))
L
Linus Torvalds 已提交
31

32 33 34
#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 已提交
35 36 37 38
#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)
39 40
#define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
#define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
L
Linus Torvalds 已提交
41 42 43
#define CTRL_SET_UM(val, m) (val |= (m << 8))
#define CTRL_SET_EVENT(val, e) (val |= e)

44
static u64 *reset_value;
45

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

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

57
	for (i = 0; i < num_counters; i++) {
58 59 60 61 62
		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 已提交
63 64 65 66 67 68 69 70
}


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

71
	if (!reset_value) {
72
		reset_value = kmalloc(sizeof(reset_value[0]) * num_counters,
73 74 75 76 77 78 79 80 81 82 83 84
					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 已提交
85
	/* clear all counters */
86
	for (i = 0 ; i < num_counters; ++i) {
87
		if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
88
			continue;
L
Linus Torvalds 已提交
89 90 91 92
		CTRL_READ(low, high, msrs, i);
		CTRL_CLEAR(low);
		CTRL_WRITE(low, high, msrs, i);
	}
93

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

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

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

			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);
116 117
		} else {
			reset_value[i] = 0;
L
Linus Torvalds 已提交
118 119 120 121
		}
	}
}

122

L
Linus Torvalds 已提交
123 124 125
static int ppro_check_ctrs(struct pt_regs * const regs,
			   struct op_msrs const * const msrs)
{
126
	u64 val;
L
Linus Torvalds 已提交
127
	int i;
128

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

	/* 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;
}

153

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

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


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

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

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

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


206
struct op_x86_model_spec op_ppro_spec = {
207 208 209 210 211 212 213 214
	.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
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
};

/*
 * 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;
244 245
	op_ppro_spec.num_counters = num_counters;
	op_ppro_spec.num_controls = num_counters;
246 247 248 249
}

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