blob: a922a1a815c319187021adb10ae2678716751b16 [file] [log] [blame]
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * @file op_model_ppro.h
Andi Kleenb9917022008-08-18 14:50:31 +02003 * Family 6 perfmon and architectural perfmon MSR operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * @remark Copyright 2002 OProfile authors
Andi Kleenb9917022008-08-18 14:50:31 +02006 * @remark Copyright 2008 Intel Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * @remark Read the file COPYING
8 *
9 * @author John Levon
10 * @author Philippe Elie
11 * @author Graydon Hoare
Andi Kleenb9917022008-08-18 14:50:31 +020012 * @author Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#include <linux/oprofile.h>
Andi Kleenb9917022008-08-18 14:50:31 +020016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/ptrace.h>
18#include <asm/msr.h>
19#include <asm/apic.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020020#include <asm/nmi.h>
Andi Kleenb9917022008-08-18 14:50:31 +020021#include <asm/intel_arch_perfmon.h>
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "op_x86_model.h"
24#include "op_counter.h"
25
Andi Kleenb9917022008-08-18 14:50:31 +020026static int num_counters = 2;
27static int counter_width = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Andi Kleen7c64ade2008-11-07 14:02:49 +010029#define CTR_OVERFLOWED(n) (!((n) & (1ULL<<(counter_width-1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010031#define CTRL_READ(l, h, msrs, c) do {rdmsr((msrs->controls[(c)].addr), (l), (h)); } while (0)
32#define CTRL_WRITE(l, h, msrs, c) do {wrmsr((msrs->controls[(c)].addr), (l), (h)); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#define CTRL_CLEAR(x) (x &= (1<<21))
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define CTRL_SET_EVENT(val, e) (val |= e)
35
Andi Kleenb9917022008-08-18 14:50:31 +020036static u64 *reset_value;
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static void ppro_fill_in_addresses(struct op_msrs * const msrs)
39{
Don Zickuscb9c4482006-09-26 10:52:26 +020040 int i;
41
Andi Kleenb9917022008-08-18 14:50:31 +020042 for (i = 0; i < num_counters; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +020043 if (reserve_perfctr_nmi(MSR_P6_PERFCTR0 + i))
44 msrs->counters[i].addr = MSR_P6_PERFCTR0 + i;
45 else
46 msrs->counters[i].addr = 0;
47 }
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010048
Andi Kleenb9917022008-08-18 14:50:31 +020049 for (i = 0; i < num_counters; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +020050 if (reserve_evntsel_nmi(MSR_P6_EVNTSEL0 + i))
51 msrs->controls[i].addr = MSR_P6_EVNTSEL0 + i;
52 else
53 msrs->controls[i].addr = 0;
54 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57
58static void ppro_setup_ctrs(struct op_msrs const * const msrs)
59{
60 unsigned int low, high;
61 int i;
62
Andi Kleenb9917022008-08-18 14:50:31 +020063 if (!reset_value) {
Eric Dumazeta4a16be2008-11-10 09:05:37 +010064 reset_value = kmalloc(sizeof(reset_value[0]) * num_counters,
Andi Kleenb9917022008-08-18 14:50:31 +020065 GFP_ATOMIC);
66 if (!reset_value)
67 return;
68 }
69
70 if (cpu_has_arch_perfmon) {
71 union cpuid10_eax eax;
72 eax.full = cpuid_eax(0xa);
Tim Blechmann780eef92009-02-19 17:34:03 +010073
74 /*
75 * For Core2 (family 6, model 15), don't reset the
76 * counter width:
77 */
78 if (!(eax.split.version_id == 0 &&
79 current_cpu_data.x86 == 6 &&
80 current_cpu_data.x86_model == 15)) {
81
82 if (counter_width < eax.split.bit_width)
83 counter_width = eax.split.bit_width;
84 }
Andi Kleenb9917022008-08-18 14:50:31 +020085 }
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 /* clear all counters */
Andi Kleenb9917022008-08-18 14:50:31 +020088 for (i = 0 ; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010089 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020090 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 CTRL_READ(low, high, msrs, i);
92 CTRL_CLEAR(low);
93 CTRL_WRITE(low, high, msrs, i);
94 }
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010095
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* avoid a false detection of ctr overflows in NMI handler */
Andi Kleenb9917022008-08-18 14:50:31 +020097 for (i = 0; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010098 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020099 continue;
Andi Kleenb9917022008-08-18 14:50:31 +0200100 wrmsrl(msrs->counters[i].addr, -1LL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
103 /* enable active counters */
Andi Kleenb9917022008-08-18 14:50:31 +0200104 for (i = 0; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100105 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 reset_value[i] = counter_config[i].count;
107
Andi Kleenb9917022008-08-18 14:50:31 +0200108 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 CTRL_READ(low, high, msrs, i);
111 CTRL_CLEAR(low);
112 CTRL_SET_ENABLE(low);
113 CTRL_SET_USR(low, counter_config[i].user);
114 CTRL_SET_KERN(low, counter_config[i].kernel);
115 CTRL_SET_UM(low, counter_config[i].unit_mask);
116 CTRL_SET_EVENT(low, counter_config[i].event);
117 CTRL_WRITE(low, high, msrs, i);
Don Zickuscb9c4482006-09-26 10:52:26 +0200118 } else {
119 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 }
122}
123
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int ppro_check_ctrs(struct pt_regs * const regs,
126 struct op_msrs const * const msrs)
127{
Andi Kleen7c64ade2008-11-07 14:02:49 +0100128 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int i;
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100130
Andi Kleenb9917022008-08-18 14:50:31 +0200131 for (i = 0 ; i < num_counters; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200132 if (!reset_value[i])
133 continue;
Andi Kleen7c64ade2008-11-07 14:02:49 +0100134 rdmsrl(msrs->counters[i].addr, val);
135 if (CTR_OVERFLOWED(val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 oprofile_add_sample(regs, i);
Andi Kleenb9917022008-08-18 14:50:31 +0200137 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 }
140
141 /* Only P6 based Pentium M need to re-unmask the apic vector but it
142 * doesn't hurt other P6 variant */
143 apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
144
145 /* We can't work out if we really handled an interrupt. We
146 * might have caught a *second* counter just after overflowing
147 * the interrupt for this counter then arrives
148 * and we don't find a counter that's overflowed, so we
149 * would return 0 and get dazed + confused. Instead we always
150 * assume we found an overflow. This sucks.
151 */
152 return 1;
153}
154
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void ppro_start(struct op_msrs const * const msrs)
157{
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100158 unsigned int low, high;
Arun Sharma6b77df02006-09-29 02:00:01 -0700159 int i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200160
Eric Dumazet9ea84ad2008-12-02 07:21:21 +0100161 if (!reset_value)
162 return;
Andi Kleenb9917022008-08-18 14:50:31 +0200163 for (i = 0; i < num_counters; ++i) {
Arun Sharma6b77df02006-09-29 02:00:01 -0700164 if (reset_value[i]) {
165 CTRL_READ(low, high, msrs, i);
166 CTRL_SET_ACTIVE(low);
167 CTRL_WRITE(low, high, msrs, i);
168 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172
173static void ppro_stop(struct op_msrs const * const msrs)
174{
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100175 unsigned int low, high;
Arun Sharma6b77df02006-09-29 02:00:01 -0700176 int i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200177
Eric Dumazet9ea84ad2008-12-02 07:21:21 +0100178 if (!reset_value)
179 return;
Andi Kleenb9917022008-08-18 14:50:31 +0200180 for (i = 0; i < num_counters; ++i) {
Arun Sharma6b77df02006-09-29 02:00:01 -0700181 if (!reset_value[i])
182 continue;
183 CTRL_READ(low, high, msrs, i);
Don Zickuscb9c4482006-09-26 10:52:26 +0200184 CTRL_SET_INACTIVE(low);
Arun Sharma6b77df02006-09-29 02:00:01 -0700185 CTRL_WRITE(low, high, msrs, i);
Don Zickuscb9c4482006-09-26 10:52:26 +0200186 }
187}
188
189static void ppro_shutdown(struct op_msrs const * const msrs)
190{
191 int i;
192
Andi Kleenb9917022008-08-18 14:50:31 +0200193 for (i = 0 ; i < num_counters ; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100194 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200195 release_perfctr_nmi(MSR_P6_PERFCTR0 + i);
196 }
Andi Kleenb9917022008-08-18 14:50:31 +0200197 for (i = 0 ; i < num_counters ; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100198 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200199 release_evntsel_nmi(MSR_P6_EVNTSEL0 + i);
200 }
Andi Kleenb9917022008-08-18 14:50:31 +0200201 if (reset_value) {
202 kfree(reset_value);
203 reset_value = NULL;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207
Robert Richter849620f2009-05-14 17:10:52 +0200208struct op_x86_model_spec const op_ppro_spec = {
209 .num_counters = 2,
210 .num_controls = 2,
Robert Richterc92960f2008-09-05 17:12:36 +0200211 .fill_in_addresses = &ppro_fill_in_addresses,
212 .setup_ctrs = &ppro_setup_ctrs,
213 .check_ctrs = &ppro_check_ctrs,
214 .start = &ppro_start,
215 .stop = &ppro_stop,
216 .shutdown = &ppro_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
Andi Kleenb9917022008-08-18 14:50:31 +0200218
219/*
220 * Architectural performance monitoring.
221 *
222 * Newer Intel CPUs (Core1+) have support for architectural
223 * events described in CPUID 0xA. See the IA32 SDM Vol3b.18 for details.
224 * The advantage of this is that it can be done without knowing about
225 * the specific CPU.
226 */
227
Robert Richtere4192942008-10-12 15:12:34 -0400228static void arch_perfmon_setup_counters(void)
Andi Kleenb9917022008-08-18 14:50:31 +0200229{
230 union cpuid10_eax eax;
231
232 eax.full = cpuid_eax(0xa);
233
234 /* Workaround for BIOS bugs in 6/15. Taken from perfmon2 */
235 if (eax.split.version_id == 0 && current_cpu_data.x86 == 6 &&
236 current_cpu_data.x86_model == 15) {
237 eax.split.version_id = 2;
238 eax.split.num_counters = 2;
239 eax.split.bit_width = 40;
240 }
241
242 num_counters = eax.split.num_counters;
243
244 op_arch_perfmon_spec.num_counters = num_counters;
245 op_arch_perfmon_spec.num_controls = num_counters;
246}
247
Robert Richtere4192942008-10-12 15:12:34 -0400248static int arch_perfmon_init(struct oprofile_operations *ignore)
249{
250 arch_perfmon_setup_counters();
251 return 0;
252}
253
Andi Kleenb9917022008-08-18 14:50:31 +0200254struct op_x86_model_spec op_arch_perfmon_spec = {
Robert Richtere4192942008-10-12 15:12:34 -0400255 .init = &arch_perfmon_init,
Andi Kleenb9917022008-08-18 14:50:31 +0200256 /* num_counters/num_controls filled in at runtime */
Robert Richter5a289392008-10-15 22:19:41 +0200257 .fill_in_addresses = &ppro_fill_in_addresses,
Andi Kleenb9917022008-08-18 14:50:31 +0200258 /* user space does the cpuid check for available events */
Robert Richter5a289392008-10-15 22:19:41 +0200259 .setup_ctrs = &ppro_setup_ctrs,
260 .check_ctrs = &ppro_check_ctrs,
261 .start = &ppro_start,
262 .stop = &ppro_stop,
263 .shutdown = &ppro_shutdown
Andi Kleenb9917022008-08-18 14:50:31 +0200264};