18 #include <linux/device.h>
19 #include <linux/pci.h>
22 #include <asm/ptrace.h>
26 #include <asm/processor.h>
27 #include <asm/cpufeature.h>
32 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
33 #define NUM_VIRT_COUNTERS 32
35 #define NUM_VIRT_COUNTERS 0
38 #define OP_EVENT_MASK 0x0FFF
39 #define OP_CTR_OVERFLOW (1ULL<<31)
41 #define MSR_AMD_EVENTSEL_RESERVED ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
46 #define IBS_FETCH_SIZE 6
47 #define IBS_OP_SIZE 12
73 #define IBS_RANDOM_BITS 12
74 #define IBS_RANDOM_MASK ((1ULL << IBS_RANDOM_BITS) - 1)
75 #define IBS_RANDOM_MAXCNT_OFFSET (1ULL << (IBS_RANDOM_BITS - 5))
83 static unsigned int lfsr_random(
void)
85 static unsigned int lfsr_value = 0xF00D;
89 bit = ((lfsr_value >> 0) ^
92 (lfsr_value >> 5)) & 0x0001;
95 lfsr_value = (lfsr_value >> 1) | (bit << 15);
107 static inline u64 op_amd_randomize_ibs_op(
u64 val)
109 unsigned int random = lfsr_random();
125 val += (
s8)(random >> 4);
134 struct op_msrs const *
const msrs)
191 static inline void op_amd_start_ibs(
void)
217 if (!(ibs_caps & IBS_CAPS_RDWROPCNT)) {
222 val =
clamp(val, 0x0081ULL, 0xFF80ULL);
253 static void op_amd_stop_ibs(
void)
267 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
270 struct op_msrs const *
const msrs)
276 for (i = 0; i < num_counters; ++
i) {
278 if (!reset_value[virt])
280 rdmsrl(msrs->
controls[i].addr, val);
283 wrmsrl(msrs->
controls[i].addr, val);
291 static void op_amd_shutdown(
struct op_msrs const *
const msrs)
295 for (i = 0; i < num_counters; ++
i) {
303 static int op_amd_fill_in_addresses(
struct op_msrs *
const msrs)
307 for (i = 0; i < num_counters; i++) {
326 op_x86_warn_reserved(i);
327 op_amd_shutdown(msrs);
335 struct op_msrs const *
const msrs)
350 for (i = 0; i < num_counters; ++
i) {
353 rdmsrl(msrs->
controls[i].addr, val);
355 op_x86_warn_in_use(i);
357 wrmsrl(msrs->
controls[i].addr, val);
366 for (i = 0; i < num_counters; ++
i) {
368 if (!reset_value[virt])
372 wrmsrl(msrs->
counters[i].addr, -(
u64)reset_value[virt]);
375 rdmsrl(msrs->
controls[i].addr, val);
378 wrmsrl(msrs->
controls[i].addr, val);
382 static int op_amd_check_ctrs(
struct pt_regs *
const regs,
383 struct op_msrs const *
const msrs)
388 for (i = 0; i < num_counters; ++
i) {
390 if (!reset_value[virt])
392 rdmsrl(msrs->
counters[i].addr, val);
397 wrmsrl(msrs->
counters[i].addr, -(
u64)reset_value[virt]);
400 op_amd_handle_ibs(regs, msrs);
406 static void op_amd_start(
struct op_msrs const *
const msrs)
411 for (i = 0; i < num_counters; ++
i) {
414 rdmsrl(msrs->
controls[i].addr, val);
416 wrmsrl(msrs->
controls[i].addr, val);
422 static void op_amd_stop(
struct op_msrs const *
const msrs)
431 for (i = 0; i < num_counters; ++
i) {
434 rdmsrl(msrs->
controls[i].addr, val);
436 wrmsrl(msrs->
controls[i].addr, val);
447 static void init_ibs(
void)
465 if (create_arch_files)
466 ret = create_arch_files(sb, root);
534 .fill_in_addresses = &op_amd_fill_in_addresses,
535 .setup_ctrs = &op_amd_setup_ctrs,
536 .check_ctrs = &op_amd_check_ctrs,
537 .start = &op_amd_start,
538 .stop = &op_amd_stop,
539 .shutdown = &op_amd_shutdown,
540 #ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
541 .switch_ctrl = &op_mux_switch_ctrl,