blob: 9bf90176241191fafcecdd8806d27c64844d0a65 [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) {
Robert Richter217d3cf2009-06-04 02:36:44 +020093 if (unlikely(!msrs->controls[i].addr))
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) {
Robert Richter217d3cf2009-06-04 02:36:44 +0200102 if (unlikely(!msrs->counters[i].addr))
Don Zickuscb9c4482006-09-26 10:52:26 +0200103 continue;
Robert Richterbbc59862009-05-25 17:38:19 +0200104 wrmsrl(msrs->counters[i].addr, -1LL);
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) {
Robert Richter217d3cf2009-06-04 02:36:44 +0200109 if (counter_config[i].enabled && msrs->counters[i].addr) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200110 reset_value[i] = counter_config[i].count;
Robert Richterbbc59862009-05-25 17:38:19 +0200111 wrmsrl(msrs->counters[i].addr,
112 -(s64)counter_config[i].count);
Robert Richter3370d352009-05-25 15:10:32 +0200113 rdmsrl(msrs->controls[i].addr, val);
114 val &= model->reserved;
115 val |= op_x86_get_ctrl(model, &counter_config[i]);
116 wrmsrl(msrs->controls[i].addr, val);
Robert Richter4c168ea2008-09-24 11:08:52 +0200117 } else {
118 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 }
121}
122
Robert Richter852402c2008-07-22 21:09:06 +0200123#ifdef CONFIG_OPROFILE_IBS
124
Robert Richter7939d2b2008-07-22 21:08:56 +0200125static inline int
126op_amd_handle_ibs(struct pt_regs * const regs,
127 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Robert Richter1acda872009-01-05 10:35:31 +0100129 u32 low, high;
130 u64 msr;
131 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Robert Richterfc81be82008-12-18 00:28:27 +0100133 if (!has_ibs)
Robert Richter7939d2b2008-07-22 21:08:56 +0200134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Robert Richter7939d2b2008-07-22 21:08:56 +0200136 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200137 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200138 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100139 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100140 oprofile_write_reserve(&entry, regs, msr,
141 IBS_FETCH_CODE, IBS_FETCH_SIZE);
142 oprofile_add_data(&entry, (u32)msr);
143 oprofile_add_data(&entry, (u32)(msr >> 32));
144 oprofile_add_data(&entry, low);
145 oprofile_add_data(&entry, high);
Robert Richter1acda872009-01-05 10:35:31 +0100146 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100147 oprofile_add_data(&entry, (u32)msr);
148 oprofile_add_data(&entry, (u32)(msr >> 32));
149 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200150
Robert Richterfd13f6c2008-10-19 21:00:09 +0200151 /* reenable the IRQ */
Robert Richter87f0bac2008-07-22 21:09:03 +0200152 high &= ~IBS_FETCH_HIGH_VALID_BIT;
153 high |= IBS_FETCH_HIGH_ENABLE;
154 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200155 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
156 }
157 }
158
Robert Richter7939d2b2008-07-22 21:08:56 +0200159 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200160 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200161 if (low & IBS_OP_LOW_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100162 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100163 oprofile_write_reserve(&entry, regs, msr,
164 IBS_OP_CODE, IBS_OP_SIZE);
165 oprofile_add_data(&entry, (u32)msr);
166 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100167 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100168 oprofile_add_data(&entry, (u32)msr);
169 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100170 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100171 oprofile_add_data(&entry, (u32)msr);
172 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100173 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100174 oprofile_add_data(&entry, (u32)msr);
175 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100176 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100177 oprofile_add_data(&entry, (u32)msr);
178 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100179 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100180 oprofile_add_data(&entry, (u32)msr);
181 oprofile_add_data(&entry, (u32)(msr >> 32));
182 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200183
184 /* reenable the IRQ */
Robert Richter543a1572008-07-22 21:09:04 +0200185 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200186 low &= ~IBS_OP_LOW_VALID_BIT;
187 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200188 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
189 }
190 }
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 1;
193}
194
Robert Richter90637592009-03-10 19:15:57 +0100195static inline void op_amd_start_ibs(void)
196{
197 unsigned int low, high;
198 if (has_ibs && ibs_config.fetch_enabled) {
199 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
200 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
201 + IBS_FETCH_HIGH_ENABLE;
202 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
203 }
204
205 if (has_ibs && ibs_config.op_enabled) {
206 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
207 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
208 + IBS_OP_LOW_ENABLE;
209 high = 0;
210 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
211 }
212}
213
214static void op_amd_stop_ibs(void)
215{
216 unsigned int low, high;
217 if (has_ibs && ibs_config.fetch_enabled) {
218 /* clear max count and enable */
219 low = 0;
220 high = 0;
221 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
222 }
223
224 if (has_ibs && ibs_config.op_enabled) {
225 /* clear max count and enable */
226 low = 0;
227 high = 0;
228 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
229 }
230}
231
232#else
233
234static inline int op_amd_handle_ibs(struct pt_regs * const regs,
235 struct op_msrs const * const msrs) { }
236static inline void op_amd_start_ibs(void) { }
237static inline void op_amd_stop_ibs(void) { }
238
Robert Richter852402c2008-07-22 21:09:06 +0200239#endif
240
Robert Richter7939d2b2008-07-22 21:08:56 +0200241static int op_amd_check_ctrs(struct pt_regs * const regs,
242 struct op_msrs const * const msrs)
243{
Robert Richter42399ad2009-05-25 17:59:06 +0200244 u64 val;
Robert Richter7939d2b2008-07-22 21:08:56 +0200245 int i;
246
Robert Richter4c168ea2008-09-24 11:08:52 +0200247 for (i = 0 ; i < NUM_COUNTERS; ++i) {
248 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200249 continue;
Robert Richter42399ad2009-05-25 17:59:06 +0200250 rdmsrl(msrs->counters[i].addr, val);
251 /* bit is clear if overflowed: */
252 if (val & OP_CTR_OVERFLOW)
253 continue;
254 oprofile_add_sample(regs, i);
Robert Richterbbc59862009-05-25 17:38:19 +0200255 wrmsrl(msrs->counters[i].addr, -(s64)reset_value[i]);
Robert Richter7939d2b2008-07-22 21:08:56 +0200256 }
257
258 op_amd_handle_ibs(regs, msrs);
259
260 /* See op_model_ppro.c */
261 return 1;
262}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100263
Robert Richter6657fe42008-07-22 21:08:50 +0200264static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Robert Richterdea37662009-05-25 18:11:52 +0200266 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200268 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
269 if (reset_value[i]) {
Robert Richterdea37662009-05-25 18:11:52 +0200270 rdmsrl(msrs->controls[i].addr, val);
271 val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
272 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 }
Robert Richter852402c2008-07-22 21:09:06 +0200275
Robert Richter90637592009-03-10 19:15:57 +0100276 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Robert Richter6657fe42008-07-22 21:08:50 +0200279static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Robert Richterdea37662009-05-25 18:11:52 +0200281 u64 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int i;
283
Robert Richterfd13f6c2008-10-19 21:00:09 +0200284 /*
285 * Subtle: stop on all counters to avoid race with setting our
286 * pm callback
287 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200288 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
289 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200290 continue;
Robert Richterdea37662009-05-25 18:11:52 +0200291 rdmsrl(msrs->controls[i].addr, val);
292 val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
293 wrmsrl(msrs->controls[i].addr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200295
Robert Richter90637592009-03-10 19:15:57 +0100296 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Robert Richter6657fe42008-07-22 21:08:50 +0200299static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200300{
301 int i;
302
Robert Richter4c168ea2008-09-24 11:08:52 +0200303 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Robert Richter217d3cf2009-06-04 02:36:44 +0200304 if (msrs->counters[i].addr)
Don Zickuscb9c4482006-09-26 10:52:26 +0200305 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
306 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200307 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Robert Richter217d3cf2009-06-04 02:36:44 +0200308 if (msrs->controls[i].addr)
Don Zickuscb9c4482006-09-26 10:52:26 +0200309 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
310 }
311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Robert Richter9fa68122008-11-24 14:21:03 +0100313#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200314
Robert Richter7d77f2d2008-07-22 21:08:57 +0200315static u8 ibs_eilvt_off;
316
Barry Kasindorf56784f12008-07-22 21:08:55 +0200317static inline void apic_init_ibs_nmi_per_cpu(void *arg)
318{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200319 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200320}
321
322static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
323{
324 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
325}
326
Robert Richterfe615cb2008-11-24 14:58:03 +0100327static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200328{
329#define IBSCTL_LVTOFFSETVAL (1 << 8)
330#define IBSCTL 0x1cc
331 struct pci_dev *cpu_cfg;
332 int nodes;
333 u32 value = 0;
334
335 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200336 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200337
338 nodes = 0;
339 cpu_cfg = NULL;
340 do {
341 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
342 PCI_DEVICE_ID_AMD_10H_NB_MISC,
343 cpu_cfg);
344 if (!cpu_cfg)
345 break;
346 ++nodes;
347 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
348 | IBSCTL_LVTOFFSETVAL);
349 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
350 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100351 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200352 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
353 "IBSCTL = 0x%08x", value);
354 return 1;
355 }
356 } while (1);
357
358 if (!nodes) {
359 printk(KERN_DEBUG "No CPU node configured for IBS");
360 return 1;
361 }
362
363#ifdef CONFIG_NUMA
364 /* Sanity check */
365 /* Works only for 64bit with proper numa implementation. */
366 if (nodes != num_possible_nodes()) {
367 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
368 "found: %d, expected %d",
369 nodes, num_possible_nodes());
370 return 1;
371 }
372#endif
373 return 0;
374}
375
Robert Richterfe615cb2008-11-24 14:58:03 +0100376/* uninitialize the APIC for the IBS interrupts if needed */
377static void clear_ibs_nmi(void)
378{
Robert Richterfc81be82008-12-18 00:28:27 +0100379 if (has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100380 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
381}
382
Robert Richterfd13f6c2008-10-19 21:00:09 +0200383/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100384static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200385{
Robert Richterfc81be82008-12-18 00:28:27 +0100386 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200387
Robert Richterfc81be82008-12-18 00:28:27 +0100388 if (!has_ibs)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200389 return;
390
Robert Richterfe615cb2008-11-24 14:58:03 +0100391 if (init_ibs_nmi()) {
Robert Richterfc81be82008-12-18 00:28:27 +0100392 has_ibs = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200393 return;
394 }
395
396 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200397}
398
Robert Richterfe615cb2008-11-24 14:58:03 +0100399static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200400{
Robert Richterfc81be82008-12-18 00:28:27 +0100401 if (!has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100402 return;
403
404 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200405}
406
Robert Richter25ad2912008-09-05 17:12:36 +0200407static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200408
Robert Richter25ad2912008-09-05 17:12:36 +0200409static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200410{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200411 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200412 int ret = 0;
413
414 /* architecture specific files */
415 if (create_arch_files)
416 ret = create_arch_files(sb, root);
417
418 if (ret)
419 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200420
Robert Richterfc81be82008-12-18 00:28:27 +0100421 if (!has_ibs)
Robert Richter270d3e12008-07-22 21:09:01 +0200422 return ret;
423
424 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200425
426 /* setup some reasonable defaults */
427 ibs_config.max_cnt_fetch = 250000;
428 ibs_config.fetch_enabled = 0;
429 ibs_config.max_cnt_op = 250000;
430 ibs_config.op_enabled = 0;
431 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200432
433 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
434 oprofilefs_create_ulong(sb, dir, "enable",
435 &ibs_config.fetch_enabled);
436 oprofilefs_create_ulong(sb, dir, "max_count",
437 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200438 oprofilefs_create_ulong(sb, dir, "rand_enable",
439 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200440
Robert Richterccd755c2008-07-29 16:57:10 +0200441 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200442 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200443 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200444 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200445 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200446 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200447 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200448
449 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200450}
451
Robert Richteradf5ec02008-07-22 21:08:48 +0200452static int op_amd_init(struct oprofile_operations *ops)
453{
Robert Richterfe615cb2008-11-24 14:58:03 +0100454 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200455 create_arch_files = ops->create_files;
456 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200457 return 0;
458}
459
460static void op_amd_exit(void)
461{
Robert Richterfe615cb2008-11-24 14:58:03 +0100462 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200463}
464
Robert Richter9fa68122008-11-24 14:21:03 +0100465#else
466
467/* no IBS support */
468
469static int op_amd_init(struct oprofile_operations *ops)
470{
471 return 0;
472}
473
474static void op_amd_exit(void) {}
475
476#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200477
Robert Richter6657fe42008-07-22 21:08:50 +0200478struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200479 .num_counters = NUM_COUNTERS,
480 .num_controls = NUM_CONTROLS,
Robert Richter3370d352009-05-25 15:10:32 +0200481 .reserved = MSR_AMD_EVENTSEL_RESERVED,
482 .event_mask = OP_EVENT_MASK,
483 .init = op_amd_init,
484 .exit = op_amd_exit,
Robert Richterc92960f2008-09-05 17:12:36 +0200485 .fill_in_addresses = &op_amd_fill_in_addresses,
486 .setup_ctrs = &op_amd_setup_ctrs,
487 .check_ctrs = &op_amd_check_ctrs,
488 .start = &op_amd_start,
489 .stop = &op_amd_stop,
Robert Richter3370d352009-05-25 15:10:32 +0200490 .shutdown = &op_amd_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491};