blob: 40b44ee521d5423f042d9bc61ba129c14690dd05 [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>
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "op_x86_model.h"
23#include "op_counter.h"
24
Andi Kleenb9917022008-08-18 14:50:31 +020025static int num_counters = 2;
26static int counter_width = 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Andi Kleen7c64ade2008-11-07 14:02:49 +010028#define CTR_OVERFLOWED(n) (!((n) & (1ULL<<(counter_width-1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define CTRL_CLEAR(x) (x &= (1<<21))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define CTRL_SET_EVENT(val, e) (val |= e)
31
Andi Kleenb9917022008-08-18 14:50:31 +020032static u64 *reset_value;
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static void ppro_fill_in_addresses(struct op_msrs * const msrs)
35{
Don Zickuscb9c4482006-09-26 10:52:26 +020036 int i;
37
Andi Kleenb9917022008-08-18 14:50:31 +020038 for (i = 0; i < num_counters; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +020039 if (reserve_perfctr_nmi(MSR_P6_PERFCTR0 + i))
40 msrs->counters[i].addr = MSR_P6_PERFCTR0 + i;
41 else
42 msrs->counters[i].addr = 0;
43 }
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010044
Andi Kleenb9917022008-08-18 14:50:31 +020045 for (i = 0; i < num_counters; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +020046 if (reserve_evntsel_nmi(MSR_P6_EVNTSEL0 + i))
47 msrs->controls[i].addr = MSR_P6_EVNTSEL0 + i;
48 else
49 msrs->controls[i].addr = 0;
50 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
53
Robert Richteref8828d2009-05-25 19:31:44 +020054static void ppro_setup_ctrs(struct op_x86_model_spec const *model,
55 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 unsigned int low, high;
58 int i;
59
Andi Kleenb9917022008-08-18 14:50:31 +020060 if (!reset_value) {
Eric Dumazeta4a16be2008-11-10 09:05:37 +010061 reset_value = kmalloc(sizeof(reset_value[0]) * num_counters,
Andi Kleenb9917022008-08-18 14:50:31 +020062 GFP_ATOMIC);
63 if (!reset_value)
64 return;
65 }
66
67 if (cpu_has_arch_perfmon) {
68 union cpuid10_eax eax;
69 eax.full = cpuid_eax(0xa);
Tim Blechmann780eef92009-02-19 17:34:03 +010070
71 /*
72 * For Core2 (family 6, model 15), don't reset the
73 * counter width:
74 */
75 if (!(eax.split.version_id == 0 &&
76 current_cpu_data.x86 == 6 &&
77 current_cpu_data.x86_model == 15)) {
78
79 if (counter_width < eax.split.bit_width)
80 counter_width = eax.split.bit_width;
81 }
Andi Kleenb9917022008-08-18 14:50:31 +020082 }
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* clear all counters */
Andi Kleenb9917022008-08-18 14:50:31 +020085 for (i = 0 ; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010086 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020087 continue;
Robert Richter74c9a5c2009-05-22 19:47:38 +020088 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 CTRL_CLEAR(low);
Robert Richter74c9a5c2009-05-22 19:47:38 +020090 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* avoid a false detection of ctr overflows in NMI handler */
Andi Kleenb9917022008-08-18 14:50:31 +020094 for (i = 0; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010095 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020096 continue;
Andi Kleenb9917022008-08-18 14:50:31 +020097 wrmsrl(msrs->counters[i].addr, -1LL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
100 /* enable active counters */
Andi Kleenb9917022008-08-18 14:50:31 +0200101 for (i = 0; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100102 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 reset_value[i] = counter_config[i].count;
104
Andi Kleenb9917022008-08-18 14:50:31 +0200105 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Robert Richter74c9a5c2009-05-22 19:47:38 +0200107 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 CTRL_CLEAR(low);
109 CTRL_SET_ENABLE(low);
110 CTRL_SET_USR(low, counter_config[i].user);
111 CTRL_SET_KERN(low, counter_config[i].kernel);
112 CTRL_SET_UM(low, counter_config[i].unit_mask);
113 CTRL_SET_EVENT(low, counter_config[i].event);
Robert Richter74c9a5c2009-05-22 19:47:38 +0200114 wrmsr(msrs->controls[i].addr, low, high);
Don Zickuscb9c4482006-09-26 10:52:26 +0200115 } else {
116 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118 }
119}
120
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122static int ppro_check_ctrs(struct pt_regs * const regs,
123 struct op_msrs const * const msrs)
124{
Andi Kleen7c64ade2008-11-07 14:02:49 +0100125 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int i;
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100127
Andi Kleenb9917022008-08-18 14:50:31 +0200128 for (i = 0 ; i < num_counters; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200129 if (!reset_value[i])
130 continue;
Andi Kleen7c64ade2008-11-07 14:02:49 +0100131 rdmsrl(msrs->counters[i].addr, val);
132 if (CTR_OVERFLOWED(val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 oprofile_add_sample(regs, i);
Andi Kleenb9917022008-08-18 14:50:31 +0200134 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136 }
137
138 /* Only P6 based Pentium M need to re-unmask the apic vector but it
139 * doesn't hurt other P6 variant */
140 apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
141
142 /* We can't work out if we really handled an interrupt. We
143 * might have caught a *second* counter just after overflowing
144 * the interrupt for this counter then arrives
145 * and we don't find a counter that's overflowed, so we
146 * would return 0 and get dazed + confused. Instead we always
147 * assume we found an overflow. This sucks.
148 */
149 return 1;
150}
151
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static void ppro_start(struct op_msrs const * const msrs)
154{
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100155 unsigned int low, high;
Arun Sharma6b77df02006-09-29 02:00:01 -0700156 int i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200157
Eric Dumazet9ea84ad2008-12-02 07:21:21 +0100158 if (!reset_value)
159 return;
Andi Kleenb9917022008-08-18 14:50:31 +0200160 for (i = 0; i < num_counters; ++i) {
Arun Sharma6b77df02006-09-29 02:00:01 -0700161 if (reset_value[i]) {
Robert Richter74c9a5c2009-05-22 19:47:38 +0200162 rdmsr(msrs->controls[i].addr, low, high);
Arun Sharma6b77df02006-09-29 02:00:01 -0700163 CTRL_SET_ACTIVE(low);
Robert Richter74c9a5c2009-05-22 19:47:38 +0200164 wrmsr(msrs->controls[i].addr, low, high);
Arun Sharma6b77df02006-09-29 02:00:01 -0700165 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169
170static void ppro_stop(struct op_msrs const * const msrs)
171{
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100172 unsigned int low, high;
Arun Sharma6b77df02006-09-29 02:00:01 -0700173 int i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200174
Eric Dumazet9ea84ad2008-12-02 07:21:21 +0100175 if (!reset_value)
176 return;
Andi Kleenb9917022008-08-18 14:50:31 +0200177 for (i = 0; i < num_counters; ++i) {
Arun Sharma6b77df02006-09-29 02:00:01 -0700178 if (!reset_value[i])
179 continue;
Robert Richter74c9a5c2009-05-22 19:47:38 +0200180 rdmsr(msrs->controls[i].addr, low, high);
Don Zickuscb9c4482006-09-26 10:52:26 +0200181 CTRL_SET_INACTIVE(low);
Robert Richter74c9a5c2009-05-22 19:47:38 +0200182 wrmsr(msrs->controls[i].addr, low, high);
Don Zickuscb9c4482006-09-26 10:52:26 +0200183 }
184}
185
186static void ppro_shutdown(struct op_msrs const * const msrs)
187{
188 int i;
189
Andi Kleenb9917022008-08-18 14:50:31 +0200190 for (i = 0 ; i < num_counters ; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100191 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200192 release_perfctr_nmi(MSR_P6_PERFCTR0 + i);
193 }
Andi Kleenb9917022008-08-18 14:50:31 +0200194 for (i = 0 ; i < num_counters ; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100195 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200196 release_evntsel_nmi(MSR_P6_EVNTSEL0 + i);
197 }
Andi Kleenb9917022008-08-18 14:50:31 +0200198 if (reset_value) {
199 kfree(reset_value);
200 reset_value = NULL;
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204
Robert Richter849620f2009-05-14 17:10:52 +0200205struct op_x86_model_spec const op_ppro_spec = {
206 .num_counters = 2,
207 .num_controls = 2,
Robert Richterc92960f2008-09-05 17:12:36 +0200208 .fill_in_addresses = &ppro_fill_in_addresses,
209 .setup_ctrs = &ppro_setup_ctrs,
210 .check_ctrs = &ppro_check_ctrs,
211 .start = &ppro_start,
212 .stop = &ppro_stop,
213 .shutdown = &ppro_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
Andi Kleenb9917022008-08-18 14:50:31 +0200215
216/*
217 * Architectural performance monitoring.
218 *
219 * Newer Intel CPUs (Core1+) have support for architectural
220 * events described in CPUID 0xA. See the IA32 SDM Vol3b.18 for details.
221 * The advantage of this is that it can be done without knowing about
222 * the specific CPU.
223 */
224
Robert Richtere4192942008-10-12 15:12:34 -0400225static void arch_perfmon_setup_counters(void)
Andi Kleenb9917022008-08-18 14:50:31 +0200226{
227 union cpuid10_eax eax;
228
229 eax.full = cpuid_eax(0xa);
230
231 /* Workaround for BIOS bugs in 6/15. Taken from perfmon2 */
232 if (eax.split.version_id == 0 && current_cpu_data.x86 == 6 &&
233 current_cpu_data.x86_model == 15) {
234 eax.split.version_id = 2;
235 eax.split.num_counters = 2;
236 eax.split.bit_width = 40;
237 }
238
239 num_counters = eax.split.num_counters;
240
241 op_arch_perfmon_spec.num_counters = num_counters;
242 op_arch_perfmon_spec.num_controls = num_counters;
243}
244
Robert Richtere4192942008-10-12 15:12:34 -0400245static int arch_perfmon_init(struct oprofile_operations *ignore)
246{
247 arch_perfmon_setup_counters();
248 return 0;
249}
250
Andi Kleenb9917022008-08-18 14:50:31 +0200251struct op_x86_model_spec op_arch_perfmon_spec = {
Robert Richtere4192942008-10-12 15:12:34 -0400252 .init = &arch_perfmon_init,
Andi Kleenb9917022008-08-18 14:50:31 +0200253 /* num_counters/num_controls filled in at runtime */
Robert Richter5a289392008-10-15 22:19:41 +0200254 .fill_in_addresses = &ppro_fill_in_addresses,
Andi Kleenb9917022008-08-18 14:50:31 +0200255 /* user space does the cpuid check for available events */
Robert Richter5a289392008-10-15 22:19:41 +0200256 .setup_ctrs = &ppro_setup_ctrs,
257 .check_ctrs = &ppro_check_ctrs,
258 .start = &ppro_start,
259 .stop = &ppro_stop,
260 .shutdown = &ppro_shutdown
Andi Kleenb9917022008-08-18 14:50:31 +0200261};