blob: a011bcc0f94331d82c8abfa7d4afdbbd0c59eff5 [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>
27#include <asm/cpufeature.h>
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "op_x86_model.h"
30#include "op_counter.h"
31
Robert Richter4c168ea2008-09-24 11:08:52 +020032#define NUM_COUNTERS 4
Jason Yeh4d4036e2009-07-08 13:49:38 +020033#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
34#define NUM_VIRT_COUNTERS 32
Jason Yeh4d4036e2009-07-08 13:49:38 +020035#else
36#define NUM_VIRT_COUNTERS NUM_COUNTERS
Jason Yeh4d4036e2009-07-08 13:49:38 +020037#endif
38
Robert Richter3370d352009-05-25 15:10:32 +020039#define OP_EVENT_MASK 0x0FFF
Robert Richter42399ad2009-05-25 17:59:06 +020040#define OP_CTR_OVERFLOW (1ULL<<31)
Robert Richter3370d352009-05-25 15:10:32 +020041
42#define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Jason Yeh4d4036e2009-07-08 13:49:38 +020044static unsigned long reset_value[NUM_VIRT_COUNTERS];
Robert Richter852402c2008-07-22 21:09:06 +020045
Robert Richterc572ae42009-06-03 20:10:39 +020046#define IBS_FETCH_SIZE 6
47#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020048
Robert Richter64683da2010-02-04 10:57:23 +010049static u32 ibs_caps;
Barry Kasindorf56784f12008-07-22 21:08:55 +020050
Robert Richter53b39e92010-09-21 17:58:15 +020051struct ibs_config {
Barry Kasindorf56784f12008-07-22 21:08:55 +020052 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 Richter25da6952010-09-21 15:49:31 +020058 unsigned long branch_target;
Barry Kasindorf56784f12008-07-22 21:08:55 +020059};
60
Robert Richter53b39e92010-09-21 17:58:15 +020061struct ibs_state {
Robert Richter25da6952010-09-21 15:49:31 +020062 u64 ibs_op_ctl;
63 int branch_target;
64 unsigned long sample_size;
Robert Richter53b39e92010-09-21 17:58:15 +020065};
66
67static struct ibs_config ibs_config;
68static struct ibs_state ibs_state;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010069
Robert Richter64683da2010-02-04 10:57:23 +010070/*
71 * IBS cpuid feature detection
72 */
73
Robert Richter27afdf22010-10-06 12:27:54 +020074#define IBS_CPUID_FEATURES 0x8000001b
Robert Richter64683da2010-02-04 10:57:23 +010075
76/*
77 * Same bit mask as for IBS cpuid feature flags (Fn8000_001B_EAX), but
78 * bit 0 is used to indicate the existence of IBS.
79 */
Robert Richter27afdf22010-10-06 12:27:54 +020080#define IBS_CAPS_AVAIL (1U<<0)
Robert Richter4ac945f2010-09-21 15:58:32 +020081#define IBS_CAPS_FETCHSAM (1U<<1)
82#define IBS_CAPS_OPSAM (1U<<2)
Robert Richter27afdf22010-10-06 12:27:54 +020083#define IBS_CAPS_RDWROPCNT (1U<<3)
84#define IBS_CAPS_OPCNT (1U<<4)
Robert Richter25da6952010-09-21 15:49:31 +020085#define IBS_CAPS_BRNTRGT (1U<<5)
Robert Richterb47fad32010-09-22 17:45:39 +020086#define IBS_CAPS_OPCNTEXT (1U<<6)
Robert Richter4ac945f2010-09-21 15:58:32 +020087
88#define IBS_CAPS_DEFAULT (IBS_CAPS_AVAIL \
89 | IBS_CAPS_FETCHSAM \
90 | IBS_CAPS_OPSAM)
Robert Richter27afdf22010-10-06 12:27:54 +020091
92/*
93 * IBS APIC setup
94 */
95#define IBSCTL 0x1cc
96#define IBSCTL_LVT_OFFSET_VALID (1ULL<<8)
97#define IBSCTL_LVT_OFFSET_MASK 0x0F
Robert Richter64683da2010-02-04 10:57:23 +010098
Robert Richterba520782010-02-23 15:46:49 +010099/*
100 * IBS randomization macros
101 */
102#define IBS_RANDOM_BITS 12
103#define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1)
104#define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5))
105
Robert Richter64683da2010-02-04 10:57:23 +0100106static u32 get_ibs_caps(void)
107{
108 u32 ibs_caps;
109 unsigned int max_level;
110
111 if (!boot_cpu_has(X86_FEATURE_IBS))
112 return 0;
113
114 /* check IBS cpuid feature flags */
115 max_level = cpuid_eax(0x80000000);
116 if (max_level < IBS_CPUID_FEATURES)
Robert Richter4ac945f2010-09-21 15:58:32 +0200117 return IBS_CAPS_DEFAULT;
Robert Richter64683da2010-02-04 10:57:23 +0100118
119 ibs_caps = cpuid_eax(IBS_CPUID_FEATURES);
120 if (!(ibs_caps & IBS_CAPS_AVAIL))
121 /* cpuid flags not valid */
Robert Richter4ac945f2010-09-21 15:58:32 +0200122 return IBS_CAPS_DEFAULT;
Robert Richter64683da2010-02-04 10:57:23 +0100123
124 return ibs_caps;
125}
126
Suravee Suthikulpanitf125be12010-01-18 11:25:45 -0600127/*
128 * 16-bit Linear Feedback Shift Register (LFSR)
129 *
130 * 16 14 13 11
131 * Feedback polynomial = X + X + X + X + 1
132 */
133static unsigned int lfsr_random(void)
134{
135 static unsigned int lfsr_value = 0xF00D;
136 unsigned int bit;
137
138 /* Compute next bit to shift in */
139 bit = ((lfsr_value >> 0) ^
140 (lfsr_value >> 2) ^
141 (lfsr_value >> 3) ^
142 (lfsr_value >> 5)) & 0x0001;
143
144 /* Advance to next register value */
145 lfsr_value = (lfsr_value >> 1) | (bit << 15);
146
147 return lfsr_value;
148}
149
Robert Richterba520782010-02-23 15:46:49 +0100150/*
151 * IBS software randomization
152 *
153 * The IBS periodic op counter is randomized in software. The lower 12
154 * bits of the 20 bit counter are randomized. IbsOpCurCnt is
155 * initialized with a 12 bit random value.
156 */
157static inline u64 op_amd_randomize_ibs_op(u64 val)
158{
159 unsigned int random = lfsr_random();
160
161 if (!(ibs_caps & IBS_CAPS_RDWROPCNT))
162 /*
163 * Work around if the hw can not write to IbsOpCurCnt
164 *
165 * Randomize the lower 8 bits of the 16 bit
166 * IbsOpMaxCnt [15:0] value in the range of -128 to
167 * +127 by adding/subtracting an offset to the
168 * maximum count (IbsOpMaxCnt).
169 *
170 * To avoid over or underflows and protect upper bits
171 * starting at bit 16, the initial value for
172 * IbsOpMaxCnt must fit in the range from 0x0081 to
173 * 0xff80.
174 */
175 val += (s8)(random >> 4);
176 else
177 val |= (u64)(random & IBS_RANDOM_MASK) << 32;
178
179 return val;
180}
181
Andrew Morton4680e642009-06-23 12:36:08 -0700182static inline void
Robert Richter7939d2b2008-07-22 21:08:56 +0200183op_amd_handle_ibs(struct pt_regs * const regs,
184 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Robert Richterc572ae42009-06-03 20:10:39 +0200186 u64 val, ctl;
Robert Richter1acda872009-01-05 10:35:31 +0100187 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Robert Richter64683da2010-02-04 10:57:23 +0100189 if (!ibs_caps)
Andrew Morton4680e642009-06-23 12:36:08 -0700190 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Robert Richter7939d2b2008-07-22 21:08:56 +0200192 if (ibs_config.fetch_enabled) {
Robert Richterc572ae42009-06-03 20:10:39 +0200193 rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
194 if (ctl & IBS_FETCH_VAL) {
195 rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
196 oprofile_write_reserve(&entry, regs, val,
Robert Richter14f0ca82009-01-07 21:50:22 +0100197 IBS_FETCH_CODE, IBS_FETCH_SIZE);
Robert Richter51563a02009-06-03 20:54:56 +0200198 oprofile_add_data64(&entry, val);
199 oprofile_add_data64(&entry, ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200200 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200201 oprofile_add_data64(&entry, val);
Robert Richter14f0ca82009-01-07 21:50:22 +0100202 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200203
Robert Richterfd13f6c2008-10-19 21:00:09 +0200204 /* reenable the IRQ */
Robert Richtera163b102010-02-25 19:43:07 +0100205 ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT);
Robert Richterc572ae42009-06-03 20:10:39 +0200206 ctl |= IBS_FETCH_ENABLE;
207 wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200208 }
209 }
210
Robert Richter7939d2b2008-07-22 21:08:56 +0200211 if (ibs_config.op_enabled) {
Robert Richterc572ae42009-06-03 20:10:39 +0200212 rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
213 if (ctl & IBS_OP_VAL) {
214 rdmsrl(MSR_AMD64_IBSOPRIP, val);
Robert Richter25da6952010-09-21 15:49:31 +0200215 oprofile_write_reserve(&entry, regs, val, IBS_OP_CODE,
216 ibs_state.sample_size);
Robert Richter51563a02009-06-03 20:54:56 +0200217 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200218 rdmsrl(MSR_AMD64_IBSOPDATA, val);
Robert Richter51563a02009-06-03 20:54:56 +0200219 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200220 rdmsrl(MSR_AMD64_IBSOPDATA2, val);
Robert Richter51563a02009-06-03 20:54:56 +0200221 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200222 rdmsrl(MSR_AMD64_IBSOPDATA3, val);
Robert Richter51563a02009-06-03 20:54:56 +0200223 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200224 rdmsrl(MSR_AMD64_IBSDCLINAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200225 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200226 rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200227 oprofile_add_data64(&entry, val);
Robert Richter25da6952010-09-21 15:49:31 +0200228 if (ibs_state.branch_target) {
229 rdmsrl(MSR_AMD64_IBSBRTARGET, val);
230 oprofile_add_data(&entry, (unsigned long)val);
231 }
Robert Richter14f0ca82009-01-07 21:50:22 +0100232 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200233
234 /* reenable the IRQ */
Robert Richter53b39e92010-09-21 17:58:15 +0200235 ctl = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200236 wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200237 }
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Robert Richter90637592009-03-10 19:15:57 +0100241static inline void op_amd_start_ibs(void)
242{
Robert Richterc572ae42009-06-03 20:10:39 +0200243 u64 val;
Robert Richter64683da2010-02-04 10:57:23 +0100244
245 if (!ibs_caps)
246 return;
247
Robert Richter53b39e92010-09-21 17:58:15 +0200248 memset(&ibs_state, 0, sizeof(ibs_state));
249
Robert Richterb47fad32010-09-22 17:45:39 +0200250 /*
251 * Note: Since the max count settings may out of range we
252 * write back the actual used values so that userland can read
253 * it.
254 */
255
Robert Richter64683da2010-02-04 10:57:23 +0100256 if (ibs_config.fetch_enabled) {
Robert Richterb47fad32010-09-22 17:45:39 +0200257 val = ibs_config.max_cnt_fetch >> 4;
258 val = min(val, IBS_FETCH_MAX_CNT);
259 ibs_config.max_cnt_fetch = val << 4;
Robert Richterc572ae42009-06-03 20:10:39 +0200260 val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
261 val |= IBS_FETCH_ENABLE;
262 wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
Robert Richter90637592009-03-10 19:15:57 +0100263 }
264
Robert Richter64683da2010-02-04 10:57:23 +0100265 if (ibs_config.op_enabled) {
Robert Richter53b39e92010-09-21 17:58:15 +0200266 val = ibs_config.max_cnt_op >> 4;
Robert Richterba520782010-02-23 15:46:49 +0100267 if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) {
268 /*
269 * IbsOpCurCnt not supported. See
270 * op_amd_randomize_ibs_op() for details.
271 */
Robert Richter53b39e92010-09-21 17:58:15 +0200272 val = clamp(val, 0x0081ULL, 0xFF80ULL);
Robert Richterb47fad32010-09-22 17:45:39 +0200273 ibs_config.max_cnt_op = val << 4;
Robert Richterba520782010-02-23 15:46:49 +0100274 } else {
275 /*
276 * The start value is randomized with a
277 * positive offset, we need to compensate it
278 * with the half of the randomized range. Also
279 * avoid underflows.
280 */
Robert Richterb47fad32010-09-22 17:45:39 +0200281 val += IBS_RANDOM_MAXCNT_OFFSET;
282 if (ibs_caps & IBS_CAPS_OPCNTEXT)
283 val = min(val, IBS_OP_MAX_CNT_EXT);
284 else
285 val = min(val, IBS_OP_MAX_CNT);
286 ibs_config.max_cnt_op =
287 (val - IBS_RANDOM_MAXCNT_OFFSET) << 4;
Robert Richterba520782010-02-23 15:46:49 +0100288 }
Robert Richterb47fad32010-09-22 17:45:39 +0200289 val = ((val & ~IBS_OP_MAX_CNT) << 4) | (val & IBS_OP_MAX_CNT);
Robert Richter53b39e92010-09-21 17:58:15 +0200290 val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
291 val |= IBS_OP_ENABLE;
292 ibs_state.ibs_op_ctl = val;
Robert Richter25da6952010-09-21 15:49:31 +0200293 ibs_state.sample_size = IBS_OP_SIZE;
294 if (ibs_config.branch_target) {
295 ibs_state.branch_target = 1;
296 ibs_state.sample_size++;
297 }
Robert Richter53b39e92010-09-21 17:58:15 +0200298 val = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200299 wrmsrl(MSR_AMD64_IBSOPCTL, val);
Robert Richter90637592009-03-10 19:15:57 +0100300 }
301}
302
303static void op_amd_stop_ibs(void)
304{
Robert Richter64683da2010-02-04 10:57:23 +0100305 if (!ibs_caps)
306 return;
307
308 if (ibs_config.fetch_enabled)
Robert Richter90637592009-03-10 19:15:57 +0100309 /* clear max count and enable */
Robert Richterc572ae42009-06-03 20:10:39 +0200310 wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
Robert Richter90637592009-03-10 19:15:57 +0100311
Robert Richter64683da2010-02-04 10:57:23 +0100312 if (ibs_config.op_enabled)
Robert Richter90637592009-03-10 19:15:57 +0100313 /* clear max count and enable */
Robert Richterc572ae42009-06-03 20:10:39 +0200314 wrmsrl(MSR_AMD64_IBSOPCTL, 0);
Robert Richter90637592009-03-10 19:15:57 +0100315}
316
Robert Richter27afdf22010-10-06 12:27:54 +0200317static inline int eilvt_is_available(int offset)
318{
319 /* check if we may assign a vector */
320 return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
321}
322
323static inline int ibs_eilvt_valid(void)
324{
Robert Richter27afdf22010-10-06 12:27:54 +0200325 int offset;
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200326 u64 val;
Robert Richter27afdf22010-10-06 12:27:54 +0200327
328 rdmsrl(MSR_AMD64_IBSCTL, val);
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200329 offset = val & IBSCTL_LVT_OFFSET_MASK;
330
Robert Richter27afdf22010-10-06 12:27:54 +0200331 if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200332 pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
333 smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
Robert Richter27afdf22010-10-06 12:27:54 +0200334 return 0;
335 }
336
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200337 if (!eilvt_is_available(offset)) {
338 pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
339 smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
340 return 0;
341 }
Robert Richter27afdf22010-10-06 12:27:54 +0200342
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200343 return 1;
Robert Richter27afdf22010-10-06 12:27:54 +0200344}
345
346static inline int get_ibs_offset(void)
347{
348 u64 val;
349
350 rdmsrl(MSR_AMD64_IBSCTL, val);
351 if (!(val & IBSCTL_LVT_OFFSET_VALID))
352 return -EINVAL;
353
354 return val & IBSCTL_LVT_OFFSET_MASK;
355}
356
357static void setup_APIC_ibs(void)
358{
359 int offset;
360
361 offset = get_ibs_offset();
362 if (offset < 0)
363 goto failed;
364
365 if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
366 return;
367failed:
368 pr_warn("oprofile: IBS APIC setup failed on cpu #%d\n",
369 smp_processor_id());
370}
371
372static void clear_APIC_ibs(void)
373{
374 int offset;
375
376 offset = get_ibs_offset();
377 if (offset >= 0)
378 setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
379}
380
Robert Richterda759fe2010-02-26 10:54:56 +0100381#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
382
383static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
384 struct op_msrs const * const msrs)
385{
386 u64 val;
387 int i;
388
389 /* enable active counters */
390 for (i = 0; i < NUM_COUNTERS; ++i) {
391 int virt = op_x86_phys_to_virt(i);
392 if (!reset_value[virt])
393 continue;
394 rdmsrl(msrs->controls[i].addr, val);
395 val &= model->reserved;
396 val |= op_x86_get_ctrl(model, &counter_config[virt]);
397 wrmsrl(msrs->controls[i].addr, val);
398 }
399}
400
401#endif
402
403/* functions for op_amd_spec */
404
405static void op_amd_shutdown(struct op_msrs const * const msrs)
406{
407 int i;
408
409 for (i = 0; i < NUM_COUNTERS; ++i) {
410 if (!msrs->counters[i].addr)
411 continue;
412 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
413 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
414 }
415}
416
417static int op_amd_fill_in_addresses(struct op_msrs * const msrs)
418{
419 int i;
420
421 for (i = 0; i < NUM_COUNTERS; i++) {
422 if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
423 goto fail;
424 if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) {
425 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
426 goto fail;
427 }
428 /* both registers must be reserved */
429 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
430 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
431 continue;
432 fail:
433 if (!counter_config[i].enabled)
434 continue;
435 op_x86_warn_reserved(i);
436 op_amd_shutdown(msrs);
437 return -EBUSY;
438 }
439
440 return 0;
441}
442
443static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
444 struct op_msrs const * const msrs)
445{
446 u64 val;
447 int i;
448
449 /* setup reset_value */
450 for (i = 0; i < NUM_VIRT_COUNTERS; ++i) {
451 if (counter_config[i].enabled
452 && msrs->counters[op_x86_virt_to_phys(i)].addr)
453 reset_value[i] = counter_config[i].count;
454 else
455 reset_value[i] = 0;
456 }
457
458 /* clear all counters */
459 for (i = 0; i < NUM_COUNTERS; ++i) {
460 if (!msrs->controls[i].addr)
461 continue;
462 rdmsrl(msrs->controls[i].addr, val);
463 if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
464 op_x86_warn_in_use(i);
465 val &= model->reserved;
466 wrmsrl(msrs->controls[i].addr, val);
467 /*
468 * avoid a false detection of ctr overflows in NMI
469 * handler
470 */
471 wrmsrl(msrs->counters[i].addr, -1LL);
472 }
473
474 /* enable active counters */
475 for (i = 0; i < NUM_COUNTERS; ++i) {
476 int virt = op_x86_phys_to_virt(i);
477 if (!reset_value[virt])
478 continue;
479
480 /* setup counter registers */
481 wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
482
483 /* setup control registers */
484 rdmsrl(msrs->controls[i].addr, val);
485 val &= model->reserved;
486 val |= op_x86_get_ctrl(model, &counter_config[virt]);
487 wrmsrl(msrs->controls[i].addr, val);
488 }
Robert Richterbae663b2010-05-05 17:47:17 +0200489
490 if (ibs_caps)
Robert Richter27afdf22010-10-06 12:27:54 +0200491 setup_APIC_ibs();
Robert Richterbae663b2010-05-05 17:47:17 +0200492}
493
494static void op_amd_cpu_shutdown(void)
495{
496 if (ibs_caps)
Robert Richter27afdf22010-10-06 12:27:54 +0200497 clear_APIC_ibs();
Robert Richterda759fe2010-02-26 10:54:56 +0100498}
499
Robert Richter7939d2b2008-07-22 21:08:56 +0200500static int op_amd_check_ctrs(struct pt_regs * const regs,
501 struct op_msrs const * const msrs)
502{
Robert Richter42399ad2009-05-25 17:59:06 +0200503 u64 val;
Robert Richter7939d2b2008-07-22 21:08:56 +0200504 int i;
505
Robert Richter6e63ea42009-07-07 19:25:39 +0200506 for (i = 0; i < NUM_COUNTERS; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200507 int virt = op_x86_phys_to_virt(i);
508 if (!reset_value[virt])
Robert Richter7939d2b2008-07-22 21:08:56 +0200509 continue;
Robert Richter42399ad2009-05-25 17:59:06 +0200510 rdmsrl(msrs->counters[i].addr, val);
511 /* bit is clear if overflowed: */
512 if (val & OP_CTR_OVERFLOW)
513 continue;
Robert Richterd8471ad2009-07-16 13:04:43 +0200514 oprofile_add_sample(regs, virt);
515 wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
Robert Richter7939d2b2008-07-22 21:08:56 +0200516 }
517
518 op_amd_handle_ibs(regs, msrs);
519
520 /* See op_model_ppro.c */
521 return 1;
522}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100523
Robert Richter6657fe42008-07-22 21:08:50 +0200524static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Robert Richterdea37662009-05-25 18:11:52 +0200526 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int i;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200528
Robert Richter6e63ea42009-07-07 19:25:39 +0200529 for (i = 0; i < NUM_COUNTERS; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200530 if (!reset_value[op_x86_phys_to_virt(i)])
531 continue;
532 rdmsrl(msrs->controls[i].addr, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100533 val |= ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richterd8471ad2009-07-16 13:04:43 +0200534 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Robert Richter852402c2008-07-22 21:09:06 +0200536
Robert Richter90637592009-03-10 19:15:57 +0100537 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Robert Richter6657fe42008-07-22 21:08:50 +0200540static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Robert Richterdea37662009-05-25 18:11:52 +0200542 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int i;
544
Robert Richterfd13f6c2008-10-19 21:00:09 +0200545 /*
546 * Subtle: stop on all counters to avoid race with setting our
547 * pm callback
548 */
Robert Richter6e63ea42009-07-07 19:25:39 +0200549 for (i = 0; i < NUM_COUNTERS; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200550 if (!reset_value[op_x86_phys_to_virt(i)])
Don Zickuscb9c4482006-09-26 10:52:26 +0200551 continue;
Robert Richterdea37662009-05-25 18:11:52 +0200552 rdmsrl(msrs->controls[i].addr, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100553 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richterdea37662009-05-25 18:11:52 +0200554 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200556
Robert Richter90637592009-03-10 19:15:57 +0100557 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Robert Richter27afdf22010-10-06 12:27:54 +0200560static int setup_ibs_ctl(int ibs_eilvt_off)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200561{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200562 struct pci_dev *cpu_cfg;
563 int nodes;
564 u32 value = 0;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200565
566 nodes = 0;
567 cpu_cfg = NULL;
568 do {
569 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
570 PCI_DEVICE_ID_AMD_10H_NB_MISC,
571 cpu_cfg);
572 if (!cpu_cfg)
573 break;
574 ++nodes;
575 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
Robert Richter27afdf22010-10-06 12:27:54 +0200576 | IBSCTL_LVT_OFFSET_VALID);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200577 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
Robert Richter27afdf22010-10-06 12:27:54 +0200578 if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100579 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200580 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
Robert Richter27afdf22010-10-06 12:27:54 +0200581 "IBSCTL = 0x%08x\n", value);
582 return -EINVAL;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200583 }
584 } while (1);
585
586 if (!nodes) {
Robert Richter27afdf22010-10-06 12:27:54 +0200587 printk(KERN_DEBUG "No CPU node configured for IBS\n");
588 return -ENODEV;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200589 }
590
Robert Richter7d77f2d2008-07-22 21:08:57 +0200591 return 0;
592}
593
Robert Richter27afdf22010-10-06 12:27:54 +0200594static int force_ibs_eilvt_setup(void)
595{
596 int i;
597 int ret;
598
599 /* find the next free available EILVT entry */
600 for (i = 1; i < 4; i++) {
601 if (!eilvt_is_available(i))
602 continue;
603 ret = setup_ibs_ctl(i);
604 if (ret)
605 return ret;
606 return 0;
607 }
608
609 printk(KERN_DEBUG "No EILVT entry available\n");
610
611 return -EBUSY;
612}
613
614static int __init_ibs_nmi(void)
615{
616 int ret;
617
618 if (ibs_eilvt_valid())
619 return 0;
620
621 ret = force_ibs_eilvt_setup();
622 if (ret)
623 return ret;
624
625 if (!ibs_eilvt_valid())
626 return -EFAULT;
627
628 pr_err(FW_BUG "workaround enabled for IBS LVT offset\n");
629
630 return 0;
631}
632
Robert Richterfd13f6c2008-10-19 21:00:09 +0200633/* initialize the APIC for the IBS interrupts if available */
Robert Richterbae663b2010-05-05 17:47:17 +0200634static void init_ibs(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200635{
Robert Richter64683da2010-02-04 10:57:23 +0100636 ibs_caps = get_ibs_caps();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200637
Robert Richter64683da2010-02-04 10:57:23 +0100638 if (!ibs_caps)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200639 return;
640
Robert Richterbae663b2010-05-05 17:47:17 +0200641 if (__init_ibs_nmi()) {
Robert Richter64683da2010-02-04 10:57:23 +0100642 ibs_caps = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200643 return;
644 }
645
Robert Richter64683da2010-02-04 10:57:23 +0100646 printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n",
647 (unsigned)ibs_caps);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200648}
649
Robert Richter25ad29132008-09-05 17:12:36 +0200650static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200651
Robert Richter25ad29132008-09-05 17:12:36 +0200652static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200653{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200654 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200655 int ret = 0;
656
657 /* architecture specific files */
658 if (create_arch_files)
659 ret = create_arch_files(sb, root);
660
661 if (ret)
662 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200663
Robert Richter64683da2010-02-04 10:57:23 +0100664 if (!ibs_caps)
Robert Richter270d3e12008-07-22 21:09:01 +0200665 return ret;
666
667 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200668
669 /* setup some reasonable defaults */
Robert Richter25da6952010-09-21 15:49:31 +0200670 memset(&ibs_config, 0, sizeof(ibs_config));
Barry Kasindorf56784f12008-07-22 21:08:55 +0200671 ibs_config.max_cnt_fetch = 250000;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200672 ibs_config.max_cnt_op = 250000;
Robert Richter2d55a472008-07-18 17:56:05 +0200673
Robert Richter4ac945f2010-09-21 15:58:32 +0200674 if (ibs_caps & IBS_CAPS_FETCHSAM) {
675 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
676 oprofilefs_create_ulong(sb, dir, "enable",
677 &ibs_config.fetch_enabled);
678 oprofilefs_create_ulong(sb, dir, "max_count",
679 &ibs_config.max_cnt_fetch);
680 oprofilefs_create_ulong(sb, dir, "rand_enable",
681 &ibs_config.rand_en);
682 }
Robert Richter2d55a472008-07-18 17:56:05 +0200683
Robert Richter4ac945f2010-09-21 15:58:32 +0200684 if (ibs_caps & IBS_CAPS_OPSAM) {
685 dir = oprofilefs_mkdir(sb, root, "ibs_op");
686 oprofilefs_create_ulong(sb, dir, "enable",
687 &ibs_config.op_enabled);
688 oprofilefs_create_ulong(sb, dir, "max_count",
689 &ibs_config.max_cnt_op);
690 if (ibs_caps & IBS_CAPS_OPCNT)
691 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
692 &ibs_config.dispatched_ops);
Robert Richter25da6952010-09-21 15:49:31 +0200693 if (ibs_caps & IBS_CAPS_BRNTRGT)
694 oprofilefs_create_ulong(sb, dir, "branch_target",
695 &ibs_config.branch_target);
Robert Richter4ac945f2010-09-21 15:58:32 +0200696 }
Robert Richterfc2bd732008-07-22 21:09:00 +0200697
698 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200699}
700
Robert Richteradf5ec02008-07-22 21:08:48 +0200701static int op_amd_init(struct oprofile_operations *ops)
702{
Robert Richterbae663b2010-05-05 17:47:17 +0200703 init_ibs();
Robert Richter270d3e12008-07-22 21:09:01 +0200704 create_arch_files = ops->create_files;
705 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200706 return 0;
707}
708
Robert Richter259a83a2009-07-09 15:12:35 +0200709struct op_x86_model_spec op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200710 .num_counters = NUM_COUNTERS,
Robert Richterd0e41202010-03-23 19:33:21 +0100711 .num_controls = NUM_COUNTERS,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200712 .num_virt_counters = NUM_VIRT_COUNTERS,
Robert Richter3370d352009-05-25 15:10:32 +0200713 .reserved = MSR_AMD_EVENTSEL_RESERVED,
714 .event_mask = OP_EVENT_MASK,
715 .init = op_amd_init,
Robert Richterc92960f2008-09-05 17:12:36 +0200716 .fill_in_addresses = &op_amd_fill_in_addresses,
717 .setup_ctrs = &op_amd_setup_ctrs,
Robert Richterbae663b2010-05-05 17:47:17 +0200718 .cpu_down = &op_amd_cpu_shutdown,
Robert Richterc92960f2008-09-05 17:12:36 +0200719 .check_ctrs = &op_amd_check_ctrs,
720 .start = &op_amd_start,
721 .stop = &op_amd_stop,
Robert Richter3370d352009-05-25 15:10:32 +0200722 .shutdown = &op_amd_shutdown,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200723#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
Robert Richter7e7478c2009-07-16 13:09:53 +0200724 .switch_ctrl = &op_mux_switch_ctrl,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726};