x86/oprofile: replace macros to calculate control register
[deliverable/linux.git] / arch / x86 / oprofile / op_model_amd.c
CommitLineData
d4413732 1/*
6852fd9b 2 * @file op_model_amd.c
bd87f1f0 3 * athlon / K7 / K8 / Family 10h model-specific MSR operations
1da177e4 4 *
ae735e99 5 * @remark Copyright 2002-2009 OProfile authors
1da177e4
LT
6 * @remark Read the file COPYING
7 *
8 * @author John Levon
9 * @author Philippe Elie
10 * @author Graydon Hoare
adf5ec0b 11 * @author Robert Richter <robert.richter@amd.com>
56784f11 12 * @author Barry Kasindorf
ae735e99 13 */
1da177e4
LT
14
15#include <linux/oprofile.h>
56784f11
BK
16#include <linux/device.h>
17#include <linux/pci.h>
18
1da177e4
LT
19#include <asm/ptrace.h>
20#include <asm/msr.h>
3e4ff115 21#include <asm/nmi.h>
d4413732 22
1da177e4
LT
23#include "op_x86_model.h"
24#include "op_counter.h"
25
4c168eaf
RR
26#define NUM_COUNTERS 4
27#define NUM_CONTROLS 4
3370d358
RR
28#define OP_EVENT_MASK 0x0FFF
29
30#define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
1da177e4 31
1da177e4 32#define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
1da177e4 33
852402cc
RR
34static unsigned long reset_value[NUM_COUNTERS];
35
36#ifdef CONFIG_OPROFILE_IBS
37
87f0bacc
RR
38/* IbsFetchCtl bits/masks */
39#define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
40#define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
41#define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
56784f11 42
87f0bacc
RR
43/*IbsOpCtl bits */
44#define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
45#define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
56784f11 46
1acda878
RR
47#define IBS_FETCH_SIZE 6
48#define IBS_OP_SIZE 12
56784f11 49
fc81be8c 50static int has_ibs; /* AMD Family10h and later */
56784f11
BK
51
52struct op_ibs_config {
53 unsigned long op_enabled;
54 unsigned long fetch_enabled;
55 unsigned long max_cnt_fetch;
56 unsigned long max_cnt_op;
57 unsigned long rand_en;
58 unsigned long dispatched_ops;
59};
60
61static struct op_ibs_config ibs_config;
d4413732 62
852402cc
RR
63#endif
64
6657fe4f 65/* functions for op_amd_spec */
dfa15428 66
6657fe4f 67static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
1da177e4 68{
cb9c448c
DZ
69 int i;
70
d4413732 71 for (i = 0; i < NUM_COUNTERS; i++) {
4c168eaf
RR
72 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
73 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
cb9c448c
DZ
74 else
75 msrs->counters[i].addr = 0;
76 }
77
d4413732 78 for (i = 0; i < NUM_CONTROLS; i++) {
4c168eaf
RR
79 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
80 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
cb9c448c
DZ
81 else
82 msrs->controls[i].addr = 0;
83 }
1da177e4
LT
84}
85
ef8828dd
RR
86static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
87 struct op_msrs const * const msrs)
1da177e4 88{
3370d358 89 u64 val;
1da177e4 90 int i;
d4413732 91
1da177e4 92 /* clear all counters */
4c168eaf 93 for (i = 0 ; i < NUM_CONTROLS; ++i) {
d4413732 94 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
cb9c448c 95 continue;
3370d358
RR
96 rdmsrl(msrs->controls[i].addr, val);
97 val &= model->reserved;
98 wrmsrl(msrs->controls[i].addr, val);
1da177e4 99 }
cb9c448c 100
1da177e4 101 /* avoid a false detection of ctr overflows in NMI handler */
4c168eaf 102 for (i = 0; i < NUM_COUNTERS; ++i) {
d4413732 103 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
cb9c448c 104 continue;
d2731a43 105 wrmsr(msrs->counters[i].addr, -1, -1);
1da177e4
LT
106 }
107
108 /* enable active counters */
4c168eaf
RR
109 for (i = 0; i < NUM_COUNTERS; ++i) {
110 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
111 reset_value[i] = counter_config[i].count;
d2731a43 112 wrmsr(msrs->counters[i].addr, -(unsigned int)counter_config[i].count, -1);
3370d358
RR
113 rdmsrl(msrs->controls[i].addr, val);
114 val &= model->reserved;
115 val |= op_x86_get_ctrl(model, &counter_config[i]);
116 wrmsrl(msrs->controls[i].addr, val);
4c168eaf
RR
117 } else {
118 reset_value[i] = 0;
1da177e4
LT
119 }
120 }
121}
122
852402cc
RR
123#ifdef CONFIG_OPROFILE_IBS
124
7939d2bf
RR
125static inline int
126op_amd_handle_ibs(struct pt_regs * const regs,
127 struct op_msrs const * const msrs)
1da177e4 128{
1acda878
RR
129 u32 low, high;
130 u64 msr;
131 struct op_entry entry;
1da177e4 132
fc81be8c 133 if (!has_ibs)
7939d2bf 134 return 1;
1da177e4 135
7939d2bf 136 if (ibs_config.fetch_enabled) {
56784f11 137 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
87f0bacc 138 if (high & IBS_FETCH_HIGH_VALID_BIT) {
1acda878 139 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
14f0ca8e
RR
140 oprofile_write_reserve(&entry, regs, msr,
141 IBS_FETCH_CODE, IBS_FETCH_SIZE);
142 oprofile_add_data(&entry, (u32)msr);
143 oprofile_add_data(&entry, (u32)(msr >> 32));
144 oprofile_add_data(&entry, low);
145 oprofile_add_data(&entry, high);
1acda878 146 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
14f0ca8e
RR
147 oprofile_add_data(&entry, (u32)msr);
148 oprofile_add_data(&entry, (u32)(msr >> 32));
149 oprofile_write_commit(&entry);
56784f11 150
fd13f6c8 151 /* reenable the IRQ */
87f0bacc
RR
152 high &= ~IBS_FETCH_HIGH_VALID_BIT;
153 high |= IBS_FETCH_HIGH_ENABLE;
154 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
56784f11
BK
155 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
156 }
157 }
158
7939d2bf 159 if (ibs_config.op_enabled) {
56784f11 160 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
87f0bacc 161 if (low & IBS_OP_LOW_VALID_BIT) {
1acda878 162 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
14f0ca8e
RR
163 oprofile_write_reserve(&entry, regs, msr,
164 IBS_OP_CODE, IBS_OP_SIZE);
165 oprofile_add_data(&entry, (u32)msr);
166 oprofile_add_data(&entry, (u32)(msr >> 32));
1acda878 167 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
14f0ca8e
RR
168 oprofile_add_data(&entry, (u32)msr);
169 oprofile_add_data(&entry, (u32)(msr >> 32));
1acda878 170 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
14f0ca8e
RR
171 oprofile_add_data(&entry, (u32)msr);
172 oprofile_add_data(&entry, (u32)(msr >> 32));
1acda878 173 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
14f0ca8e
RR
174 oprofile_add_data(&entry, (u32)msr);
175 oprofile_add_data(&entry, (u32)(msr >> 32));
1acda878 176 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
14f0ca8e
RR
177 oprofile_add_data(&entry, (u32)msr);
178 oprofile_add_data(&entry, (u32)(msr >> 32));
1acda878 179 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
14f0ca8e
RR
180 oprofile_add_data(&entry, (u32)msr);
181 oprofile_add_data(&entry, (u32)(msr >> 32));
182 oprofile_write_commit(&entry);
56784f11
BK
183
184 /* reenable the IRQ */
543a157b 185 high = 0;
87f0bacc
RR
186 low &= ~IBS_OP_LOW_VALID_BIT;
187 low |= IBS_OP_LOW_ENABLE;
56784f11
BK
188 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
189 }
190 }
191
1da177e4
LT
192 return 1;
193}
194
90637595
RR
195static inline void op_amd_start_ibs(void)
196{
197 unsigned int low, high;
198 if (has_ibs && ibs_config.fetch_enabled) {
199 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
200 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
201 + IBS_FETCH_HIGH_ENABLE;
202 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
203 }
204
205 if (has_ibs && ibs_config.op_enabled) {
206 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
207 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
208 + IBS_OP_LOW_ENABLE;
209 high = 0;
210 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
211 }
212}
213
214static void op_amd_stop_ibs(void)
215{
216 unsigned int low, high;
217 if (has_ibs && ibs_config.fetch_enabled) {
218 /* clear max count and enable */
219 low = 0;
220 high = 0;
221 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
222 }
223
224 if (has_ibs && ibs_config.op_enabled) {
225 /* clear max count and enable */
226 low = 0;
227 high = 0;
228 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
229 }
230}
231
232#else
233
234static inline int op_amd_handle_ibs(struct pt_regs * const regs,
235 struct op_msrs const * const msrs) { }
236static inline void op_amd_start_ibs(void) { }
237static inline void op_amd_stop_ibs(void) { }
238
852402cc
RR
239#endif
240
7939d2bf
RR
241static int op_amd_check_ctrs(struct pt_regs * const regs,
242 struct op_msrs const * const msrs)
243{
244 unsigned int low, high;
245 int i;
246
4c168eaf
RR
247 for (i = 0 ; i < NUM_COUNTERS; ++i) {
248 if (!reset_value[i])
7939d2bf 249 continue;
d2731a43 250 rdmsr(msrs->counters[i].addr, low, high);
7939d2bf 251 if (CTR_OVERFLOWED(low)) {
4c168eaf 252 oprofile_add_sample(regs, i);
d2731a43 253 wrmsr(msrs->counters[i].addr, -(unsigned int)reset_value[i], -1);
7939d2bf
RR
254 }
255 }
256
257 op_amd_handle_ibs(regs, msrs);
258
259 /* See op_model_ppro.c */
260 return 1;
261}
d4413732 262
6657fe4f 263static void op_amd_start(struct op_msrs const * const msrs)
1da177e4
LT
264{
265 unsigned int low, high;
266 int i;
4c168eaf
RR
267 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
268 if (reset_value[i]) {
d2731a43 269 rdmsr(msrs->controls[i].addr, low, high);
1da177e4 270 CTRL_SET_ACTIVE(low);
d2731a43 271 wrmsr(msrs->controls[i].addr, low, high);
1da177e4
LT
272 }
273 }
852402cc 274
90637595 275 op_amd_start_ibs();
1da177e4
LT
276}
277
6657fe4f 278static void op_amd_stop(struct op_msrs const * const msrs)
1da177e4 279{
d4413732 280 unsigned int low, high;
1da177e4
LT
281 int i;
282
fd13f6c8
RR
283 /*
284 * Subtle: stop on all counters to avoid race with setting our
285 * pm callback
286 */
4c168eaf
RR
287 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
288 if (!reset_value[i])
cb9c448c 289 continue;
d2731a43 290 rdmsr(msrs->controls[i].addr, low, high);
1da177e4 291 CTRL_SET_INACTIVE(low);
d2731a43 292 wrmsr(msrs->controls[i].addr, low, high);
1da177e4 293 }
56784f11 294
90637595 295 op_amd_stop_ibs();
1da177e4
LT
296}
297
6657fe4f 298static void op_amd_shutdown(struct op_msrs const * const msrs)
cb9c448c
DZ
299{
300 int i;
301
4c168eaf 302 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
d4413732 303 if (CTR_IS_RESERVED(msrs, i))
cb9c448c
DZ
304 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
305 }
4c168eaf 306 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
d4413732 307 if (CTRL_IS_RESERVED(msrs, i))
cb9c448c
DZ
308 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
309 }
310}
1da177e4 311
9fa6812d 312#ifdef CONFIG_OPROFILE_IBS
a4c408a4 313
7d77f2dc
RR
314static u8 ibs_eilvt_off;
315
56784f11
BK
316static inline void apic_init_ibs_nmi_per_cpu(void *arg)
317{
7d77f2dc 318 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
56784f11
BK
319}
320
321static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
322{
323 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
324}
325
fe615cbf 326static int init_ibs_nmi(void)
7d77f2dc
RR
327{
328#define IBSCTL_LVTOFFSETVAL (1 << 8)
329#define IBSCTL 0x1cc
330 struct pci_dev *cpu_cfg;
331 int nodes;
332 u32 value = 0;
333
334 /* per CPU setup */
ebb535de 335 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
7d77f2dc
RR
336
337 nodes = 0;
338 cpu_cfg = NULL;
339 do {
340 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
341 PCI_DEVICE_ID_AMD_10H_NB_MISC,
342 cpu_cfg);
343 if (!cpu_cfg)
344 break;
345 ++nodes;
346 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
347 | IBSCTL_LVTOFFSETVAL);
348 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
349 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
83bd9243 350 pci_dev_put(cpu_cfg);
7d77f2dc
RR
351 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
352 "IBSCTL = 0x%08x", value);
353 return 1;
354 }
355 } while (1);
356
357 if (!nodes) {
358 printk(KERN_DEBUG "No CPU node configured for IBS");
359 return 1;
360 }
361
362#ifdef CONFIG_NUMA
363 /* Sanity check */
364 /* Works only for 64bit with proper numa implementation. */
365 if (nodes != num_possible_nodes()) {
366 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
367 "found: %d, expected %d",
368 nodes, num_possible_nodes());
369 return 1;
370 }
371#endif
372 return 0;
373}
374
fe615cbf
RR
375/* uninitialize the APIC for the IBS interrupts if needed */
376static void clear_ibs_nmi(void)
377{
fc81be8c 378 if (has_ibs)
fe615cbf
RR
379 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
380}
381
fd13f6c8 382/* initialize the APIC for the IBS interrupts if available */
fe615cbf 383static void ibs_init(void)
56784f11 384{
fc81be8c 385 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
56784f11 386
fc81be8c 387 if (!has_ibs)
56784f11
BK
388 return;
389
fe615cbf 390 if (init_ibs_nmi()) {
fc81be8c 391 has_ibs = 0;
852402cc
RR
392 return;
393 }
394
395 printk(KERN_INFO "oprofile: AMD IBS detected\n");
56784f11
BK
396}
397
fe615cbf 398static void ibs_exit(void)
56784f11 399{
fc81be8c 400 if (!has_ibs)
fe615cbf
RR
401 return;
402
403 clear_ibs_nmi();
56784f11
BK
404}
405
25ad2913 406static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
270d3e1a 407
25ad2913 408static int setup_ibs_files(struct super_block *sb, struct dentry *root)
56784f11 409{
56784f11 410 struct dentry *dir;
270d3e1a
RR
411 int ret = 0;
412
413 /* architecture specific files */
414 if (create_arch_files)
415 ret = create_arch_files(sb, root);
416
417 if (ret)
418 return ret;
56784f11 419
fc81be8c 420 if (!has_ibs)
270d3e1a
RR
421 return ret;
422
423 /* model specific files */
56784f11
BK
424
425 /* setup some reasonable defaults */
426 ibs_config.max_cnt_fetch = 250000;
427 ibs_config.fetch_enabled = 0;
428 ibs_config.max_cnt_op = 250000;
429 ibs_config.op_enabled = 0;
430 ibs_config.dispatched_ops = 1;
2d55a478
RR
431
432 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
56784f11 433 oprofilefs_create_ulong(sb, dir, "enable",
2d55a478 434 &ibs_config.fetch_enabled);
56784f11 435 oprofilefs_create_ulong(sb, dir, "max_count",
2d55a478
RR
436 &ibs_config.max_cnt_fetch);
437 oprofilefs_create_ulong(sb, dir, "rand_enable",
438 &ibs_config.rand_en);
439
ccd755c2 440 dir = oprofilefs_mkdir(sb, root, "ibs_op");
56784f11 441 oprofilefs_create_ulong(sb, dir, "enable",
2d55a478 442 &ibs_config.op_enabled);
56784f11 443 oprofilefs_create_ulong(sb, dir, "max_count",
2d55a478 444 &ibs_config.max_cnt_op);
56784f11 445 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
2d55a478 446 &ibs_config.dispatched_ops);
fc2bd734
RR
447
448 return 0;
56784f11
BK
449}
450
adf5ec0b
RR
451static int op_amd_init(struct oprofile_operations *ops)
452{
fe615cbf 453 ibs_init();
270d3e1a
RR
454 create_arch_files = ops->create_files;
455 ops->create_files = setup_ibs_files;
adf5ec0b
RR
456 return 0;
457}
458
459static void op_amd_exit(void)
460{
fe615cbf 461 ibs_exit();
adf5ec0b
RR
462}
463
9fa6812d
RR
464#else
465
466/* no IBS support */
467
468static int op_amd_init(struct oprofile_operations *ops)
469{
470 return 0;
471}
472
473static void op_amd_exit(void) {}
474
475#endif /* CONFIG_OPROFILE_IBS */
a4c408a4 476
6657fe4f 477struct op_x86_model_spec const op_amd_spec = {
c92960fc
RR
478 .num_counters = NUM_COUNTERS,
479 .num_controls = NUM_CONTROLS,
3370d358
RR
480 .reserved = MSR_AMD_EVENTSEL_RESERVED,
481 .event_mask = OP_EVENT_MASK,
482 .init = op_amd_init,
483 .exit = op_amd_exit,
c92960fc
RR
484 .fill_in_addresses = &op_amd_fill_in_addresses,
485 .setup_ctrs = &op_amd_setup_ctrs,
486 .check_ctrs = &op_amd_check_ctrs,
487 .start = &op_amd_start,
488 .stop = &op_amd_stop,
3370d358 489 .shutdown = &op_amd_shutdown,
1da177e4 490};
This page took 0.396489 seconds and 5 git commands to generate.