blob: 86e0a01ba125c16dc821825f740f13680ee4dab7 [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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Robert Richter852402c2008-07-22 21:09:06 +020035static unsigned long reset_value[NUM_COUNTERS];
36
37#ifdef CONFIG_OPROFILE_IBS
38
Robert Richter87f0bac2008-07-22 21:09:03 +020039/* IbsFetchCtl bits/masks */
40#define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
41#define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
42#define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
Barry Kasindorf56784f12008-07-22 21:08:55 +020043
Robert Richter87f0bac2008-07-22 21:09:03 +020044/*IbsOpCtl bits */
45#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
46#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
Barry Kasindorf56784f12008-07-22 21:08:55 +020047
Robert Richter1acda872009-01-05 10:35:31 +010048#define IBS_FETCH_SIZE 6
49#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020050
Robert Richterfc81be82008-12-18 00:28:27 +010051static int has_ibs; /* AMD Family10h and later */
Barry Kasindorf56784f12008-07-22 21:08:55 +020052
53struct op_ibs_config {
54 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;
60};
61
62static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010063
Robert Richter852402c2008-07-22 21:09:06 +020064#endif
65
Robert Richter6657fe42008-07-22 21:08:50 +020066/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +020067
Robert Richter6657fe42008-07-22 21:08:50 +020068static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Don Zickuscb9c4482006-09-26 10:52:26 +020070 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010072 for (i = 0; i < NUM_COUNTERS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020073 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
74 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020075 else
76 msrs->counters[i].addr = 0;
77 }
78
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010079 for (i = 0; i < NUM_CONTROLS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020080 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
81 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020082 else
83 msrs->controls[i].addr = 0;
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010087
Robert Richteref8828d2009-05-25 19:31:44 +020088static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
89 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 unsigned int low, high;
92 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +020095 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010096 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020097 continue;
Robert Richterd2731a42009-05-22 19:47:38 +020098 rdmsr(msrs->controls[i].addr, low, high);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010099 CTRL_CLEAR_LO(low);
100 CTRL_CLEAR_HI(high);
Robert Richterd2731a42009-05-22 19:47:38 +0200101 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200105 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100106 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200107 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200108 wrmsr(msrs->counters[i].addr, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110
111 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200112 for (i = 0; i < NUM_COUNTERS; ++i) {
113 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
114 reset_value[i] = counter_config[i].count;
115
Robert Richterd2731a42009-05-22 19:47:38 +0200116 wrmsr(msrs->counters[i].addr, -(unsigned int)counter_config[i].count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Robert Richterd2731a42009-05-22 19:47:38 +0200118 rdmsr(msrs->controls[i].addr, low, high);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100119 CTRL_CLEAR_LO(low);
120 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 CTRL_SET_ENABLE(low);
Robert Richter4c168ea2008-09-24 11:08:52 +0200122 CTRL_SET_USR(low, counter_config[i].user);
123 CTRL_SET_KERN(low, counter_config[i].kernel);
124 CTRL_SET_UM(low, counter_config[i].unit_mask);
125 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
126 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
Robert Richterd2731a42009-05-22 19:47:38 +0200127 wrmsr(msrs->controls[i].addr, low, high);
Robert Richter4c168ea2008-09-24 11:08:52 +0200128 } else {
129 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 }
132}
133
Robert Richter852402c2008-07-22 21:09:06 +0200134#ifdef CONFIG_OPROFILE_IBS
135
Robert Richter7939d2b2008-07-22 21:08:56 +0200136static inline int
137op_amd_handle_ibs(struct pt_regs * const regs,
138 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Robert Richter1acda872009-01-05 10:35:31 +0100140 u32 low, high;
141 u64 msr;
142 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Robert Richterfc81be82008-12-18 00:28:27 +0100144 if (!has_ibs)
Robert Richter7939d2b2008-07-22 21:08:56 +0200145 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Robert Richter7939d2b2008-07-22 21:08:56 +0200147 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200148 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200149 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100150 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100151 oprofile_write_reserve(&entry, regs, msr,
152 IBS_FETCH_CODE, IBS_FETCH_SIZE);
153 oprofile_add_data(&entry, (u32)msr);
154 oprofile_add_data(&entry, (u32)(msr >> 32));
155 oprofile_add_data(&entry, low);
156 oprofile_add_data(&entry, high);
Robert Richter1acda872009-01-05 10:35:31 +0100157 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100158 oprofile_add_data(&entry, (u32)msr);
159 oprofile_add_data(&entry, (u32)(msr >> 32));
160 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200161
Robert Richterfd13f6c2008-10-19 21:00:09 +0200162 /* reenable the IRQ */
Robert Richter87f0bac2008-07-22 21:09:03 +0200163 high &= ~IBS_FETCH_HIGH_VALID_BIT;
164 high |= IBS_FETCH_HIGH_ENABLE;
165 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200166 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
167 }
168 }
169
Robert Richter7939d2b2008-07-22 21:08:56 +0200170 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200171 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200172 if (low & IBS_OP_LOW_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100173 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100174 oprofile_write_reserve(&entry, regs, msr,
175 IBS_OP_CODE, IBS_OP_SIZE);
176 oprofile_add_data(&entry, (u32)msr);
177 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100178 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100179 oprofile_add_data(&entry, (u32)msr);
180 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100181 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100182 oprofile_add_data(&entry, (u32)msr);
183 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100184 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100185 oprofile_add_data(&entry, (u32)msr);
186 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100187 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100188 oprofile_add_data(&entry, (u32)msr);
189 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100190 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100191 oprofile_add_data(&entry, (u32)msr);
192 oprofile_add_data(&entry, (u32)(msr >> 32));
193 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200194
195 /* reenable the IRQ */
Robert Richter543a1572008-07-22 21:09:04 +0200196 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200197 low &= ~IBS_OP_LOW_VALID_BIT;
198 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200199 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
200 }
201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return 1;
204}
205
Robert Richter90637592009-03-10 19:15:57 +0100206static inline void op_amd_start_ibs(void)
207{
208 unsigned int low, high;
209 if (has_ibs && ibs_config.fetch_enabled) {
210 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
211 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
212 + IBS_FETCH_HIGH_ENABLE;
213 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
214 }
215
216 if (has_ibs && ibs_config.op_enabled) {
217 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
218 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
219 + IBS_OP_LOW_ENABLE;
220 high = 0;
221 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
222 }
223}
224
225static void op_amd_stop_ibs(void)
226{
227 unsigned int low, high;
228 if (has_ibs && ibs_config.fetch_enabled) {
229 /* clear max count and enable */
230 low = 0;
231 high = 0;
232 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
233 }
234
235 if (has_ibs && ibs_config.op_enabled) {
236 /* clear max count and enable */
237 low = 0;
238 high = 0;
239 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
240 }
241}
242
243#else
244
245static inline int op_amd_handle_ibs(struct pt_regs * const regs,
246 struct op_msrs const * const msrs) { }
247static inline void op_amd_start_ibs(void) { }
248static inline void op_amd_stop_ibs(void) { }
249
Robert Richter852402c2008-07-22 21:09:06 +0200250#endif
251
Robert Richter7939d2b2008-07-22 21:08:56 +0200252static int op_amd_check_ctrs(struct pt_regs * const regs,
253 struct op_msrs const * const msrs)
254{
255 unsigned int low, high;
256 int i;
257
Robert Richter4c168ea2008-09-24 11:08:52 +0200258 for (i = 0 ; i < NUM_COUNTERS; ++i) {
259 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200260 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200261 rdmsr(msrs->counters[i].addr, low, high);
Robert Richter7939d2b2008-07-22 21:08:56 +0200262 if (CTR_OVERFLOWED(low)) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200263 oprofile_add_sample(regs, i);
Robert Richterd2731a42009-05-22 19:47:38 +0200264 wrmsr(msrs->counters[i].addr, -(unsigned int)reset_value[i], -1);
Robert Richter7939d2b2008-07-22 21:08:56 +0200265 }
266 }
267
268 op_amd_handle_ibs(regs, msrs);
269
270 /* See op_model_ppro.c */
271 return 1;
272}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100273
Robert Richter6657fe42008-07-22 21:08:50 +0200274static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 unsigned int low, high;
277 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200278 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
279 if (reset_value[i]) {
Robert Richterd2731a42009-05-22 19:47:38 +0200280 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 CTRL_SET_ACTIVE(low);
Robert Richterd2731a42009-05-22 19:47:38 +0200282 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 }
Robert Richter852402c2008-07-22 21:09:06 +0200285
Robert Richter90637592009-03-10 19:15:57 +0100286 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Robert Richter6657fe42008-07-22 21:08:50 +0200289static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100291 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 int i;
293
Robert Richterfd13f6c2008-10-19 21:00:09 +0200294 /*
295 * Subtle: stop on all counters to avoid race with setting our
296 * pm callback
297 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200298 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
299 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200300 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200301 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 CTRL_SET_INACTIVE(low);
Robert Richterd2731a42009-05-22 19:47:38 +0200303 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200305
Robert Richter90637592009-03-10 19:15:57 +0100306 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Robert Richter6657fe42008-07-22 21:08:50 +0200309static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200310{
311 int i;
312
Robert Richter4c168ea2008-09-24 11:08:52 +0200313 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100314 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200315 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
316 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200317 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100318 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200319 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
320 }
321}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Robert Richter9fa68122008-11-24 14:21:03 +0100323#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200324
Robert Richter7d77f2d2008-07-22 21:08:57 +0200325static u8 ibs_eilvt_off;
326
Barry Kasindorf56784f12008-07-22 21:08:55 +0200327static inline void apic_init_ibs_nmi_per_cpu(void *arg)
328{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200329 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200330}
331
332static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
333{
334 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
335}
336
Robert Richterfe615cb2008-11-24 14:58:03 +0100337static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200338{
339#define IBSCTL_LVTOFFSETVAL (1 << 8)
340#define IBSCTL 0x1cc
341 struct pci_dev *cpu_cfg;
342 int nodes;
343 u32 value = 0;
344
345 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200346 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200347
348 nodes = 0;
349 cpu_cfg = NULL;
350 do {
351 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
352 PCI_DEVICE_ID_AMD_10H_NB_MISC,
353 cpu_cfg);
354 if (!cpu_cfg)
355 break;
356 ++nodes;
357 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
358 | IBSCTL_LVTOFFSETVAL);
359 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
360 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100361 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200362 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
363 "IBSCTL = 0x%08x", value);
364 return 1;
365 }
366 } while (1);
367
368 if (!nodes) {
369 printk(KERN_DEBUG "No CPU node configured for IBS");
370 return 1;
371 }
372
373#ifdef CONFIG_NUMA
374 /* Sanity check */
375 /* Works only for 64bit with proper numa implementation. */
376 if (nodes != num_possible_nodes()) {
377 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
378 "found: %d, expected %d",
379 nodes, num_possible_nodes());
380 return 1;
381 }
382#endif
383 return 0;
384}
385
Robert Richterfe615cb2008-11-24 14:58:03 +0100386/* uninitialize the APIC for the IBS interrupts if needed */
387static void clear_ibs_nmi(void)
388{
Robert Richterfc81be82008-12-18 00:28:27 +0100389 if (has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100390 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
391}
392
Robert Richterfd13f6c2008-10-19 21:00:09 +0200393/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100394static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200395{
Robert Richterfc81be82008-12-18 00:28:27 +0100396 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200397
Robert Richterfc81be82008-12-18 00:28:27 +0100398 if (!has_ibs)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200399 return;
400
Robert Richterfe615cb2008-11-24 14:58:03 +0100401 if (init_ibs_nmi()) {
Robert Richterfc81be82008-12-18 00:28:27 +0100402 has_ibs = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200403 return;
404 }
405
406 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200407}
408
Robert Richterfe615cb2008-11-24 14:58:03 +0100409static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200410{
Robert Richterfc81be82008-12-18 00:28:27 +0100411 if (!has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100412 return;
413
414 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200415}
416
Robert Richter25ad2912008-09-05 17:12:36 +0200417static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200418
Robert Richter25ad2912008-09-05 17:12:36 +0200419static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200420{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200421 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200422 int ret = 0;
423
424 /* architecture specific files */
425 if (create_arch_files)
426 ret = create_arch_files(sb, root);
427
428 if (ret)
429 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200430
Robert Richterfc81be82008-12-18 00:28:27 +0100431 if (!has_ibs)
Robert Richter270d3e12008-07-22 21:09:01 +0200432 return ret;
433
434 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200435
436 /* setup some reasonable defaults */
437 ibs_config.max_cnt_fetch = 250000;
438 ibs_config.fetch_enabled = 0;
439 ibs_config.max_cnt_op = 250000;
440 ibs_config.op_enabled = 0;
441 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200442
443 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
444 oprofilefs_create_ulong(sb, dir, "enable",
445 &ibs_config.fetch_enabled);
446 oprofilefs_create_ulong(sb, dir, "max_count",
447 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200448 oprofilefs_create_ulong(sb, dir, "rand_enable",
449 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200450
Robert Richterccd755c2008-07-29 16:57:10 +0200451 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200452 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200453 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200454 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200455 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200456 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200457 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200458
459 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200460}
461
Robert Richteradf5ec02008-07-22 21:08:48 +0200462static int op_amd_init(struct oprofile_operations *ops)
463{
Robert Richterfe615cb2008-11-24 14:58:03 +0100464 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200465 create_arch_files = ops->create_files;
466 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200467 return 0;
468}
469
470static void op_amd_exit(void)
471{
Robert Richterfe615cb2008-11-24 14:58:03 +0100472 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200473}
474
Robert Richter9fa68122008-11-24 14:21:03 +0100475#else
476
477/* no IBS support */
478
479static int op_amd_init(struct oprofile_operations *ops)
480{
481 return 0;
482}
483
484static void op_amd_exit(void) {}
485
486#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200487
Robert Richter6657fe42008-07-22 21:08:50 +0200488struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200489 .init = op_amd_init,
490 .exit = op_amd_exit,
491 .num_counters = NUM_COUNTERS,
492 .num_controls = NUM_CONTROLS,
493 .fill_in_addresses = &op_amd_fill_in_addresses,
494 .setup_ctrs = &op_amd_setup_ctrs,
495 .check_ctrs = &op_amd_check_ctrs,
496 .start = &op_amd_start,
497 .stop = &op_amd_stop,
498 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499};