perf tools: Make test use the preset debugfs path
[deliverable/linux.git] / arch / x86 / kernel / cpu / perf_event_p4.c
CommitLineData
a072738e 1/*
0d2eb44f 2 * Netburst Performance Events (P4, old Xeon)
a072738e
CG
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 */
c9cf4a01
CG
21 unsigned int escr_emask; /* valid ESCR EventMask bits */
22 unsigned int shared; /* event is shared across threads */
1ff3d7d7 23 char cntr[2][P4_CNTR_LIMIT]; /* counter index (offset), -1 on abscence */
a072738e
CG
24};
25
39ef13a4 26struct p4_pebs_bind {
d814f301
CG
27 unsigned int metric_pebs;
28 unsigned int metric_vert;
a072738e
CG
29};
30
39ef13a4
CG
31/* it sets P4_PEBS_ENABLE_UOP_TAG as well */
32#define P4_GEN_PEBS_BIND(name, pebs, vert) \
33 [P4_PEBS_METRIC__##name] = { \
34 .metric_pebs = pebs | P4_PEBS_ENABLE_UOP_TAG, \
35 .metric_vert = vert, \
d814f301
CG
36 }
37
39ef13a4
CG
38/*
39 * note we have P4_PEBS_ENABLE_UOP_TAG always set here
40 *
41 * it's needed for mapping P4_PEBS_CONFIG_METRIC_MASK bits of
42 * event configuration to find out which values are to be
43 * written into MSR_IA32_PEBS_ENABLE and MSR_P4_PEBS_MATRIX_VERT
44 * resgisters
45 */
46static struct p4_pebs_bind p4_pebs_bind_map[] = {
47 P4_GEN_PEBS_BIND(1stl_cache_load_miss_retired, 0x0000001, 0x0000001),
48 P4_GEN_PEBS_BIND(2ndl_cache_load_miss_retired, 0x0000002, 0x0000001),
49 P4_GEN_PEBS_BIND(dtlb_load_miss_retired, 0x0000004, 0x0000001),
50 P4_GEN_PEBS_BIND(dtlb_store_miss_retired, 0x0000004, 0x0000002),
51 P4_GEN_PEBS_BIND(dtlb_all_miss_retired, 0x0000004, 0x0000003),
52 P4_GEN_PEBS_BIND(tagged_mispred_branch, 0x0018000, 0x0000010),
53 P4_GEN_PEBS_BIND(mob_load_replay_retired, 0x0000200, 0x0000001),
54 P4_GEN_PEBS_BIND(split_load_retired, 0x0000400, 0x0000001),
55 P4_GEN_PEBS_BIND(split_store_retired, 0x0000400, 0x0000002),
d814f301
CG
56};
57
58/*
59 * Note that we don't use CCCR1 here, there is an
60 * exception for P4_BSQ_ALLOCATION but we just have
61 * no workaround
62 *
63 * consider this binding as resources which particular
64 * event may borrow, it doesn't contain EventMask,
65 * Tags and friends -- they are left to a caller
66 */
67static struct p4_event_bind p4_event_bind_map[] = {
68 [P4_EVENT_TC_DELIVER_MODE] = {
69 .opcode = P4_OPCODE(P4_EVENT_TC_DELIVER_MODE),
70 .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 },
c9cf4a01
CG
71 .escr_emask =
72 P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, DD) |
73 P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, DB) |
74 P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, DI) |
75 P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, BD) |
76 P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, BB) |
77 P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, BI) |
78 P4_ESCR_EMASK_BIT(P4_EVENT_TC_DELIVER_MODE, ID),
79 .shared = 1,
d814f301
CG
80 .cntr = { {4, 5, -1}, {6, 7, -1} },
81 },
82 [P4_EVENT_BPU_FETCH_REQUEST] = {
83 .opcode = P4_OPCODE(P4_EVENT_BPU_FETCH_REQUEST),
84 .escr_msr = { MSR_P4_BPU_ESCR0, MSR_P4_BPU_ESCR1 },
c9cf4a01
CG
85 .escr_emask =
86 P4_ESCR_EMASK_BIT(P4_EVENT_BPU_FETCH_REQUEST, TCMISS),
d814f301
CG
87 .cntr = { {0, -1, -1}, {2, -1, -1} },
88 },
89 [P4_EVENT_ITLB_REFERENCE] = {
90 .opcode = P4_OPCODE(P4_EVENT_ITLB_REFERENCE),
91 .escr_msr = { MSR_P4_ITLB_ESCR0, MSR_P4_ITLB_ESCR1 },
c9cf4a01
CG
92 .escr_emask =
93 P4_ESCR_EMASK_BIT(P4_EVENT_ITLB_REFERENCE, HIT) |
94 P4_ESCR_EMASK_BIT(P4_EVENT_ITLB_REFERENCE, MISS) |
95 P4_ESCR_EMASK_BIT(P4_EVENT_ITLB_REFERENCE, HIT_UK),
d814f301
CG
96 .cntr = { {0, -1, -1}, {2, -1, -1} },
97 },
98 [P4_EVENT_MEMORY_CANCEL] = {
99 .opcode = P4_OPCODE(P4_EVENT_MEMORY_CANCEL),
100 .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 },
c9cf4a01
CG
101 .escr_emask =
102 P4_ESCR_EMASK_BIT(P4_EVENT_MEMORY_CANCEL, ST_RB_FULL) |
103 P4_ESCR_EMASK_BIT(P4_EVENT_MEMORY_CANCEL, 64K_CONF),
d814f301
CG
104 .cntr = { {8, 9, -1}, {10, 11, -1} },
105 },
106 [P4_EVENT_MEMORY_COMPLETE] = {
107 .opcode = P4_OPCODE(P4_EVENT_MEMORY_COMPLETE),
108 .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 },
c9cf4a01
CG
109 .escr_emask =
110 P4_ESCR_EMASK_BIT(P4_EVENT_MEMORY_COMPLETE, LSC) |
111 P4_ESCR_EMASK_BIT(P4_EVENT_MEMORY_COMPLETE, SSC),
d814f301
CG
112 .cntr = { {8, 9, -1}, {10, 11, -1} },
113 },
114 [P4_EVENT_LOAD_PORT_REPLAY] = {
115 .opcode = P4_OPCODE(P4_EVENT_LOAD_PORT_REPLAY),
116 .escr_msr = { MSR_P4_SAAT_ESCR0, MSR_P4_SAAT_ESCR1 },
c9cf4a01
CG
117 .escr_emask =
118 P4_ESCR_EMASK_BIT(P4_EVENT_LOAD_PORT_REPLAY, SPLIT_LD),
d814f301
CG
119 .cntr = { {8, 9, -1}, {10, 11, -1} },
120 },
121 [P4_EVENT_STORE_PORT_REPLAY] = {
122 .opcode = P4_OPCODE(P4_EVENT_STORE_PORT_REPLAY),
123 .escr_msr = { MSR_P4_SAAT_ESCR0 , MSR_P4_SAAT_ESCR1 },
c9cf4a01
CG
124 .escr_emask =
125 P4_ESCR_EMASK_BIT(P4_EVENT_STORE_PORT_REPLAY, SPLIT_ST),
d814f301
CG
126 .cntr = { {8, 9, -1}, {10, 11, -1} },
127 },
128 [P4_EVENT_MOB_LOAD_REPLAY] = {
129 .opcode = P4_OPCODE(P4_EVENT_MOB_LOAD_REPLAY),
130 .escr_msr = { MSR_P4_MOB_ESCR0, MSR_P4_MOB_ESCR1 },
c9cf4a01
CG
131 .escr_emask =
132 P4_ESCR_EMASK_BIT(P4_EVENT_MOB_LOAD_REPLAY, NO_STA) |
133 P4_ESCR_EMASK_BIT(P4_EVENT_MOB_LOAD_REPLAY, NO_STD) |
134 P4_ESCR_EMASK_BIT(P4_EVENT_MOB_LOAD_REPLAY, PARTIAL_DATA) |
135 P4_ESCR_EMASK_BIT(P4_EVENT_MOB_LOAD_REPLAY, UNALGN_ADDR),
d814f301
CG
136 .cntr = { {0, -1, -1}, {2, -1, -1} },
137 },
138 [P4_EVENT_PAGE_WALK_TYPE] = {
139 .opcode = P4_OPCODE(P4_EVENT_PAGE_WALK_TYPE),
140 .escr_msr = { MSR_P4_PMH_ESCR0, MSR_P4_PMH_ESCR1 },
c9cf4a01
CG
141 .escr_emask =
142 P4_ESCR_EMASK_BIT(P4_EVENT_PAGE_WALK_TYPE, DTMISS) |
143 P4_ESCR_EMASK_BIT(P4_EVENT_PAGE_WALK_TYPE, ITMISS),
144 .shared = 1,
d814f301
CG
145 .cntr = { {0, -1, -1}, {2, -1, -1} },
146 },
147 [P4_EVENT_BSQ_CACHE_REFERENCE] = {
148 .opcode = P4_OPCODE(P4_EVENT_BSQ_CACHE_REFERENCE),
149 .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR1 },
c9cf4a01
CG
150 .escr_emask =
151 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) |
152 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) |
153 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) |
154 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) |
155 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) |
156 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITM) |
157 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) |
158 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) |
159 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, WR_2ndL_MISS),
d814f301
CG
160 .cntr = { {0, -1, -1}, {2, -1, -1} },
161 },
162 [P4_EVENT_IOQ_ALLOCATION] = {
163 .opcode = P4_OPCODE(P4_EVENT_IOQ_ALLOCATION),
164 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
c9cf4a01
CG
165 .escr_emask =
166 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, DEFAULT) |
167 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, ALL_READ) |
168 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, ALL_WRITE) |
169 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_UC) |
170 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_WC) |
171 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_WT) |
172 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_WP) |
173 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, MEM_WB) |
174 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, OWN) |
175 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, OTHER) |
176 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ALLOCATION, PREFETCH),
d814f301
CG
177 .cntr = { {0, -1, -1}, {2, -1, -1} },
178 },
179 [P4_EVENT_IOQ_ACTIVE_ENTRIES] = { /* shared ESCR */
180 .opcode = P4_OPCODE(P4_EVENT_IOQ_ACTIVE_ENTRIES),
181 .escr_msr = { MSR_P4_FSB_ESCR1, MSR_P4_FSB_ESCR1 },
c9cf4a01
CG
182 .escr_emask =
183 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, DEFAULT) |
184 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, ALL_READ) |
185 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, ALL_WRITE) |
186 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_UC) |
187 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_WC) |
188 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_WT) |
189 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_WP) |
190 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, MEM_WB) |
191 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, OWN) |
192 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, OTHER) |
193 P4_ESCR_EMASK_BIT(P4_EVENT_IOQ_ACTIVE_ENTRIES, PREFETCH),
d814f301
CG
194 .cntr = { {2, -1, -1}, {3, -1, -1} },
195 },
196 [P4_EVENT_FSB_DATA_ACTIVITY] = {
197 .opcode = P4_OPCODE(P4_EVENT_FSB_DATA_ACTIVITY),
198 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
c9cf4a01
CG
199 .escr_emask =
200 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_DRV) |
201 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OWN) |
202 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OTHER) |
203 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DBSY_DRV) |
204 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DBSY_OWN) |
205 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DBSY_OTHER),
206 .shared = 1,
d814f301
CG
207 .cntr = { {0, -1, -1}, {2, -1, -1} },
208 },
209 [P4_EVENT_BSQ_ALLOCATION] = { /* shared ESCR, broken CCCR1 */
210 .opcode = P4_OPCODE(P4_EVENT_BSQ_ALLOCATION),
211 .escr_msr = { MSR_P4_BSU_ESCR0, MSR_P4_BSU_ESCR0 },
c9cf4a01
CG
212 .escr_emask =
213 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_TYPE0) |
214 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_TYPE1) |
215 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_LEN0) |
216 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_LEN1) |
217 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_IO_TYPE) |
218 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_LOCK_TYPE) |
219 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_CACHE_TYPE) |
220 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_SPLIT_TYPE) |
221 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_DEM_TYPE) |
222 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, REQ_ORD_TYPE) |
223 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, MEM_TYPE0) |
224 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, MEM_TYPE1) |
225 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ALLOCATION, MEM_TYPE2),
d814f301
CG
226 .cntr = { {0, -1, -1}, {1, -1, -1} },
227 },
228 [P4_EVENT_BSQ_ACTIVE_ENTRIES] = { /* shared ESCR */
229 .opcode = P4_OPCODE(P4_EVENT_BSQ_ACTIVE_ENTRIES),
230 .escr_msr = { MSR_P4_BSU_ESCR1 , MSR_P4_BSU_ESCR1 },
c9cf4a01
CG
231 .escr_emask =
232 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_TYPE0) |
233 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_TYPE1) |
234 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_LEN0) |
235 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_LEN1) |
236 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_IO_TYPE) |
237 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_LOCK_TYPE) |
238 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_CACHE_TYPE) |
239 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_SPLIT_TYPE) |
240 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_DEM_TYPE) |
241 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, REQ_ORD_TYPE) |
242 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, MEM_TYPE0) |
243 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, MEM_TYPE1) |
244 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_ACTIVE_ENTRIES, MEM_TYPE2),
d814f301
CG
245 .cntr = { {2, -1, -1}, {3, -1, -1} },
246 },
247 [P4_EVENT_SSE_INPUT_ASSIST] = {
248 .opcode = P4_OPCODE(P4_EVENT_SSE_INPUT_ASSIST),
249 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
c9cf4a01
CG
250 .escr_emask =
251 P4_ESCR_EMASK_BIT(P4_EVENT_SSE_INPUT_ASSIST, ALL),
252 .shared = 1,
d814f301
CG
253 .cntr = { {8, 9, -1}, {10, 11, -1} },
254 },
255 [P4_EVENT_PACKED_SP_UOP] = {
256 .opcode = P4_OPCODE(P4_EVENT_PACKED_SP_UOP),
257 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
c9cf4a01
CG
258 .escr_emask =
259 P4_ESCR_EMASK_BIT(P4_EVENT_PACKED_SP_UOP, ALL),
260 .shared = 1,
d814f301
CG
261 .cntr = { {8, 9, -1}, {10, 11, -1} },
262 },
263 [P4_EVENT_PACKED_DP_UOP] = {
264 .opcode = P4_OPCODE(P4_EVENT_PACKED_DP_UOP),
265 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
c9cf4a01
CG
266 .escr_emask =
267 P4_ESCR_EMASK_BIT(P4_EVENT_PACKED_DP_UOP, ALL),
268 .shared = 1,
d814f301
CG
269 .cntr = { {8, 9, -1}, {10, 11, -1} },
270 },
271 [P4_EVENT_SCALAR_SP_UOP] = {
272 .opcode = P4_OPCODE(P4_EVENT_SCALAR_SP_UOP),
273 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
c9cf4a01
CG
274 .escr_emask =
275 P4_ESCR_EMASK_BIT(P4_EVENT_SCALAR_SP_UOP, ALL),
276 .shared = 1,
d814f301
CG
277 .cntr = { {8, 9, -1}, {10, 11, -1} },
278 },
279 [P4_EVENT_SCALAR_DP_UOP] = {
280 .opcode = P4_OPCODE(P4_EVENT_SCALAR_DP_UOP),
281 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
c9cf4a01
CG
282 .escr_emask =
283 P4_ESCR_EMASK_BIT(P4_EVENT_SCALAR_DP_UOP, ALL),
284 .shared = 1,
d814f301
CG
285 .cntr = { {8, 9, -1}, {10, 11, -1} },
286 },
287 [P4_EVENT_64BIT_MMX_UOP] = {
288 .opcode = P4_OPCODE(P4_EVENT_64BIT_MMX_UOP),
289 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
c9cf4a01
CG
290 .escr_emask =
291 P4_ESCR_EMASK_BIT(P4_EVENT_64BIT_MMX_UOP, ALL),
292 .shared = 1,
d814f301
CG
293 .cntr = { {8, 9, -1}, {10, 11, -1} },
294 },
295 [P4_EVENT_128BIT_MMX_UOP] = {
296 .opcode = P4_OPCODE(P4_EVENT_128BIT_MMX_UOP),
297 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
c9cf4a01
CG
298 .escr_emask =
299 P4_ESCR_EMASK_BIT(P4_EVENT_128BIT_MMX_UOP, ALL),
300 .shared = 1,
d814f301
CG
301 .cntr = { {8, 9, -1}, {10, 11, -1} },
302 },
303 [P4_EVENT_X87_FP_UOP] = {
304 .opcode = P4_OPCODE(P4_EVENT_X87_FP_UOP),
305 .escr_msr = { MSR_P4_FIRM_ESCR0, MSR_P4_FIRM_ESCR1 },
c9cf4a01
CG
306 .escr_emask =
307 P4_ESCR_EMASK_BIT(P4_EVENT_X87_FP_UOP, ALL),
308 .shared = 1,
d814f301
CG
309 .cntr = { {8, 9, -1}, {10, 11, -1} },
310 },
311 [P4_EVENT_TC_MISC] = {
312 .opcode = P4_OPCODE(P4_EVENT_TC_MISC),
313 .escr_msr = { MSR_P4_TC_ESCR0, MSR_P4_TC_ESCR1 },
c9cf4a01
CG
314 .escr_emask =
315 P4_ESCR_EMASK_BIT(P4_EVENT_TC_MISC, FLUSH),
d814f301
CG
316 .cntr = { {4, 5, -1}, {6, 7, -1} },
317 },
318 [P4_EVENT_GLOBAL_POWER_EVENTS] = {
319 .opcode = P4_OPCODE(P4_EVENT_GLOBAL_POWER_EVENTS),
320 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
c9cf4a01
CG
321 .escr_emask =
322 P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING),
d814f301
CG
323 .cntr = { {0, -1, -1}, {2, -1, -1} },
324 },
325 [P4_EVENT_TC_MS_XFER] = {
326 .opcode = P4_OPCODE(P4_EVENT_TC_MS_XFER),
327 .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 },
c9cf4a01
CG
328 .escr_emask =
329 P4_ESCR_EMASK_BIT(P4_EVENT_TC_MS_XFER, CISC),
d814f301
CG
330 .cntr = { {4, 5, -1}, {6, 7, -1} },
331 },
332 [P4_EVENT_UOP_QUEUE_WRITES] = {
333 .opcode = P4_OPCODE(P4_EVENT_UOP_QUEUE_WRITES),
334 .escr_msr = { MSR_P4_MS_ESCR0, MSR_P4_MS_ESCR1 },
c9cf4a01
CG
335 .escr_emask =
336 P4_ESCR_EMASK_BIT(P4_EVENT_UOP_QUEUE_WRITES, FROM_TC_BUILD) |
337 P4_ESCR_EMASK_BIT(P4_EVENT_UOP_QUEUE_WRITES, FROM_TC_DELIVER) |
338 P4_ESCR_EMASK_BIT(P4_EVENT_UOP_QUEUE_WRITES, FROM_ROM),
d814f301
CG
339 .cntr = { {4, 5, -1}, {6, 7, -1} },
340 },
341 [P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE] = {
342 .opcode = P4_OPCODE(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE),
343 .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR0 },
c9cf4a01
CG
344 .escr_emask =
345 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE, CONDITIONAL) |
346 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE, CALL) |
347 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE, RETURN) |
348 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_MISPRED_BRANCH_TYPE, INDIRECT),
d814f301
CG
349 .cntr = { {4, 5, -1}, {6, 7, -1} },
350 },
351 [P4_EVENT_RETIRED_BRANCH_TYPE] = {
352 .opcode = P4_OPCODE(P4_EVENT_RETIRED_BRANCH_TYPE),
353 .escr_msr = { MSR_P4_TBPU_ESCR0 , MSR_P4_TBPU_ESCR1 },
c9cf4a01
CG
354 .escr_emask =
355 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CONDITIONAL) |
356 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CALL) |
357 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, RETURN) |
358 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, INDIRECT),
d814f301
CG
359 .cntr = { {4, 5, -1}, {6, 7, -1} },
360 },
361 [P4_EVENT_RESOURCE_STALL] = {
362 .opcode = P4_OPCODE(P4_EVENT_RESOURCE_STALL),
363 .escr_msr = { MSR_P4_ALF_ESCR0, MSR_P4_ALF_ESCR1 },
c9cf4a01
CG
364 .escr_emask =
365 P4_ESCR_EMASK_BIT(P4_EVENT_RESOURCE_STALL, SBFULL),
d814f301
CG
366 .cntr = { {12, 13, 16}, {14, 15, 17} },
367 },
368 [P4_EVENT_WC_BUFFER] = {
369 .opcode = P4_OPCODE(P4_EVENT_WC_BUFFER),
370 .escr_msr = { MSR_P4_DAC_ESCR0, MSR_P4_DAC_ESCR1 },
c9cf4a01
CG
371 .escr_emask =
372 P4_ESCR_EMASK_BIT(P4_EVENT_WC_BUFFER, WCB_EVICTS) |
373 P4_ESCR_EMASK_BIT(P4_EVENT_WC_BUFFER, WCB_FULL_EVICTS),
374 .shared = 1,
d814f301
CG
375 .cntr = { {8, 9, -1}, {10, 11, -1} },
376 },
377 [P4_EVENT_B2B_CYCLES] = {
378 .opcode = P4_OPCODE(P4_EVENT_B2B_CYCLES),
379 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
c9cf4a01 380 .escr_emask = 0,
d814f301
CG
381 .cntr = { {0, -1, -1}, {2, -1, -1} },
382 },
383 [P4_EVENT_BNR] = {
384 .opcode = P4_OPCODE(P4_EVENT_BNR),
385 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
c9cf4a01 386 .escr_emask = 0,
d814f301
CG
387 .cntr = { {0, -1, -1}, {2, -1, -1} },
388 },
389 [P4_EVENT_SNOOP] = {
390 .opcode = P4_OPCODE(P4_EVENT_SNOOP),
391 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
c9cf4a01 392 .escr_emask = 0,
d814f301
CG
393 .cntr = { {0, -1, -1}, {2, -1, -1} },
394 },
395 [P4_EVENT_RESPONSE] = {
396 .opcode = P4_OPCODE(P4_EVENT_RESPONSE),
397 .escr_msr = { MSR_P4_FSB_ESCR0, MSR_P4_FSB_ESCR1 },
c9cf4a01 398 .escr_emask = 0,
d814f301
CG
399 .cntr = { {0, -1, -1}, {2, -1, -1} },
400 },
401 [P4_EVENT_FRONT_END_EVENT] = {
402 .opcode = P4_OPCODE(P4_EVENT_FRONT_END_EVENT),
403 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
c9cf4a01
CG
404 .escr_emask =
405 P4_ESCR_EMASK_BIT(P4_EVENT_FRONT_END_EVENT, NBOGUS) |
406 P4_ESCR_EMASK_BIT(P4_EVENT_FRONT_END_EVENT, BOGUS),
d814f301
CG
407 .cntr = { {12, 13, 16}, {14, 15, 17} },
408 },
409 [P4_EVENT_EXECUTION_EVENT] = {
410 .opcode = P4_OPCODE(P4_EVENT_EXECUTION_EVENT),
411 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
c9cf4a01
CG
412 .escr_emask =
413 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS0) |
414 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS1) |
415 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS2) |
416 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS3) |
417 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS0) |
418 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS1) |
419 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS2) |
420 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS3),
d814f301
CG
421 .cntr = { {12, 13, 16}, {14, 15, 17} },
422 },
423 [P4_EVENT_REPLAY_EVENT] = {
424 .opcode = P4_OPCODE(P4_EVENT_REPLAY_EVENT),
425 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
c9cf4a01
CG
426 .escr_emask =
427 P4_ESCR_EMASK_BIT(P4_EVENT_REPLAY_EVENT, NBOGUS) |
428 P4_ESCR_EMASK_BIT(P4_EVENT_REPLAY_EVENT, BOGUS),
d814f301
CG
429 .cntr = { {12, 13, 16}, {14, 15, 17} },
430 },
431 [P4_EVENT_INSTR_RETIRED] = {
432 .opcode = P4_OPCODE(P4_EVENT_INSTR_RETIRED),
433 .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
c9cf4a01
CG
434 .escr_emask =
435 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSNTAG) |
436 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSTAG) |
437 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSNTAG) |
438 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSTAG),
d814f301
CG
439 .cntr = { {12, 13, 16}, {14, 15, 17} },
440 },
441 [P4_EVENT_UOPS_RETIRED] = {
442 .opcode = P4_OPCODE(P4_EVENT_UOPS_RETIRED),
443 .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
c9cf4a01
CG
444 .escr_emask =
445 P4_ESCR_EMASK_BIT(P4_EVENT_UOPS_RETIRED, NBOGUS) |
446 P4_ESCR_EMASK_BIT(P4_EVENT_UOPS_RETIRED, BOGUS),
d814f301
CG
447 .cntr = { {12, 13, 16}, {14, 15, 17} },
448 },
449 [P4_EVENT_UOP_TYPE] = {
450 .opcode = P4_OPCODE(P4_EVENT_UOP_TYPE),
451 .escr_msr = { MSR_P4_RAT_ESCR0, MSR_P4_RAT_ESCR1 },
c9cf4a01
CG
452 .escr_emask =
453 P4_ESCR_EMASK_BIT(P4_EVENT_UOP_TYPE, TAGLOADS) |
454 P4_ESCR_EMASK_BIT(P4_EVENT_UOP_TYPE, TAGSTORES),
d814f301
CG
455 .cntr = { {12, 13, 16}, {14, 15, 17} },
456 },
457 [P4_EVENT_BRANCH_RETIRED] = {
458 .opcode = P4_OPCODE(P4_EVENT_BRANCH_RETIRED),
459 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
c9cf4a01
CG
460 .escr_emask =
461 P4_ESCR_EMASK_BIT(P4_EVENT_BRANCH_RETIRED, MMNP) |
462 P4_ESCR_EMASK_BIT(P4_EVENT_BRANCH_RETIRED, MMNM) |
463 P4_ESCR_EMASK_BIT(P4_EVENT_BRANCH_RETIRED, MMTP) |
464 P4_ESCR_EMASK_BIT(P4_EVENT_BRANCH_RETIRED, MMTM),
d814f301
CG
465 .cntr = { {12, 13, 16}, {14, 15, 17} },
466 },
467 [P4_EVENT_MISPRED_BRANCH_RETIRED] = {
468 .opcode = P4_OPCODE(P4_EVENT_MISPRED_BRANCH_RETIRED),
469 .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
c9cf4a01 470 .escr_emask =
1d321881 471 P4_ESCR_EMASK_BIT(P4_EVENT_MISPRED_BRANCH_RETIRED, NBOGUS),
d814f301
CG
472 .cntr = { {12, 13, 16}, {14, 15, 17} },
473 },
474 [P4_EVENT_X87_ASSIST] = {
475 .opcode = P4_OPCODE(P4_EVENT_X87_ASSIST),
476 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
c9cf4a01
CG
477 .escr_emask =
478 P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, FPSU) |
479 P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, FPSO) |
480 P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, POAO) |
481 P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, POAU) |
482 P4_ESCR_EMASK_BIT(P4_EVENT_X87_ASSIST, PREA),
d814f301
CG
483 .cntr = { {12, 13, 16}, {14, 15, 17} },
484 },
485 [P4_EVENT_MACHINE_CLEAR] = {
486 .opcode = P4_OPCODE(P4_EVENT_MACHINE_CLEAR),
487 .escr_msr = { MSR_P4_CRU_ESCR2, MSR_P4_CRU_ESCR3 },
c9cf4a01
CG
488 .escr_emask =
489 P4_ESCR_EMASK_BIT(P4_EVENT_MACHINE_CLEAR, CLEAR) |
490 P4_ESCR_EMASK_BIT(P4_EVENT_MACHINE_CLEAR, MOCLEAR) |
491 P4_ESCR_EMASK_BIT(P4_EVENT_MACHINE_CLEAR, SMCLEAR),
d814f301
CG
492 .cntr = { {12, 13, 16}, {14, 15, 17} },
493 },
494 [P4_EVENT_INSTR_COMPLETED] = {
495 .opcode = P4_OPCODE(P4_EVENT_INSTR_COMPLETED),
496 .escr_msr = { MSR_P4_CRU_ESCR0, MSR_P4_CRU_ESCR1 },
c9cf4a01
CG
497 .escr_emask =
498 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_COMPLETED, NBOGUS) |
499 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_COMPLETED, BOGUS),
d814f301
CG
500 .cntr = { {12, 13, 16}, {14, 15, 17} },
501 },
502};
a072738e 503
39ef13a4 504#define P4_GEN_CACHE_EVENT(event, bit, metric) \
d814f301
CG
505 p4_config_pack_escr(P4_ESCR_EVENT(event) | \
506 P4_ESCR_EMASK_BIT(event, bit)) | \
39ef13a4 507 p4_config_pack_cccr(metric | \
d814f301 508 P4_CCCR_ESEL(P4_OPCODE_ESEL(P4_OPCODE(event))))
cb7d6b50 509
caaa8be3 510static __initconst const u64 p4_hw_cache_event_ids
cb7d6b50
LM
511 [PERF_COUNT_HW_CACHE_MAX]
512 [PERF_COUNT_HW_CACHE_OP_MAX]
513 [PERF_COUNT_HW_CACHE_RESULT_MAX] =
514{
515 [ C(L1D ) ] = {
516 [ C(OP_READ) ] = {
517 [ C(RESULT_ACCESS) ] = 0x0,
d814f301 518 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
39ef13a4 519 P4_PEBS_METRIC__1stl_cache_load_miss_retired),
cb7d6b50
LM
520 },
521 },
522 [ C(LL ) ] = {
523 [ C(OP_READ) ] = {
524 [ C(RESULT_ACCESS) ] = 0x0,
d814f301 525 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
39ef13a4 526 P4_PEBS_METRIC__2ndl_cache_load_miss_retired),
cb7d6b50 527 },
d814f301 528},
cb7d6b50
LM
529 [ C(DTLB) ] = {
530 [ C(OP_READ) ] = {
531 [ C(RESULT_ACCESS) ] = 0x0,
d814f301 532 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
39ef13a4 533 P4_PEBS_METRIC__dtlb_load_miss_retired),
cb7d6b50
LM
534 },
535 [ C(OP_WRITE) ] = {
536 [ C(RESULT_ACCESS) ] = 0x0,
d814f301 537 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_REPLAY_EVENT, NBOGUS,
39ef13a4 538 P4_PEBS_METRIC__dtlb_store_miss_retired),
cb7d6b50
LM
539 },
540 },
541 [ C(ITLB) ] = {
542 [ C(OP_READ) ] = {
d814f301 543 [ C(RESULT_ACCESS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, HIT,
39ef13a4 544 P4_PEBS_METRIC__none),
d814f301 545 [ C(RESULT_MISS) ] = P4_GEN_CACHE_EVENT(P4_EVENT_ITLB_REFERENCE, MISS,
39ef13a4 546 P4_PEBS_METRIC__none),
cb7d6b50
LM
547 },
548 [ C(OP_WRITE) ] = {
549 [ C(RESULT_ACCESS) ] = -1,
550 [ C(RESULT_MISS) ] = -1,
551 },
552 [ C(OP_PREFETCH) ] = {
553 [ C(RESULT_ACCESS) ] = -1,
554 [ C(RESULT_MISS) ] = -1,
555 },
556 },
89d6c0b5
PZ
557 [ C(NODE) ] = {
558 [ C(OP_READ) ] = {
559 [ C(RESULT_ACCESS) ] = -1,
560 [ C(RESULT_MISS) ] = -1,
561 },
562 [ C(OP_WRITE) ] = {
563 [ C(RESULT_ACCESS) ] = -1,
564 [ C(RESULT_MISS) ] = -1,
565 },
566 [ C(OP_PREFETCH) ] = {
567 [ C(RESULT_ACCESS) ] = -1,
568 [ C(RESULT_MISS) ] = -1,
569 },
570 },
cb7d6b50
LM
571};
572
f9129870
CG
573/*
574 * Because of Netburst being quite restricted in now
575 * many same events can run simultaneously, we use
576 * event aliases, ie different events which have the
577 * same functionallity but use non-intersected resources
578 * (ESCR/CCCR/couter registers). This allow us to run
579 * two or more semi-same events together. It is done
580 * transparently to a user space.
581 *
582 * Never set any cusom internal bits such as P4_CONFIG_HT,
583 * P4_CONFIG_ALIASABLE or bits for P4_PEBS_METRIC, they are
584 * either up-to-dated automatically either not appliable
585 * at all.
586 *
587 * And be really carefull choosing aliases!
588 */
589struct p4_event_alias {
590 u64 orig;
591 u64 alter;
592} p4_event_aliases[] = {
593 {
594 /*
595 * Non-halted cycles can be substituted with
596 * non-sleeping cycles (see Intel SDM Vol3b for
597 * details).
598 */
599 .orig =
600 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) |
601 P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)),
602 .alter =
603 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_EXECUTION_EVENT) |
604 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS0)|
605 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS1)|
606 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS2)|
607 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, NBOGUS3)|
608 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS0) |
609 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS1) |
610 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS2) |
611 P4_ESCR_EMASK_BIT(P4_EVENT_EXECUTION_EVENT, BOGUS3))|
612 p4_config_pack_cccr(P4_CCCR_THRESHOLD(15) | P4_CCCR_COMPLEMENT |
613 P4_CCCR_COMPARE),
614 },
615};
616
617static u64 p4_get_alias_event(u64 config)
618{
619 u64 config_match;
620 int i;
621
622 /*
623 * Probably we're lucky and don't have to do
624 * matching over all config bits.
625 */
626 if (!(config & P4_CONFIG_ALIASABLE))
627 return 0;
628
629 config_match = config & P4_CONFIG_EVENT_ALIAS_MASK;
630
631 /*
632 * If an event was previously swapped to the alter config
633 * we should swap it back otherwise contnention on registers
634 * will return back.
635 */
636 for (i = 0; i < ARRAY_SIZE(p4_event_aliases); i++) {
637 if (config_match == p4_event_aliases[i].orig) {
638 config_match = p4_event_aliases[i].alter;
639 break;
640 } else if (config_match == p4_event_aliases[i].alter) {
641 config_match = p4_event_aliases[i].orig;
642 break;
643 }
644 }
645
646 if (i >= ARRAY_SIZE(p4_event_aliases))
647 return 0;
648
649 return config_match |
650 (config & P4_CONFIG_EVENT_ALIAS_IMMUTABLE_BITS);
651}
652
d814f301
CG
653static u64 p4_general_events[PERF_COUNT_HW_MAX] = {
654 /* non-halted CPU clocks */
655 [PERF_COUNT_HW_CPU_CYCLES] =
656 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_GLOBAL_POWER_EVENTS) |
f9129870
CG
657 P4_ESCR_EMASK_BIT(P4_EVENT_GLOBAL_POWER_EVENTS, RUNNING)) |
658 P4_CONFIG_ALIASABLE,
d814f301
CG
659
660 /*
661 * retired instructions
662 * in a sake of simplicity we don't use the FSB tagging
663 */
664 [PERF_COUNT_HW_INSTRUCTIONS] =
665 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_INSTR_RETIRED) |
666 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, NBOGUSNTAG) |
667 P4_ESCR_EMASK_BIT(P4_EVENT_INSTR_RETIRED, BOGUSNTAG)),
668
669 /* cache hits */
670 [PERF_COUNT_HW_CACHE_REFERENCES] =
671 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) |
672 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITS) |
673 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITE) |
674 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_HITM) |
675 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITS) |
676 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITE) |
677 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_HITM)),
678
679 /* cache misses */
680 [PERF_COUNT_HW_CACHE_MISSES] =
681 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_BSQ_CACHE_REFERENCE) |
682 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_2ndL_MISS) |
683 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, RD_3rdL_MISS) |
684 P4_ESCR_EMASK_BIT(P4_EVENT_BSQ_CACHE_REFERENCE, WR_2ndL_MISS)),
685
686 /* branch instructions retired */
687 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] =
688 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_RETIRED_BRANCH_TYPE) |
689 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CONDITIONAL) |
690 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, CALL) |
691 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, RETURN) |
692 P4_ESCR_EMASK_BIT(P4_EVENT_RETIRED_BRANCH_TYPE, INDIRECT)),
693
694 /* mispredicted branches retired */
695 [PERF_COUNT_HW_BRANCH_MISSES] =
696 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_MISPRED_BRANCH_RETIRED) |
697 P4_ESCR_EMASK_BIT(P4_EVENT_MISPRED_BRANCH_RETIRED, NBOGUS)),
698
699 /* bus ready clocks (cpu is driving #DRDY_DRV\#DRDY_OWN): */
700 [PERF_COUNT_HW_BUS_CYCLES] =
701 p4_config_pack_escr(P4_ESCR_EVENT(P4_EVENT_FSB_DATA_ACTIVITY) |
702 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_DRV) |
703 P4_ESCR_EMASK_BIT(P4_EVENT_FSB_DATA_ACTIVITY, DRDY_OWN)) |
704 p4_config_pack_cccr(P4_CCCR_EDGE | P4_CCCR_COMPARE),
a072738e
CG
705};
706
d814f301
CG
707static struct p4_event_bind *p4_config_get_bind(u64 config)
708{
709 unsigned int evnt = p4_config_unpack_event(config);
710 struct p4_event_bind *bind = NULL;
711
712 if (evnt < ARRAY_SIZE(p4_event_bind_map))
713 bind = &p4_event_bind_map[evnt];
714
715 return bind;
716}
717
a072738e
CG
718static u64 p4_pmu_event_map(int hw_event)
719{
d814f301
CG
720 struct p4_event_bind *bind;
721 unsigned int esel;
a072738e
CG
722 u64 config;
723
d814f301
CG
724 config = p4_general_events[hw_event];
725 bind = p4_config_get_bind(config);
726 esel = P4_OPCODE_ESEL(bind->opcode);
727 config |= p4_config_pack_cccr(P4_CCCR_ESEL(esel));
a072738e 728
a072738e
CG
729 return config;
730}
731
c9cf4a01
CG
732/* check cpu model specifics */
733static bool p4_event_match_cpu_model(unsigned int event_idx)
734{
735 /* INSTR_COMPLETED event only exist for model 3, 4, 6 (Prescott) */
736 if (event_idx == P4_EVENT_INSTR_COMPLETED) {
737 if (boot_cpu_data.x86_model != 3 &&
738 boot_cpu_data.x86_model != 4 &&
739 boot_cpu_data.x86_model != 6)
740 return false;
741 }
742
743 /*
744 * For info
745 * - IQ_ESCR0, IQ_ESCR1 only for models 1 and 2
746 */
747
748 return true;
749}
750
39ef13a4
CG
751static int p4_validate_raw_event(struct perf_event *event)
752{
c9cf4a01 753 unsigned int v, emask;
39ef13a4 754
c9cf4a01 755 /* User data may have out-of-bound event index */
39ef13a4 756 v = p4_config_unpack_event(event->attr.config);
c9cf4a01
CG
757 if (v >= ARRAY_SIZE(p4_event_bind_map))
758 return -EINVAL;
759
760 /* It may be unsupported: */
761 if (!p4_event_match_cpu_model(v))
39ef13a4 762 return -EINVAL;
c9cf4a01
CG
763
764 /*
765 * NOTE: P4_CCCR_THREAD_ANY has not the same meaning as
766 * in Architectural Performance Monitoring, it means not
767 * on _which_ logical cpu to count but rather _when_, ie it
768 * depends on logical cpu state -- count event if one cpu active,
769 * none, both or any, so we just allow user to pass any value
770 * desired.
771 *
772 * In turn we always set Tx_OS/Tx_USR bits bound to logical
773 * cpu without their propagation to another cpu
774 */
775
776 /*
0d2eb44f 777 * if an event is shared across the logical threads
c9cf4a01
CG
778 * the user needs special permissions to be able to use it
779 */
d038b12c 780 if (p4_ht_active() && p4_event_bind_map[v].shared) {
c9cf4a01
CG
781 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
782 return -EACCES;
39ef13a4
CG
783 }
784
c9cf4a01
CG
785 /* ESCR EventMask bits may be invalid */
786 emask = p4_config_unpack_escr(event->attr.config) & P4_ESCR_EVENTMASK_MASK;
787 if (emask & ~p4_event_bind_map[v].escr_emask)
788 return -EINVAL;
789
39ef13a4 790 /*
c9cf4a01 791 * it may have some invalid PEBS bits
39ef13a4 792 */
c9cf4a01 793 if (p4_config_pebs_has(event->attr.config, P4_PEBS_CONFIG_ENABLE))
39ef13a4 794 return -EINVAL;
c9cf4a01 795
39ef13a4 796 v = p4_config_unpack_metric(event->attr.config);
c9cf4a01 797 if (v >= ARRAY_SIZE(p4_pebs_bind_map))
39ef13a4 798 return -EINVAL;
39ef13a4
CG
799
800 return 0;
801}
802
b4cdc5c2 803static int p4_hw_config(struct perf_event *event)
a072738e 804{
137351e0
CG
805 int cpu = get_cpu();
806 int rc = 0;
d814f301 807 u32 escr, cccr;
a072738e
CG
808
809 /*
810 * the reason we use cpu that early is that: if we get scheduled
811 * first time on the same cpu -- we will not need swap thread
812 * specific flags in config (and will save some cpu cycles)
813 */
814
d814f301 815 cccr = p4_default_cccr_conf(cpu);
b4cdc5c2
PZ
816 escr = p4_default_escr_conf(cpu, event->attr.exclude_kernel,
817 event->attr.exclude_user);
818 event->hw.config = p4_config_pack_escr(escr) |
819 p4_config_pack_cccr(cccr);
a072738e 820
cb7d6b50 821 if (p4_ht_active() && p4_ht_thread(cpu))
b4cdc5c2
PZ
822 event->hw.config = p4_set_ht_bit(event->hw.config);
823
de902d96 824 if (event->attr.type == PERF_TYPE_RAW) {
d038b12c
SE
825 struct p4_event_bind *bind;
826 unsigned int esel;
c9cf4a01
CG
827 /*
828 * Clear bits we reserve to be managed by kernel itself
829 * and never allowed from a user space
830 */
831 event->attr.config &= P4_CONFIG_MASK;
832
39ef13a4
CG
833 rc = p4_validate_raw_event(event);
834 if (rc)
c7993165 835 goto out;
c7993165 836
de902d96 837 /*
39ef13a4
CG
838 * Note that for RAW events we allow user to use P4_CCCR_RESERVED
839 * bits since we keep additional info here (for cache events and etc)
de902d96 840 */
c9cf4a01 841 event->hw.config |= event->attr.config;
d038b12c
SE
842 bind = p4_config_get_bind(event->attr.config);
843 if (!bind) {
844 rc = -EINVAL;
845 goto out;
846 }
847 esel = P4_OPCODE_ESEL(bind->opcode);
848 event->hw.config |= p4_config_pack_cccr(P4_CCCR_ESEL(esel));
de902d96 849 }
cb7d6b50 850
137351e0 851 rc = x86_setup_perfctr(event);
c7993165 852out:
137351e0 853 put_cpu();
137351e0 854 return rc;
a072738e
CG
855}
856
0db1a7bc 857static inline int p4_pmu_clear_cccr_ovf(struct hw_perf_event *hwc)
a072738e 858{
047a3772 859 u64 v;
a072738e 860
047a3772 861 /* an official way for overflow indication */
73d6e522 862 rdmsrl(hwc->config_base, v);
047a3772 863 if (v & P4_CCCR_OVF) {
73d6e522 864 wrmsrl(hwc->config_base, v & ~P4_CCCR_OVF);
047a3772 865 return 1;
a072738e 866 }
0db1a7bc 867
7d44ec19
CG
868 /*
869 * In some circumstances the overflow might issue an NMI but did
870 * not set P4_CCCR_OVF bit. Because a counter holds a negative value
871 * we simply check for high bit being set, if it's cleared it means
872 * the counter has reached zero value and continued counting before
873 * real NMI signal was received:
874 */
242214f9 875 rdmsrl(hwc->event_base, v);
7d44ec19 876 if (!(v & ARCH_P4_UNFLAGGED_BIT))
047a3772
CG
877 return 1;
878
879 return 0;
a072738e
CG
880}
881
39ef13a4
CG
882static void p4_pmu_disable_pebs(void)
883{
884 /*
885 * FIXME
886 *
887 * It's still allowed that two threads setup same cache
888 * events so we can't simply clear metrics until we knew
0d2eb44f 889 * no one is depending on us, so we need kind of counter
39ef13a4
CG
890 * for "ReplayEvent" users.
891 *
892 * What is more complex -- RAW events, if user (for some
893 * reason) will pass some cache event metric with improper
894 * event opcode -- it's fine from hardware point of view
0d2eb44f 895 * but completely nonsense from "meaning" of such action.
39ef13a4
CG
896 *
897 * So at moment let leave metrics turned on forever -- it's
898 * ok for now but need to be revisited!
899 *
900 * (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)0);
901 * (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)0);
902 */
903}
904
a072738e
CG
905static inline void p4_pmu_disable_event(struct perf_event *event)
906{
907 struct hw_perf_event *hwc = &event->hw;
908
909 /*
910 * If event gets disabled while counter is in overflowed
911 * state we need to clear P4_CCCR_OVF, otherwise interrupt get
912 * asserted again and again
913 */
73d6e522 914 (void)checking_wrmsrl(hwc->config_base,
a072738e 915 (u64)(p4_config_unpack_cccr(hwc->config)) &
d814f301 916 ~P4_CCCR_ENABLE & ~P4_CCCR_OVF & ~P4_CCCR_RESERVED);
a072738e
CG
917}
918
919static void p4_pmu_disable_all(void)
920{
921 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
922 int idx;
923
948b1bb8 924 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
a072738e
CG
925 struct perf_event *event = cpuc->events[idx];
926 if (!test_bit(idx, cpuc->active_mask))
927 continue;
928 p4_pmu_disable_event(event);
929 }
39ef13a4
CG
930
931 p4_pmu_disable_pebs();
932}
933
934/* configuration must be valid */
935static void p4_pmu_enable_pebs(u64 config)
936{
937 struct p4_pebs_bind *bind;
938 unsigned int idx;
939
940 BUILD_BUG_ON(P4_PEBS_METRIC__max > P4_PEBS_CONFIG_METRIC_MASK);
941
942 idx = p4_config_unpack_metric(config);
943 if (idx == P4_PEBS_METRIC__none)
944 return;
945
946 bind = &p4_pebs_bind_map[idx];
947
948 (void)checking_wrmsrl(MSR_IA32_PEBS_ENABLE, (u64)bind->metric_pebs);
949 (void)checking_wrmsrl(MSR_P4_PEBS_MATRIX_VERT, (u64)bind->metric_vert);
a072738e
CG
950}
951
952static void p4_pmu_enable_event(struct perf_event *event)
953{
954 struct hw_perf_event *hwc = &event->hw;
955 int thread = p4_ht_config_thread(hwc->config);
956 u64 escr_conf = p4_config_unpack_escr(p4_clear_ht_bit(hwc->config));
d814f301 957 unsigned int idx = p4_config_unpack_event(hwc->config);
d814f301 958 struct p4_event_bind *bind;
d814f301 959 u64 escr_addr, cccr;
a072738e 960
d814f301
CG
961 bind = &p4_event_bind_map[idx];
962 escr_addr = (u64)bind->escr_msr[thread];
a072738e
CG
963
964 /*
965 * - we dont support cascaded counters yet
966 * - and counter 1 is broken (erratum)
967 */
968 WARN_ON_ONCE(p4_is_event_cascaded(hwc->config));
969 WARN_ON_ONCE(hwc->idx == 1);
970
d814f301
CG
971 /* we need a real Event value */
972 escr_conf &= ~P4_ESCR_EVENT_MASK;
973 escr_conf |= P4_ESCR_EVENT(P4_OPCODE_EVNT(bind->opcode));
974
975 cccr = p4_config_unpack_cccr(hwc->config);
976
977 /*
39ef13a4
CG
978 * it could be Cache event so we need to write metrics
979 * into additional MSRs
d814f301 980 */
39ef13a4 981 p4_pmu_enable_pebs(hwc->config);
d814f301
CG
982
983 (void)checking_wrmsrl(escr_addr, escr_conf);
73d6e522 984 (void)checking_wrmsrl(hwc->config_base,
d814f301 985 (cccr & ~P4_CCCR_RESERVED) | P4_CCCR_ENABLE);
a072738e
CG
986}
987
11164cd4 988static void p4_pmu_enable_all(int added)
a072738e
CG
989{
990 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
991 int idx;
992
948b1bb8 993 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
a072738e
CG
994 struct perf_event *event = cpuc->events[idx];
995 if (!test_bit(idx, cpuc->active_mask))
996 continue;
997 p4_pmu_enable_event(event);
998 }
999}
1000
1001static int p4_pmu_handle_irq(struct pt_regs *regs)
1002{
1003 struct perf_sample_data data;
1004 struct cpu_hw_events *cpuc;
1005 struct perf_event *event;
1006 struct hw_perf_event *hwc;
1007 int idx, handled = 0;
1008 u64 val;
1009
103b3934 1010 perf_sample_data_init(&data, 0);
a072738e
CG
1011
1012 cpuc = &__get_cpu_var(cpu_hw_events);
1013
948b1bb8 1014 for (idx = 0; idx < x86_pmu.num_counters; idx++) {
1c250d70 1015 int overflow;
a072738e 1016
03e22198
CG
1017 if (!test_bit(idx, cpuc->active_mask)) {
1018 /* catch in-flight IRQs */
1019 if (__test_and_clear_bit(idx, cpuc->running))
1020 handled++;
a072738e 1021 continue;
03e22198 1022 }
a072738e
CG
1023
1024 event = cpuc->events[idx];
1025 hwc = &event->hw;
1026
1027 WARN_ON_ONCE(hwc->idx != idx);
1028
0db1a7bc 1029 /* it might be unflagged overflow */
1c250d70 1030 overflow = p4_pmu_clear_cccr_ovf(hwc);
a072738e
CG
1031
1032 val = x86_perf_event_update(event);
1c250d70 1033 if (!overflow && (val & (1ULL << (x86_pmu.cntval_bits - 1))))
a072738e
CG
1034 continue;
1035
1c250d70
CG
1036 handled += overflow;
1037
0db1a7bc
CG
1038 /* event overflow for sure */
1039 data.period = event->hw.last_period;
a072738e
CG
1040
1041 if (!x86_perf_event_set_period(event))
1042 continue;
a8b0ca17 1043 if (perf_event_overflow(event, &data, regs))
1ea5a6af 1044 x86_pmu_stop(event, 0);
a072738e
CG
1045 }
1046
2bce5dac 1047 if (handled)
a072738e 1048 inc_irq_stat(apic_perf_irqs);
2bce5dac
DZ
1049
1050 /*
1051 * When dealing with the unmasking of the LVTPC on P4 perf hw, it has
1052 * been observed that the OVF bit flag has to be cleared first _before_
1053 * the LVTPC can be unmasked.
1054 *
1055 * The reason is the NMI line will continue to be asserted while the OVF
1056 * bit is set. This causes a second NMI to generate if the LVTPC is
1057 * unmasked before the OVF bit is cleared, leading to unknown NMI
1058 * messages.
1059 */
1060 apic_write(APIC_LVTPC, APIC_DM_NMI);
a072738e 1061
de725dec 1062 return handled;
a072738e
CG
1063}
1064
1065/*
1066 * swap thread specific fields according to a thread
1067 * we are going to run on
1068 */
1069static void p4_pmu_swap_config_ts(struct hw_perf_event *hwc, int cpu)
1070{
1071 u32 escr, cccr;
1072
1073 /*
1074 * we either lucky and continue on same cpu or no HT support
1075 */
1076 if (!p4_should_swap_ts(hwc->config, cpu))
1077 return;
1078
1079 /*
1080 * the event is migrated from an another logical
1081 * cpu, so we need to swap thread specific flags
1082 */
1083
1084 escr = p4_config_unpack_escr(hwc->config);
1085 cccr = p4_config_unpack_cccr(hwc->config);
1086
1087 if (p4_ht_thread(cpu)) {
1088 cccr &= ~P4_CCCR_OVF_PMI_T0;
1089 cccr |= P4_CCCR_OVF_PMI_T1;
d814f301
CG
1090 if (escr & P4_ESCR_T0_OS) {
1091 escr &= ~P4_ESCR_T0_OS;
1092 escr |= P4_ESCR_T1_OS;
a072738e 1093 }
d814f301
CG
1094 if (escr & P4_ESCR_T0_USR) {
1095 escr &= ~P4_ESCR_T0_USR;
1096 escr |= P4_ESCR_T1_USR;
a072738e
CG
1097 }
1098 hwc->config = p4_config_pack_escr(escr);
1099 hwc->config |= p4_config_pack_cccr(cccr);
1100 hwc->config |= P4_CONFIG_HT;
1101 } else {
1102 cccr &= ~P4_CCCR_OVF_PMI_T1;
1103 cccr |= P4_CCCR_OVF_PMI_T0;
d814f301
CG
1104 if (escr & P4_ESCR_T1_OS) {
1105 escr &= ~P4_ESCR_T1_OS;
1106 escr |= P4_ESCR_T0_OS;
a072738e 1107 }
d814f301
CG
1108 if (escr & P4_ESCR_T1_USR) {
1109 escr &= ~P4_ESCR_T1_USR;
1110 escr |= P4_ESCR_T0_USR;
a072738e
CG
1111 }
1112 hwc->config = p4_config_pack_escr(escr);
1113 hwc->config |= p4_config_pack_cccr(cccr);
1114 hwc->config &= ~P4_CONFIG_HT;
1115 }
1116}
1117
72001990
CG
1118/*
1119 * ESCR address hashing is tricky, ESCRs are not sequential
623aab89 1120 * in memory but all starts from MSR_P4_BSU_ESCR0 (0x03a0) and
72001990
CG
1121 * the metric between any ESCRs is laid in range [0xa0,0xe1]
1122 *
1123 * so we make ~70% filled hashtable
1124 */
1125
1126#define P4_ESCR_MSR_BASE 0x000003a0
1127#define P4_ESCR_MSR_MAX 0x000003e1
1128#define P4_ESCR_MSR_TABLE_SIZE (P4_ESCR_MSR_MAX - P4_ESCR_MSR_BASE + 1)
1129#define P4_ESCR_MSR_IDX(msr) (msr - P4_ESCR_MSR_BASE)
1130#define P4_ESCR_MSR_TABLE_ENTRY(msr) [P4_ESCR_MSR_IDX(msr)] = msr
1131
1132static const unsigned int p4_escr_table[P4_ESCR_MSR_TABLE_SIZE] = {
1133 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR0),
1134 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ALF_ESCR1),
1135 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR0),
1136 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BPU_ESCR1),
1137 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR0),
1138 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_BSU_ESCR1),
1139 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR0),
1140 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR1),
1141 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR2),
1142 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR3),
1143 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR4),
1144 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_CRU_ESCR5),
1145 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR0),
1146 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_DAC_ESCR1),
1147 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR0),
1148 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FIRM_ESCR1),
1149 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR0),
1150 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FLAME_ESCR1),
1151 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR0),
1152 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_FSB_ESCR1),
1153 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR0),
1154 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IQ_ESCR1),
1155 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR0),
1156 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IS_ESCR1),
1157 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR0),
1158 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_ITLB_ESCR1),
1159 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR0),
1160 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_IX_ESCR1),
1161 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR0),
1162 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MOB_ESCR1),
1163 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR0),
1164 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_MS_ESCR1),
1165 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR0),
1166 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_PMH_ESCR1),
1167 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR0),
1168 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_RAT_ESCR1),
1169 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR0),
1170 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SAAT_ESCR1),
1171 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR0),
1172 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_SSU_ESCR1),
1173 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR0),
1174 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TBPU_ESCR1),
1175 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR0),
1176 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_TC_ESCR1),
1177 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR0),
1178 P4_ESCR_MSR_TABLE_ENTRY(MSR_P4_U2L_ESCR1),
a072738e
CG
1179};
1180
1181static int p4_get_escr_idx(unsigned int addr)
1182{
72001990 1183 unsigned int idx = P4_ESCR_MSR_IDX(addr);
a072738e 1184
623aab89
CG
1185 if (unlikely(idx >= P4_ESCR_MSR_TABLE_SIZE ||
1186 !p4_escr_table[idx] ||
1187 p4_escr_table[idx] != addr)) {
72001990
CG
1188 WARN_ONCE(1, "P4 PMU: Wrong address passed: %x\n", addr);
1189 return -1;
a072738e
CG
1190 }
1191
72001990 1192 return idx;
a072738e
CG
1193}
1194
d814f301
CG
1195static int p4_next_cntr(int thread, unsigned long *used_mask,
1196 struct p4_event_bind *bind)
1197{
1ff3d7d7 1198 int i, j;
d814f301
CG
1199
1200 for (i = 0; i < P4_CNTR_LIMIT; i++) {
1ff3d7d7
CG
1201 j = bind->cntr[thread][i];
1202 if (j != -1 && !test_bit(j, used_mask))
d814f301
CG
1203 return j;
1204 }
1205
1206 return -1;
1207}
1208
a072738e
CG
1209static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
1210{
1211 unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
72001990 1212 unsigned long escr_mask[BITS_TO_LONGS(P4_ESCR_MSR_TABLE_SIZE)];
9d36dfcf 1213 int cpu = smp_processor_id();
d814f301
CG
1214 struct hw_perf_event *hwc;
1215 struct p4_event_bind *bind;
1216 unsigned int i, thread, num;
1217 int cntr_idx, escr_idx;
f9129870
CG
1218 u64 config_alias;
1219 int pass;
a072738e
CG
1220
1221 bitmap_zero(used_mask, X86_PMC_IDX_MAX);
72001990 1222 bitmap_zero(escr_mask, P4_ESCR_MSR_TABLE_SIZE);
a072738e 1223
a072738e 1224 for (i = 0, num = n; i < n; i++, num--) {
d814f301 1225
a072738e 1226 hwc = &cpuc->event_list[i]->hw;
a072738e 1227 thread = p4_ht_thread(cpu);
f9129870
CG
1228 pass = 0;
1229
1230again:
1231 /*
1232 * Aliases are swappable so we may hit circular
1233 * lock if both original config and alias need
1234 * resources (MSR registers) which already busy.
1235 */
1236 if (pass > 2)
1237 goto done;
1238
d814f301
CG
1239 bind = p4_config_get_bind(hwc->config);
1240 escr_idx = p4_get_escr_idx(bind->escr_msr[thread]);
72001990
CG
1241 if (unlikely(escr_idx == -1))
1242 goto done;
a072738e 1243
a072738e 1244 if (hwc->idx != -1 && !p4_should_swap_ts(hwc->config, cpu)) {
d814f301 1245 cntr_idx = hwc->idx;
a072738e
CG
1246 if (assign)
1247 assign[i] = hwc->idx;
a072738e
CG
1248 goto reserve;
1249 }
1250
d814f301 1251 cntr_idx = p4_next_cntr(thread, used_mask, bind);
f9129870
CG
1252 if (cntr_idx == -1 || test_bit(escr_idx, escr_mask)) {
1253 /*
1254 * Probably an event alias is still available.
1255 */
1256 config_alias = p4_get_alias_event(hwc->config);
1257 if (!config_alias)
1258 goto done;
1259 hwc->config = config_alias;
1260 pass++;
1261 goto again;
1262 }
a072738e 1263
a072738e 1264 p4_pmu_swap_config_ts(hwc, cpu);
d814f301
CG
1265 if (assign)
1266 assign[i] = cntr_idx;
a072738e 1267reserve:
d814f301 1268 set_bit(cntr_idx, used_mask);
a072738e
CG
1269 set_bit(escr_idx, escr_mask);
1270 }
1271
1272done:
1273 return num ? -ENOSPC : 0;
1274}
1275
caaa8be3 1276static __initconst const struct x86_pmu p4_pmu = {
a072738e
CG
1277 .name = "Netburst P4/Xeon",
1278 .handle_irq = p4_pmu_handle_irq,
1279 .disable_all = p4_pmu_disable_all,
1280 .enable_all = p4_pmu_enable_all,
1281 .enable = p4_pmu_enable_event,
1282 .disable = p4_pmu_disable_event,
1283 .eventsel = MSR_P4_BPU_CCCR0,
1284 .perfctr = MSR_P4_BPU_PERFCTR0,
1285 .event_map = p4_pmu_event_map,
d814f301 1286 .max_events = ARRAY_SIZE(p4_general_events),
a072738e
CG
1287 .get_event_constraints = x86_get_event_constraints,
1288 /*
1289 * IF HT disabled we may need to use all
1290 * ARCH_P4_MAX_CCCR counters simulaneously
1291 * though leave it restricted at moment assuming
1292 * HT is on
1293 */
948b1bb8 1294 .num_counters = ARCH_P4_MAX_CCCR,
a072738e 1295 .apic = 1,
047a3772
CG
1296 .cntval_bits = ARCH_P4_CNTRVAL_BITS,
1297 .cntval_mask = ARCH_P4_CNTRVAL_MASK,
1298 .max_period = (1ULL << (ARCH_P4_CNTRVAL_BITS - 1)) - 1,
a072738e
CG
1299 .hw_config = p4_hw_config,
1300 .schedule_events = p4_pmu_schedule_events,
68aa00ac
CG
1301 /*
1302 * This handles erratum N15 in intel doc 249199-029,
1303 * the counter may not be updated correctly on write
1304 * so we need a second write operation to do the trick
1305 * (the official workaround didn't work)
1306 *
1307 * the former idea is taken from OProfile code
1308 */
1309 .perfctr_second_write = 1,
a072738e
CG
1310};
1311
1312static __init int p4_pmu_init(void)
1313{
1314 unsigned int low, high;
1315
fa7b6947 1316 /* If we get stripped -- indexing fails */
a072738e
CG
1317 BUILD_BUG_ON(ARCH_P4_MAX_CCCR > X86_PMC_MAX_GENERIC);
1318
1319 rdmsr(MSR_IA32_MISC_ENABLE, low, high);
1320 if (!(low & (1 << 7))) {
1321 pr_cont("unsupported Netburst CPU model %d ",
1322 boot_cpu_data.x86_model);
1323 return -ENODEV;
1324 }
1325
cb7d6b50 1326 memcpy(hw_cache_event_ids, p4_hw_cache_event_ids,
d814f301 1327 sizeof(hw_cache_event_ids));
cb7d6b50 1328
a072738e
CG
1329 pr_cont("Netburst events, ");
1330
1331 x86_pmu = p4_pmu;
1332
1333 return 0;
1334}
1335
1336#endif /* CONFIG_CPU_SUP_INTEL */
This page took 0.440498 seconds and 5 git commands to generate.