]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86/kernel/cpu/perf_event_p4.c
Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/davej/cpufreq
[net-next-2.6.git] / arch / x86 / kernel / cpu / perf_event_p4.c
CommitLineData
a072738e
CG
1/*
2 * Netburst Perfomance Events (P4, old Xeon)
3 *
4 * Copyright (C) 2010 Parallels, Inc., Cyrill Gorcunov <gorcunov@openvz.org>
5 * Copyright (C) 2010 Intel Corporation, Lin Ming <ming.m.lin@intel.com>
6 *
7 * For licencing details see kernel-base/COPYING
8 */
9
10#ifdef CONFIG_CPU_SUP_INTEL
11
12#include <asm/perf_event_p4.h>
13
d814f301 14#define P4_CNTR_LIMIT 3
a072738e
CG
15/*
16 * array indices: 0,1 - HT threads, used with HT enabled cpu
17 */
d814f301
CG
18struct p4_event_bind {
19 unsigned int opcode; /* Event code and ESCR selector */
20 unsigned int escr_msr[2]; /* ESCR MSR for this event */
1ff3d7d7 21 char cntr[2][P4_CNTR_LIMIT]; /* counter index (offset), -1 on abscence */
a072738e
CG
22};
23
39ef13a4 24struct p4_pebs_bind {
d814f301
CG
25 unsigned int metric_pebs;
26 unsigned int metric_vert;
a072738e
CG
27};
28
39ef13a4
CG
29/* it sets P4_PEBS_ENABLE_UOP_TAG as well */
30#define P4_GEN_PEBS_BIND(name, pebs, vert) \
31 [P4_PEBS_METRIC__##name] = { \
32 .metric_pebs = pebs | P4_PEBS_ENABLE_UOP_TAG, \
33 .metric_vert = vert, \
d814f301
CG
34 }
35
39ef13a4
CG
36/*
37 * note we have P4_PEBS_ENABLE_UOP_TAG always set here
38 *
39 * it's needed for mapping P4_PEBS_CONFIG_METRIC_MASK bits of
40 * event configuration to find out which values are to be
41 * written into MSR_IA32_PEBS_ENABLE and MSR_P4_PEBS_MATRIX_VERT
42 * resgisters
43 */
44static struct p4_pebs_bind p4_pebs_bind_map[] = {
45 P4_GEN_PEBS_BIND(1stl_cache_load_miss_retired, 0x0000001, 0x0000001),
46 P4_GEN_PEBS_BIND(2ndl_cache_load_miss_retired, 0x0000002, 0x0000001),
47 P4_GEN_PEBS_BIND(dtlb_load_miss_retired, 0x0000004, 0x0000001),
48 P4_GEN_PEBS_BIND(dtlb_store_miss_retired, 0x0000004, 0x0000002),
49 P4_GEN_PEBS_BIND(dtlb_all_miss_retired, 0x0000004, 0x0000003),
50 P4_GEN_PEBS_BIND(tagged_mispred_branch, 0x0018000, 0x0000010),
51 P4_GEN_PEBS_BIND(mob_load_replay_retired, 0x0000200, 0x0000001),
52 P4_GEN_PEBS_BIND(split_load_retired, 0x0000400, 0x0000001),
53 P4_GEN_PEBS_BIND(split_store_retired, 0x0000400, 0x0000002),
d814f301
CG
54};
55
56/*
57 * Note that we don't use CCCR1 here, there is an
58 * exception for P4_BSQ_ALLOCATION but we just have
59 * no workaround
60 *
61 * consider this binding as resources which particular
62 * event may borrow, it doesn't contain EventMask,
63 * Tags and friends -- they are left to a caller
64 */
65static struct p4_event_bind p4_event_bind_map[] = {
66 [P4_EVENT_TC_DELIVER_MODE] = {
67 .opcode = P4_OPCODE(P4_EVENT_TC_DELIVER_MODE),
68 .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 },
69 .cntr = { {4, 5, -1}, {6, 7, -1} },
70 },
71 [P4_EVENT_BPU_FETCH_REQUEST] = {
72 .opcode = P4_OPCODE(P4_EVENT_BPU_FETCH_REQUEST),
73 .escr_msr = { MSR_P4_BPU_ESCR0, MSR_P4_BPU_ESCR1 },
74 .cntr = { {0, -1, -1}, {2, -1, -1} },
75 },
76 [P4_EVENT_ITLB_REFERENCE] = {
77 .opcode = P4_OPCODE(P4_EVENT_ITLB_REFERENCE),
78 .escr_msr = { MSR_P4_ITLB_ESCR0, MSR_P4_ITLB_ESCR1 },
79 .cntr = { {0, -1, -1}, {2, -1, -1} },
80 },
81 [P4_EVENT_MEMORY_CANCEL] = {
82 .opcode = P4_OPCODE(P4_EVENT_MEMORY_CANCEL),
83 .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 },
84 .cntr = { {8, 9, -1}, {10, 11, -1} },
85 },
86 [P4_EVENT_MEMORY_COMPLETE] = {
87 .opcode = P4_OPCODE(P4_EVENT_MEMORY_COMPLETE),
88 .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 },
89 .cntr = { {8, 9, -1}, {10, 11, -1} },
90 },
91 [P4_EVENT_LOAD_PORT_REPLAY] = {
92 .opcode = P4_OPCODE(P4_EVENT_LOAD_PORT_REPLAY),
93 .escr_msr = { MSR_P4_SAAT_ESCR0, MSR_P4_SAAT_ESCR1 },
94 .cntr = { {8, 9, -1}, {10, 11, -1} },
95 },
96 [P4_EVENT_STORE_PORT_REPLAY] = {
97 .opcode = P4_OPCODE(P4_EVENT_STORE_PORT_REPLAY),
98 .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 },
99 .cntr = { {8, 9, -1}, {10, 11, -1} },
100 },
101 [P4_EVENT_MOB_LOAD_REPLAY] = {
102 .opcode = P4_OPCODE(P4_EVENT_MOB_LOAD_REPLAY),
103 .escr_msr = { MSR_P4_MOB_ESCR0, MSR_P4_MOB_ESCR1 },
104 .cntr = { {0, -1, -1}, {2, -1, -1} },
105 },
106 [P4_EVENT_PAGE_WALK_TYPE] = {
107 .opcode = P4_OPCODE(P4_EVENT_PAGE_WALK_TYPE),
108 .escr_msr = { MSR_P4_PMH_ESCR0, MSR_P4_PMH_ESCR1 },
109 .cntr = { {0, -1, -1}, {2, -1, -1} },
110 },
111 [P4_EVENT_BSQ_CACHE_REFERENCE] = {
112 .opcode = P4_OPCODE(P4_EVENT_BSQ_CACHE_REFERENCE),
113 .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
114 .cntr = { {0, -1, -1}, {2, -1, -1} },
115 },
116 [P4_EVENT_IOQ_ALLOCATION] = {
117 .opcode = P4_OPCODE(P4_EVENT_IOQ_ALLOCATION),
118 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
119 .cntr = { {0, -1, -1}, {2, -1, -1} },
120 },
121 [P4_EVENT_IOQ_ACTIVE_ENTRIES] = { /* shared ESCR */
122 .opcode = P4_OPCODE(P4_EVENT_IOQ_ACTIVE_ENTRIES),
123 .escr_msr = { MSR_P4_FSB_ESCR1, MSR_P4_FSB_ESCR1 },
124 .cntr = { {2, -1, -1}, {3, -1, -1} },
125 },
126 [P4_EVENT_FSB_DATA_ACTIVITY] = {
127 .opcode = P4_OPCODE(P4_EVENT_FSB_DATA_ACTIVITY),
128 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
129 .cntr = { {0, -1, -1}, {2, -1, -1} },
130 },
131 [P4_EVENT_BSQ_ALLOCATION] = { /* shared ESCR, broken CCCR1 */
132 .opcode = P4_OPCODE(P4_EVENT_BSQ_ALLOCATION),
133 .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR0 },
134 .cntr = { {0, -1, -1}, {1, -1, -1} },
135 },
136 [P4_EVENT_BSQ_ACTIVE_ENTRIES] = { /* shared ESCR */
137 .opcode = P4_OPCODE(P4_EVENT_BSQ_ACTIVE_ENTRIES),
138 .escr_msr = { MSR_P4_BSU_ESCR1 , MSR_P4_BSU_ESCR1 },
139 .cntr = { {2, -1, -1}, {3, -1, -1} },
140 },
141 [P4_EVENT_SSE_INPUT_ASSIST] = {
142 .opcode = P4_OPCODE(P4_EVENT_SSE_INPUT_ASSIST),
143 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
144 .cntr = { {8, 9, -1}, {10, 11, -1} },
145 },
146 [P4_EVENT_PACKED_SP_UOP] = {
147 .opcode = P4_OPCODE(P4_EVENT_PACKED_SP_UOP),
148 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
149 .cntr = { {8, 9, -1}, {10, 11, -1} },
150 },
151 [P4_EVENT_PACKED_DP_UOP] = {
152 .opcode = P4_OPCODE(P4_EVENT_PACKED_DP_UOP),
153 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
154 .cntr = { {8, 9, -1}, {10, 11, -1} },
155 },
156 [P4_EVENT_SCALAR_SP_UOP] = {
157 .opcode = P4_OPCODE(P4_EVENT_SCALAR_SP_UOP),
158 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
159 .cntr = { {8, 9, -1}, {10, 11, -1} },
160 },
161 [P4_EVENT_SCALAR_DP_UOP] = {
162 .opcode = P4_OPCODE(P4_EVENT_SCALAR_DP_UOP),
163 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
164 .cntr = { {8, 9, -1}, {10, 11, -1} },
165 },
166 [P4_EVENT_64BIT_MMX_UOP] = {
167 .opcode = P4_OPCODE(P4_EVENT_64BIT_MMX_UOP),
168 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
169 .cntr = { {8, 9, -1}, {10, 11, -1} },
170 },
171 [P4_EVENT_128BIT_MMX_UOP] = {
172 .opcode = P4_OPCODE(P4_EVENT_128BIT_MMX_UOP),
173 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
174 .cntr = { {8, 9, -1}, {10, 11, -1} },
175 },
176 [P4_EVENT_X87_FP_UOP] = {
177 .opcode = P4_OPCODE(P4_EVENT_X87_FP_UOP),
178 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
179 .cntr = { {8, 9, -1}, {10, 11, -1} },
180 },
181 [P4_EVENT_TC_MISC] = {
182 .opcode = P4_OPCODE(P4_EVENT_TC_MISC),
183 .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 },
184 .cntr = { {4, 5, -1}, {6, 7, -1} },
185 },
186 [P4_EVENT_GLOBAL_POWER_EVENTS] = {
187 .opcode = P4_OPCODE(P4_EVENT_GLOBAL_POWER_EVENTS),
188 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
189 .cntr = { {0, -1, -1}, {2, -1, -1} },
190 },
191 [P4_EVENT_TC_MS_XFER] = {
192 .opcode = P4_OPCODE(P4_EVENT_TC_MS_XFER),
193 .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 },
194 .cntr = { {4, 5, -1}, {6, 7, -1} },
195 },
196 [P4_EVENT_UOP_QUEUE_WRITES] = {
197 .opcode = P4_OPCODE(P4_EVENT_UOP_QUEUE_WRITES),
198 .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 },
199 .cntr = { {4, 5, -1}, {6, 7, -1} },
200 },
201 [P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE] = {
202 .opcode = P4_OPCODE(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE),
203 .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR0 },
204 .cntr = { {4, 5, -1}, {6, 7, -1} },
205 },
206 [P4_EVENT_RETIRED_BRANCH_TYPE] = {
207 .opcode = P4_OPCODE(P4_EVENT_RETIRED_BRANCH_TYPE),
208 .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR1 },
209 .cntr = { {4, 5, -1}, {6, 7, -1} },
210 },
211 [P4_EVENT_RESOURCE_STALL] = {
212 .opcode = P4_OPCODE(P4_EVENT_RESOURCE_STALL),
213 .escr_msr = { MSR_P4_ALF_ESCR0, MSR_P4_ALF_ESCR1 },
214 .cntr = { {12, 13, 16}, {14, 15, 17} },
215 },
216 [P4_EVENT_WC_BUFFER] = {
217 .opcode = P4_OPCODE(P4_EVENT_WC_BUFFER),
218 .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 },
219 .cntr = { {8, 9, -1}, {10, 11, -1} },
220 },
221 [P4_EVENT_B2B_CYCLES] = {
222 .opcode = P4_OPCODE(P4_EVENT_B2B_CYCLES),
223 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
224 .cntr = { {0, -1, -1}, {2, -1, -1} },
225 },
226 [P4_EVENT_BNR] = {
227 .opcode = P4_OPCODE(P4_EVENT_BNR),
228 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
229 .cntr = { {0, -1, -1}, {2, -1, -1} },
230 },
231 [P4_EVENT_SNOOP] = {
232 .opcode = P4_OPCODE(P4_EVENT_SNOOP),
233 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
234 .cntr = { {0, -1, -1}, {2, -1, -1} },
235 },
236 [P4_EVENT_RESPONSE] = {
237 .opcode = P4_OPCODE(P4_EVENT_RESPONSE),
238 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
239 .cntr = { {0, -1, -1}, {2, -1, -1} },
240 },
241 [P4_EVENT_FRONT_END_EVENT] = {
242 .opcode = P4_OPCODE(P4_EVENT_FRONT_END_EVENT),
243 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
244 .cntr = { {12, 13, 16}, {14, 15, 17} },
245 },
246 [P4_EVENT_EXECUTION_EVENT] = {
247 .opcode = P4_OPCODE(P4_EVENT_EXECUTION_EVENT),
248 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
249 .cntr = { {12, 13, 16}, {14, 15, 17} },
250 },
251 [P4_EVENT_REPLAY_EVENT] = {
252 .opcode = P4_OPCODE(P4_EVENT_REPLAY_EVENT),
253 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
254 .cntr = { {12, 13, 16}, {14, 15, 17} },
255 },
256 [P4_EVENT_INSTR_RETIRED] = {
257 .opcode = P4_OPCODE(P4_EVENT_INSTR_RETIRED),
258 .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
259 .cntr = { {12, 13, 16}, {14, 15, 17} },
260 },
261 [P4_EVENT_UOPS_RETIRED] = {
262 .opcode = P4_OPCODE(P4_EVENT_UOPS_RETIRED),
263 .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
264 .cntr = { {12, 13, 16}, {14, 15, 17} },
265 },
266 [P4_EVENT_UOP_TYPE] = {
267 .opcode = P4_OPCODE(P4_EVENT_UOP_TYPE),
268 .escr_msr = { MSR_P4_RAT_ESCR0, MSR_P4_RAT_ESCR1 },
269 .cntr = { {12, 13, 16}, {14, 15, 17} },
270 },
271 [P4_EVENT_BRANCH_RETIRED] = {
272 .opcode = P4_OPCODE(P4_EVENT_BRANCH_RETIRED),
273 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
274 .cntr = { {12, 13, 16}, {14, 15, 17} },
275 },
276 [P4_EVENT_MISPRED_BRANCH_RETIRED] = {
277 .opcode = P4_OPCODE(P4_EVENT_MISPRED_BRANCH_RETIRED),
278 .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
279 .cntr = { {12, 13, 16}, {14, 15, 17} },
280 },
281 [P4_EVENT_X87_ASSIST] = {
282 .opcode = P4_OPCODE(P4_EVENT_X87_ASSIST),
283 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
284 .cntr = { {12, 13, 16}, {14, 15, 17} },
285 },
286 [P4_EVENT_MACHINE_CLEAR] = {
287 .opcode = P4_OPCODE(P4_EVENT_MACHINE_CLEAR),
288 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
289 .cntr = { {12, 13, 16}, {14, 15, 17} },
290 },
291 [P4_EVENT_INSTR_COMPLETED] = {
292 .opcode = P4_OPCODE(P4_EVENT_INSTR_COMPLETED),
293 .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
294 .cntr = { {12, 13, 16}, {14, 15, 17} },
295 },
296};
a072738e 297
39ef13a4 298#define P4_GEN_CACHE_EVENT(event, bit, metric) \
d814f301
CG
299 p4_config_pack_escr(P4_ESCR_EVENT(event) | \
300 P4_ESCR_EMASK_BIT(event, bit)) | \
39ef13a4 301 p4_config_pack_cccr(metric | \
d814f301 302 P4_CCCR_ESEL(P4_OPCODE_ESEL(P4_OPCODE(event))))
cb7d6b50 303
caaa8be3 304static __initconst const u64 p4_hw_cache_event_ids
cb7d6b50
LM
305 [PERF_COUNT_HW_CACHE_MAX]
306 [PERF_COUNT_HW_CACHE_OP_MAX]
307 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
308{
309 [ C(L1D ) ] = {
310 [ C(OP_READ) ] = {
311 [ C(RESULT_ACCESS) ] = 0x0,
d814f301 312 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
39ef13a4 313 P4_PEBS_METRIC__1stl_cache_load_miss_retired),
cb7d6b50
LM
314 },
315 },
316 [ C(LL ) ] = {
317 [ C(OP_READ) ] = {
318 [ C(RESULT_ACCESS) ] = 0x0,
d814f301 319 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
39ef13a4 320 P4_PEBS_METRIC__2ndl_cache_load_miss_retired),
cb7d6b50 321 },
d814f301 322},
cb7d6b50
LM
323 [ C(DTLB) ] = {
324 [ C(OP_READ) ] = {
325 [ C(RESULT_ACCESS) ] = 0x0,
d814f301 326 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
39ef13a4 327 P4_PEBS_METRIC__dtlb_load_miss_retired),
cb7d6b50
LM
328 },
329 [ C(OP_WRITE) ] = {
330 [ C(RESULT_ACCESS) ] = 0x0,
d814f301 331 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
39ef13a4 332 P4_PEBS_METRIC__dtlb_store_miss_retired),
cb7d6b50
LM
333 },
334 },
335 [ C(ITLB) ] = {
336 [ C(OP_READ) ] = {
d814f301 337 [ C(RESULT_ACCESS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, HIT,
39ef13a4 338 P4_PEBS_METRIC__none),
d814f301 339 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, MISS,
39ef13a4 340 P4_PEBS_METRIC__none),
cb7d6b50
LM
341 },
342 [ C(OP_WRITE) ] = {
343 [ C(RESULT_ACCESS) ] = -1,
344 [ C(RESULT_MISS) ] = -1,
345 },
346 [ C(OP_PREFETCH) ] = {
347 [ C(RESULT_ACCESS) ] = -1,
348 [ C(RESULT_MISS) ] = -1,
349 },
350 },
351};
352
d814f301
CG
353static u64 p4_general_events[PERF_COUNT_HW_MAX] = {
354 /* non-halted CPU clocks */
355 [PERF_COUNT_HW_CPU_CYCLES] =
356 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) |
357 P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)),
358
359 /*
360 * retired instructions
361 * in a sake of simplicity we don't use the FSB tagging
362 */
363 [PERF_COUNT_HW_INSTRUCTIONS] =
364 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_INSTR_RETIRED) |
365 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSNTAG) |
366 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSNTAG)),
367
368 /* cache hits */
369 [PERF_COUNT_HW_CACHE_REFERENCES] =
370 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) |
371 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) |
372 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) |
373 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) |
374 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) |
375 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) |
376 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITM)),
377
378 /* cache misses */
379 [PERF_COUNT_HW_CACHE_MISSES] =
380 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) |
381 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) |
382 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) |
383 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, WR_2ndL_MISS)),
384
385 /* branch instructions retired */
386 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] =
387 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_RETIRED_BRANCH_TYPE) |
388 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CONDITIONAL) |
389 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CALL) |
390 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, RETURN) |
391 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, INDIRECT)),
392
393 /* mispredicted branches retired */
394 [PERF_COUNT_HW_BRANCH_MISSES] =
395 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_MISPRED_BRANCH_RETIRED) |
396 P4_ESCR_EMASK_BIT(P4_EVENT_MISPRED_BRANCH_RETIRED, NBOGUS)),
397
398 /* bus ready clocks (cpu is driving #DRDY_DRV\#DRDY_OWN): */
399 [PERF_COUNT_HW_BUS_CYCLES] =
400 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_FSB_DATA_ACTIVITY) |
401 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_DRV) |
402 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OWN)) |
403 p4_config_pack_cccr(P4_CCCR_EDGE | P4_CCCR_COMPARE),
a072738e
CG
404};
405
d814f301
CG
406static struct p4_event_bind *p4_config_get_bind(u64 config)
407{
408 unsigned int evnt = p4_config_unpack_event(config);
409 struct p4_event_bind *bind = NULL;
410
411 if (evnt < ARRAY_SIZE(p4_event_bind_map))
412 bind = &p4_event_bind_map[evnt];
413
414 return bind;
415}
416
a072738e
CG
417static u64 p4_pmu_event_map(int hw_event)
418{
d814f301
CG
419 struct p4_event_bind *bind;
420 unsigned int esel;
a072738e
CG
421 u64 config;
422
d814f301
CG
423 config = p4_general_events[hw_event];
424 bind = p4_config_get_bind(config);
425 esel = P4_OPCODE_ESEL(bind->opcode);
426 config |= p4_config_pack_cccr(P4_CCCR_ESEL(esel));
a072738e 427
a072738e
CG
428 return config;
429}
430
39ef13a4
CG
431static int p4_validate_raw_event(struct perf_event *event)
432{
433 unsigned int v;
434
435 /* user data may have out-of-bound event index */
436 v = p4_config_unpack_event(event->attr.config);
437 if (v >= ARRAY_SIZE(p4_event_bind_map)) {
438 pr_warning("P4 PMU: Unknown event code: %d\n", v);
439 return -EINVAL;
440 }
441
442 /*
443 * it may have some screwed PEBS bits
444 */
445 if (p4_config_pebs_has(event->attr.config, P4_PEBS_CONFIG_ENABLE)) {
446 pr_warning("P4 PMU: PEBS are not supported yet\n");
447 return -EINVAL;
448 }
449 v = p4_config_unpack_metric(event->attr.config);
450 if (v >= ARRAY_SIZE(p4_pebs_bind_map)) {
451 pr_warning("P4 PMU: Unknown metric code: %d\n", v);
452 return -EINVAL;
453 }
454
455 return 0;
456}
457
b4cdc5c2 458static int p4_hw_config(struct perf_event *event)
a072738e 459{
137351e0
CG
460 int cpu = get_cpu();
461 int rc = 0;
d814f301 462 u32 escr, cccr;
a072738e
CG
463
464 /*
465 * the reason we use cpu that early is that: if we get scheduled
466 * first time on the same cpu -- we will not need swap thread
467 * specific flags in config (and will save some cpu cycles)
468 */
469
d814f301 470 cccr = p4_default_cccr_conf(cpu);
b4cdc5c2
PZ
471 escr = p4_default_escr_conf(cpu, event->attr.exclude_kernel,
472 event->attr.exclude_user);
473 event->hw.config = p4_config_pack_escr(escr) |
474 p4_config_pack_cccr(cccr);
a072738e 475
cb7d6b50 476 if (p4_ht_active() && p4_ht_thread(cpu))
b4cdc5c2
PZ
477 event->hw.config = p4_set_ht_bit(event->hw.config);
478
de902d96 479 if (event->attr.type == PERF_TYPE_RAW) {
c7993165 480
39ef13a4
CG
481 rc = p4_validate_raw_event(event);
482 if (rc)
c7993165 483 goto out;
c7993165 484
de902d96
CG
485 /*
486 * We don't control raw events so it's up to the caller
487 * to pass sane values (and we don't count the thread number
488 * on HT machine but allow HT-compatible specifics to be
489 * passed on)
490 *
39ef13a4
CG
491 * Note that for RAW events we allow user to use P4_CCCR_RESERVED
492 * bits since we keep additional info here (for cache events and etc)
493 *
de902d96
CG
494 * XXX: HT wide things should check perf_paranoid_cpu() &&
495 * CAP_SYS_ADMIN
496 */
497 event->hw.config |= event->attr.config &
498 (p4_config_pack_escr(P4_ESCR_MASK_HT) |
39ef13a4 499 p4_config_pack_cccr(P4_CCCR_MASK_HT | P4_CCCR_RESERVED));
de902d96 500 }
cb7d6b50 501
137351e0 502 rc = x86_setup_perfctr(event);
c7993165 503out:
137351e0 504 put_cpu();
137351e0 505 return rc;
a072738e
CG
506}
507
0db1a7bc 508static inline int p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc)
a072738e 509{
0db1a7bc
CG
510 int overflow = 0;
511 u32 low, high;
a072738e 512
0db1a7bc
CG
513 rdmsr(hwc->config_base + hwc->idx, low, high);
514
515 /* we need to check high bit for unflagged overflows */
ef4f30f5 516 if ((low & P4_CCCR_OVF) || !(high & (1 << 31))) {
0db1a7bc 517 overflow = 1;
a072738e 518 (void)checking_wrmsrl(hwc->config_base + hwc->idx,
0db1a7bc 519 ((u64)low) & ~P4_CCCR_OVF);
a072738e 520 }
0db1a7bc
CG
521
522 return overflow;
a072738e
CG
523}
524
39ef13a4
CG
525static void p4_pmu_disable_pebs(void)
526{
527 /*
528 * FIXME
529 *
530 * It's still allowed that two threads setup same cache
531 * events so we can't simply clear metrics until we knew
532 * noone is depending on us, so we need kind of counter
533 * for "ReplayEvent" users.
534 *
535 * What is more complex -- RAW events, if user (for some
536 * reason) will pass some cache event metric with improper
537 * event opcode -- it's fine from hardware point of view
538 * but completely nonsence from "meaning" of such action.
539 *
540 * So at moment let leave metrics turned on forever -- it's
541 * ok for now but need to be revisited!
542 *
543 * (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)0);
544 * (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)0);
545 */
546}
547
a072738e
CG
548static inline void p4_pmu_disable_event(struct perf_event *event)
549{
550 struct hw_perf_event *hwc = &event->hw;
551
552 /*
553 * If event gets disabled while counter is in overflowed
554 * state we need to clear P4_CCCR_OVF, otherwise interrupt get
555 * asserted again and again
556 */
557 (void)checking_wrmsrl(hwc->config_base + hwc->idx,
558 (u64)(p4_config_unpack_cccr(hwc->config)) &
d814f301 559 ~P4_CCCR_ENABLE & ~P4_CCCR_OVF & ~P4_CCCR_RESERVED);
a072738e
CG
560}
561
562static void p4_pmu_disable_all(void)
563{
564 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
565 int idx;
566
948b1bb8 567 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
a072738e
CG
568 struct perf_event *event = cpuc->events[idx];
569 if (!test_bit(idx, cpuc->active_mask))
570 continue;
571 p4_pmu_disable_event(event);
572 }
39ef13a4
CG
573
574 p4_pmu_disable_pebs();
575}
576
577/* configuration must be valid */
578static void p4_pmu_enable_pebs(u64 config)
579{
580 struct p4_pebs_bind *bind;
581 unsigned int idx;
582
583 BUILD_BUG_ON(P4_PEBS_METRIC__max > P4_PEBS_CONFIG_METRIC_MASK);
584
585 idx = p4_config_unpack_metric(config);
586 if (idx == P4_PEBS_METRIC__none)
587 return;
588
589 bind = &p4_pebs_bind_map[idx];
590
591 (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)bind->metric_pebs);
592 (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)bind->metric_vert);
a072738e
CG
593}
594
595static void p4_pmu_enable_event(struct perf_event *event)
596{
597 struct hw_perf_event *hwc = &event->hw;
598 int thread = p4_ht_config_thread(hwc->config);
599 u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config));
d814f301 600 unsigned int idx = p4_config_unpack_event(hwc->config);
d814f301 601 struct p4_event_bind *bind;
d814f301 602 u64 escr_addr, cccr;
a072738e 603
d814f301
CG
604 bind = &p4_event_bind_map[idx];
605 escr_addr = (u64)bind->escr_msr[thread];
a072738e
CG
606
607 /*
608 * - we dont support cascaded counters yet
609 * - and counter 1 is broken (erratum)
610 */
611 WARN_ON_ONCE(p4_is_event_cascaded(hwc->config));
612 WARN_ON_ONCE(hwc->idx == 1);
613
d814f301
CG
614 /* we need a real Event value */
615 escr_conf &= ~P4_ESCR_EVENT_MASK;
616 escr_conf |= P4_ESCR_EVENT(P4_OPCODE_EVNT(bind->opcode));
617
618 cccr = p4_config_unpack_cccr(hwc->config);
619
620 /*
39ef13a4
CG
621 * it could be Cache event so we need to write metrics
622 * into additional MSRs
d814f301 623 */
39ef13a4 624 p4_pmu_enable_pebs(hwc->config);
d814f301
CG
625
626 (void)checking_wrmsrl(escr_addr, escr_conf);
a072738e 627 (void)checking_wrmsrl(hwc->config_base + hwc->idx,
d814f301 628 (cccr & ~P4_CCCR_RESERVED) | P4_CCCR_ENABLE);
a072738e
CG
629}
630
11164cd4 631static void p4_pmu_enable_all(int added)
a072738e
CG
632{
633 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
634 int idx;
635
948b1bb8 636 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
a072738e
CG
637 struct perf_event *event = cpuc->events[idx];
638 if (!test_bit(idx, cpuc->active_mask))
639 continue;
640 p4_pmu_enable_event(event);
641 }
642}
643
644static int p4_pmu_handle_irq(struct pt_regs *regs)
645{
646 struct perf_sample_data data;
647 struct cpu_hw_events *cpuc;
648 struct perf_event *event;
649 struct hw_perf_event *hwc;
650 int idx, handled = 0;
651 u64 val;
652
653 data.addr = 0;
654 data.raw = NULL;
655
656 cpuc = &__get_cpu_var(cpu_hw_events);
657
948b1bb8 658 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
1c250d70 659 int overflow;
a072738e
CG
660
661 if (!test_bit(idx, cpuc->active_mask))
662 continue;
663
664 event = cpuc->events[idx];
665 hwc = &event->hw;
666
667 WARN_ON_ONCE(hwc->idx != idx);
668
0db1a7bc 669 /* it might be unflagged overflow */
1c250d70 670 overflow = p4_pmu_clear_cccr_ovf(hwc);
a072738e
CG
671
672 val = x86_perf_event_update(event);
1c250d70 673 if (!overflow && (val & (1ULL << (x86_pmu.cntval_bits - 1))))
a072738e
CG
674 continue;
675
1c250d70
CG
676 handled += overflow;
677
0db1a7bc
CG
678 /* event overflow for sure */
679 data.period = event->hw.last_period;
a072738e
CG
680
681 if (!x86_perf_event_set_period(event))
682 continue;
683 if (perf_event_overflow(event, 1, &data, regs))
684 p4_pmu_disable_event(event);
685 }
686
687 if (handled) {
688 /* p4 quirk: unmask it again */
689 apic_write(APIC_LVTPC, apic_read(APIC_LVTPC) & ~APIC_LVT_MASKED);
690 inc_irq_stat(apic_perf_irqs);
691 }
692
1c250d70 693 return handled > 0;
a072738e
CG
694}
695
696/*
697 * swap thread specific fields according to a thread
698 * we are going to run on
699 */
700static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu)
701{
702 u32 escr, cccr;
703
704 /*
705 * we either lucky and continue on same cpu or no HT support
706 */
707 if (!p4_should_swap_ts(hwc->config, cpu))
708 return;
709
710 /*
711 * the event is migrated from an another logical
712 * cpu, so we need to swap thread specific flags
713 */
714
715 escr = p4_config_unpack_escr(hwc->config);
716 cccr = p4_config_unpack_cccr(hwc->config);
717
718 if (p4_ht_thread(cpu)) {
719 cccr &= ~P4_CCCR_OVF_PMI_T0;
720 cccr |= P4_CCCR_OVF_PMI_T1;
d814f301
CG
721 if (escr & P4_ESCR_T0_OS) {
722 escr &= ~P4_ESCR_T0_OS;
723 escr |= P4_ESCR_T1_OS;
a072738e 724 }
d814f301
CG
725 if (escr & P4_ESCR_T0_USR) {
726 escr &= ~P4_ESCR_T0_USR;
727 escr |= P4_ESCR_T1_USR;
a072738e
CG
728 }
729 hwc->config = p4_config_pack_escr(escr);
730 hwc->config |= p4_config_pack_cccr(cccr);
731 hwc->config |= P4_CONFIG_HT;
732 } else {
733 cccr &= ~P4_CCCR_OVF_PMI_T1;
734 cccr |= P4_CCCR_OVF_PMI_T0;
d814f301
CG
735 if (escr & P4_ESCR_T1_OS) {
736 escr &= ~P4_ESCR_T1_OS;
737 escr |= P4_ESCR_T0_OS;
a072738e 738 }
d814f301
CG
739 if (escr & P4_ESCR_T1_USR) {
740 escr &= ~P4_ESCR_T1_USR;
741 escr |= P4_ESCR_T0_USR;
a072738e
CG
742 }
743 hwc->config = p4_config_pack_escr(escr);
744 hwc->config |= p4_config_pack_cccr(cccr);
745 hwc->config &= ~P4_CONFIG_HT;
746 }
747}
748
72001990
CG
749/*
750 * ESCR address hashing is tricky, ESCRs are not sequential
623aab89 751 * in memory but all starts from MSR_P4_BSU_ESCR0 (0x03a0) and
72001990
CG
752 * the metric between any ESCRs is laid in range [0xa0,0xe1]
753 *
754 * so we make ~70% filled hashtable
755 */
756
757#define P4_ESCR_MSR_BASE 0x000003a0
758#define P4_ESCR_MSR_MAX 0x000003e1
759#define P4_ESCR_MSR_TABLE_SIZE (P4_ESCR_MSR_MAX - P4_ESCR_MSR_BASE + 1)
760#define P4_ESCR_MSR_IDX(msr) (msr - P4_ESCR_MSR_BASE)
761#define P4_ESCR_MSR_TABLE_ENTRY(msr) [P4_ESCR_MSR_IDX(msr)] = msr
762
763static const unsigned int p4_escr_table[P4_ESCR_MSR_TABLE_SIZE] = {
764 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR0),
765 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR1),
766 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR0),
767 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR1),
768 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR0),
769 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR1),
770 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR0),
771 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR1),
772 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR2),
773 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR3),
774 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR4),
775 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR5),
776 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR0),
777 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR1),
778 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR0),
779 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR1),
780 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR0),
781 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR1),
782 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR0),
783 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR1),
784 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR0),
785 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR1),
786 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR0),
787 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR1),
788 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR0),
789 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR1),
790 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR0),
791 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR1),
792 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR0),
793 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR1),
794 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR0),
795 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR1),
796 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR0),
797 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR1),
798 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR0),
799 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR1),
800 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR0),
801 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR1),
802 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR0),
803 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR1),
804 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR0),
805 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR1),
806 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR0),
807 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR1),
808 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR0),
809 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR1),
a072738e
CG
810};
811
812static int p4_get_escr_idx(unsigned int addr)
813{
72001990 814 unsigned int idx = P4_ESCR_MSR_IDX(addr);
a072738e 815
623aab89
CG
816 if (unlikely(idx >= P4_ESCR_MSR_TABLE_SIZE ||
817 !p4_escr_table[idx] ||
818 p4_escr_table[idx] != addr)) {
72001990
CG
819 WARN_ONCE(1, "P4 PMU: Wrong address passed: %x\n", addr);
820 return -1;
a072738e
CG
821 }
822
72001990 823 return idx;
a072738e
CG
824}
825
d814f301
CG
826static int p4_next_cntr(int thread, unsigned long *used_mask,
827 struct p4_event_bind *bind)
828{
1ff3d7d7 829 int i, j;
d814f301
CG
830
831 for (i = 0; i < P4_CNTR_LIMIT; i++) {
1ff3d7d7
CG
832 j = bind->cntr[thread][i];
833 if (j != -1 && !test_bit(j, used_mask))
d814f301
CG
834 return j;
835 }
836
837 return -1;
838}
839
a072738e
CG
840static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
841{
842 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
72001990 843 unsigned long escr_mask[BITS_TO_LONGS(P4_ESCR_MSR_TABLE_SIZE)];
9d36dfcf 844 int cpu = smp_processor_id();
d814f301
CG
845 struct hw_perf_event *hwc;
846 struct p4_event_bind *bind;
847 unsigned int i, thread, num;
848 int cntr_idx, escr_idx;
a072738e
CG
849
850 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
72001990 851 bitmap_zero(escr_mask, P4_ESCR_MSR_TABLE_SIZE);
a072738e 852
a072738e 853 for (i = 0, num = n; i < n; i++, num--) {
d814f301 854
a072738e 855 hwc = &cpuc->event_list[i]->hw;
a072738e 856 thread = p4_ht_thread(cpu);
d814f301
CG
857 bind = p4_config_get_bind(hwc->config);
858 escr_idx = p4_get_escr_idx(bind->escr_msr[thread]);
72001990
CG
859 if (unlikely(escr_idx == -1))
860 goto done;
a072738e 861
a072738e 862 if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) {
d814f301 863 cntr_idx = hwc->idx;
a072738e
CG
864 if (assign)
865 assign[i] = hwc->idx;
a072738e
CG
866 goto reserve;
867 }
868
d814f301
CG
869 cntr_idx = p4_next_cntr(thread, used_mask, bind);
870 if (cntr_idx == -1 || test_bit(escr_idx, escr_mask))
a072738e
CG
871 goto done;
872
a072738e 873 p4_pmu_swap_config_ts(hwc, cpu);
d814f301
CG
874 if (assign)
875 assign[i] = cntr_idx;
a072738e 876reserve:
d814f301 877 set_bit(cntr_idx, used_mask);
a072738e
CG
878 set_bit(escr_idx, escr_mask);
879 }
880
881done:
882 return num ? -ENOSPC : 0;
883}
884
caaa8be3 885static __initconst const struct x86_pmu p4_pmu = {
a072738e
CG
886 .name = "Netburst P4/Xeon",
887 .handle_irq = p4_pmu_handle_irq,
888 .disable_all = p4_pmu_disable_all,
889 .enable_all = p4_pmu_enable_all,
890 .enable = p4_pmu_enable_event,
891 .disable = p4_pmu_disable_event,
892 .eventsel = MSR_P4_BPU_CCCR0,
893 .perfctr = MSR_P4_BPU_PERFCTR0,
894 .event_map = p4_pmu_event_map,
d814f301 895 .max_events = ARRAY_SIZE(p4_general_events),
a072738e
CG
896 .get_event_constraints = x86_get_event_constraints,
897 /*
898 * IF HT disabled we may need to use all
899 * ARCH_P4_MAX_CCCR counters simulaneously
900 * though leave it restricted at moment assuming
901 * HT is on
902 */
948b1bb8 903 .num_counters = ARCH_P4_MAX_CCCR,
a072738e 904 .apic = 1,
948b1bb8
RR
905 .cntval_bits = 40,
906 .cntval_mask = (1ULL << 40) - 1,
a072738e
CG
907 .max_period = (1ULL << 39) - 1,
908 .hw_config = p4_hw_config,
909 .schedule_events = p4_pmu_schedule_events,
68aa00ac
CG
910 /*
911 * This handles erratum N15 in intel doc 249199-029,
912 * the counter may not be updated correctly on write
913 * so we need a second write operation to do the trick
914 * (the official workaround didn't work)
915 *
916 * the former idea is taken from OProfile code
917 */
918 .perfctr_second_write = 1,
a072738e
CG
919};
920
921static __init int p4_pmu_init(void)
922{
923 unsigned int low, high;
924
925 /* If we get stripped -- indexig fails */
926 BUILD_BUG_ON(ARCH_P4_MAX_CCCR > X86_PMC_MAX_GENERIC);
927
928 rdmsr(MSR_IA32_MISC_ENABLE, low, high);
929 if (!(low & (1 << 7))) {
930 pr_cont("unsupported Netburst CPU model %d ",
931 boot_cpu_data.x86_model);
932 return -ENODEV;
933 }
934
cb7d6b50 935 memcpy(hw_cache_event_ids, p4_hw_cache_event_ids,
d814f301 936 sizeof(hw_cache_event_ids));
cb7d6b50 937
a072738e
CG
938 pr_cont("Netburst events, ");
939
940 x86_pmu = p4_pmu;
941
942 return 0;
943}
944
945#endif /* CONFIG_CPU_SUP_INTEL */