S3C: Add a flag to allow leds to start "on"
[linux-2.6/mini2440.git] / arch / x86 / oprofile / op_model_ppro.c
blob4da7230b3d17138f7fcec0cf0b13f265c633f862
1 /*
2 * @file op_model_ppro.h
3 * Family 6 perfmon and architectural perfmon MSR operations
5 * @remark Copyright 2002 OProfile authors
6 * @remark Copyright 2008 Intel Corporation
7 * @remark Read the file COPYING
9 * @author John Levon
10 * @author Philippe Elie
11 * @author Graydon Hoare
12 * @author Andi Kleen
15 #include <linux/oprofile.h>
16 #include <linux/slab.h>
17 #include <asm/ptrace.h>
18 #include <asm/msr.h>
19 #include <asm/apic.h>
20 #include <asm/nmi.h>
21 #include <asm/perf_counter.h>
23 #include "op_x86_model.h"
24 #include "op_counter.h"
26 static int num_counters = 2;
27 static int counter_width = 32;
29 #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
30 #define CTR_OVERFLOWED(n) (!((n) & (1ULL<<(counter_width-1))))
32 #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)
35 #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)
39 #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
40 #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
41 #define CTRL_SET_UM(val, m) (val |= (m << 8))
42 #define CTRL_SET_EVENT(val, e) (val |= e)
44 static u64 *reset_value;
46 static void ppro_fill_in_addresses(struct op_msrs * const msrs)
48 int i;
50 for (i = 0; i < num_counters; i++) {
51 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;
57 for (i = 0; i < num_counters; i++) {
58 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;
66 static void ppro_setup_ctrs(struct op_msrs const * const msrs)
68 unsigned int low, high;
69 int i;
71 if (!reset_value) {
72 reset_value = kmalloc(sizeof(reset_value[0]) * num_counters,
73 GFP_ATOMIC);
74 if (!reset_value)
75 return;
78 if (cpu_has_arch_perfmon) {
79 union cpuid10_eax eax;
80 eax.full = cpuid_eax(0xa);
83 * For Core2 (family 6, model 15), don't reset the
84 * counter width:
86 if (!(eax.split.version_id == 0 &&
87 current_cpu_data.x86 == 6 &&
88 current_cpu_data.x86_model == 15)) {
90 if (counter_width < eax.split.bit_width)
91 counter_width = eax.split.bit_width;
95 /* clear all counters */
96 for (i = 0 ; i < num_counters; ++i) {
97 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
98 continue;
99 CTRL_READ(low, high, msrs, i);
100 CTRL_CLEAR(low);
101 CTRL_WRITE(low, high, msrs, i);
104 /* avoid a false detection of ctr overflows in NMI handler */
105 for (i = 0; i < num_counters; ++i) {
106 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
107 continue;
108 wrmsrl(msrs->counters[i].addr, -1LL);
111 /* enable active counters */
112 for (i = 0; i < num_counters; ++i) {
113 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
114 reset_value[i] = counter_config[i].count;
116 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
118 CTRL_READ(low, high, msrs, i);
119 CTRL_CLEAR(low);
120 CTRL_SET_ENABLE(low);
121 CTRL_SET_USR(low, counter_config[i].user);
122 CTRL_SET_KERN(low, counter_config[i].kernel);
123 CTRL_SET_UM(low, counter_config[i].unit_mask);
124 CTRL_SET_EVENT(low, counter_config[i].event);
125 CTRL_WRITE(low, high, msrs, i);
126 } else {
127 reset_value[i] = 0;
133 static int ppro_check_ctrs(struct pt_regs * const regs,
134 struct op_msrs const * const msrs)
136 u64 val;
137 int i;
140 * This can happen if perf counters are in use when
141 * we steal the die notifier NMI.
143 if (unlikely(!reset_value))
144 goto out;
146 for (i = 0 ; i < num_counters; ++i) {
147 if (!reset_value[i])
148 continue;
149 rdmsrl(msrs->counters[i].addr, val);
150 if (CTR_OVERFLOWED(val)) {
151 oprofile_add_sample(regs, i);
152 wrmsrl(msrs->counters[i].addr, -reset_value[i]);
156 out:
157 /* Only P6 based Pentium M need to re-unmask the apic vector but it
158 * doesn't hurt other P6 variant */
159 apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
161 /* We can't work out if we really handled an interrupt. We
162 * might have caught a *second* counter just after overflowing
163 * the interrupt for this counter then arrives
164 * and we don't find a counter that's overflowed, so we
165 * would return 0 and get dazed + confused. Instead we always
166 * assume we found an overflow. This sucks.
168 return 1;
172 static void ppro_start(struct op_msrs const * const msrs)
174 unsigned int low, high;
175 int i;
177 if (!reset_value)
178 return;
179 for (i = 0; i < num_counters; ++i) {
180 if (reset_value[i]) {
181 CTRL_READ(low, high, msrs, i);
182 CTRL_SET_ACTIVE(low);
183 CTRL_WRITE(low, high, msrs, i);
189 static void ppro_stop(struct op_msrs const * const msrs)
191 unsigned int low, high;
192 int i;
194 if (!reset_value)
195 return;
196 for (i = 0; i < num_counters; ++i) {
197 if (!reset_value[i])
198 continue;
199 CTRL_READ(low, high, msrs, i);
200 CTRL_SET_INACTIVE(low);
201 CTRL_WRITE(low, high, msrs, i);
205 static void ppro_shutdown(struct op_msrs const * const msrs)
207 int i;
209 for (i = 0 ; i < num_counters ; ++i) {
210 if (CTR_IS_RESERVED(msrs, i))
211 release_perfctr_nmi(MSR_P6_PERFCTR0 + i);
213 for (i = 0 ; i < num_counters ; ++i) {
214 if (CTRL_IS_RESERVED(msrs, i))
215 release_evntsel_nmi(MSR_P6_EVNTSEL0 + i);
217 if (reset_value) {
218 kfree(reset_value);
219 reset_value = NULL;
224 struct op_x86_model_spec op_ppro_spec = {
225 .num_counters = 2, /* can be overriden */
226 .num_controls = 2, /* dito */
227 .fill_in_addresses = &ppro_fill_in_addresses,
228 .setup_ctrs = &ppro_setup_ctrs,
229 .check_ctrs = &ppro_check_ctrs,
230 .start = &ppro_start,
231 .stop = &ppro_stop,
232 .shutdown = &ppro_shutdown
236 * Architectural performance monitoring.
238 * Newer Intel CPUs (Core1+) have support for architectural
239 * events described in CPUID 0xA. See the IA32 SDM Vol3b.18 for details.
240 * The advantage of this is that it can be done without knowing about
241 * the specific CPU.
244 void arch_perfmon_setup_counters(void)
246 union cpuid10_eax eax;
248 eax.full = cpuid_eax(0xa);
250 /* Workaround for BIOS bugs in 6/15. Taken from perfmon2 */
251 if (eax.split.version_id == 0 && current_cpu_data.x86 == 6 &&
252 current_cpu_data.x86_model == 15) {
253 eax.split.version_id = 2;
254 eax.split.num_counters = 2;
255 eax.split.bit_width = 40;
258 num_counters = eax.split.num_counters;
260 op_arch_perfmon_spec.num_counters = num_counters;
261 op_arch_perfmon_spec.num_controls = num_counters;
262 op_ppro_spec.num_counters = num_counters;
263 op_ppro_spec.num_controls = num_counters;
266 struct op_x86_model_spec op_arch_perfmon_spec = {
267 /* num_counters/num_controls filled in at runtime */
268 .fill_in_addresses = &ppro_fill_in_addresses,
269 /* user space does the cpuid check for available events */
270 .setup_ctrs = &ppro_setup_ctrs,
271 .check_ctrs = &ppro_check_ctrs,
272 .start = &ppro_start,
273 .stop = &ppro_stop,
274 .shutdown = &ppro_shutdown