blob: f2984d43a6b3bbdf9973cf30146721d98fb8467c [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 Richterda169f52010-09-24 15:54:43 +020032#define NUM_COUNTERS 4
33#define NUM_COUNTERS_F15H 6
Jason Yeh4d4036e2009-07-08 13:49:38 +020034#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
Robert Richterda169f52010-09-24 15:54:43 +020035#define NUM_VIRT_COUNTERS 32
Jason Yeh4d4036e2009-07-08 13:49:38 +020036#else
Robert Richterda169f52010-09-24 15:54:43 +020037#define NUM_VIRT_COUNTERS 0
Jason Yeh4d4036e2009-07-08 13:49:38 +020038#endif
39
Robert Richter3370d352009-05-25 15:10:32 +020040#define OP_EVENT_MASK 0x0FFF
Robert Richter42399ad2009-05-25 17:59:06 +020041#define OP_CTR_OVERFLOW (1ULL<<31)
Robert Richter3370d352009-05-25 15:10:32 +020042
43#define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Robert Richterda169f52010-09-24 15:54:43 +020045static int num_counters;
46static unsigned long reset_value[OP_MAX_COUNTER];
Robert Richter852402c2008-07-22 21:09:06 +020047
Robert Richterc572ae42009-06-03 20:10:39 +020048#define IBS_FETCH_SIZE 6
49#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020050
Robert Richter64683da2010-02-04 10:57:23 +010051static u32 ibs_caps;
Barry Kasindorf56784f12008-07-22 21:08:55 +020052
Robert Richter53b39e92010-09-21 17:58:15 +020053struct ibs_config {
Barry Kasindorf56784f12008-07-22 21:08:55 +020054 unsigned long op_enabled;
55 unsigned long fetch_enabled;
56 unsigned long max_cnt_fetch;
57 unsigned long max_cnt_op;
58 unsigned long rand_en;
59 unsigned long dispatched_ops;
Robert Richter25da6952010-09-21 15:49:31 +020060 unsigned long branch_target;
Barry Kasindorf56784f12008-07-22 21:08:55 +020061};
62
Robert Richter53b39e92010-09-21 17:58:15 +020063struct ibs_state {
Robert Richter25da6952010-09-21 15:49:31 +020064 u64 ibs_op_ctl;
65 int branch_target;
66 unsigned long sample_size;
Robert Richter53b39e92010-09-21 17:58:15 +020067};
68
69static struct ibs_config ibs_config;
70static struct ibs_state ibs_state;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010071
Robert Richter64683da2010-02-04 10:57:23 +010072/*
73 * IBS cpuid feature detection
74 */
75
Robert Richter27afdf22010-10-06 12:27:54 +020076#define IBS_CPUID_FEATURES 0x8000001b
Robert Richter64683da2010-02-04 10:57:23 +010077
78/*
79 * Same bit mask as for IBS cpuid feature flags (Fn8000_001B_EAX), but
80 * bit 0 is used to indicate the existence of IBS.
81 */
Robert Richter27afdf22010-10-06 12:27:54 +020082#define IBS_CAPS_AVAIL (1U<<0)
Robert Richter4ac945f2010-09-21 15:58:32 +020083#define IBS_CAPS_FETCHSAM (1U<<1)
84#define IBS_CAPS_OPSAM (1U<<2)
Robert Richter27afdf22010-10-06 12:27:54 +020085#define IBS_CAPS_RDWROPCNT (1U<<3)
86#define IBS_CAPS_OPCNT (1U<<4)
Robert Richter25da6952010-09-21 15:49:31 +020087#define IBS_CAPS_BRNTRGT (1U<<5)
Robert Richterb47fad32010-09-22 17:45:39 +020088#define IBS_CAPS_OPCNTEXT (1U<<6)
Robert Richter4ac945f2010-09-21 15:58:32 +020089
90#define IBS_CAPS_DEFAULT (IBS_CAPS_AVAIL \
91 | IBS_CAPS_FETCHSAM \
92 | IBS_CAPS_OPSAM)
Robert Richter27afdf22010-10-06 12:27:54 +020093
94/*
95 * IBS APIC setup
96 */
97#define IBSCTL 0x1cc
98#define IBSCTL_LVT_OFFSET_VALID (1ULL<<8)
99#define IBSCTL_LVT_OFFSET_MASK 0x0F
Robert Richter64683da2010-02-04 10:57:23 +0100100
Robert Richterba520782010-02-23 15:46:49 +0100101/*
102 * IBS randomization macros
103 */
104#define IBS_RANDOM_BITS 12
105#define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1)
106#define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5))
107
Robert Richter64683da2010-02-04 10:57:23 +0100108static u32 get_ibs_caps(void)
109{
110 u32 ibs_caps;
111 unsigned int max_level;
112
113 if (!boot_cpu_has(X86_FEATURE_IBS))
114 return 0;
115
116 /* check IBS cpuid feature flags */
117 max_level = cpuid_eax(0x80000000);
118 if (max_level < IBS_CPUID_FEATURES)
Robert Richter4ac945f2010-09-21 15:58:32 +0200119 return IBS_CAPS_DEFAULT;
Robert Richter64683da2010-02-04 10:57:23 +0100120
121 ibs_caps = cpuid_eax(IBS_CPUID_FEATURES);
122 if (!(ibs_caps & IBS_CAPS_AVAIL))
123 /* cpuid flags not valid */
Robert Richter4ac945f2010-09-21 15:58:32 +0200124 return IBS_CAPS_DEFAULT;
Robert Richter64683da2010-02-04 10:57:23 +0100125
126 return ibs_caps;
127}
128
Suravee Suthikulpanitf125be12010-01-18 11:25:45 -0600129/*
130 * 16-bit Linear Feedback Shift Register (LFSR)
131 *
132 * 16 14 13 11
133 * Feedback polynomial = X + X + X + X + 1
134 */
135static unsigned int lfsr_random(void)
136{
137 static unsigned int lfsr_value = 0xF00D;
138 unsigned int bit;
139
140 /* Compute next bit to shift in */
141 bit = ((lfsr_value >> 0) ^
142 (lfsr_value >> 2) ^
143 (lfsr_value >> 3) ^
144 (lfsr_value >> 5)) & 0x0001;
145
146 /* Advance to next register value */
147 lfsr_value = (lfsr_value >> 1) | (bit << 15);
148
149 return lfsr_value;
150}
151
Robert Richterba520782010-02-23 15:46:49 +0100152/*
153 * IBS software randomization
154 *
155 * The IBS periodic op counter is randomized in software. The lower 12
156 * bits of the 20 bit counter are randomized. IbsOpCurCnt is
157 * initialized with a 12 bit random value.
158 */
159static inline u64 op_amd_randomize_ibs_op(u64 val)
160{
161 unsigned int random = lfsr_random();
162
163 if (!(ibs_caps & IBS_CAPS_RDWROPCNT))
164 /*
165 * Work around if the hw can not write to IbsOpCurCnt
166 *
167 * Randomize the lower 8 bits of the 16 bit
168 * IbsOpMaxCnt [15:0] value in the range of -128 to
169 * +127 by adding/subtracting an offset to the
170 * maximum count (IbsOpMaxCnt).
171 *
172 * To avoid over or underflows and protect upper bits
173 * starting at bit 16, the initial value for
174 * IbsOpMaxCnt must fit in the range from 0x0081 to
175 * 0xff80.
176 */
177 val += (s8)(random >> 4);
178 else
179 val |= (u64)(random & IBS_RANDOM_MASK) << 32;
180
181 return val;
182}
183
Andrew Morton4680e642009-06-23 12:36:08 -0700184static inline void
Robert Richter7939d2b2008-07-22 21:08:56 +0200185op_amd_handle_ibs(struct pt_regs * const regs,
186 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Robert Richterc572ae42009-06-03 20:10:39 +0200188 u64 val, ctl;
Robert Richter1acda872009-01-05 10:35:31 +0100189 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Robert Richter64683da2010-02-04 10:57:23 +0100191 if (!ibs_caps)
Andrew Morton4680e642009-06-23 12:36:08 -0700192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Robert Richter7939d2b2008-07-22 21:08:56 +0200194 if (ibs_config.fetch_enabled) {
Robert Richterc572ae42009-06-03 20:10:39 +0200195 rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
196 if (ctl & IBS_FETCH_VAL) {
197 rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
198 oprofile_write_reserve(&entry, regs, val,
Robert Richter14f0ca82009-01-07 21:50:22 +0100199 IBS_FETCH_CODE, IBS_FETCH_SIZE);
Robert Richter51563a02009-06-03 20:54:56 +0200200 oprofile_add_data64(&entry, val);
201 oprofile_add_data64(&entry, ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200202 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200203 oprofile_add_data64(&entry, val);
Robert Richter14f0ca82009-01-07 21:50:22 +0100204 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200205
Robert Richterfd13f6c2008-10-19 21:00:09 +0200206 /* reenable the IRQ */
Robert Richtera163b102010-02-25 19:43:07 +0100207 ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT);
Robert Richterc572ae42009-06-03 20:10:39 +0200208 ctl |= IBS_FETCH_ENABLE;
209 wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200210 }
211 }
212
Robert Richter7939d2b2008-07-22 21:08:56 +0200213 if (ibs_config.op_enabled) {
Robert Richterc572ae42009-06-03 20:10:39 +0200214 rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
215 if (ctl & IBS_OP_VAL) {
216 rdmsrl(MSR_AMD64_IBSOPRIP, val);
Robert Richter25da6952010-09-21 15:49:31 +0200217 oprofile_write_reserve(&entry, regs, val, IBS_OP_CODE,
218 ibs_state.sample_size);
Robert Richter51563a02009-06-03 20:54:56 +0200219 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200220 rdmsrl(MSR_AMD64_IBSOPDATA, 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_IBSOPDATA2, 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_IBSOPDATA3, 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_IBSDCLINAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200227 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200228 rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200229 oprofile_add_data64(&entry, val);
Robert Richter25da6952010-09-21 15:49:31 +0200230 if (ibs_state.branch_target) {
231 rdmsrl(MSR_AMD64_IBSBRTARGET, val);
232 oprofile_add_data(&entry, (unsigned long)val);
233 }
Robert Richter14f0ca82009-01-07 21:50:22 +0100234 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200235
236 /* reenable the IRQ */
Robert Richter53b39e92010-09-21 17:58:15 +0200237 ctl = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200238 wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200239 }
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Robert Richter90637592009-03-10 19:15:57 +0100243static inline void op_amd_start_ibs(void)
244{
Robert Richterc572ae42009-06-03 20:10:39 +0200245 u64 val;
Robert Richter64683da2010-02-04 10:57:23 +0100246
247 if (!ibs_caps)
248 return;
249
Robert Richter53b39e92010-09-21 17:58:15 +0200250 memset(&ibs_state, 0, sizeof(ibs_state));
251
Robert Richterb47fad32010-09-22 17:45:39 +0200252 /*
253 * Note: Since the max count settings may out of range we
254 * write back the actual used values so that userland can read
255 * it.
256 */
257
Robert Richter64683da2010-02-04 10:57:23 +0100258 if (ibs_config.fetch_enabled) {
Robert Richterb47fad32010-09-22 17:45:39 +0200259 val = ibs_config.max_cnt_fetch >> 4;
260 val = min(val, IBS_FETCH_MAX_CNT);
261 ibs_config.max_cnt_fetch = val << 4;
Robert Richterc572ae42009-06-03 20:10:39 +0200262 val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
263 val |= IBS_FETCH_ENABLE;
264 wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
Robert Richter90637592009-03-10 19:15:57 +0100265 }
266
Robert Richter64683da2010-02-04 10:57:23 +0100267 if (ibs_config.op_enabled) {
Robert Richter53b39e92010-09-21 17:58:15 +0200268 val = ibs_config.max_cnt_op >> 4;
Robert Richterba520782010-02-23 15:46:49 +0100269 if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) {
270 /*
271 * IbsOpCurCnt not supported. See
272 * op_amd_randomize_ibs_op() for details.
273 */
Robert Richter53b39e92010-09-21 17:58:15 +0200274 val = clamp(val, 0x0081ULL, 0xFF80ULL);
Robert Richterb47fad32010-09-22 17:45:39 +0200275 ibs_config.max_cnt_op = val << 4;
Robert Richterba520782010-02-23 15:46:49 +0100276 } else {
277 /*
278 * The start value is randomized with a
279 * positive offset, we need to compensate it
280 * with the half of the randomized range. Also
281 * avoid underflows.
282 */
Robert Richterb47fad32010-09-22 17:45:39 +0200283 val += IBS_RANDOM_MAXCNT_OFFSET;
284 if (ibs_caps & IBS_CAPS_OPCNTEXT)
285 val = min(val, IBS_OP_MAX_CNT_EXT);
286 else
287 val = min(val, IBS_OP_MAX_CNT);
288 ibs_config.max_cnt_op =
289 (val - IBS_RANDOM_MAXCNT_OFFSET) << 4;
Robert Richterba520782010-02-23 15:46:49 +0100290 }
Robert Richterb47fad32010-09-22 17:45:39 +0200291 val = ((val & ~IBS_OP_MAX_CNT) << 4) | (val & IBS_OP_MAX_CNT);
Robert Richter53b39e92010-09-21 17:58:15 +0200292 val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
293 val |= IBS_OP_ENABLE;
294 ibs_state.ibs_op_ctl = val;
Robert Richter25da6952010-09-21 15:49:31 +0200295 ibs_state.sample_size = IBS_OP_SIZE;
296 if (ibs_config.branch_target) {
297 ibs_state.branch_target = 1;
298 ibs_state.sample_size++;
299 }
Robert Richter53b39e92010-09-21 17:58:15 +0200300 val = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200301 wrmsrl(MSR_AMD64_IBSOPCTL, val);
Robert Richter90637592009-03-10 19:15:57 +0100302 }
303}
304
305static void op_amd_stop_ibs(void)
306{
Robert Richter64683da2010-02-04 10:57:23 +0100307 if (!ibs_caps)
308 return;
309
310 if (ibs_config.fetch_enabled)
Robert Richter90637592009-03-10 19:15:57 +0100311 /* clear max count and enable */
Robert Richterc572ae42009-06-03 20:10:39 +0200312 wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
Robert Richter90637592009-03-10 19:15:57 +0100313
Robert Richter64683da2010-02-04 10:57:23 +0100314 if (ibs_config.op_enabled)
Robert Richter90637592009-03-10 19:15:57 +0100315 /* clear max count and enable */
Robert Richterc572ae42009-06-03 20:10:39 +0200316 wrmsrl(MSR_AMD64_IBSOPCTL, 0);
Robert Richter90637592009-03-10 19:15:57 +0100317}
318
Robert Richter27afdf22010-10-06 12:27:54 +0200319static inline int eilvt_is_available(int offset)
320{
321 /* check if we may assign a vector */
322 return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
323}
324
325static inline int ibs_eilvt_valid(void)
326{
Robert Richter27afdf22010-10-06 12:27:54 +0200327 int offset;
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200328 u64 val;
Robert Richter27afdf22010-10-06 12:27:54 +0200329
330 rdmsrl(MSR_AMD64_IBSCTL, val);
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200331 offset = val & IBSCTL_LVT_OFFSET_MASK;
332
Robert Richter27afdf22010-10-06 12:27:54 +0200333 if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200334 pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
335 smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
Robert Richter27afdf22010-10-06 12:27:54 +0200336 return 0;
337 }
338
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200339 if (!eilvt_is_available(offset)) {
340 pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
341 smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
342 return 0;
343 }
Robert Richter27afdf22010-10-06 12:27:54 +0200344
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200345 return 1;
Robert Richter27afdf22010-10-06 12:27:54 +0200346}
347
348static inline int get_ibs_offset(void)
349{
350 u64 val;
351
352 rdmsrl(MSR_AMD64_IBSCTL, val);
353 if (!(val & IBSCTL_LVT_OFFSET_VALID))
354 return -EINVAL;
355
356 return val & IBSCTL_LVT_OFFSET_MASK;
357}
358
359static void setup_APIC_ibs(void)
360{
361 int offset;
362
363 offset = get_ibs_offset();
364 if (offset < 0)
365 goto failed;
366
367 if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
368 return;
369failed:
370 pr_warn("oprofile: IBS APIC setup failed on cpu #%d\n",
371 smp_processor_id());
372}
373
374static void clear_APIC_ibs(void)
375{
376 int offset;
377
378 offset = get_ibs_offset();
379 if (offset >= 0)
380 setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
381}
382
Robert Richterda759fe2010-02-26 10:54:56 +0100383#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
384
385static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
386 struct op_msrs const * const msrs)
387{
388 u64 val;
389 int i;
390
391 /* enable active counters */
Robert Richterda169f52010-09-24 15:54:43 +0200392 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100393 int virt = op_x86_phys_to_virt(i);
394 if (!reset_value[virt])
395 continue;
396 rdmsrl(msrs->controls[i].addr, val);
397 val &= model->reserved;
398 val |= op_x86_get_ctrl(model, &counter_config[virt]);
399 wrmsrl(msrs->controls[i].addr, val);
400 }
401}
402
403#endif
404
405/* functions for op_amd_spec */
406
407static void op_amd_shutdown(struct op_msrs const * const msrs)
408{
409 int i;
410
Robert Richterda169f52010-09-24 15:54:43 +0200411 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100412 if (!msrs->counters[i].addr)
413 continue;
414 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
415 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
416 }
417}
418
419static int op_amd_fill_in_addresses(struct op_msrs * const msrs)
420{
421 int i;
422
Robert Richterda169f52010-09-24 15:54:43 +0200423 for (i = 0; i < num_counters; i++) {
Robert Richterda759fe2010-02-26 10:54:56 +0100424 if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
425 goto fail;
426 if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) {
427 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
428 goto fail;
429 }
430 /* both registers must be reserved */
Robert Richterda169f52010-09-24 15:54:43 +0200431 if (num_counters == NUM_COUNTERS_F15H) {
432 msrs->counters[i].addr = MSR_F15H_PERF_CTR + (i << 1);
433 msrs->controls[i].addr = MSR_F15H_PERF_CTL + (i << 1);
434 } else {
435 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
436 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
437 }
Robert Richterda759fe2010-02-26 10:54:56 +0100438 continue;
439 fail:
440 if (!counter_config[i].enabled)
441 continue;
442 op_x86_warn_reserved(i);
443 op_amd_shutdown(msrs);
444 return -EBUSY;
445 }
446
447 return 0;
448}
449
450static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
451 struct op_msrs const * const msrs)
452{
453 u64 val;
454 int i;
455
456 /* setup reset_value */
Robert Richterda169f52010-09-24 15:54:43 +0200457 for (i = 0; i < OP_MAX_COUNTER; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100458 if (counter_config[i].enabled
459 && msrs->counters[op_x86_virt_to_phys(i)].addr)
460 reset_value[i] = counter_config[i].count;
461 else
462 reset_value[i] = 0;
463 }
464
465 /* clear all counters */
Robert Richterda169f52010-09-24 15:54:43 +0200466 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100467 if (!msrs->controls[i].addr)
468 continue;
469 rdmsrl(msrs->controls[i].addr, val);
470 if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
471 op_x86_warn_in_use(i);
472 val &= model->reserved;
473 wrmsrl(msrs->controls[i].addr, val);
474 /*
475 * avoid a false detection of ctr overflows in NMI
476 * handler
477 */
478 wrmsrl(msrs->counters[i].addr, -1LL);
479 }
480
481 /* enable active counters */
Robert Richterda169f52010-09-24 15:54:43 +0200482 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100483 int virt = op_x86_phys_to_virt(i);
484 if (!reset_value[virt])
485 continue;
486
487 /* setup counter registers */
488 wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
489
490 /* setup control registers */
491 rdmsrl(msrs->controls[i].addr, val);
492 val &= model->reserved;
493 val |= op_x86_get_ctrl(model, &counter_config[virt]);
494 wrmsrl(msrs->controls[i].addr, val);
495 }
Robert Richterbae663b2010-05-05 17:47:17 +0200496
497 if (ibs_caps)
Robert Richter27afdf22010-10-06 12:27:54 +0200498 setup_APIC_ibs();
Robert Richterbae663b2010-05-05 17:47:17 +0200499}
500
501static void op_amd_cpu_shutdown(void)
502{
503 if (ibs_caps)
Robert Richter27afdf22010-10-06 12:27:54 +0200504 clear_APIC_ibs();
Robert Richterda759fe2010-02-26 10:54:56 +0100505}
506
Robert Richter7939d2b2008-07-22 21:08:56 +0200507static int op_amd_check_ctrs(struct pt_regs * const regs,
508 struct op_msrs const * const msrs)
509{
Robert Richter42399ad2009-05-25 17:59:06 +0200510 u64 val;
Robert Richter7939d2b2008-07-22 21:08:56 +0200511 int i;
512
Robert Richterda169f52010-09-24 15:54:43 +0200513 for (i = 0; i < num_counters; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200514 int virt = op_x86_phys_to_virt(i);
515 if (!reset_value[virt])
Robert Richter7939d2b2008-07-22 21:08:56 +0200516 continue;
Robert Richter42399ad2009-05-25 17:59:06 +0200517 rdmsrl(msrs->counters[i].addr, val);
518 /* bit is clear if overflowed: */
519 if (val & OP_CTR_OVERFLOW)
520 continue;
Robert Richterd8471ad2009-07-16 13:04:43 +0200521 oprofile_add_sample(regs, virt);
522 wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
Robert Richter7939d2b2008-07-22 21:08:56 +0200523 }
524
525 op_amd_handle_ibs(regs, msrs);
526
527 /* See op_model_ppro.c */
528 return 1;
529}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100530
Robert Richter6657fe42008-07-22 21:08:50 +0200531static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Robert Richterdea37662009-05-25 18:11:52 +0200533 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 int i;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200535
Robert Richterda169f52010-09-24 15:54:43 +0200536 for (i = 0; i < num_counters; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200537 if (!reset_value[op_x86_phys_to_virt(i)])
538 continue;
539 rdmsrl(msrs->controls[i].addr, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100540 val |= ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richterd8471ad2009-07-16 13:04:43 +0200541 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Robert Richter852402c2008-07-22 21:09:06 +0200543
Robert Richter90637592009-03-10 19:15:57 +0100544 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Robert Richter6657fe42008-07-22 21:08:50 +0200547static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Robert Richterdea37662009-05-25 18:11:52 +0200549 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int i;
551
Robert Richterfd13f6c2008-10-19 21:00:09 +0200552 /*
553 * Subtle: stop on all counters to avoid race with setting our
554 * pm callback
555 */
Robert Richterda169f52010-09-24 15:54:43 +0200556 for (i = 0; i < num_counters; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200557 if (!reset_value[op_x86_phys_to_virt(i)])
Don Zickuscb9c4482006-09-26 10:52:26 +0200558 continue;
Robert Richterdea37662009-05-25 18:11:52 +0200559 rdmsrl(msrs->controls[i].addr, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100560 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richterdea37662009-05-25 18:11:52 +0200561 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200563
Robert Richter90637592009-03-10 19:15:57 +0100564 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Robert Richter27afdf22010-10-06 12:27:54 +0200567static int setup_ibs_ctl(int ibs_eilvt_off)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200568{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200569 struct pci_dev *cpu_cfg;
570 int nodes;
571 u32 value = 0;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200572
573 nodes = 0;
574 cpu_cfg = NULL;
575 do {
576 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
577 PCI_DEVICE_ID_AMD_10H_NB_MISC,
578 cpu_cfg);
579 if (!cpu_cfg)
580 break;
581 ++nodes;
582 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
Robert Richter27afdf22010-10-06 12:27:54 +0200583 | IBSCTL_LVT_OFFSET_VALID);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200584 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
Robert Richter27afdf22010-10-06 12:27:54 +0200585 if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100586 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200587 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
Robert Richter27afdf22010-10-06 12:27:54 +0200588 "IBSCTL = 0x%08x\n", value);
589 return -EINVAL;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200590 }
591 } while (1);
592
593 if (!nodes) {
Robert Richter27afdf22010-10-06 12:27:54 +0200594 printk(KERN_DEBUG "No CPU node configured for IBS\n");
595 return -ENODEV;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200596 }
597
Robert Richter7d77f2d2008-07-22 21:08:57 +0200598 return 0;
599}
600
Robert Richter27afdf22010-10-06 12:27:54 +0200601static int force_ibs_eilvt_setup(void)
602{
603 int i;
604 int ret;
605
606 /* find the next free available EILVT entry */
607 for (i = 1; i < 4; i++) {
608 if (!eilvt_is_available(i))
609 continue;
610 ret = setup_ibs_ctl(i);
611 if (ret)
612 return ret;
613 return 0;
614 }
615
616 printk(KERN_DEBUG "No EILVT entry available\n");
617
618 return -EBUSY;
619}
620
621static int __init_ibs_nmi(void)
622{
623 int ret;
624
625 if (ibs_eilvt_valid())
626 return 0;
627
628 ret = force_ibs_eilvt_setup();
629 if (ret)
630 return ret;
631
632 if (!ibs_eilvt_valid())
633 return -EFAULT;
634
635 pr_err(FW_BUG "workaround enabled for IBS LVT offset\n");
636
637 return 0;
638}
639
Robert Richterfd13f6c2008-10-19 21:00:09 +0200640/* initialize the APIC for the IBS interrupts if available */
Robert Richterbae663b2010-05-05 17:47:17 +0200641static void init_ibs(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200642{
Robert Richter64683da2010-02-04 10:57:23 +0100643 ibs_caps = get_ibs_caps();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200644
Robert Richter64683da2010-02-04 10:57:23 +0100645 if (!ibs_caps)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200646 return;
647
Robert Richterbae663b2010-05-05 17:47:17 +0200648 if (__init_ibs_nmi()) {
Robert Richter64683da2010-02-04 10:57:23 +0100649 ibs_caps = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200650 return;
651 }
652
Robert Richter64683da2010-02-04 10:57:23 +0100653 printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n",
654 (unsigned)ibs_caps);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200655}
656
Robert Richter25ad29132008-09-05 17:12:36 +0200657static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200658
Robert Richter25ad29132008-09-05 17:12:36 +0200659static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200660{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200661 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200662 int ret = 0;
663
664 /* architecture specific files */
665 if (create_arch_files)
666 ret = create_arch_files(sb, root);
667
668 if (ret)
669 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200670
Robert Richter64683da2010-02-04 10:57:23 +0100671 if (!ibs_caps)
Robert Richter270d3e12008-07-22 21:09:01 +0200672 return ret;
673
674 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200675
676 /* setup some reasonable defaults */
Robert Richter25da6952010-09-21 15:49:31 +0200677 memset(&ibs_config, 0, sizeof(ibs_config));
Barry Kasindorf56784f12008-07-22 21:08:55 +0200678 ibs_config.max_cnt_fetch = 250000;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200679 ibs_config.max_cnt_op = 250000;
Robert Richter2d55a472008-07-18 17:56:05 +0200680
Robert Richter4ac945f2010-09-21 15:58:32 +0200681 if (ibs_caps & IBS_CAPS_FETCHSAM) {
682 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
683 oprofilefs_create_ulong(sb, dir, "enable",
684 &ibs_config.fetch_enabled);
685 oprofilefs_create_ulong(sb, dir, "max_count",
686 &ibs_config.max_cnt_fetch);
687 oprofilefs_create_ulong(sb, dir, "rand_enable",
688 &ibs_config.rand_en);
689 }
Robert Richter2d55a472008-07-18 17:56:05 +0200690
Robert Richter4ac945f2010-09-21 15:58:32 +0200691 if (ibs_caps & IBS_CAPS_OPSAM) {
692 dir = oprofilefs_mkdir(sb, root, "ibs_op");
693 oprofilefs_create_ulong(sb, dir, "enable",
694 &ibs_config.op_enabled);
695 oprofilefs_create_ulong(sb, dir, "max_count",
696 &ibs_config.max_cnt_op);
697 if (ibs_caps & IBS_CAPS_OPCNT)
698 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
699 &ibs_config.dispatched_ops);
Robert Richter25da6952010-09-21 15:49:31 +0200700 if (ibs_caps & IBS_CAPS_BRNTRGT)
701 oprofilefs_create_ulong(sb, dir, "branch_target",
702 &ibs_config.branch_target);
Robert Richter4ac945f2010-09-21 15:58:32 +0200703 }
Robert Richterfc2bd732008-07-22 21:09:00 +0200704
705 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200706}
707
Robert Richterda169f52010-09-24 15:54:43 +0200708struct op_x86_model_spec op_amd_spec;
709
Robert Richteradf5ec02008-07-22 21:08:48 +0200710static int op_amd_init(struct oprofile_operations *ops)
711{
Robert Richterbae663b2010-05-05 17:47:17 +0200712 init_ibs();
Robert Richter270d3e12008-07-22 21:09:01 +0200713 create_arch_files = ops->create_files;
714 ops->create_files = setup_ibs_files;
Robert Richterda169f52010-09-24 15:54:43 +0200715
716 if (boot_cpu_data.x86 == 0x15) {
717 num_counters = NUM_COUNTERS_F15H;
718 } else {
719 num_counters = NUM_COUNTERS;
720 }
721
722 op_amd_spec.num_counters = num_counters;
723 op_amd_spec.num_controls = num_counters;
724 op_amd_spec.num_virt_counters = max(num_counters, NUM_VIRT_COUNTERS);
725
Robert Richteradf5ec02008-07-22 21:08:48 +0200726 return 0;
727}
728
Robert Richter259a83a2009-07-09 15:12:35 +0200729struct op_x86_model_spec op_amd_spec = {
Robert Richterda169f52010-09-24 15:54:43 +0200730 /* num_counters/num_controls filled in at runtime */
Robert Richter3370d352009-05-25 15:10:32 +0200731 .reserved = MSR_AMD_EVENTSEL_RESERVED,
732 .event_mask = OP_EVENT_MASK,
733 .init = op_amd_init,
Robert Richterc92960f2008-09-05 17:12:36 +0200734 .fill_in_addresses = &op_amd_fill_in_addresses,
735 .setup_ctrs = &op_amd_setup_ctrs,
Robert Richterbae663b2010-05-05 17:47:17 +0200736 .cpu_down = &op_amd_cpu_shutdown,
Robert Richterc92960f2008-09-05 17:12:36 +0200737 .check_ctrs = &op_amd_check_ctrs,
738 .start = &op_amd_start,
739 .stop = &op_amd_stop,
Robert Richter3370d352009-05-25 15:10:32 +0200740 .shutdown = &op_amd_shutdown,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200741#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
Robert Richter7e7478c2009-07-16 13:09:53 +0200742 .switch_ctrl = &op_mux_switch_ctrl,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200743#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744};