3 * athlon / K7 / K8 / Family 10h model-specific MSR operations
5 * @remark Copyright 2002-2009 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 static unsigned long reset_value[NUM_COUNTERS];
52 #ifdef CONFIG_OPROFILE_IBS
54 /* 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 */
60 #define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
61 #define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
63 #define IBS_FETCH_SIZE 6
64 #define IBS_OP_SIZE 12
66 static int has_ibs; /* AMD Family10h and later */
68 struct 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;
77 static struct op_ibs_config ibs_config;
81 /* functions for op_amd_spec */
83 static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
87 for (i = 0; i < NUM_COUNTERS; i++) {
88 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
89 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
91 msrs->counters[i].addr = 0;
94 for (i = 0; i < NUM_CONTROLS; i++) {
95 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
96 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
98 msrs->controls[i].addr = 0;
103 static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
105 unsigned int low, high;
108 /* clear all counters */
109 for (i = 0 ; i < NUM_CONTROLS; ++i) {
110 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
112 CTRL_READ(low, high, msrs, i);
115 CTRL_WRITE(low, high, msrs, i);
118 /* avoid a false detection of ctr overflows in NMI handler */
119 for (i = 0; i < NUM_COUNTERS; ++i) {
120 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
122 CTR_WRITE(1, msrs, i);
125 /* enable active counters */
126 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;
130 CTR_WRITE(counter_config[i].count, msrs, i);
132 CTRL_READ(low, high, msrs, i);
135 CTRL_SET_ENABLE(low);
136 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);
141 CTRL_SET_HOST_ONLY(high, 0);
142 CTRL_SET_GUEST_ONLY(high, 0);
144 CTRL_WRITE(low, high, msrs, i);
151 #ifdef CONFIG_OPROFILE_IBS
154 op_amd_handle_ibs(struct pt_regs * const regs,
155 struct op_msrs const * const msrs)
159 struct op_entry entry;
164 if (ibs_config.fetch_enabled) {
165 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
166 if (high & IBS_FETCH_HIGH_VALID_BIT) {
167 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
168 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);
174 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
175 oprofile_add_data(&entry, (u32)msr);
176 oprofile_add_data(&entry, (u32)(msr >> 32));
177 oprofile_write_commit(&entry);
179 /* reenable the IRQ */
180 high &= ~IBS_FETCH_HIGH_VALID_BIT;
181 high |= IBS_FETCH_HIGH_ENABLE;
182 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
183 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
187 if (ibs_config.op_enabled) {
188 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
189 if (low & IBS_OP_LOW_VALID_BIT) {
190 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
191 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));
195 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
196 oprofile_add_data(&entry, (u32)msr);
197 oprofile_add_data(&entry, (u32)(msr >> 32));
198 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
199 oprofile_add_data(&entry, (u32)msr);
200 oprofile_add_data(&entry, (u32)(msr >> 32));
201 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
202 oprofile_add_data(&entry, (u32)msr);
203 oprofile_add_data(&entry, (u32)(msr >> 32));
204 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
205 oprofile_add_data(&entry, (u32)msr);
206 oprofile_add_data(&entry, (u32)(msr >> 32));
207 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
208 oprofile_add_data(&entry, (u32)msr);
209 oprofile_add_data(&entry, (u32)(msr >> 32));
210 oprofile_write_commit(&entry);
212 /* reenable the IRQ */
214 low &= ~IBS_OP_LOW_VALID_BIT;
215 low |= IBS_OP_LOW_ENABLE;
216 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
223 static inline void op_amd_start_ibs(void)
225 unsigned int low, high;
226 if (has_ibs && ibs_config.fetch_enabled) {
227 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
228 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
229 + IBS_FETCH_HIGH_ENABLE;
230 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
233 if (has_ibs && ibs_config.op_enabled) {
234 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
235 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
238 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
242 static void op_amd_stop_ibs(void)
244 unsigned int low, high;
245 if (has_ibs && ibs_config.fetch_enabled) {
246 /* clear max count and enable */
249 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
252 if (has_ibs && ibs_config.op_enabled) {
253 /* clear max count and enable */
256 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
262 static inline int op_amd_handle_ibs(struct pt_regs * const regs,
263 struct op_msrs const * const msrs) { }
264 static inline void op_amd_start_ibs(void) { }
265 static inline void op_amd_stop_ibs(void) { }
269 static int op_amd_check_ctrs(struct pt_regs * const regs,
270 struct op_msrs const * const msrs)
272 unsigned int low, high;
275 for (i = 0 ; i < NUM_COUNTERS; ++i) {
278 CTR_READ(low, high, msrs, i);
279 if (CTR_OVERFLOWED(low)) {
280 oprofile_add_sample(regs, i);
281 CTR_WRITE(reset_value[i], msrs, i);
285 op_amd_handle_ibs(regs, msrs);
287 /* See op_model_ppro.c */
291 static void op_amd_start(struct op_msrs const * const msrs)
293 unsigned int low, high;
295 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
296 if (reset_value[i]) {
297 CTRL_READ(low, high, msrs, i);
298 CTRL_SET_ACTIVE(low);
299 CTRL_WRITE(low, high, msrs, i);
306 static void op_amd_stop(struct op_msrs const * const msrs)
308 unsigned int low, high;
312 * Subtle: stop on all counters to avoid race with setting our
315 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
318 CTRL_READ(low, high, msrs, i);
319 CTRL_SET_INACTIVE(low);
320 CTRL_WRITE(low, high, msrs, i);
326 static void op_amd_shutdown(struct op_msrs const * const msrs)
330 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
331 if (CTR_IS_RESERVED(msrs, i))
332 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
334 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
335 if (CTRL_IS_RESERVED(msrs, i))
336 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
340 #ifdef CONFIG_OPROFILE_IBS
342 static u8 ibs_eilvt_off;
344 static inline void apic_init_ibs_nmi_per_cpu(void *arg)
346 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
349 static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
351 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
354 static int init_ibs_nmi(void)
356 #define IBSCTL_LVTOFFSETVAL (1 << 8)
358 struct pci_dev *cpu_cfg;
363 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
368 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
369 PCI_DEVICE_ID_AMD_10H_NB_MISC,
374 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
375 | IBSCTL_LVTOFFSETVAL);
376 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
377 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
378 pci_dev_put(cpu_cfg);
379 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
380 "IBSCTL = 0x%08x", value);
386 printk(KERN_DEBUG "No CPU node configured for IBS");
392 /* Works only for 64bit with proper numa implementation. */
393 if (nodes != num_possible_nodes()) {
394 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
395 "found: %d, expected %d",
396 nodes, num_possible_nodes());
403 /* uninitialize the APIC for the IBS interrupts if needed */
404 static void clear_ibs_nmi(void)
407 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
410 /* initialize the APIC for the IBS interrupts if available */
411 static void ibs_init(void)
413 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
418 if (init_ibs_nmi()) {
423 printk(KERN_INFO "oprofile: AMD IBS detected\n");
426 static void ibs_exit(void)
434 static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
436 static int setup_ibs_files(struct super_block *sb, struct dentry *root)
441 /* architecture specific files */
442 if (create_arch_files)
443 ret = create_arch_files(sb, root);
451 /* model specific files */
453 /* setup some reasonable defaults */
454 ibs_config.max_cnt_fetch = 250000;
455 ibs_config.fetch_enabled = 0;
456 ibs_config.max_cnt_op = 250000;
457 ibs_config.op_enabled = 0;
458 ibs_config.dispatched_ops = 1;
460 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
461 oprofilefs_create_ulong(sb, dir, "enable",
462 &ibs_config.fetch_enabled);
463 oprofilefs_create_ulong(sb, dir, "max_count",
464 &ibs_config.max_cnt_fetch);
465 oprofilefs_create_ulong(sb, dir, "rand_enable",
466 &ibs_config.rand_en);
468 dir = oprofilefs_mkdir(sb, root, "ibs_op");
469 oprofilefs_create_ulong(sb, dir, "enable",
470 &ibs_config.op_enabled);
471 oprofilefs_create_ulong(sb, dir, "max_count",
472 &ibs_config.max_cnt_op);
473 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
474 &ibs_config.dispatched_ops);
479 static int op_amd_init(struct oprofile_operations *ops)
482 create_arch_files = ops->create_files;
483 ops->create_files = setup_ibs_files;
487 static void op_amd_exit(void)
496 static int op_amd_init(struct oprofile_operations *ops)
501 static void op_amd_exit(void) {}
503 #endif /* CONFIG_OPROFILE_IBS */
505 struct op_x86_model_spec const op_amd_spec = {
508 .num_counters = NUM_COUNTERS,
509 .num_controls = NUM_CONTROLS,
510 .fill_in_addresses = &op_amd_fill_in_addresses,
511 .setup_ctrs = &op_amd_setup_ctrs,
512 .check_ctrs = &op_amd_check_ctrs,
513 .start = &op_amd_start,
514 .stop = &op_amd_stop,
515 .shutdown = &op_amd_shutdown