blob: 660a83c8287b61dc52c069832128a83f256258af [file] [log] [blame]
Paolo Ciarrocchid4413732008-02-19 23:51:27 +01001/*
Robert Richter6852fd92008-07-22 21:09:08 +02002 * @file op_model_amd.c
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +01003 * athlon / K7 / K8 / Family 10h model-specific MSR operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Robert Richterae735e92008-12-25 17:26:07 +01005 * @remark Copyright 2002-2009 OProfile authors
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * @remark Read the file COPYING
7 *
8 * @author John Levon
9 * @author Philippe Elie
10 * @author Graydon Hoare
Robert Richteradf5ec02008-07-22 21:08:48 +020011 * @author Robert Richter <robert.richter@amd.com>
Jason Yeh4d4036e2009-07-08 13:49:38 +020012 * @author Barry Kasindorf <barry.kasindorf@amd.com>
13 * @author Jason Yeh <jason.yeh@amd.com>
14 * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
Robert Richterae735e92008-12-25 17:26:07 +010015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/oprofile.h>
Barry Kasindorf56784f12008-07-22 21:08:55 +020018#include <linux/device.h>
19#include <linux/pci.h>
Jason Yeh4d4036e2009-07-08 13:49:38 +020020#include <linux/percpu.h>
Barry Kasindorf56784f12008-07-22 21:08:55 +020021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/ptrace.h>
23#include <asm/msr.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020024#include <asm/nmi.h>
Robert Richter013cfc52010-01-28 18:05:26 +010025#include <asm/apic.h>
Robert Richter64683da2010-02-04 10:57:23 +010026#include <asm/processor.h>
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "op_x86_model.h"
29#include "op_counter.h"
30
Jason Yeh4d4036e2009-07-08 13:49:38 +020031#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
Robert Richterda169f52010-09-24 15:54:43 +020032#define NUM_VIRT_COUNTERS 32
Jason Yeh4d4036e2009-07-08 13:49:38 +020033#else
Robert Richterda169f52010-09-24 15:54:43 +020034#define NUM_VIRT_COUNTERS 0
Jason Yeh4d4036e2009-07-08 13:49:38 +020035#endif
36
Robert Richter3370d352009-05-25 15:10:32 +020037#define OP_EVENT_MASK 0x0FFF
Robert Richter42399ad2009-05-25 17:59:06 +020038#define OP_CTR_OVERFLOW (1ULL<<31)
Robert Richter3370d352009-05-25 15:10:32 +020039
40#define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Robert Richterda169f52010-09-24 15:54:43 +020042static int num_counters;
43static unsigned long reset_value[OP_MAX_COUNTER];
Robert Richter852402c2008-07-22 21:09:06 +020044
Robert Richterc572ae42009-06-03 20:10:39 +020045#define IBS_FETCH_SIZE 6
46#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020047
Robert Richter64683da2010-02-04 10:57:23 +010048static u32 ibs_caps;
Barry Kasindorf56784f12008-07-22 21:08:55 +020049
Robert Richter53b39e92010-09-21 17:58:15 +020050struct ibs_config {
Barry Kasindorf56784f12008-07-22 21:08:55 +020051 unsigned long op_enabled;
52 unsigned long fetch_enabled;
53 unsigned long max_cnt_fetch;
54 unsigned long max_cnt_op;
55 unsigned long rand_en;
56 unsigned long dispatched_ops;
Robert Richter25da6952010-09-21 15:49:31 +020057 unsigned long branch_target;
Barry Kasindorf56784f12008-07-22 21:08:55 +020058};
59
Robert Richter53b39e92010-09-21 17:58:15 +020060struct ibs_state {
Robert Richter25da6952010-09-21 15:49:31 +020061 u64 ibs_op_ctl;
62 int branch_target;
63 unsigned long sample_size;
Robert Richter53b39e92010-09-21 17:58:15 +020064};
65
66static struct ibs_config ibs_config;
67static struct ibs_state ibs_state;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010068
Robert Richter64683da2010-02-04 10:57:23 +010069/*
Robert Richterba520782010-02-23 15:46:49 +010070 * IBS randomization macros
71 */
72#define IBS_RANDOM_BITS 12
73#define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1)
74#define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5))
75
Suravee Suthikulpanitf125be12010-01-18 11:25:45 -060076/*
77 * 16-bit Linear Feedback Shift Register (LFSR)
78 *
79 * 16 14 13 11
80 * Feedback polynomial = X + X + X + X + 1
81 */
82static unsigned int lfsr_random(void)
83{
84 static unsigned int lfsr_value = 0xF00D;
85 unsigned int bit;
86
87 /* Compute next bit to shift in */
88 bit = ((lfsr_value >> 0) ^
89 (lfsr_value >> 2) ^
90 (lfsr_value >> 3) ^
91 (lfsr_value >> 5)) & 0x0001;
92
93 /* Advance to next register value */
94 lfsr_value = (lfsr_value >> 1) | (bit << 15);
95
96 return lfsr_value;
97}
98
Robert Richterba520782010-02-23 15:46:49 +010099/*
100 * IBS software randomization
101 *
102 * The IBS periodic op counter is randomized in software. The lower 12
103 * bits of the 20 bit counter are randomized. IbsOpCurCnt is
104 * initialized with a 12 bit random value.
105 */
106static inline u64 op_amd_randomize_ibs_op(u64 val)
107{
108 unsigned int random = lfsr_random();
109
110 if (!(ibs_caps & IBS_CAPS_RDWROPCNT))
111 /*
112 * Work around if the hw can not write to IbsOpCurCnt
113 *
114 * Randomize the lower 8 bits of the 16 bit
115 * IbsOpMaxCnt [15:0] value in the range of -128 to
116 * +127 by adding/subtracting an offset to the
117 * maximum count (IbsOpMaxCnt).
118 *
119 * To avoid over or underflows and protect upper bits
120 * starting at bit 16, the initial value for
121 * IbsOpMaxCnt must fit in the range from 0x0081 to
122 * 0xff80.
123 */
124 val += (s8)(random >> 4);
125 else
126 val |= (u64)(random & IBS_RANDOM_MASK) << 32;
127
128 return val;
129}
130
Andrew Morton4680e642009-06-23 12:36:08 -0700131static inline void
Robert Richter7939d2b2008-07-22 21:08:56 +0200132op_amd_handle_ibs(struct pt_regs * const regs,
133 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Robert Richterc572ae42009-06-03 20:10:39 +0200135 u64 val, ctl;
Robert Richter1acda872009-01-05 10:35:31 +0100136 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Robert Richter64683da2010-02-04 10:57:23 +0100138 if (!ibs_caps)
Andrew Morton4680e642009-06-23 12:36:08 -0700139 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Robert Richter7939d2b2008-07-22 21:08:56 +0200141 if (ibs_config.fetch_enabled) {
Robert Richterc572ae42009-06-03 20:10:39 +0200142 rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
143 if (ctl & IBS_FETCH_VAL) {
144 rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
145 oprofile_write_reserve(&entry, regs, val,
Robert Richter14f0ca82009-01-07 21:50:22 +0100146 IBS_FETCH_CODE, IBS_FETCH_SIZE);
Robert Richter51563a02009-06-03 20:54:56 +0200147 oprofile_add_data64(&entry, val);
148 oprofile_add_data64(&entry, ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200149 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200150 oprofile_add_data64(&entry, val);
Robert Richter14f0ca82009-01-07 21:50:22 +0100151 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200152
Robert Richterfd13f6c2008-10-19 21:00:09 +0200153 /* reenable the IRQ */
Robert Richtera163b102010-02-25 19:43:07 +0100154 ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT);
Robert Richterc572ae42009-06-03 20:10:39 +0200155 ctl |= IBS_FETCH_ENABLE;
156 wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200157 }
158 }
159
Robert Richter7939d2b2008-07-22 21:08:56 +0200160 if (ibs_config.op_enabled) {
Robert Richterc572ae42009-06-03 20:10:39 +0200161 rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
162 if (ctl & IBS_OP_VAL) {
163 rdmsrl(MSR_AMD64_IBSOPRIP, val);
Robert Richter25da6952010-09-21 15:49:31 +0200164 oprofile_write_reserve(&entry, regs, val, IBS_OP_CODE,
165 ibs_state.sample_size);
Robert Richter51563a02009-06-03 20:54:56 +0200166 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200167 rdmsrl(MSR_AMD64_IBSOPDATA, val);
Robert Richter51563a02009-06-03 20:54:56 +0200168 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200169 rdmsrl(MSR_AMD64_IBSOPDATA2, val);
Robert Richter51563a02009-06-03 20:54:56 +0200170 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200171 rdmsrl(MSR_AMD64_IBSOPDATA3, val);
Robert Richter51563a02009-06-03 20:54:56 +0200172 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200173 rdmsrl(MSR_AMD64_IBSDCLINAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200174 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200175 rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200176 oprofile_add_data64(&entry, val);
Robert Richter25da6952010-09-21 15:49:31 +0200177 if (ibs_state.branch_target) {
178 rdmsrl(MSR_AMD64_IBSBRTARGET, val);
179 oprofile_add_data(&entry, (unsigned long)val);
180 }
Robert Richter14f0ca82009-01-07 21:50:22 +0100181 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200182
183 /* reenable the IRQ */
Robert Richter53b39e92010-09-21 17:58:15 +0200184 ctl = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200185 wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200186 }
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Robert Richter90637592009-03-10 19:15:57 +0100190static inline void op_amd_start_ibs(void)
191{
Robert Richterc572ae42009-06-03 20:10:39 +0200192 u64 val;
Robert Richter64683da2010-02-04 10:57:23 +0100193
194 if (!ibs_caps)
195 return;
196
Robert Richter53b39e92010-09-21 17:58:15 +0200197 memset(&ibs_state, 0, sizeof(ibs_state));
198
Robert Richterb47fad32010-09-22 17:45:39 +0200199 /*
200 * Note: Since the max count settings may out of range we
201 * write back the actual used values so that userland can read
202 * it.
203 */
204
Robert Richter64683da2010-02-04 10:57:23 +0100205 if (ibs_config.fetch_enabled) {
Robert Richterb47fad32010-09-22 17:45:39 +0200206 val = ibs_config.max_cnt_fetch >> 4;
207 val = min(val, IBS_FETCH_MAX_CNT);
208 ibs_config.max_cnt_fetch = val << 4;
Robert Richterc572ae42009-06-03 20:10:39 +0200209 val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
210 val |= IBS_FETCH_ENABLE;
211 wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
Robert Richter90637592009-03-10 19:15:57 +0100212 }
213
Robert Richter64683da2010-02-04 10:57:23 +0100214 if (ibs_config.op_enabled) {
Robert Richter53b39e92010-09-21 17:58:15 +0200215 val = ibs_config.max_cnt_op >> 4;
Robert Richterba520782010-02-23 15:46:49 +0100216 if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) {
217 /*
218 * IbsOpCurCnt not supported. See
219 * op_amd_randomize_ibs_op() for details.
220 */
Robert Richter53b39e92010-09-21 17:58:15 +0200221 val = clamp(val, 0x0081ULL, 0xFF80ULL);
Robert Richterb47fad32010-09-22 17:45:39 +0200222 ibs_config.max_cnt_op = val << 4;
Robert Richterba520782010-02-23 15:46:49 +0100223 } else {
224 /*
225 * The start value is randomized with a
226 * positive offset, we need to compensate it
227 * with the half of the randomized range. Also
228 * avoid underflows.
229 */
Robert Richterb47fad32010-09-22 17:45:39 +0200230 val += IBS_RANDOM_MAXCNT_OFFSET;
231 if (ibs_caps & IBS_CAPS_OPCNTEXT)
232 val = min(val, IBS_OP_MAX_CNT_EXT);
233 else
234 val = min(val, IBS_OP_MAX_CNT);
235 ibs_config.max_cnt_op =
236 (val - IBS_RANDOM_MAXCNT_OFFSET) << 4;
Robert Richterba520782010-02-23 15:46:49 +0100237 }
Robert Richterb47fad32010-09-22 17:45:39 +0200238 val = ((val & ~IBS_OP_MAX_CNT) << 4) | (val & IBS_OP_MAX_CNT);
Robert Richter53b39e92010-09-21 17:58:15 +0200239 val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
240 val |= IBS_OP_ENABLE;
241 ibs_state.ibs_op_ctl = val;
Robert Richter25da6952010-09-21 15:49:31 +0200242 ibs_state.sample_size = IBS_OP_SIZE;
243 if (ibs_config.branch_target) {
244 ibs_state.branch_target = 1;
245 ibs_state.sample_size++;
246 }
Robert Richter53b39e92010-09-21 17:58:15 +0200247 val = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200248 wrmsrl(MSR_AMD64_IBSOPCTL, val);
Robert Richter90637592009-03-10 19:15:57 +0100249 }
250}
251
252static void op_amd_stop_ibs(void)
253{
Robert Richter64683da2010-02-04 10:57:23 +0100254 if (!ibs_caps)
255 return;
256
257 if (ibs_config.fetch_enabled)
Robert Richter90637592009-03-10 19:15:57 +0100258 /* clear max count and enable */
Robert Richterc572ae42009-06-03 20:10:39 +0200259 wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
Robert Richter90637592009-03-10 19:15:57 +0100260
Robert Richter64683da2010-02-04 10:57:23 +0100261 if (ibs_config.op_enabled)
Robert Richter90637592009-03-10 19:15:57 +0100262 /* clear max count and enable */
Robert Richterc572ae42009-06-03 20:10:39 +0200263 wrmsrl(MSR_AMD64_IBSOPCTL, 0);
Robert Richter90637592009-03-10 19:15:57 +0100264}
265
Robert Richterda759fe2010-02-26 10:54:56 +0100266#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
267
268static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
269 struct op_msrs const * const msrs)
270{
271 u64 val;
272 int i;
273
274 /* enable active counters */
Robert Richterda169f52010-09-24 15:54:43 +0200275 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100276 int virt = op_x86_phys_to_virt(i);
277 if (!reset_value[virt])
278 continue;
279 rdmsrl(msrs->controls[i].addr, val);
280 val &= model->reserved;
281 val |= op_x86_get_ctrl(model, &counter_config[virt]);
282 wrmsrl(msrs->controls[i].addr, val);
283 }
284}
285
286#endif
287
288/* functions for op_amd_spec */
289
290static void op_amd_shutdown(struct op_msrs const * const msrs)
291{
292 int i;
293
Robert Richterda169f52010-09-24 15:54:43 +0200294 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100295 if (!msrs->counters[i].addr)
296 continue;
297 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
298 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
299 }
300}
301
302static int op_amd_fill_in_addresses(struct op_msrs * const msrs)
303{
304 int i;
305
Robert Richterda169f52010-09-24 15:54:43 +0200306 for (i = 0; i < num_counters; i++) {
Robert Richterda759fe2010-02-26 10:54:56 +0100307 if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
308 goto fail;
309 if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) {
310 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
311 goto fail;
312 }
313 /* both registers must be reserved */
Robert Richterb1dc3c42012-06-20 20:46:35 +0200314 if (num_counters == AMD64_NUM_COUNTERS_CORE) {
Robert Richterda169f52010-09-24 15:54:43 +0200315 msrs->counters[i].addr = MSR_F15H_PERF_CTR + (i << 1);
316 msrs->controls[i].addr = MSR_F15H_PERF_CTL + (i << 1);
317 } else {
318 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
319 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
320 }
Robert Richterda759fe2010-02-26 10:54:56 +0100321 continue;
322 fail:
323 if (!counter_config[i].enabled)
324 continue;
325 op_x86_warn_reserved(i);
326 op_amd_shutdown(msrs);
327 return -EBUSY;
328 }
329
330 return 0;
331}
332
333static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
334 struct op_msrs const * const msrs)
335{
336 u64 val;
337 int i;
338
339 /* setup reset_value */
Robert Richterda169f52010-09-24 15:54:43 +0200340 for (i = 0; i < OP_MAX_COUNTER; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100341 if (counter_config[i].enabled
342 && msrs->counters[op_x86_virt_to_phys(i)].addr)
343 reset_value[i] = counter_config[i].count;
344 else
345 reset_value[i] = 0;
346 }
347
348 /* clear all counters */
Robert Richterda169f52010-09-24 15:54:43 +0200349 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100350 if (!msrs->controls[i].addr)
351 continue;
352 rdmsrl(msrs->controls[i].addr, val);
353 if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
354 op_x86_warn_in_use(i);
355 val &= model->reserved;
356 wrmsrl(msrs->controls[i].addr, val);
357 /*
358 * avoid a false detection of ctr overflows in NMI
359 * handler
360 */
361 wrmsrl(msrs->counters[i].addr, -1LL);
362 }
363
364 /* enable active counters */
Robert Richterda169f52010-09-24 15:54:43 +0200365 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100366 int virt = op_x86_phys_to_virt(i);
367 if (!reset_value[virt])
368 continue;
369
370 /* setup counter registers */
371 wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
372
373 /* setup control registers */
374 rdmsrl(msrs->controls[i].addr, val);
375 val &= model->reserved;
376 val |= op_x86_get_ctrl(model, &counter_config[virt]);
377 wrmsrl(msrs->controls[i].addr, val);
378 }
379}
380
Robert Richter7939d2b2008-07-22 21:08:56 +0200381static int op_amd_check_ctrs(struct pt_regs * const regs,
382 struct op_msrs const * const msrs)
383{
Robert Richter42399ad2009-05-25 17:59:06 +0200384 u64 val;
Robert Richter7939d2b2008-07-22 21:08:56 +0200385 int i;
386
Robert Richterda169f52010-09-24 15:54:43 +0200387 for (i = 0; i < num_counters; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200388 int virt = op_x86_phys_to_virt(i);
389 if (!reset_value[virt])
Robert Richter7939d2b2008-07-22 21:08:56 +0200390 continue;
Robert Richter42399ad2009-05-25 17:59:06 +0200391 rdmsrl(msrs->counters[i].addr, val);
392 /* bit is clear if overflowed: */
393 if (val & OP_CTR_OVERFLOW)
394 continue;
Robert Richterd8471ad2009-07-16 13:04:43 +0200395 oprofile_add_sample(regs, virt);
396 wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
Robert Richter7939d2b2008-07-22 21:08:56 +0200397 }
398
399 op_amd_handle_ibs(regs, msrs);
400
401 /* See op_model_ppro.c */
402 return 1;
403}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100404
Robert Richter6657fe42008-07-22 21:08:50 +0200405static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Robert Richterdea37662009-05-25 18:11:52 +0200407 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int i;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200409
Robert Richterda169f52010-09-24 15:54:43 +0200410 for (i = 0; i < num_counters; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200411 if (!reset_value[op_x86_phys_to_virt(i)])
412 continue;
413 rdmsrl(msrs->controls[i].addr, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100414 val |= ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richterd8471ad2009-07-16 13:04:43 +0200415 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Robert Richter852402c2008-07-22 21:09:06 +0200417
Robert Richter90637592009-03-10 19:15:57 +0100418 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Robert Richter6657fe42008-07-22 21:08:50 +0200421static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Robert Richterdea37662009-05-25 18:11:52 +0200423 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 int i;
425
Robert Richterfd13f6c2008-10-19 21:00:09 +0200426 /*
427 * Subtle: stop on all counters to avoid race with setting our
428 * pm callback
429 */
Robert Richterda169f52010-09-24 15:54:43 +0200430 for (i = 0; i < num_counters; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200431 if (!reset_value[op_x86_phys_to_virt(i)])
Don Zickuscb9c4482006-09-26 10:52:26 +0200432 continue;
Robert Richterdea37662009-05-25 18:11:52 +0200433 rdmsrl(msrs->controls[i].addr, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100434 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richterdea37662009-05-25 18:11:52 +0200435 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200437
Robert Richter90637592009-03-10 19:15:57 +0100438 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Robert Richterc7c25802011-01-03 12:15:14 +0100441/*
442 * check and reserve APIC extended interrupt LVT offset for IBS if
443 * available
Robert Richterc7c25802011-01-03 12:15:14 +0100444 */
445
Robert Richterbae663b2010-05-05 17:47:17 +0200446static void init_ibs(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200447{
Robert Richter64683da2010-02-04 10:57:23 +0100448 ibs_caps = get_ibs_caps();
Robert Richter3d2606f2011-05-20 09:46:54 +0200449
Robert Richter64683da2010-02-04 10:57:23 +0100450 if (!ibs_caps)
Robert Richter3d2606f2011-05-20 09:46:54 +0200451 return;
452
Robert Richter3d2606f2011-05-20 09:46:54 +0200453 printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n", ibs_caps);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200454}
455
Al Viroef7bca12013-07-19 15:52:42 +0400456static int (*create_arch_files)(struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200457
Al Viroef7bca12013-07-19 15:52:42 +0400458static int setup_ibs_files(struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200459{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200460 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200461 int ret = 0;
462
463 /* architecture specific files */
464 if (create_arch_files)
Al Viroef7bca12013-07-19 15:52:42 +0400465 ret = create_arch_files(root);
Robert Richter270d3e12008-07-22 21:09:01 +0200466
467 if (ret)
468 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200469
Robert Richter64683da2010-02-04 10:57:23 +0100470 if (!ibs_caps)
Robert Richter270d3e12008-07-22 21:09:01 +0200471 return ret;
472
473 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200474
475 /* setup some reasonable defaults */
Robert Richter25da6952010-09-21 15:49:31 +0200476 memset(&ibs_config, 0, sizeof(ibs_config));
Barry Kasindorf56784f12008-07-22 21:08:55 +0200477 ibs_config.max_cnt_fetch = 250000;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200478 ibs_config.max_cnt_op = 250000;
Robert Richter2d55a472008-07-18 17:56:05 +0200479
Robert Richter4ac945f2010-09-21 15:58:32 +0200480 if (ibs_caps & IBS_CAPS_FETCHSAM) {
Al Viroecde2822013-07-19 15:58:27 +0400481 dir = oprofilefs_mkdir(root, "ibs_fetch");
Al Viro6af4ea02013-07-19 16:10:36 +0400482 oprofilefs_create_ulong(dir, "enable",
Robert Richter4ac945f2010-09-21 15:58:32 +0200483 &ibs_config.fetch_enabled);
Al Viro6af4ea02013-07-19 16:10:36 +0400484 oprofilefs_create_ulong(dir, "max_count",
Robert Richter4ac945f2010-09-21 15:58:32 +0200485 &ibs_config.max_cnt_fetch);
Al Viro6af4ea02013-07-19 16:10:36 +0400486 oprofilefs_create_ulong(dir, "rand_enable",
Robert Richter4ac945f2010-09-21 15:58:32 +0200487 &ibs_config.rand_en);
488 }
Robert Richter2d55a472008-07-18 17:56:05 +0200489
Robert Richter4ac945f2010-09-21 15:58:32 +0200490 if (ibs_caps & IBS_CAPS_OPSAM) {
Al Viroecde2822013-07-19 15:58:27 +0400491 dir = oprofilefs_mkdir(root, "ibs_op");
Al Viro6af4ea02013-07-19 16:10:36 +0400492 oprofilefs_create_ulong(dir, "enable",
Robert Richter4ac945f2010-09-21 15:58:32 +0200493 &ibs_config.op_enabled);
Al Viro6af4ea02013-07-19 16:10:36 +0400494 oprofilefs_create_ulong(dir, "max_count",
Robert Richter4ac945f2010-09-21 15:58:32 +0200495 &ibs_config.max_cnt_op);
496 if (ibs_caps & IBS_CAPS_OPCNT)
Al Viro6af4ea02013-07-19 16:10:36 +0400497 oprofilefs_create_ulong(dir, "dispatched_ops",
Robert Richter4ac945f2010-09-21 15:58:32 +0200498 &ibs_config.dispatched_ops);
Robert Richter25da6952010-09-21 15:49:31 +0200499 if (ibs_caps & IBS_CAPS_BRNTRGT)
Al Viro6af4ea02013-07-19 16:10:36 +0400500 oprofilefs_create_ulong(dir, "branch_target",
Robert Richter25da6952010-09-21 15:49:31 +0200501 &ibs_config.branch_target);
Robert Richter4ac945f2010-09-21 15:58:32 +0200502 }
Robert Richterfc2bd732008-07-22 21:09:00 +0200503
504 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200505}
506
Robert Richterda169f52010-09-24 15:54:43 +0200507struct op_x86_model_spec op_amd_spec;
508
Robert Richteradf5ec02008-07-22 21:08:48 +0200509static int op_amd_init(struct oprofile_operations *ops)
510{
Robert Richterbae663b2010-05-05 17:47:17 +0200511 init_ibs();
Robert Richter270d3e12008-07-22 21:09:01 +0200512 create_arch_files = ops->create_files;
513 ops->create_files = setup_ibs_files;
Robert Richterda169f52010-09-24 15:54:43 +0200514
515 if (boot_cpu_data.x86 == 0x15) {
Robert Richterb1dc3c42012-06-20 20:46:35 +0200516 num_counters = AMD64_NUM_COUNTERS_CORE;
Robert Richterda169f52010-09-24 15:54:43 +0200517 } else {
Robert Richteree5789d2011-09-21 11:30:17 +0200518 num_counters = AMD64_NUM_COUNTERS;
Robert Richterda169f52010-09-24 15:54:43 +0200519 }
520
521 op_amd_spec.num_counters = num_counters;
522 op_amd_spec.num_controls = num_counters;
523 op_amd_spec.num_virt_counters = max(num_counters, NUM_VIRT_COUNTERS);
524
Robert Richteradf5ec02008-07-22 21:08:48 +0200525 return 0;
526}
527
Robert Richter259a83a2009-07-09 15:12:35 +0200528struct op_x86_model_spec op_amd_spec = {
Robert Richterda169f52010-09-24 15:54:43 +0200529 /* num_counters/num_controls filled in at runtime */
Robert Richter3370d352009-05-25 15:10:32 +0200530 .reserved = MSR_AMD_EVENTSEL_RESERVED,
531 .event_mask = OP_EVENT_MASK,
532 .init = op_amd_init,
Robert Richterc92960f2008-09-05 17:12:36 +0200533 .fill_in_addresses = &op_amd_fill_in_addresses,
534 .setup_ctrs = &op_amd_setup_ctrs,
535 .check_ctrs = &op_amd_check_ctrs,
536 .start = &op_amd_start,
537 .stop = &op_amd_stop,
Robert Richter3370d352009-05-25 15:10:32 +0200538 .shutdown = &op_amd_shutdown,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200539#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
Robert Richter7e7478c2009-07-16 13:09:53 +0200540 .switch_ctrl = &op_mux_switch_ctrl,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200541#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542};