blob: 4b9254a67e68f0e2212827aa06cd165dd452bf4b [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
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010029#define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
30#define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
32
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010033#define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
34#define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010035#define CTRL_CLEAR_LO(x) (x &= (1<<21))
36#define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010037#define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
38#define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
39#define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
40#define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Robert Richter852402c2008-07-22 21:09:06 +020042static unsigned long reset_value[NUM_COUNTERS];
43
44#ifdef CONFIG_OPROFILE_IBS
45
Robert Richter87f0bac2008-07-22 21:09:03 +020046/* IbsFetchCtl bits/masks */
47#define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
48#define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
49#define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
Barry Kasindorf56784f12008-07-22 21:08:55 +020050
Robert Richter87f0bac2008-07-22 21:09:03 +020051/*IbsOpCtl bits */
52#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
53#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
Barry Kasindorf56784f12008-07-22 21:08:55 +020054
Robert Richter1acda872009-01-05 10:35:31 +010055#define IBS_FETCH_SIZE 6
56#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020057
Robert Richterfc81be82008-12-18 00:28:27 +010058static int has_ibs; /* AMD Family10h and later */
Barry Kasindorf56784f12008-07-22 21:08:55 +020059
60struct op_ibs_config {
61 unsigned long op_enabled;
62 unsigned long fetch_enabled;
63 unsigned long max_cnt_fetch;
64 unsigned long max_cnt_op;
65 unsigned long rand_en;
66 unsigned long dispatched_ops;
67};
68
69static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010070
Robert Richter852402c2008-07-22 21:09:06 +020071#endif
72
Robert Richter6657fe42008-07-22 21:08:50 +020073/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +020074
Robert Richter6657fe42008-07-22 21:08:50 +020075static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Don Zickuscb9c4482006-09-26 10:52:26 +020077 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010079 for (i = 0; i < NUM_COUNTERS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020080 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
81 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020082 else
83 msrs->counters[i].addr = 0;
84 }
85
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010086 for (i = 0; i < NUM_CONTROLS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020087 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
88 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020089 else
90 msrs->controls[i].addr = 0;
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010094
Robert Richter6657fe42008-07-22 21:08:50 +020095static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 unsigned int low, high;
98 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200101 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100102 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200103 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100105 CTRL_CLEAR_LO(low);
106 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 CTRL_WRITE(low, high, msrs, i);
108 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200111 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100112 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200113 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 CTR_WRITE(1, msrs, i);
115 }
116
117 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200118 for (i = 0; i < NUM_COUNTERS; ++i) {
119 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
120 reset_value[i] = counter_config[i].count;
121
122 CTR_WRITE(counter_config[i].count, msrs, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100125 CTRL_CLEAR_LO(low);
126 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 CTRL_SET_ENABLE(low);
Robert Richter4c168ea2008-09-24 11:08:52 +0200128 CTRL_SET_USR(low, counter_config[i].user);
129 CTRL_SET_KERN(low, counter_config[i].kernel);
130 CTRL_SET_UM(low, counter_config[i].unit_mask);
131 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
132 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100133 CTRL_SET_HOST_ONLY(high, 0);
134 CTRL_SET_GUEST_ONLY(high, 0);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 CTRL_WRITE(low, high, msrs, i);
Robert Richter4c168ea2008-09-24 11:08:52 +0200137 } else {
138 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 }
141}
142
Robert Richter852402c2008-07-22 21:09:06 +0200143#ifdef CONFIG_OPROFILE_IBS
144
Robert Richter7939d2b2008-07-22 21:08:56 +0200145static inline int
146op_amd_handle_ibs(struct pt_regs * const regs,
147 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Robert Richter1acda872009-01-05 10:35:31 +0100149 u32 low, high;
150 u64 msr;
151 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Robert Richterfc81be82008-12-18 00:28:27 +0100153 if (!has_ibs)
Robert Richter7939d2b2008-07-22 21:08:56 +0200154 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Robert Richter7939d2b2008-07-22 21:08:56 +0200156 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200157 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200158 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100159 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100160 oprofile_write_reserve(&entry, regs, msr,
161 IBS_FETCH_CODE, IBS_FETCH_SIZE);
162 oprofile_add_data(&entry, (u32)msr);
163 oprofile_add_data(&entry, (u32)(msr >> 32));
164 oprofile_add_data(&entry, low);
165 oprofile_add_data(&entry, high);
Robert Richter1acda872009-01-05 10:35:31 +0100166 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100167 oprofile_add_data(&entry, (u32)msr);
168 oprofile_add_data(&entry, (u32)(msr >> 32));
169 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200170
Robert Richterfd13f6c2008-10-19 21:00:09 +0200171 /* reenable the IRQ */
Robert Richter87f0bac2008-07-22 21:09:03 +0200172 high &= ~IBS_FETCH_HIGH_VALID_BIT;
173 high |= IBS_FETCH_HIGH_ENABLE;
174 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200175 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
176 }
177 }
178
Robert Richter7939d2b2008-07-22 21:08:56 +0200179 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200180 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200181 if (low & IBS_OP_LOW_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100182 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100183 oprofile_write_reserve(&entry, regs, msr,
184 IBS_OP_CODE, IBS_OP_SIZE);
185 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_IBSOPDATA, 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_IBSOPDATA2, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100191 oprofile_add_data(&entry, (u32)msr);
192 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100193 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100194 oprofile_add_data(&entry, (u32)msr);
195 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100196 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100197 oprofile_add_data(&entry, (u32)msr);
198 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100199 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100200 oprofile_add_data(&entry, (u32)msr);
201 oprofile_add_data(&entry, (u32)(msr >> 32));
202 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200203
204 /* reenable the IRQ */
Robert Richter543a1572008-07-22 21:09:04 +0200205 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200206 low &= ~IBS_OP_LOW_VALID_BIT;
207 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200208 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
209 }
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return 1;
213}
214
Robert Richter90637592009-03-10 19:15:57 +0100215static inline void op_amd_start_ibs(void)
216{
217 unsigned int low, high;
218 if (has_ibs && ibs_config.fetch_enabled) {
219 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
220 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
221 + IBS_FETCH_HIGH_ENABLE;
222 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
223 }
224
225 if (has_ibs && ibs_config.op_enabled) {
226 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
227 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
228 + IBS_OP_LOW_ENABLE;
229 high = 0;
230 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
231 }
232}
233
234static void op_amd_stop_ibs(void)
235{
236 unsigned int low, high;
237 if (has_ibs && ibs_config.fetch_enabled) {
238 /* clear max count and enable */
239 low = 0;
240 high = 0;
241 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
242 }
243
244 if (has_ibs && ibs_config.op_enabled) {
245 /* clear max count and enable */
246 low = 0;
247 high = 0;
248 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
249 }
250}
251
252#else
253
254static inline int op_amd_handle_ibs(struct pt_regs * const regs,
255 struct op_msrs const * const msrs) { }
256static inline void op_amd_start_ibs(void) { }
257static inline void op_amd_stop_ibs(void) { }
258
Robert Richter852402c2008-07-22 21:09:06 +0200259#endif
260
Robert Richter7939d2b2008-07-22 21:08:56 +0200261static int op_amd_check_ctrs(struct pt_regs * const regs,
262 struct op_msrs const * const msrs)
263{
264 unsigned int low, high;
265 int i;
266
Robert Richter4c168ea2008-09-24 11:08:52 +0200267 for (i = 0 ; i < NUM_COUNTERS; ++i) {
268 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200269 continue;
270 CTR_READ(low, high, msrs, i);
271 if (CTR_OVERFLOWED(low)) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200272 oprofile_add_sample(regs, i);
273 CTR_WRITE(reset_value[i], msrs, i);
Robert Richter7939d2b2008-07-22 21:08:56 +0200274 }
275 }
276
277 op_amd_handle_ibs(regs, msrs);
278
279 /* See op_model_ppro.c */
280 return 1;
281}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100282
Robert Richter6657fe42008-07-22 21:08:50 +0200283static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 unsigned int low, high;
286 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200287 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
288 if (reset_value[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 CTRL_READ(low, high, msrs, i);
290 CTRL_SET_ACTIVE(low);
291 CTRL_WRITE(low, high, msrs, i);
292 }
293 }
Robert Richter852402c2008-07-22 21:09:06 +0200294
Robert Richter90637592009-03-10 19:15:57 +0100295 op_amd_start_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Robert Richter6657fe42008-07-22 21:08:50 +0200298static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100300 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int i;
302
Robert Richterfd13f6c2008-10-19 21:00:09 +0200303 /*
304 * Subtle: stop on all counters to avoid race with setting our
305 * pm callback
306 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200307 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
308 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200309 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 CTRL_READ(low, high, msrs, i);
311 CTRL_SET_INACTIVE(low);
312 CTRL_WRITE(low, high, msrs, i);
313 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200314
Robert Richter90637592009-03-10 19:15:57 +0100315 op_amd_stop_ibs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Robert Richter6657fe42008-07-22 21:08:50 +0200318static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200319{
320 int i;
321
Robert Richter4c168ea2008-09-24 11:08:52 +0200322 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100323 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200324 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
325 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200326 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100327 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200328 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
329 }
330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Robert Richter9fa68122008-11-24 14:21:03 +0100332#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200333
Robert Richter7d77f2d2008-07-22 21:08:57 +0200334static u8 ibs_eilvt_off;
335
Barry Kasindorf56784f12008-07-22 21:08:55 +0200336static inline void apic_init_ibs_nmi_per_cpu(void *arg)
337{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200338 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200339}
340
341static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
342{
343 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
344}
345
Robert Richterfe615cb2008-11-24 14:58:03 +0100346static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200347{
348#define IBSCTL_LVTOFFSETVAL (1 << 8)
349#define IBSCTL 0x1cc
350 struct pci_dev *cpu_cfg;
351 int nodes;
352 u32 value = 0;
353
354 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200355 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200356
357 nodes = 0;
358 cpu_cfg = NULL;
359 do {
360 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
361 PCI_DEVICE_ID_AMD_10H_NB_MISC,
362 cpu_cfg);
363 if (!cpu_cfg)
364 break;
365 ++nodes;
366 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
367 | IBSCTL_LVTOFFSETVAL);
368 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
369 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100370 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200371 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
372 "IBSCTL = 0x%08x", value);
373 return 1;
374 }
375 } while (1);
376
377 if (!nodes) {
378 printk(KERN_DEBUG "No CPU node configured for IBS");
379 return 1;
380 }
381
382#ifdef CONFIG_NUMA
383 /* Sanity check */
384 /* Works only for 64bit with proper numa implementation. */
385 if (nodes != num_possible_nodes()) {
386 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
387 "found: %d, expected %d",
388 nodes, num_possible_nodes());
389 return 1;
390 }
391#endif
392 return 0;
393}
394
Robert Richterfe615cb2008-11-24 14:58:03 +0100395/* uninitialize the APIC for the IBS interrupts if needed */
396static void clear_ibs_nmi(void)
397{
Robert Richterfc81be82008-12-18 00:28:27 +0100398 if (has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100399 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
400}
401
Robert Richterfd13f6c2008-10-19 21:00:09 +0200402/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100403static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200404{
Robert Richterfc81be82008-12-18 00:28:27 +0100405 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200406
Robert Richterfc81be82008-12-18 00:28:27 +0100407 if (!has_ibs)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200408 return;
409
Robert Richterfe615cb2008-11-24 14:58:03 +0100410 if (init_ibs_nmi()) {
Robert Richterfc81be82008-12-18 00:28:27 +0100411 has_ibs = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200412 return;
413 }
414
415 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200416}
417
Robert Richterfe615cb2008-11-24 14:58:03 +0100418static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200419{
Robert Richterfc81be82008-12-18 00:28:27 +0100420 if (!has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100421 return;
422
423 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200424}
425
Robert Richter25ad2912008-09-05 17:12:36 +0200426static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200427
Robert Richter25ad2912008-09-05 17:12:36 +0200428static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200429{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200430 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200431 int ret = 0;
432
433 /* architecture specific files */
434 if (create_arch_files)
435 ret = create_arch_files(sb, root);
436
437 if (ret)
438 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200439
Robert Richterfc81be82008-12-18 00:28:27 +0100440 if (!has_ibs)
Robert Richter270d3e12008-07-22 21:09:01 +0200441 return ret;
442
443 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200444
445 /* setup some reasonable defaults */
446 ibs_config.max_cnt_fetch = 250000;
447 ibs_config.fetch_enabled = 0;
448 ibs_config.max_cnt_op = 250000;
449 ibs_config.op_enabled = 0;
450 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200451
452 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
453 oprofilefs_create_ulong(sb, dir, "enable",
454 &ibs_config.fetch_enabled);
455 oprofilefs_create_ulong(sb, dir, "max_count",
456 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200457 oprofilefs_create_ulong(sb, dir, "rand_enable",
458 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200459
Robert Richterccd755c2008-07-29 16:57:10 +0200460 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200461 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200462 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200463 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200464 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200465 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200466 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200467
468 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200469}
470
Robert Richteradf5ec02008-07-22 21:08:48 +0200471static int op_amd_init(struct oprofile_operations *ops)
472{
Robert Richterfe615cb2008-11-24 14:58:03 +0100473 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200474 create_arch_files = ops->create_files;
475 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200476 return 0;
477}
478
479static void op_amd_exit(void)
480{
Robert Richterfe615cb2008-11-24 14:58:03 +0100481 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200482}
483
Robert Richter9fa68122008-11-24 14:21:03 +0100484#else
485
486/* no IBS support */
487
488static int op_amd_init(struct oprofile_operations *ops)
489{
490 return 0;
491}
492
493static void op_amd_exit(void) {}
494
495#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200496
Robert Richter6657fe42008-07-22 21:08:50 +0200497struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200498 .init = op_amd_init,
499 .exit = op_amd_exit,
500 .num_counters = NUM_COUNTERS,
501 .num_controls = NUM_CONTROLS,
502 .fill_in_addresses = &op_amd_fill_in_addresses,
503 .setup_ctrs = &op_amd_setup_ctrs,
504 .check_ctrs = &op_amd_check_ctrs,
505 .start = &op_amd_start,
506 .stop = &op_amd_stop,
507 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508};