2 * @file op_model_athlon.c
3 * athlon / K7 / K8 / Family 10h model-specific MSR operations
5 * @remark Copyright 2002-2008 OProfile authors
6 * @remark Read the file COPYING
9 * @author Philippe Elie
10 * @author Graydon Hoare
11 * @author Robert Richter <robert.richter@amd.com>
14 #include <linux/oprofile.h>
15 #include <asm/ptrace.h>
19 #include "op_x86_model.h"
20 #include "op_counter.h"
22 #define NUM_COUNTERS 4
23 #define NUM_CONTROLS 4
25 #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
26 #define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
27 #define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0)
28 #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
30 #define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
31 #define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
32 #define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
33 #define CTRL_SET_ACTIVE(n) (n |= (1<<22))
34 #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
35 #define CTRL_CLEAR_LO(x) (x &= (1<<21))
36 #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
37 #define CTRL_SET_ENABLE(val) (val |= 1<<20)
38 #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
39 #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
40 #define CTRL_SET_UM(val, m) (val |= (m << 8))
41 #define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
42 #define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
43 #define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
44 #define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
46 static unsigned long reset_value[NUM_COUNTERS];
48 /* functions for op_amd_spec */
50 static void op_amd_fill_in_addresses(struct op_msrs * const msrs)
54 for (i = 0; i < NUM_COUNTERS; i++) {
55 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i))
56 msrs->counters[i].addr = MSR_K7_PERFCTR0 + i;
58 msrs->counters[i].addr = 0;
61 for (i = 0; i < NUM_CONTROLS; i++) {
62 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i))
63 msrs->controls[i].addr = MSR_K7_EVNTSEL0 + i;
65 msrs->controls[i].addr = 0;
70 static void op_amd_setup_ctrs(struct op_msrs const * const msrs)
72 unsigned int low, high;
75 /* clear all counters */
76 for (i = 0 ; i < NUM_CONTROLS; ++i) {
77 if (unlikely(!CTRL_IS_RESERVED(msrs, i)))
79 CTRL_READ(low, high, msrs, i);
82 CTRL_WRITE(low, high, msrs, i);
85 /* avoid a false detection of ctr overflows in NMI handler */
86 for (i = 0; i < NUM_COUNTERS; ++i) {
87 if (unlikely(!CTR_IS_RESERVED(msrs, i)))
89 CTR_WRITE(1, msrs, i);
92 /* enable active counters */
93 for (i = 0; i < NUM_COUNTERS; ++i) {
94 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs, i))) {
95 reset_value[i] = counter_config[i].count;
97 CTR_WRITE(counter_config[i].count, msrs, i);
99 CTRL_READ(low, high, msrs, i);
102 CTRL_SET_ENABLE(low);
103 CTRL_SET_USR(low, counter_config[i].user);
104 CTRL_SET_KERN(low, counter_config[i].kernel);
105 CTRL_SET_UM(low, counter_config[i].unit_mask);
106 CTRL_SET_EVENT_LOW(low, counter_config[i].event);
107 CTRL_SET_EVENT_HIGH(high, counter_config[i].event);
108 CTRL_SET_HOST_ONLY(high, 0);
109 CTRL_SET_GUEST_ONLY(high, 0);
111 CTRL_WRITE(low, high, msrs, i);
119 static int op_amd_check_ctrs(struct pt_regs * const regs,
120 struct op_msrs const * const msrs)
122 unsigned int low, high;
125 for (i = 0 ; i < NUM_COUNTERS; ++i) {
128 CTR_READ(low, high, msrs, i);
129 if (CTR_OVERFLOWED(low)) {
130 oprofile_add_sample(regs, i);
131 CTR_WRITE(reset_value[i], msrs, i);
135 /* See op_model_ppro.c */
140 static void op_amd_start(struct op_msrs const * const msrs)
142 unsigned int low, high;
144 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
145 if (reset_value[i]) {
146 CTRL_READ(low, high, msrs, i);
147 CTRL_SET_ACTIVE(low);
148 CTRL_WRITE(low, high, msrs, i);
154 static void op_amd_stop(struct op_msrs const * const msrs)
156 unsigned int low, high;
159 /* Subtle: stop on all counters to avoid race with
160 * setting our pm callback */
161 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
164 CTRL_READ(low, high, msrs, i);
165 CTRL_SET_INACTIVE(low);
166 CTRL_WRITE(low, high, msrs, i);
170 static void op_amd_shutdown(struct op_msrs const * const msrs)
174 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
175 if (CTR_IS_RESERVED(msrs, i))
176 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
178 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
179 if (CTRL_IS_RESERVED(msrs, i))
180 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
184 static int op_amd_init(struct oprofile_operations *ops)
189 static void op_amd_exit(void)
193 struct op_x86_model_spec const op_amd_spec = {
196 .num_counters = NUM_COUNTERS,
197 .num_controls = NUM_CONTROLS,
198 .fill_in_addresses = &op_amd_fill_in_addresses,
199 .setup_ctrs = &op_amd_setup_ctrs,
200 .check_ctrs = &op_amd_check_ctrs,
201 .start = &op_amd_start,
202 .stop = &op_amd_stop,
203 .shutdown = &op_amd_shutdown