blob: 0d8390319797c00b9e9f174289a545eb0b6e9edb [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
Barry Kasindorf56784f12008-07-22 21:08:55 +020050#define IBS_FETCH_CTL_HIGH_MASK 0xFFFFFFFF
51/* high dword bit IbsFetchCtl[bit 49] */
52#define IBS_FETCH_VALID_BIT (1UL << 17)
53/* high dword bit IbsFetchCtl[bit 52] */
54#define IBS_FETCH_PHY_ADDR_VALID_BIT (1UL << 20)
55/* high dword bit IbsFetchCtl[bit 48] */
56#define IBS_FETCH_ENABLE (1UL << 16)
57
58#define IBS_FETCH_CTL_CNT_MASK 0x00000000FFFF0000UL
59#define IBS_FETCH_CTL_MAX_CNT_MASK 0x000000000000FFFFUL
60
61/*IbsOpCtl masks/bits */
62#define IBS_OP_VALID_BIT (1ULL<<18) /* IbsOpCtl[bit18] */
63#define IBS_OP_ENABLE (1ULL<<17) /* IBS_OP_ENABLE[bit17]*/
64
65/* Codes used in cpu_buffer.c */
66#define IBS_FETCH_BEGIN 3
67#define IBS_OP_BEGIN 4
68
69/*IbsOpData3 masks */
70#define IBS_CTL_LVT_OFFSET_VALID_BIT (1ULL<<8)
71
72/*PCI Extended Configuration Constants */
73/* MSR to set the IBS control register APIC LVT offset */
74#define IBS_LVT_OFFSET_PCI 0x1CC
75
Robert Richter90645702008-07-22 21:08:58 +020076/* The function interface needs to be fixed, something like add
77 data. Should then be added to linux/oprofile.h. */
78extern void oprofile_add_ibs_sample(struct pt_regs *const regs,
79 unsigned int * const ibs_sample, u8 code);
80
Barry Kasindorf56784f12008-07-22 21:08:55 +020081struct ibs_fetch_sample {
82 /* MSRC001_1031 IBS Fetch Linear Address Register */
83 unsigned int ibs_fetch_lin_addr_low;
84 unsigned int ibs_fetch_lin_addr_high;
85 /* MSRC001_1030 IBS Fetch Control Register */
86 unsigned int ibs_fetch_ctl_low;
87 unsigned int ibs_fetch_ctl_high;
88 /* MSRC001_1032 IBS Fetch Physical Address Register */
89 unsigned int ibs_fetch_phys_addr_low;
90 unsigned int ibs_fetch_phys_addr_high;
91};
92
93struct ibs_op_sample {
94 /* MSRC001_1034 IBS Op Logical Address Register (IbsRIP) */
95 unsigned int ibs_op_rip_low;
96 unsigned int ibs_op_rip_high;
97 /* MSRC001_1035 IBS Op Data Register */
98 unsigned int ibs_op_data1_low;
99 unsigned int ibs_op_data1_high;
100 /* MSRC001_1036 IBS Op Data 2 Register */
101 unsigned int ibs_op_data2_low;
102 unsigned int ibs_op_data2_high;
103 /* MSRC001_1037 IBS Op Data 3 Register */
104 unsigned int ibs_op_data3_low;
105 unsigned int ibs_op_data3_high;
106 /* MSRC001_1038 IBS DC Linear Address Register (IbsDcLinAd) */
107 unsigned int ibs_dc_linear_low;
108 unsigned int ibs_dc_linear_high;
109 /* MSRC001_1039 IBS DC Physical Address Register (IbsDcPhysAd) */
110 unsigned int ibs_dc_phys_low;
111 unsigned int ibs_dc_phys_high;
112};
113
114/*
115 * unitialize the APIC for the IBS interrupts if needed on AMD Family10h+
116*/
117static void clear_ibs_nmi(void);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static unsigned long reset_value[NUM_COUNTERS];
Barry Kasindorf56784f12008-07-22 21:08:55 +0200120static int ibs_allowed; /* AMD Family10h and later */
121
122struct op_ibs_config {
123 unsigned long op_enabled;
124 unsigned long fetch_enabled;
125 unsigned long max_cnt_fetch;
126 unsigned long max_cnt_op;
127 unsigned long rand_en;
128 unsigned long dispatched_ops;
129};
130
131static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100132
Robert Richter6657fe42008-07-22 21:08:50 +0200133/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +0200134
Robert Richter6657fe42008-07-22 21:08:50 +0200135static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Don Zickuscb9c4482006-09-26 10:52:26 +0200137 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100139 for (i = 0; i < NUM_COUNTERS; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200140 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
141 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
142 else
143 msrs->counters[i].addr = 0;
144 }
145
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100146 for (i = 0; i < NUM_CONTROLS; i++) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200147 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
148 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
149 else
150 msrs->controls[i].addr = 0;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100154
Robert Richter6657fe42008-07-22 21:08:50 +0200155static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 unsigned int low, high;
158 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* clear all counters */
161 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100162 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200163 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100165 CTRL_CLEAR_LO(low);
166 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 CTRL_WRITE(low, high, msrs, i);
168 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* avoid a false detection of ctr overflows in NMI handler */
171 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100172 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200173 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 CTR_WRITE(1, msrs, i);
175 }
176
177 /* enable active counters */
178 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100179 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 reset_value[i] = counter_config[i].count;
181
182 CTR_WRITE(counter_config[i].count, msrs, i);
183
184 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100185 CTRL_CLEAR_LO(low);
186 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 CTRL_SET_ENABLE(low);
188 CTRL_SET_USR(low, counter_config[i].user);
189 CTRL_SET_KERN(low, counter_config[i].kernel);
190 CTRL_SET_UM(low, counter_config[i].unit_mask);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100191 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
192 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
193 CTRL_SET_HOST_ONLY(high, 0);
194 CTRL_SET_GUEST_ONLY(high, 0);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 CTRL_WRITE(low, high, msrs, i);
197 } else {
198 reset_value[i] = 0;
199 }
200 }
201}
202
Robert Richter7939d2b2008-07-22 21:08:56 +0200203static inline int
204op_amd_handle_ibs(struct pt_regs * const regs,
205 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 unsigned int low, high;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200208 struct ibs_fetch_sample ibs_fetch;
209 struct ibs_op_sample ibs_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Robert Richter7939d2b2008-07-22 21:08:56 +0200211 if (!ibs_allowed)
212 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Robert Richter7939d2b2008-07-22 21:08:56 +0200214 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200215 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
216 if (high & IBS_FETCH_VALID_BIT) {
217 ibs_fetch.ibs_fetch_ctl_high = high;
218 ibs_fetch.ibs_fetch_ctl_low = low;
219 rdmsr(MSR_AMD64_IBSFETCHLINAD, low, high);
220 ibs_fetch.ibs_fetch_lin_addr_high = high;
221 ibs_fetch.ibs_fetch_lin_addr_low = low;
222 rdmsr(MSR_AMD64_IBSFETCHPHYSAD, low, high);
223 ibs_fetch.ibs_fetch_phys_addr_high = high;
224 ibs_fetch.ibs_fetch_phys_addr_low = low;
225
226 oprofile_add_ibs_sample(regs,
227 (unsigned int *)&ibs_fetch,
228 IBS_FETCH_BEGIN);
229
230 /*reenable the IRQ */
231 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
232 high &= ~(IBS_FETCH_VALID_BIT);
233 high |= IBS_FETCH_ENABLE;
234 low &= IBS_FETCH_CTL_MAX_CNT_MASK;
235 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
236 }
237 }
238
Robert Richter7939d2b2008-07-22 21:08:56 +0200239 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200240 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
241 if (low & IBS_OP_VALID_BIT) {
242 rdmsr(MSR_AMD64_IBSOPRIP, low, high);
243 ibs_op.ibs_op_rip_low = low;
244 ibs_op.ibs_op_rip_high = high;
245 rdmsr(MSR_AMD64_IBSOPDATA, low, high);
246 ibs_op.ibs_op_data1_low = low;
247 ibs_op.ibs_op_data1_high = high;
248 rdmsr(MSR_AMD64_IBSOPDATA2, low, high);
249 ibs_op.ibs_op_data2_low = low;
250 ibs_op.ibs_op_data2_high = high;
251 rdmsr(MSR_AMD64_IBSOPDATA3, low, high);
252 ibs_op.ibs_op_data3_low = low;
253 ibs_op.ibs_op_data3_high = high;
254 rdmsr(MSR_AMD64_IBSDCLINAD, low, high);
255 ibs_op.ibs_dc_linear_low = low;
256 ibs_op.ibs_dc_linear_high = high;
257 rdmsr(MSR_AMD64_IBSDCPHYSAD, low, high);
258 ibs_op.ibs_dc_phys_low = low;
259 ibs_op.ibs_dc_phys_high = high;
260
261 /* reenable the IRQ */
262 oprofile_add_ibs_sample(regs,
263 (unsigned int *)&ibs_op,
264 IBS_OP_BEGIN);
265 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
266 low &= ~(IBS_OP_VALID_BIT);
267 low |= IBS_OP_ENABLE;
268 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
269 }
270 }
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 1;
273}
274
Robert Richter7939d2b2008-07-22 21:08:56 +0200275static int op_amd_check_ctrs(struct pt_regs * const regs,
276 struct op_msrs const * const msrs)
277{
278 unsigned int low, high;
279 int i;
280
281 for (i = 0 ; i < NUM_COUNTERS; ++i) {
282 if (!reset_value[i])
283 continue;
284 CTR_READ(low, high, msrs, i);
285 if (CTR_OVERFLOWED(low)) {
286 oprofile_add_sample(regs, i);
287 CTR_WRITE(reset_value[i], msrs, i);
288 }
289 }
290
291 op_amd_handle_ibs(regs, msrs);
292
293 /* See op_model_ppro.c */
294 return 1;
295}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100296
Robert Richter6657fe42008-07-22 21:08:50 +0200297static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned int low, high;
300 int i;
301 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
302 if (reset_value[i]) {
303 CTRL_READ(low, high, msrs, i);
304 CTRL_SET_ACTIVE(low);
305 CTRL_WRITE(low, high, msrs, i);
306 }
307 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200308 if (ibs_allowed && ibs_config.fetch_enabled) {
309 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
310 high = IBS_FETCH_ENABLE;
311 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
312 }
313
314 if (ibs_allowed && ibs_config.op_enabled) {
315 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF) + IBS_OP_ENABLE;
316 high = 0;
317 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321
Robert Richter6657fe42008-07-22 21:08:50 +0200322static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100324 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 int i;
326
327 /* Subtle: stop on all counters to avoid race with
328 * setting our pm callback */
329 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200330 if (!reset_value[i])
331 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 CTRL_READ(low, high, msrs, i);
333 CTRL_SET_INACTIVE(low);
334 CTRL_WRITE(low, high, msrs, i);
335 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200336
337 if (ibs_allowed && ibs_config.fetch_enabled) {
338 low = 0; /* clear max count and enable */
339 high = 0;
340 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
341 }
342
343 if (ibs_allowed && ibs_config.op_enabled) {
344 low = 0; /* clear max count and enable */
345 high = 0;
346 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Robert Richter6657fe42008-07-22 21:08:50 +0200350static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200351{
352 int i;
353
354 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100355 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200356 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
357 }
358 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100359 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200360 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
361 }
362}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Robert Richter7d77f2d2008-07-22 21:08:57 +0200364static u8 ibs_eilvt_off;
365
Barry Kasindorf56784f12008-07-22 21:08:55 +0200366static inline void apic_init_ibs_nmi_per_cpu(void *arg)
367{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200368 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200369}
370
371static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
372{
373 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
374}
375
Robert Richter7d77f2d2008-07-22 21:08:57 +0200376static int pfm_amd64_setup_eilvt(void)
377{
378#define IBSCTL_LVTOFFSETVAL (1 << 8)
379#define IBSCTL 0x1cc
380 struct pci_dev *cpu_cfg;
381 int nodes;
382 u32 value = 0;
383
384 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200385 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200386
387 nodes = 0;
388 cpu_cfg = NULL;
389 do {
390 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
391 PCI_DEVICE_ID_AMD_10H_NB_MISC,
392 cpu_cfg);
393 if (!cpu_cfg)
394 break;
395 ++nodes;
396 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
397 | IBSCTL_LVTOFFSETVAL);
398 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
399 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
400 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
401 "IBSCTL = 0x%08x", value);
402 return 1;
403 }
404 } while (1);
405
406 if (!nodes) {
407 printk(KERN_DEBUG "No CPU node configured for IBS");
408 return 1;
409 }
410
411#ifdef CONFIG_NUMA
412 /* Sanity check */
413 /* Works only for 64bit with proper numa implementation. */
414 if (nodes != num_possible_nodes()) {
415 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
416 "found: %d, expected %d",
417 nodes, num_possible_nodes());
418 return 1;
419 }
420#endif
421 return 0;
422}
423
Barry Kasindorf56784f12008-07-22 21:08:55 +0200424/*
425 * initialize the APIC for the IBS interrupts
Robert Richter7d77f2d2008-07-22 21:08:57 +0200426 * if available (AMD Family10h rev B0 and later)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200427 */
428static void setup_ibs(void)
429{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200430 ibs_allowed = boot_cpu_has(X86_FEATURE_IBS);
431
432 if (!ibs_allowed)
433 return;
434
Robert Richter7d77f2d2008-07-22 21:08:57 +0200435 if (pfm_amd64_setup_eilvt())
436 ibs_allowed = 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200437}
438
439
440/*
441 * unitialize the APIC for the IBS interrupts if needed on AMD Family10h
442 * rev B0 and later */
443static void clear_ibs_nmi(void)
444{
445 if (ibs_allowed)
Robert Richterebb535d2008-07-22 21:08:59 +0200446 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200447}
448
Robert Richter270d3e12008-07-22 21:09:01 +0200449static int (*create_arch_files)(struct super_block * sb, struct dentry * root);
450
Robert Richterfc2bd732008-07-22 21:09:00 +0200451static int setup_ibs_files(struct super_block * sb, struct dentry * root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200452{
453 char buf[12];
454 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200455 int ret = 0;
456
457 /* architecture specific files */
458 if (create_arch_files)
459 ret = create_arch_files(sb, root);
460
461 if (ret)
462 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200463
464 if (!ibs_allowed)
Robert Richter270d3e12008-07-22 21:09:01 +0200465 return ret;
466
467 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200468
469 /* setup some reasonable defaults */
470 ibs_config.max_cnt_fetch = 250000;
471 ibs_config.fetch_enabled = 0;
472 ibs_config.max_cnt_op = 250000;
473 ibs_config.op_enabled = 0;
474 ibs_config.dispatched_ops = 1;
475 snprintf(buf, sizeof(buf), "ibs_fetch");
476 dir = oprofilefs_mkdir(sb, root, buf);
477 oprofilefs_create_ulong(sb, dir, "rand_enable",
478 &ibs_config.rand_en);
479 oprofilefs_create_ulong(sb, dir, "enable",
480 &ibs_config.fetch_enabled);
481 oprofilefs_create_ulong(sb, dir, "max_count",
482 &ibs_config.max_cnt_fetch);
483 snprintf(buf, sizeof(buf), "ibs_uops");
484 dir = oprofilefs_mkdir(sb, root, buf);
485 oprofilefs_create_ulong(sb, dir, "enable",
486 &ibs_config.op_enabled);
487 oprofilefs_create_ulong(sb, dir, "max_count",
488 &ibs_config.max_cnt_op);
489 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
490 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200491
492 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200493}
494
Robert Richteradf5ec02008-07-22 21:08:48 +0200495static int op_amd_init(struct oprofile_operations *ops)
496{
Robert Richter270d3e12008-07-22 21:09:01 +0200497 setup_ibs();
498 create_arch_files = ops->create_files;
499 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200500 return 0;
501}
502
503static void op_amd_exit(void)
504{
Robert Richter270d3e12008-07-22 21:09:01 +0200505 clear_ibs_nmi();
Robert Richteradf5ec02008-07-22 21:08:48 +0200506}
507
Robert Richter6657fe42008-07-22 21:08:50 +0200508struct op_x86_model_spec const op_amd_spec = {
Robert Richteradf5ec02008-07-22 21:08:48 +0200509 .init = op_amd_init,
510 .exit = op_amd_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 .num_counters = NUM_COUNTERS,
512 .num_controls = NUM_CONTROLS,
Robert Richter6657fe42008-07-22 21:08:50 +0200513 .fill_in_addresses = &op_amd_fill_in_addresses,
514 .setup_ctrs = &op_amd_setup_ctrs,
515 .check_ctrs = &op_amd_check_ctrs,
516 .start = &op_amd_start,
517 .stop = &op_amd_stop,
518 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519};