blob: fb67e1999d8524b7edc58c446c5eb3b11eb51d0d [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 Richteradf5ec02008-07-22 21:08:48 +02005 * @remark Copyright 2002-2008 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 Richteradf5ec02008-07-22 21:08:48 +020013*/
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_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
30#define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
31#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 -070032#define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
33
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010034#define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
35#define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
36#define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define CTRL_SET_ACTIVE(n) (n |= (1<<22))
38#define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010039#define CTRL_CLEAR_LO(x) (x &= (1<<21))
40#define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define CTRL_SET_ENABLE(val) (val |= 1<<20)
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010042#define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
43#define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define CTRL_SET_UM(val, m) (val |= (m << 8))
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010045#define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
46#define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
47#define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
48#define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Robert Richter852402c2008-07-22 21:09:06 +020050static unsigned long reset_value[NUM_COUNTERS];
51
52#ifdef CONFIG_OPROFILE_IBS
53
Robert Richter87f0bac2008-07-22 21:09:03 +020054/* IbsFetchCtl bits/masks */
55#define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
56#define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
57#define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
Barry Kasindorf56784f12008-07-22 21:08:55 +020058
Robert Richter87f0bac2008-07-22 21:09:03 +020059/*IbsOpCtl bits */
60#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
61#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
Barry Kasindorf56784f12008-07-22 21:08:55 +020062
63/* Codes used in cpu_buffer.c */
Robert Richter87f0bac2008-07-22 21:09:03 +020064/* This produces duplicate code, need to be fixed */
Barry Kasindorf56784f12008-07-22 21:08:55 +020065#define IBS_FETCH_BEGIN 3
66#define IBS_OP_BEGIN 4
67
Robert Richterfd13f6c2008-10-19 21:00:09 +020068/*
69 * The function interface needs to be fixed, something like add
70 * data. Should then be added to linux/oprofile.h.
71 */
Robert Richtere2fee272008-07-18 17:36:20 +020072extern void
73oprofile_add_ibs_sample(struct pt_regs *const regs,
Robert Richter25ad29132008-09-05 17:12:36 +020074 unsigned int *const ibs_sample, int ibs_code);
Robert Richter90645702008-07-22 21:08:58 +020075
Barry Kasindorf56784f12008-07-22 21:08:55 +020076struct ibs_fetch_sample {
77 /* MSRC001_1031 IBS Fetch Linear Address Register */
78 unsigned int ibs_fetch_lin_addr_low;
79 unsigned int ibs_fetch_lin_addr_high;
80 /* MSRC001_1030 IBS Fetch Control Register */
81 unsigned int ibs_fetch_ctl_low;
82 unsigned int ibs_fetch_ctl_high;
83 /* MSRC001_1032 IBS Fetch Physical Address Register */
84 unsigned int ibs_fetch_phys_addr_low;
85 unsigned int ibs_fetch_phys_addr_high;
86};
87
88struct ibs_op_sample {
89 /* MSRC001_1034 IBS Op Logical Address Register (IbsRIP) */
90 unsigned int ibs_op_rip_low;
91 unsigned int ibs_op_rip_high;
92 /* MSRC001_1035 IBS Op Data Register */
93 unsigned int ibs_op_data1_low;
94 unsigned int ibs_op_data1_high;
95 /* MSRC001_1036 IBS Op Data 2 Register */
96 unsigned int ibs_op_data2_low;
97 unsigned int ibs_op_data2_high;
98 /* MSRC001_1037 IBS Op Data 3 Register */
99 unsigned int ibs_op_data3_low;
100 unsigned int ibs_op_data3_high;
101 /* MSRC001_1038 IBS DC Linear Address Register (IbsDcLinAd) */
102 unsigned int ibs_dc_linear_low;
103 unsigned int ibs_dc_linear_high;
104 /* MSRC001_1039 IBS DC Physical Address Register (IbsDcPhysAd) */
105 unsigned int ibs_dc_phys_low;
106 unsigned int ibs_dc_phys_high;
107};
108
109/*
110 * unitialize the APIC for the IBS interrupts if needed on AMD Family10h+
Robert Richterfd13f6c2008-10-19 21:00:09 +0200111 */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200112static void clear_ibs_nmi(void);
113
Barry Kasindorf56784f12008-07-22 21:08:55 +0200114static int ibs_allowed; /* AMD Family10h and later */
115
116struct op_ibs_config {
117 unsigned long op_enabled;
118 unsigned long fetch_enabled;
119 unsigned long max_cnt_fetch;
120 unsigned long max_cnt_op;
121 unsigned long rand_en;
122 unsigned long dispatched_ops;
123};
124
125static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100126
Robert Richter852402c2008-07-22 21:09:06 +0200127#endif
128
Robert Richter6657fe42008-07-22 21:08:50 +0200129/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +0200130
Robert Richter6657fe42008-07-22 21:08:50 +0200131static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Don Zickuscb9c4482006-09-26 10:52:26 +0200133 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100135 for (i = 0; i < NUM_COUNTERS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200136 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
137 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200138 else
139 msrs->counters[i].addr = 0;
140 }
141
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100142 for (i = 0; i < NUM_CONTROLS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200143 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
144 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200145 else
146 msrs->controls[i].addr = 0;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100150
Robert Richter6657fe42008-07-22 21:08:50 +0200151static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 unsigned int low, high;
154 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200157 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100158 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200159 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100161 CTRL_CLEAR_LO(low);
162 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 CTRL_WRITE(low, high, msrs, i);
164 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200167 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100168 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200169 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 CTR_WRITE(1, msrs, i);
171 }
172
173 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200174 for (i = 0; i < NUM_COUNTERS; ++i) {
175 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
176 reset_value[i] = counter_config[i].count;
177
178 CTR_WRITE(counter_config[i].count, msrs, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100181 CTRL_CLEAR_LO(low);
182 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 CTRL_SET_ENABLE(low);
Robert Richter4c168ea2008-09-24 11:08:52 +0200184 CTRL_SET_USR(low, counter_config[i].user);
185 CTRL_SET_KERN(low, counter_config[i].kernel);
186 CTRL_SET_UM(low, counter_config[i].unit_mask);
187 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
188 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100189 CTRL_SET_HOST_ONLY(high, 0);
190 CTRL_SET_GUEST_ONLY(high, 0);
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 CTRL_WRITE(low, high, msrs, i);
Robert Richter4c168ea2008-09-24 11:08:52 +0200193 } else {
194 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 }
197}
198
Robert Richter852402c2008-07-22 21:09:06 +0200199#ifdef CONFIG_OPROFILE_IBS
200
Robert Richter7939d2b2008-07-22 21:08:56 +0200201static inline int
202op_amd_handle_ibs(struct pt_regs * const regs,
203 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 unsigned int low, high;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200206 struct ibs_fetch_sample ibs_fetch;
207 struct ibs_op_sample ibs_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Robert Richter7939d2b2008-07-22 21:08:56 +0200209 if (!ibs_allowed)
210 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Robert Richter7939d2b2008-07-22 21:08:56 +0200212 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200213 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200214 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200215 ibs_fetch.ibs_fetch_ctl_high = high;
216 ibs_fetch.ibs_fetch_ctl_low = low;
217 rdmsr(MSR_AMD64_IBSFETCHLINAD, low, high);
218 ibs_fetch.ibs_fetch_lin_addr_high = high;
219 ibs_fetch.ibs_fetch_lin_addr_low = low;
220 rdmsr(MSR_AMD64_IBSFETCHPHYSAD, low, high);
221 ibs_fetch.ibs_fetch_phys_addr_high = high;
222 ibs_fetch.ibs_fetch_phys_addr_low = low;
223
224 oprofile_add_ibs_sample(regs,
225 (unsigned int *)&ibs_fetch,
226 IBS_FETCH_BEGIN);
227
Robert Richterfd13f6c2008-10-19 21:00:09 +0200228 /* reenable the IRQ */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200229 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200230 high &= ~IBS_FETCH_HIGH_VALID_BIT;
231 high |= IBS_FETCH_HIGH_ENABLE;
232 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200233 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
234 }
235 }
236
Robert Richter7939d2b2008-07-22 21:08:56 +0200237 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200238 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200239 if (low & IBS_OP_LOW_VALID_BIT) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200240 rdmsr(MSR_AMD64_IBSOPRIP, low, high);
241 ibs_op.ibs_op_rip_low = low;
242 ibs_op.ibs_op_rip_high = high;
243 rdmsr(MSR_AMD64_IBSOPDATA, low, high);
244 ibs_op.ibs_op_data1_low = low;
245 ibs_op.ibs_op_data1_high = high;
246 rdmsr(MSR_AMD64_IBSOPDATA2, low, high);
247 ibs_op.ibs_op_data2_low = low;
248 ibs_op.ibs_op_data2_high = high;
249 rdmsr(MSR_AMD64_IBSOPDATA3, low, high);
250 ibs_op.ibs_op_data3_low = low;
251 ibs_op.ibs_op_data3_high = high;
252 rdmsr(MSR_AMD64_IBSDCLINAD, low, high);
253 ibs_op.ibs_dc_linear_low = low;
254 ibs_op.ibs_dc_linear_high = high;
255 rdmsr(MSR_AMD64_IBSDCPHYSAD, low, high);
256 ibs_op.ibs_dc_phys_low = low;
257 ibs_op.ibs_dc_phys_high = high;
258
259 /* reenable the IRQ */
260 oprofile_add_ibs_sample(regs,
261 (unsigned int *)&ibs_op,
262 IBS_OP_BEGIN);
263 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter543a1572008-07-22 21:09:04 +0200264 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200265 low &= ~IBS_OP_LOW_VALID_BIT;
266 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200267 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
268 }
269 }
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 1;
272}
273
Robert Richter852402c2008-07-22 21:09:06 +0200274#endif
275
Robert Richter7939d2b2008-07-22 21:08:56 +0200276static int op_amd_check_ctrs(struct pt_regs * const regs,
277 struct op_msrs const * const msrs)
278{
279 unsigned int low, high;
280 int i;
281
Robert Richter4c168ea2008-09-24 11:08:52 +0200282 for (i = 0 ; i < NUM_COUNTERS; ++i) {
283 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200284 continue;
285 CTR_READ(low, high, msrs, i);
286 if (CTR_OVERFLOWED(low)) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200287 oprofile_add_sample(regs, i);
288 CTR_WRITE(reset_value[i], msrs, i);
Robert Richter7939d2b2008-07-22 21:08:56 +0200289 }
290 }
291
Robert Richter852402c2008-07-22 21:09:06 +0200292#ifdef CONFIG_OPROFILE_IBS
Robert Richter7939d2b2008-07-22 21:08:56 +0200293 op_amd_handle_ibs(regs, msrs);
Robert Richter852402c2008-07-22 21:09:06 +0200294#endif
Robert Richter7939d2b2008-07-22 21:08:56 +0200295
296 /* See op_model_ppro.c */
297 return 1;
298}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100299
Robert Richter6657fe42008-07-22 21:08:50 +0200300static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 unsigned int low, high;
303 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200304 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
305 if (reset_value[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 CTRL_READ(low, high, msrs, i);
307 CTRL_SET_ACTIVE(low);
308 CTRL_WRITE(low, high, msrs, i);
309 }
310 }
Robert Richter852402c2008-07-22 21:09:06 +0200311
312#ifdef CONFIG_OPROFILE_IBS
Barry Kasindorf56784f12008-07-22 21:08:55 +0200313 if (ibs_allowed && ibs_config.fetch_enabled) {
314 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
Suravee Suthikulpanit5f87dfb2008-10-15 08:15:51 -0500315 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
316 + IBS_FETCH_HIGH_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200317 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
318 }
319
320 if (ibs_allowed && ibs_config.op_enabled) {
Suravee Suthikulpanit5f87dfb2008-10-15 08:15:51 -0500321 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
322 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
323 + IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200324 high = 0;
325 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
326 }
Robert Richter852402c2008-07-22 21:09:06 +0200327#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330
Robert Richter6657fe42008-07-22 21:08:50 +0200331static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100333 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int i;
335
Robert Richterfd13f6c2008-10-19 21:00:09 +0200336 /*
337 * Subtle: stop on all counters to avoid race with setting our
338 * pm callback
339 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200340 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
341 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200342 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 CTRL_READ(low, high, msrs, i);
344 CTRL_SET_INACTIVE(low);
345 CTRL_WRITE(low, high, msrs, i);
346 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200347
Robert Richter852402c2008-07-22 21:09:06 +0200348#ifdef CONFIG_OPROFILE_IBS
Barry Kasindorf56784f12008-07-22 21:08:55 +0200349 if (ibs_allowed && ibs_config.fetch_enabled) {
Robert Richterfd13f6c2008-10-19 21:00:09 +0200350 /* clear max count and enable */
351 low = 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200352 high = 0;
353 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
354 }
355
356 if (ibs_allowed && ibs_config.op_enabled) {
Robert Richterfd13f6c2008-10-19 21:00:09 +0200357 /* clear max count and enable */
358 low = 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200359 high = 0;
360 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
361 }
Robert Richter852402c2008-07-22 21:09:06 +0200362#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Robert Richter6657fe42008-07-22 21:08:50 +0200365static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200366{
367 int i;
368
Robert Richter4c168ea2008-09-24 11:08:52 +0200369 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100370 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200371 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
372 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200373 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100374 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200375 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
376 }
377}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Robert Richter852402c2008-07-22 21:09:06 +0200379#ifndef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200380
381/* no IBS support */
382
Robert Richtera4c408a2008-07-22 21:09:02 +0200383static int op_amd_init(struct oprofile_operations *ops)
384{
385 return 0;
386}
387
388static void op_amd_exit(void) {}
389
390#else
391
Robert Richter7d77f2d2008-07-22 21:08:57 +0200392static u8 ibs_eilvt_off;
393
Barry Kasindorf56784f12008-07-22 21:08:55 +0200394static inline void apic_init_ibs_nmi_per_cpu(void *arg)
395{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200396 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200397}
398
399static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
400{
401 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
402}
403
Robert Richter7d77f2d2008-07-22 21:08:57 +0200404static int pfm_amd64_setup_eilvt(void)
405{
406#define IBSCTL_LVTOFFSETVAL (1 << 8)
407#define IBSCTL 0x1cc
408 struct pci_dev *cpu_cfg;
409 int nodes;
410 u32 value = 0;
411
412 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200413 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200414
415 nodes = 0;
416 cpu_cfg = NULL;
417 do {
418 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
419 PCI_DEVICE_ID_AMD_10H_NB_MISC,
420 cpu_cfg);
421 if (!cpu_cfg)
422 break;
423 ++nodes;
424 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
425 | IBSCTL_LVTOFFSETVAL);
426 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
427 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
428 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
429 "IBSCTL = 0x%08x", value);
430 return 1;
431 }
432 } while (1);
433
434 if (!nodes) {
435 printk(KERN_DEBUG "No CPU node configured for IBS");
436 return 1;
437 }
438
439#ifdef CONFIG_NUMA
440 /* Sanity check */
441 /* Works only for 64bit with proper numa implementation. */
442 if (nodes != num_possible_nodes()) {
443 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
444 "found: %d, expected %d",
445 nodes, num_possible_nodes());
446 return 1;
447 }
448#endif
449 return 0;
450}
451
Robert Richterfd13f6c2008-10-19 21:00:09 +0200452/* initialize the APIC for the IBS interrupts if available */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200453static void setup_ibs(void)
454{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200455 ibs_allowed = boot_cpu_has(X86_FEATURE_IBS);
456
457 if (!ibs_allowed)
458 return;
459
Robert Richter852402c2008-07-22 21:09:06 +0200460 if (pfm_amd64_setup_eilvt()) {
Robert Richter7d77f2d2008-07-22 21:08:57 +0200461 ibs_allowed = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200462 return;
463 }
464
465 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200466}
467
468
Robert Richterfd13f6c2008-10-19 21:00:09 +0200469/* uninitialize the APIC for the IBS interrupts if needed */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200470static void clear_ibs_nmi(void)
471{
472 if (ibs_allowed)
Robert Richterebb535d2008-07-22 21:08:59 +0200473 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200474}
475
Robert Richter25ad29132008-09-05 17:12:36 +0200476static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200477
Robert Richter25ad29132008-09-05 17:12:36 +0200478static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200479{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200480 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200481 int ret = 0;
482
483 /* architecture specific files */
484 if (create_arch_files)
485 ret = create_arch_files(sb, root);
486
487 if (ret)
488 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200489
490 if (!ibs_allowed)
Robert Richter270d3e12008-07-22 21:09:01 +0200491 return ret;
492
493 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200494
495 /* setup some reasonable defaults */
496 ibs_config.max_cnt_fetch = 250000;
497 ibs_config.fetch_enabled = 0;
498 ibs_config.max_cnt_op = 250000;
499 ibs_config.op_enabled = 0;
500 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200501
502 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
503 oprofilefs_create_ulong(sb, dir, "enable",
504 &ibs_config.fetch_enabled);
505 oprofilefs_create_ulong(sb, dir, "max_count",
506 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200507 oprofilefs_create_ulong(sb, dir, "rand_enable",
508 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200509
Robert Richterccd755c2008-07-29 16:57:10 +0200510 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200511 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200512 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200513 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200514 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200515 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200516 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200517
518 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200519}
520
Robert Richteradf5ec02008-07-22 21:08:48 +0200521static int op_amd_init(struct oprofile_operations *ops)
522{
Robert Richter270d3e12008-07-22 21:09:01 +0200523 setup_ibs();
524 create_arch_files = ops->create_files;
525 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200526 return 0;
527}
528
529static void op_amd_exit(void)
530{
Robert Richter270d3e12008-07-22 21:09:01 +0200531 clear_ibs_nmi();
Robert Richteradf5ec02008-07-22 21:08:48 +0200532}
533
Robert Richtera4c408a2008-07-22 21:09:02 +0200534#endif
535
Robert Richter6657fe42008-07-22 21:08:50 +0200536struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200537 .init = op_amd_init,
538 .exit = op_amd_exit,
539 .num_counters = NUM_COUNTERS,
540 .num_controls = NUM_CONTROLS,
541 .fill_in_addresses = &op_amd_fill_in_addresses,
542 .setup_ctrs = &op_amd_setup_ctrs,
543 .check_ctrs = &op_amd_check_ctrs,
544 .start = &op_amd_start,
545 .stop = &op_amd_stop,
546 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547};