aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/oprofile/op_model_amd.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/oprofile/op_model_amd.c')
-rw-r--r--arch/x86/oprofile/op_model_amd.c76
1 files changed, 30 insertions, 46 deletions
diff --git a/arch/x86/oprofile/op_model_amd.c b/arch/x86/oprofile/op_model_amd.c
index bbf2b68bcc5d..d9faf607b3a6 100644
--- a/arch/x86/oprofile/op_model_amd.c
+++ b/arch/x86/oprofile/op_model_amd.c
@@ -15,7 +15,6 @@
15#include <linux/oprofile.h> 15#include <linux/oprofile.h>
16#include <linux/device.h> 16#include <linux/device.h>
17#include <linux/pci.h> 17#include <linux/pci.h>
18#include <linux/percpu.h>
19 18
20#include <asm/ptrace.h> 19#include <asm/ptrace.h>
21#include <asm/msr.h> 20#include <asm/msr.h>
@@ -24,10 +23,8 @@
24#include "op_x86_model.h" 23#include "op_x86_model.h"
25#include "op_counter.h" 24#include "op_counter.h"
26 25
27#define NUM_COUNTERS 32 26#define NUM_COUNTERS 4
28#define NUM_HARDWARE_COUNTERS 4 27#define NUM_CONTROLS 4
29#define NUM_CONTROLS 32
30#define NUM_HARDWARE_CONTROLS 4
31 28
32#define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0) 29#define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
33#define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0) 30#define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
@@ -51,7 +48,6 @@
51#define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8)) 48#define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
52 49
53static unsigned long reset_value[NUM_COUNTERS]; 50static unsigned long reset_value[NUM_COUNTERS];
54DECLARE_PER_CPU(int, switch_index);
55 51
56#ifdef CONFIG_OPROFILE_IBS 52#ifdef CONFIG_OPROFILE_IBS
57 53
@@ -134,17 +130,15 @@ static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
134 int i; 130 int i;
135 131
136 for (i = 0; i < NUM_COUNTERS; i++) { 132 for (i = 0; i < NUM_COUNTERS; i++) {
137 int hw_counter = i % NUM_HARDWARE_COUNTERS; 133 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
138 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + hw_counter)) 134 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
139 msrs->counters[i].addr = MSR_K7_PERFCTR0 + hw_counter;
140 else 135 else
141 msrs->counters[i].addr = 0; 136 msrs->counters[i].addr = 0;
142 } 137 }
143 138
144 for (i = 0; i < NUM_CONTROLS; i++) { 139 for (i = 0; i < NUM_CONTROLS; i++) {
145 int hw_control = i % NUM_HARDWARE_CONTROLS; 140 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
146 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + hw_control)) 141 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
147 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + hw_control;
148 else 142 else
149 msrs->controls[i].addr = 0; 143 msrs->controls[i].addr = 0;
150 } 144 }
@@ -156,16 +150,8 @@ static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
156 unsigned int low, high; 150 unsigned int low, high;
157 int i; 151 int i;
158 152
159 for (i = 0; i < NUM_HARDWARE_CONTROLS; ++i) {
160 int offset = i + __get_cpu_var(switch_index);
161 if (counter_config[offset].enabled)
162 reset_value[offset] = counter_config[offset].count;
163 else
164 reset_value[offset] = 0;
165 }
166
167 /* clear all counters */ 153 /* clear all counters */
168 for (i = 0 ; i < NUM_HARDWARE_CONTROLS; ++i) { 154 for (i = 0 ; i < NUM_CONTROLS; ++i) {
169 if (unlikely(!CTRL_IS_RESERVED(msrs, i))) 155 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
170 continue; 156 continue;
171 CTRL_READ(low, high, msrs, i); 157 CTRL_READ(low, high, msrs, i);
@@ -175,31 +161,34 @@ static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
175 } 161 }
176 162
177 /* avoid a false detection of ctr overflows in NMI handler */ 163 /* avoid a false detection of ctr overflows in NMI handler */
178 for (i = 0; i < NUM_HARDWARE_COUNTERS; ++i) { 164 for (i = 0; i < NUM_COUNTERS; ++i) {
179 if (unlikely(!CTR_IS_RESERVED(msrs, i))) 165 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
180 continue; 166 continue;
181 CTR_WRITE(1, msrs, i); 167 CTR_WRITE(1, msrs, i);
182 } 168 }
183 169
184 /* enable active counters */ 170 /* enable active counters */
185 for (i = 0; i < NUM_HARDWARE_COUNTERS; ++i) { 171 for (i = 0; i < NUM_COUNTERS; ++i) {
186 int offset = i + __get_cpu_var(switch_index); 172 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
187 if ((counter_config[offset].enabled) && (CTR_IS_RESERVED(msrs, i))) { 173 reset_value[i] = counter_config[i].count;
188 CTR_WRITE(counter_config[offset].count, msrs, i); 174
175 CTR_WRITE(counter_config[i].count, msrs, i);
189 176
190 CTRL_READ(low, high, msrs, i); 177 CTRL_READ(low, high, msrs, i);
191 CTRL_CLEAR_LO(low); 178 CTRL_CLEAR_LO(low);
192 CTRL_CLEAR_HI(high); 179 CTRL_CLEAR_HI(high);
193 CTRL_SET_ENABLE(low); 180 CTRL_SET_ENABLE(low);
194 CTRL_SET_USR(low, counter_config[offset].user); 181 CTRL_SET_USR(low, counter_config[i].user);
195 CTRL_SET_KERN(low, counter_config[offset].kernel); 182 CTRL_SET_KERN(low, counter_config[i].kernel);
196 CTRL_SET_UM(low, counter_config[offset].unit_mask); 183 CTRL_SET_UM(low, counter_config[i].unit_mask);
197 CTRL_SET_EVENT_LOW(low, counter_config[offset].event); 184 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
198 CTRL_SET_EVENT_HIGH(high, counter_config[offset].event); 185 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
199 CTRL_SET_HOST_ONLY(high, 0); 186 CTRL_SET_HOST_ONLY(high, 0);
200 CTRL_SET_GUEST_ONLY(high, 0); 187 CTRL_SET_GUEST_ONLY(high, 0);
201 188
202 CTRL_WRITE(low, high, msrs, i); 189 CTRL_WRITE(low, high, msrs, i);
190 } else {
191 reset_value[i] = 0;
203 } 192 }
204 } 193 }
205} 194}
@@ -287,14 +276,13 @@ static int op_amd_check_ctrs(struct pt_regs * const regs,
287 unsigned int low, high; 276 unsigned int low, high;
288 int i; 277 int i;
289 278
290 for (i = 0 ; i < NUM_HARDWARE_COUNTERS ; ++i) { 279 for (i = 0 ; i < NUM_COUNTERS; ++i) {
291 int offset = i + __get_cpu_var(switch_index); 280 if (!reset_value[i])
292 if (!reset_value[offset])
293 continue; 281 continue;
294 CTR_READ(low, high, msrs, i); 282 CTR_READ(low, high, msrs, i);
295 if (CTR_OVERFLOWED(low)) { 283 if (CTR_OVERFLOWED(low)) {
296 oprofile_add_sample(regs, offset); 284 oprofile_add_sample(regs, i);
297 CTR_WRITE(reset_value[offset], msrs, i); 285 CTR_WRITE(reset_value[i], msrs, i);
298 } 286 }
299 } 287 }
300 288
@@ -310,10 +298,8 @@ static void op_amd_start(struct op_msrs const * const msrs)
310{ 298{
311 unsigned int low, high; 299 unsigned int low, high;
312 int i; 300 int i;
313 301 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
314 for (i = 0 ; i < NUM_HARDWARE_COUNTERS ; ++i) { 302 if (reset_value[i]) {
315 int offset = i + __get_cpu_var(switch_index);
316 if (reset_value[offset]) {
317 CTRL_READ(low, high, msrs, i); 303 CTRL_READ(low, high, msrs, i);
318 CTRL_SET_ACTIVE(low); 304 CTRL_SET_ACTIVE(low);
319 CTRL_WRITE(low, high, msrs, i); 305 CTRL_WRITE(low, high, msrs, i);
@@ -343,8 +329,8 @@ static void op_amd_stop(struct op_msrs const * const msrs)
343 329
344 /* Subtle: stop on all counters to avoid race with 330 /* Subtle: stop on all counters to avoid race with
345 * setting our pm callback */ 331 * setting our pm callback */
346 for (i = 0 ; i < NUM_HARDWARE_COUNTERS ; ++i) { 332 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
347 if (!reset_value[i + per_cpu(switch_index, smp_processor_id())]) 333 if (!reset_value[i])
348 continue; 334 continue;
349 CTRL_READ(low, high, msrs, i); 335 CTRL_READ(low, high, msrs, i);
350 CTRL_SET_INACTIVE(low); 336 CTRL_SET_INACTIVE(low);
@@ -370,11 +356,11 @@ static void op_amd_shutdown(struct op_msrs const * const msrs)
370{ 356{
371 int i; 357 int i;
372 358
373 for (i = 0 ; i < NUM_HARDWARE_COUNTERS ; ++i) { 359 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
374 if (CTR_IS_RESERVED(msrs, i)) 360 if (CTR_IS_RESERVED(msrs, i))
375 release_perfctr_nmi(MSR_K7_PERFCTR0 + i); 361 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
376 } 362 }
377 for (i = 0 ; i < NUM_HARDWARE_COUNTERS ; ++i) { 363 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
378 if (CTRL_IS_RESERVED(msrs, i)) 364 if (CTRL_IS_RESERVED(msrs, i))
379 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i); 365 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
380 } 366 }
@@ -548,8 +534,6 @@ struct op_x86_model_spec const op_amd_spec = {
548 .exit = op_amd_exit, 534 .exit = op_amd_exit,
549 .num_counters = NUM_COUNTERS, 535 .num_counters = NUM_COUNTERS,
550 .num_controls = NUM_CONTROLS, 536 .num_controls = NUM_CONTROLS,
551 .num_hardware_counters = NUM_HARDWARE_COUNTERS,
552 .num_hardware_controls = NUM_HARDWARE_CONTROLS,
553 .fill_in_addresses = &op_amd_fill_in_addresses, 537 .fill_in_addresses = &op_amd_fill_in_addresses,
554 .setup_ctrs = &op_amd_setup_ctrs, 538 .setup_ctrs = &op_amd_setup_ctrs,
555 .check_ctrs = &op_amd_check_ctrs, 539 .check_ctrs = &op_amd_check_ctrs,