blob: 8fdf06e4edf93c4e93b37d71907bb274a0ebbfc3 [file] [log] [blame]
Paolo Ciarrocchid4413732008-02-19 23:51:27 +01001/*
Robert Richter6852fd92008-07-22 21:09:08 +02002 * @file op_model_amd.c
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +01003 * athlon / K7 / K8 / Family 10h model-specific MSR operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Robert Richterae735e92008-12-25 17:26:07 +01005 * @remark Copyright 2002-2009 OProfile authors
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * @remark Read the file COPYING
7 *
8 * @author John Levon
9 * @author Philippe Elie
10 * @author Graydon Hoare
Robert Richteradf5ec02008-07-22 21:08:48 +020011 * @author Robert Richter <robert.richter@amd.com>
Barry Kasindorf56784f12008-07-22 21:08:55 +020012 * @author Barry Kasindorf
Robert Richterae735e92008-12-25 17:26:07 +010013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/oprofile.h>
Barry Kasindorf56784f12008-07-22 21:08:55 +020016#include <linux/device.h>
17#include <linux/pci.h>
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/ptrace.h>
20#include <asm/msr.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020021#include <asm/nmi.h>
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "op_x86_model.h"
24#include "op_counter.h"
25
Robert Richter4c168ea2008-09-24 11:08:52 +020026#define NUM_COUNTERS 4
27#define NUM_CONTROLS 4
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010029#define CTR_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
Robert Richter1acda872009-01-05 10:35:31 +010063#define IBS_FETCH_SIZE 6
64#define IBS_OP_SIZE 12
Barry Kasindorf56784f12008-07-22 21:08:55 +020065
Robert Richterfc81be82008-12-18 00:28:27 +010066static int has_ibs; /* AMD Family10h and later */
Barry Kasindorf56784f12008-07-22 21:08:55 +020067
68struct op_ibs_config {
69 unsigned long op_enabled;
70 unsigned long fetch_enabled;
71 unsigned long max_cnt_fetch;
72 unsigned long max_cnt_op;
73 unsigned long rand_en;
74 unsigned long dispatched_ops;
75};
76
77static struct op_ibs_config ibs_config;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010078
Robert Richter852402c2008-07-22 21:09:06 +020079#endif
80
Robert Richter6657fe42008-07-22 21:08:50 +020081/* functions for op_amd_spec */
Robert Richterdfa15422008-07-22 21:08:49 +020082
Robert Richter6657fe42008-07-22 21:08:50 +020083static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Don Zickuscb9c4482006-09-26 10:52:26 +020085 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010087 for (i = 0; i < NUM_COUNTERS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020088 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
89 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020090 else
91 msrs->counters[i].addr = 0;
92 }
93
Paolo Ciarrocchid4413732008-02-19 23:51:27 +010094 for (i = 0; i < NUM_CONTROLS; i++) {
Robert Richter4c168ea2008-09-24 11:08:52 +020095 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
96 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
Don Zickuscb9c4482006-09-26 10:52:26 +020097 else
98 msrs->controls[i].addr = 0;
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100102
Robert Richter6657fe42008-07-22 21:08:50 +0200103static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 unsigned int low, high;
106 int i;
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* clear all counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200109 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100110 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200111 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100113 CTRL_CLEAR_LO(low);
114 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 CTRL_WRITE(low, high, msrs, i);
116 }
Don Zickuscb9c4482006-09-26 10:52:26 +0200117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* avoid a false detection of ctr overflows in NMI handler */
Robert Richter4c168ea2008-09-24 11:08:52 +0200119 for (i = 0; i < NUM_COUNTERS; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100120 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
Don Zickuscb9c4482006-09-26 10:52:26 +0200121 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 CTR_WRITE(1, msrs, i);
123 }
124
125 /* enable active counters */
Robert Richter4c168ea2008-09-24 11:08:52 +0200126 for (i = 0; i < NUM_COUNTERS; ++i) {
127 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
128 reset_value[i] = counter_config[i].count;
129
130 CTR_WRITE(counter_config[i].count, msrs, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100133 CTRL_CLEAR_LO(low);
134 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 CTRL_SET_ENABLE(low);
Robert Richter4c168ea2008-09-24 11:08:52 +0200136 CTRL_SET_USR(low, counter_config[i].user);
137 CTRL_SET_KERN(low, counter_config[i].kernel);
138 CTRL_SET_UM(low, counter_config[i].unit_mask);
139 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
140 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100141 CTRL_SET_HOST_ONLY(high, 0);
142 CTRL_SET_GUEST_ONLY(high, 0);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 CTRL_WRITE(low, high, msrs, i);
Robert Richter4c168ea2008-09-24 11:08:52 +0200145 } else {
146 reset_value[i] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148 }
149}
150
Robert Richter852402c2008-07-22 21:09:06 +0200151#ifdef CONFIG_OPROFILE_IBS
152
Robert Richter7939d2b2008-07-22 21:08:56 +0200153static inline int
154op_amd_handle_ibs(struct pt_regs * const regs,
155 struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Robert Richter1acda872009-01-05 10:35:31 +0100157 u32 low, high;
158 u64 msr;
159 struct op_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Robert Richterfc81be82008-12-18 00:28:27 +0100161 if (!has_ibs)
Robert Richter7939d2b2008-07-22 21:08:56 +0200162 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Robert Richter7939d2b2008-07-22 21:08:56 +0200164 if (ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200165 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200166 if (high & IBS_FETCH_HIGH_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100167 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100168 oprofile_write_reserve(&entry, regs, msr,
169 IBS_FETCH_CODE, IBS_FETCH_SIZE);
170 oprofile_add_data(&entry, (u32)msr);
171 oprofile_add_data(&entry, (u32)(msr >> 32));
172 oprofile_add_data(&entry, low);
173 oprofile_add_data(&entry, high);
Robert Richter1acda872009-01-05 10:35:31 +0100174 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100175 oprofile_add_data(&entry, (u32)msr);
176 oprofile_add_data(&entry, (u32)(msr >> 32));
177 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200178
Robert Richterfd13f6c2008-10-19 21:00:09 +0200179 /* reenable the IRQ */
Robert Richter87f0bac2008-07-22 21:09:03 +0200180 high &= ~IBS_FETCH_HIGH_VALID_BIT;
181 high |= IBS_FETCH_HIGH_ENABLE;
182 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200183 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
184 }
185 }
186
Robert Richter7939d2b2008-07-22 21:08:56 +0200187 if (ibs_config.op_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200188 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
Robert Richter87f0bac2008-07-22 21:09:03 +0200189 if (low & IBS_OP_LOW_VALID_BIT) {
Robert Richter1acda872009-01-05 10:35:31 +0100190 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100191 oprofile_write_reserve(&entry, regs, msr,
192 IBS_OP_CODE, IBS_OP_SIZE);
193 oprofile_add_data(&entry, (u32)msr);
194 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100195 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100196 oprofile_add_data(&entry, (u32)msr);
197 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100198 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100199 oprofile_add_data(&entry, (u32)msr);
200 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100201 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100202 oprofile_add_data(&entry, (u32)msr);
203 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100204 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100205 oprofile_add_data(&entry, (u32)msr);
206 oprofile_add_data(&entry, (u32)(msr >> 32));
Robert Richter1acda872009-01-05 10:35:31 +0100207 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
Robert Richter14f0ca82009-01-07 21:50:22 +0100208 oprofile_add_data(&entry, (u32)msr);
209 oprofile_add_data(&entry, (u32)(msr >> 32));
210 oprofile_write_commit(&entry);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200211
212 /* reenable the IRQ */
Robert Richter543a1572008-07-22 21:09:04 +0200213 high = 0;
Robert Richter87f0bac2008-07-22 21:09:03 +0200214 low &= ~IBS_OP_LOW_VALID_BIT;
215 low |= IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200216 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
217 }
218 }
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return 1;
221}
222
Robert Richter852402c2008-07-22 21:09:06 +0200223#endif
224
Robert Richter7939d2b2008-07-22 21:08:56 +0200225static int op_amd_check_ctrs(struct pt_regs * const regs,
226 struct op_msrs const * const msrs)
227{
228 unsigned int low, high;
229 int i;
230
Robert Richter4c168ea2008-09-24 11:08:52 +0200231 for (i = 0 ; i < NUM_COUNTERS; ++i) {
232 if (!reset_value[i])
Robert Richter7939d2b2008-07-22 21:08:56 +0200233 continue;
234 CTR_READ(low, high, msrs, i);
235 if (CTR_OVERFLOWED(low)) {
Robert Richter4c168ea2008-09-24 11:08:52 +0200236 oprofile_add_sample(regs, i);
237 CTR_WRITE(reset_value[i], msrs, i);
Robert Richter7939d2b2008-07-22 21:08:56 +0200238 }
239 }
240
Robert Richter852402c2008-07-22 21:09:06 +0200241#ifdef CONFIG_OPROFILE_IBS
Robert Richter7939d2b2008-07-22 21:08:56 +0200242 op_amd_handle_ibs(regs, msrs);
Robert Richter852402c2008-07-22 21:09:06 +0200243#endif
Robert Richter7939d2b2008-07-22 21:08:56 +0200244
245 /* See op_model_ppro.c */
246 return 1;
247}
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100248
Robert Richter6657fe42008-07-22 21:08:50 +0200249static void op_amd_start(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 unsigned int low, high;
252 int i;
Robert Richter4c168ea2008-09-24 11:08:52 +0200253 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
254 if (reset_value[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 CTRL_READ(low, high, msrs, i);
256 CTRL_SET_ACTIVE(low);
257 CTRL_WRITE(low, high, msrs, i);
258 }
259 }
Robert Richter852402c2008-07-22 21:09:06 +0200260
261#ifdef CONFIG_OPROFILE_IBS
Robert Richterfc81be82008-12-18 00:28:27 +0100262 if (has_ibs && ibs_config.fetch_enabled) {
Barry Kasindorf56784f12008-07-22 21:08:55 +0200263 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
Suravee Suthikulpanit5f87dfb2008-10-15 08:15:51 -0500264 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
265 + IBS_FETCH_HIGH_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200266 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
267 }
268
Robert Richterfc81be82008-12-18 00:28:27 +0100269 if (has_ibs && ibs_config.op_enabled) {
Suravee Suthikulpanit5f87dfb2008-10-15 08:15:51 -0500270 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
271 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
272 + IBS_OP_LOW_ENABLE;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200273 high = 0;
274 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
275 }
Robert Richter852402c2008-07-22 21:09:06 +0200276#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279
Robert Richter6657fe42008-07-22 21:08:50 +0200280static void op_amd_stop(struct op_msrs const * const msrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100282 unsigned int low, high;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 int i;
284
Robert Richterfd13f6c2008-10-19 21:00:09 +0200285 /*
286 * Subtle: stop on all counters to avoid race with setting our
287 * pm callback
288 */
Robert Richter4c168ea2008-09-24 11:08:52 +0200289 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
290 if (!reset_value[i])
Don Zickuscb9c4482006-09-26 10:52:26 +0200291 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 CTRL_READ(low, high, msrs, i);
293 CTRL_SET_INACTIVE(low);
294 CTRL_WRITE(low, high, msrs, i);
295 }
Barry Kasindorf56784f12008-07-22 21:08:55 +0200296
Robert Richter852402c2008-07-22 21:09:06 +0200297#ifdef CONFIG_OPROFILE_IBS
Robert Richterfc81be82008-12-18 00:28:27 +0100298 if (has_ibs && ibs_config.fetch_enabled) {
Robert Richterfd13f6c2008-10-19 21:00:09 +0200299 /* clear max count and enable */
300 low = 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200301 high = 0;
302 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
303 }
304
Robert Richterfc81be82008-12-18 00:28:27 +0100305 if (has_ibs && ibs_config.op_enabled) {
Robert Richterfd13f6c2008-10-19 21:00:09 +0200306 /* clear max count and enable */
307 low = 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200308 high = 0;
309 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
310 }
Robert Richter852402c2008-07-22 21:09:06 +0200311#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Robert Richter6657fe42008-07-22 21:08:50 +0200314static void op_amd_shutdown(struct op_msrs const * const msrs)
Don Zickuscb9c4482006-09-26 10:52:26 +0200315{
316 int i;
317
Robert Richter4c168ea2008-09-24 11:08:52 +0200318 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100319 if (CTR_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200320 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
321 }
Robert Richter4c168ea2008-09-24 11:08:52 +0200322 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
Paolo Ciarrocchid4413732008-02-19 23:51:27 +0100323 if (CTRL_IS_RESERVED(msrs, i))
Don Zickuscb9c4482006-09-26 10:52:26 +0200324 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
325 }
326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Robert Richter9fa68122008-11-24 14:21:03 +0100328#ifdef CONFIG_OPROFILE_IBS
Robert Richtera4c408a2008-07-22 21:09:02 +0200329
Robert Richter7d77f2d2008-07-22 21:08:57 +0200330static u8 ibs_eilvt_off;
331
Barry Kasindorf56784f12008-07-22 21:08:55 +0200332static inline void apic_init_ibs_nmi_per_cpu(void *arg)
333{
Robert Richter7d77f2d2008-07-22 21:08:57 +0200334 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200335}
336
337static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
338{
339 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
340}
341
Robert Richterfe615cb2008-11-24 14:58:03 +0100342static int init_ibs_nmi(void)
Robert Richter7d77f2d2008-07-22 21:08:57 +0200343{
344#define IBSCTL_LVTOFFSETVAL (1 << 8)
345#define IBSCTL 0x1cc
346 struct pci_dev *cpu_cfg;
347 int nodes;
348 u32 value = 0;
349
350 /* per CPU setup */
Robert Richterebb535d2008-07-22 21:08:59 +0200351 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200352
353 nodes = 0;
354 cpu_cfg = NULL;
355 do {
356 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
357 PCI_DEVICE_ID_AMD_10H_NB_MISC,
358 cpu_cfg);
359 if (!cpu_cfg)
360 break;
361 ++nodes;
362 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
363 | IBSCTL_LVTOFFSETVAL);
364 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
365 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
Robert Richter83bd9242008-12-15 15:09:50 +0100366 pci_dev_put(cpu_cfg);
Robert Richter7d77f2d2008-07-22 21:08:57 +0200367 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
368 "IBSCTL = 0x%08x", value);
369 return 1;
370 }
371 } while (1);
372
373 if (!nodes) {
374 printk(KERN_DEBUG "No CPU node configured for IBS");
375 return 1;
376 }
377
378#ifdef CONFIG_NUMA
379 /* Sanity check */
380 /* Works only for 64bit with proper numa implementation. */
381 if (nodes != num_possible_nodes()) {
382 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
383 "found: %d, expected %d",
384 nodes, num_possible_nodes());
385 return 1;
386 }
387#endif
388 return 0;
389}
390
Robert Richterfe615cb2008-11-24 14:58:03 +0100391/* uninitialize the APIC for the IBS interrupts if needed */
392static void clear_ibs_nmi(void)
393{
Robert Richterfc81be82008-12-18 00:28:27 +0100394 if (has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100395 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
396}
397
Robert Richterfd13f6c2008-10-19 21:00:09 +0200398/* initialize the APIC for the IBS interrupts if available */
Robert Richterfe615cb2008-11-24 14:58:03 +0100399static void ibs_init(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200400{
Robert Richterfc81be82008-12-18 00:28:27 +0100401 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200402
Robert Richterfc81be82008-12-18 00:28:27 +0100403 if (!has_ibs)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200404 return;
405
Robert Richterfe615cb2008-11-24 14:58:03 +0100406 if (init_ibs_nmi()) {
Robert Richterfc81be82008-12-18 00:28:27 +0100407 has_ibs = 0;
Robert Richter852402c2008-07-22 21:09:06 +0200408 return;
409 }
410
411 printk(KERN_INFO "oprofile: AMD IBS detected\n");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200412}
413
Robert Richterfe615cb2008-11-24 14:58:03 +0100414static void ibs_exit(void)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200415{
Robert Richterfc81be82008-12-18 00:28:27 +0100416 if (!has_ibs)
Robert Richterfe615cb2008-11-24 14:58:03 +0100417 return;
418
419 clear_ibs_nmi();
Barry Kasindorf56784f12008-07-22 21:08:55 +0200420}
421
Robert Richter25ad29132008-09-05 17:12:36 +0200422static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
Robert Richter270d3e12008-07-22 21:09:01 +0200423
Robert Richter25ad29132008-09-05 17:12:36 +0200424static int setup_ibs_files(struct super_block *sb, struct dentry *root)
Barry Kasindorf56784f12008-07-22 21:08:55 +0200425{
Barry Kasindorf56784f12008-07-22 21:08:55 +0200426 struct dentry *dir;
Robert Richter270d3e12008-07-22 21:09:01 +0200427 int ret = 0;
428
429 /* architecture specific files */
430 if (create_arch_files)
431 ret = create_arch_files(sb, root);
432
433 if (ret)
434 return ret;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200435
Robert Richterfc81be82008-12-18 00:28:27 +0100436 if (!has_ibs)
Robert Richter270d3e12008-07-22 21:09:01 +0200437 return ret;
438
439 /* model specific files */
Barry Kasindorf56784f12008-07-22 21:08:55 +0200440
441 /* setup some reasonable defaults */
442 ibs_config.max_cnt_fetch = 250000;
443 ibs_config.fetch_enabled = 0;
444 ibs_config.max_cnt_op = 250000;
445 ibs_config.op_enabled = 0;
446 ibs_config.dispatched_ops = 1;
Robert Richter2d55a472008-07-18 17:56:05 +0200447
448 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
449 oprofilefs_create_ulong(sb, dir, "enable",
450 &ibs_config.fetch_enabled);
451 oprofilefs_create_ulong(sb, dir, "max_count",
452 &ibs_config.max_cnt_fetch);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200453 oprofilefs_create_ulong(sb, dir, "rand_enable",
454 &ibs_config.rand_en);
Robert Richter2d55a472008-07-18 17:56:05 +0200455
Robert Richterccd755c2008-07-29 16:57:10 +0200456 dir = oprofilefs_mkdir(sb, root, "ibs_op");
Barry Kasindorf56784f12008-07-22 21:08:55 +0200457 oprofilefs_create_ulong(sb, dir, "enable",
Robert Richter2d55a472008-07-18 17:56:05 +0200458 &ibs_config.op_enabled);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200459 oprofilefs_create_ulong(sb, dir, "max_count",
Robert Richter2d55a472008-07-18 17:56:05 +0200460 &ibs_config.max_cnt_op);
Barry Kasindorf56784f12008-07-22 21:08:55 +0200461 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
Robert Richter2d55a472008-07-18 17:56:05 +0200462 &ibs_config.dispatched_ops);
Robert Richterfc2bd732008-07-22 21:09:00 +0200463
464 return 0;
Barry Kasindorf56784f12008-07-22 21:08:55 +0200465}
466
Robert Richteradf5ec02008-07-22 21:08:48 +0200467static int op_amd_init(struct oprofile_operations *ops)
468{
Robert Richterfe615cb2008-11-24 14:58:03 +0100469 ibs_init();
Robert Richter270d3e12008-07-22 21:09:01 +0200470 create_arch_files = ops->create_files;
471 ops->create_files = setup_ibs_files;
Robert Richteradf5ec02008-07-22 21:08:48 +0200472 return 0;
473}
474
475static void op_amd_exit(void)
476{
Robert Richterfe615cb2008-11-24 14:58:03 +0100477 ibs_exit();
Robert Richteradf5ec02008-07-22 21:08:48 +0200478}
479
Robert Richter9fa68122008-11-24 14:21:03 +0100480#else
481
482/* no IBS support */
483
484static int op_amd_init(struct oprofile_operations *ops)
485{
486 return 0;
487}
488
489static void op_amd_exit(void) {}
490
491#endif /* CONFIG_OPROFILE_IBS */
Robert Richtera4c408a2008-07-22 21:09:02 +0200492
Robert Richter6657fe42008-07-22 21:08:50 +0200493struct op_x86_model_spec const op_amd_spec = {
Robert Richterc92960f2008-09-05 17:12:36 +0200494 .init = op_amd_init,
495 .exit = op_amd_exit,
496 .num_counters = NUM_COUNTERS,
497 .num_controls = NUM_CONTROLS,
498 .fill_in_addresses = &op_amd_fill_in_addresses,
499 .setup_ctrs = &op_amd_setup_ctrs,
500 .check_ctrs = &op_amd_check_ctrs,
501 .start = &op_amd_start,
502 .stop = &op_amd_stop,
503 .shutdown = &op_amd_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504};