blob: c6181c265ae092d679a46ca1c47bf6edaf4b601d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010030#define CTRL_CLEAR_LO(x) (x &= (1<<21))
31#define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010032#define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
33#define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
34#define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
35#define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Robert Richter852402c2008-07-22 21:09:06 +020037static unsigned long reset_value[NUM_COUNTERS];
38
39#ifdef CONFIG_OPROFILE_IBS
40
Robert Richter87f0bac2008-07-22 21:09:03 +020041/* IbsFetchCtl bits/masks */
42#define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
43#define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
44#define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
Barry Kasindorf56784f12008-07-22 21:08:55 +020045
Robert Richter87f0bac2008-07-22 21:09:03 +020046/*IbsOpCtl bits */
47#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
48#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
Barry Kasindorf56784f12008-07-22 21:08:55 +020049
Robert Richter1acda872009-01-05 10:35:31 +010050#define IBS_FETCH_SIZE 6
51#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020052
Robert Richterfc81be82008-12-18 00:28:27 +010053static int has_ibs; /* AMD Family10h and later */
Barry Kasindorf56784f12008-07-22 21:08:55 +020054
55struct op_ibs_config {
56 unsigned long op_enabled;
57 unsigned long fetch_enabled;
58 unsigned long max_cnt_fetch;
59 unsigned long max_cnt_op;
60 unsigned long rand_en;
61 unsigned long dispatched_ops;
62};
63
64static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010065
Robert Richter852402c2008-07-22 21:09:06 +020066#endif
67
Robert Richter6657fe42008-07-22 21:08:50 +020068/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +020069
Robert Richter6657fe42008-07-22 21:08:50 +020070static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Don Zickuscb9c4482006-09-26 10:52:26 +020072 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010074 for (i = 0; i < NUM_COUNTERS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020075 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
76 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020077 else
78 msrs->counters[i].addr = 0;
79 }
80
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010081 for (i = 0; i < NUM_CONTROLS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020082 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
83 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020084 else
85 msrs->controls[i].addr = 0;
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010089
Robert Richter6657fe42008-07-22 21:08:50 +020090static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 unsigned int low, high;
93 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +020096 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010097 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020098 continue;
Robert Richterd2731a42009-05-22 19:47:38 +020099 rdmsr(msrs->controls[i].addr, low, high);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100100 CTRL_CLEAR_LO(low);
101 CTRL_CLEAR_HI(high);
Robert Richterd2731a42009-05-22 19:47:38 +0200102 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200106 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100107 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200108 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200109 wrmsr(msrs->counters[i].addr, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
112 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200113 for (i = 0; i < NUM_COUNTERS; ++i) {
114 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
115 reset_value[i] = counter_config[i].count;
116
Robert Richterd2731a42009-05-22 19:47:38 +0200117 wrmsr(msrs->counters[i].addr, -(unsigned int)counter_config[i].count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Robert Richterd2731a42009-05-22 19:47:38 +0200119 rdmsr(msrs->controls[i].addr, low, high);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100120 CTRL_CLEAR_LO(low);
121 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 CTRL_SET_ENABLE(low);
Robert Richter4c168ea2008-09-24 11:08:52 +0200123 CTRL_SET_USR(low, counter_config[i].user);
124 CTRL_SET_KERN(low, counter_config[i].kernel);
125 CTRL_SET_UM(low, counter_config[i].unit_mask);
126 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
127 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100128 CTRL_SET_HOST_ONLY(high, 0);
129 CTRL_SET_GUEST_ONLY(high, 0);
130
Robert Richterd2731a42009-05-22 19:47:38 +0200131 wrmsr(msrs->controls[i].addr, low, high);
Robert Richter4c168ea2008-09-24 11:08:52 +0200132 } else {
133 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 }
136}
137
Robert Richter852402c2008-07-22 21:09:06 +0200138#ifdef CONFIG_OPROFILE_IBS
139
Robert Richter7939d2b2008-07-22 21:08:56 +0200140static inline int
141op_amd_handle_ibs(struct pt_regs * const regs,
142 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Robert Richter1acda872009-01-05 10:35:31 +0100144 u32 low, high;
145 u64 msr;
146 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Robert Richterfc81be82008-12-18 00:28:27 +0100148 if (!has_ibs)
Robert Richter7939d2b2008-07-22 21:08:56 +0200149 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Robert Richter7939d2b2008-07-22 21:08:56 +0200151 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200152 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200153 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100154 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100155 oprofile_write_reserve(&entry, regs, msr,
156 IBS_FETCH_CODE, IBS_FETCH_SIZE);
157 oprofile_add_data(&entry, (u32)msr);
158 oprofile_add_data(&entry, (u32)(msr >> 32));
159 oprofile_add_data(&entry, low);
160 oprofile_add_data(&entry, high);
Robert Richter1acda872009-01-05 10:35:31 +0100161 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100162 oprofile_add_data(&entry, (u32)msr);
163 oprofile_add_data(&entry, (u32)(msr >> 32));
164 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200165
Robert Richterfd13f6c2008-10-19 21:00:09 +0200166 /* reenable the IRQ */
Robert Richter87f0bac2008-07-22 21:09:03 +0200167 high &= ~IBS_FETCH_HIGH_VALID_BIT;
168 high |= IBS_FETCH_HIGH_ENABLE;
169 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200170 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
171 }
172 }
173
Robert Richter7939d2b2008-07-22 21:08:56 +0200174 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200175 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200176 if (low & IBS_OP_LOW_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100177 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100178 oprofile_write_reserve(&entry, regs, msr,
179 IBS_OP_CODE, IBS_OP_SIZE);
180 oprofile_add_data(&entry, (u32)msr);
181 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100182 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100183 oprofile_add_data(&entry, (u32)msr);
184 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100185 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100186 oprofile_add_data(&entry, (u32)msr);
187 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100188 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100189 oprofile_add_data(&entry, (u32)msr);
190 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100191 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100192 oprofile_add_data(&entry, (u32)msr);
193 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100194 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100195 oprofile_add_data(&entry, (u32)msr);
196 oprofile_add_data(&entry, (u32)(msr >> 32));
197 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200198
199 /* reenable the IRQ */
Robert Richter543a1572008-07-22 21:09:04 +0200200 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200201 low &= ~IBS_OP_LOW_VALID_BIT;
202 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200203 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
204 }
205 }
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 1;
208}
209
Robert Richter90637592009-03-10 19:15:57 +0100210static inline void op_amd_start_ibs(void)
211{
212 unsigned int low, high;
213 if (has_ibs && ibs_config.fetch_enabled) {
214 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
215 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
216 + IBS_FETCH_HIGH_ENABLE;
217 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
218 }
219
220 if (has_ibs && ibs_config.op_enabled) {
221 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
222 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
223 + IBS_OP_LOW_ENABLE;
224 high = 0;
225 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
226 }
227}
228
229static void op_amd_stop_ibs(void)
230{
231 unsigned int low, high;
232 if (has_ibs && ibs_config.fetch_enabled) {
233 /* clear max count and enable */
234 low = 0;
235 high = 0;
236 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
237 }
238
239 if (has_ibs && ibs_config.op_enabled) {
240 /* clear max count and enable */
241 low = 0;
242 high = 0;
243 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
244 }
245}
246
247#else
248
249static inline int op_amd_handle_ibs(struct pt_regs * const regs,
250 struct op_msrs const * const msrs) { }
251static inline void op_amd_start_ibs(void) { }
252static inline void op_amd_stop_ibs(void) { }
253
Robert Richter852402c2008-07-22 21:09:06 +0200254#endif
255
Robert Richter7939d2b2008-07-22 21:08:56 +0200256static int op_amd_check_ctrs(struct pt_regs * const regs,
257 struct op_msrs const * const msrs)
258{
259 unsigned int low, high;
260 int i;
261
Robert Richter4c168ea2008-09-24 11:08:52 +0200262 for (i = 0 ; i < NUM_COUNTERS; ++i) {
263 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200264 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200265 rdmsr(msrs->counters[i].addr, low, high);
Robert Richter7939d2b2008-07-22 21:08:56 +0200266 if (CTR_OVERFLOWED(low)) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200267 oprofile_add_sample(regs, i);
Robert Richterd2731a42009-05-22 19:47:38 +0200268 wrmsr(msrs->counters[i].addr, -(unsigned int)reset_value[i], -1);
Robert Richter7939d2b2008-07-22 21:08:56 +0200269 }
270 }
271
272 op_amd_handle_ibs(regs, msrs);
273
274 /* See op_model_ppro.c */
275 return 1;
276}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100277
Robert Richter6657fe42008-07-22 21:08:50 +0200278static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 unsigned int low, high;
281 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200282 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
283 if (reset_value[i]) {
Robert Richterd2731a42009-05-22 19:47:38 +0200284 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 CTRL_SET_ACTIVE(low);
Robert Richterd2731a42009-05-22 19:47:38 +0200286 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 }
Robert Richter852402c2008-07-22 21:09:06 +0200289
Robert Richter90637592009-03-10 19:15:57 +0100290 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Robert Richter6657fe42008-07-22 21:08:50 +0200293static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100295 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int i;
297
Robert Richterfd13f6c2008-10-19 21:00:09 +0200298 /*
299 * Subtle: stop on all counters to avoid race with setting our
300 * pm callback
301 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200302 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
303 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200304 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200305 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 CTRL_SET_INACTIVE(low);
Robert Richterd2731a42009-05-22 19:47:38 +0200307 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200309
Robert Richter90637592009-03-10 19:15:57 +0100310 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Robert Richter6657fe42008-07-22 21:08:50 +0200313static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200314{
315 int i;
316
Robert Richter4c168ea2008-09-24 11:08:52 +0200317 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100318 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200319 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
320 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200321 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100322 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200323 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
324 }
325}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Robert Richter9fa68122008-11-24 14:21:03 +0100327#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200328
Robert Richter7d77f2d2008-07-22 21:08:57 +0200329static u8 ibs_eilvt_off;
330
Barry Kasindorf56784f12008-07-22 21:08:55 +0200331static inline void apic_init_ibs_nmi_per_cpu(void *arg)
332{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200333 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200334}
335
336static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
337{
338 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
339}
340
Robert Richterfe615cb2008-11-24 14:58:03 +0100341static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200342{
343#define IBSCTL_LVTOFFSETVAL (1 << 8)
344#define IBSCTL 0x1cc
345 struct pci_dev *cpu_cfg;
346 int nodes;
347 u32 value = 0;
348
349 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200350 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200351
352 nodes = 0;
353 cpu_cfg = NULL;
354 do {
355 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
356 PCI_DEVICE_ID_AMD_10H_NB_MISC,
357 cpu_cfg);
358 if (!cpu_cfg)
359 break;
360 ++nodes;
361 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
362 | IBSCTL_LVTOFFSETVAL);
363 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
364 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100365 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200366 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
367 "IBSCTL = 0x%08x", value);
368 return 1;
369 }
370 } while (1);
371
372 if (!nodes) {
373 printk(KERN_DEBUG "No CPU node configured for IBS");
374 return 1;
375 }
376
377#ifdef CONFIG_NUMA
378 /* Sanity check */
379 /* Works only for 64bit with proper numa implementation. */
380 if (nodes != num_possible_nodes()) {
381 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
382 "found: %d, expected %d",
383 nodes, num_possible_nodes());
384 return 1;
385 }
386#endif
387 return 0;
388}
389
Robert Richterfe615cb2008-11-24 14:58:03 +0100390/* uninitialize the APIC for the IBS interrupts if needed */
391static void clear_ibs_nmi(void)
392{
Robert Richterfc81be82008-12-18 00:28:27 +0100393 if (has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100394 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
395}
396
Robert Richterfd13f6c2008-10-19 21:00:09 +0200397/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100398static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200399{
Robert Richterfc81be82008-12-18 00:28:27 +0100400 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200401
Robert Richterfc81be82008-12-18 00:28:27 +0100402 if (!has_ibs)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200403 return;
404
Robert Richterfe615cb2008-11-24 14:58:03 +0100405 if (init_ibs_nmi()) {
Robert Richterfc81be82008-12-18 00:28:27 +0100406 has_ibs = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200407 return;
408 }
409
410 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200411}
412
Robert Richterfe615cb2008-11-24 14:58:03 +0100413static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200414{
Robert Richterfc81be82008-12-18 00:28:27 +0100415 if (!has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100416 return;
417
418 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200419}
420
Robert Richter25ad2912008-09-05 17:12:36 +0200421static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200422
Robert Richter25ad2912008-09-05 17:12:36 +0200423static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200424{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200425 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200426 int ret = 0;
427
428 /* architecture specific files */
429 if (create_arch_files)
430 ret = create_arch_files(sb, root);
431
432 if (ret)
433 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200434
Robert Richterfc81be82008-12-18 00:28:27 +0100435 if (!has_ibs)
Robert Richter270d3e12008-07-22 21:09:01 +0200436 return ret;
437
438 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200439
440 /* setup some reasonable defaults */
441 ibs_config.max_cnt_fetch = 250000;
442 ibs_config.fetch_enabled = 0;
443 ibs_config.max_cnt_op = 250000;
444 ibs_config.op_enabled = 0;
445 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200446
447 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
448 oprofilefs_create_ulong(sb, dir, "enable",
449 &ibs_config.fetch_enabled);
450 oprofilefs_create_ulong(sb, dir, "max_count",
451 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200452 oprofilefs_create_ulong(sb, dir, "rand_enable",
453 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200454
Robert Richterccd755c2008-07-29 16:57:10 +0200455 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200456 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200457 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200458 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200459 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200460 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200461 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200462
463 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200464}
465
Robert Richteradf5ec02008-07-22 21:08:48 +0200466static int op_amd_init(struct oprofile_operations *ops)
467{
Robert Richterfe615cb2008-11-24 14:58:03 +0100468 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200469 create_arch_files = ops->create_files;
470 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200471 return 0;
472}
473
474static void op_amd_exit(void)
475{
Robert Richterfe615cb2008-11-24 14:58:03 +0100476 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200477}
478
Robert Richter9fa68122008-11-24 14:21:03 +0100479#else
480
481/* no IBS support */
482
483static int op_amd_init(struct oprofile_operations *ops)
484{
485 return 0;
486}
487
488static void op_amd_exit(void) {}
489
490#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200491
Robert Richter6657fe42008-07-22 21:08:50 +0200492struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200493 .init = op_amd_init,
494 .exit = op_amd_exit,
495 .num_counters = NUM_COUNTERS,
496 .num_controls = NUM_CONTROLS,
497 .fill_in_addresses = &op_amd_fill_in_addresses,
498 .setup_ctrs = &op_amd_setup_ctrs,
499 .check_ctrs = &op_amd_check_ctrs,
500 .start = &op_amd_start,
501 .stop = &op_amd_stop,
502 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503};