Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * @file op_model_ppro.h |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 3 | * Family 6 perfmon and architectural perfmon MSR operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * @remark Copyright 2002 OProfile authors |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 6 | * @remark Copyright 2008 Intel Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * @remark Read the file COPYING |
| 8 | * |
| 9 | * @author John Levon |
| 10 | * @author Philippe Elie |
| 11 | * @author Graydon Hoare |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 12 | * @author Andi Kleen |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/oprofile.h> |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/ptrace.h> |
| 18 | #include <asm/msr.h> |
| 19 | #include <asm/apic.h> |
Don Zickus | 3e4ff11 | 2006-06-26 13:57:01 +0200 | [diff] [blame] | 20 | #include <asm/nmi.h> |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 21 | #include <asm/intel_arch_perfmon.h> |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "op_x86_model.h" |
| 24 | #include "op_counter.h" |
| 25 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 26 | static int num_counters = 2; |
| 27 | static int counter_width = 32; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 29 | #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0) |
Andi Kleen | 7c64ade | 2008-11-07 14:02:49 +0100 | [diff] [blame] | 30 | #define CTR_OVERFLOWED(n) (!((n) & (1ULL<<(counter_width-1)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 39 | #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16)) |
| 40 | #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define CTRL_SET_UM(val, m) (val |= (m << 8)) |
| 42 | #define CTRL_SET_EVENT(val, e) (val |= e) |
| 43 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 44 | static u64 *reset_value; |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static void ppro_fill_in_addresses(struct op_msrs * const msrs) |
| 47 | { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 48 | int i; |
| 49 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 50 | for (i = 0; i < num_counters; i++) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 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; |
| 55 | } |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 56 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 57 | for (i = 0; i < num_counters; i++) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 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; |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
| 66 | static void ppro_setup_ctrs(struct op_msrs const * const msrs) |
| 67 | { |
| 68 | unsigned int low, high; |
| 69 | int i; |
| 70 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 71 | if (!reset_value) { |
Eric Dumazet | a4a16be | 2008-11-10 09:05:37 +0100 | [diff] [blame] | 72 | reset_value = kmalloc(sizeof(reset_value[0]) * num_counters, |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 73 | 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); |
Tim Blechmann | 780eef9 | 2009-02-19 17:34:03 +0100 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * For Core2 (family 6, model 15), don't reset the |
| 84 | * counter width: |
| 85 | */ |
| 86 | if (!(eax.split.version_id == 0 && |
| 87 | current_cpu_data.x86 == 6 && |
| 88 | current_cpu_data.x86_model == 15)) { |
| 89 | |
| 90 | if (counter_width < eax.split.bit_width) |
| 91 | counter_width = eax.split.bit_width; |
| 92 | } |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /* clear all counters */ |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 96 | for (i = 0 ; i < num_counters; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 97 | if (unlikely(!CTRL_IS_RESERVED(msrs, i))) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 98 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | CTRL_READ(low, high, msrs, i); |
| 100 | CTRL_CLEAR(low); |
| 101 | CTRL_WRITE(low, high, msrs, i); |
| 102 | } |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | /* avoid a false detection of ctr overflows in NMI handler */ |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 105 | for (i = 0; i < num_counters; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 106 | if (unlikely(!CTR_IS_RESERVED(msrs, i))) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 107 | continue; |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 108 | wrmsrl(msrs->counters[i].addr, -1LL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* enable active counters */ |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 112 | for (i = 0; i < num_counters; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 113 | if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | reset_value[i] = counter_config[i].count; |
| 115 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 116 | wrmsrl(msrs->counters[i].addr, -reset_value[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 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); |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 126 | } else { |
| 127 | reset_value[i] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | static int ppro_check_ctrs(struct pt_regs * const regs, |
| 134 | struct op_msrs const * const msrs) |
| 135 | { |
Andi Kleen | 7c64ade | 2008-11-07 14:02:49 +0100 | [diff] [blame] | 136 | u64 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | int i; |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 138 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 139 | for (i = 0 ; i < num_counters; ++i) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 140 | if (!reset_value[i]) |
| 141 | continue; |
Andi Kleen | 7c64ade | 2008-11-07 14:02:49 +0100 | [diff] [blame] | 142 | rdmsrl(msrs->counters[i].addr, val); |
| 143 | if (CTR_OVERFLOWED(val)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | oprofile_add_sample(regs, i); |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 145 | wrmsrl(msrs->counters[i].addr, -reset_value[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | /* Only P6 based Pentium M need to re-unmask the apic vector but it |
| 150 | * doesn't hurt other P6 variant */ |
| 151 | apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED); |
| 152 | |
| 153 | /* We can't work out if we really handled an interrupt. We |
| 154 | * might have caught a *second* counter just after overflowing |
| 155 | * the interrupt for this counter then arrives |
| 156 | * and we don't find a counter that's overflowed, so we |
| 157 | * would return 0 and get dazed + confused. Instead we always |
| 158 | * assume we found an overflow. This sucks. |
| 159 | */ |
| 160 | return 1; |
| 161 | } |
| 162 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | static void ppro_start(struct op_msrs const * const msrs) |
| 165 | { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 166 | unsigned int low, high; |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 167 | int i; |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 168 | |
Eric Dumazet | 9ea84ad | 2008-12-02 07:21:21 +0100 | [diff] [blame] | 169 | if (!reset_value) |
| 170 | return; |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 171 | for (i = 0; i < num_counters; ++i) { |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 172 | if (reset_value[i]) { |
| 173 | CTRL_READ(low, high, msrs, i); |
| 174 | CTRL_SET_ACTIVE(low); |
| 175 | CTRL_WRITE(low, high, msrs, i); |
| 176 | } |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | static void ppro_stop(struct op_msrs const * const msrs) |
| 182 | { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 183 | unsigned int low, high; |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 184 | int i; |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 185 | |
Eric Dumazet | 9ea84ad | 2008-12-02 07:21:21 +0100 | [diff] [blame] | 186 | if (!reset_value) |
| 187 | return; |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 188 | for (i = 0; i < num_counters; ++i) { |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 189 | if (!reset_value[i]) |
| 190 | continue; |
| 191 | CTRL_READ(low, high, msrs, i); |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 192 | CTRL_SET_INACTIVE(low); |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 193 | CTRL_WRITE(low, high, msrs, i); |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | static void ppro_shutdown(struct op_msrs const * const msrs) |
| 198 | { |
| 199 | int i; |
| 200 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 201 | for (i = 0 ; i < num_counters ; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 202 | if (CTR_IS_RESERVED(msrs, i)) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 203 | release_perfctr_nmi(MSR_P6_PERFCTR0 + i); |
| 204 | } |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 205 | for (i = 0 ; i < num_counters ; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame] | 206 | if (CTRL_IS_RESERVED(msrs, i)) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 207 | release_evntsel_nmi(MSR_P6_EVNTSEL0 + i); |
| 208 | } |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 209 | if (reset_value) { |
| 210 | kfree(reset_value); |
| 211 | reset_value = NULL; |
| 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | |
Robert Richter | 849620f | 2009-05-14 17:10:52 +0200 | [diff] [blame] | 216 | struct op_x86_model_spec const op_ppro_spec = { |
| 217 | .num_counters = 2, |
| 218 | .num_controls = 2, |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 219 | .fill_in_addresses = &ppro_fill_in_addresses, |
| 220 | .setup_ctrs = &ppro_setup_ctrs, |
| 221 | .check_ctrs = &ppro_check_ctrs, |
| 222 | .start = &ppro_start, |
| 223 | .stop = &ppro_stop, |
| 224 | .shutdown = &ppro_shutdown |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | }; |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * Architectural performance monitoring. |
| 229 | * |
| 230 | * Newer Intel CPUs (Core1+) have support for architectural |
| 231 | * events described in CPUID 0xA. See the IA32 SDM Vol3b.18 for details. |
| 232 | * The advantage of this is that it can be done without knowing about |
| 233 | * the specific CPU. |
| 234 | */ |
| 235 | |
Robert Richter | e419294 | 2008-10-12 15:12:34 -0400 | [diff] [blame^] | 236 | static void arch_perfmon_setup_counters(void) |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 237 | { |
| 238 | union cpuid10_eax eax; |
| 239 | |
| 240 | eax.full = cpuid_eax(0xa); |
| 241 | |
| 242 | /* Workaround for BIOS bugs in 6/15. Taken from perfmon2 */ |
| 243 | if (eax.split.version_id == 0 && current_cpu_data.x86 == 6 && |
| 244 | current_cpu_data.x86_model == 15) { |
| 245 | eax.split.version_id = 2; |
| 246 | eax.split.num_counters = 2; |
| 247 | eax.split.bit_width = 40; |
| 248 | } |
| 249 | |
| 250 | num_counters = eax.split.num_counters; |
| 251 | |
| 252 | op_arch_perfmon_spec.num_counters = num_counters; |
| 253 | op_arch_perfmon_spec.num_controls = num_counters; |
| 254 | } |
| 255 | |
Robert Richter | e419294 | 2008-10-12 15:12:34 -0400 | [diff] [blame^] | 256 | static int arch_perfmon_init(struct oprofile_operations *ignore) |
| 257 | { |
| 258 | arch_perfmon_setup_counters(); |
| 259 | return 0; |
| 260 | } |
| 261 | |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 262 | struct op_x86_model_spec op_arch_perfmon_spec = { |
Robert Richter | e419294 | 2008-10-12 15:12:34 -0400 | [diff] [blame^] | 263 | .init = &arch_perfmon_init, |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 264 | /* num_counters/num_controls filled in at runtime */ |
Robert Richter | 5a28939 | 2008-10-15 22:19:41 +0200 | [diff] [blame] | 265 | .fill_in_addresses = &ppro_fill_in_addresses, |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 266 | /* user space does the cpuid check for available events */ |
Robert Richter | 5a28939 | 2008-10-15 22:19:41 +0200 | [diff] [blame] | 267 | .setup_ctrs = &ppro_setup_ctrs, |
| 268 | .check_ctrs = &ppro_check_ctrs, |
| 269 | .start = &ppro_start, |
| 270 | .stop = &ppro_stop, |
| 271 | .shutdown = &ppro_shutdown |
Andi Kleen | b991702 | 2008-08-18 14:50:31 +0200 | [diff] [blame] | 272 | }; |