Merge branch 'x86-kbuild-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / arch / x86 / oprofile / op_model_amd.c
index 6493ef7..39686c2 100644 (file)
@@ -9,12 +9,15 @@
  * @author Philippe Elie
  * @author Graydon Hoare
  * @author Robert Richter <robert.richter@amd.com>
- * @author Barry Kasindorf
+ * @author Barry Kasindorf <barry.kasindorf@amd.com>
+ * @author Jason Yeh <jason.yeh@amd.com>
+ * @author Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
  */
 
 #include <linux/oprofile.h>
 #include <linux/device.h>
 #include <linux/pci.h>
+#include <linux/percpu.h>
 
 #include <asm/ptrace.h>
 #include <asm/msr.h>
 
 #define NUM_COUNTERS 4
 #define NUM_CONTROLS 4
+#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
+#define NUM_VIRT_COUNTERS 32
+#define NUM_VIRT_CONTROLS 32
+#else
+#define NUM_VIRT_COUNTERS NUM_COUNTERS
+#define NUM_VIRT_CONTROLS NUM_CONTROLS
+#endif
+
 #define OP_EVENT_MASK                  0x0FFF
 #define OP_CTR_OVERFLOW                        (1ULL<<31)
 
 #define MSR_AMD_EVENTSEL_RESERVED      ((0xFFFFFCF0ULL<<32)|(1ULL<<21))
 
-static unsigned long reset_value[NUM_COUNTERS];
+static unsigned long reset_value[NUM_VIRT_COUNTERS];
 
 #ifdef CONFIG_OPROFILE_IBS
 
@@ -63,6 +74,45 @@ static struct op_ibs_config ibs_config;
 
 #endif
 
+#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
+
+static void op_mux_fill_in_addresses(struct op_msrs * const msrs)
+{
+       int i;
+
+       for (i = 0; i < NUM_VIRT_COUNTERS; i++) {
+               int hw_counter = op_x86_virt_to_phys(i);
+               if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
+                       msrs->multiplex[i].addr = MSR_K7_PERFCTR0 + hw_counter;
+               else
+                       msrs->multiplex[i].addr = 0;
+       }
+}
+
+static void op_mux_switch_ctrl(struct op_x86_model_spec const *model,
+                              struct op_msrs const * const msrs)
+{
+       u64 val;
+       int i;
+
+       /* enable active counters */
+       for (i = 0; i < NUM_COUNTERS; ++i) {
+               int virt = op_x86_phys_to_virt(i);
+               if (!counter_config[virt].enabled)
+                       continue;
+               rdmsrl(msrs->controls[i].addr, val);
+               val &= model->reserved;
+               val |= op_x86_get_ctrl(model, &counter_config[virt]);
+               wrmsrl(msrs->controls[i].addr, val);
+       }
+}
+
+#else
+
+static inline void op_mux_fill_in_addresses(struct op_msrs * const msrs) { }
+
+#endif
+
 /* functions for op_amd_spec */
 
 static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
@@ -82,6 +132,8 @@ static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
                else
                        msrs->controls[i].addr = 0;
        }
+
+       op_mux_fill_in_addresses(msrs);
 }
 
 static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
@@ -90,8 +142,16 @@ static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
        u64 val;
        int i;
 
+       /* setup reset_value */
+       for (i = 0; i < NUM_VIRT_COUNTERS; ++i) {
+               if (counter_config[i].enabled)
+                       reset_value[i] = counter_config[i].count;
+               else
+                       reset_value[i] = 0;
+       }
+
        /* clear all counters */
-       for (i = 0 ; i < NUM_CONTROLS; ++i) {
+       for (i = 0; i < NUM_CONTROLS; ++i) {
                if (unlikely(!msrs->controls[i].addr))
                        continue;
                rdmsrl(msrs->controls[i].addr, val);
@@ -108,23 +168,26 @@ static void op_amd_setup_ctrs(struct op_x86_model_spec const *model,
 
        /* enable active counters */
        for (i = 0; i < NUM_COUNTERS; ++i) {
-               if (counter_config[i].enabled && msrs->counters[i].addr) {
-                       reset_value[i] = counter_config[i].count;
-                       wrmsrl(msrs->counters[i].addr,
-                              -(s64)counter_config[i].count);
-                       rdmsrl(msrs->controls[i].addr, val);
-                       val &= model->reserved;
-                       val |= op_x86_get_ctrl(model, &counter_config[i]);
-                       wrmsrl(msrs->controls[i].addr, val);
-               } else {
-                       reset_value[i] = 0;
-               }
+               int virt = op_x86_phys_to_virt(i);
+               if (!counter_config[virt].enabled)
+                       continue;
+               if (!msrs->counters[i].addr)
+                       continue;
+
+               /* setup counter registers */
+               wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
+
+               /* setup control registers */
+               rdmsrl(msrs->controls[i].addr, val);
+               val &= model->reserved;
+               val |= op_x86_get_ctrl(model, &counter_config[virt]);
+               wrmsrl(msrs->controls[i].addr, val);
        }
 }
 
 #ifdef CONFIG_OPROFILE_IBS
 
-static inline int
+static inline void
 op_amd_handle_ibs(struct pt_regs * const regs,
                  struct op_msrs const * const msrs)
 {
@@ -132,7 +195,7 @@ op_amd_handle_ibs(struct pt_regs * const regs,
        struct op_entry entry;
 
        if (!has_ibs)
-               return 1;
+               return;
 
        if (ibs_config.fetch_enabled) {
                rdmsrl(MSR_AMD64_IBSFETCHCTL, ctl);
@@ -140,13 +203,10 @@ op_amd_handle_ibs(struct pt_regs * const regs,
                        rdmsrl(MSR_AMD64_IBSFETCHLINAD, val);
                        oprofile_write_reserve(&entry, regs, val,
                                               IBS_FETCH_CODE, IBS_FETCH_SIZE);
-                       oprofile_add_data(&entry, (u32)val);
-                       oprofile_add_data(&entry, (u32)(val >> 32));
-                       oprofile_add_data(&entry, (u32)ctl);
-                       oprofile_add_data(&entry, (u32)(ctl >> 32));
+                       oprofile_add_data64(&entry, val);
+                       oprofile_add_data64(&entry, ctl);
                        rdmsrl(MSR_AMD64_IBSFETCHPHYSAD, val);
-                       oprofile_add_data(&entry, (u32)val);
-                       oprofile_add_data(&entry, (u32)(val >> 32));
+                       oprofile_add_data64(&entry, val);
                        oprofile_write_commit(&entry);
 
                        /* reenable the IRQ */
@@ -162,23 +222,17 @@ op_amd_handle_ibs(struct pt_regs * const regs,
                        rdmsrl(MSR_AMD64_IBSOPRIP, val);
                        oprofile_write_reserve(&entry, regs, val,
                                               IBS_OP_CODE, IBS_OP_SIZE);
-                       oprofile_add_data(&entry, (u32)val);
-                       oprofile_add_data(&entry, (u32)(val >> 32));
+                       oprofile_add_data64(&entry, val);
                        rdmsrl(MSR_AMD64_IBSOPDATA, val);
-                       oprofile_add_data(&entry, (u32)val);
-                       oprofile_add_data(&entry, (u32)(val >> 32));
+                       oprofile_add_data64(&entry, val);
                        rdmsrl(MSR_AMD64_IBSOPDATA2, val);
-                       oprofile_add_data(&entry, (u32)val);
-                       oprofile_add_data(&entry, (u32)(val >> 32));
+                       oprofile_add_data64(&entry, val);
                        rdmsrl(MSR_AMD64_IBSOPDATA3, val);
-                       oprofile_add_data(&entry, (u32)val);
-                       oprofile_add_data(&entry, (u32)(val >> 32));
+                       oprofile_add_data64(&entry, val);
                        rdmsrl(MSR_AMD64_IBSDCLINAD, val);
-                       oprofile_add_data(&entry, (u32)val);
-                       oprofile_add_data(&entry, (u32)(val >> 32));
+                       oprofile_add_data64(&entry, val);
                        rdmsrl(MSR_AMD64_IBSDCPHYSAD, val);
-                       oprofile_add_data(&entry, (u32)val);
-                       oprofile_add_data(&entry, (u32)(val >> 32));
+                       oprofile_add_data64(&entry, val);
                        oprofile_write_commit(&entry);
 
                        /* reenable the IRQ */
@@ -187,8 +241,6 @@ op_amd_handle_ibs(struct pt_regs * const regs,
                        wrmsrl(MSR_AMD64_IBSOPCTL, ctl);
                }
        }
-
-       return 1;
 }
 
 static inline void op_amd_start_ibs(void)
@@ -222,7 +274,7 @@ static void op_amd_stop_ibs(void)
 
 #else
 
-static inline int op_amd_handle_ibs(struct pt_regs * const regs,
+static inline void op_amd_handle_ibs(struct pt_regs * const regs,
                                    struct op_msrs const * const msrs) { }
 static inline void op_amd_start_ibs(void) { }
 static inline void op_amd_stop_ibs(void) { }
@@ -235,15 +287,16 @@ static int op_amd_check_ctrs(struct pt_regs * const regs,
        u64 val;
        int i;
 
-       for (i = 0 ; i < NUM_COUNTERS; ++i) {
-               if (!reset_value[i])
+       for (i = 0; i < NUM_COUNTERS; ++i) {
+               int virt = op_x86_phys_to_virt(i);
+               if (!reset_value[virt])
                        continue;
                rdmsrl(msrs->counters[i].addr, val);
                /* bit is clear if overflowed: */
                if (val & OP_CTR_OVERFLOW)
                        continue;
-               oprofile_add_sample(regs, i);
-               wrmsrl(msrs->counters[i].addr, -(s64)reset_value[i]);
+               oprofile_add_sample(regs, virt);
+               wrmsrl(msrs->counters[i].addr, -(u64)reset_value[virt]);
        }
 
        op_amd_handle_ibs(regs, msrs);
@@ -256,12 +309,13 @@ static void op_amd_start(struct op_msrs const * const msrs)
 {
        u64 val;
        int i;
-       for (i = 0 ; i < NUM_COUNTERS ; ++i) {
-               if (reset_value[i]) {
-                       rdmsrl(msrs->controls[i].addr, val);
-                       val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
-                       wrmsrl(msrs->controls[i].addr, val);
-               }
+
+       for (i = 0; i < NUM_COUNTERS; ++i) {
+               if (!reset_value[op_x86_phys_to_virt(i)])
+                       continue;
+               rdmsrl(msrs->controls[i].addr, val);
+               val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
+               wrmsrl(msrs->controls[i].addr, val);
        }
 
        op_amd_start_ibs();
@@ -276,8 +330,8 @@ static void op_amd_stop(struct op_msrs const * const msrs)
         * Subtle: stop on all counters to avoid race with setting our
         * pm callback
         */
-       for (i = 0 ; i < NUM_COUNTERS ; ++i) {
-               if (!reset_value[i])
+       for (i = 0; i < NUM_COUNTERS; ++i) {
+               if (!reset_value[op_x86_phys_to_virt(i)])
                        continue;
                rdmsrl(msrs->controls[i].addr, val);
                val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
@@ -291,11 +345,11 @@ static void op_amd_shutdown(struct op_msrs const * const msrs)
 {
        int i;
 
-       for (i = 0 ; i < NUM_COUNTERS ; ++i) {
+       for (i = 0; i < NUM_COUNTERS; ++i) {
                if (msrs->counters[i].addr)
                        release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
        }
-       for (i = 0 ; i < NUM_CONTROLS ; ++i) {
+       for (i = 0; i < NUM_CONTROLS; ++i) {
                if (msrs->controls[i].addr)
                        release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
        }
@@ -466,9 +520,10 @@ static void op_amd_exit(void) {}
 
 #endif /* CONFIG_OPROFILE_IBS */
 
-struct op_x86_model_spec const op_amd_spec = {
+struct op_x86_model_spec op_amd_spec = {
        .num_counters           = NUM_COUNTERS,
        .num_controls           = NUM_CONTROLS,
+       .num_virt_counters      = NUM_VIRT_COUNTERS,
        .reserved               = MSR_AMD_EVENTSEL_RESERVED,
        .event_mask             = OP_EVENT_MASK,
        .init                   = op_amd_init,
@@ -479,4 +534,7 @@ struct op_x86_model_spec const op_amd_spec = {
        .start                  = &op_amd_start,
        .stop                   = &op_amd_stop,
        .shutdown               = &op_amd_shutdown,
+#ifdef CONFIG_OPROFILE_EVENT_MULTIPLEX
+       .switch_ctrl            = &op_mux_switch_ctrl,
+#endif
 };