Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 1 | /* |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 2 | * @file op_model_athlon.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 | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 5 | * @remark Copyright 2002-2008 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> |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 12 | * @author Barry Kasindorf |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #include <linux/oprofile.h> |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 16 | #include <linux/device.h> |
| 17 | #include <linux/pci.h> |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/ptrace.h> |
| 20 | #include <asm/msr.h> |
Don Zickus | 3e4ff11 | 2006-06-26 13:57:01 +0200 | [diff] [blame] | 21 | #include <asm/nmi.h> |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +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 | |
| 26 | #define NUM_COUNTERS 4 |
| 27 | #define NUM_CONTROLS 4 |
| 28 | |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 29 | #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0) |
| 30 | #define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0) |
| 31 | #define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #define CTR_OVERFLOWED(n) (!((n) & (1U<<31))) |
| 33 | |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 34 | #define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0) |
| 35 | #define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0) |
| 36 | #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] | 37 | #define CTRL_SET_ACTIVE(n) (n |= (1<<22)) |
| 38 | #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22)) |
Barry Kasindorf | bd87f1f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 39 | #define CTRL_CLEAR_LO(x) (x &= (1<<21)) |
| 40 | #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define CTRL_SET_ENABLE(val) (val |= 1<<20) |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 42 | #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16)) |
| 43 | #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define CTRL_SET_UM(val, m) (val |= (m << 8)) |
Barry Kasindorf | bd87f1f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 45 | #define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff)) |
| 46 | #define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf)) |
| 47 | #define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9)) |
| 48 | #define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 50 | #define IBS_FETCH_CTL_HIGH_MASK 0xFFFFFFFF |
| 51 | /* high dword bit IbsFetchCtl[bit 49] */ |
| 52 | #define IBS_FETCH_VALID_BIT (1UL << 17) |
| 53 | /* high dword bit IbsFetchCtl[bit 52] */ |
| 54 | #define IBS_FETCH_PHY_ADDR_VALID_BIT (1UL << 20) |
| 55 | /* high dword bit IbsFetchCtl[bit 48] */ |
| 56 | #define IBS_FETCH_ENABLE (1UL << 16) |
| 57 | |
| 58 | #define IBS_FETCH_CTL_CNT_MASK 0x00000000FFFF0000UL |
| 59 | #define IBS_FETCH_CTL_MAX_CNT_MASK 0x000000000000FFFFUL |
| 60 | |
| 61 | /*IbsOpCtl masks/bits */ |
| 62 | #define IBS_OP_VALID_BIT (1ULL<<18) /* IbsOpCtl[bit18] */ |
| 63 | #define IBS_OP_ENABLE (1ULL<<17) /* IBS_OP_ENABLE[bit17]*/ |
| 64 | |
| 65 | /* Codes used in cpu_buffer.c */ |
| 66 | #define IBS_FETCH_BEGIN 3 |
| 67 | #define IBS_OP_BEGIN 4 |
| 68 | |
| 69 | /*IbsOpData3 masks */ |
| 70 | #define IBS_CTL_LVT_OFFSET_VALID_BIT (1ULL<<8) |
| 71 | |
| 72 | /*PCI Extended Configuration Constants */ |
| 73 | /* MSR to set the IBS control register APIC LVT offset */ |
| 74 | #define IBS_LVT_OFFSET_PCI 0x1CC |
| 75 | |
| 76 | struct ibs_fetch_sample { |
| 77 | /* MSRC001_1031 IBS Fetch Linear Address Register */ |
| 78 | unsigned int ibs_fetch_lin_addr_low; |
| 79 | unsigned int ibs_fetch_lin_addr_high; |
| 80 | /* MSRC001_1030 IBS Fetch Control Register */ |
| 81 | unsigned int ibs_fetch_ctl_low; |
| 82 | unsigned int ibs_fetch_ctl_high; |
| 83 | /* MSRC001_1032 IBS Fetch Physical Address Register */ |
| 84 | unsigned int ibs_fetch_phys_addr_low; |
| 85 | unsigned int ibs_fetch_phys_addr_high; |
| 86 | }; |
| 87 | |
| 88 | struct ibs_op_sample { |
| 89 | /* MSRC001_1034 IBS Op Logical Address Register (IbsRIP) */ |
| 90 | unsigned int ibs_op_rip_low; |
| 91 | unsigned int ibs_op_rip_high; |
| 92 | /* MSRC001_1035 IBS Op Data Register */ |
| 93 | unsigned int ibs_op_data1_low; |
| 94 | unsigned int ibs_op_data1_high; |
| 95 | /* MSRC001_1036 IBS Op Data 2 Register */ |
| 96 | unsigned int ibs_op_data2_low; |
| 97 | unsigned int ibs_op_data2_high; |
| 98 | /* MSRC001_1037 IBS Op Data 3 Register */ |
| 99 | unsigned int ibs_op_data3_low; |
| 100 | unsigned int ibs_op_data3_high; |
| 101 | /* MSRC001_1038 IBS DC Linear Address Register (IbsDcLinAd) */ |
| 102 | unsigned int ibs_dc_linear_low; |
| 103 | unsigned int ibs_dc_linear_high; |
| 104 | /* MSRC001_1039 IBS DC Physical Address Register (IbsDcPhysAd) */ |
| 105 | unsigned int ibs_dc_phys_low; |
| 106 | unsigned int ibs_dc_phys_high; |
| 107 | }; |
| 108 | |
| 109 | /* |
| 110 | * unitialize the APIC for the IBS interrupts if needed on AMD Family10h+ |
| 111 | */ |
| 112 | static void clear_ibs_nmi(void); |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | static unsigned long reset_value[NUM_COUNTERS]; |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 115 | static int ibs_allowed; /* AMD Family10h and later */ |
| 116 | |
| 117 | struct op_ibs_config { |
| 118 | unsigned long op_enabled; |
| 119 | unsigned long fetch_enabled; |
| 120 | unsigned long max_cnt_fetch; |
| 121 | unsigned long max_cnt_op; |
| 122 | unsigned long rand_en; |
| 123 | unsigned long dispatched_ops; |
| 124 | }; |
| 125 | |
| 126 | static struct op_ibs_config ibs_config; |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 127 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 128 | /* functions for op_amd_spec */ |
Robert Richter | dfa1542 | 2008-07-22 21:08:49 +0200 | [diff] [blame] | 129 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 130 | static void op_amd_fill_in_addresses(struct op_msrs * const msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 132 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 134 | for (i = 0; i < NUM_COUNTERS; i++) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 135 | if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i)) |
| 136 | msrs->counters[i].addr = MSR_K7_PERFCTR0 + i; |
| 137 | else |
| 138 | msrs->counters[i].addr = 0; |
| 139 | } |
| 140 | |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 141 | for (i = 0; i < NUM_CONTROLS; i++) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 142 | if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) |
| 143 | msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i; |
| 144 | else |
| 145 | msrs->controls[i].addr = 0; |
| 146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 149 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 150 | static void op_amd_setup_ctrs(struct op_msrs const * const msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | unsigned int low, high; |
| 153 | int i; |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* clear all counters */ |
| 156 | for (i = 0 ; i < NUM_CONTROLS; ++i) { |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 157 | if (unlikely(!CTRL_IS_RESERVED(msrs, i))) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 158 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | CTRL_READ(low, high, msrs, i); |
Barry Kasindorf | bd87f1f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 160 | CTRL_CLEAR_LO(low); |
| 161 | CTRL_CLEAR_HI(high); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | CTRL_WRITE(low, high, msrs, i); |
| 163 | } |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | /* avoid a false detection of ctr overflows in NMI handler */ |
| 166 | for (i = 0; i < NUM_COUNTERS; ++i) { |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 167 | if (unlikely(!CTR_IS_RESERVED(msrs, i))) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 168 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | CTR_WRITE(1, msrs, i); |
| 170 | } |
| 171 | |
| 172 | /* enable active counters */ |
| 173 | for (i = 0; i < NUM_COUNTERS; ++i) { |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 174 | if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | reset_value[i] = counter_config[i].count; |
| 176 | |
| 177 | CTR_WRITE(counter_config[i].count, msrs, i); |
| 178 | |
| 179 | CTRL_READ(low, high, msrs, i); |
Barry Kasindorf | bd87f1f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 180 | CTRL_CLEAR_LO(low); |
| 181 | CTRL_CLEAR_HI(high); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | CTRL_SET_ENABLE(low); |
| 183 | CTRL_SET_USR(low, counter_config[i].user); |
| 184 | CTRL_SET_KERN(low, counter_config[i].kernel); |
| 185 | CTRL_SET_UM(low, counter_config[i].unit_mask); |
Barry Kasindorf | bd87f1f | 2007-12-18 18:05:58 +0100 | [diff] [blame] | 186 | CTRL_SET_EVENT_LOW(low, counter_config[i].event); |
| 187 | CTRL_SET_EVENT_HIGH(high, counter_config[i].event); |
| 188 | CTRL_SET_HOST_ONLY(high, 0); |
| 189 | CTRL_SET_GUEST_ONLY(high, 0); |
| 190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | CTRL_WRITE(low, high, msrs, i); |
| 192 | } else { |
| 193 | reset_value[i] = 0; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 198 | static inline int |
| 199 | op_amd_handle_ibs(struct pt_regs * const regs, |
| 200 | struct op_msrs const * const msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
| 202 | unsigned int low, high; |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 203 | struct ibs_fetch_sample ibs_fetch; |
| 204 | struct ibs_op_sample ibs_op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 206 | if (!ibs_allowed) |
| 207 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 209 | if (ibs_config.fetch_enabled) { |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 210 | rdmsr(MSR_AMD64_IBSFETCHCTL, low, high); |
| 211 | if (high & IBS_FETCH_VALID_BIT) { |
| 212 | ibs_fetch.ibs_fetch_ctl_high = high; |
| 213 | ibs_fetch.ibs_fetch_ctl_low = low; |
| 214 | rdmsr(MSR_AMD64_IBSFETCHLINAD, low, high); |
| 215 | ibs_fetch.ibs_fetch_lin_addr_high = high; |
| 216 | ibs_fetch.ibs_fetch_lin_addr_low = low; |
| 217 | rdmsr(MSR_AMD64_IBSFETCHPHYSAD, low, high); |
| 218 | ibs_fetch.ibs_fetch_phys_addr_high = high; |
| 219 | ibs_fetch.ibs_fetch_phys_addr_low = low; |
| 220 | |
| 221 | oprofile_add_ibs_sample(regs, |
| 222 | (unsigned int *)&ibs_fetch, |
| 223 | IBS_FETCH_BEGIN); |
| 224 | |
| 225 | /*reenable the IRQ */ |
| 226 | rdmsr(MSR_AMD64_IBSFETCHCTL, low, high); |
| 227 | high &= ~(IBS_FETCH_VALID_BIT); |
| 228 | high |= IBS_FETCH_ENABLE; |
| 229 | low &= IBS_FETCH_CTL_MAX_CNT_MASK; |
| 230 | wrmsr(MSR_AMD64_IBSFETCHCTL, low, high); |
| 231 | } |
| 232 | } |
| 233 | |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 234 | if (ibs_config.op_enabled) { |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 235 | rdmsr(MSR_AMD64_IBSOPCTL, low, high); |
| 236 | if (low & IBS_OP_VALID_BIT) { |
| 237 | rdmsr(MSR_AMD64_IBSOPRIP, low, high); |
| 238 | ibs_op.ibs_op_rip_low = low; |
| 239 | ibs_op.ibs_op_rip_high = high; |
| 240 | rdmsr(MSR_AMD64_IBSOPDATA, low, high); |
| 241 | ibs_op.ibs_op_data1_low = low; |
| 242 | ibs_op.ibs_op_data1_high = high; |
| 243 | rdmsr(MSR_AMD64_IBSOPDATA2, low, high); |
| 244 | ibs_op.ibs_op_data2_low = low; |
| 245 | ibs_op.ibs_op_data2_high = high; |
| 246 | rdmsr(MSR_AMD64_IBSOPDATA3, low, high); |
| 247 | ibs_op.ibs_op_data3_low = low; |
| 248 | ibs_op.ibs_op_data3_high = high; |
| 249 | rdmsr(MSR_AMD64_IBSDCLINAD, low, high); |
| 250 | ibs_op.ibs_dc_linear_low = low; |
| 251 | ibs_op.ibs_dc_linear_high = high; |
| 252 | rdmsr(MSR_AMD64_IBSDCPHYSAD, low, high); |
| 253 | ibs_op.ibs_dc_phys_low = low; |
| 254 | ibs_op.ibs_dc_phys_high = high; |
| 255 | |
| 256 | /* reenable the IRQ */ |
| 257 | oprofile_add_ibs_sample(regs, |
| 258 | (unsigned int *)&ibs_op, |
| 259 | IBS_OP_BEGIN); |
| 260 | rdmsr(MSR_AMD64_IBSOPCTL, low, high); |
| 261 | low &= ~(IBS_OP_VALID_BIT); |
| 262 | low |= IBS_OP_ENABLE; |
| 263 | wrmsr(MSR_AMD64_IBSOPCTL, low, high); |
| 264 | } |
| 265 | } |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | return 1; |
| 268 | } |
| 269 | |
Robert Richter | 7939d2b | 2008-07-22 21:08:56 +0200 | [diff] [blame] | 270 | static int op_amd_check_ctrs(struct pt_regs * const regs, |
| 271 | struct op_msrs const * const msrs) |
| 272 | { |
| 273 | unsigned int low, high; |
| 274 | int i; |
| 275 | |
| 276 | for (i = 0 ; i < NUM_COUNTERS; ++i) { |
| 277 | if (!reset_value[i]) |
| 278 | continue; |
| 279 | CTR_READ(low, high, msrs, i); |
| 280 | if (CTR_OVERFLOWED(low)) { |
| 281 | oprofile_add_sample(regs, i); |
| 282 | CTR_WRITE(reset_value[i], msrs, i); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | op_amd_handle_ibs(regs, msrs); |
| 287 | |
| 288 | /* See op_model_ppro.c */ |
| 289 | return 1; |
| 290 | } |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 291 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 292 | static void op_amd_start(struct op_msrs const * const msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
| 294 | unsigned int low, high; |
| 295 | int i; |
| 296 | for (i = 0 ; i < NUM_COUNTERS ; ++i) { |
| 297 | if (reset_value[i]) { |
| 298 | CTRL_READ(low, high, msrs, i); |
| 299 | CTRL_SET_ACTIVE(low); |
| 300 | CTRL_WRITE(low, high, msrs, i); |
| 301 | } |
| 302 | } |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 303 | if (ibs_allowed && ibs_config.fetch_enabled) { |
| 304 | low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF; |
| 305 | high = IBS_FETCH_ENABLE; |
| 306 | wrmsr(MSR_AMD64_IBSFETCHCTL, low, high); |
| 307 | } |
| 308 | |
| 309 | if (ibs_allowed && ibs_config.op_enabled) { |
| 310 | low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF) + IBS_OP_ENABLE; |
| 311 | high = 0; |
| 312 | wrmsr(MSR_AMD64_IBSOPCTL, low, high); |
| 313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 317 | static void op_amd_stop(struct op_msrs const * const msrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 319 | unsigned int low, high; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | int i; |
| 321 | |
| 322 | /* Subtle: stop on all counters to avoid race with |
| 323 | * setting our pm callback */ |
| 324 | for (i = 0 ; i < NUM_COUNTERS ; ++i) { |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 325 | if (!reset_value[i]) |
| 326 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | CTRL_READ(low, high, msrs, i); |
| 328 | CTRL_SET_INACTIVE(low); |
| 329 | CTRL_WRITE(low, high, msrs, i); |
| 330 | } |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 331 | |
| 332 | if (ibs_allowed && ibs_config.fetch_enabled) { |
| 333 | low = 0; /* clear max count and enable */ |
| 334 | high = 0; |
| 335 | wrmsr(MSR_AMD64_IBSFETCHCTL, low, high); |
| 336 | } |
| 337 | |
| 338 | if (ibs_allowed && ibs_config.op_enabled) { |
| 339 | low = 0; /* clear max count and enable */ |
| 340 | high = 0; |
| 341 | wrmsr(MSR_AMD64_IBSOPCTL, low, high); |
| 342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 345 | static void op_amd_shutdown(struct op_msrs const * const msrs) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 346 | { |
| 347 | int i; |
| 348 | |
| 349 | for (i = 0 ; i < NUM_COUNTERS ; ++i) { |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 350 | if (CTR_IS_RESERVED(msrs, i)) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 351 | release_perfctr_nmi(MSR_K7_PERFCTR0 + i); |
| 352 | } |
| 353 | for (i = 0 ; i < NUM_CONTROLS ; ++i) { |
Paolo Ciarrocchi | d441373 | 2008-02-19 23:51:27 +0100 | [diff] [blame] | 354 | if (CTRL_IS_RESERVED(msrs, i)) |
Don Zickus | cb9c448 | 2006-09-26 10:52:26 +0200 | [diff] [blame] | 355 | release_evntsel_nmi(MSR_K7_EVNTSEL0 + i); |
| 356 | } |
| 357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Robert Richter | 7d77f2d | 2008-07-22 21:08:57 +0200 | [diff] [blame^] | 359 | static u8 ibs_eilvt_off; |
| 360 | |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 361 | static inline void apic_init_ibs_nmi_per_cpu(void *arg) |
| 362 | { |
Robert Richter | 7d77f2d | 2008-07-22 21:08:57 +0200 | [diff] [blame^] | 363 | ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0); |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | static inline void apic_clear_ibs_nmi_per_cpu(void *arg) |
| 367 | { |
| 368 | setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1); |
| 369 | } |
| 370 | |
Robert Richter | 7d77f2d | 2008-07-22 21:08:57 +0200 | [diff] [blame^] | 371 | static int pfm_amd64_setup_eilvt(void) |
| 372 | { |
| 373 | #define IBSCTL_LVTOFFSETVAL (1 << 8) |
| 374 | #define IBSCTL 0x1cc |
| 375 | struct pci_dev *cpu_cfg; |
| 376 | int nodes; |
| 377 | u32 value = 0; |
| 378 | |
| 379 | /* per CPU setup */ |
| 380 | on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 0, 1); |
| 381 | |
| 382 | nodes = 0; |
| 383 | cpu_cfg = NULL; |
| 384 | do { |
| 385 | cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD, |
| 386 | PCI_DEVICE_ID_AMD_10H_NB_MISC, |
| 387 | cpu_cfg); |
| 388 | if (!cpu_cfg) |
| 389 | break; |
| 390 | ++nodes; |
| 391 | pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off |
| 392 | | IBSCTL_LVTOFFSETVAL); |
| 393 | pci_read_config_dword(cpu_cfg, IBSCTL, &value); |
| 394 | if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) { |
| 395 | printk(KERN_DEBUG "Failed to setup IBS LVT offset, " |
| 396 | "IBSCTL = 0x%08x", value); |
| 397 | return 1; |
| 398 | } |
| 399 | } while (1); |
| 400 | |
| 401 | if (!nodes) { |
| 402 | printk(KERN_DEBUG "No CPU node configured for IBS"); |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | #ifdef CONFIG_NUMA |
| 407 | /* Sanity check */ |
| 408 | /* Works only for 64bit with proper numa implementation. */ |
| 409 | if (nodes != num_possible_nodes()) { |
| 410 | printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, " |
| 411 | "found: %d, expected %d", |
| 412 | nodes, num_possible_nodes()); |
| 413 | return 1; |
| 414 | } |
| 415 | #endif |
| 416 | return 0; |
| 417 | } |
| 418 | |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 419 | /* |
| 420 | * initialize the APIC for the IBS interrupts |
Robert Richter | 7d77f2d | 2008-07-22 21:08:57 +0200 | [diff] [blame^] | 421 | * if available (AMD Family10h rev B0 and later) |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 422 | */ |
| 423 | static void setup_ibs(void) |
| 424 | { |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 425 | ibs_allowed = boot_cpu_has(X86_FEATURE_IBS); |
| 426 | |
| 427 | if (!ibs_allowed) |
| 428 | return; |
| 429 | |
Robert Richter | 7d77f2d | 2008-07-22 21:08:57 +0200 | [diff] [blame^] | 430 | if (pfm_amd64_setup_eilvt()) |
| 431 | ibs_allowed = 0; |
Barry Kasindorf | 56784f1 | 2008-07-22 21:08:55 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | |
| 435 | /* |
| 436 | * unitialize the APIC for the IBS interrupts if needed on AMD Family10h |
| 437 | * rev B0 and later */ |
| 438 | static void clear_ibs_nmi(void) |
| 439 | { |
| 440 | if (ibs_allowed) |
| 441 | on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1, 1); |
| 442 | } |
| 443 | |
| 444 | static void setup_ibs_files(struct super_block *sb, struct dentry *root) |
| 445 | { |
| 446 | char buf[12]; |
| 447 | struct dentry *dir; |
| 448 | |
| 449 | if (!ibs_allowed) |
| 450 | return; |
| 451 | |
| 452 | /* setup some reasonable defaults */ |
| 453 | ibs_config.max_cnt_fetch = 250000; |
| 454 | ibs_config.fetch_enabled = 0; |
| 455 | ibs_config.max_cnt_op = 250000; |
| 456 | ibs_config.op_enabled = 0; |
| 457 | ibs_config.dispatched_ops = 1; |
| 458 | snprintf(buf, sizeof(buf), "ibs_fetch"); |
| 459 | dir = oprofilefs_mkdir(sb, root, buf); |
| 460 | oprofilefs_create_ulong(sb, dir, "rand_enable", |
| 461 | &ibs_config.rand_en); |
| 462 | oprofilefs_create_ulong(sb, dir, "enable", |
| 463 | &ibs_config.fetch_enabled); |
| 464 | oprofilefs_create_ulong(sb, dir, "max_count", |
| 465 | &ibs_config.max_cnt_fetch); |
| 466 | snprintf(buf, sizeof(buf), "ibs_uops"); |
| 467 | dir = oprofilefs_mkdir(sb, root, buf); |
| 468 | oprofilefs_create_ulong(sb, dir, "enable", |
| 469 | &ibs_config.op_enabled); |
| 470 | oprofilefs_create_ulong(sb, dir, "max_count", |
| 471 | &ibs_config.max_cnt_op); |
| 472 | oprofilefs_create_ulong(sb, dir, "dispatched_ops", |
| 473 | &ibs_config.dispatched_ops); |
| 474 | } |
| 475 | |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 476 | static int op_amd_init(struct oprofile_operations *ops) |
| 477 | { |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static void op_amd_exit(void) |
| 482 | { |
| 483 | } |
| 484 | |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 485 | struct op_x86_model_spec const op_amd_spec = { |
Robert Richter | adf5ec0 | 2008-07-22 21:08:48 +0200 | [diff] [blame] | 486 | .init = op_amd_init, |
| 487 | .exit = op_amd_exit, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | .num_counters = NUM_COUNTERS, |
| 489 | .num_controls = NUM_CONTROLS, |
Robert Richter | 6657fe4 | 2008-07-22 21:08:50 +0200 | [diff] [blame] | 490 | .fill_in_addresses = &op_amd_fill_in_addresses, |
| 491 | .setup_ctrs = &op_amd_setup_ctrs, |
| 492 | .check_ctrs = &op_amd_check_ctrs, |
| 493 | .start = &op_amd_start, |
| 494 | .stop = &op_amd_stop, |
| 495 | .shutdown = &op_amd_shutdown |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | }; |