blob: b5d678fbf038a499c321fed8e914a7be6050f0bb [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
Robert Richter3370d352009-05-25 15:10:32 +020028#define OP_EVENT_MASK 0x0FFF
Robert Richter42399ad2009-05-25 17:59:06 +020029#define OP_CTR_OVERFLOW (1ULL<<31)
Robert Richter3370d352009-05-25 15:10:32 +020030
31#define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Robert Richter852402c2008-07-22 21:09:06 +020033static unsigned long reset_value[NUM_COUNTERS];
34
35#ifdef CONFIG_OPROFILE_IBS
36
Robert Richter87f0bac2008-07-22 21:09:03 +020037/* IbsFetchCtl bits/masks */
38#define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
39#define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
40#define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
Barry Kasindorf56784f12008-07-22 21:08:55 +020041
Robert Richter87f0bac2008-07-22 21:09:03 +020042/*IbsOpCtl bits */
43#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
44#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
Barry Kasindorf56784f12008-07-22 21:08:55 +020045
Robert Richter1acda872009-01-05 10:35:31 +010046#define IBS_FETCH_SIZE 6
47#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020048
Robert Richterfc81be82008-12-18 00:28:27 +010049static int has_ibs; /* AMD Family10h and later */
Barry Kasindorf56784f12008-07-22 21:08:55 +020050
51struct op_ibs_config {
52 unsigned long op_enabled;
53 unsigned long fetch_enabled;
54 unsigned long max_cnt_fetch;
55 unsigned long max_cnt_op;
56 unsigned long rand_en;
57 unsigned long dispatched_ops;
58};
59
60static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010061
Robert Richter852402c2008-07-22 21:09:06 +020062#endif
63
Robert Richter6657fe42008-07-22 21:08:50 +020064/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +020065
Robert Richter6657fe42008-07-22 21:08:50 +020066static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Don Zickuscb9c4482006-09-26 10:52:26 +020068 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010070 for (i = 0; i < NUM_COUNTERS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020071 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
72 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020073 else
74 msrs->counters[i].addr = 0;
75 }
76
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010077 for (i = 0; i < NUM_CONTROLS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020078 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
79 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020080 else
81 msrs->controls[i].addr = 0;
82 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Robert Richteref8828d2009-05-25 19:31:44 +020085static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
86 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Robert Richter3370d352009-05-25 15:10:32 +020088 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +020092 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010093 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +020094 continue;
Robert Richter3370d352009-05-25 15:10:32 +020095 rdmsrl(msrs->controls[i].addr, val);
96 val &= model->reserved;
97 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Don Zickuscb9c4482006-09-26 10:52:26 +020099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200101 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100102 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200103 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200104 wrmsr(msrs->counters[i].addr, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
107 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200108 for (i = 0; i < NUM_COUNTERS; ++i) {
109 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
110 reset_value[i] = counter_config[i].count;
Robert Richterd2731a42009-05-22 19:47:38 +0200111 wrmsr(msrs->counters[i].addr, -(unsigned int)counter_config[i].count, -1);
Robert Richter3370d352009-05-25 15:10:32 +0200112 rdmsrl(msrs->controls[i].addr, val);
113 val &= model->reserved;
114 val |= op_x86_get_ctrl(model, &counter_config[i]);
115 wrmsrl(msrs->controls[i].addr, val);
Robert Richter4c168ea2008-09-24 11:08:52 +0200116 } else {
117 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119 }
120}
121
Robert Richter852402c2008-07-22 21:09:06 +0200122#ifdef CONFIG_OPROFILE_IBS
123
Robert Richter7939d2b2008-07-22 21:08:56 +0200124static inline int
125op_amd_handle_ibs(struct pt_regs * const regs,
126 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Robert Richter1acda872009-01-05 10:35:31 +0100128 u32 low, high;
129 u64 msr;
130 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Robert Richterfc81be82008-12-18 00:28:27 +0100132 if (!has_ibs)
Robert Richter7939d2b2008-07-22 21:08:56 +0200133 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Robert Richter7939d2b2008-07-22 21:08:56 +0200135 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200136 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200137 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100138 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100139 oprofile_write_reserve(&entry, regs, msr,
140 IBS_FETCH_CODE, IBS_FETCH_SIZE);
141 oprofile_add_data(&entry, (u32)msr);
142 oprofile_add_data(&entry, (u32)(msr >> 32));
143 oprofile_add_data(&entry, low);
144 oprofile_add_data(&entry, high);
Robert Richter1acda872009-01-05 10:35:31 +0100145 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100146 oprofile_add_data(&entry, (u32)msr);
147 oprofile_add_data(&entry, (u32)(msr >> 32));
148 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200149
Robert Richterfd13f6c2008-10-19 21:00:09 +0200150 /* reenable the IRQ */
Robert Richter87f0bac2008-07-22 21:09:03 +0200151 high &= ~IBS_FETCH_HIGH_VALID_BIT;
152 high |= IBS_FETCH_HIGH_ENABLE;
153 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200154 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
155 }
156 }
157
Robert Richter7939d2b2008-07-22 21:08:56 +0200158 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200159 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200160 if (low & IBS_OP_LOW_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100161 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100162 oprofile_write_reserve(&entry, regs, msr,
163 IBS_OP_CODE, IBS_OP_SIZE);
164 oprofile_add_data(&entry, (u32)msr);
165 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100166 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100167 oprofile_add_data(&entry, (u32)msr);
168 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100169 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100170 oprofile_add_data(&entry, (u32)msr);
171 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100172 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100173 oprofile_add_data(&entry, (u32)msr);
174 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100175 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100176 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_IBSDCPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100179 oprofile_add_data(&entry, (u32)msr);
180 oprofile_add_data(&entry, (u32)(msr >> 32));
181 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200182
183 /* reenable the IRQ */
Robert Richter543a1572008-07-22 21:09:04 +0200184 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200185 low &= ~IBS_OP_LOW_VALID_BIT;
186 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200187 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
188 }
189 }
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 1;
192}
193
Robert Richter90637592009-03-10 19:15:57 +0100194static inline void op_amd_start_ibs(void)
195{
196 unsigned int low, high;
197 if (has_ibs && ibs_config.fetch_enabled) {
198 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
199 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
200 + IBS_FETCH_HIGH_ENABLE;
201 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
202 }
203
204 if (has_ibs && ibs_config.op_enabled) {
205 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
206 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
207 + IBS_OP_LOW_ENABLE;
208 high = 0;
209 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
210 }
211}
212
213static void op_amd_stop_ibs(void)
214{
215 unsigned int low, high;
216 if (has_ibs && ibs_config.fetch_enabled) {
217 /* clear max count and enable */
218 low = 0;
219 high = 0;
220 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
221 }
222
223 if (has_ibs && ibs_config.op_enabled) {
224 /* clear max count and enable */
225 low = 0;
226 high = 0;
227 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
228 }
229}
230
231#else
232
233static inline int op_amd_handle_ibs(struct pt_regs * const regs,
234 struct op_msrs const * const msrs) { }
235static inline void op_amd_start_ibs(void) { }
236static inline void op_amd_stop_ibs(void) { }
237
Robert Richter852402c2008-07-22 21:09:06 +0200238#endif
239
Robert Richter7939d2b2008-07-22 21:08:56 +0200240static int op_amd_check_ctrs(struct pt_regs * const regs,
241 struct op_msrs const * const msrs)
242{
Robert Richter42399ad2009-05-25 17:59:06 +0200243 u64 val;
Robert Richter7939d2b2008-07-22 21:08:56 +0200244 int i;
245
Robert Richter4c168ea2008-09-24 11:08:52 +0200246 for (i = 0 ; i < NUM_COUNTERS; ++i) {
247 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200248 continue;
Robert Richter42399ad2009-05-25 17:59:06 +0200249 rdmsrl(msrs->counters[i].addr, val);
250 /* bit is clear if overflowed: */
251 if (val & OP_CTR_OVERFLOW)
252 continue;
253 oprofile_add_sample(regs, i);
254 wrmsr(msrs->counters[i].addr, -(unsigned int)reset_value[i], -1);
Robert Richter7939d2b2008-07-22 21:08:56 +0200255 }
256
257 op_amd_handle_ibs(regs, msrs);
258
259 /* See op_model_ppro.c */
260 return 1;
261}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100262
Robert Richter6657fe42008-07-22 21:08:50 +0200263static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 unsigned int low, high;
266 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200267 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
268 if (reset_value[i]) {
Robert Richterd2731a42009-05-22 19:47:38 +0200269 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 CTRL_SET_ACTIVE(low);
Robert Richterd2731a42009-05-22 19:47:38 +0200271 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 }
Robert Richter852402c2008-07-22 21:09:06 +0200274
Robert Richter90637592009-03-10 19:15:57 +0100275 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Robert Richter6657fe42008-07-22 21:08:50 +0200278static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100280 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 int i;
282
Robert Richterfd13f6c2008-10-19 21:00:09 +0200283 /*
284 * Subtle: stop on all counters to avoid race with setting our
285 * pm callback
286 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200287 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
288 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200289 continue;
Robert Richterd2731a42009-05-22 19:47:38 +0200290 rdmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 CTRL_SET_INACTIVE(low);
Robert Richterd2731a42009-05-22 19:47:38 +0200292 wrmsr(msrs->controls[i].addr, low, high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200294
Robert Richter90637592009-03-10 19:15:57 +0100295 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Robert Richter6657fe42008-07-22 21:08:50 +0200298static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200299{
300 int i;
301
Robert Richter4c168ea2008-09-24 11:08:52 +0200302 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100303 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200304 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
305 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200306 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100307 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200308 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
309 }
310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Robert Richter9fa68122008-11-24 14:21:03 +0100312#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200313
Robert Richter7d77f2d2008-07-22 21:08:57 +0200314static u8 ibs_eilvt_off;
315
Barry Kasindorf56784f12008-07-22 21:08:55 +0200316static inline void apic_init_ibs_nmi_per_cpu(void *arg)
317{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200318 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200319}
320
321static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
322{
323 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
324}
325
Robert Richterfe615cb2008-11-24 14:58:03 +0100326static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200327{
328#define IBSCTL_LVTOFFSETVAL (1 << 8)
329#define IBSCTL 0x1cc
330 struct pci_dev *cpu_cfg;
331 int nodes;
332 u32 value = 0;
333
334 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200335 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200336
337 nodes = 0;
338 cpu_cfg = NULL;
339 do {
340 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
341 PCI_DEVICE_ID_AMD_10H_NB_MISC,
342 cpu_cfg);
343 if (!cpu_cfg)
344 break;
345 ++nodes;
346 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
347 | IBSCTL_LVTOFFSETVAL);
348 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
349 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100350 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200351 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
352 "IBSCTL = 0x%08x", value);
353 return 1;
354 }
355 } while (1);
356
357 if (!nodes) {
358 printk(KERN_DEBUG "No CPU node configured for IBS");
359 return 1;
360 }
361
362#ifdef CONFIG_NUMA
363 /* Sanity check */
364 /* Works only for 64bit with proper numa implementation. */
365 if (nodes != num_possible_nodes()) {
366 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
367 "found: %d, expected %d",
368 nodes, num_possible_nodes());
369 return 1;
370 }
371#endif
372 return 0;
373}
374
Robert Richterfe615cb2008-11-24 14:58:03 +0100375/* uninitialize the APIC for the IBS interrupts if needed */
376static void clear_ibs_nmi(void)
377{
Robert Richterfc81be82008-12-18 00:28:27 +0100378 if (has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100379 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
380}
381
Robert Richterfd13f6c2008-10-19 21:00:09 +0200382/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100383static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200384{
Robert Richterfc81be82008-12-18 00:28:27 +0100385 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200386
Robert Richterfc81be82008-12-18 00:28:27 +0100387 if (!has_ibs)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200388 return;
389
Robert Richterfe615cb2008-11-24 14:58:03 +0100390 if (init_ibs_nmi()) {
Robert Richterfc81be82008-12-18 00:28:27 +0100391 has_ibs = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200392 return;
393 }
394
395 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200396}
397
Robert Richterfe615cb2008-11-24 14:58:03 +0100398static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200399{
Robert Richterfc81be82008-12-18 00:28:27 +0100400 if (!has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100401 return;
402
403 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200404}
405
Robert Richter25ad29132008-09-05 17:12:36 +0200406static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200407
Robert Richter25ad29132008-09-05 17:12:36 +0200408static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200409{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200410 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200411 int ret = 0;
412
413 /* architecture specific files */
414 if (create_arch_files)
415 ret = create_arch_files(sb, root);
416
417 if (ret)
418 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200419
Robert Richterfc81be82008-12-18 00:28:27 +0100420 if (!has_ibs)
Robert Richter270d3e12008-07-22 21:09:01 +0200421 return ret;
422
423 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200424
425 /* setup some reasonable defaults */
426 ibs_config.max_cnt_fetch = 250000;
427 ibs_config.fetch_enabled = 0;
428 ibs_config.max_cnt_op = 250000;
429 ibs_config.op_enabled = 0;
430 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200431
432 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
433 oprofilefs_create_ulong(sb, dir, "enable",
434 &ibs_config.fetch_enabled);
435 oprofilefs_create_ulong(sb, dir, "max_count",
436 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200437 oprofilefs_create_ulong(sb, dir, "rand_enable",
438 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200439
Robert Richterccd755c2008-07-29 16:57:10 +0200440 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200441 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200442 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200443 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200444 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200445 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200446 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200447
448 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200449}
450
Robert Richteradf5ec02008-07-22 21:08:48 +0200451static int op_amd_init(struct oprofile_operations *ops)
452{
Robert Richterfe615cb2008-11-24 14:58:03 +0100453 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200454 create_arch_files = ops->create_files;
455 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200456 return 0;
457}
458
459static void op_amd_exit(void)
460{
Robert Richterfe615cb2008-11-24 14:58:03 +0100461 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200462}
463
Robert Richter9fa68122008-11-24 14:21:03 +0100464#else
465
466/* no IBS support */
467
468static int op_amd_init(struct oprofile_operations *ops)
469{
470 return 0;
471}
472
473static void op_amd_exit(void) {}
474
475#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200476
Robert Richter6657fe42008-07-22 21:08:50 +0200477struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200478 .num_counters = NUM_COUNTERS,
479 .num_controls = NUM_CONTROLS,
Robert Richter3370d352009-05-25 15:10:32 +0200480 .reserved = MSR_AMD_EVENTSEL_RESERVED,
481 .event_mask = OP_EVENT_MASK,
482 .init = op_amd_init,
483 .exit = op_amd_exit,
Robert Richterc92960f2008-09-05 17:12:36 +0200484 .fill_in_addresses = &op_amd_fill_in_addresses,
485 .setup_ctrs = &op_amd_setup_ctrs,
486 .check_ctrs = &op_amd_check_ctrs,
487 .start = &op_amd_start,
488 .stop = &op_amd_stop,
Robert Richter3370d352009-05-25 15:10:32 +0200489 .shutdown = &op_amd_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490};