blob: 61ee8f640535a9d37261bee7e31d9b4282eca79d [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
54static void ppro_setup_ctrs(struct op_msrs const * const msrs)
55{
56 unsigned int low, high;
57 int i;
58
Andi Kleenb9917022008-08-18 14:50:31 +020059 if (!reset_value) {
Eric Dumazeta4a16be2008-11-10 09:05:37 +010060 reset_value = kmalloc(sizeof(reset_value[0]) * num_counters,
Andi Kleenb9917022008-08-18 14:50:31 +020061 GFP_ATOMIC);
62 if (!reset_value)
63 return;
64 }
65
66 if (cpu_has_arch_perfmon) {
67 union cpuid10_eax eax;
68 eax.full = cpuid_eax(0xa);
Tim Blechmann780eef92009-02-19 17:34:03 +010069
70 /*
71 * For Core2 (family 6, model 15), don't reset the
72 * counter width:
73 */
74 if (!(eax.split.version_id == 0 &&
75 current_cpu_data.x86 == 6 &&
76 current_cpu_data.x86_model == 15)) {
77
78 if (counter_width < eax.split.bit_width)
79 counter_width = eax.split.bit_width;
80 }
Andi Kleenb9917022008-08-18 14:50:31 +020081 }
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* clear all counters */
Andi Kleenb9917022008-08-18 14:50:31 +020084 for (i = 0 ; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010085 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020086 continue;
Robert Richter74c9a5c2009-05-22 19:47:38 +020087 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 CTRL_CLEAR(low);
Robert Richter74c9a5c2009-05-22 19:47:38 +020089 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /* avoid a false detection of ctr overflows in NMI handler */
Andi Kleenb9917022008-08-18 14:50:31 +020093 for (i = 0; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010094 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020095 continue;
Andi Kleenb9917022008-08-18 14:50:31 +020096 wrmsrl(msrs->counters[i].addr, -1LL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
99 /* enable active counters */
Andi Kleenb9917022008-08-18 14:50:31 +0200100 for (i = 0; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100101 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 reset_value[i] = counter_config[i].count;
103
Andi Kleenb9917022008-08-18 14:50:31 +0200104 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Robert Richter74c9a5c2009-05-22 19:47:38 +0200106 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 CTRL_CLEAR(low);
108 CTRL_SET_ENABLE(low);
109 CTRL_SET_USR(low, counter_config[i].user);
110 CTRL_SET_KERN(low, counter_config[i].kernel);
111 CTRL_SET_UM(low, counter_config[i].unit_mask);
112 CTRL_SET_EVENT(low, counter_config[i].event);
Robert Richter74c9a5c2009-05-22 19:47:38 +0200113 wrmsr(msrs->controls[i].addr, low, high);
Don Zickuscb9c4482006-09-26 10:52:26 +0200114 } else {
115 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 }
118}
119
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static int ppro_check_ctrs(struct pt_regs * const regs,
122 struct op_msrs const * const msrs)
123{
Andi Kleen7c64ade2008-11-07 14:02:49 +0100124 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int i;
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100126
Andi Kleenb9917022008-08-18 14:50:31 +0200127 for (i = 0 ; i < num_counters; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200128 if (!reset_value[i])
129 continue;
Andi Kleen7c64ade2008-11-07 14:02:49 +0100130 rdmsrl(msrs->counters[i].addr, val);
131 if (CTR_OVERFLOWED(val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 oprofile_add_sample(regs, i);
Andi Kleenb9917022008-08-18 14:50:31 +0200133 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 }
136
137 /* Only P6 based Pentium M need to re-unmask the apic vector but it
138 * doesn't hurt other P6 variant */
139 apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
140
141 /* We can't work out if we really handled an interrupt. We
142 * might have caught a *second* counter just after overflowing
143 * the interrupt for this counter then arrives
144 * and we don't find a counter that's overflowed, so we
145 * would return 0 and get dazed + confused. Instead we always
146 * assume we found an overflow. This sucks.
147 */
148 return 1;
149}
150
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static void ppro_start(struct op_msrs const * const msrs)
153{
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100154 unsigned int low, high;
Arun Sharma6b77df02006-09-29 02:00:01 -0700155 int i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200156
Eric Dumazet9ea84ad2008-12-02 07:21:21 +0100157 if (!reset_value)
158 return;
Andi Kleenb9917022008-08-18 14:50:31 +0200159 for (i = 0; i < num_counters; ++i) {
Arun Sharma6b77df02006-09-29 02:00:01 -0700160 if (reset_value[i]) {
Robert Richter74c9a5c2009-05-22 19:47:38 +0200161 rdmsr(msrs->controls[i].addr, low, high);
Arun Sharma6b77df02006-09-29 02:00:01 -0700162 CTRL_SET_ACTIVE(low);
Robert Richter74c9a5c2009-05-22 19:47:38 +0200163 wrmsr(msrs->controls[i].addr, low, high);
Arun Sharma6b77df02006-09-29 02:00:01 -0700164 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168
169static void ppro_stop(struct op_msrs const * const msrs)
170{
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100171 unsigned int low, high;
Arun Sharma6b77df02006-09-29 02:00:01 -0700172 int i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200173
Eric Dumazet9ea84ad2008-12-02 07:21:21 +0100174 if (!reset_value)
175 return;
Andi Kleenb9917022008-08-18 14:50:31 +0200176 for (i = 0; i < num_counters; ++i) {
Arun Sharma6b77df02006-09-29 02:00:01 -0700177 if (!reset_value[i])
178 continue;
Robert Richter74c9a5c2009-05-22 19:47:38 +0200179 rdmsr(msrs->controls[i].addr, low, high);
Don Zickuscb9c4482006-09-26 10:52:26 +0200180 CTRL_SET_INACTIVE(low);
Robert Richter74c9a5c2009-05-22 19:47:38 +0200181 wrmsr(msrs->controls[i].addr, low, high);
Don Zickuscb9c4482006-09-26 10:52:26 +0200182 }
183}
184
185static void ppro_shutdown(struct op_msrs const * const msrs)
186{
187 int i;
188
Andi Kleenb9917022008-08-18 14:50:31 +0200189 for (i = 0 ; i < num_counters ; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100190 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200191 release_perfctr_nmi(MSR_P6_PERFCTR0 + 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 (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200195 release_evntsel_nmi(MSR_P6_EVNTSEL0 + i);
196 }
Andi Kleenb9917022008-08-18 14:50:31 +0200197 if (reset_value) {
198 kfree(reset_value);
199 reset_value = NULL;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203
Robert Richter849620f2009-05-14 17:10:52 +0200204struct op_x86_model_spec const op_ppro_spec = {
205 .num_counters = 2,
206 .num_controls = 2,
Robert Richterc92960f2008-09-05 17:12:36 +0200207 .fill_in_addresses = &ppro_fill_in_addresses,
208 .setup_ctrs = &ppro_setup_ctrs,
209 .check_ctrs = &ppro_check_ctrs,
210 .start = &ppro_start,
211 .stop = &ppro_stop,
212 .shutdown = &ppro_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213};
Andi Kleenb9917022008-08-18 14:50:31 +0200214
215/*
216 * Architectural performance monitoring.
217 *
218 * Newer Intel CPUs (Core1+) have support for architectural
219 * events described in CPUID 0xA. See the IA32 SDM Vol3b.18 for details.
220 * The advantage of this is that it can be done without knowing about
221 * the specific CPU.
222 */
223
Robert Richtere4192942008-10-12 15:12:34 -0400224static void arch_perfmon_setup_counters(void)
Andi Kleenb9917022008-08-18 14:50:31 +0200225{
226 union cpuid10_eax eax;
227
228 eax.full = cpuid_eax(0xa);
229
230 /* Workaround for BIOS bugs in 6/15. Taken from perfmon2 */
231 if (eax.split.version_id == 0 && current_cpu_data.x86 == 6 &&
232 current_cpu_data.x86_model == 15) {
233 eax.split.version_id = 2;
234 eax.split.num_counters = 2;
235 eax.split.bit_width = 40;
236 }
237
238 num_counters = eax.split.num_counters;
239
240 op_arch_perfmon_spec.num_counters = num_counters;
241 op_arch_perfmon_spec.num_controls = num_counters;
242}
243
Robert Richtere4192942008-10-12 15:12:34 -0400244static int arch_perfmon_init(struct oprofile_operations *ignore)
245{
246 arch_perfmon_setup_counters();
247 return 0;
248}
249
Andi Kleenb9917022008-08-18 14:50:31 +0200250struct op_x86_model_spec op_arch_perfmon_spec = {
Robert Richtere4192942008-10-12 15:12:34 -0400251 .init = &arch_perfmon_init,
Andi Kleenb9917022008-08-18 14:50:31 +0200252 /* num_counters/num_controls filled in at runtime */
Robert Richter5a289392008-10-15 22:19:41 +0200253 .fill_in_addresses = &ppro_fill_in_addresses,
Andi Kleenb9917022008-08-18 14:50:31 +0200254 /* user space does the cpuid check for available events */
Robert Richter5a289392008-10-15 22:19:41 +0200255 .setup_ctrs = &ppro_setup_ctrs,
256 .check_ctrs = &ppro_check_ctrs,
257 .start = &ppro_start,
258 .stop = &ppro_stop,
259 .shutdown = &ppro_shutdown
Andi Kleenb9917022008-08-18 14:50:31 +0200260};