]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4util/include/ARCH-x86/perform.h
147fc7f090ea2edd30d23087fda3d9bb4b8c619c
[l4.git] / l4 / pkg / l4util / include / ARCH-x86 / perform.h
1 /**
2  * \file
3  * \brief Perfomance Monitoring using P5/P6 Measurement Counters.
4  *
5  * Define either CPU_PENTIUM or CPU_P6
6  */
7 /*
8  * (c) 2008-2009 Technische Universität Dresden
9  * This file is part of TUD:OS and distributed under the terms of the
10  * GNU Lesser General Public License 2.1.
11  * Please see the COPYING-LGPL-2.1 file for details.
12  */
13
14 #ifndef __L4UTIL_PERFORM_H
15 #define __L4UTIL_PERFORM_H
16
17 #include <l4/sys/types.h>
18 #include <l4/sys/compiler.h>
19
20 EXTERN_C_BEGIN
21
22 extern const char*strp6pmc_event(l4_uint32_t event);
23
24 #ifndef CONFIG_PERFORM_ONLY_PROTOTYPES
25
26 #if ! (defined CPU_PENTIUM  ^ defined CPU_P6 ^ defined CPU_K7)
27
28 #error You must define your target architecture.
29 #error Define EITHER CPU_PENTIUM for Intel Pentium or CPU_P6 for Intel PPro/PII/PIII.
30
31 #else
32
33 /* P5/P6/K7 section */
34
35 /* Makros for access to model specific registers (MSR) */
36
37 /* Write the 64-Bit Model Specific Register. First argument is the register,
38    second the 64-Bit value. This can only be called at priviledge level 0.
39    With L4, the kernel emulates the WRMSR when calling in PL 3.
40    */
41 static inline void l4_i586_wrmsr(unsigned reg,unsigned long long*val){
42   unsigned long dummyeax, dummyecx, dummyedx;
43
44   asm volatile(
45         ".byte 0xf; .byte 0x30\n"       /* wrmsr */
46         : "=a" (dummyeax), "=d" (dummyedx), "=c" (dummyecx)
47         : "2" (reg), "0" (*(unsigned *)val), "1" (*((unsigned *)val+1))
48         );
49 }
50
51 /* Read the 64-Bit Model Specific Register. First argument is the register,
52    second the address to a 64-Bit value. This can only be called at
53    priviledge level 0.  With L4, the kernel emulates the RDMSR when calling
54    in PL 3.
55    */
56 static inline void l4_i586_rdmsr(unsigned reg,unsigned long long*val){
57   unsigned dummy;
58
59   asm volatile(
60         ".byte 0xf; .byte 0x32\n"       /* rdmsr */
61         : "=a" (*(unsigned *)val), "=d" (*((unsigned *)val+1)), "=c" (dummy)
62         : "2" (reg)
63         );
64 }
65
66
67 #ifdef CPU_PENTIUM
68 /* Pentium section */
69
70 /* functions and events defined here are only usable at Pentium
71    Processors. P6 architecture does NOT support this kind of measuring and
72    these events. P6 architecture has its own counters and its own events.
73    See P6-section for details. */
74
75 /* from l4linux/arch/l4-i386/include/perform.h */
76
77 static inline void 
78 l4_i586_reset_event_counter(void){
79    asm volatile("xor %%eax, %%eax\n"
80                 "xor %%edx, %%edx\n"
81                 "movl $0x12, %%ecx\n"
82                 ".byte 0x0f, 0x30\n"
83                 "movl $0x13, %%ecx\n"
84                 ".byte 0x0f, 0x30\n"
85                 : : : "cx", "ax", "dx" 
86                 );
87 };
88
89 static inline void
90 l4_i586_read_event_counter_long(long long *counter0, long long *counter1)
91 {
92   asm volatile(
93                /*              "movl    $0, %%eax\n"
94                "movl    $0x11, %%ecx\n"
95                ".byte 0x0f, 0x30\n" *//* stop event counting */
96                "movl $0x12, %%ecx\n"
97                ".byte 0x0f, 0x32\n"
98                "movl %%eax, (%%ebx)\n"
99                "movl %%edx, 4(%%ebx)\n"
100                "movl $0x13, %%ecx\n"
101                ".byte 0x0f, 0x32\n"
102                "movl %%eax, (%%esi)\n"
103                "movl %%edx, 4(%%esi)\n"
104                : /* no output */
105                : "b" (counter0), "S" (counter1)
106                : "ax", "cx", "dx"
107                );
108 }
109
110 static inline void
111 l4_i586_read_event_counter(int *counter0, int *counter1)
112 {
113   asm volatile("pushl   %%edx\n"
114                ".byte 0x0f, 0x30\n"
115                "movl $0x12, %%ecx\n"
116                ".byte 0x0f, 0x32\n"
117                "movl %%eax, %%ebx\n"
118                "movl $0x13, %%ecx\n"
119                ".byte 0x0f, 0x32\n"
120                "popl    %%edx\n"
121                : "=b" (*counter0), "=a" (*counter1)
122                : "1" (0), "c" (0x11)
123                );
124 }
125
126 static inline void 
127 l4_i586_select_event(int event0, int event1)
128 {
129    asm volatile(".byte 0x0f, 0x30\n"
130                 :
131                 :
132                 "a" (event0 + (event1 << 16)),
133                 "d" (0),
134                 "c" (0x11)
135                 );
136 };
137
138 #define P5_RD_MISS          0x003       /* 000011B */
139 #define P5_WR_MISS          0x008       /* 000100B */
140 #define P5_RW_MISS          0x029       /* 101001B */
141 #define P5_EX_MISS          0x00e       /* 001110B */
142
143 #define P5_D_WBACK          0x006       /* 000110B */
144
145 #define P5_RW_TLB           0x002       /* 00010B */
146 #define P5_EX_TLB           0x00d       /* 01101B */
147
148 #define P5_A_STALL          0x01f       /* 11111B */
149 #define P5_W_STALL          0x019       /* 11001B */
150 #define P5_R_STALL          0x01a       /* 11010B */
151 #define P5_X_STALL          0x01b       /* 11011B */
152
153 #define P5_AGI_STALL        0x01f       /* 11111B */
154
155 #define P5_PIPLINE_FLUSH    0x015       /* 10101B */
156
157 #define P5_NON_CACHE_RD     0x01e       /* 11110B */
158 #define P5_NCACHE_REFS      0x01e       /* 11110B */
159 #define P5_LOCKED_BUS       0x01c       /* 11100B */
160
161 #define P5_MEM2PIPE         0x009       /* 01001B */
162 #define P5_BANK_CONF        0x00a       /* 01010B */
163
164
165 #define P5_INSTRS_EX        0x016       /* 10110B */
166 #define P5_INSTRS_EX_V      0x017       /* 10111B */
167
168
169 #define P5_CNT_NOTHING      (0x00 << 6) /* 00B << 6 */
170 #define P5_CNT_EVENT_PL0    (0x01 << 6) /* 01B << 6 */
171 #define P5_CNT_EVENT_PL3    (0x02 << 6) /* 10B << 6 */
172 #define P5_CNT_EVENT        (0x03 << 6) /* 11B << 6 */
173 #define P5_CNT_CLOCKS_PL0   (0x05 << 6) /* 101B << 6 */
174 #define P5_CNT_CLOCKS_PL3   (0x06 << 6) /* 110B << 6 */
175 #define P5_CNT_CLOCKS       (0x07 << 6) /* 111B << 6 */
176
177
178 #else
179 #if defined CPU_P6
180 /* PPro/PII/PIII section */
181
182 /*-
183  * Copyright (c) 1997 The President and Fellows of Harvard College.
184  * All rights reserved.
185  * Copyright (c) 1997 Aaron B. Brown.
186  *
187  * Redistribution and use in source and binary forms, with or without
188  * modification, are permitted provided that the following conditions
189  * are met:
190  * 1. Redistributions of source code must retain the above copyright
191  *    notice, this list of conditions and the following disclaimer.
192  * 2. Redistributions in binary form must reproduce the above copyright
193  *    notice, this list of conditions and the following disclaimer in the
194  *    documentation and/or other materials provided with the distribution.
195  * 3. All advertising materials mentioning features or use of this software
196  *    must display the following acknowledgement:
197  *      This product includes software developed by Harvard University
198  *      and its contributors.
199  * 4. Neither the name of the University nor the names of its contributors
200  *    may be used to endorse or promote products derived from this software
201  *    without specific prior written permission.
202  *
203  * THIS SOFTWARE IS PROVIDED BY HARVARD AND CONTRIBUTORS ``AS IS'' AND
204  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
205  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206  * ARE DISCLAIMED.  IN NO EVENT SHALL HARVARD UNIVERSITY OR CONTRIBUTORS BE
207  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
208  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
209  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
210  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
211  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
212  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
213  * POSSIBILITY OF SUCH DAMAGE.
214  */
215
216 /*********************************************************************
217  ** Symbolic names for counter numbers (used in select_p6counter()) **
218  *********************************************************************
219  *
220  * These correspond in order to the Pentium Pro counters. Add new counters at
221  * the end. These agree with the mneumonics in the Pentium Pro Family
222  * Developer's Manual, vol 3.
223  *
224  * Those events marked with a $ require a MESI unit field; those marked with
225  * a @ require a self/any unit field. Those marked with a 0 are only supported
226  * in counter 0; those marked with 1 are only supported in counter 1.
227  */
228
229 /* Data cache unit */
230 #define P6_DATA_MEM_REFS        0x43    /* total memory refs */
231 #define P6_DCU_LINES_IN         0x45    /* all lines allocated in cache unit */
232 #define P6_DCU_M_LINES_IN       0x46    /* M lines allocated in cache unit */
233 #define P6_DCU_M_LINES_OUT      0x47    /* M lines evicted from cache */
234 #define P6_DCU_MISS_OUTSTANDING 0x48    /* #cycles a miss is outstanding */
235
236 /* Instruction fetch unit */
237 #define P6_IFU_IFETCH           0x80    /* instruction fetches */
238 #define P6_IFU_IFETCH_MISS      0x81    /* instruction fetch misses */
239 #define P6_ITLB_MISS            0x85    /* ITLB misses */
240 #define P6_IFU_MEM_STALL        0x86    /* number of cycles IFU is stalled */
241 #define P6_ILD_STALL            0x87    /* #stalls in instr length decode */
242
243 /* L2 Cache */
244 #define P6_L2_IFETCH            0x28    /* ($) l2 ifetches */
245 #define P6_L2_LD                0x29    /* ($) l2 data loads */
246 #define P6_L2_ST                0x2a    /* ($) l2 data stores */
247 #define P6_L2_LINES_IN          0x24    /* lines allocated in l2 */
248 #define P6_L2_LINES_OUT         0x26    /* lines removed from l2 */
249 #define P6_L2_M_LINES_INM       0x25    /* modified lines allocated in L2 */
250 #define P6_L2_M_LINES_OUTM      0x27    /* modified lines removed from L2 */
251 #define P6_L2_RQSTS             0x2e    /* ($) number of l2 requests */
252 #define P6_L2_ADS               0x21    /* number of l2 addr strobes */
253 #define P6_L2_DBUS_BUSY         0x22    /* number of data bus busy cycles */
254 #define P6_L2_DBUS_BUSY_RD      0x23    /* #bus cycles xferring l2->cpu */
255
256 /* External bus logic */
257 #define P6_BUS_DRDY_CLOCKS      0x62    /* (@) #clocks DRDY is asserted */
258 #define P6_BUS_LOCK_CLOCKS      0x63    /* (@) #clocks LOCK is asserted */
259 #define P6_BUS_REQ_OUTSTANDING  0x60    /* #bus requests outstanding */
260 #define P6_BUS_TRAN_BRD         0x65    /* (@) bus burst read txns */
261 #define P6_BUS_TRAN_RFO         0x66    /* (@) bus read for ownership txns */
262 #define P6_BUS_TRAN_WB          0x67    /* (@) bus writeback txns */
263 #define P6_BUS_TRAN_IFETCH      0x68    /* (@) bus instr fetch txns */
264 #define P6_BUS_TRAN_INVAL       0x69    /* (@) bus invalidate txns */
265 #define P6_BUS_TRAN_PWR         0x6a    /* (@) bus partial write txns */
266 #define P6_BUS_TRANS_P          0x6b    /* (@) bus partial txns */
267 #define P6_BUS_TRANS_IO         0x6c    /* (@) bus I/O txns */
268 #define P6_BUS_TRAN_DEF         0x6d    /* (@) bus deferred txns */
269 #define P6_BUS_TRAN_BURST       0x6e    /* (@) bus burst txns */
270 #define P6_BUS_TRAN_ANY         0x70    /* (@) total bus txns */
271 #define P6_BUS_TRAN_MEM         0x6f    /* (@) total memory txns */
272 #define P6_BUS_DATA_RCV         0x64    /* #busclocks CPU is receiving data */
273 #define P6_BUS_BNR_DRV          0x61    /* #busclocks CPU is driving BNR pin */
274 #define P6_BUS_HIT_DRV          0x7a    /* #busclocks CPU is driving HIT pin */
275 #define P6_BUS_HITM_DRV         0x7b    /* #busclocks CPU is driving HITM pin*/
276 #define P6_BUS_SNOOP_STALL      0x7e    /* #clkcycles bus is snoop-stalled */
277
278 /* FPU */
279 #define P6_FLOPS                0xc1    /* (0) number of FP ops retired */
280 #define P6_FP_COMP_OPS          0x10    /* (0) computational FPOPS exec'd */
281 #define P6_FP_ASSIST            0x11    /* (1) FP excep's handled in ucode */
282 #define P6_MUL                  0x12    /* (1) number of FP multiplies */
283 #define P6_DIV                  0x13    /* (1) number of FP divides */
284 #define P6_CYCLES_DIV_BUSY      0x14    /* (0) number of cycles divider busy */
285
286 /* Memory ordering */
287 #define P6_LD_BLOCKS            0x03    /* number of store buffer blocks */
288 #define P6_SB_DRAINS            0x04    /* # of store buffer drain cycles */
289 #define P6_MISALING_MEM_REF     0x05    /* # misaligned data memory refs */
290
291 /* Instruction decoding and retirement */
292 #define P6_INST_RETIRED         0xc0    /* number of instrs retired */
293 #define P6_UOPS_RETIRED         0xc2    /* number of micro-ops retired */
294 #define P6_INST_DECODER         0xd0    /* number of instructions decoded */
295
296 /* Interrupts */
297 #define P6_HW_INT_RX            0xc8    /* number of hardware interrupts */
298 #define P6_CYCLES_INT_MASKED    0xc6    /* number of cycles hardints masked */
299 #define P6_CYCLES_INT_PENDING_AND_MASKED 0xc7 /* #cycles masked but pending */
300
301 /* Branches */
302 #define P6_BR_INST_RETIRED      0xc4    /* number of branch instrs retired */
303 #define P6_BR_MISS_PRED_RETIRED 0xc5    /* number of mispred'd brs retired */
304 #define P6_BR_TAKEN_RETIRED     0xc9    /* number of taken branches retired */
305 #define P6_BR_MISS_PRED_TAKEN_RET 0xca  /* #taken mispredictions br's retired*/
306 #define P6_BR_INST_DECODED      0xe0    /* number of branch instrs decoded */
307 #define P6_BTB_MISSES           0xe2    /* # of branches that missed in BTB */
308 #define P6_BR_BOGUS             0xe4    /* number of bogus branches */
309 #define P6_BACLEARS             0xe6    /* # times BACLEAR is asserted */
310
311 /* Stalls */
312 #define P6_RESOURCE_STALLS      0xa2    /* # resource-related stall cycles */
313 #define P6_PARTIAL_RAT_STALLS   0xd2    /* # cycles/events for partial stalls*/
314
315 /* Segment register loads */
316 #define P6_SEGMENT_REG_LOADS    0x06    /* number of segment register loads */
317
318 /* Clocks */
319 #define P6_CPU_CLK_UNHALTED     0x79    /* #clocks CPU is not halted */
320
321 /* Unit field tags */
322 #define P6_UNIT_M               0x0800
323 #define P6_UNIT_E               0x0400
324 #define P6_UNIT_S               0x0200
325 #define P6_UNIT_I               0x0100
326 #define P6_UNIT_MESI            0x0f00
327
328 #define P6_UNIT_SELF            0x0000
329 #define P6_UNIT_ANY             0x2000
330
331 /****************************************************************************
332  ** Flag bit definitions (used for the 'flag' field in select_p6counter()) **
333  ****************************************************************************
334  *
335  * The driver accepts fully-formed counter specifications from user-level.
336  * The following flags are mneumonics for the bits that get set in the
337  * PerfEvtSel0 and PerfEvtSel1 MSR's
338  *
339  */
340 #define P6CNT_U  0x010000       /* Monitor user-level events */
341 #define P6CNT_K  0x020000       /* Monitor kernel-level events */
342 #define P6CNT_E  0x040000       /* Edge detect: count state transitions */
343 #define P6CNT_PC 0x080000       /* Pin control: ?? */
344 #define P6CNT_IE 0x100000       /* Int enable: enable interrupt on overflow */
345 #define P6CNT_F  0x200000       /* Freeze counter (handled in software) */
346 #define P6CNT_EN 0x400000       /* enable counters (in PerfEvtSel0) */
347 #define P6CNT_IV 0x800000       /* Invert counter mask comparison result */
348
349 /*****************************
350  ** Miscellaneous constants **
351  *****************************
352  *
353  * Number of Pentium Pro programable hardware counters. 
354  */
355 #define NUM_P6HWC 2
356
357 /*****************************************************************************
358 *
359 * End of Copyright by Harvard College
360 *
361 *****************************************************************************/
362
363
364 #define MSR_P6_EVNTSEL0 0x186
365 #define MSR_P6_EVNTSEL1 0x187
366 #define MSR_P6_PERFCTR0 0xc1
367 #define MSR_P6_PERFCTR1 0xc2
368
369 /* P6-specific Makros to manipulate and read counters */
370
371 /* Read the 40 bit performance monitoring counter. This requires 
372    the PCE-flag in CR4 to be set. Otherwise GP0 is raised. Works only
373    at P6.
374    */
375 #define l4_i686_rdpmc(cntr, res_p) \
376   __asm __volatile(                                             \
377          "movl %2, %%ecx        # put counter number in         \n\
378          .byte 0xf; .byte 0x33  # RDPMC instruction             \n\
379          movl %%edx, %1         # High order 32 bits            \n\
380          movl %%eax, %0         # Low order 32 bits"            \
381         : "=g" (*(int *)(res_p)), "=g" (*(((int *)res_p)+1))    \
382         : "g" (cntr)                                            \
383         : "ecx", "eax", "edx")
384
385 static inline l4_uint32_t l4_i686_rdpmc_32(int cntr){
386   l4_uint32_t x;
387   
388   __asm__ __volatile__(
389          ".byte 0xf; .byte 0x33 # RDPMC instruction"
390         : "=a" (x)
391         : "c" (cntr)
392         : "ecx", "eax", "edx");
393   return x;
394 }
395
396 static inline void l4_i686_select_perfctr_event(int counter, 
397                                                 unsigned long long val){
398   l4_i586_wrmsr(MSR_P6_EVNTSEL0+counter, &val);
399 }
400
401 static inline void l4_i686_select_perfctr0_event(long long *val){
402   asm volatile(
403                "movl $MSR_P6_EVNTSEL0, %%ecx\n"
404                "movl (%%ebx), %%eax\n"
405                "movl 4(%%ebx), %%edx\n"
406                //".byte 0xcc, 0xeb, 0x01, 0x21\n"
407                ".byte 0x0f, 0x30\n"     // wrmsr
408                //".byte 0xcc, 0xeb, 0x01, 0x21\n"
409                : /* no output */
410                : "b" (val)
411                : "ax", "cx", "dx", "bx"
412                );
413
414 }
415
416 /* end of P6 section */
417 #else
418
419 #define K7CNT_U  0x010000       /* Monitor user-level events */
420 #define K7CNT_K  0x020000       /* Monitor kernel-level events */
421 #define K7CNT_E  0x040000       /* Edge detect: count state transitions */
422 #define K7CNT_PC 0x080000       /* Pin control: ?? */
423 #define K7CNT_IE 0x100000       /* Int enable: enable interrupt on overflow */
424 #define K7CNT_F  0x200000       /* Freeze counter (handled in software) */
425 #define K7CNT_EN 0x400000       /* enable counters (in PerfEvtSel0) */
426 #define K7CNT_IV 0x800000       /* Invert counter mask comparison result */
427
428 #define MSR_K7_EVNTSEL0 0xC0010000
429 #define MSR_K7_EVNTSEL1 0xC0010001
430 #define MSR_K7_EVNTSEL2 0xC0010002
431 #define MSR_K7_EVNTSEL3 0xC0010003
432 #define MSR_K7_PERFCTR0 0xC0010004
433 #define MSR_K7_PERFCTR1 0xC0010005
434 #define MSR_K7_PERFCTR2 0xC0010006
435 #define MSR_K7_PERFCTR3 0xC0010007
436
437 #endif
438
439 #endif
440
441 /* end of P5/P6/K7 section*/
442 #endif
443
444 /* end of not only lib-prototypes section */
445 #endif
446
447 EXTERN_C_END
448
449 #endif