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_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
30 #define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0)
31 #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
33 #define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
34 #define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
35 #define CTRL_CLEAR_LO(x) (x &= (1<<21))
36 #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
37 #define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
38 #define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
39 #define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
40 #define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
42 static unsigned long reset_value[NUM_COUNTERS];
44 #ifdef CONFIG_OPROFILE_IBS
46 /* IbsFetchCtl bits/masks */
47 #define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
48 #define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
49 #define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
52 #define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
53 #define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
55 #define IBS_FETCH_SIZE 6
56 #define IBS_OP_SIZE 12
58 static int has_ibs; /* AMD Family10h and later */
60 struct op_ibs_config {
61 unsigned long op_enabled;
62 unsigned long fetch_enabled;
63 unsigned long max_cnt_fetch;
64 unsigned long max_cnt_op;
65 unsigned long rand_en;
66 unsigned long dispatched_ops;
69 static struct op_ibs_config ibs_config;
73 /* functions for op_amd_spec */
75 static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
79 for (i = 0; i < NUM_COUNTERS; i++) {
80 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
81 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
83 msrs->counters[i].addr = 0;
86 for (i = 0; i < NUM_CONTROLS; i++) {
87 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
88 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
90 msrs->controls[i].addr = 0;
95 static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
97 unsigned int low, high;
100 /* clear all counters */
101 for (i = 0 ; i < NUM_CONTROLS; ++i) {
102 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
104 CTRL_READ(low, high, msrs, i);
107 CTRL_WRITE(low, high, msrs, i);
110 /* avoid a false detection of ctr overflows in NMI handler */
111 for (i = 0; i < NUM_COUNTERS; ++i) {
112 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
114 CTR_WRITE(1, msrs, i);
117 /* enable active counters */
118 for (i = 0; i < NUM_COUNTERS; ++i) {
119 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
120 reset_value[i] = counter_config[i].count;
122 CTR_WRITE(counter_config[i].count, msrs, i);
124 CTRL_READ(low, high, msrs, i);
127 CTRL_SET_ENABLE(low);
128 CTRL_SET_USR(low, counter_config[i].user);
129 CTRL_SET_KERN(low, counter_config[i].kernel);
130 CTRL_SET_UM(low, counter_config[i].unit_mask);
131 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
132 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
133 CTRL_SET_HOST_ONLY(high, 0);
134 CTRL_SET_GUEST_ONLY(high, 0);
136 CTRL_WRITE(low, high, msrs, i);
143 #ifdef CONFIG_OPROFILE_IBS
146 op_amd_handle_ibs(struct pt_regs * const regs,
147 struct op_msrs const * const msrs)
151 struct op_entry entry;
156 if (ibs_config.fetch_enabled) {
157 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
158 if (high & IBS_FETCH_HIGH_VALID_BIT) {
159 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
160 oprofile_write_reserve(&entry, regs, msr,
161 IBS_FETCH_CODE, IBS_FETCH_SIZE);
162 oprofile_add_data(&entry, (u32)msr);
163 oprofile_add_data(&entry, (u32)(msr >> 32));
164 oprofile_add_data(&entry, low);
165 oprofile_add_data(&entry, high);
166 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
167 oprofile_add_data(&entry, (u32)msr);
168 oprofile_add_data(&entry, (u32)(msr >> 32));
169 oprofile_write_commit(&entry);
171 /* reenable the IRQ */
172 high &= ~IBS_FETCH_HIGH_VALID_BIT;
173 high |= IBS_FETCH_HIGH_ENABLE;
174 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
175 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
179 if (ibs_config.op_enabled) {
180 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
181 if (low & IBS_OP_LOW_VALID_BIT) {
182 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
183 oprofile_write_reserve(&entry, regs, msr,
184 IBS_OP_CODE, IBS_OP_SIZE);
185 oprofile_add_data(&entry, (u32)msr);
186 oprofile_add_data(&entry, (u32)(msr >> 32));
187 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
188 oprofile_add_data(&entry, (u32)msr);
189 oprofile_add_data(&entry, (u32)(msr >> 32));
190 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
191 oprofile_add_data(&entry, (u32)msr);
192 oprofile_add_data(&entry, (u32)(msr >> 32));
193 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
194 oprofile_add_data(&entry, (u32)msr);
195 oprofile_add_data(&entry, (u32)(msr >> 32));
196 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
197 oprofile_add_data(&entry, (u32)msr);
198 oprofile_add_data(&entry, (u32)(msr >> 32));
199 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
200 oprofile_add_data(&entry, (u32)msr);
201 oprofile_add_data(&entry, (u32)(msr >> 32));
202 oprofile_write_commit(&entry);
204 /* reenable the IRQ */
206 low &= ~IBS_OP_LOW_VALID_BIT;
207 low |= IBS_OP_LOW_ENABLE;
208 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
215 static inline void op_amd_start_ibs(void)
217 unsigned int low, high;
218 if (has_ibs && ibs_config.fetch_enabled) {
219 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
220 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
221 + IBS_FETCH_HIGH_ENABLE;
222 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
225 if (has_ibs && ibs_config.op_enabled) {
226 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
227 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
230 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
234 static void op_amd_stop_ibs(void)
236 unsigned int low, high;
237 if (has_ibs && ibs_config.fetch_enabled) {
238 /* clear max count and enable */
241 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
244 if (has_ibs && ibs_config.op_enabled) {
245 /* clear max count and enable */
248 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
254 static inline int op_amd_handle_ibs(struct pt_regs * const regs,
255 struct op_msrs const * const msrs) { }
256 static inline void op_amd_start_ibs(void) { }
257 static inline void op_amd_stop_ibs(void) { }
261 static int op_amd_check_ctrs(struct pt_regs * const regs,
262 struct op_msrs const * const msrs)
264 unsigned int low, high;
267 for (i = 0 ; i < NUM_COUNTERS; ++i) {
270 CTR_READ(low, high, msrs, i);
271 if (CTR_OVERFLOWED(low)) {
272 oprofile_add_sample(regs, i);
273 CTR_WRITE(reset_value[i], msrs, i);
277 op_amd_handle_ibs(regs, msrs);
279 /* See op_model_ppro.c */
283 static void op_amd_start(struct op_msrs const * const msrs)
285 unsigned int low, high;
287 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
288 if (reset_value[i]) {
289 CTRL_READ(low, high, msrs, i);
290 CTRL_SET_ACTIVE(low);
291 CTRL_WRITE(low, high, msrs, i);
298 static void op_amd_stop(struct op_msrs const * const msrs)
300 unsigned int low, high;
304 * Subtle: stop on all counters to avoid race with setting our
307 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
310 CTRL_READ(low, high, msrs, i);
311 CTRL_SET_INACTIVE(low);
312 CTRL_WRITE(low, high, msrs, i);
318 static void op_amd_shutdown(struct op_msrs const * const msrs)
322 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
323 if (CTR_IS_RESERVED(msrs, i))
324 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
326 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
327 if (CTRL_IS_RESERVED(msrs, i))
328 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
332 #ifdef CONFIG_OPROFILE_IBS
334 static u8 ibs_eilvt_off;
336 static inline void apic_init_ibs_nmi_per_cpu(void *arg)
338 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
341 static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
343 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
346 static int init_ibs_nmi(void)
348 #define IBSCTL_LVTOFFSETVAL (1 << 8)
350 struct pci_dev *cpu_cfg;
355 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
360 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
361 PCI_DEVICE_ID_AMD_10H_NB_MISC,
366 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
367 | IBSCTL_LVTOFFSETVAL);
368 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
369 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
370 pci_dev_put(cpu_cfg);
371 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
372 "IBSCTL = 0x%08x", value);
378 printk(KERN_DEBUG "No CPU node configured for IBS");
384 /* Works only for 64bit with proper numa implementation. */
385 if (nodes != num_possible_nodes()) {
386 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
387 "found: %d, expected %d",
388 nodes, num_possible_nodes());
395 /* uninitialize the APIC for the IBS interrupts if needed */
396 static void clear_ibs_nmi(void)
399 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
402 /* initialize the APIC for the IBS interrupts if available */
403 static void ibs_init(void)
405 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
410 if (init_ibs_nmi()) {
415 printk(KERN_INFO "oprofile: AMD IBS detected\n");
418 static void ibs_exit(void)
426 static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
428 static int setup_ibs_files(struct super_block *sb, struct dentry *root)
433 /* architecture specific files */
434 if (create_arch_files)
435 ret = create_arch_files(sb, root);
443 /* model specific files */
445 /* setup some reasonable defaults */
446 ibs_config.max_cnt_fetch = 250000;
447 ibs_config.fetch_enabled = 0;
448 ibs_config.max_cnt_op = 250000;
449 ibs_config.op_enabled = 0;
450 ibs_config.dispatched_ops = 1;
452 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
453 oprofilefs_create_ulong(sb, dir, "enable",
454 &ibs_config.fetch_enabled);
455 oprofilefs_create_ulong(sb, dir, "max_count",
456 &ibs_config.max_cnt_fetch);
457 oprofilefs_create_ulong(sb, dir, "rand_enable",
458 &ibs_config.rand_en);
460 dir = oprofilefs_mkdir(sb, root, "ibs_op");
461 oprofilefs_create_ulong(sb, dir, "enable",
462 &ibs_config.op_enabled);
463 oprofilefs_create_ulong(sb, dir, "max_count",
464 &ibs_config.max_cnt_op);
465 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
466 &ibs_config.dispatched_ops);
471 static int op_amd_init(struct oprofile_operations *ops)
474 create_arch_files = ops->create_files;
475 ops->create_files = setup_ibs_files;
479 static void op_amd_exit(void)
488 static int op_amd_init(struct oprofile_operations *ops)
493 static void op_amd_exit(void) {}
495 #endif /* CONFIG_OPROFILE_IBS */
497 struct op_x86_model_spec const op_amd_spec = {
500 .num_counters = NUM_COUNTERS,
501 .num_controls = NUM_CONTROLS,
502 .fill_in_addresses = &op_amd_fill_in_addresses,
503 .setup_ctrs = &op_amd_setup_ctrs,
504 .check_ctrs = &op_amd_check_ctrs,
505 .start = &op_amd_start,
506 .stop = &op_amd_stop,
507 .shutdown = &op_amd_shutdown