Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 1 | /* |
Robert Richter | 6852fd9 | 2008-07-22 21:09:08 +0200 | [diff] [blame] | 2 | * @file op_model_amd.c |
Barry Kasindorf | bd87f1f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 3 | * athlon / K7 / K8 / Family 10h model-specific MSR operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 5 | * @remark Copyright 2002-2009 OProfile authors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * @remark Read the file COPYING |
| 7 | * |
| 8 | * @author John Levon |
| 9 | * @author Philippe Elie |
| 10 | * @author Graydon Hoare |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 11 | * @author Robert Richter <robert.richter@amd.com> |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 12 | * @author Barry Kasindorf <barry.kasindorf@amd.com> |
| 13 | * @author Jason Yeh <jason.yeh@amd.com> |
| 14 | * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com> |
Robert Richter | ae735e9 | 2008-12-25 17:26:07 +0100 | [diff] [blame] | 15 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #include <linux/oprofile.h> |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 18 | #include <linux/device.h> |
| 19 | #include <linux/pci.h> |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 20 | #include <linux/percpu.h> |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/ptrace.h> |
| 23 | #include <asm/msr.h> |
Don Zickus | 3e4ff11 | 2006-06-26 13:57:01 +0200 | [diff] [blame] | 24 | #include <asm/nmi.h> |
Robert Richter | 013cfc5 | 2010-01-28 18:05:26 +0100 | [diff] [blame] | 25 | #include <asm/apic.h> |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 26 | #include <asm/processor.h> |
| 27 | #include <asm/cpufeature.h> |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "op_x86_model.h" |
| 30 | #include "op_counter.h" |
| 31 | |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 32 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 33 | #define NUM_VIRT_COUNTERS 32 |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 34 | #else |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 35 | #define NUM_VIRT_COUNTERS 0 |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 36 | #endif |
| 37 | |
Robert Richter | 3370d35 | 2009-05-25 15:10:32 +0200 | [diff] [blame] | 38 | #define OP_EVENT_MASK 0x0FFF |
Robert Richter | 42399ad | 2009-05-25 17:59:06 +0200 | [diff] [blame] | 39 | #define OP_CTR_OVERFLOW (1ULL<<31) |
Robert Richter | 3370d35 | 2009-05-25 15:10:32 +0200 | [diff] [blame] | 40 | |
| 41 | #define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 43 | static int num_counters; |
| 44 | static unsigned long reset_value[OP_MAX_COUNTER]; |
Robert Richter | 852402c | 2008-07-22 21:09:06 +0200 | [diff] [blame] | 45 | |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 46 | #define IBS_FETCH_SIZE 6 |
| 47 | #define IBS_OP_SIZE 12 |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 48 | |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 49 | static u32 ibs_caps; |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 50 | |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 51 | struct ibs_config { |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 52 | unsigned long op_enabled; |
| 53 | unsigned long fetch_enabled; |
| 54 | unsigned long max_cnt_fetch; |
| 55 | unsigned long max_cnt_op; |
| 56 | unsigned long rand_en; |
| 57 | unsigned long dispatched_ops; |
Robert Richter | 25da695 | 2010-09-21 15:49:31 +0200 | [diff] [blame] | 58 | unsigned long branch_target; |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 61 | struct ibs_state { |
Robert Richter | 25da695 | 2010-09-21 15:49:31 +0200 | [diff] [blame] | 62 | u64 ibs_op_ctl; |
| 63 | int branch_target; |
| 64 | unsigned long sample_size; |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | static struct ibs_config ibs_config; |
| 68 | static struct ibs_state ibs_state; |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 69 | |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 70 | /* |
Robert Richter | ba52078 | 2010-02-23 15:46:49 +0100 | [diff] [blame] | 71 | * IBS randomization macros |
| 72 | */ |
| 73 | #define IBS_RANDOM_BITS 12 |
| 74 | #define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1) |
| 75 | #define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5)) |
| 76 | |
Suravee Suthikulpanit | f125be1 | 2010-01-18 11:25:45 -0600 | [diff] [blame] | 77 | /* |
| 78 | * 16-bit Linear Feedback Shift Register (LFSR) |
| 79 | * |
| 80 | * 16 14 13 11 |
| 81 | * Feedback polynomial = X + X + X + X + 1 |
| 82 | */ |
| 83 | static unsigned int lfsr_random(void) |
| 84 | { |
| 85 | static unsigned int lfsr_value = 0xF00D; |
| 86 | unsigned int bit; |
| 87 | |
| 88 | /* Compute next bit to shift in */ |
| 89 | bit = ((lfsr_value >> 0) ^ |
| 90 | (lfsr_value >> 2) ^ |
| 91 | (lfsr_value >> 3) ^ |
| 92 | (lfsr_value >> 5)) & 0x0001; |
| 93 | |
| 94 | /* Advance to next register value */ |
| 95 | lfsr_value = (lfsr_value >> 1) | (bit << 15); |
| 96 | |
| 97 | return lfsr_value; |
| 98 | } |
| 99 | |
Robert Richter | ba52078 | 2010-02-23 15:46:49 +0100 | [diff] [blame] | 100 | /* |
| 101 | * IBS software randomization |
| 102 | * |
| 103 | * The IBS periodic op counter is randomized in software. The lower 12 |
| 104 | * bits of the 20 bit counter are randomized. IbsOpCurCnt is |
| 105 | * initialized with a 12 bit random value. |
| 106 | */ |
| 107 | static inline u64 op_amd_randomize_ibs_op(u64 val) |
| 108 | { |
| 109 | unsigned int random = lfsr_random(); |
| 110 | |
| 111 | if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) |
| 112 | /* |
| 113 | * Work around if the hw can not write to IbsOpCurCnt |
| 114 | * |
| 115 | * Randomize the lower 8 bits of the 16 bit |
| 116 | * IbsOpMaxCnt [15:0] value in the range of -128 to |
| 117 | * +127 by adding/subtracting an offset to the |
| 118 | * maximum count (IbsOpMaxCnt). |
| 119 | * |
| 120 | * To avoid over or underflows and protect upper bits |
| 121 | * starting at bit 16, the initial value for |
| 122 | * IbsOpMaxCnt must fit in the range from 0x0081 to |
| 123 | * 0xff80. |
| 124 | */ |
| 125 | val += (s8)(random >> 4); |
| 126 | else |
| 127 | val |= (u64)(random & IBS_RANDOM_MASK) << 32; |
| 128 | |
| 129 | return val; |
| 130 | } |
| 131 | |
Andrew Morton | 4680e64 | 2009-06-23 12:36:08 -0700 | [diff] [blame] | 132 | static inline void |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 133 | op_amd_handle_ibs(struct pt_regs * const regs, |
| 134 | struct op_msrs const * const msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 136 | u64 val, ctl; |
Robert Richter | 1acda87 | 2009-01-05 10:35:31 +0100 | [diff] [blame] | 137 | struct op_entry entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 139 | if (!ibs_caps) |
Andrew Morton | 4680e64 | 2009-06-23 12:36:08 -0700 | [diff] [blame] | 140 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 142 | if (ibs_config.fetch_enabled) { |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 143 | rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl); |
| 144 | if (ctl & IBS_FETCH_VAL) { |
| 145 | rdmsrl(MSR_AMD64_IBSFETCHLINAD, val); |
| 146 | oprofile_write_reserve(&entry, regs, val, |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 147 | IBS_FETCH_CODE, IBS_FETCH_SIZE); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 148 | oprofile_add_data64(&entry, val); |
| 149 | oprofile_add_data64(&entry, ctl); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 150 | rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 151 | oprofile_add_data64(&entry, val); |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 152 | oprofile_write_commit(&entry); |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 153 | |
Robert Richter | fd13f6c | 2008-10-19 21:00:09 +0200 | [diff] [blame] | 154 | /* reenable the IRQ */ |
Robert Richter | a163b10 | 2010-02-25 19:43:07 +0100 | [diff] [blame] | 155 | ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 156 | ctl |= IBS_FETCH_ENABLE; |
| 157 | wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl); |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 161 | if (ibs_config.op_enabled) { |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 162 | rdmsrl(MSR_AMD64_IBSOPCTL, ctl); |
| 163 | if (ctl & IBS_OP_VAL) { |
| 164 | rdmsrl(MSR_AMD64_IBSOPRIP, val); |
Robert Richter | 25da695 | 2010-09-21 15:49:31 +0200 | [diff] [blame] | 165 | oprofile_write_reserve(&entry, regs, val, IBS_OP_CODE, |
| 166 | ibs_state.sample_size); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 167 | oprofile_add_data64(&entry, val); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 168 | rdmsrl(MSR_AMD64_IBSOPDATA, val); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 169 | oprofile_add_data64(&entry, val); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 170 | rdmsrl(MSR_AMD64_IBSOPDATA2, val); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 171 | oprofile_add_data64(&entry, val); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 172 | rdmsrl(MSR_AMD64_IBSOPDATA3, val); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 173 | oprofile_add_data64(&entry, val); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 174 | rdmsrl(MSR_AMD64_IBSDCLINAD, val); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 175 | oprofile_add_data64(&entry, val); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 176 | rdmsrl(MSR_AMD64_IBSDCPHYSAD, val); |
Robert Richter | 51563a0 | 2009-06-03 20:54:56 +0200 | [diff] [blame] | 177 | oprofile_add_data64(&entry, val); |
Robert Richter | 25da695 | 2010-09-21 15:49:31 +0200 | [diff] [blame] | 178 | if (ibs_state.branch_target) { |
| 179 | rdmsrl(MSR_AMD64_IBSBRTARGET, val); |
| 180 | oprofile_add_data(&entry, (unsigned long)val); |
| 181 | } |
Robert Richter | 14f0ca8 | 2009-01-07 21:50:22 +0100 | [diff] [blame] | 182 | oprofile_write_commit(&entry); |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 183 | |
| 184 | /* reenable the IRQ */ |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 185 | ctl = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 186 | wrmsrl(MSR_AMD64_IBSOPCTL, ctl); |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 187 | } |
| 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 191 | static inline void op_amd_start_ibs(void) |
| 192 | { |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 193 | u64 val; |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 194 | |
| 195 | if (!ibs_caps) |
| 196 | return; |
| 197 | |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 198 | memset(&ibs_state, 0, sizeof(ibs_state)); |
| 199 | |
Robert Richter | b47fad3 | 2010-09-22 17:45:39 +0200 | [diff] [blame] | 200 | /* |
| 201 | * Note: Since the max count settings may out of range we |
| 202 | * write back the actual used values so that userland can read |
| 203 | * it. |
| 204 | */ |
| 205 | |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 206 | if (ibs_config.fetch_enabled) { |
Robert Richter | b47fad3 | 2010-09-22 17:45:39 +0200 | [diff] [blame] | 207 | val = ibs_config.max_cnt_fetch >> 4; |
| 208 | val = min(val, IBS_FETCH_MAX_CNT); |
| 209 | ibs_config.max_cnt_fetch = val << 4; |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 210 | val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0; |
| 211 | val |= IBS_FETCH_ENABLE; |
| 212 | wrmsrl(MSR_AMD64_IBSFETCHCTL, val); |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 213 | } |
| 214 | |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 215 | if (ibs_config.op_enabled) { |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 216 | val = ibs_config.max_cnt_op >> 4; |
Robert Richter | ba52078 | 2010-02-23 15:46:49 +0100 | [diff] [blame] | 217 | if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) { |
| 218 | /* |
| 219 | * IbsOpCurCnt not supported. See |
| 220 | * op_amd_randomize_ibs_op() for details. |
| 221 | */ |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 222 | val = clamp(val, 0x0081ULL, 0xFF80ULL); |
Robert Richter | b47fad3 | 2010-09-22 17:45:39 +0200 | [diff] [blame] | 223 | ibs_config.max_cnt_op = val << 4; |
Robert Richter | ba52078 | 2010-02-23 15:46:49 +0100 | [diff] [blame] | 224 | } else { |
| 225 | /* |
| 226 | * The start value is randomized with a |
| 227 | * positive offset, we need to compensate it |
| 228 | * with the half of the randomized range. Also |
| 229 | * avoid underflows. |
| 230 | */ |
Robert Richter | b47fad3 | 2010-09-22 17:45:39 +0200 | [diff] [blame] | 231 | val += IBS_RANDOM_MAXCNT_OFFSET; |
| 232 | if (ibs_caps & IBS_CAPS_OPCNTEXT) |
| 233 | val = min(val, IBS_OP_MAX_CNT_EXT); |
| 234 | else |
| 235 | val = min(val, IBS_OP_MAX_CNT); |
| 236 | ibs_config.max_cnt_op = |
| 237 | (val - IBS_RANDOM_MAXCNT_OFFSET) << 4; |
Robert Richter | ba52078 | 2010-02-23 15:46:49 +0100 | [diff] [blame] | 238 | } |
Robert Richter | b47fad3 | 2010-09-22 17:45:39 +0200 | [diff] [blame] | 239 | val = ((val & ~IBS_OP_MAX_CNT) << 4) | (val & IBS_OP_MAX_CNT); |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 240 | val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0; |
| 241 | val |= IBS_OP_ENABLE; |
| 242 | ibs_state.ibs_op_ctl = val; |
Robert Richter | 25da695 | 2010-09-21 15:49:31 +0200 | [diff] [blame] | 243 | ibs_state.sample_size = IBS_OP_SIZE; |
| 244 | if (ibs_config.branch_target) { |
| 245 | ibs_state.branch_target = 1; |
| 246 | ibs_state.sample_size++; |
| 247 | } |
Robert Richter | 53b39e9 | 2010-09-21 17:58:15 +0200 | [diff] [blame] | 248 | val = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl); |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 249 | wrmsrl(MSR_AMD64_IBSOPCTL, val); |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | static void op_amd_stop_ibs(void) |
| 254 | { |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 255 | if (!ibs_caps) |
| 256 | return; |
| 257 | |
| 258 | if (ibs_config.fetch_enabled) |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 259 | /* clear max count and enable */ |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 260 | wrmsrl(MSR_AMD64_IBSFETCHCTL, 0); |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 261 | |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 262 | if (ibs_config.op_enabled) |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 263 | /* clear max count and enable */ |
Robert Richter | c572ae4 | 2009-06-03 20:10:39 +0200 | [diff] [blame] | 264 | wrmsrl(MSR_AMD64_IBSOPCTL, 0); |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Robert Richter | da759fe | 2010-02-26 10:54:56 +0100 | [diff] [blame] | 267 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
| 268 | |
| 269 | static void op_mux_switch_ctrl(struct op_x86_model_spec const *model, |
| 270 | struct op_msrs const * const msrs) |
| 271 | { |
| 272 | u64 val; |
| 273 | int i; |
| 274 | |
| 275 | /* enable active counters */ |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 276 | for (i = 0; i < num_counters; ++i) { |
Robert Richter | da759fe | 2010-02-26 10:54:56 +0100 | [diff] [blame] | 277 | int virt = op_x86_phys_to_virt(i); |
| 278 | if (!reset_value[virt]) |
| 279 | continue; |
| 280 | rdmsrl(msrs->controls[i].addr, val); |
| 281 | val &= model->reserved; |
| 282 | val |= op_x86_get_ctrl(model, &counter_config[virt]); |
| 283 | wrmsrl(msrs->controls[i].addr, val); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | #endif |
| 288 | |
| 289 | /* functions for op_amd_spec */ |
| 290 | |
| 291 | static void op_amd_shutdown(struct op_msrs const * const msrs) |
| 292 | { |
| 293 | int i; |
| 294 | |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 295 | for (i = 0; i < num_counters; ++i) { |
Robert Richter | da759fe | 2010-02-26 10:54:56 +0100 | [diff] [blame] | 296 | if (!msrs->counters[i].addr) |
| 297 | continue; |
| 298 | release_perfctr_nmi(MSR_K7_PERFCTR0 + i); |
| 299 | release_evntsel_nmi(MSR_K7_EVNTSEL0 + i); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | static int op_amd_fill_in_addresses(struct op_msrs * const msrs) |
| 304 | { |
| 305 | int i; |
| 306 | |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 307 | for (i = 0; i < num_counters; i++) { |
Robert Richter | da759fe | 2010-02-26 10:54:56 +0100 | [diff] [blame] | 308 | if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i)) |
| 309 | goto fail; |
| 310 | if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) { |
| 311 | release_perfctr_nmi(MSR_K7_PERFCTR0 + i); |
| 312 | goto fail; |
| 313 | } |
| 314 | /* both registers must be reserved */ |
Robert Richter | b1dc3c4 | 2012-06-20 20:46:35 +0200 | [diff] [blame] | 315 | if (num_counters == AMD64_NUM_COUNTERS_CORE) { |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 316 | msrs->counters[i].addr = MSR_F15H_PERF_CTR + (i << 1); |
| 317 | msrs->controls[i].addr = MSR_F15H_PERF_CTL + (i << 1); |
| 318 | } else { |
| 319 | msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i; |
| 320 | msrs->counters[i].addr = MSR_K7_PERFCTR0 + i; |
| 321 | } |
Robert Richter | da759fe | 2010-02-26 10:54:56 +0100 | [diff] [blame] | 322 | continue; |
| 323 | fail: |
| 324 | if (!counter_config[i].enabled) |
| 325 | continue; |
| 326 | op_x86_warn_reserved(i); |
| 327 | op_amd_shutdown(msrs); |
| 328 | return -EBUSY; |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static void op_amd_setup_ctrs(struct op_x86_model_spec const *model, |
| 335 | struct op_msrs const * const msrs) |
| 336 | { |
| 337 | u64 val; |
| 338 | int i; |
| 339 | |
| 340 | /* setup reset_value */ |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 341 | for (i = 0; i < OP_MAX_COUNTER; ++i) { |
Robert Richter | da759fe | 2010-02-26 10:54:56 +0100 | [diff] [blame] | 342 | if (counter_config[i].enabled |
| 343 | && msrs->counters[op_x86_virt_to_phys(i)].addr) |
| 344 | reset_value[i] = counter_config[i].count; |
| 345 | else |
| 346 | reset_value[i] = 0; |
| 347 | } |
| 348 | |
| 349 | /* clear all counters */ |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 350 | for (i = 0; i < num_counters; ++i) { |
Robert Richter | da759fe | 2010-02-26 10:54:56 +0100 | [diff] [blame] | 351 | if (!msrs->controls[i].addr) |
| 352 | continue; |
| 353 | rdmsrl(msrs->controls[i].addr, val); |
| 354 | if (val & ARCH_PERFMON_EVENTSEL_ENABLE) |
| 355 | op_x86_warn_in_use(i); |
| 356 | val &= model->reserved; |
| 357 | wrmsrl(msrs->controls[i].addr, val); |
| 358 | /* |
| 359 | * avoid a false detection of ctr overflows in NMI |
| 360 | * handler |
| 361 | */ |
| 362 | wrmsrl(msrs->counters[i].addr, -1LL); |
| 363 | } |
| 364 | |
| 365 | /* enable active counters */ |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 366 | for (i = 0; i < num_counters; ++i) { |
Robert Richter | da759fe | 2010-02-26 10:54:56 +0100 | [diff] [blame] | 367 | int virt = op_x86_phys_to_virt(i); |
| 368 | if (!reset_value[virt]) |
| 369 | continue; |
| 370 | |
| 371 | /* setup counter registers */ |
| 372 | wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]); |
| 373 | |
| 374 | /* setup control registers */ |
| 375 | rdmsrl(msrs->controls[i].addr, val); |
| 376 | val &= model->reserved; |
| 377 | val |= op_x86_get_ctrl(model, &counter_config[virt]); |
| 378 | wrmsrl(msrs->controls[i].addr, val); |
| 379 | } |
| 380 | } |
| 381 | |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 382 | static int op_amd_check_ctrs(struct pt_regs * const regs, |
| 383 | struct op_msrs const * const msrs) |
| 384 | { |
Robert Richter | 42399ad | 2009-05-25 17:59:06 +0200 | [diff] [blame] | 385 | u64 val; |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 386 | int i; |
| 387 | |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 388 | for (i = 0; i < num_counters; ++i) { |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 389 | int virt = op_x86_phys_to_virt(i); |
| 390 | if (!reset_value[virt]) |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 391 | continue; |
Robert Richter | 42399ad | 2009-05-25 17:59:06 +0200 | [diff] [blame] | 392 | rdmsrl(msrs->counters[i].addr, val); |
| 393 | /* bit is clear if overflowed: */ |
| 394 | if (val & OP_CTR_OVERFLOW) |
| 395 | continue; |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 396 | oprofile_add_sample(regs, virt); |
| 397 | wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]); |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | op_amd_handle_ibs(regs, msrs); |
| 401 | |
| 402 | /* See op_model_ppro.c */ |
| 403 | return 1; |
| 404 | } |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 405 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 406 | static void op_amd_start(struct op_msrs const * const msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Robert Richter | dea3766 | 2009-05-25 18:11:52 +0200 | [diff] [blame] | 408 | u64 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | int i; |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 410 | |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 411 | for (i = 0; i < num_counters; ++i) { |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 412 | if (!reset_value[op_x86_phys_to_virt(i)]) |
| 413 | continue; |
| 414 | rdmsrl(msrs->controls[i].addr, val); |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 415 | val |= ARCH_PERFMON_EVENTSEL_ENABLE; |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 416 | wrmsrl(msrs->controls[i].addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
Robert Richter | 852402c | 2008-07-22 21:09:06 +0200 | [diff] [blame] | 418 | |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 419 | op_amd_start_ibs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 422 | static void op_amd_stop(struct op_msrs const * const msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Robert Richter | dea3766 | 2009-05-25 18:11:52 +0200 | [diff] [blame] | 424 | u64 val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | int i; |
| 426 | |
Robert Richter | fd13f6c | 2008-10-19 21:00:09 +0200 | [diff] [blame] | 427 | /* |
| 428 | * Subtle: stop on all counters to avoid race with setting our |
| 429 | * pm callback |
| 430 | */ |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 431 | for (i = 0; i < num_counters; ++i) { |
Robert Richter | d8471ad | 2009-07-16 13:04:43 +0200 | [diff] [blame] | 432 | if (!reset_value[op_x86_phys_to_virt(i)]) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 433 | continue; |
Robert Richter | dea3766 | 2009-05-25 18:11:52 +0200 | [diff] [blame] | 434 | rdmsrl(msrs->controls[i].addr, val); |
Robert Richter | bb1165d | 2010-03-01 14:21:23 +0100 | [diff] [blame] | 435 | val &= ~ARCH_PERFMON_EVENTSEL_ENABLE; |
Robert Richter | dea3766 | 2009-05-25 18:11:52 +0200 | [diff] [blame] | 436 | wrmsrl(msrs->controls[i].addr, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 438 | |
Robert Richter | 9063759 | 2009-03-10 19:15:57 +0100 | [diff] [blame] | 439 | op_amd_stop_ibs(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Robert Richter | c7c2580 | 2011-01-03 12:15:14 +0100 | [diff] [blame] | 442 | /* |
| 443 | * check and reserve APIC extended interrupt LVT offset for IBS if |
| 444 | * available |
Robert Richter | c7c2580 | 2011-01-03 12:15:14 +0100 | [diff] [blame] | 445 | */ |
| 446 | |
Robert Richter | bae663b | 2010-05-05 17:47:17 +0200 | [diff] [blame] | 447 | static void init_ibs(void) |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 448 | { |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 449 | ibs_caps = get_ibs_caps(); |
Robert Richter | 3d2606f | 2011-05-20 09:46:54 +0200 | [diff] [blame] | 450 | |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 451 | if (!ibs_caps) |
Robert Richter | 3d2606f | 2011-05-20 09:46:54 +0200 | [diff] [blame] | 452 | return; |
| 453 | |
Robert Richter | 3d2606f | 2011-05-20 09:46:54 +0200 | [diff] [blame] | 454 | printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n", ibs_caps); |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 455 | } |
| 456 | |
Al Viro | ef7bca1 | 2013-07-19 15:52:42 +0400 | [diff] [blame] | 457 | static int (*create_arch_files)(struct dentry *root); |
Robert Richter | 270d3e1 | 2008-07-22 21:09:01 +0200 | [diff] [blame] | 458 | |
Al Viro | ef7bca1 | 2013-07-19 15:52:42 +0400 | [diff] [blame] | 459 | static int setup_ibs_files(struct dentry *root) |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 460 | { |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 461 | struct dentry *dir; |
Robert Richter | 270d3e1 | 2008-07-22 21:09:01 +0200 | [diff] [blame] | 462 | int ret = 0; |
| 463 | |
| 464 | /* architecture specific files */ |
| 465 | if (create_arch_files) |
Al Viro | ef7bca1 | 2013-07-19 15:52:42 +0400 | [diff] [blame] | 466 | ret = create_arch_files(root); |
Robert Richter | 270d3e1 | 2008-07-22 21:09:01 +0200 | [diff] [blame] | 467 | |
| 468 | if (ret) |
| 469 | return ret; |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 470 | |
Robert Richter | 64683da | 2010-02-04 10:57:23 +0100 | [diff] [blame] | 471 | if (!ibs_caps) |
Robert Richter | 270d3e1 | 2008-07-22 21:09:01 +0200 | [diff] [blame] | 472 | return ret; |
| 473 | |
| 474 | /* model specific files */ |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 475 | |
| 476 | /* setup some reasonable defaults */ |
Robert Richter | 25da695 | 2010-09-21 15:49:31 +0200 | [diff] [blame] | 477 | memset(&ibs_config, 0, sizeof(ibs_config)); |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 478 | ibs_config.max_cnt_fetch = 250000; |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 479 | ibs_config.max_cnt_op = 250000; |
Robert Richter | 2d55a47 | 2008-07-18 17:56:05 +0200 | [diff] [blame] | 480 | |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 481 | if (ibs_caps & IBS_CAPS_FETCHSAM) { |
Al Viro | ecde282 | 2013-07-19 15:58:27 +0400 | [diff] [blame] | 482 | dir = oprofilefs_mkdir(root, "ibs_fetch"); |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 483 | oprofilefs_create_ulong(dir, "enable", |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 484 | &ibs_config.fetch_enabled); |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 485 | oprofilefs_create_ulong(dir, "max_count", |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 486 | &ibs_config.max_cnt_fetch); |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 487 | oprofilefs_create_ulong(dir, "rand_enable", |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 488 | &ibs_config.rand_en); |
| 489 | } |
Robert Richter | 2d55a47 | 2008-07-18 17:56:05 +0200 | [diff] [blame] | 490 | |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 491 | if (ibs_caps & IBS_CAPS_OPSAM) { |
Al Viro | ecde282 | 2013-07-19 15:58:27 +0400 | [diff] [blame] | 492 | dir = oprofilefs_mkdir(root, "ibs_op"); |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 493 | oprofilefs_create_ulong(dir, "enable", |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 494 | &ibs_config.op_enabled); |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 495 | oprofilefs_create_ulong(dir, "max_count", |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 496 | &ibs_config.max_cnt_op); |
| 497 | if (ibs_caps & IBS_CAPS_OPCNT) |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 498 | oprofilefs_create_ulong(dir, "dispatched_ops", |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 499 | &ibs_config.dispatched_ops); |
Robert Richter | 25da695 | 2010-09-21 15:49:31 +0200 | [diff] [blame] | 500 | if (ibs_caps & IBS_CAPS_BRNTRGT) |
Al Viro | 6af4ea0 | 2013-07-19 16:10:36 +0400 | [diff] [blame] | 501 | oprofilefs_create_ulong(dir, "branch_target", |
Robert Richter | 25da695 | 2010-09-21 15:49:31 +0200 | [diff] [blame] | 502 | &ibs_config.branch_target); |
Robert Richter | 4ac945f | 2010-09-21 15:58:32 +0200 | [diff] [blame] | 503 | } |
Robert Richter | fc2bd73 | 2008-07-22 21:09:00 +0200 | [diff] [blame] | 504 | |
| 505 | return 0; |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 506 | } |
| 507 | |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 508 | struct op_x86_model_spec op_amd_spec; |
| 509 | |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 510 | static int op_amd_init(struct oprofile_operations *ops) |
| 511 | { |
Robert Richter | bae663b | 2010-05-05 17:47:17 +0200 | [diff] [blame] | 512 | init_ibs(); |
Robert Richter | 270d3e1 | 2008-07-22 21:09:01 +0200 | [diff] [blame] | 513 | create_arch_files = ops->create_files; |
| 514 | ops->create_files = setup_ibs_files; |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 515 | |
| 516 | if (boot_cpu_data.x86 == 0x15) { |
Robert Richter | b1dc3c4 | 2012-06-20 20:46:35 +0200 | [diff] [blame] | 517 | num_counters = AMD64_NUM_COUNTERS_CORE; |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 518 | } else { |
Robert Richter | ee5789d | 2011-09-21 11:30:17 +0200 | [diff] [blame] | 519 | num_counters = AMD64_NUM_COUNTERS; |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | op_amd_spec.num_counters = num_counters; |
| 523 | op_amd_spec.num_controls = num_counters; |
| 524 | op_amd_spec.num_virt_counters = max(num_counters, NUM_VIRT_COUNTERS); |
| 525 | |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
Robert Richter | 259a83a | 2009-07-09 15:12:35 +0200 | [diff] [blame] | 529 | struct op_x86_model_spec op_amd_spec = { |
Robert Richter | da169f5 | 2010-09-24 15:54:43 +0200 | [diff] [blame] | 530 | /* num_counters/num_controls filled in at runtime */ |
Robert Richter | 3370d35 | 2009-05-25 15:10:32 +0200 | [diff] [blame] | 531 | .reserved = MSR_AMD_EVENTSEL_RESERVED, |
| 532 | .event_mask = OP_EVENT_MASK, |
| 533 | .init = op_amd_init, |
Robert Richter | c92960f | 2008-09-05 17:12:36 +0200 | [diff] [blame] | 534 | .fill_in_addresses = &op_amd_fill_in_addresses, |
| 535 | .setup_ctrs = &op_amd_setup_ctrs, |
| 536 | .check_ctrs = &op_amd_check_ctrs, |
| 537 | .start = &op_amd_start, |
| 538 | .stop = &op_amd_stop, |
Robert Richter | 3370d35 | 2009-05-25 15:10:32 +0200 | [diff] [blame] | 539 | .shutdown = &op_amd_shutdown, |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 540 | #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX |
Robert Richter | 7e7478c | 2009-07-16 13:09:53 +0200 | [diff] [blame] | 541 | .switch_ctrl = &op_mux_switch_ctrl, |
Jason Yeh | 4d4036e | 2009-07-08 13:49:38 +0200 | [diff] [blame] | 542 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | }; |