2 * @file op_model_athlon.h
3 * athlon / K7 / K8 / Family 10h model-specific MSR operations
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
9 * @author Philippe Elie
10 * @author Graydon Hoare
13 #include <linux/oprofile.h>
14 #include <asm/ptrace.h>
18 #include "op_x86_model.h"
19 #include "op_counter.h"
21 #define NUM_COUNTERS 4
22 #define NUM_CONTROLS 4
24 #define CTR_IS_RESERVED(msrs, c) (msrs->counters[(c)].addr ? 1 : 0)
25 #define CTR_READ(l, h, msrs, c) do {rdmsr(msrs->counters[(c)].addr, (l), (h)); } while (0)
26 #define CTR_WRITE(l, msrs, c) do {wrmsr(msrs->counters[(c)].addr, -(unsigned int)(l), -1); } while (0)
27 #define CTR_OVERFLOWED(n) (!((n) & (1U<<31)))
29 #define CTRL_IS_RESERVED(msrs, c) (msrs->controls[(c)].addr ? 1 : 0)
30 #define CTRL_READ(l, h, msrs, c) do {rdmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
31 #define CTRL_WRITE(l, h, msrs, c) do {wrmsr(msrs->controls[(c)].addr, (l), (h)); } while (0)
32 #define CTRL_SET_ACTIVE(n) (n |= (1<<22))
33 #define CTRL_SET_INACTIVE(n) (n &= ~(1<<22))
34 #define CTRL_CLEAR_LO(x) (x &= (1<<21))
35 #define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
36 #define CTRL_SET_ENABLE(val) (val |= 1<<20)
37 #define CTRL_SET_USR(val, u) (val |= ((u & 1) << 16))
38 #define CTRL_SET_KERN(val, k) (val |= ((k & 1) << 17))
39 #define CTRL_SET_UM(val, m) (val |= (m << 8))
40 #define CTRL_SET_EVENT_LOW(val, e) (val |= (e & 0xff))
41 #define CTRL_SET_EVENT_HIGH(val, e) (val |= ((e >> 8) & 0xf))
42 #define CTRL_SET_HOST_ONLY(val, h) (val |= ((h & 1) << 9))
43 #define CTRL_SET_GUEST_ONLY(val, h) (val |= ((h & 1) << 8))
45 static unsigned long reset_value
[NUM_COUNTERS
];
47 static void athlon_fill_in_addresses(struct op_msrs
* const msrs
)
51 for (i
= 0; i
< NUM_COUNTERS
; i
++) {
52 if (reserve_perfctr_nmi(MSR_K7_PERFCTR0
+ i
))
53 msrs
->counters
[i
].addr
= MSR_K7_PERFCTR0
+ i
;
55 msrs
->counters
[i
].addr
= 0;
58 for (i
= 0; i
< NUM_CONTROLS
; i
++) {
59 if (reserve_evntsel_nmi(MSR_K7_EVNTSEL0
+ i
))
60 msrs
->controls
[i
].addr
= MSR_K7_EVNTSEL0
+ i
;
62 msrs
->controls
[i
].addr
= 0;
67 static void athlon_setup_ctrs(struct op_msrs
const * const msrs
)
69 unsigned int low
, high
;
72 /* clear all counters */
73 for (i
= 0 ; i
< NUM_CONTROLS
; ++i
) {
74 if (unlikely(!CTRL_IS_RESERVED(msrs
, i
)))
76 CTRL_READ(low
, high
, msrs
, i
);
79 CTRL_WRITE(low
, high
, msrs
, i
);
82 /* avoid a false detection of ctr overflows in NMI handler */
83 for (i
= 0; i
< NUM_COUNTERS
; ++i
) {
84 if (unlikely(!CTR_IS_RESERVED(msrs
, i
)))
86 CTR_WRITE(1, msrs
, i
);
89 /* enable active counters */
90 for (i
= 0; i
< NUM_COUNTERS
; ++i
) {
91 if ((counter_config
[i
].enabled
) && (CTR_IS_RESERVED(msrs
, i
))) {
92 reset_value
[i
] = counter_config
[i
].count
;
94 CTR_WRITE(counter_config
[i
].count
, msrs
, i
);
96 CTRL_READ(low
, high
, msrs
, i
);
100 CTRL_SET_USR(low
, counter_config
[i
].user
);
101 CTRL_SET_KERN(low
, counter_config
[i
].kernel
);
102 CTRL_SET_UM(low
, counter_config
[i
].unit_mask
);
103 CTRL_SET_EVENT_LOW(low
, counter_config
[i
].event
);
104 CTRL_SET_EVENT_HIGH(high
, counter_config
[i
].event
);
105 CTRL_SET_HOST_ONLY(high
, 0);
106 CTRL_SET_GUEST_ONLY(high
, 0);
108 CTRL_WRITE(low
, high
, msrs
, i
);
116 static int athlon_check_ctrs(struct pt_regs
* const regs
,
117 struct op_msrs
const * const msrs
)
119 unsigned int low
, high
;
122 for (i
= 0 ; i
< NUM_COUNTERS
; ++i
) {
125 CTR_READ(low
, high
, msrs
, i
);
126 if (CTR_OVERFLOWED(low
)) {
127 oprofile_add_sample(regs
, i
);
128 CTR_WRITE(reset_value
[i
], msrs
, i
);
132 /* See op_model_ppro.c */
137 static void athlon_start(struct op_msrs
const * const msrs
)
139 unsigned int low
, high
;
141 for (i
= 0 ; i
< NUM_COUNTERS
; ++i
) {
142 if (reset_value
[i
]) {
143 CTRL_READ(low
, high
, msrs
, i
);
144 CTRL_SET_ACTIVE(low
);
145 CTRL_WRITE(low
, high
, msrs
, i
);
151 static void athlon_stop(struct op_msrs
const * const msrs
)
153 unsigned int low
, high
;
156 /* Subtle: stop on all counters to avoid race with
157 * setting our pm callback */
158 for (i
= 0 ; i
< NUM_COUNTERS
; ++i
) {
161 CTRL_READ(low
, high
, msrs
, i
);
162 CTRL_SET_INACTIVE(low
);
163 CTRL_WRITE(low
, high
, msrs
, i
);
167 static void athlon_shutdown(struct op_msrs
const * const msrs
)
171 for (i
= 0 ; i
< NUM_COUNTERS
; ++i
) {
172 if (CTR_IS_RESERVED(msrs
, i
))
173 release_perfctr_nmi(MSR_K7_PERFCTR0
+ i
);
175 for (i
= 0 ; i
< NUM_CONTROLS
; ++i
) {
176 if (CTRL_IS_RESERVED(msrs
, i
))
177 release_evntsel_nmi(MSR_K7_EVNTSEL0
+ i
);
181 struct op_x86_model_spec
const op_athlon_spec
= {
182 .num_counters
= NUM_COUNTERS
,
183 .num_controls
= NUM_CONTROLS
,
184 .fill_in_addresses
= &athlon_fill_in_addresses
,
185 .setup_ctrs
= &athlon_setup_ctrs
,
186 .check_ctrs
= &athlon_check_ctrs
,
187 .start
= &athlon_start
,
188 .stop
= &athlon_stop
,
189 .shutdown
= &athlon_shutdown