blob: 85eb6268374f561ac187170fafbee15d7a618cdd [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>
Ingo Molnar5c167b82008-12-17 09:02:19 +010021#include <asm/perf_counter.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
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010029#define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
Andi Kleen7c64ade2008-11-07 14:02:49 +010030#define CTR_OVERFLOWED(n) (!((n) & (1ULL<<(counter_width-1))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010032#define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
33#define CTRL_READ(l, h, msrs, c) do {rdmsr((msrs->controls[(c)].addr), (l), (h)); } while (0)
34#define CTRL_WRITE(l, h, msrs, c) do {wrmsr((msrs->controls[(c)].addr), (l), (h)); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define CTRL_SET_ACTIVE(n) (n |= (1<<22))
36#define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
37#define CTRL_CLEAR(x) (x &= (1<<21))
38#define CTRL_SET_ENABLE(val) (val |= 1<<20)
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010039#define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
40#define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define CTRL_SET_UM(val, m) (val |= (m << 8))
42#define CTRL_SET_EVENT(val, e) (val |= e)
43
Andi Kleenb9917022008-08-18 14:50:31 +020044static u64 *reset_value;
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static void ppro_fill_in_addresses(struct op_msrs * const msrs)
47{
Don Zickuscb9c4482006-09-26 10:52:26 +020048 int i;
49
Andi Kleenb9917022008-08-18 14:50:31 +020050 for (i = 0; i < num_counters; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +020051 if (reserve_perfctr_nmi(MSR_P6_PERFCTR0 + i))
52 msrs->counters[i].addr = MSR_P6_PERFCTR0 + i;
53 else
54 msrs->counters[i].addr = 0;
55 }
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010056
Andi Kleenb9917022008-08-18 14:50:31 +020057 for (i = 0; i < num_counters; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +020058 if (reserve_evntsel_nmi(MSR_P6_EVNTSEL0 + i))
59 msrs->controls[i].addr = MSR_P6_EVNTSEL0 + i;
60 else
61 msrs->controls[i].addr = 0;
62 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65
66static void ppro_setup_ctrs(struct op_msrs const * const msrs)
67{
68 unsigned int low, high;
69 int i;
70
Andi Kleenb9917022008-08-18 14:50:31 +020071 if (!reset_value) {
Eric Dumazeta4a16be2008-11-10 09:05:37 +010072 reset_value = kmalloc(sizeof(reset_value[0]) * num_counters,
Andi Kleenb9917022008-08-18 14:50:31 +020073 GFP_ATOMIC);
74 if (!reset_value)
75 return;
76 }
77
78 if (cpu_has_arch_perfmon) {
79 union cpuid10_eax eax;
80 eax.full = cpuid_eax(0xa);
81 if (counter_width < eax.split.bit_width)
82 counter_width = eax.split.bit_width;
83 }
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* clear all counters */
Andi Kleenb9917022008-08-18 14:50:31 +020086 for (i = 0 ; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010087 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020088 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 CTRL_READ(low, high, msrs, i);
90 CTRL_CLEAR(low);
91 CTRL_WRITE(low, high, msrs, i);
92 }
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* avoid a false detection of ctr overflows in NMI handler */
Andi Kleenb9917022008-08-18 14:50:31 +020095 for (i = 0; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +010096 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020097 continue;
Andi Kleenb9917022008-08-18 14:50:31 +020098 wrmsrl(msrs->counters[i].addr, -1LL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
101 /* enable active counters */
Andi Kleenb9917022008-08-18 14:50:31 +0200102 for (i = 0; i < num_counters; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100103 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 reset_value[i] = counter_config[i].count;
105
Andi Kleenb9917022008-08-18 14:50:31 +0200106 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 CTRL_READ(low, high, msrs, i);
109 CTRL_CLEAR(low);
110 CTRL_SET_ENABLE(low);
111 CTRL_SET_USR(low, counter_config[i].user);
112 CTRL_SET_KERN(low, counter_config[i].kernel);
113 CTRL_SET_UM(low, counter_config[i].unit_mask);
114 CTRL_SET_EVENT(low, counter_config[i].event);
115 CTRL_WRITE(low, high, msrs, i);
Don Zickuscb9c4482006-09-26 10:52:26 +0200116 } else {
117 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 }
120}
121
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int ppro_check_ctrs(struct pt_regs * const regs,
124 struct op_msrs const * const msrs)
125{
Andi Kleen7c64ade2008-11-07 14:02:49 +0100126 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int i;
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100128
Ingo Molnar82aa9a12009-02-05 15:23:08 +0100129 /*
130 * This can happen if perf counters are in use when
131 * we steal the die notifier NMI.
132 */
133 if (unlikely(!reset_value))
134 goto out;
135
Andi Kleenb9917022008-08-18 14:50:31 +0200136 for (i = 0 ; i < num_counters; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200137 if (!reset_value[i])
138 continue;
Andi Kleen7c64ade2008-11-07 14:02:49 +0100139 rdmsrl(msrs->counters[i].addr, val);
140 if (CTR_OVERFLOWED(val)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 oprofile_add_sample(regs, i);
Andi Kleenb9917022008-08-18 14:50:31 +0200142 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 }
145
Ingo Molnar82aa9a12009-02-05 15:23:08 +0100146out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* Only P6 based Pentium M need to re-unmask the apic vector but it
148 * doesn't hurt other P6 variant */
149 apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
150
151 /* We can't work out if we really handled an interrupt. We
152 * might have caught a *second* counter just after overflowing
153 * the interrupt for this counter then arrives
154 * and we don't find a counter that's overflowed, so we
155 * would return 0 and get dazed + confused. Instead we always
156 * assume we found an overflow. This sucks.
157 */
158 return 1;
159}
160
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static void ppro_start(struct op_msrs const * const msrs)
163{
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100164 unsigned int low, high;
Arun Sharma6b77df02006-09-29 02:00:01 -0700165 int i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200166
Eric Dumazet9ea84ad2008-12-02 07:21:21 +0100167 if (!reset_value)
168 return;
Andi Kleenb9917022008-08-18 14:50:31 +0200169 for (i = 0; i < num_counters; ++i) {
Arun Sharma6b77df02006-09-29 02:00:01 -0700170 if (reset_value[i]) {
171 CTRL_READ(low, high, msrs, i);
172 CTRL_SET_ACTIVE(low);
173 CTRL_WRITE(low, high, msrs, i);
174 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178
179static void ppro_stop(struct op_msrs const * const msrs)
180{
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100181 unsigned int low, high;
Arun Sharma6b77df02006-09-29 02:00:01 -0700182 int i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200183
Eric Dumazet9ea84ad2008-12-02 07:21:21 +0100184 if (!reset_value)
185 return;
Andi Kleenb9917022008-08-18 14:50:31 +0200186 for (i = 0; i < num_counters; ++i) {
Arun Sharma6b77df02006-09-29 02:00:01 -0700187 if (!reset_value[i])
188 continue;
189 CTRL_READ(low, high, msrs, i);
Don Zickuscb9c4482006-09-26 10:52:26 +0200190 CTRL_SET_INACTIVE(low);
Arun Sharma6b77df02006-09-29 02:00:01 -0700191 CTRL_WRITE(low, high, msrs, i);
Don Zickuscb9c4482006-09-26 10:52:26 +0200192 }
193}
194
195static void ppro_shutdown(struct op_msrs const * const msrs)
196{
197 int i;
198
Andi Kleenb9917022008-08-18 14:50:31 +0200199 for (i = 0 ; i < num_counters ; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100200 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200201 release_perfctr_nmi(MSR_P6_PERFCTR0 + i);
202 }
Andi Kleenb9917022008-08-18 14:50:31 +0200203 for (i = 0 ; i < num_counters ; ++i) {
Paolo Ciarrocchi8b45b722008-02-19 23:43:25 +0100204 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200205 release_evntsel_nmi(MSR_P6_EVNTSEL0 + i);
206 }
Andi Kleenb9917022008-08-18 14:50:31 +0200207 if (reset_value) {
208 kfree(reset_value);
209 reset_value = NULL;
210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213
Andi Kleen59512902008-09-29 22:23:33 +0200214struct op_x86_model_spec op_ppro_spec = {
Robert Richter5a289392008-10-15 22:19:41 +0200215 .num_counters = 2, /* can be overriden */
216 .num_controls = 2, /* dito */
Robert Richterc92960f2008-09-05 17:12:36 +0200217 .fill_in_addresses = &ppro_fill_in_addresses,
218 .setup_ctrs = &ppro_setup_ctrs,
219 .check_ctrs = &ppro_check_ctrs,
220 .start = &ppro_start,
221 .stop = &ppro_stop,
222 .shutdown = &ppro_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
Andi Kleenb9917022008-08-18 14:50:31 +0200224
225/*
226 * Architectural performance monitoring.
227 *
228 * Newer Intel CPUs (Core1+) have support for architectural
229 * events described in CPUID 0xA. See the IA32 SDM Vol3b.18 for details.
230 * The advantage of this is that it can be done without knowing about
231 * the specific CPU.
232 */
233
234void arch_perfmon_setup_counters(void)
235{
236 union cpuid10_eax eax;
237
238 eax.full = cpuid_eax(0xa);
239
240 /* Workaround for BIOS bugs in 6/15. Taken from perfmon2 */
241 if (eax.split.version_id == 0 && current_cpu_data.x86 == 6 &&
242 current_cpu_data.x86_model == 15) {
243 eax.split.version_id = 2;
244 eax.split.num_counters = 2;
245 eax.split.bit_width = 40;
246 }
247
248 num_counters = eax.split.num_counters;
249
250 op_arch_perfmon_spec.num_counters = num_counters;
251 op_arch_perfmon_spec.num_controls = num_counters;
Andi Kleen59512902008-09-29 22:23:33 +0200252 op_ppro_spec.num_counters = num_counters;
253 op_ppro_spec.num_controls = num_counters;
Andi Kleenb9917022008-08-18 14:50:31 +0200254}
255
256struct op_x86_model_spec op_arch_perfmon_spec = {
257 /* num_counters/num_controls filled in at runtime */
Robert Richter5a289392008-10-15 22:19:41 +0200258 .fill_in_addresses = &ppro_fill_in_addresses,
Andi Kleenb9917022008-08-18 14:50:31 +0200259 /* user space does the cpuid check for available events */
Robert Richter5a289392008-10-15 22:19:41 +0200260 .setup_ctrs = &ppro_setup_ctrs,
261 .check_ctrs = &ppro_check_ctrs,
262 .start = &ppro_start,
263 .stop = &ppro_stop,
264 .shutdown = &ppro_shutdown
Andi Kleenb9917022008-08-18 14:50:31 +0200265};