blob: aaa7ffaed6b985e9192f92b31285b214d233b645 [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 Richter6657fe42008-07-22 21:08:50 +020088static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 unsigned int low, high;
91 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +020094 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010095 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020096 continue;
Robert Richterd2731a42009-05-22 19:47:38 +020097 rdmsr(msrs->controls[i].addr, low, high);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010098 CTRL_CLEAR_LO(low);
99 CTRL_CLEAR_HI(high);
Robert Richterd2731a42009-05-22 19:47:38 +0200100 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200104 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100105 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200106 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200107 wrmsr(msrs->counters[i].addr, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
110 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200111 for (i = 0; i < NUM_COUNTERS; ++i) {
112 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
113 reset_value[i] = counter_config[i].count;
114
Robert Richterd2731a42009-05-22 19:47:38 +0200115 wrmsr(msrs->counters[i].addr, -(unsigned int)counter_config[i].count, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Robert Richterd2731a42009-05-22 19:47:38 +0200117 rdmsr(msrs->controls[i].addr, low, high);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100118 CTRL_CLEAR_LO(low);
119 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 CTRL_SET_ENABLE(low);
Robert Richter4c168ea2008-09-24 11:08:52 +0200121 CTRL_SET_USR(low, counter_config[i].user);
122 CTRL_SET_KERN(low, counter_config[i].kernel);
123 CTRL_SET_UM(low, counter_config[i].unit_mask);
124 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
125 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
Robert Richterd2731a42009-05-22 19:47:38 +0200126 wrmsr(msrs->controls[i].addr, low, high);
Robert Richter4c168ea2008-09-24 11:08:52 +0200127 } else {
128 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130 }
131}
132
Robert Richter852402c2008-07-22 21:09:06 +0200133#ifdef CONFIG_OPROFILE_IBS
134
Robert Richter7939d2b2008-07-22 21:08:56 +0200135static inline int
136op_amd_handle_ibs(struct pt_regs * const regs,
137 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Robert Richter1acda872009-01-05 10:35:31 +0100139 u32 low, high;
140 u64 msr;
141 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Robert Richterfc81be82008-12-18 00:28:27 +0100143 if (!has_ibs)
Robert Richter7939d2b2008-07-22 21:08:56 +0200144 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Robert Richter7939d2b2008-07-22 21:08:56 +0200146 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200147 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200148 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100149 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100150 oprofile_write_reserve(&entry, regs, msr,
151 IBS_FETCH_CODE, IBS_FETCH_SIZE);
152 oprofile_add_data(&entry, (u32)msr);
153 oprofile_add_data(&entry, (u32)(msr >> 32));
154 oprofile_add_data(&entry, low);
155 oprofile_add_data(&entry, high);
Robert Richter1acda872009-01-05 10:35:31 +0100156 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100157 oprofile_add_data(&entry, (u32)msr);
158 oprofile_add_data(&entry, (u32)(msr >> 32));
159 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200160
Robert Richterfd13f6c2008-10-19 21:00:09 +0200161 /* reenable the IRQ */
Robert Richter87f0bac2008-07-22 21:09:03 +0200162 high &= ~IBS_FETCH_HIGH_VALID_BIT;
163 high |= IBS_FETCH_HIGH_ENABLE;
164 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200165 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
166 }
167 }
168
Robert Richter7939d2b2008-07-22 21:08:56 +0200169 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200170 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200171 if (low & IBS_OP_LOW_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100172 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100173 oprofile_write_reserve(&entry, regs, msr,
174 IBS_OP_CODE, IBS_OP_SIZE);
175 oprofile_add_data(&entry, (u32)msr);
176 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100177 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100178 oprofile_add_data(&entry, (u32)msr);
179 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100180 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100181 oprofile_add_data(&entry, (u32)msr);
182 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100183 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100184 oprofile_add_data(&entry, (u32)msr);
185 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100186 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100187 oprofile_add_data(&entry, (u32)msr);
188 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100189 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100190 oprofile_add_data(&entry, (u32)msr);
191 oprofile_add_data(&entry, (u32)(msr >> 32));
192 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200193
194 /* reenable the IRQ */
Robert Richter543a1572008-07-22 21:09:04 +0200195 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200196 low &= ~IBS_OP_LOW_VALID_BIT;
197 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200198 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
199 }
200 }
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return 1;
203}
204
Robert Richter90637592009-03-10 19:15:57 +0100205static inline void op_amd_start_ibs(void)
206{
207 unsigned int low, high;
208 if (has_ibs && ibs_config.fetch_enabled) {
209 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
210 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
211 + IBS_FETCH_HIGH_ENABLE;
212 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
213 }
214
215 if (has_ibs && ibs_config.op_enabled) {
216 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
217 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
218 + IBS_OP_LOW_ENABLE;
219 high = 0;
220 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
221 }
222}
223
224static void op_amd_stop_ibs(void)
225{
226 unsigned int low, high;
227 if (has_ibs && ibs_config.fetch_enabled) {
228 /* clear max count and enable */
229 low = 0;
230 high = 0;
231 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
232 }
233
234 if (has_ibs && ibs_config.op_enabled) {
235 /* clear max count and enable */
236 low = 0;
237 high = 0;
238 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
239 }
240}
241
242#else
243
244static inline int op_amd_handle_ibs(struct pt_regs * const regs,
245 struct op_msrs const * const msrs) { }
246static inline void op_amd_start_ibs(void) { }
247static inline void op_amd_stop_ibs(void) { }
248
Robert Richter852402c2008-07-22 21:09:06 +0200249#endif
250
Robert Richter7939d2b2008-07-22 21:08:56 +0200251static int op_amd_check_ctrs(struct pt_regs * const regs,
252 struct op_msrs const * const msrs)
253{
254 unsigned int low, high;
255 int i;
256
Robert Richter4c168ea2008-09-24 11:08:52 +0200257 for (i = 0 ; i < NUM_COUNTERS; ++i) {
258 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200259 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200260 rdmsr(msrs->counters[i].addr, low, high);
Robert Richter7939d2b2008-07-22 21:08:56 +0200261 if (CTR_OVERFLOWED(low)) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200262 oprofile_add_sample(regs, i);
Robert Richterd2731a42009-05-22 19:47:38 +0200263 wrmsr(msrs->counters[i].addr, -(unsigned int)reset_value[i], -1);
Robert Richter7939d2b2008-07-22 21:08:56 +0200264 }
265 }
266
267 op_amd_handle_ibs(regs, msrs);
268
269 /* See op_model_ppro.c */
270 return 1;
271}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100272
Robert Richter6657fe42008-07-22 21:08:50 +0200273static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 unsigned int low, high;
276 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200277 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
278 if (reset_value[i]) {
Robert Richterd2731a42009-05-22 19:47:38 +0200279 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 CTRL_SET_ACTIVE(low);
Robert Richterd2731a42009-05-22 19:47:38 +0200281 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 }
Robert Richter852402c2008-07-22 21:09:06 +0200284
Robert Richter90637592009-03-10 19:15:57 +0100285 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Robert Richter6657fe42008-07-22 21:08:50 +0200288static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100290 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 int i;
292
Robert Richterfd13f6c2008-10-19 21:00:09 +0200293 /*
294 * Subtle: stop on all counters to avoid race with setting our
295 * pm callback
296 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200297 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
298 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200299 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200300 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 CTRL_SET_INACTIVE(low);
Robert Richterd2731a42009-05-22 19:47:38 +0200302 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200304
Robert Richter90637592009-03-10 19:15:57 +0100305 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Robert Richter6657fe42008-07-22 21:08:50 +0200308static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200309{
310 int i;
311
Robert Richter4c168ea2008-09-24 11:08:52 +0200312 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100313 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200314 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
315 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200316 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100317 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200318 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
319 }
320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Robert Richter9fa68122008-11-24 14:21:03 +0100322#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200323
Robert Richter7d77f2d2008-07-22 21:08:57 +0200324static u8 ibs_eilvt_off;
325
Barry Kasindorf56784f12008-07-22 21:08:55 +0200326static inline void apic_init_ibs_nmi_per_cpu(void *arg)
327{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200328 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200329}
330
331static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
332{
333 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
334}
335
Robert Richterfe615cb2008-11-24 14:58:03 +0100336static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200337{
338#define IBSCTL_LVTOFFSETVAL (1 << 8)
339#define IBSCTL 0x1cc
340 struct pci_dev *cpu_cfg;
341 int nodes;
342 u32 value = 0;
343
344 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200345 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200346
347 nodes = 0;
348 cpu_cfg = NULL;
349 do {
350 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
351 PCI_DEVICE_ID_AMD_10H_NB_MISC,
352 cpu_cfg);
353 if (!cpu_cfg)
354 break;
355 ++nodes;
356 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
357 | IBSCTL_LVTOFFSETVAL);
358 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
359 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100360 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200361 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
362 "IBSCTL = 0x%08x", value);
363 return 1;
364 }
365 } while (1);
366
367 if (!nodes) {
368 printk(KERN_DEBUG "No CPU node configured for IBS");
369 return 1;
370 }
371
372#ifdef CONFIG_NUMA
373 /* Sanity check */
374 /* Works only for 64bit with proper numa implementation. */
375 if (nodes != num_possible_nodes()) {
376 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
377 "found: %d, expected %d",
378 nodes, num_possible_nodes());
379 return 1;
380 }
381#endif
382 return 0;
383}
384
Robert Richterfe615cb2008-11-24 14:58:03 +0100385/* uninitialize the APIC for the IBS interrupts if needed */
386static void clear_ibs_nmi(void)
387{
Robert Richterfc81be82008-12-18 00:28:27 +0100388 if (has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100389 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
390}
391
Robert Richterfd13f6c2008-10-19 21:00:09 +0200392/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100393static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200394{
Robert Richterfc81be82008-12-18 00:28:27 +0100395 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200396
Robert Richterfc81be82008-12-18 00:28:27 +0100397 if (!has_ibs)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200398 return;
399
Robert Richterfe615cb2008-11-24 14:58:03 +0100400 if (init_ibs_nmi()) {
Robert Richterfc81be82008-12-18 00:28:27 +0100401 has_ibs = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200402 return;
403 }
404
405 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200406}
407
Robert Richterfe615cb2008-11-24 14:58:03 +0100408static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200409{
Robert Richterfc81be82008-12-18 00:28:27 +0100410 if (!has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100411 return;
412
413 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200414}
415
Robert Richter25ad2912008-09-05 17:12:36 +0200416static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200417
Robert Richter25ad2912008-09-05 17:12:36 +0200418static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200419{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200420 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200421 int ret = 0;
422
423 /* architecture specific files */
424 if (create_arch_files)
425 ret = create_arch_files(sb, root);
426
427 if (ret)
428 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200429
Robert Richterfc81be82008-12-18 00:28:27 +0100430 if (!has_ibs)
Robert Richter270d3e12008-07-22 21:09:01 +0200431 return ret;
432
433 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200434
435 /* setup some reasonable defaults */
436 ibs_config.max_cnt_fetch = 250000;
437 ibs_config.fetch_enabled = 0;
438 ibs_config.max_cnt_op = 250000;
439 ibs_config.op_enabled = 0;
440 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200441
442 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
443 oprofilefs_create_ulong(sb, dir, "enable",
444 &ibs_config.fetch_enabled);
445 oprofilefs_create_ulong(sb, dir, "max_count",
446 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200447 oprofilefs_create_ulong(sb, dir, "rand_enable",
448 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200449
Robert Richterccd755c2008-07-29 16:57:10 +0200450 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200451 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200452 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200453 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200454 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200455 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200456 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200457
458 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200459}
460
Robert Richteradf5ec02008-07-22 21:08:48 +0200461static int op_amd_init(struct oprofile_operations *ops)
462{
Robert Richterfe615cb2008-11-24 14:58:03 +0100463 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200464 create_arch_files = ops->create_files;
465 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200466 return 0;
467}
468
469static void op_amd_exit(void)
470{
Robert Richterfe615cb2008-11-24 14:58:03 +0100471 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200472}
473
Robert Richter9fa68122008-11-24 14:21:03 +0100474#else
475
476/* no IBS support */
477
478static int op_amd_init(struct oprofile_operations *ops)
479{
480 return 0;
481}
482
483static void op_amd_exit(void) {}
484
485#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200486
Robert Richter6657fe42008-07-22 21:08:50 +0200487struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200488 .init = op_amd_init,
489 .exit = op_amd_exit,
490 .num_counters = NUM_COUNTERS,
491 .num_controls = NUM_CONTROLS,
492 .fill_in_addresses = &op_amd_fill_in_addresses,
493 .setup_ctrs = &op_amd_setup_ctrs,
494 .check_ctrs = &op_amd_check_ctrs,
495 .start = &op_amd_start,
496 .stop = &op_amd_stop,
497 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498};