2 * @file op_model_athlon.c
3 * athlon / K7 / K8 / Family 10h model-specific MSR operations
5 * @remark Copyright 2002-2008 OProfile authors
6 * @remark Read the file COPYING
9 * @author Philippe Elie
10 * @author Graydon Hoare
11 * @author Robert Richter <robert.richter@amd.com>
12 * @author Barry Kasindorf
15 #include <linux/oprofile.h>
16 #include <linux/device.h>
17 #include <linux/pci.h>
19 #include <asm/ptrace.h>
23 #include "op_x86_model.h"
24 #include "op_counter.h"
26 #define NUM_COUNTERS 4
27 #define NUM_CONTROLS 4
29 #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)
32 #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
34 #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)
37 #define CTRL_SET_ACTIVE(n) (n |= (1<<22))
38 #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
39 #define CTRL_CLEAR_LO(x) (x &= (1<<21))
40 #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
41 #define CTRL_SET_ENABLE(val) (val |= 1<<20)
42 #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
43 #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
44 #define CTRL_SET_UM(val, m) (val |= (m << 8))
45 #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))
50 #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)
58 #define IBS_FETCH_CTL_CNT_MASK 0x00000000FFFF0000UL
59 #define IBS_FETCH_CTL_MAX_CNT_MASK 0x000000000000FFFFUL
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]*/
65 /* Codes used in cpu_buffer.c */
66 #define IBS_FETCH_BEGIN 3
67 #define IBS_OP_BEGIN 4
70 #define IBS_CTL_LVT_OFFSET_VALID_BIT (1ULL<<8)
72 /*PCI Extended Configuration Constants */
73 /* MSR to set the IBS control register APIC LVT offset */
74 #define IBS_LVT_OFFSET_PCI 0x1CC
76 /* The function interface needs to be fixed, something like add
77 data. Should then be added to linux/oprofile.h. */
78 extern void oprofile_add_ibs_sample(struct pt_regs *const regs,
79 unsigned int * const ibs_sample, u8 code);
81 struct 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;
93 struct 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;
115 * unitialize the APIC for the IBS interrupts if needed on AMD Family10h+
117 static void clear_ibs_nmi(void);
119 static unsigned long reset_value[NUM_COUNTERS];
120 static int ibs_allowed; /* AMD Family10h and later */
122 struct 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;
131 static struct op_ibs_config ibs_config;
133 /* functions for op_amd_spec */
135 static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
139 for (i = 0; i < NUM_COUNTERS; i++) {
140 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
141 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
143 msrs->counters[i].addr = 0;
146 for (i = 0; i < NUM_CONTROLS; i++) {
147 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
148 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
150 msrs->controls[i].addr = 0;
155 static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
157 unsigned int low, high;
160 /* clear all counters */
161 for (i = 0 ; i < NUM_CONTROLS; ++i) {
162 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
164 CTRL_READ(low, high, msrs, i);
167 CTRL_WRITE(low, high, msrs, i);
170 /* avoid a false detection of ctr overflows in NMI handler */
171 for (i = 0; i < NUM_COUNTERS; ++i) {
172 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
174 CTR_WRITE(1, msrs, i);
177 /* enable active counters */
178 for (i = 0; i < NUM_COUNTERS; ++i) {
179 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
180 reset_value[i] = counter_config[i].count;
182 CTR_WRITE(counter_config[i].count, msrs, i);
184 CTRL_READ(low, high, msrs, i);
187 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);
191 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);
196 CTRL_WRITE(low, high, msrs, i);
204 op_amd_handle_ibs(struct pt_regs * const regs,
205 struct op_msrs const * const msrs)
207 unsigned int low, high;
208 struct ibs_fetch_sample ibs_fetch;
209 struct ibs_op_sample ibs_op;
214 if (ibs_config.fetch_enabled) {
215 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;
226 oprofile_add_ibs_sample(regs,
227 (unsigned int *)&ibs_fetch,
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);
239 if (ibs_config.op_enabled) {
240 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;
261 /* reenable the IRQ */
262 oprofile_add_ibs_sample(regs,
263 (unsigned int *)&ibs_op,
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);
275 static int op_amd_check_ctrs(struct pt_regs * const regs,
276 struct op_msrs const * const msrs)
278 unsigned int low, high;
281 for (i = 0 ; i < NUM_COUNTERS; ++i) {
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);
291 op_amd_handle_ibs(regs, msrs);
293 /* See op_model_ppro.c */
297 static void op_amd_start(struct op_msrs const * const msrs)
299 unsigned int low, high;
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);
308 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);
314 if (ibs_allowed && ibs_config.op_enabled) {
315 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF) + IBS_OP_ENABLE;
317 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
322 static void op_amd_stop(struct op_msrs const * const msrs)
324 unsigned int low, high;
327 /* Subtle: stop on all counters to avoid race with
328 * setting our pm callback */
329 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
332 CTRL_READ(low, high, msrs, i);
333 CTRL_SET_INACTIVE(low);
334 CTRL_WRITE(low, high, msrs, i);
337 if (ibs_allowed && ibs_config.fetch_enabled) {
338 low = 0; /* clear max count and enable */
340 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
343 if (ibs_allowed && ibs_config.op_enabled) {
344 low = 0; /* clear max count and enable */
346 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
350 static void op_amd_shutdown(struct op_msrs const * const msrs)
354 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
355 if (CTR_IS_RESERVED(msrs, i))
356 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
358 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
359 if (CTRL_IS_RESERVED(msrs, i))
360 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
368 static void setup_ibs(void)
373 static void clear_ibs_nmi(void) {}
375 static int op_amd_init(struct oprofile_operations *ops)
380 static void op_amd_exit(void) {}
384 static u8 ibs_eilvt_off;
386 static inline void apic_init_ibs_nmi_per_cpu(void *arg)
388 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
391 static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
393 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
396 static int pfm_amd64_setup_eilvt(void)
398 #define IBSCTL_LVTOFFSETVAL (1 << 8)
400 struct pci_dev *cpu_cfg;
405 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
410 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
411 PCI_DEVICE_ID_AMD_10H_NB_MISC,
416 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
417 | IBSCTL_LVTOFFSETVAL);
418 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
419 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
420 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
421 "IBSCTL = 0x%08x", value);
427 printk(KERN_DEBUG "No CPU node configured for IBS");
433 /* Works only for 64bit with proper numa implementation. */
434 if (nodes != num_possible_nodes()) {
435 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
436 "found: %d, expected %d",
437 nodes, num_possible_nodes());
445 * initialize the APIC for the IBS interrupts
446 * if available (AMD Family10h rev B0 and later)
448 static void setup_ibs(void)
450 ibs_allowed = boot_cpu_has(X86_FEATURE_IBS);
455 if (pfm_amd64_setup_eilvt())
461 * unitialize the APIC for the IBS interrupts if needed on AMD Family10h
462 * rev B0 and later */
463 static void clear_ibs_nmi(void)
466 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
469 static int (*create_arch_files)(struct super_block * sb, struct dentry * root);
471 static int setup_ibs_files(struct super_block * sb, struct dentry * root)
477 /* architecture specific files */
478 if (create_arch_files)
479 ret = create_arch_files(sb, root);
487 /* model specific files */
489 /* setup some reasonable defaults */
490 ibs_config.max_cnt_fetch = 250000;
491 ibs_config.fetch_enabled = 0;
492 ibs_config.max_cnt_op = 250000;
493 ibs_config.op_enabled = 0;
494 ibs_config.dispatched_ops = 1;
495 snprintf(buf, sizeof(buf), "ibs_fetch");
496 dir = oprofilefs_mkdir(sb, root, buf);
497 oprofilefs_create_ulong(sb, dir, "rand_enable",
498 &ibs_config.rand_en);
499 oprofilefs_create_ulong(sb, dir, "enable",
500 &ibs_config.fetch_enabled);
501 oprofilefs_create_ulong(sb, dir, "max_count",
502 &ibs_config.max_cnt_fetch);
503 snprintf(buf, sizeof(buf), "ibs_uops");
504 dir = oprofilefs_mkdir(sb, root, buf);
505 oprofilefs_create_ulong(sb, dir, "enable",
506 &ibs_config.op_enabled);
507 oprofilefs_create_ulong(sb, dir, "max_count",
508 &ibs_config.max_cnt_op);
509 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
510 &ibs_config.dispatched_ops);
515 static int op_amd_init(struct oprofile_operations *ops)
518 create_arch_files = ops->create_files;
519 ops->create_files = setup_ibs_files;
523 static void op_amd_exit(void)
530 struct op_x86_model_spec const op_amd_spec = {
533 .num_counters = NUM_COUNTERS,
534 .num_controls = NUM_CONTROLS,
535 .fill_in_addresses = &op_amd_fill_in_addresses,
536 .setup_ctrs = &op_amd_setup_ctrs,
537 .check_ctrs = &op_amd_check_ctrs,
538 .start = &op_amd_start,
539 .stop = &op_amd_stop,
540 .shutdown = &op_amd_shutdown