blob: e947e5cb2e6109632336d31773e00feac3d9836b [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
Jason Yeh4d4036e2009-07-08 13:49:38 +020032#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
Robert Richterda169f52010-09-24 15:54:43 +020033#define NUM_VIRT_COUNTERS 32
Jason Yeh4d4036e2009-07-08 13:49:38 +020034#else
Robert Richterda169f52010-09-24 15:54:43 +020035#define NUM_VIRT_COUNTERS 0
Jason Yeh4d4036e2009-07-08 13:49:38 +020036#endif
37
Robert Richter3370d352009-05-25 15:10:32 +020038#define OP_EVENT_MASK 0x0FFF
Robert Richter42399ad2009-05-25 17:59:06 +020039#define OP_CTR_OVERFLOW (1ULL<<31)
Robert Richter3370d352009-05-25 15:10:32 +020040
41#define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Robert Richterda169f52010-09-24 15:54:43 +020043static int num_counters;
44static unsigned long reset_value[OP_MAX_COUNTER];
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/*
Robert Richterba520782010-02-23 15:46:49 +010071 * IBS randomization macros
72 */
73#define IBS_RANDOM_BITS 12
74#define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1)
75#define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5))
76
Robert Richter64683da2010-02-04 10:57:23 +010077static u32 get_ibs_caps(void)
78{
79 u32 ibs_caps;
80 unsigned int max_level;
81
82 if (!boot_cpu_has(X86_FEATURE_IBS))
83 return 0;
84
85 /* check IBS cpuid feature flags */
86 max_level = cpuid_eax(0x80000000);
87 if (max_level < IBS_CPUID_FEATURES)
Robert Richter4ac945f2010-09-21 15:58:32 +020088 return IBS_CAPS_DEFAULT;
Robert Richter64683da2010-02-04 10:57:23 +010089
90 ibs_caps = cpuid_eax(IBS_CPUID_FEATURES);
91 if (!(ibs_caps & IBS_CAPS_AVAIL))
92 /* cpuid flags not valid */
Robert Richter4ac945f2010-09-21 15:58:32 +020093 return IBS_CAPS_DEFAULT;
Robert Richter64683da2010-02-04 10:57:23 +010094
95 return ibs_caps;
96}
97
Suravee Suthikulpanitf125be12010-01-18 11:25:45 -060098/*
99 * 16-bit Linear Feedback Shift Register (LFSR)
100 *
101 * 16 14 13 11
102 * Feedback polynomial = X + X + X + X + 1
103 */
104static unsigned int lfsr_random(void)
105{
106 static unsigned int lfsr_value = 0xF00D;
107 unsigned int bit;
108
109 /* Compute next bit to shift in */
110 bit = ((lfsr_value >> 0) ^
111 (lfsr_value >> 2) ^
112 (lfsr_value >> 3) ^
113 (lfsr_value >> 5)) & 0x0001;
114
115 /* Advance to next register value */
116 lfsr_value = (lfsr_value >> 1) | (bit << 15);
117
118 return lfsr_value;
119}
120
Robert Richterba520782010-02-23 15:46:49 +0100121/*
122 * IBS software randomization
123 *
124 * The IBS periodic op counter is randomized in software. The lower 12
125 * bits of the 20 bit counter are randomized. IbsOpCurCnt is
126 * initialized with a 12 bit random value.
127 */
128static inline u64 op_amd_randomize_ibs_op(u64 val)
129{
130 unsigned int random = lfsr_random();
131
132 if (!(ibs_caps & IBS_CAPS_RDWROPCNT))
133 /*
134 * Work around if the hw can not write to IbsOpCurCnt
135 *
136 * Randomize the lower 8 bits of the 16 bit
137 * IbsOpMaxCnt [15:0] value in the range of -128 to
138 * +127 by adding/subtracting an offset to the
139 * maximum count (IbsOpMaxCnt).
140 *
141 * To avoid over or underflows and protect upper bits
142 * starting at bit 16, the initial value for
143 * IbsOpMaxCnt must fit in the range from 0x0081 to
144 * 0xff80.
145 */
146 val += (s8)(random >> 4);
147 else
148 val |= (u64)(random & IBS_RANDOM_MASK) << 32;
149
150 return val;
151}
152
Andrew Morton4680e642009-06-23 12:36:08 -0700153static inline void
Robert Richter7939d2b2008-07-22 21:08:56 +0200154op_amd_handle_ibs(struct pt_regs * const regs,
155 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Robert Richterc572ae42009-06-03 20:10:39 +0200157 u64 val, ctl;
Robert Richter1acda872009-01-05 10:35:31 +0100158 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Robert Richter64683da2010-02-04 10:57:23 +0100160 if (!ibs_caps)
Andrew Morton4680e642009-06-23 12:36:08 -0700161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Robert Richter7939d2b2008-07-22 21:08:56 +0200163 if (ibs_config.fetch_enabled) {
Robert Richterc572ae42009-06-03 20:10:39 +0200164 rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
165 if (ctl & IBS_FETCH_VAL) {
166 rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
167 oprofile_write_reserve(&entry, regs, val,
Robert Richter14f0ca82009-01-07 21:50:22 +0100168 IBS_FETCH_CODE, IBS_FETCH_SIZE);
Robert Richter51563a02009-06-03 20:54:56 +0200169 oprofile_add_data64(&entry, val);
170 oprofile_add_data64(&entry, ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200171 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200172 oprofile_add_data64(&entry, val);
Robert Richter14f0ca82009-01-07 21:50:22 +0100173 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200174
Robert Richterfd13f6c2008-10-19 21:00:09 +0200175 /* reenable the IRQ */
Robert Richtera163b102010-02-25 19:43:07 +0100176 ctl &= ~(IBS_FETCH_VAL | IBS_FETCH_CNT);
Robert Richterc572ae42009-06-03 20:10:39 +0200177 ctl |= IBS_FETCH_ENABLE;
178 wrmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200179 }
180 }
181
Robert Richter7939d2b2008-07-22 21:08:56 +0200182 if (ibs_config.op_enabled) {
Robert Richterc572ae42009-06-03 20:10:39 +0200183 rdmsrl(MSR_AMD64_IBSOPCTL, ctl);
184 if (ctl & IBS_OP_VAL) {
185 rdmsrl(MSR_AMD64_IBSOPRIP, val);
Robert Richter25da6952010-09-21 15:49:31 +0200186 oprofile_write_reserve(&entry, regs, val, IBS_OP_CODE,
187 ibs_state.sample_size);
Robert Richter51563a02009-06-03 20:54:56 +0200188 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200189 rdmsrl(MSR_AMD64_IBSOPDATA, val);
Robert Richter51563a02009-06-03 20:54:56 +0200190 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200191 rdmsrl(MSR_AMD64_IBSOPDATA2, val);
Robert Richter51563a02009-06-03 20:54:56 +0200192 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200193 rdmsrl(MSR_AMD64_IBSOPDATA3, val);
Robert Richter51563a02009-06-03 20:54:56 +0200194 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200195 rdmsrl(MSR_AMD64_IBSDCLINAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200196 oprofile_add_data64(&entry, val);
Robert Richterc572ae42009-06-03 20:10:39 +0200197 rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
Robert Richter51563a02009-06-03 20:54:56 +0200198 oprofile_add_data64(&entry, val);
Robert Richter25da6952010-09-21 15:49:31 +0200199 if (ibs_state.branch_target) {
200 rdmsrl(MSR_AMD64_IBSBRTARGET, val);
201 oprofile_add_data(&entry, (unsigned long)val);
202 }
Robert Richter14f0ca82009-01-07 21:50:22 +0100203 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200204
205 /* reenable the IRQ */
Robert Richter53b39e92010-09-21 17:58:15 +0200206 ctl = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200207 wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200208 }
209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Robert Richter90637592009-03-10 19:15:57 +0100212static inline void op_amd_start_ibs(void)
213{
Robert Richterc572ae42009-06-03 20:10:39 +0200214 u64 val;
Robert Richter64683da2010-02-04 10:57:23 +0100215
216 if (!ibs_caps)
217 return;
218
Robert Richter53b39e92010-09-21 17:58:15 +0200219 memset(&ibs_state, 0, sizeof(ibs_state));
220
Robert Richterb47fad32010-09-22 17:45:39 +0200221 /*
222 * Note: Since the max count settings may out of range we
223 * write back the actual used values so that userland can read
224 * it.
225 */
226
Robert Richter64683da2010-02-04 10:57:23 +0100227 if (ibs_config.fetch_enabled) {
Robert Richterb47fad32010-09-22 17:45:39 +0200228 val = ibs_config.max_cnt_fetch >> 4;
229 val = min(val, IBS_FETCH_MAX_CNT);
230 ibs_config.max_cnt_fetch = val << 4;
Robert Richterc572ae42009-06-03 20:10:39 +0200231 val |= ibs_config.rand_en ? IBS_FETCH_RAND_EN : 0;
232 val |= IBS_FETCH_ENABLE;
233 wrmsrl(MSR_AMD64_IBSFETCHCTL, val);
Robert Richter90637592009-03-10 19:15:57 +0100234 }
235
Robert Richter64683da2010-02-04 10:57:23 +0100236 if (ibs_config.op_enabled) {
Robert Richter53b39e92010-09-21 17:58:15 +0200237 val = ibs_config.max_cnt_op >> 4;
Robert Richterba520782010-02-23 15:46:49 +0100238 if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) {
239 /*
240 * IbsOpCurCnt not supported. See
241 * op_amd_randomize_ibs_op() for details.
242 */
Robert Richter53b39e92010-09-21 17:58:15 +0200243 val = clamp(val, 0x0081ULL, 0xFF80ULL);
Robert Richterb47fad32010-09-22 17:45:39 +0200244 ibs_config.max_cnt_op = val << 4;
Robert Richterba520782010-02-23 15:46:49 +0100245 } else {
246 /*
247 * The start value is randomized with a
248 * positive offset, we need to compensate it
249 * with the half of the randomized range. Also
250 * avoid underflows.
251 */
Robert Richterb47fad32010-09-22 17:45:39 +0200252 val += IBS_RANDOM_MAXCNT_OFFSET;
253 if (ibs_caps & IBS_CAPS_OPCNTEXT)
254 val = min(val, IBS_OP_MAX_CNT_EXT);
255 else
256 val = min(val, IBS_OP_MAX_CNT);
257 ibs_config.max_cnt_op =
258 (val - IBS_RANDOM_MAXCNT_OFFSET) << 4;
Robert Richterba520782010-02-23 15:46:49 +0100259 }
Robert Richterb47fad32010-09-22 17:45:39 +0200260 val = ((val & ~IBS_OP_MAX_CNT) << 4) | (val & IBS_OP_MAX_CNT);
Robert Richter53b39e92010-09-21 17:58:15 +0200261 val |= ibs_config.dispatched_ops ? IBS_OP_CNT_CTL : 0;
262 val |= IBS_OP_ENABLE;
263 ibs_state.ibs_op_ctl = val;
Robert Richter25da6952010-09-21 15:49:31 +0200264 ibs_state.sample_size = IBS_OP_SIZE;
265 if (ibs_config.branch_target) {
266 ibs_state.branch_target = 1;
267 ibs_state.sample_size++;
268 }
Robert Richter53b39e92010-09-21 17:58:15 +0200269 val = op_amd_randomize_ibs_op(ibs_state.ibs_op_ctl);
Robert Richterc572ae42009-06-03 20:10:39 +0200270 wrmsrl(MSR_AMD64_IBSOPCTL, val);
Robert Richter90637592009-03-10 19:15:57 +0100271 }
272}
273
274static void op_amd_stop_ibs(void)
275{
Robert Richter64683da2010-02-04 10:57:23 +0100276 if (!ibs_caps)
277 return;
278
279 if (ibs_config.fetch_enabled)
Robert Richter90637592009-03-10 19:15:57 +0100280 /* clear max count and enable */
Robert Richterc572ae42009-06-03 20:10:39 +0200281 wrmsrl(MSR_AMD64_IBSFETCHCTL, 0);
Robert Richter90637592009-03-10 19:15:57 +0100282
Robert Richter64683da2010-02-04 10:57:23 +0100283 if (ibs_config.op_enabled)
Robert Richter90637592009-03-10 19:15:57 +0100284 /* clear max count and enable */
Robert Richterc572ae42009-06-03 20:10:39 +0200285 wrmsrl(MSR_AMD64_IBSOPCTL, 0);
Robert Richter90637592009-03-10 19:15:57 +0100286}
287
Robert Richter3d2606f2011-05-20 09:46:54 +0200288static inline int get_eilvt(int offset)
Robert Richter27afdf22010-10-06 12:27:54 +0200289{
Robert Richter27afdf22010-10-06 12:27:54 +0200290 return !setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 1);
291}
292
Robert Richter3d2606f2011-05-20 09:46:54 +0200293static inline int put_eilvt(int offset)
294{
295 return !setup_APIC_eilvt(offset, 0, 0, 1);
296}
297
Robert Richter27afdf22010-10-06 12:27:54 +0200298static inline int ibs_eilvt_valid(void)
299{
Robert Richter27afdf22010-10-06 12:27:54 +0200300 int offset;
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200301 u64 val;
Robert Richter3d2606f2011-05-20 09:46:54 +0200302 int valid = 0;
303
304 preempt_disable();
Robert Richter27afdf22010-10-06 12:27:54 +0200305
306 rdmsrl(MSR_AMD64_IBSCTL, val);
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200307 offset = val & IBSCTL_LVT_OFFSET_MASK;
308
Robert Richter27afdf22010-10-06 12:27:54 +0200309 if (!(val & IBSCTL_LVT_OFFSET_VALID)) {
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200310 pr_err(FW_BUG "cpu %d, invalid IBS interrupt offset %d (MSR%08X=0x%016llx)\n",
311 smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
Robert Richter3d2606f2011-05-20 09:46:54 +0200312 goto out;
Robert Richter27afdf22010-10-06 12:27:54 +0200313 }
314
Robert Richter3d2606f2011-05-20 09:46:54 +0200315 if (!get_eilvt(offset)) {
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200316 pr_err(FW_BUG "cpu %d, IBS interrupt offset %d not available (MSR%08X=0x%016llx)\n",
317 smp_processor_id(), offset, MSR_AMD64_IBSCTL, val);
Robert Richter3d2606f2011-05-20 09:46:54 +0200318 goto out;
Ingo Molnar2c78ffe2010-10-25 08:41:09 +0200319 }
Robert Richter27afdf22010-10-06 12:27:54 +0200320
Robert Richter3d2606f2011-05-20 09:46:54 +0200321 valid = 1;
322out:
323 preempt_enable();
324
325 return valid;
Robert Richter27afdf22010-10-06 12:27:54 +0200326}
327
328static inline int get_ibs_offset(void)
329{
330 u64 val;
331
332 rdmsrl(MSR_AMD64_IBSCTL, val);
333 if (!(val & IBSCTL_LVT_OFFSET_VALID))
334 return -EINVAL;
335
336 return val & IBSCTL_LVT_OFFSET_MASK;
337}
338
339static void setup_APIC_ibs(void)
340{
341 int offset;
342
343 offset = get_ibs_offset();
344 if (offset < 0)
345 goto failed;
346
347 if (!setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_NMI, 0))
348 return;
349failed:
350 pr_warn("oprofile: IBS APIC setup failed on cpu #%d\n",
351 smp_processor_id());
352}
353
354static void clear_APIC_ibs(void)
355{
356 int offset;
357
358 offset = get_ibs_offset();
359 if (offset >= 0)
360 setup_APIC_eilvt(offset, 0, APIC_EILVT_MSG_FIX, 1);
361}
362
Robert Richterda759fe2010-02-26 10:54:56 +0100363#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
364
365static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
366 struct op_msrs const * const msrs)
367{
368 u64 val;
369 int i;
370
371 /* enable active counters */
Robert Richterda169f52010-09-24 15:54:43 +0200372 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100373 int virt = op_x86_phys_to_virt(i);
374 if (!reset_value[virt])
375 continue;
376 rdmsrl(msrs->controls[i].addr, val);
377 val &= model->reserved;
378 val |= op_x86_get_ctrl(model, &counter_config[virt]);
379 wrmsrl(msrs->controls[i].addr, val);
380 }
381}
382
383#endif
384
385/* functions for op_amd_spec */
386
387static void op_amd_shutdown(struct op_msrs const * const msrs)
388{
389 int i;
390
Robert Richterda169f52010-09-24 15:54:43 +0200391 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100392 if (!msrs->counters[i].addr)
393 continue;
394 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
395 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
396 }
397}
398
399static int op_amd_fill_in_addresses(struct op_msrs * const msrs)
400{
401 int i;
402
Robert Richterda169f52010-09-24 15:54:43 +0200403 for (i = 0; i < num_counters; i++) {
Robert Richterda759fe2010-02-26 10:54:56 +0100404 if (!reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
405 goto fail;
406 if (!reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i)) {
407 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
408 goto fail;
409 }
410 /* both registers must be reserved */
Robert Richteree5789d2011-09-21 11:30:17 +0200411 if (num_counters == AMD64_NUM_COUNTERS_F15H) {
Robert Richterda169f52010-09-24 15:54:43 +0200412 msrs->counters[i].addr = MSR_F15H_PERF_CTR + (i << 1);
413 msrs->controls[i].addr = MSR_F15H_PERF_CTL + (i << 1);
414 } else {
415 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
416 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
417 }
Robert Richterda759fe2010-02-26 10:54:56 +0100418 continue;
419 fail:
420 if (!counter_config[i].enabled)
421 continue;
422 op_x86_warn_reserved(i);
423 op_amd_shutdown(msrs);
424 return -EBUSY;
425 }
426
427 return 0;
428}
429
430static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
431 struct op_msrs const * const msrs)
432{
433 u64 val;
434 int i;
435
436 /* setup reset_value */
Robert Richterda169f52010-09-24 15:54:43 +0200437 for (i = 0; i < OP_MAX_COUNTER; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100438 if (counter_config[i].enabled
439 && msrs->counters[op_x86_virt_to_phys(i)].addr)
440 reset_value[i] = counter_config[i].count;
441 else
442 reset_value[i] = 0;
443 }
444
445 /* clear all counters */
Robert Richterda169f52010-09-24 15:54:43 +0200446 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100447 if (!msrs->controls[i].addr)
448 continue;
449 rdmsrl(msrs->controls[i].addr, val);
450 if (val & ARCH_PERFMON_EVENTSEL_ENABLE)
451 op_x86_warn_in_use(i);
452 val &= model->reserved;
453 wrmsrl(msrs->controls[i].addr, val);
454 /*
455 * avoid a false detection of ctr overflows in NMI
456 * handler
457 */
458 wrmsrl(msrs->counters[i].addr, -1LL);
459 }
460
461 /* enable active counters */
Robert Richterda169f52010-09-24 15:54:43 +0200462 for (i = 0; i < num_counters; ++i) {
Robert Richterda759fe2010-02-26 10:54:56 +0100463 int virt = op_x86_phys_to_virt(i);
464 if (!reset_value[virt])
465 continue;
466
467 /* setup counter registers */
468 wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
469
470 /* setup control registers */
471 rdmsrl(msrs->controls[i].addr, val);
472 val &= model->reserved;
473 val |= op_x86_get_ctrl(model, &counter_config[virt]);
474 wrmsrl(msrs->controls[i].addr, val);
475 }
Robert Richterbae663b2010-05-05 17:47:17 +0200476
477 if (ibs_caps)
Robert Richter27afdf22010-10-06 12:27:54 +0200478 setup_APIC_ibs();
Robert Richterbae663b2010-05-05 17:47:17 +0200479}
480
481static void op_amd_cpu_shutdown(void)
482{
483 if (ibs_caps)
Robert Richter27afdf22010-10-06 12:27:54 +0200484 clear_APIC_ibs();
Robert Richterda759fe2010-02-26 10:54:56 +0100485}
486
Robert Richter7939d2b2008-07-22 21:08:56 +0200487static int op_amd_check_ctrs(struct pt_regs * const regs,
488 struct op_msrs const * const msrs)
489{
Robert Richter42399ad2009-05-25 17:59:06 +0200490 u64 val;
Robert Richter7939d2b2008-07-22 21:08:56 +0200491 int i;
492
Robert Richterda169f52010-09-24 15:54:43 +0200493 for (i = 0; i < num_counters; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200494 int virt = op_x86_phys_to_virt(i);
495 if (!reset_value[virt])
Robert Richter7939d2b2008-07-22 21:08:56 +0200496 continue;
Robert Richter42399ad2009-05-25 17:59:06 +0200497 rdmsrl(msrs->counters[i].addr, val);
498 /* bit is clear if overflowed: */
499 if (val & OP_CTR_OVERFLOW)
500 continue;
Robert Richterd8471ad2009-07-16 13:04:43 +0200501 oprofile_add_sample(regs, virt);
502 wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
Robert Richter7939d2b2008-07-22 21:08:56 +0200503 }
504
505 op_amd_handle_ibs(regs, msrs);
506
507 /* See op_model_ppro.c */
508 return 1;
509}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100510
Robert Richter6657fe42008-07-22 21:08:50 +0200511static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Robert Richterdea37662009-05-25 18:11:52 +0200513 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int i;
Jason Yeh4d4036e2009-07-08 13:49:38 +0200515
Robert Richterda169f52010-09-24 15:54:43 +0200516 for (i = 0; i < num_counters; ++i) {
Robert Richterd8471ad2009-07-16 13:04:43 +0200517 if (!reset_value[op_x86_phys_to_virt(i)])
518 continue;
519 rdmsrl(msrs->controls[i].addr, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100520 val |= ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richterd8471ad2009-07-16 13:04:43 +0200521 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Robert Richter852402c2008-07-22 21:09:06 +0200523
Robert Richter90637592009-03-10 19:15:57 +0100524 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Robert Richter6657fe42008-07-22 21:08:50 +0200527static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Robert Richterdea37662009-05-25 18:11:52 +0200529 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int i;
531
Robert Richterfd13f6c2008-10-19 21:00:09 +0200532 /*
533 * Subtle: stop on all counters to avoid race with setting our
534 * pm callback
535 */
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)])
Don Zickuscb9c4482006-09-26 10:52:26 +0200538 continue;
Robert Richterdea37662009-05-25 18:11:52 +0200539 rdmsrl(msrs->controls[i].addr, val);
Robert Richterbb1165d2010-03-01 14:21:23 +0100540 val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
Robert Richterdea37662009-05-25 18:11:52 +0200541 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200543
Robert Richter90637592009-03-10 19:15:57 +0100544 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Robert Richter27afdf22010-10-06 12:27:54 +0200547static int setup_ibs_ctl(int ibs_eilvt_off)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200548{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200549 struct pci_dev *cpu_cfg;
550 int nodes;
551 u32 value = 0;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200552
553 nodes = 0;
554 cpu_cfg = NULL;
555 do {
556 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
557 PCI_DEVICE_ID_AMD_10H_NB_MISC,
558 cpu_cfg);
559 if (!cpu_cfg)
560 break;
561 ++nodes;
562 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
Robert Richter27afdf22010-10-06 12:27:54 +0200563 | IBSCTL_LVT_OFFSET_VALID);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200564 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
Robert Richter27afdf22010-10-06 12:27:54 +0200565 if (value != (ibs_eilvt_off | IBSCTL_LVT_OFFSET_VALID)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100566 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200567 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
Robert Richter27afdf22010-10-06 12:27:54 +0200568 "IBSCTL = 0x%08x\n", value);
569 return -EINVAL;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200570 }
571 } while (1);
572
573 if (!nodes) {
Robert Richter27afdf22010-10-06 12:27:54 +0200574 printk(KERN_DEBUG "No CPU node configured for IBS\n");
575 return -ENODEV;
Robert Richter7d77f2d2008-07-22 21:08:57 +0200576 }
577
Robert Richter7d77f2d2008-07-22 21:08:57 +0200578 return 0;
579}
580
Robert Richtercbf74ce2011-05-30 16:31:11 +0200581/*
582 * This runs only on the current cpu. We try to find an LVT offset and
583 * setup the local APIC. For this we must disable preemption. On
584 * success we initialize all nodes with this offset. This updates then
585 * the offset in the IBS_CTL per-node msr. The per-core APIC setup of
586 * the IBS interrupt vector is called from op_amd_setup_ctrs()/op_-
587 * amd_cpu_shutdown() using the new offset.
588 */
Robert Richter27afdf22010-10-06 12:27:54 +0200589static int force_ibs_eilvt_setup(void)
590{
Robert Richter3d2606f2011-05-20 09:46:54 +0200591 int offset;
Robert Richter27afdf22010-10-06 12:27:54 +0200592 int ret;
593
Robert Richter3d2606f2011-05-20 09:46:54 +0200594 preempt_disable();
Robert Richtercbf74ce2011-05-30 16:31:11 +0200595 /* find the next free available EILVT entry, skip offset 0 */
Robert Richter3d2606f2011-05-20 09:46:54 +0200596 for (offset = 1; offset < APIC_EILVT_NR_MAX; offset++) {
597 if (get_eilvt(offset))
598 break;
599 }
600 preempt_enable();
601
602 if (offset == APIC_EILVT_NR_MAX) {
603 printk(KERN_DEBUG "No EILVT entry available\n");
604 return -EBUSY;
Robert Richter27afdf22010-10-06 12:27:54 +0200605 }
606
Robert Richter3d2606f2011-05-20 09:46:54 +0200607 ret = setup_ibs_ctl(offset);
Robert Richter27afdf22010-10-06 12:27:54 +0200608 if (ret)
Robert Richter3d2606f2011-05-20 09:46:54 +0200609 goto out;
Robert Richter27afdf22010-10-06 12:27:54 +0200610
Robert Richter3d2606f2011-05-20 09:46:54 +0200611 if (!ibs_eilvt_valid()) {
612 ret = -EFAULT;
613 goto out;
614 }
Robert Richter27afdf22010-10-06 12:27:54 +0200615
Robert Richter3d2606f2011-05-20 09:46:54 +0200616 pr_err(FW_BUG "using offset %d for IBS interrupts\n", offset);
Robert Richter27afdf22010-10-06 12:27:54 +0200617 pr_err(FW_BUG "workaround enabled for IBS LVT offset\n");
618
619 return 0;
Robert Richter3d2606f2011-05-20 09:46:54 +0200620out:
621 preempt_disable();
622 put_eilvt(offset);
623 preempt_enable();
624 return ret;
Robert Richter27afdf22010-10-06 12:27:54 +0200625}
626
Robert Richterc7c25802011-01-03 12:15:14 +0100627/*
628 * check and reserve APIC extended interrupt LVT offset for IBS if
629 * available
Robert Richterc7c25802011-01-03 12:15:14 +0100630 */
631
Robert Richterbae663b2010-05-05 17:47:17 +0200632static void init_ibs(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200633{
Robert Richter64683da2010-02-04 10:57:23 +0100634 ibs_caps = get_ibs_caps();
Robert Richter3d2606f2011-05-20 09:46:54 +0200635
Robert Richter64683da2010-02-04 10:57:23 +0100636 if (!ibs_caps)
Robert Richter3d2606f2011-05-20 09:46:54 +0200637 return;
638
639 if (ibs_eilvt_valid())
Robert Richterc7c25802011-01-03 12:15:14 +0100640 goto out;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200641
Robert Richter3d2606f2011-05-20 09:46:54 +0200642 if (!force_ibs_eilvt_setup())
643 goto out;
644
645 /* Failed to setup ibs */
646 ibs_caps = 0;
647 return;
Robert Richter852402c2008-07-22 21:09:06 +0200648
Robert Richterc7c25802011-01-03 12:15:14 +0100649out:
Robert Richter3d2606f2011-05-20 09:46:54 +0200650 printk(KERN_INFO "oprofile: AMD IBS detected (0x%08x)\n", ibs_caps);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200651}
652
Robert Richter25ad29132008-09-05 17:12:36 +0200653static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200654
Robert Richter25ad29132008-09-05 17:12:36 +0200655static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200656{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200657 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200658 int ret = 0;
659
660 /* architecture specific files */
661 if (create_arch_files)
662 ret = create_arch_files(sb, root);
663
664 if (ret)
665 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200666
Robert Richter64683da2010-02-04 10:57:23 +0100667 if (!ibs_caps)
Robert Richter270d3e12008-07-22 21:09:01 +0200668 return ret;
669
670 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200671
672 /* setup some reasonable defaults */
Robert Richter25da6952010-09-21 15:49:31 +0200673 memset(&ibs_config, 0, sizeof(ibs_config));
Barry Kasindorf56784f12008-07-22 21:08:55 +0200674 ibs_config.max_cnt_fetch = 250000;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200675 ibs_config.max_cnt_op = 250000;
Robert Richter2d55a472008-07-18 17:56:05 +0200676
Robert Richter4ac945f2010-09-21 15:58:32 +0200677 if (ibs_caps & IBS_CAPS_FETCHSAM) {
678 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
679 oprofilefs_create_ulong(sb, dir, "enable",
680 &ibs_config.fetch_enabled);
681 oprofilefs_create_ulong(sb, dir, "max_count",
682 &ibs_config.max_cnt_fetch);
683 oprofilefs_create_ulong(sb, dir, "rand_enable",
684 &ibs_config.rand_en);
685 }
Robert Richter2d55a472008-07-18 17:56:05 +0200686
Robert Richter4ac945f2010-09-21 15:58:32 +0200687 if (ibs_caps & IBS_CAPS_OPSAM) {
688 dir = oprofilefs_mkdir(sb, root, "ibs_op");
689 oprofilefs_create_ulong(sb, dir, "enable",
690 &ibs_config.op_enabled);
691 oprofilefs_create_ulong(sb, dir, "max_count",
692 &ibs_config.max_cnt_op);
693 if (ibs_caps & IBS_CAPS_OPCNT)
694 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
695 &ibs_config.dispatched_ops);
Robert Richter25da6952010-09-21 15:49:31 +0200696 if (ibs_caps & IBS_CAPS_BRNTRGT)
697 oprofilefs_create_ulong(sb, dir, "branch_target",
698 &ibs_config.branch_target);
Robert Richter4ac945f2010-09-21 15:58:32 +0200699 }
Robert Richterfc2bd732008-07-22 21:09:00 +0200700
701 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200702}
703
Robert Richterda169f52010-09-24 15:54:43 +0200704struct op_x86_model_spec op_amd_spec;
705
Robert Richteradf5ec02008-07-22 21:08:48 +0200706static int op_amd_init(struct oprofile_operations *ops)
707{
Robert Richterbae663b2010-05-05 17:47:17 +0200708 init_ibs();
Robert Richter270d3e12008-07-22 21:09:01 +0200709 create_arch_files = ops->create_files;
710 ops->create_files = setup_ibs_files;
Robert Richterda169f52010-09-24 15:54:43 +0200711
712 if (boot_cpu_data.x86 == 0x15) {
Robert Richteree5789d2011-09-21 11:30:17 +0200713 num_counters = AMD64_NUM_COUNTERS_F15H;
Robert Richterda169f52010-09-24 15:54:43 +0200714 } else {
Robert Richteree5789d2011-09-21 11:30:17 +0200715 num_counters = AMD64_NUM_COUNTERS;
Robert Richterda169f52010-09-24 15:54:43 +0200716 }
717
718 op_amd_spec.num_counters = num_counters;
719 op_amd_spec.num_controls = num_counters;
720 op_amd_spec.num_virt_counters = max(num_counters, NUM_VIRT_COUNTERS);
721
Robert Richteradf5ec02008-07-22 21:08:48 +0200722 return 0;
723}
724
Robert Richter259a83a2009-07-09 15:12:35 +0200725struct op_x86_model_spec op_amd_spec = {
Robert Richterda169f52010-09-24 15:54:43 +0200726 /* num_counters/num_controls filled in at runtime */
Robert Richter3370d352009-05-25 15:10:32 +0200727 .reserved = MSR_AMD_EVENTSEL_RESERVED,
728 .event_mask = OP_EVENT_MASK,
729 .init = op_amd_init,
Robert Richterc92960f2008-09-05 17:12:36 +0200730 .fill_in_addresses = &op_amd_fill_in_addresses,
731 .setup_ctrs = &op_amd_setup_ctrs,
Robert Richterbae663b2010-05-05 17:47:17 +0200732 .cpu_down = &op_amd_cpu_shutdown,
Robert Richterc92960f2008-09-05 17:12:36 +0200733 .check_ctrs = &op_amd_check_ctrs,
734 .start = &op_amd_start,
735 .stop = &op_amd_stop,
Robert Richter3370d352009-05-25 15:10:32 +0200736 .shutdown = &op_amd_shutdown,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200737#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
Robert Richter7e7478c2009-07-16 13:09:53 +0200738 .switch_ctrl = &op_mux_switch_ctrl,
Jason Yeh4d4036e2009-07-08 13:49:38 +0200739#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740};