blob: c3ee43333f26a28ae714d4160d7d9755be69791a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * @file op_model_athlon.h
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +01003 * athlon / K7 / K8 / Family 10h model-specific MSR operations
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author John Levon
9 * @author Philippe Elie
10 * @author Graydon Hoare
11 */
12
13#include <linux/oprofile.h>
14#include <asm/ptrace.h>
15#include <asm/msr.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020016#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include "op_x86_model.h"
19#include "op_counter.h"
20
21#define NUM_COUNTERS 4
22#define NUM_CONTROLS 4
23
Don Zickuscb9c4482006-09-26 10:52:26 +020024#define CTR_IS_RESERVED(msrs,c) (msrs->counters[(c)].addr ? 1 : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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)))
28
Don Zickuscb9c4482006-09-26 10:52:26 +020029#define CTRL_IS_RESERVED(msrs,c) (msrs->controls[(c)].addr ? 1 : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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))
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010034#define CTRL_CLEAR_LO(x) (x &= (1<<21))
35#define CTRL_CLEAR_HI(x) (x &= 0xfffffcf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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))
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010040#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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45static unsigned long reset_value[NUM_COUNTERS];
46
47static void athlon_fill_in_addresses(struct op_msrs * const msrs)
48{
Don Zickuscb9c4482006-09-26 10:52:26 +020049 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Don Zickuscb9c4482006-09-26 10:52:26 +020051 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;
54 else
55 msrs->counters[i].addr = 0;
56 }
57
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;
61 else
62 msrs->controls[i].addr = 0;
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66
67static void athlon_setup_ctrs(struct op_msrs const * const msrs)
68{
69 unsigned int low, high;
70 int i;
71
72 /* clear all counters */
73 for (i = 0 ; i < NUM_CONTROLS; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +020074 if (unlikely(!CTRL_IS_RESERVED(msrs,i)))
75 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010077 CTRL_CLEAR_LO(low);
78 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 CTRL_WRITE(low, high, msrs, i);
80 }
Don Zickuscb9c4482006-09-26 10:52:26 +020081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /* avoid a false detection of ctr overflows in NMI handler */
83 for (i = 0; i < NUM_COUNTERS; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +020084 if (unlikely(!CTR_IS_RESERVED(msrs,i)))
85 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 CTR_WRITE(1, msrs, i);
87 }
88
89 /* enable active counters */
90 for (i = 0; i < NUM_COUNTERS; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +020091 if ((counter_config[i].enabled) && (CTR_IS_RESERVED(msrs,i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 reset_value[i] = counter_config[i].count;
93
94 CTR_WRITE(counter_config[i].count, msrs, i);
95
96 CTRL_READ(low, high, msrs, i);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +010097 CTRL_CLEAR_LO(low);
98 CTRL_CLEAR_HI(high);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 CTRL_SET_ENABLE(low);
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);
Barry Kasindorfbd87f1f2007-12-18 18:05:58 +0100103 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);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 CTRL_WRITE(low, high, msrs, i);
109 } else {
110 reset_value[i] = 0;
111 }
112 }
113}
114
115
116static int athlon_check_ctrs(struct pt_regs * const regs,
117 struct op_msrs const * const msrs)
118{
119 unsigned int low, high;
120 int i;
121
122 for (i = 0 ; i < NUM_COUNTERS; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200123 if (!reset_value[i])
124 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 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);
129 }
130 }
131
132 /* See op_model_ppro.c */
133 return 1;
134}
135
136
137static void athlon_start(struct op_msrs const * const msrs)
138{
139 unsigned int low, high;
140 int i;
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);
146 }
147 }
148}
149
150
151static void athlon_stop(struct op_msrs const * const msrs)
152{
153 unsigned int low,high;
154 int i;
155
156 /* Subtle: stop on all counters to avoid race with
157 * setting our pm callback */
158 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
Don Zickuscb9c4482006-09-26 10:52:26 +0200159 if (!reset_value[i])
160 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 CTRL_READ(low, high, msrs, i);
162 CTRL_SET_INACTIVE(low);
163 CTRL_WRITE(low, high, msrs, i);
164 }
165}
166
Don Zickuscb9c4482006-09-26 10:52:26 +0200167static void athlon_shutdown(struct op_msrs const * const msrs)
168{
169 int i;
170
171 for (i = 0 ; i < NUM_COUNTERS ; ++i) {
172 if (CTR_IS_RESERVED(msrs,i))
173 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
174 }
175 for (i = 0 ; i < NUM_CONTROLS ; ++i) {
176 if (CTRL_IS_RESERVED(msrs,i))
177 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
178 }
179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181struct 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,
Don Zickuscb9c4482006-09-26 10:52:26 +0200188 .stop = &athlon_stop,
189 .shutdown = &athlon_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190};