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 |
| 3 | * pentium pro / P6 model-specific MSR operations |
| 4 | * |
| 5 | * @remark Copyright 2002 OProfile authors |
| 6 | * @remark Read the file COPYING |
| 7 | * |
| 8 | * @author John Levon |
| 9 | * @author Philippe Elie |
| 10 | * @author Graydon Hoare |
| 11 | */ |
| 12 | |
| 13 | #include <linux/oprofile.h> |
| 14 | #include <asm/ptrace.h> |
| 15 | #include <asm/msr.h> |
| 16 | #include <asm/apic.h> |
Don Zickus | 3e4ff11 | 2006-06-26 13:57:01 +0200 | [diff] [blame] | 17 | #include <asm/nmi.h> |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "op_x86_model.h" |
| 20 | #include "op_counter.h" |
| 21 | |
| 22 | #define NUM_COUNTERS 2 |
| 23 | #define NUM_CONTROLS 2 |
| 24 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 25 | #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0) |
| 26 | #define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0) |
| 27 | #define CTR_32BIT_WRITE(l, msrs, c) \ |
| 28 | do {wrmsr(msrs->counters[(c)].addr, -(u32)(l), 0); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #define CTR_OVERFLOWED(n) (!((n) & (1U<<31))) |
| 30 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 31 | #define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0) |
| 32 | #define CTRL_READ(l, h, msrs, c) do {rdmsr((msrs->controls[(c)].addr), (l), (h)); } while (0) |
| 33 | #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] | 34 | #define CTRL_SET_ACTIVE(n) (n |= (1<<22)) |
| 35 | #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22)) |
| 36 | #define CTRL_CLEAR(x) (x &= (1<<21)) |
| 37 | #define CTRL_SET_ENABLE(val) (val |= 1<<20) |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 38 | #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16)) |
| 39 | #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define CTRL_SET_UM(val, m) (val |= (m << 8)) |
| 41 | #define CTRL_SET_EVENT(val, e) (val |= e) |
| 42 | |
| 43 | static unsigned long reset_value[NUM_COUNTERS]; |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static void ppro_fill_in_addresses(struct op_msrs * const msrs) |
| 46 | { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 47 | int i; |
| 48 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 49 | for (i = 0; i < NUM_COUNTERS; i++) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 50 | if (reserve_perfctr_nmi(MSR_P6_PERFCTR0 + i)) |
| 51 | msrs->counters[i].addr = MSR_P6_PERFCTR0 + i; |
| 52 | else |
| 53 | msrs->counters[i].addr = 0; |
| 54 | } |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 55 | |
| 56 | for (i = 0; i < NUM_CONTROLS; i++) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 57 | if (reserve_evntsel_nmi(MSR_P6_EVNTSEL0 + i)) |
| 58 | msrs->controls[i].addr = MSR_P6_EVNTSEL0 + i; |
| 59 | else |
| 60 | msrs->controls[i].addr = 0; |
| 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
| 65 | static void ppro_setup_ctrs(struct op_msrs const * const msrs) |
| 66 | { |
| 67 | unsigned int low, high; |
| 68 | int i; |
| 69 | |
| 70 | /* clear all counters */ |
| 71 | for (i = 0 ; i < NUM_CONTROLS; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 72 | if (unlikely(!CTRL_IS_RESERVED(msrs, i))) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 73 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | CTRL_READ(low, high, msrs, i); |
| 75 | CTRL_CLEAR(low); |
| 76 | CTRL_WRITE(low, high, msrs, i); |
| 77 | } |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | /* avoid a false detection of ctr overflows in NMI handler */ |
| 80 | for (i = 0; i < NUM_COUNTERS; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 81 | if (unlikely(!CTR_IS_RESERVED(msrs, i))) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 82 | continue; |
Venkatesh Pallipadi | 4426426 | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 83 | CTR_32BIT_WRITE(1, msrs, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* enable active counters */ |
| 87 | for (i = 0; i < NUM_COUNTERS; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 88 | if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | reset_value[i] = counter_config[i].count; |
| 90 | |
Venkatesh Pallipadi | 4426426 | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 91 | CTR_32BIT_WRITE(counter_config[i].count, msrs, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | CTRL_READ(low, high, msrs, i); |
| 94 | CTRL_CLEAR(low); |
| 95 | CTRL_SET_ENABLE(low); |
| 96 | CTRL_SET_USR(low, counter_config[i].user); |
| 97 | CTRL_SET_KERN(low, counter_config[i].kernel); |
| 98 | CTRL_SET_UM(low, counter_config[i].unit_mask); |
| 99 | CTRL_SET_EVENT(low, counter_config[i].event); |
| 100 | CTRL_WRITE(low, high, msrs, i); |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 101 | } else { |
| 102 | reset_value[i] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | static int ppro_check_ctrs(struct pt_regs * const regs, |
| 109 | struct op_msrs const * const msrs) |
| 110 | { |
| 111 | unsigned int low, high; |
| 112 | int i; |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | for (i = 0 ; i < NUM_COUNTERS; ++i) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 115 | if (!reset_value[i]) |
| 116 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | CTR_READ(low, high, msrs, i); |
| 118 | if (CTR_OVERFLOWED(low)) { |
| 119 | oprofile_add_sample(regs, i); |
Venkatesh Pallipadi | 4426426 | 2007-02-13 13:26:23 +0100 | [diff] [blame] | 120 | CTR_32BIT_WRITE(reset_value[i], msrs, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | /* Only P6 based Pentium M need to re-unmask the apic vector but it |
| 125 | * doesn't hurt other P6 variant */ |
| 126 | apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED); |
| 127 | |
| 128 | /* We can't work out if we really handled an interrupt. We |
| 129 | * might have caught a *second* counter just after overflowing |
| 130 | * the interrupt for this counter then arrives |
| 131 | * and we don't find a counter that's overflowed, so we |
| 132 | * would return 0 and get dazed + confused. Instead we always |
| 133 | * assume we found an overflow. This sucks. |
| 134 | */ |
| 135 | return 1; |
| 136 | } |
| 137 | |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | static void ppro_start(struct op_msrs const * const msrs) |
| 140 | { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 141 | unsigned int low, high; |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 142 | int i; |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 143 | |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 144 | for (i = 0; i < NUM_COUNTERS; ++i) { |
| 145 | if (reset_value[i]) { |
| 146 | CTRL_READ(low, high, msrs, i); |
| 147 | CTRL_SET_ACTIVE(low); |
| 148 | CTRL_WRITE(low, high, msrs, i); |
| 149 | } |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 150 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
| 154 | static void ppro_stop(struct op_msrs const * const msrs) |
| 155 | { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 156 | unsigned int low, high; |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 157 | int i; |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 158 | |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 159 | for (i = 0; i < NUM_COUNTERS; ++i) { |
| 160 | if (!reset_value[i]) |
| 161 | continue; |
| 162 | CTRL_READ(low, high, msrs, i); |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 163 | CTRL_SET_INACTIVE(low); |
Arun Sharma | 6b77df0 | 2006-09-29 02:00:01 -0700 | [diff] [blame] | 164 | CTRL_WRITE(low, high, msrs, i); |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | static void ppro_shutdown(struct op_msrs const * const msrs) |
| 169 | { |
| 170 | int i; |
| 171 | |
| 172 | for (i = 0 ; i < NUM_COUNTERS ; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 173 | if (CTR_IS_RESERVED(msrs, i)) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 174 | release_perfctr_nmi(MSR_P6_PERFCTR0 + i); |
| 175 | } |
| 176 | for (i = 0 ; i < NUM_CONTROLS ; ++i) { |
Paolo Ciarrocchi | 8b45b72 | 2008-02-19 23:43:25 +0100 | [diff] [blame^] | 177 | if (CTRL_IS_RESERVED(msrs, i)) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 178 | release_evntsel_nmi(MSR_P6_EVNTSEL0 + i); |
| 179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
| 183 | struct op_x86_model_spec const op_ppro_spec = { |
| 184 | .num_counters = NUM_COUNTERS, |
| 185 | .num_controls = NUM_CONTROLS, |
| 186 | .fill_in_addresses = &ppro_fill_in_addresses, |
| 187 | .setup_ctrs = &ppro_setup_ctrs, |
| 188 | .check_ctrs = &ppro_check_ctrs, |
| 189 | .start = &ppro_start, |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 190 | .stop = &ppro_stop, |
| 191 | .shutdown = &ppro_shutdown |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | }; |