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"
25 #include "../../../drivers/oprofile/cpu_buffer.h"
27 #define NUM_COUNTERS 4
28 #define NUM_CONTROLS 4
30 #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
31 #define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
32 #define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0)
33 #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
35 #define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
36 #define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
37 #define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
38 #define CTRL_SET_ACTIVE(n) (n |= (1<<22))
39 #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
40 #define CTRL_CLEAR_LO(x) (x &= (1<<21))
41 #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
42 #define CTRL_SET_ENABLE(val) (val |= 1<<20)
43 #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
44 #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
45 #define CTRL_SET_UM(val, m) (val |= (m << 8))
46 #define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
47 #define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
48 #define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
49 #define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
51 static unsigned long reset_value[NUM_COUNTERS];
53 #ifdef CONFIG_OPROFILE_IBS
55 /* IbsFetchCtl bits/masks */
56 #define IBS_FETCH_HIGH_VALID_BIT (1UL << 17) /* bit 49 */
57 #define IBS_FETCH_HIGH_ENABLE (1UL << 16) /* bit 48 */
58 #define IBS_FETCH_LOW_MAX_CNT_MASK 0x0000FFFFUL /* MaxCnt mask */
61 #define IBS_OP_LOW_VALID_BIT (1ULL<<18) /* bit 18 */
62 #define IBS_OP_LOW_ENABLE (1ULL<<17) /* bit 17 */
65 * The function interface needs to be fixed, something like add
66 * data. Should then be added to linux/oprofile.h.
69 void oprofile_add_data(struct op_entry *entry, struct pt_regs * const regs,
70 unsigned long pc, int code, int size);
72 #define IBS_FETCH_SIZE 6
73 #define IBS_OP_SIZE 12
75 static int has_ibs; /* AMD Family10h and later */
77 struct op_ibs_config {
78 unsigned long op_enabled;
79 unsigned long fetch_enabled;
80 unsigned long max_cnt_fetch;
81 unsigned long max_cnt_op;
82 unsigned long rand_en;
83 unsigned long dispatched_ops;
86 static struct op_ibs_config ibs_config;
90 /* functions for op_amd_spec */
92 static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
96 for (i = 0; i < NUM_COUNTERS; i++) {
97 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
98 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
100 msrs->counters[i].addr = 0;
103 for (i = 0; i < NUM_CONTROLS; i++) {
104 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
105 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
107 msrs->controls[i].addr = 0;
112 static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
114 unsigned int low, high;
117 /* clear all counters */
118 for (i = 0 ; i < NUM_CONTROLS; ++i) {
119 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
121 CTRL_READ(low, high, msrs, i);
124 CTRL_WRITE(low, high, msrs, i);
127 /* avoid a false detection of ctr overflows in NMI handler */
128 for (i = 0; i < NUM_COUNTERS; ++i) {
129 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
131 CTR_WRITE(1, msrs, i);
134 /* enable active counters */
135 for (i = 0; i < NUM_COUNTERS; ++i) {
136 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
137 reset_value[i] = counter_config[i].count;
139 CTR_WRITE(counter_config[i].count, msrs, i);
141 CTRL_READ(low, high, msrs, i);
144 CTRL_SET_ENABLE(low);
145 CTRL_SET_USR(low, counter_config[i].user);
146 CTRL_SET_KERN(low, counter_config[i].kernel);
147 CTRL_SET_UM(low, counter_config[i].unit_mask);
148 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
149 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
150 CTRL_SET_HOST_ONLY(high, 0);
151 CTRL_SET_GUEST_ONLY(high, 0);
153 CTRL_WRITE(low, high, msrs, i);
160 #ifdef CONFIG_OPROFILE_IBS
163 op_amd_handle_ibs(struct pt_regs * const regs,
164 struct op_msrs const * const msrs)
168 struct op_entry entry;
173 if (ibs_config.fetch_enabled) {
174 rdmsr(MSR_AMD64_IBSFETCHCTL, low, high);
175 if (high & IBS_FETCH_HIGH_VALID_BIT) {
176 rdmsrl(MSR_AMD64_IBSFETCHLINAD, msr);
177 oprofile_add_data(&entry, regs, msr, IBS_FETCH_CODE,
179 op_cpu_buffer_add_data(&entry, (u32)msr);
180 op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
181 op_cpu_buffer_add_data(&entry, low);
182 op_cpu_buffer_add_data(&entry, high);
183 rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, msr);
184 op_cpu_buffer_add_data(&entry, (u32)msr);
185 op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
186 op_cpu_buffer_write_commit(&entry);
188 /* reenable the IRQ */
189 high &= ~IBS_FETCH_HIGH_VALID_BIT;
190 high |= IBS_FETCH_HIGH_ENABLE;
191 low &= IBS_FETCH_LOW_MAX_CNT_MASK;
192 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
196 if (ibs_config.op_enabled) {
197 rdmsr(MSR_AMD64_IBSOPCTL, low, high);
198 if (low & IBS_OP_LOW_VALID_BIT) {
199 rdmsrl(MSR_AMD64_IBSOPRIP, msr);
200 oprofile_add_data(&entry, regs, msr, IBS_OP_CODE,
202 op_cpu_buffer_add_data(&entry, (u32)msr);
203 op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
204 rdmsrl(MSR_AMD64_IBSOPDATA, msr);
205 op_cpu_buffer_add_data(&entry, (u32)msr);
206 op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
207 rdmsrl(MSR_AMD64_IBSOPDATA2, msr);
208 op_cpu_buffer_add_data(&entry, (u32)msr);
209 op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
210 rdmsrl(MSR_AMD64_IBSOPDATA3, msr);
211 op_cpu_buffer_add_data(&entry, (u32)msr);
212 op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
213 rdmsrl(MSR_AMD64_IBSDCLINAD, msr);
214 op_cpu_buffer_add_data(&entry, (u32)msr);
215 op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
216 rdmsrl(MSR_AMD64_IBSDCPHYSAD, msr);
217 op_cpu_buffer_add_data(&entry, (u32)msr);
218 op_cpu_buffer_add_data(&entry, (u32)(msr >> 32));
219 op_cpu_buffer_write_commit(&entry);
221 /* reenable the IRQ */
223 low &= ~IBS_OP_LOW_VALID_BIT;
224 low |= IBS_OP_LOW_ENABLE;
225 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
234 static int op_amd_check_ctrs(struct pt_regs * const regs,
235 struct op_msrs const * const msrs)
237 unsigned int low, high;
240 for (i = 0 ; i < NUM_COUNTERS; ++i) {
243 CTR_READ(low, high, msrs, i);
244 if (CTR_OVERFLOWED(low)) {
245 oprofile_add_sample(regs, i);
246 CTR_WRITE(reset_value[i], msrs, i);
250 #ifdef CONFIG_OPROFILE_IBS
251 op_amd_handle_ibs(regs, msrs);
254 /* See op_model_ppro.c */
258 static void op_amd_start(struct op_msrs const * const msrs)
260 unsigned int low, high;
262 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
263 if (reset_value[i]) {
264 CTRL_READ(low, high, msrs, i);
265 CTRL_SET_ACTIVE(low);
266 CTRL_WRITE(low, high, msrs, i);
270 #ifdef CONFIG_OPROFILE_IBS
271 if (has_ibs && ibs_config.fetch_enabled) {
272 low = (ibs_config.max_cnt_fetch >> 4) & 0xFFFF;
273 high = ((ibs_config.rand_en & 0x1) << 25) /* bit 57 */
274 + IBS_FETCH_HIGH_ENABLE;
275 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
278 if (has_ibs && ibs_config.op_enabled) {
279 low = ((ibs_config.max_cnt_op >> 4) & 0xFFFF)
280 + ((ibs_config.dispatched_ops & 0x1) << 19) /* bit 19 */
283 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
289 static void op_amd_stop(struct op_msrs const * const msrs)
291 unsigned int low, high;
295 * Subtle: stop on all counters to avoid race with setting our
298 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
301 CTRL_READ(low, high, msrs, i);
302 CTRL_SET_INACTIVE(low);
303 CTRL_WRITE(low, high, msrs, i);
306 #ifdef CONFIG_OPROFILE_IBS
307 if (has_ibs && ibs_config.fetch_enabled) {
308 /* clear max count and enable */
311 wrmsr(MSR_AMD64_IBSFETCHCTL, low, high);
314 if (has_ibs && ibs_config.op_enabled) {
315 /* clear max count and enable */
318 wrmsr(MSR_AMD64_IBSOPCTL, low, high);
323 static void op_amd_shutdown(struct op_msrs const * const msrs)
327 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
328 if (CTR_IS_RESERVED(msrs, i))
329 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
331 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
332 if (CTRL_IS_RESERVED(msrs, i))
333 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
337 #ifdef CONFIG_OPROFILE_IBS
339 static u8 ibs_eilvt_off;
341 static inline void apic_init_ibs_nmi_per_cpu(void *arg)
343 ibs_eilvt_off = setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_NMI, 0);
346 static inline void apic_clear_ibs_nmi_per_cpu(void *arg)
348 setup_APIC_eilvt_ibs(0, APIC_EILVT_MSG_FIX, 1);
351 static int init_ibs_nmi(void)
353 #define IBSCTL_LVTOFFSETVAL (1 << 8)
355 struct pci_dev *cpu_cfg;
360 on_each_cpu(apic_init_ibs_nmi_per_cpu, NULL, 1);
365 cpu_cfg = pci_get_device(PCI_VENDOR_ID_AMD,
366 PCI_DEVICE_ID_AMD_10H_NB_MISC,
371 pci_write_config_dword(cpu_cfg, IBSCTL, ibs_eilvt_off
372 | IBSCTL_LVTOFFSETVAL);
373 pci_read_config_dword(cpu_cfg, IBSCTL, &value);
374 if (value != (ibs_eilvt_off | IBSCTL_LVTOFFSETVAL)) {
375 pci_dev_put(cpu_cfg);
376 printk(KERN_DEBUG "Failed to setup IBS LVT offset, "
377 "IBSCTL = 0x%08x", value);
383 printk(KERN_DEBUG "No CPU node configured for IBS");
389 /* Works only for 64bit with proper numa implementation. */
390 if (nodes != num_possible_nodes()) {
391 printk(KERN_DEBUG "Failed to setup CPU node(s) for IBS, "
392 "found: %d, expected %d",
393 nodes, num_possible_nodes());
400 /* uninitialize the APIC for the IBS interrupts if needed */
401 static void clear_ibs_nmi(void)
404 on_each_cpu(apic_clear_ibs_nmi_per_cpu, NULL, 1);
407 /* initialize the APIC for the IBS interrupts if available */
408 static void ibs_init(void)
410 has_ibs = boot_cpu_has(X86_FEATURE_IBS);
415 if (init_ibs_nmi()) {
420 printk(KERN_INFO "oprofile: AMD IBS detected\n");
423 static void ibs_exit(void)
431 static int (*create_arch_files)(struct super_block *sb, struct dentry *root);
433 static int setup_ibs_files(struct super_block *sb, struct dentry *root)
438 /* architecture specific files */
439 if (create_arch_files)
440 ret = create_arch_files(sb, root);
448 /* model specific files */
450 /* setup some reasonable defaults */
451 ibs_config.max_cnt_fetch = 250000;
452 ibs_config.fetch_enabled = 0;
453 ibs_config.max_cnt_op = 250000;
454 ibs_config.op_enabled = 0;
455 ibs_config.dispatched_ops = 1;
457 dir = oprofilefs_mkdir(sb, root, "ibs_fetch");
458 oprofilefs_create_ulong(sb, dir, "enable",
459 &ibs_config.fetch_enabled);
460 oprofilefs_create_ulong(sb, dir, "max_count",
461 &ibs_config.max_cnt_fetch);
462 oprofilefs_create_ulong(sb, dir, "rand_enable",
463 &ibs_config.rand_en);
465 dir = oprofilefs_mkdir(sb, root, "ibs_op");
466 oprofilefs_create_ulong(sb, dir, "enable",
467 &ibs_config.op_enabled);
468 oprofilefs_create_ulong(sb, dir, "max_count",
469 &ibs_config.max_cnt_op);
470 oprofilefs_create_ulong(sb, dir, "dispatched_ops",
471 &ibs_config.dispatched_ops);
476 static int op_amd_init(struct oprofile_operations *ops)
479 create_arch_files = ops->create_files;
480 ops->create_files = setup_ibs_files;
484 static void op_amd_exit(void)
493 static int op_amd_init(struct oprofile_operations *ops)
498 static void op_amd_exit(void) {}
500 #endif /* CONFIG_OPROFILE_IBS */
502 struct op_x86_model_spec const op_amd_spec = {
505 .num_counters = NUM_COUNTERS,
506 .num_controls = NUM_CONTROLS,
507 .fill_in_addresses = &op_amd_fill_in_addresses,
508 .setup_ctrs = &op_amd_setup_ctrs,
509 .check_ctrs = &op_amd_check_ctrs,
510 .start = &op_amd_start,
511 .stop = &op_amd_stop,
512 .shutdown = &op_amd_shutdown