blob: c5b5c7fb3ceda286cd5f24764e6307f344f16fc6 [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
Robert Richtercdc18342008-09-26 22:18:44 -040073oprofile_add_ibs_sample(struct pt_regs * const regs,
74 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
Barry Kasindorf56784f12008-07-22 21:08:55 +0200109static int ibs_allowed; /* AMD Family10h and later */
110
111struct op_ibs_config {
112 unsigned long op_enabled;
113 unsigned long fetch_enabled;
114 unsigned long max_cnt_fetch;
115 unsigned long max_cnt_op;
116 unsigned long rand_en;
117 unsigned long dispatched_ops;
118};
119
120static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100121
Robert Richter852402c2008-07-22 21:09:06 +0200122#endif
123
Robert Richter6657fe42008-07-22 21:08:50 +0200124/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +0200125
Robert Richter6657fe42008-07-22 21:08:50 +0200126static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Don Zickuscb9c4482006-09-26 10:52:26 +0200128 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100130 for (i = 0; i < NUM_COUNTERS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200131 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
132 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200133 else
134 msrs->counters[i].addr = 0;
135 }
136
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100137 for (i = 0; i < NUM_CONTROLS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200138 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
139 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +0200140 else
141 msrs->controls[i].addr = 0;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100145
Robert Richter6657fe42008-07-22 21:08:50 +0200146static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 unsigned int low, high;
149 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200152 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100153 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200154 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100156 CTRL_CLEAR_LO(low);
157 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 CTRL_WRITE(low, high, msrs, i);
159 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200162 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100163 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200164 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 CTR_WRITE(1, msrs, i);
166 }
167
168 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200169 for (i = 0; i < NUM_COUNTERS; ++i) {
170 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
171 reset_value[i] = counter_config[i].count;
172
173 CTR_WRITE(counter_config[i].count, msrs, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100176 CTRL_CLEAR_LO(low);
177 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 CTRL_SET_ENABLE(low);
Robert Richter4c168ea2008-09-24 11:08:52 +0200179 CTRL_SET_USR(low, counter_config[i].user);
180 CTRL_SET_KERN(low, counter_config[i].kernel);
181 CTRL_SET_UM(low, counter_config[i].unit_mask);
182 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
183 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100184 CTRL_SET_HOST_ONLY(high, 0);
185 CTRL_SET_GUEST_ONLY(high, 0);
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 CTRL_WRITE(low, high, msrs, i);
Robert Richter4c168ea2008-09-24 11:08:52 +0200188 } else {
189 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191 }
192}
193
Robert Richter852402c2008-07-22 21:09:06 +0200194#ifdef CONFIG_OPROFILE_IBS
195
Robert Richter7939d2b2008-07-22 21:08:56 +0200196static inline int
197op_amd_handle_ibs(struct pt_regs * const regs,
198 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 unsigned int low, high;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200201 struct ibs_fetch_sample ibs_fetch;
202 struct ibs_op_sample ibs_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Robert Richter7939d2b2008-07-22 21:08:56 +0200204 if (!ibs_allowed)
205 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Robert Richter7939d2b2008-07-22 21:08:56 +0200207 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200208 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200209 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200210 ibs_fetch.ibs_fetch_ctl_high = high;
211 ibs_fetch.ibs_fetch_ctl_low = low;
212 rdmsr(MSR_AMD64_IBSFETCHLINAD, low, high);
213 ibs_fetch.ibs_fetch_lin_addr_high = high;
214 ibs_fetch.ibs_fetch_lin_addr_low = low;
215 rdmsr(MSR_AMD64_IBSFETCHPHYSAD, low, high);
216 ibs_fetch.ibs_fetch_phys_addr_high = high;
217 ibs_fetch.ibs_fetch_phys_addr_low = low;
218
219 oprofile_add_ibs_sample(regs,
220 (unsigned int *)&ibs_fetch,
221 IBS_FETCH_BEGIN);
222
Robert Richterfd13f6c2008-10-19 21:00:09 +0200223 /* reenable the IRQ */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200224 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200225 high &= ~IBS_FETCH_HIGH_VALID_BIT;
226 high |= IBS_FETCH_HIGH_ENABLE;
227 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200228 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
229 }
230 }
231
Robert Richter7939d2b2008-07-22 21:08:56 +0200232 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200233 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200234 if (low & IBS_OP_LOW_VALID_BIT) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200235 rdmsr(MSR_AMD64_IBSOPRIP, low, high);
236 ibs_op.ibs_op_rip_low = low;
237 ibs_op.ibs_op_rip_high = high;
238 rdmsr(MSR_AMD64_IBSOPDATA, low, high);
239 ibs_op.ibs_op_data1_low = low;
240 ibs_op.ibs_op_data1_high = high;
241 rdmsr(MSR_AMD64_IBSOPDATA2, low, high);
242 ibs_op.ibs_op_data2_low = low;
243 ibs_op.ibs_op_data2_high = high;
244 rdmsr(MSR_AMD64_IBSOPDATA3, low, high);
245 ibs_op.ibs_op_data3_low = low;
246 ibs_op.ibs_op_data3_high = high;
247 rdmsr(MSR_AMD64_IBSDCLINAD, low, high);
248 ibs_op.ibs_dc_linear_low = low;
249 ibs_op.ibs_dc_linear_high = high;
250 rdmsr(MSR_AMD64_IBSDCPHYSAD, low, high);
251 ibs_op.ibs_dc_phys_low = low;
252 ibs_op.ibs_dc_phys_high = high;
253
254 /* reenable the IRQ */
255 oprofile_add_ibs_sample(regs,
256 (unsigned int *)&ibs_op,
257 IBS_OP_BEGIN);
258 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter543a1572008-07-22 21:09:04 +0200259 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200260 low &= ~IBS_OP_LOW_VALID_BIT;
261 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200262 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
263 }
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 1;
267}
268
Robert Richter852402c2008-07-22 21:09:06 +0200269#endif
270
Robert Richter7939d2b2008-07-22 21:08:56 +0200271static int op_amd_check_ctrs(struct pt_regs * const regs,
272 struct op_msrs const * const msrs)
273{
274 unsigned int low, high;
275 int i;
276
Robert Richter4c168ea2008-09-24 11:08:52 +0200277 for (i = 0 ; i < NUM_COUNTERS; ++i) {
278 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200279 continue;
280 CTR_READ(low, high, msrs, i);
281 if (CTR_OVERFLOWED(low)) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200282 oprofile_add_sample(regs, i);
283 CTR_WRITE(reset_value[i], msrs, i);
Robert Richter7939d2b2008-07-22 21:08:56 +0200284 }
285 }
286
Robert Richter852402c2008-07-22 21:09:06 +0200287#ifdef CONFIG_OPROFILE_IBS
Robert Richter7939d2b2008-07-22 21:08:56 +0200288 op_amd_handle_ibs(regs, msrs);
Robert Richter852402c2008-07-22 21:09:06 +0200289#endif
Robert Richter7939d2b2008-07-22 21:08:56 +0200290
291 /* See op_model_ppro.c */
292 return 1;
293}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100294
Robert Richter6657fe42008-07-22 21:08:50 +0200295static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 unsigned int low, high;
298 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200299 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
300 if (reset_value[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 CTRL_READ(low, high, msrs, i);
302 CTRL_SET_ACTIVE(low);
303 CTRL_WRITE(low, high, msrs, i);
304 }
305 }
Robert Richter852402c2008-07-22 21:09:06 +0200306
307#ifdef CONFIG_OPROFILE_IBS
Barry Kasindorf56784f12008-07-22 21:08:55 +0200308 if (ibs_allowed && ibs_config.fetch_enabled) {
309 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
Suravee Suthikulpanit5f87dfb2008-10-15 08:15:51 -0500310 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
311 + IBS_FETCH_HIGH_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200312 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
313 }
314
315 if (ibs_allowed && ibs_config.op_enabled) {
Suravee Suthikulpanit5f87dfb2008-10-15 08:15:51 -0500316 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
317 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
318 + IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200319 high = 0;
320 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
321 }
Robert Richter852402c2008-07-22 21:09:06 +0200322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325
Robert Richter6657fe42008-07-22 21:08:50 +0200326static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100328 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int i;
330
Robert Richterfd13f6c2008-10-19 21:00:09 +0200331 /*
332 * Subtle: stop on all counters to avoid race with setting our
333 * pm callback
334 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200335 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
336 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200337 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 CTRL_READ(low, high, msrs, i);
339 CTRL_SET_INACTIVE(low);
340 CTRL_WRITE(low, high, msrs, i);
341 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200342
Robert Richter852402c2008-07-22 21:09:06 +0200343#ifdef CONFIG_OPROFILE_IBS
Barry Kasindorf56784f12008-07-22 21:08:55 +0200344 if (ibs_allowed && ibs_config.fetch_enabled) {
Robert Richterfd13f6c2008-10-19 21:00:09 +0200345 /* clear max count and enable */
346 low = 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200347 high = 0;
348 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
349 }
350
351 if (ibs_allowed && ibs_config.op_enabled) {
Robert Richterfd13f6c2008-10-19 21:00:09 +0200352 /* clear max count and enable */
353 low = 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200354 high = 0;
355 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
356 }
Robert Richter852402c2008-07-22 21:09:06 +0200357#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Robert Richter6657fe42008-07-22 21:08:50 +0200360static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200361{
362 int i;
363
Robert Richter4c168ea2008-09-24 11:08:52 +0200364 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100365 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200366 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
367 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200368 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100369 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200370 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
371 }
372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Robert Richter9fa68122008-11-24 14:21:03 +0100374#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200375
Robert Richter7d77f2d2008-07-22 21:08:57 +0200376static u8 ibs_eilvt_off;
377
Barry Kasindorf56784f12008-07-22 21:08:55 +0200378static inline void apic_init_ibs_nmi_per_cpu(void *arg)
379{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200380 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200381}
382
383static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
384{
385 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
386}
387
Robert Richterfe615cb2008-11-24 14:58:03 +0100388static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200389{
390#define IBSCTL_LVTOFFSETVAL (1 << 8)
391#define IBSCTL 0x1cc
392 struct pci_dev *cpu_cfg;
393 int nodes;
394 u32 value = 0;
395
396 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200397 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200398
399 nodes = 0;
400 cpu_cfg = NULL;
401 do {
402 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
403 PCI_DEVICE_ID_AMD_10H_NB_MISC,
404 cpu_cfg);
405 if (!cpu_cfg)
406 break;
407 ++nodes;
408 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
409 | IBSCTL_LVTOFFSETVAL);
410 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
411 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100412 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200413 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
414 "IBSCTL = 0x%08x", value);
415 return 1;
416 }
417 } while (1);
418
419 if (!nodes) {
420 printk(KERN_DEBUG "No CPU node configured for IBS");
421 return 1;
422 }
423
424#ifdef CONFIG_NUMA
425 /* Sanity check */
426 /* Works only for 64bit with proper numa implementation. */
427 if (nodes != num_possible_nodes()) {
428 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
429 "found: %d, expected %d",
430 nodes, num_possible_nodes());
431 return 1;
432 }
433#endif
434 return 0;
435}
436
Robert Richterfe615cb2008-11-24 14:58:03 +0100437/* uninitialize the APIC for the IBS interrupts if needed */
438static void clear_ibs_nmi(void)
439{
440 if (ibs_allowed)
441 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
442}
443
Robert Richterfd13f6c2008-10-19 21:00:09 +0200444/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100445static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200446{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200447 ibs_allowed = boot_cpu_has(X86_FEATURE_IBS);
448
449 if (!ibs_allowed)
450 return;
451
Robert Richterfe615cb2008-11-24 14:58:03 +0100452 if (init_ibs_nmi()) {
Robert Richter7d77f2d2008-07-22 21:08:57 +0200453 ibs_allowed = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200454 return;
455 }
456
457 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200458}
459
Robert Richterfe615cb2008-11-24 14:58:03 +0100460static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200461{
Robert Richterfe615cb2008-11-24 14:58:03 +0100462 if (!ibs_allowed)
463 return;
464
465 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200466}
467
Robert Richter25ad2912008-09-05 17:12:36 +0200468static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200469
Robert Richter25ad2912008-09-05 17:12:36 +0200470static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200471{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200472 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200473 int ret = 0;
474
475 /* architecture specific files */
476 if (create_arch_files)
477 ret = create_arch_files(sb, root);
478
479 if (ret)
480 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200481
482 if (!ibs_allowed)
Robert Richter270d3e12008-07-22 21:09:01 +0200483 return ret;
484
485 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200486
487 /* setup some reasonable defaults */
488 ibs_config.max_cnt_fetch = 250000;
489 ibs_config.fetch_enabled = 0;
490 ibs_config.max_cnt_op = 250000;
491 ibs_config.op_enabled = 0;
492 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200493
494 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
495 oprofilefs_create_ulong(sb, dir, "enable",
496 &ibs_config.fetch_enabled);
497 oprofilefs_create_ulong(sb, dir, "max_count",
498 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200499 oprofilefs_create_ulong(sb, dir, "rand_enable",
500 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200501
Robert Richterccd755c2008-07-29 16:57:10 +0200502 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200503 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200504 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200505 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200506 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200507 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200508 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200509
510 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200511}
512
Robert Richteradf5ec02008-07-22 21:08:48 +0200513static int op_amd_init(struct oprofile_operations *ops)
514{
Robert Richterfe615cb2008-11-24 14:58:03 +0100515 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200516 create_arch_files = ops->create_files;
517 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200518 return 0;
519}
520
521static void op_amd_exit(void)
522{
Robert Richterfe615cb2008-11-24 14:58:03 +0100523 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200524}
525
Robert Richter9fa68122008-11-24 14:21:03 +0100526#else
527
528/* no IBS support */
529
530static int op_amd_init(struct oprofile_operations *ops)
531{
532 return 0;
533}
534
535static void op_amd_exit(void) {}
536
537#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200538
Robert Richter6657fe42008-07-22 21:08:50 +0200539struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200540 .init = op_amd_init,
541 .exit = op_amd_exit,
542 .num_counters = NUM_COUNTERS,
543 .num_controls = NUM_CONTROLS,
544 .fill_in_addresses = &op_amd_fill_in_addresses,
545 .setup_ctrs = &op_amd_setup_ctrs,
546 .check_ctrs = &op_amd_check_ctrs,
547 .start = &op_amd_start,
548 .stop = &op_amd_stop,
549 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550};