blob: b54c0880b7dca3da75a4bd01db7517720e6378e3 [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>
Barry Kasindorf56784f12008-07-22 21:08:55 +020012 * @author Barry Kasindorf
Robert Richterae735e92008-12-25 17:26:07 +010013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/oprofile.h>
Barry Kasindorf56784f12008-07-22 21:08:55 +020016#include <linux/device.h>
17#include <linux/pci.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/ptrace.h>
20#include <asm/msr.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020021#include <asm/nmi.h>
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "op_x86_model.h"
24#include "op_counter.h"
25
Robert Richter4c168ea2008-09-24 11:08:52 +020026#define NUM_COUNTERS 4
27#define NUM_CONTROLS 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010029#define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
30#define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
31#define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
33
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010034#define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
35#define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
36#define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define CTRL_SET_ACTIVE(n) (n |= (1<<22))
38#define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010039#define CTRL_CLEAR_LO(x) (x &= (1<<21))
40#define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define CTRL_SET_ENABLE(val) (val |= 1<<20)
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010042#define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
43#define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define CTRL_SET_UM(val, m) (val |= (m << 8))
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010045#define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
46#define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
47#define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
48#define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Robert Richter852402c2008-07-22 21:09:06 +020050static unsigned long reset_value[NUM_COUNTERS];
51
52#ifdef CONFIG_OPROFILE_IBS
53
Robert Richter87f0bac2008-07-22 21:09:03 +020054/* IbsFetchCtl bits/masks */
55#define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
56#define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
57#define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
Barry Kasindorf56784f12008-07-22 21:08:55 +020058
Robert Richter87f0bac2008-07-22 21:09:03 +020059/*IbsOpCtl bits */
60#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
61#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
Barry Kasindorf56784f12008-07-22 21:08:55 +020062
Robert Richter1acda872009-01-05 10:35:31 +010063#define IBS_FETCH_SIZE 6
64#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020065
Robert Richterfc81be82008-12-18 00:28:27 +010066static int has_ibs; /* AMD Family10h and later */
Barry Kasindorf56784f12008-07-22 21:08:55 +020067
68struct op_ibs_config {
69 unsigned long op_enabled;
70 unsigned long fetch_enabled;
71 unsigned long max_cnt_fetch;
72 unsigned long max_cnt_op;
73 unsigned long rand_en;
74 unsigned long dispatched_ops;
75};
76
77static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010078
Robert Richter852402c2008-07-22 21:09:06 +020079#endif
80
Robert Richter6657fe42008-07-22 21:08:50 +020081/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +020082
Robert Richter6657fe42008-07-22 21:08:50 +020083static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Don Zickuscb9c4482006-09-26 10:52:26 +020085 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010087 for (i = 0; i < NUM_COUNTERS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020088 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
89 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020090 else
91 msrs->counters[i].addr = 0;
92 }
93
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010094 for (i = 0; i < NUM_CONTROLS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020095 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
96 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020097 else
98 msrs->controls[i].addr = 0;
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100102
Robert Richter6657fe42008-07-22 21:08:50 +0200103static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 unsigned int low, high;
106 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200109 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100110 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200111 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100113 CTRL_CLEAR_LO(low);
114 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 CTRL_WRITE(low, high, msrs, i);
116 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200119 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100120 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200121 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 CTR_WRITE(1, msrs, i);
123 }
124
125 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200126 for (i = 0; i < NUM_COUNTERS; ++i) {
127 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
128 reset_value[i] = counter_config[i].count;
129
130 CTR_WRITE(counter_config[i].count, msrs, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100133 CTRL_CLEAR_LO(low);
134 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 CTRL_SET_ENABLE(low);
Robert Richter4c168ea2008-09-24 11:08:52 +0200136 CTRL_SET_USR(low, counter_config[i].user);
137 CTRL_SET_KERN(low, counter_config[i].kernel);
138 CTRL_SET_UM(low, counter_config[i].unit_mask);
139 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
140 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100141 CTRL_SET_HOST_ONLY(high, 0);
142 CTRL_SET_GUEST_ONLY(high, 0);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 CTRL_WRITE(low, high, msrs, i);
Robert Richter4c168ea2008-09-24 11:08:52 +0200145 } else {
146 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 }
149}
150
Robert Richter852402c2008-07-22 21:09:06 +0200151#ifdef CONFIG_OPROFILE_IBS
152
Robert Richter7939d2b2008-07-22 21:08:56 +0200153static inline int
154op_amd_handle_ibs(struct pt_regs * const regs,
155 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Robert Richter1acda872009-01-05 10:35:31 +0100157 u32 low, high;
158 u64 msr;
159 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Robert Richterfc81be82008-12-18 00:28:27 +0100161 if (!has_ibs)
Robert Richter7939d2b2008-07-22 21:08:56 +0200162 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Robert Richter7939d2b2008-07-22 21:08:56 +0200164 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200165 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200166 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100167 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100168 oprofile_write_reserve(&entry, regs, msr,
169 IBS_FETCH_CODE, IBS_FETCH_SIZE);
170 oprofile_add_data(&entry, (u32)msr);
171 oprofile_add_data(&entry, (u32)(msr >> 32));
172 oprofile_add_data(&entry, low);
173 oprofile_add_data(&entry, high);
Robert Richter1acda872009-01-05 10:35:31 +0100174 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100175 oprofile_add_data(&entry, (u32)msr);
176 oprofile_add_data(&entry, (u32)(msr >> 32));
177 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200178
Robert Richterfd13f6c2008-10-19 21:00:09 +0200179 /* reenable the IRQ */
Robert Richter87f0bac2008-07-22 21:09:03 +0200180 high &= ~IBS_FETCH_HIGH_VALID_BIT;
181 high |= IBS_FETCH_HIGH_ENABLE;
182 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200183 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
184 }
185 }
186
Robert Richter7939d2b2008-07-22 21:08:56 +0200187 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200188 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200189 if (low & IBS_OP_LOW_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100190 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100191 oprofile_write_reserve(&entry, regs, msr,
192 IBS_OP_CODE, IBS_OP_SIZE);
193 oprofile_add_data(&entry, (u32)msr);
194 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100195 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100196 oprofile_add_data(&entry, (u32)msr);
197 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100198 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100199 oprofile_add_data(&entry, (u32)msr);
200 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100201 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100202 oprofile_add_data(&entry, (u32)msr);
203 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100204 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100205 oprofile_add_data(&entry, (u32)msr);
206 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100207 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100208 oprofile_add_data(&entry, (u32)msr);
209 oprofile_add_data(&entry, (u32)(msr >> 32));
210 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200211
212 /* reenable the IRQ */
Robert Richter543a1572008-07-22 21:09:04 +0200213 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200214 low &= ~IBS_OP_LOW_VALID_BIT;
215 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200216 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
217 }
218 }
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 1;
221}
222
Robert Richter90637592009-03-10 19:15:57 +0100223static inline void op_amd_start_ibs(void)
224{
225 unsigned int low, high;
226 if (has_ibs && ibs_config.fetch_enabled) {
227 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
228 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
229 + IBS_FETCH_HIGH_ENABLE;
230 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
231 }
232
233 if (has_ibs && ibs_config.op_enabled) {
234 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
235 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
236 + IBS_OP_LOW_ENABLE;
237 high = 0;
238 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
239 }
240}
241
242static void op_amd_stop_ibs(void)
243{
244 unsigned int low, high;
245 if (has_ibs && ibs_config.fetch_enabled) {
246 /* clear max count and enable */
247 low = 0;
248 high = 0;
249 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
250 }
251
252 if (has_ibs && ibs_config.op_enabled) {
253 /* clear max count and enable */
254 low = 0;
255 high = 0;
256 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
257 }
258}
259
260#else
261
262static inline int op_amd_handle_ibs(struct pt_regs * const regs,
263 struct op_msrs const * const msrs) { }
264static inline void op_amd_start_ibs(void) { }
265static inline void op_amd_stop_ibs(void) { }
266
Robert Richter852402c2008-07-22 21:09:06 +0200267#endif
268
Robert Richter7939d2b2008-07-22 21:08:56 +0200269static int op_amd_check_ctrs(struct pt_regs * const regs,
270 struct op_msrs const * const msrs)
271{
272 unsigned int low, high;
273 int i;
274
Robert Richter4c168ea2008-09-24 11:08:52 +0200275 for (i = 0 ; i < NUM_COUNTERS; ++i) {
276 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200277 continue;
278 CTR_READ(low, high, msrs, i);
279 if (CTR_OVERFLOWED(low)) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200280 oprofile_add_sample(regs, i);
281 CTR_WRITE(reset_value[i], msrs, i);
Robert Richter7939d2b2008-07-22 21:08:56 +0200282 }
283 }
284
285 op_amd_handle_ibs(regs, msrs);
286
287 /* See op_model_ppro.c */
288 return 1;
289}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100290
Robert Richter6657fe42008-07-22 21:08:50 +0200291static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 unsigned int low, high;
294 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200295 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
296 if (reset_value[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 CTRL_READ(low, high, msrs, i);
298 CTRL_SET_ACTIVE(low);
299 CTRL_WRITE(low, high, msrs, i);
300 }
301 }
Robert Richter852402c2008-07-22 21:09:06 +0200302
Robert Richter90637592009-03-10 19:15:57 +0100303 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Robert Richter6657fe42008-07-22 21:08:50 +0200306static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100308 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 int i;
310
Robert Richterfd13f6c2008-10-19 21:00:09 +0200311 /*
312 * Subtle: stop on all counters to avoid race with setting our
313 * pm callback
314 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200315 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
316 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200317 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 CTRL_READ(low, high, msrs, i);
319 CTRL_SET_INACTIVE(low);
320 CTRL_WRITE(low, high, msrs, i);
321 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200322
Robert Richter90637592009-03-10 19:15:57 +0100323 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Robert Richter6657fe42008-07-22 21:08:50 +0200326static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200327{
328 int i;
329
Robert Richter4c168ea2008-09-24 11:08:52 +0200330 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100331 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200332 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
333 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200334 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100335 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200336 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
337 }
338}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Robert Richter9fa68122008-11-24 14:21:03 +0100340#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200341
Robert Richter7d77f2d2008-07-22 21:08:57 +0200342static u8 ibs_eilvt_off;
343
Barry Kasindorf56784f12008-07-22 21:08:55 +0200344static inline void apic_init_ibs_nmi_per_cpu(void *arg)
345{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200346 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200347}
348
349static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
350{
351 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
352}
353
Robert Richterfe615cb2008-11-24 14:58:03 +0100354static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200355{
356#define IBSCTL_LVTOFFSETVAL (1 << 8)
357#define IBSCTL 0x1cc
358 struct pci_dev *cpu_cfg;
359 int nodes;
360 u32 value = 0;
361
362 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200363 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200364
365 nodes = 0;
366 cpu_cfg = NULL;
367 do {
368 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
369 PCI_DEVICE_ID_AMD_10H_NB_MISC,
370 cpu_cfg);
371 if (!cpu_cfg)
372 break;
373 ++nodes;
374 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
375 | IBSCTL_LVTOFFSETVAL);
376 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
377 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100378 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200379 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
380 "IBSCTL = 0x%08x", value);
381 return 1;
382 }
383 } while (1);
384
385 if (!nodes) {
386 printk(KERN_DEBUG "No CPU node configured for IBS");
387 return 1;
388 }
389
390#ifdef CONFIG_NUMA
391 /* Sanity check */
392 /* Works only for 64bit with proper numa implementation. */
393 if (nodes != num_possible_nodes()) {
394 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
395 "found: %d, expected %d",
396 nodes, num_possible_nodes());
397 return 1;
398 }
399#endif
400 return 0;
401}
402
Robert Richterfe615cb2008-11-24 14:58:03 +0100403/* uninitialize the APIC for the IBS interrupts if needed */
404static void clear_ibs_nmi(void)
405{
Robert Richterfc81be82008-12-18 00:28:27 +0100406 if (has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100407 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
408}
409
Robert Richterfd13f6c2008-10-19 21:00:09 +0200410/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100411static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200412{
Robert Richterfc81be82008-12-18 00:28:27 +0100413 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200414
Robert Richterfc81be82008-12-18 00:28:27 +0100415 if (!has_ibs)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200416 return;
417
Robert Richterfe615cb2008-11-24 14:58:03 +0100418 if (init_ibs_nmi()) {
Robert Richterfc81be82008-12-18 00:28:27 +0100419 has_ibs = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200420 return;
421 }
422
423 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200424}
425
Robert Richterfe615cb2008-11-24 14:58:03 +0100426static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200427{
Robert Richterfc81be82008-12-18 00:28:27 +0100428 if (!has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100429 return;
430
431 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200432}
433
Robert Richter25ad2912008-09-05 17:12:36 +0200434static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200435
Robert Richter25ad2912008-09-05 17:12:36 +0200436static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200437{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200438 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200439 int ret = 0;
440
441 /* architecture specific files */
442 if (create_arch_files)
443 ret = create_arch_files(sb, root);
444
445 if (ret)
446 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200447
Robert Richterfc81be82008-12-18 00:28:27 +0100448 if (!has_ibs)
Robert Richter270d3e12008-07-22 21:09:01 +0200449 return ret;
450
451 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200452
453 /* setup some reasonable defaults */
454 ibs_config.max_cnt_fetch = 250000;
455 ibs_config.fetch_enabled = 0;
456 ibs_config.max_cnt_op = 250000;
457 ibs_config.op_enabled = 0;
458 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200459
460 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
461 oprofilefs_create_ulong(sb, dir, "enable",
462 &ibs_config.fetch_enabled);
463 oprofilefs_create_ulong(sb, dir, "max_count",
464 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200465 oprofilefs_create_ulong(sb, dir, "rand_enable",
466 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200467
Robert Richterccd755c2008-07-29 16:57:10 +0200468 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200469 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200470 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200471 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200472 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200473 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200474 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200475
476 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200477}
478
Robert Richteradf5ec02008-07-22 21:08:48 +0200479static int op_amd_init(struct oprofile_operations *ops)
480{
Robert Richterfe615cb2008-11-24 14:58:03 +0100481 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200482 create_arch_files = ops->create_files;
483 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200484 return 0;
485}
486
487static void op_amd_exit(void)
488{
Robert Richterfe615cb2008-11-24 14:58:03 +0100489 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200490}
491
Robert Richter9fa68122008-11-24 14:21:03 +0100492#else
493
494/* no IBS support */
495
496static int op_amd_init(struct oprofile_operations *ops)
497{
498 return 0;
499}
500
501static void op_amd_exit(void) {}
502
503#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200504
Robert Richter6657fe42008-07-22 21:08:50 +0200505struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200506 .init = op_amd_init,
507 .exit = op_amd_exit,
508 .num_counters = NUM_COUNTERS,
509 .num_controls = NUM_CONTROLS,
510 .fill_in_addresses = &op_amd_fill_in_addresses,
511 .setup_ctrs = &op_amd_setup_ctrs,
512 .check_ctrs = &op_amd_check_ctrs,
513 .start = &op_amd_start,
514 .stop = &op_amd_stop,
515 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516};