blob: 9c8c8c58313246ad9a859c42fdad29e92b0ef931 [file] [log] [blame]
Paolo Ciarrocchid4413732008-02-19 23:51:27 +01001/*
Robert Richteradf5ec02008-07-22 21:08:48 +02002 * @file op_model_athlon.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
26#define NUM_COUNTERS 4
27#define NUM_CONTROLS 4
28
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 Richter87f0bac2008-07-22 21:09:03 +020050/* IbsFetchCtl bits/masks */
51#define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
52#define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
53#define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
Barry Kasindorf56784f12008-07-22 21:08:55 +020054
Robert Richter87f0bac2008-07-22 21:09:03 +020055/*IbsOpCtl bits */
56#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
57#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
Barry Kasindorf56784f12008-07-22 21:08:55 +020058
59/* Codes used in cpu_buffer.c */
Robert Richter87f0bac2008-07-22 21:09:03 +020060/* This produces duplicate code, need to be fixed */
Barry Kasindorf56784f12008-07-22 21:08:55 +020061#define IBS_FETCH_BEGIN 3
62#define IBS_OP_BEGIN 4
63
Robert Richter90645702008-07-22 21:08:58 +020064/* The function interface needs to be fixed, something like add
65 data. Should then be added to linux/oprofile.h. */
66extern void oprofile_add_ibs_sample(struct pt_regs *const regs,
67 unsigned int * const ibs_sample, u8 code);
68
Barry Kasindorf56784f12008-07-22 21:08:55 +020069struct ibs_fetch_sample {
70 /* MSRC001_1031 IBS Fetch Linear Address Register */
71 unsigned int ibs_fetch_lin_addr_low;
72 unsigned int ibs_fetch_lin_addr_high;
73 /* MSRC001_1030 IBS Fetch Control Register */
74 unsigned int ibs_fetch_ctl_low;
75 unsigned int ibs_fetch_ctl_high;
76 /* MSRC001_1032 IBS Fetch Physical Address Register */
77 unsigned int ibs_fetch_phys_addr_low;
78 unsigned int ibs_fetch_phys_addr_high;
79};
80
81struct ibs_op_sample {
82 /* MSRC001_1034 IBS Op Logical Address Register (IbsRIP) */
83 unsigned int ibs_op_rip_low;
84 unsigned int ibs_op_rip_high;
85 /* MSRC001_1035 IBS Op Data Register */
86 unsigned int ibs_op_data1_low;
87 unsigned int ibs_op_data1_high;
88 /* MSRC001_1036 IBS Op Data 2 Register */
89 unsigned int ibs_op_data2_low;
90 unsigned int ibs_op_data2_high;
91 /* MSRC001_1037 IBS Op Data 3 Register */
92 unsigned int ibs_op_data3_low;
93 unsigned int ibs_op_data3_high;
94 /* MSRC001_1038 IBS DC Linear Address Register (IbsDcLinAd) */
95 unsigned int ibs_dc_linear_low;
96 unsigned int ibs_dc_linear_high;
97 /* MSRC001_1039 IBS DC Physical Address Register (IbsDcPhysAd) */
98 unsigned int ibs_dc_phys_low;
99 unsigned int ibs_dc_phys_high;
100};
101
102/*
103 * unitialize the APIC for the IBS interrupts if needed on AMD Family10h+
104*/
105static void clear_ibs_nmi(void);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static unsigned long reset_value[NUM_COUNTERS];
Barry Kasindorf56784f12008-07-22 21:08:55 +0200108static int ibs_allowed; /* AMD Family10h and later */
109
110struct op_ibs_config {
111 unsigned long op_enabled;
112 unsigned long fetch_enabled;
113 unsigned long max_cnt_fetch;
114 unsigned long max_cnt_op;
115 unsigned long rand_en;
116 unsigned long dispatched_ops;
117};
118
119static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100120
Robert Richter6657fe42008-07-22 21:08:50 +0200121/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +0200122
Robert Richter6657fe42008-07-22 21:08:50 +0200123static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Don Zickuscb9c4482006-09-26 10:52:26 +0200125 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100127 for (i = 0; i < NUM_COUNTERS; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200128 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
129 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
130 else
131 msrs->counters[i].addr = 0;
132 }
133
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100134 for (i = 0; i < NUM_CONTROLS; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200135 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
136 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
137 else
138 msrs->controls[i].addr = 0;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100142
Robert Richter6657fe42008-07-22 21:08:50 +0200143static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 unsigned int low, high;
146 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* clear all counters */
149 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100150 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200151 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100153 CTRL_CLEAR_LO(low);
154 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 CTRL_WRITE(low, high, msrs, i);
156 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* avoid a false detection of ctr overflows in NMI handler */
159 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100160 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200161 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 CTR_WRITE(1, msrs, i);
163 }
164
165 /* enable active counters */
166 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100167 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 reset_value[i] = counter_config[i].count;
169
170 CTR_WRITE(counter_config[i].count, msrs, i);
171
172 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100173 CTRL_CLEAR_LO(low);
174 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 CTRL_SET_ENABLE(low);
176 CTRL_SET_USR(low, counter_config[i].user);
177 CTRL_SET_KERN(low, counter_config[i].kernel);
178 CTRL_SET_UM(low, counter_config[i].unit_mask);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100179 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
180 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
181 CTRL_SET_HOST_ONLY(high, 0);
182 CTRL_SET_GUEST_ONLY(high, 0);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 CTRL_WRITE(low, high, msrs, i);
185 } else {
186 reset_value[i] = 0;
187 }
188 }
189}
190
Robert Richter7939d2b2008-07-22 21:08:56 +0200191static inline int
192op_amd_handle_ibs(struct pt_regs * const regs,
193 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 unsigned int low, high;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200196 struct ibs_fetch_sample ibs_fetch;
197 struct ibs_op_sample ibs_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Robert Richter7939d2b2008-07-22 21:08:56 +0200199 if (!ibs_allowed)
200 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Robert Richter7939d2b2008-07-22 21:08:56 +0200202 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200203 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200204 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200205 ibs_fetch.ibs_fetch_ctl_high = high;
206 ibs_fetch.ibs_fetch_ctl_low = low;
207 rdmsr(MSR_AMD64_IBSFETCHLINAD, low, high);
208 ibs_fetch.ibs_fetch_lin_addr_high = high;
209 ibs_fetch.ibs_fetch_lin_addr_low = low;
210 rdmsr(MSR_AMD64_IBSFETCHPHYSAD, low, high);
211 ibs_fetch.ibs_fetch_phys_addr_high = high;
212 ibs_fetch.ibs_fetch_phys_addr_low = low;
213
214 oprofile_add_ibs_sample(regs,
215 (unsigned int *)&ibs_fetch,
216 IBS_FETCH_BEGIN);
217
218 /*reenable the IRQ */
219 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200220 high &= ~IBS_FETCH_HIGH_VALID_BIT;
221 high |= IBS_FETCH_HIGH_ENABLE;
222 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200223 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
224 }
225 }
226
Robert Richter7939d2b2008-07-22 21:08:56 +0200227 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200228 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200229 if (low & IBS_OP_LOW_VALID_BIT) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200230 rdmsr(MSR_AMD64_IBSOPRIP, low, high);
231 ibs_op.ibs_op_rip_low = low;
232 ibs_op.ibs_op_rip_high = high;
233 rdmsr(MSR_AMD64_IBSOPDATA, low, high);
234 ibs_op.ibs_op_data1_low = low;
235 ibs_op.ibs_op_data1_high = high;
236 rdmsr(MSR_AMD64_IBSOPDATA2, low, high);
237 ibs_op.ibs_op_data2_low = low;
238 ibs_op.ibs_op_data2_high = high;
239 rdmsr(MSR_AMD64_IBSOPDATA3, low, high);
240 ibs_op.ibs_op_data3_low = low;
241 ibs_op.ibs_op_data3_high = high;
242 rdmsr(MSR_AMD64_IBSDCLINAD, low, high);
243 ibs_op.ibs_dc_linear_low = low;
244 ibs_op.ibs_dc_linear_high = high;
245 rdmsr(MSR_AMD64_IBSDCPHYSAD, low, high);
246 ibs_op.ibs_dc_phys_low = low;
247 ibs_op.ibs_dc_phys_high = high;
248
249 /* reenable the IRQ */
250 oprofile_add_ibs_sample(regs,
251 (unsigned int *)&ibs_op,
252 IBS_OP_BEGIN);
253 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter543a1572008-07-22 21:09:04 +0200254 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200255 low &= ~IBS_OP_LOW_VALID_BIT;
256 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200257 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
258 }
259 }
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 1;
262}
263
Robert Richter7939d2b2008-07-22 21:08:56 +0200264static int op_amd_check_ctrs(struct pt_regs * const regs,
265 struct op_msrs const * const msrs)
266{
267 unsigned int low, high;
268 int i;
269
270 for (i = 0 ; i < NUM_COUNTERS; ++i) {
271 if (!reset_value[i])
272 continue;
273 CTR_READ(low, high, msrs, i);
274 if (CTR_OVERFLOWED(low)) {
275 oprofile_add_sample(regs, i);
276 CTR_WRITE(reset_value[i], msrs, i);
277 }
278 }
279
280 op_amd_handle_ibs(regs, msrs);
281
282 /* See op_model_ppro.c */
283 return 1;
284}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100285
Robert Richter6657fe42008-07-22 21:08:50 +0200286static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 unsigned int low, high;
289 int i;
290 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
291 if (reset_value[i]) {
292 CTRL_READ(low, high, msrs, i);
293 CTRL_SET_ACTIVE(low);
294 CTRL_WRITE(low, high, msrs, i);
295 }
296 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200297 if (ibs_allowed && ibs_config.fetch_enabled) {
298 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
Robert Richter87f0bac2008-07-22 21:09:03 +0200299 high = IBS_FETCH_HIGH_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200300 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
301 }
302
303 if (ibs_allowed && ibs_config.op_enabled) {
Robert Richter87f0bac2008-07-22 21:09:03 +0200304 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF) + IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200305 high = 0;
306 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310
Robert Richter6657fe42008-07-22 21:08:50 +0200311static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100313 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int i;
315
316 /* Subtle: stop on all counters to avoid race with
317 * setting our pm callback */
318 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200319 if (!reset_value[i])
320 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 CTRL_READ(low, high, msrs, i);
322 CTRL_SET_INACTIVE(low);
323 CTRL_WRITE(low, high, msrs, i);
324 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200325
326 if (ibs_allowed && ibs_config.fetch_enabled) {
327 low = 0; /* clear max count and enable */
328 high = 0;
329 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
330 }
331
332 if (ibs_allowed && ibs_config.op_enabled) {
333 low = 0; /* clear max count and enable */
334 high = 0;
335 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Robert Richter6657fe42008-07-22 21:08:50 +0200339static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200340{
341 int i;
342
343 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100344 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200345 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
346 }
347 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100348 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200349 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
350 }
351}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Robert Richtera4c408a2008-07-22 21:09:02 +0200353#ifndef CONFIG_SMP
354
355/* no IBS support */
356
357static void setup_ibs(void)
358{
359 ibs_allowed = 0;
360}
361
362static void clear_ibs_nmi(void) {}
363
364static int op_amd_init(struct oprofile_operations *ops)
365{
366 return 0;
367}
368
369static void op_amd_exit(void) {}
370
371#else
372
Robert Richter7d77f2d2008-07-22 21:08:57 +0200373static u8 ibs_eilvt_off;
374
Barry Kasindorf56784f12008-07-22 21:08:55 +0200375static inline void apic_init_ibs_nmi_per_cpu(void *arg)
376{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200377 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200378}
379
380static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
381{
382 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
383}
384
Robert Richter7d77f2d2008-07-22 21:08:57 +0200385static int pfm_amd64_setup_eilvt(void)
386{
387#define IBSCTL_LVTOFFSETVAL (1 << 8)
388#define IBSCTL 0x1cc
389 struct pci_dev *cpu_cfg;
390 int nodes;
391 u32 value = 0;
392
393 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200394 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200395
396 nodes = 0;
397 cpu_cfg = NULL;
398 do {
399 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
400 PCI_DEVICE_ID_AMD_10H_NB_MISC,
401 cpu_cfg);
402 if (!cpu_cfg)
403 break;
404 ++nodes;
405 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
406 | IBSCTL_LVTOFFSETVAL);
407 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
408 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
409 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
410 "IBSCTL = 0x%08x", value);
411 return 1;
412 }
413 } while (1);
414
415 if (!nodes) {
416 printk(KERN_DEBUG "No CPU node configured for IBS");
417 return 1;
418 }
419
420#ifdef CONFIG_NUMA
421 /* Sanity check */
422 /* Works only for 64bit with proper numa implementation. */
423 if (nodes != num_possible_nodes()) {
424 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
425 "found: %d, expected %d",
426 nodes, num_possible_nodes());
427 return 1;
428 }
429#endif
430 return 0;
431}
432
Barry Kasindorf56784f12008-07-22 21:08:55 +0200433/*
434 * initialize the APIC for the IBS interrupts
Robert Richter7d77f2d2008-07-22 21:08:57 +0200435 * if available (AMD Family10h rev B0 and later)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200436 */
437static void setup_ibs(void)
438{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200439 ibs_allowed = boot_cpu_has(X86_FEATURE_IBS);
440
441 if (!ibs_allowed)
442 return;
443
Robert Richter7d77f2d2008-07-22 21:08:57 +0200444 if (pfm_amd64_setup_eilvt())
445 ibs_allowed = 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200446}
447
448
449/*
450 * unitialize the APIC for the IBS interrupts if needed on AMD Family10h
451 * rev B0 and later */
452static void clear_ibs_nmi(void)
453{
454 if (ibs_allowed)
Robert Richterebb535d2008-07-22 21:08:59 +0200455 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200456}
457
Robert Richter270d3e12008-07-22 21:09:01 +0200458static int (*create_arch_files)(struct super_block * sb, struct dentry * root);
459
Robert Richterfc2bd732008-07-22 21:09:00 +0200460static int setup_ibs_files(struct super_block * sb, struct dentry * root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200461{
462 char buf[12];
463 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200464 int ret = 0;
465
466 /* architecture specific files */
467 if (create_arch_files)
468 ret = create_arch_files(sb, root);
469
470 if (ret)
471 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200472
473 if (!ibs_allowed)
Robert Richter270d3e12008-07-22 21:09:01 +0200474 return ret;
475
476 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200477
478 /* setup some reasonable defaults */
479 ibs_config.max_cnt_fetch = 250000;
480 ibs_config.fetch_enabled = 0;
481 ibs_config.max_cnt_op = 250000;
482 ibs_config.op_enabled = 0;
483 ibs_config.dispatched_ops = 1;
484 snprintf(buf, sizeof(buf), "ibs_fetch");
485 dir = oprofilefs_mkdir(sb, root, buf);
486 oprofilefs_create_ulong(sb, dir, "rand_enable",
487 &ibs_config.rand_en);
488 oprofilefs_create_ulong(sb, dir, "enable",
489 &ibs_config.fetch_enabled);
490 oprofilefs_create_ulong(sb, dir, "max_count",
491 &ibs_config.max_cnt_fetch);
492 snprintf(buf, sizeof(buf), "ibs_uops");
493 dir = oprofilefs_mkdir(sb, root, buf);
494 oprofilefs_create_ulong(sb, dir, "enable",
495 &ibs_config.op_enabled);
496 oprofilefs_create_ulong(sb, dir, "max_count",
497 &ibs_config.max_cnt_op);
498 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
499 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200500
501 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200502}
503
Robert Richteradf5ec02008-07-22 21:08:48 +0200504static int op_amd_init(struct oprofile_operations *ops)
505{
Robert Richter270d3e12008-07-22 21:09:01 +0200506 setup_ibs();
507 create_arch_files = ops->create_files;
508 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200509 return 0;
510}
511
512static void op_amd_exit(void)
513{
Robert Richter270d3e12008-07-22 21:09:01 +0200514 clear_ibs_nmi();
Robert Richteradf5ec02008-07-22 21:08:48 +0200515}
516
Robert Richtera4c408a2008-07-22 21:09:02 +0200517#endif
518
Robert Richter6657fe42008-07-22 21:08:50 +0200519struct op_x86_model_spec const op_amd_spec = {
Robert Richteradf5ec02008-07-22 21:08:48 +0200520 .init = op_amd_init,
521 .exit = op_amd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 .num_counters = NUM_COUNTERS,
523 .num_controls = NUM_CONTROLS,
Robert Richter6657fe42008-07-22 21:08:50 +0200524 .fill_in_addresses = &op_amd_fill_in_addresses,
525 .setup_ctrs = &op_amd_setup_ctrs,
526 .check_ctrs = &op_amd_check_ctrs,
527 .start = &op_amd_start,
528 .stop = &op_amd_stop,
529 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530};