2 * Copyright (c) 2003-2008 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/module.h>
34 #include <sys/syscall.h>
46 #include "libpmcinternal.h"
48 /* Function prototypes */
50 static int k7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
51 struct pmc_op_pmcallocate *_pmc_config);
53 #if defined(__amd64__) || defined(__i386__)
54 static int iaf_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
55 struct pmc_op_pmcallocate *_pmc_config);
56 static int iap_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
57 struct pmc_op_pmcallocate *_pmc_config);
58 static int ucf_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
59 struct pmc_op_pmcallocate *_pmc_config);
60 static int ucp_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
61 struct pmc_op_pmcallocate *_pmc_config);
62 static int k8_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
63 struct pmc_op_pmcallocate *_pmc_config);
64 static int p4_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
65 struct pmc_op_pmcallocate *_pmc_config);
68 static int p5_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
69 struct pmc_op_pmcallocate *_pmc_config);
70 static int p6_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
71 struct pmc_op_pmcallocate *_pmc_config);
73 #if defined(__amd64__) || defined(__i386__)
74 static int tsc_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
75 struct pmc_op_pmcallocate *_pmc_config);
78 #if defined(__XSCALE__)
79 static int xscale_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
80 struct pmc_op_pmcallocate *_pmc_config);
82 static int armv7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
83 struct pmc_op_pmcallocate *_pmc_config);
85 #if defined(__aarch64__)
86 static int arm64_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
87 struct pmc_op_pmcallocate *_pmc_config);
90 static int mips_allocate_pmc(enum pmc_event _pe, char* ctrspec,
91 struct pmc_op_pmcallocate *_pmc_config);
93 static int soft_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
94 struct pmc_op_pmcallocate *_pmc_config);
96 #if defined(__powerpc__)
97 static int powerpc_allocate_pmc(enum pmc_event _pe, char* ctrspec,
98 struct pmc_op_pmcallocate *_pmc_config);
99 #endif /* __powerpc__ */
101 #define PMC_CALL(cmd, params) \
102 syscall(pmc_syscall, PMC_OP_##cmd, (params))
105 * Event aliases provide a way for the user to ask for generic events
106 * like "cache-misses", or "instructions-retired". These aliases are
107 * mapped to the appropriate canonical event descriptions using a
110 struct pmc_event_alias {
111 const char *pm_alias;
115 static const struct pmc_event_alias *pmc_mdep_event_aliases;
118 * The pmc_event_descr structure maps symbolic names known to the user
119 * to integer codes used by the PMC KLD.
121 struct pmc_event_descr {
122 const char *pm_ev_name;
123 enum pmc_event pm_ev_code;
127 * The pmc_class_descr structure maps class name prefixes for
128 * event names to event tables and other PMC class data.
130 struct pmc_class_descr {
131 const char *pm_evc_name;
132 size_t pm_evc_name_size;
133 enum pmc_class pm_evc_class;
134 const struct pmc_event_descr *pm_evc_event_table;
135 size_t pm_evc_event_table_size;
136 int (*pm_evc_allocate_pmc)(enum pmc_event _pe,
137 char *_ctrspec, struct pmc_op_pmcallocate *_pa);
140 #define PMC_TABLE_SIZE(N) (sizeof(N)/sizeof(N[0]))
141 #define PMC_EVENT_TABLE_SIZE(N) PMC_TABLE_SIZE(N##_event_table)
144 #define __PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
147 * PMC_CLASSDEP_TABLE(NAME, CLASS)
149 * Define a table mapping event names and aliases to HWPMC event IDs.
151 #define PMC_CLASSDEP_TABLE(N, C) \
152 static const struct pmc_event_descr N##_event_table[] = \
157 PMC_CLASSDEP_TABLE(iaf, IAF);
158 PMC_CLASSDEP_TABLE(k7, K7);
159 PMC_CLASSDEP_TABLE(k8, K8);
160 PMC_CLASSDEP_TABLE(p4, P4);
161 PMC_CLASSDEP_TABLE(p5, P5);
162 PMC_CLASSDEP_TABLE(p6, P6);
163 PMC_CLASSDEP_TABLE(xscale, XSCALE);
164 PMC_CLASSDEP_TABLE(armv7, ARMV7);
165 PMC_CLASSDEP_TABLE(armv8, ARMV8);
166 PMC_CLASSDEP_TABLE(mips24k, MIPS24K);
167 PMC_CLASSDEP_TABLE(mips74k, MIPS74K);
168 PMC_CLASSDEP_TABLE(octeon, OCTEON);
169 PMC_CLASSDEP_TABLE(ucf, UCF);
170 PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
171 PMC_CLASSDEP_TABLE(ppc970, PPC970);
172 PMC_CLASSDEP_TABLE(e500, E500);
174 static struct pmc_event_descr soft_event_table[PMC_EV_DYN_COUNT];
176 #undef __PMC_EV_ALIAS
177 #define __PMC_EV_ALIAS(N,CODE) { N, PMC_EV_##CODE },
179 static const struct pmc_event_descr atom_event_table[] =
181 __PMC_EV_ALIAS_ATOM()
184 static const struct pmc_event_descr atom_silvermont_event_table[] =
186 __PMC_EV_ALIAS_ATOM_SILVERMONT()
189 static const struct pmc_event_descr core_event_table[] =
191 __PMC_EV_ALIAS_CORE()
195 static const struct pmc_event_descr core2_event_table[] =
197 __PMC_EV_ALIAS_CORE2()
200 static const struct pmc_event_descr corei7_event_table[] =
202 __PMC_EV_ALIAS_COREI7()
205 static const struct pmc_event_descr nehalem_ex_event_table[] =
207 __PMC_EV_ALIAS_COREI7()
210 static const struct pmc_event_descr haswell_event_table[] =
212 __PMC_EV_ALIAS_HASWELL()
215 static const struct pmc_event_descr haswell_xeon_event_table[] =
217 __PMC_EV_ALIAS_HASWELL_XEON()
220 static const struct pmc_event_descr broadwell_event_table[] =
222 __PMC_EV_ALIAS_BROADWELL()
225 static const struct pmc_event_descr broadwell_xeon_event_table[] =
227 __PMC_EV_ALIAS_BROADWELL_XEON()
230 static const struct pmc_event_descr skylake_event_table[] =
232 __PMC_EV_ALIAS_SKYLAKE()
235 static const struct pmc_event_descr ivybridge_event_table[] =
237 __PMC_EV_ALIAS_IVYBRIDGE()
240 static const struct pmc_event_descr ivybridge_xeon_event_table[] =
242 __PMC_EV_ALIAS_IVYBRIDGE_XEON()
245 static const struct pmc_event_descr sandybridge_event_table[] =
247 __PMC_EV_ALIAS_SANDYBRIDGE()
250 static const struct pmc_event_descr sandybridge_xeon_event_table[] =
252 __PMC_EV_ALIAS_SANDYBRIDGE_XEON()
255 static const struct pmc_event_descr westmere_event_table[] =
257 __PMC_EV_ALIAS_WESTMERE()
260 static const struct pmc_event_descr westmere_ex_event_table[] =
262 __PMC_EV_ALIAS_WESTMERE()
265 static const struct pmc_event_descr corei7uc_event_table[] =
267 __PMC_EV_ALIAS_COREI7UC()
270 static const struct pmc_event_descr haswelluc_event_table[] =
272 __PMC_EV_ALIAS_HASWELLUC()
275 static const struct pmc_event_descr broadwelluc_event_table[] =
277 __PMC_EV_ALIAS_BROADWELLUC()
280 static const struct pmc_event_descr sandybridgeuc_event_table[] =
282 __PMC_EV_ALIAS_SANDYBRIDGEUC()
285 static const struct pmc_event_descr westmereuc_event_table[] =
287 __PMC_EV_ALIAS_WESTMEREUC()
290 static const struct pmc_event_descr cortex_a8_event_table[] =
292 __PMC_EV_ALIAS_ARMV7_CORTEX_A8()
295 static const struct pmc_event_descr cortex_a9_event_table[] =
297 __PMC_EV_ALIAS_ARMV7_CORTEX_A9()
300 static const struct pmc_event_descr cortex_a53_event_table[] =
302 __PMC_EV_ALIAS_ARMV8_CORTEX_A53()
305 static const struct pmc_event_descr cortex_a57_event_table[] =
307 __PMC_EV_ALIAS_ARMV8_CORTEX_A57()
311 * PMC_MDEP_TABLE(NAME, PRIMARYCLASS, ADDITIONAL_CLASSES...)
313 * Map a CPU to the PMC classes it supports.
315 #define PMC_MDEP_TABLE(N,C,...) \
316 static const enum pmc_class N##_pmc_classes[] = { \
317 PMC_CLASS_##C, __VA_ARGS__ \
320 PMC_MDEP_TABLE(atom, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
321 PMC_MDEP_TABLE(atom_silvermont, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
322 PMC_MDEP_TABLE(core, IAP, PMC_CLASS_SOFT, PMC_CLASS_TSC);
323 PMC_MDEP_TABLE(core2, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
324 PMC_MDEP_TABLE(corei7, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
325 PMC_MDEP_TABLE(nehalem_ex, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
326 PMC_MDEP_TABLE(haswell, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
327 PMC_MDEP_TABLE(haswell_xeon, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
328 PMC_MDEP_TABLE(broadwell, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
329 PMC_MDEP_TABLE(broadwell_xeon, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
330 PMC_MDEP_TABLE(skylake, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
331 PMC_MDEP_TABLE(ivybridge, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
332 PMC_MDEP_TABLE(ivybridge_xeon, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
333 PMC_MDEP_TABLE(sandybridge, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
334 PMC_MDEP_TABLE(sandybridge_xeon, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
335 PMC_MDEP_TABLE(westmere, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
336 PMC_MDEP_TABLE(westmere_ex, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
337 PMC_MDEP_TABLE(k7, K7, PMC_CLASS_SOFT, PMC_CLASS_TSC);
338 PMC_MDEP_TABLE(k8, K8, PMC_CLASS_SOFT, PMC_CLASS_TSC);
339 PMC_MDEP_TABLE(p4, P4, PMC_CLASS_SOFT, PMC_CLASS_TSC);
340 PMC_MDEP_TABLE(p5, P5, PMC_CLASS_SOFT, PMC_CLASS_TSC);
341 PMC_MDEP_TABLE(p6, P6, PMC_CLASS_SOFT, PMC_CLASS_TSC);
342 PMC_MDEP_TABLE(xscale, XSCALE, PMC_CLASS_SOFT, PMC_CLASS_XSCALE);
343 PMC_MDEP_TABLE(cortex_a8, ARMV7, PMC_CLASS_SOFT, PMC_CLASS_ARMV7);
344 PMC_MDEP_TABLE(cortex_a9, ARMV7, PMC_CLASS_SOFT, PMC_CLASS_ARMV7);
345 PMC_MDEP_TABLE(cortex_a53, ARMV8, PMC_CLASS_SOFT, PMC_CLASS_ARMV8);
346 PMC_MDEP_TABLE(cortex_a57, ARMV8, PMC_CLASS_SOFT, PMC_CLASS_ARMV8);
347 PMC_MDEP_TABLE(mips24k, MIPS24K, PMC_CLASS_SOFT, PMC_CLASS_MIPS24K);
348 PMC_MDEP_TABLE(mips74k, MIPS74K, PMC_CLASS_SOFT, PMC_CLASS_MIPS74K);
349 PMC_MDEP_TABLE(octeon, OCTEON, PMC_CLASS_SOFT, PMC_CLASS_OCTEON);
350 PMC_MDEP_TABLE(ppc7450, PPC7450, PMC_CLASS_SOFT, PMC_CLASS_PPC7450, PMC_CLASS_TSC);
351 PMC_MDEP_TABLE(ppc970, PPC970, PMC_CLASS_SOFT, PMC_CLASS_PPC970, PMC_CLASS_TSC);
352 PMC_MDEP_TABLE(e500, E500, PMC_CLASS_SOFT, PMC_CLASS_E500, PMC_CLASS_TSC);
353 PMC_MDEP_TABLE(generic, SOFT, PMC_CLASS_SOFT);
355 static const struct pmc_event_descr tsc_event_table[] =
360 #undef PMC_CLASS_TABLE_DESC
361 #define PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR) \
362 static const struct pmc_class_descr NAME##_class_table_descr = \
364 .pm_evc_name = #CLASS "-", \
365 .pm_evc_name_size = sizeof(#CLASS "-") - 1, \
366 .pm_evc_class = PMC_CLASS_##CLASS , \
367 .pm_evc_event_table = EVENTS##_event_table , \
368 .pm_evc_event_table_size = \
369 PMC_EVENT_TABLE_SIZE(EVENTS), \
370 .pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc \
373 #if defined(__i386__) || defined(__amd64__)
374 PMC_CLASS_TABLE_DESC(iaf, IAF, iaf, iaf);
375 PMC_CLASS_TABLE_DESC(atom, IAP, atom, iap);
376 PMC_CLASS_TABLE_DESC(atom_silvermont, IAP, atom_silvermont, iap);
377 PMC_CLASS_TABLE_DESC(core, IAP, core, iap);
378 PMC_CLASS_TABLE_DESC(core2, IAP, core2, iap);
379 PMC_CLASS_TABLE_DESC(corei7, IAP, corei7, iap);
380 PMC_CLASS_TABLE_DESC(nehalem_ex, IAP, nehalem_ex, iap);
381 PMC_CLASS_TABLE_DESC(haswell, IAP, haswell, iap);
382 PMC_CLASS_TABLE_DESC(haswell_xeon, IAP, haswell_xeon, iap);
383 PMC_CLASS_TABLE_DESC(broadwell, IAP, broadwell, iap);
384 PMC_CLASS_TABLE_DESC(broadwell_xeon, IAP, broadwell_xeon, iap);
385 PMC_CLASS_TABLE_DESC(skylake, IAP, skylake, iap);
386 PMC_CLASS_TABLE_DESC(ivybridge, IAP, ivybridge, iap);
387 PMC_CLASS_TABLE_DESC(ivybridge_xeon, IAP, ivybridge_xeon, iap);
388 PMC_CLASS_TABLE_DESC(sandybridge, IAP, sandybridge, iap);
389 PMC_CLASS_TABLE_DESC(sandybridge_xeon, IAP, sandybridge_xeon, iap);
390 PMC_CLASS_TABLE_DESC(westmere, IAP, westmere, iap);
391 PMC_CLASS_TABLE_DESC(westmere_ex, IAP, westmere_ex, iap);
392 PMC_CLASS_TABLE_DESC(ucf, UCF, ucf, ucf);
393 PMC_CLASS_TABLE_DESC(corei7uc, UCP, corei7uc, ucp);
394 PMC_CLASS_TABLE_DESC(haswelluc, UCP, haswelluc, ucp);
395 PMC_CLASS_TABLE_DESC(broadwelluc, UCP, broadwelluc, ucp);
396 PMC_CLASS_TABLE_DESC(sandybridgeuc, UCP, sandybridgeuc, ucp);
397 PMC_CLASS_TABLE_DESC(westmereuc, UCP, westmereuc, ucp);
399 #if defined(__i386__)
400 PMC_CLASS_TABLE_DESC(k7, K7, k7, k7);
402 #if defined(__i386__) || defined(__amd64__)
403 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
404 PMC_CLASS_TABLE_DESC(p4, P4, p4, p4);
406 #if defined(__i386__)
407 PMC_CLASS_TABLE_DESC(p5, P5, p5, p5);
408 PMC_CLASS_TABLE_DESC(p6, P6, p6, p6);
410 #if defined(__i386__) || defined(__amd64__)
411 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
414 #if defined(__XSCALE__)
415 PMC_CLASS_TABLE_DESC(xscale, XSCALE, xscale, xscale);
417 PMC_CLASS_TABLE_DESC(cortex_a8, ARMV7, cortex_a8, armv7);
418 PMC_CLASS_TABLE_DESC(cortex_a9, ARMV7, cortex_a9, armv7);
420 #if defined(__aarch64__)
421 PMC_CLASS_TABLE_DESC(cortex_a53, ARMV8, cortex_a53, arm64);
422 PMC_CLASS_TABLE_DESC(cortex_a57, ARMV8, cortex_a57, arm64);
424 #if defined(__mips__)
425 PMC_CLASS_TABLE_DESC(mips24k, MIPS24K, mips24k, mips);
426 PMC_CLASS_TABLE_DESC(mips74k, MIPS74K, mips74k, mips);
427 PMC_CLASS_TABLE_DESC(octeon, OCTEON, octeon, mips);
428 #endif /* __mips__ */
429 #if defined(__powerpc__)
430 PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, powerpc);
431 PMC_CLASS_TABLE_DESC(ppc970, PPC970, ppc970, powerpc);
432 PMC_CLASS_TABLE_DESC(e500, E500, e500, powerpc);
435 static struct pmc_class_descr soft_class_table_descr =
437 .pm_evc_name = "SOFT-",
438 .pm_evc_name_size = sizeof("SOFT-") - 1,
439 .pm_evc_class = PMC_CLASS_SOFT,
440 .pm_evc_event_table = NULL,
441 .pm_evc_event_table_size = 0,
442 .pm_evc_allocate_pmc = soft_allocate_pmc
445 #undef PMC_CLASS_TABLE_DESC
447 static const struct pmc_class_descr **pmc_class_table;
448 #define PMC_CLASS_TABLE_SIZE cpu_info.pm_nclass
450 static const enum pmc_class *pmc_mdep_class_list;
451 static size_t pmc_mdep_class_list_size;
454 * Mapping tables, mapping enumeration values to human readable
458 static const char * pmc_capability_names[] = {
460 #define __PMC_CAP(N,V,D) #N ,
464 struct pmc_class_map {
465 enum pmc_class pm_class;
469 static const struct pmc_class_map pmc_class_names[] = {
471 #define __PMC_CLASS(S,V,D) { .pm_class = PMC_CLASS_##S, .pm_name = #S } ,
475 struct pmc_cputype_map {
476 enum pmc_cputype pm_cputype;
480 static const struct pmc_cputype_map pmc_cputype_names[] = {
482 #define __PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
486 static const char * pmc_disposition_names[] = {
488 #define __PMC_DISP(D) #D ,
492 static const char * pmc_mode_names[] = {
494 #define __PMC_MODE(M,N) #M ,
498 static const char * pmc_state_names[] = {
500 #define __PMC_STATE(S) #S ,
505 * Filled in by pmc_init().
507 static int pmc_syscall = -1;
508 static struct pmc_cpuinfo cpu_info;
509 static struct pmc_op_getdyneventinfo soft_event_info;
511 /* Event masks for events */
514 const uint64_t pm_value;
516 #define PMCMASK(N,V) { .pm_name = #N, .pm_value = (V) }
517 #define NULLMASK { .pm_name = NULL }
519 #if defined(__amd64__) || defined(__i386__)
521 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint64_t *evmask)
523 const struct pmc_masks *pm;
527 if (pmask == NULL) /* no mask keywords */
529 q = strchr(p, '='); /* skip '=' */
530 if (*++q == '\0') /* no more data */
532 c = 0; /* count of mask keywords seen */
533 while ((r = strsep(&q, "+")) != NULL) {
534 for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
537 if (pm->pm_name == NULL) /* not found */
539 *evmask |= pm->pm_value;
546 #define KWMATCH(p,kw) (strcasecmp((p), (kw)) == 0)
547 #define KWPREFIXMATCH(p,kw) (strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
548 #define EV_ALIAS(N,S) { .pm_alias = N, .pm_spec = S }
550 #if defined(__i386__)
553 * AMD K7 (Athlon) CPUs.
556 static struct pmc_event_alias k7_aliases[] = {
557 EV_ALIAS("branches", "k7-retired-branches"),
558 EV_ALIAS("branch-mispredicts", "k7-retired-branches-mispredicted"),
559 EV_ALIAS("cycles", "tsc"),
560 EV_ALIAS("dc-misses", "k7-dc-misses"),
561 EV_ALIAS("ic-misses", "k7-ic-misses"),
562 EV_ALIAS("instructions", "k7-retired-instructions"),
563 EV_ALIAS("interrupts", "k7-hardware-interrupts"),
567 #define K7_KW_COUNT "count"
568 #define K7_KW_EDGE "edge"
569 #define K7_KW_INV "inv"
570 #define K7_KW_OS "os"
571 #define K7_KW_UNITMASK "unitmask"
572 #define K7_KW_USR "usr"
575 k7_allocate_pmc(enum pmc_event pe, char *ctrspec,
576 struct pmc_op_pmcallocate *pmc_config)
580 uint32_t count, unitmask;
582 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
583 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
585 if (pe == PMC_EV_K7_DC_REFILLS_FROM_L2 ||
586 pe == PMC_EV_K7_DC_REFILLS_FROM_SYSTEM ||
587 pe == PMC_EV_K7_DC_WRITEBACKS) {
589 unitmask = AMD_PMC_UNITMASK_MOESI;
591 unitmask = has_unitmask = 0;
593 while ((p = strsep(&ctrspec, ",")) != NULL) {
594 if (KWPREFIXMATCH(p, K7_KW_COUNT "=")) {
596 if (*++q == '\0') /* skip '=' */
599 count = strtol(q, &e, 0);
600 if (e == q || *e != '\0')
603 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
604 pmc_config->pm_md.pm_amd.pm_amd_config |=
605 AMD_PMC_TO_COUNTER(count);
607 } else if (KWMATCH(p, K7_KW_EDGE)) {
608 pmc_config->pm_caps |= PMC_CAP_EDGE;
609 } else if (KWMATCH(p, K7_KW_INV)) {
610 pmc_config->pm_caps |= PMC_CAP_INVERT;
611 } else if (KWMATCH(p, K7_KW_OS)) {
612 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
613 } else if (KWPREFIXMATCH(p, K7_KW_UNITMASK "=")) {
614 if (has_unitmask == 0)
618 if (*++q == '\0') /* skip '=' */
621 while ((c = tolower(*q++)) != 0)
623 unitmask |= AMD_PMC_UNITMASK_M;
625 unitmask |= AMD_PMC_UNITMASK_O;
627 unitmask |= AMD_PMC_UNITMASK_E;
629 unitmask |= AMD_PMC_UNITMASK_S;
631 unitmask |= AMD_PMC_UNITMASK_I;
640 } else if (KWMATCH(p, K7_KW_USR)) {
641 pmc_config->pm_caps |= PMC_CAP_USER;
647 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
648 pmc_config->pm_md.pm_amd.pm_amd_config |=
649 AMD_PMC_TO_UNITMASK(unitmask);
658 #if defined(__amd64__) || defined(__i386__)
661 * Intel Core (Family 6, Model E) PMCs.
664 static struct pmc_event_alias core_aliases[] = {
665 EV_ALIAS("branches", "iap-br-instr-ret"),
666 EV_ALIAS("branch-mispredicts", "iap-br-mispred-ret"),
667 EV_ALIAS("cycles", "tsc-tsc"),
668 EV_ALIAS("ic-misses", "iap-icache-misses"),
669 EV_ALIAS("instructions", "iap-instr-ret"),
670 EV_ALIAS("interrupts", "iap-core-hw-int-rx"),
671 EV_ALIAS("unhalted-cycles", "iap-unhalted-core-cycles"),
676 * Intel Core2 (Family 6, Model F), Core2Extreme (Family 6, Model 17H)
677 * and Atom (Family 6, model 1CH) PMCs.
679 * We map aliases to events on the fixed-function counters if these
680 * are present. Note that not all CPUs in this family contain fixed-function
684 static struct pmc_event_alias core2_aliases[] = {
685 EV_ALIAS("branches", "iap-br-inst-retired.any"),
686 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
687 EV_ALIAS("cycles", "tsc-tsc"),
688 EV_ALIAS("ic-misses", "iap-l1i-misses"),
689 EV_ALIAS("instructions", "iaf-instr-retired.any"),
690 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
691 EV_ALIAS("unhalted-cycles", "iaf-cpu-clk-unhalted.core"),
695 static struct pmc_event_alias core2_aliases_without_iaf[] = {
696 EV_ALIAS("branches", "iap-br-inst-retired.any"),
697 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
698 EV_ALIAS("cycles", "tsc-tsc"),
699 EV_ALIAS("ic-misses", "iap-l1i-misses"),
700 EV_ALIAS("instructions", "iap-inst-retired.any_p"),
701 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
702 EV_ALIAS("unhalted-cycles", "iap-cpu-clk-unhalted.core_p"),
706 #define atom_aliases core2_aliases
707 #define atom_aliases_without_iaf core2_aliases_without_iaf
708 #define atom_silvermont_aliases core2_aliases
709 #define atom_silvermont_aliases_without_iaf core2_aliases_without_iaf
710 #define corei7_aliases core2_aliases
711 #define corei7_aliases_without_iaf core2_aliases_without_iaf
712 #define nehalem_ex_aliases core2_aliases
713 #define nehalem_ex_aliases_without_iaf core2_aliases_without_iaf
714 #define haswell_aliases core2_aliases
715 #define haswell_aliases_without_iaf core2_aliases_without_iaf
716 #define haswell_xeon_aliases core2_aliases
717 #define haswell_xeon_aliases_without_iaf core2_aliases_without_iaf
718 #define broadwell_aliases core2_aliases
719 #define broadwell_aliases_without_iaf core2_aliases_without_iaf
720 #define broadwell_xeon_aliases core2_aliases
721 #define broadwell_xeon_aliases_without_iaf core2_aliases_without_iaf
722 #define skylake_aliases core2_aliases
723 #define skylake_aliases_without_iaf core2_aliases_without_iaf
724 #define ivybridge_aliases core2_aliases
725 #define ivybridge_aliases_without_iaf core2_aliases_without_iaf
726 #define ivybridge_xeon_aliases core2_aliases
727 #define ivybridge_xeon_aliases_without_iaf core2_aliases_without_iaf
728 #define sandybridge_aliases core2_aliases
729 #define sandybridge_aliases_without_iaf core2_aliases_without_iaf
730 #define sandybridge_xeon_aliases core2_aliases
731 #define sandybridge_xeon_aliases_without_iaf core2_aliases_without_iaf
732 #define westmere_aliases core2_aliases
733 #define westmere_aliases_without_iaf core2_aliases_without_iaf
734 #define westmere_ex_aliases core2_aliases
735 #define westmere_ex_aliases_without_iaf core2_aliases_without_iaf
737 #define IAF_KW_OS "os"
738 #define IAF_KW_USR "usr"
739 #define IAF_KW_ANYTHREAD "anythread"
742 * Parse an event specifier for Intel fixed function counters.
745 iaf_allocate_pmc(enum pmc_event pe, char *ctrspec,
746 struct pmc_op_pmcallocate *pmc_config)
752 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
753 pmc_config->pm_md.pm_iaf.pm_iaf_flags = 0;
755 while ((p = strsep(&ctrspec, ",")) != NULL) {
756 if (KWMATCH(p, IAF_KW_OS))
757 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
758 else if (KWMATCH(p, IAF_KW_USR))
759 pmc_config->pm_caps |= PMC_CAP_USER;
760 else if (KWMATCH(p, IAF_KW_ANYTHREAD))
761 pmc_config->pm_md.pm_iaf.pm_iaf_flags |= IAF_ANY;
770 * Core/Core2 support.
773 #define IAP_KW_AGENT "agent"
774 #define IAP_KW_ANYTHREAD "anythread"
775 #define IAP_KW_CACHESTATE "cachestate"
776 #define IAP_KW_CMASK "cmask"
777 #define IAP_KW_CORE "core"
778 #define IAP_KW_EDGE "edge"
779 #define IAP_KW_INV "inv"
780 #define IAP_KW_OS "os"
781 #define IAP_KW_PREFETCH "prefetch"
782 #define IAP_KW_SNOOPRESPONSE "snoopresponse"
783 #define IAP_KW_SNOOPTYPE "snooptype"
784 #define IAP_KW_TRANSITION "trans"
785 #define IAP_KW_USR "usr"
786 #define IAP_KW_RSP "rsp"
788 static struct pmc_masks iap_core_mask[] = {
789 PMCMASK(all, (0x3 << 14)),
790 PMCMASK(this, (0x1 << 14)),
794 static struct pmc_masks iap_agent_mask[] = {
796 PMCMASK(any, (0x1 << 13)),
800 static struct pmc_masks iap_prefetch_mask[] = {
801 PMCMASK(both, (0x3 << 12)),
802 PMCMASK(only, (0x1 << 12)),
807 static struct pmc_masks iap_cachestate_mask[] = {
808 PMCMASK(i, (1 << 8)),
809 PMCMASK(s, (1 << 9)),
810 PMCMASK(e, (1 << 10)),
811 PMCMASK(m, (1 << 11)),
815 static struct pmc_masks iap_snoopresponse_mask[] = {
816 PMCMASK(clean, (1 << 8)),
817 PMCMASK(hit, (1 << 9)),
818 PMCMASK(hitm, (1 << 11)),
822 static struct pmc_masks iap_snooptype_mask[] = {
823 PMCMASK(cmp2s, (1 << 8)),
824 PMCMASK(cmp2i, (1 << 9)),
828 static struct pmc_masks iap_transition_mask[] = {
830 PMCMASK(frequency, 0x10),
834 static struct pmc_masks iap_rsp_mask_i7_wm[] = {
835 PMCMASK(DMND_DATA_RD, (1 << 0)),
836 PMCMASK(DMND_RFO, (1 << 1)),
837 PMCMASK(DMND_IFETCH, (1 << 2)),
838 PMCMASK(WB, (1 << 3)),
839 PMCMASK(PF_DATA_RD, (1 << 4)),
840 PMCMASK(PF_RFO, (1 << 5)),
841 PMCMASK(PF_IFETCH, (1 << 6)),
842 PMCMASK(OTHER, (1 << 7)),
843 PMCMASK(UNCORE_HIT, (1 << 8)),
844 PMCMASK(OTHER_CORE_HIT_SNP, (1 << 9)),
845 PMCMASK(OTHER_CORE_HITM, (1 << 10)),
846 PMCMASK(REMOTE_CACHE_FWD, (1 << 12)),
847 PMCMASK(REMOTE_DRAM, (1 << 13)),
848 PMCMASK(LOCAL_DRAM, (1 << 14)),
849 PMCMASK(NON_DRAM, (1 << 15)),
853 static struct pmc_masks iap_rsp_mask_sb_sbx_ib[] = {
854 PMCMASK(REQ_DMND_DATA_RD, (1ULL << 0)),
855 PMCMASK(REQ_DMND_RFO, (1ULL << 1)),
856 PMCMASK(REQ_DMND_IFETCH, (1ULL << 2)),
857 PMCMASK(REQ_WB, (1ULL << 3)),
858 PMCMASK(REQ_PF_DATA_RD, (1ULL << 4)),
859 PMCMASK(REQ_PF_RFO, (1ULL << 5)),
860 PMCMASK(REQ_PF_IFETCH, (1ULL << 6)),
861 PMCMASK(REQ_PF_LLC_DATA_RD, (1ULL << 7)),
862 PMCMASK(REQ_PF_LLC_RFO, (1ULL << 8)),
863 PMCMASK(REQ_PF_LLC_IFETCH, (1ULL << 9)),
864 PMCMASK(REQ_BUS_LOCKS, (1ULL << 10)),
865 PMCMASK(REQ_STRM_ST, (1ULL << 11)),
866 PMCMASK(REQ_OTHER, (1ULL << 15)),
867 PMCMASK(RES_ANY, (1ULL << 16)),
868 PMCMASK(RES_SUPPLIER_SUPP, (1ULL << 17)),
869 PMCMASK(RES_SUPPLIER_LLC_HITM, (1ULL << 18)),
870 PMCMASK(RES_SUPPLIER_LLC_HITE, (1ULL << 19)),
871 PMCMASK(RES_SUPPLIER_LLC_HITS, (1ULL << 20)),
872 PMCMASK(RES_SUPPLIER_LLC_HITF, (1ULL << 21)),
873 PMCMASK(RES_SUPPLIER_LOCAL, (1ULL << 22)),
874 PMCMASK(RES_SNOOP_SNP_NONE, (1ULL << 31)),
875 PMCMASK(RES_SNOOP_SNP_NO_NEEDED,(1ULL << 32)),
876 PMCMASK(RES_SNOOP_SNP_MISS, (1ULL << 33)),
877 PMCMASK(RES_SNOOP_HIT_NO_FWD, (1ULL << 34)),
878 PMCMASK(RES_SNOOP_HIT_FWD, (1ULL << 35)),
879 PMCMASK(RES_SNOOP_HITM, (1ULL << 36)),
880 PMCMASK(RES_NON_DRAM, (1ULL << 37)),
884 /* Broadwell is defined to use the same mask as Haswell */
885 static struct pmc_masks iap_rsp_mask_haswell[] = {
886 PMCMASK(REQ_DMND_DATA_RD, (1ULL << 0)),
887 PMCMASK(REQ_DMND_RFO, (1ULL << 1)),
888 PMCMASK(REQ_DMND_IFETCH, (1ULL << 2)),
889 PMCMASK(REQ_PF_DATA_RD, (1ULL << 4)),
890 PMCMASK(REQ_PF_RFO, (1ULL << 5)),
891 PMCMASK(REQ_PF_IFETCH, (1ULL << 6)),
892 PMCMASK(REQ_OTHER, (1ULL << 15)),
893 PMCMASK(RES_ANY, (1ULL << 16)),
894 PMCMASK(RES_SUPPLIER_SUPP, (1ULL << 17)),
895 PMCMASK(RES_SUPPLIER_LLC_HITM, (1ULL << 18)),
896 PMCMASK(RES_SUPPLIER_LLC_HITE, (1ULL << 19)),
897 PMCMASK(RES_SUPPLIER_LLC_HITS, (1ULL << 20)),
898 PMCMASK(RES_SUPPLIER_LLC_HITF, (1ULL << 21)),
899 PMCMASK(RES_SUPPLIER_LOCAL, (1ULL << 22)),
901 * For processor type 06_45H 22 is L4_HIT_LOCAL_L4
902 * and 23, 24 and 25 are also defined.
904 PMCMASK(RES_SNOOP_SNP_NONE, (1ULL << 31)),
905 PMCMASK(RES_SNOOP_SNP_NO_NEEDED,(1ULL << 32)),
906 PMCMASK(RES_SNOOP_SNP_MISS, (1ULL << 33)),
907 PMCMASK(RES_SNOOP_HIT_NO_FWD, (1ULL << 34)),
908 PMCMASK(RES_SNOOP_HIT_FWD, (1ULL << 35)),
909 PMCMASK(RES_SNOOP_HITM, (1ULL << 36)),
910 PMCMASK(RES_NON_DRAM, (1ULL << 37)),
914 static struct pmc_masks iap_rsp_mask_skylake[] = {
915 PMCMASK(REQ_DMND_DATA_RD, (1ULL << 0)),
916 PMCMASK(REQ_DMND_RFO, (1ULL << 1)),
917 PMCMASK(REQ_DMND_IFETCH, (1ULL << 2)),
918 PMCMASK(REQ_PF_DATA_RD, (1ULL << 7)),
919 PMCMASK(REQ_PF_RFO, (1ULL << 8)),
920 PMCMASK(REQ_STRM_ST, (1ULL << 11)),
921 PMCMASK(REQ_OTHER, (1ULL << 15)),
922 PMCMASK(RES_ANY, (1ULL << 16)),
923 PMCMASK(RES_SUPPLIER_SUPP, (1ULL << 17)),
924 PMCMASK(RES_SUPPLIER_LLC_HITM, (1ULL << 18)),
925 PMCMASK(RES_SUPPLIER_LLC_HITE, (1ULL << 19)),
926 PMCMASK(RES_SUPPLIER_LLC_HITS, (1ULL << 20)),
927 PMCMASK(RES_SUPPLIER_L4_HIT, (1ULL << 22)),
928 PMCMASK(RES_SUPPLIER_DRAM, (1ULL << 26)),
929 PMCMASK(RES_SUPPLIER_SPL_HIT, (1ULL << 30)),
930 PMCMASK(RES_SNOOP_SNP_NONE, (1ULL << 31)),
931 PMCMASK(RES_SNOOP_SNP_NO_NEEDED,(1ULL << 32)),
932 PMCMASK(RES_SNOOP_SNP_MISS, (1ULL << 33)),
933 PMCMASK(RES_SNOOP_HIT_NO_FWD, (1ULL << 34)),
934 PMCMASK(RES_SNOOP_HIT_FWD, (1ULL << 35)),
935 PMCMASK(RES_SNOOP_HITM, (1ULL << 36)),
936 PMCMASK(RES_NON_DRAM, (1ULL << 37)),
942 iap_allocate_pmc(enum pmc_event pe, char *ctrspec,
943 struct pmc_op_pmcallocate *pmc_config)
946 uint64_t cachestate, evmask, rsp;
949 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE |
951 pmc_config->pm_md.pm_iap.pm_iap_config = 0;
953 cachestate = evmask = rsp = 0;
955 /* Parse additional modifiers if present */
956 while ((p = strsep(&ctrspec, ",")) != NULL) {
959 if (KWPREFIXMATCH(p, IAP_KW_CMASK "=")) {
961 if (*++q == '\0') /* skip '=' */
963 count = strtol(q, &e, 0);
964 if (e == q || *e != '\0')
966 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
967 pmc_config->pm_md.pm_iap.pm_iap_config |=
969 } else if (KWMATCH(p, IAP_KW_EDGE)) {
970 pmc_config->pm_caps |= PMC_CAP_EDGE;
971 } else if (KWMATCH(p, IAP_KW_INV)) {
972 pmc_config->pm_caps |= PMC_CAP_INVERT;
973 } else if (KWMATCH(p, IAP_KW_OS)) {
974 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
975 } else if (KWMATCH(p, IAP_KW_USR)) {
976 pmc_config->pm_caps |= PMC_CAP_USER;
977 } else if (KWMATCH(p, IAP_KW_ANYTHREAD)) {
978 pmc_config->pm_md.pm_iap.pm_iap_config |= IAP_ANY;
979 } else if (KWPREFIXMATCH(p, IAP_KW_CORE "=")) {
980 n = pmc_parse_mask(iap_core_mask, p, &evmask);
983 } else if (KWPREFIXMATCH(p, IAP_KW_AGENT "=")) {
984 n = pmc_parse_mask(iap_agent_mask, p, &evmask);
987 } else if (KWPREFIXMATCH(p, IAP_KW_PREFETCH "=")) {
988 n = pmc_parse_mask(iap_prefetch_mask, p, &evmask);
991 } else if (KWPREFIXMATCH(p, IAP_KW_CACHESTATE "=")) {
992 n = pmc_parse_mask(iap_cachestate_mask, p, &cachestate);
993 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_CORE &&
994 KWPREFIXMATCH(p, IAP_KW_TRANSITION "=")) {
995 n = pmc_parse_mask(iap_transition_mask, p, &evmask);
998 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_ATOM ||
999 cpu_info.pm_cputype == PMC_CPU_INTEL_ATOM_SILVERMONT ||
1000 cpu_info.pm_cputype == PMC_CPU_INTEL_CORE2 ||
1001 cpu_info.pm_cputype == PMC_CPU_INTEL_CORE2EXTREME) {
1002 if (KWPREFIXMATCH(p, IAP_KW_SNOOPRESPONSE "=")) {
1003 n = pmc_parse_mask(iap_snoopresponse_mask, p,
1005 } else if (KWPREFIXMATCH(p, IAP_KW_SNOOPTYPE "=")) {
1006 n = pmc_parse_mask(iap_snooptype_mask, p,
1010 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_COREI7 ||
1011 cpu_info.pm_cputype == PMC_CPU_INTEL_WESTMERE ||
1012 cpu_info.pm_cputype == PMC_CPU_INTEL_NEHALEM_EX ||
1013 cpu_info.pm_cputype == PMC_CPU_INTEL_WESTMERE_EX) {
1014 if (KWPREFIXMATCH(p, IAP_KW_RSP "=")) {
1015 n = pmc_parse_mask(iap_rsp_mask_i7_wm, p, &rsp);
1018 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_SANDYBRIDGE ||
1019 cpu_info.pm_cputype == PMC_CPU_INTEL_SANDYBRIDGE_XEON ||
1020 cpu_info.pm_cputype == PMC_CPU_INTEL_IVYBRIDGE ||
1021 cpu_info.pm_cputype == PMC_CPU_INTEL_IVYBRIDGE_XEON ) {
1022 if (KWPREFIXMATCH(p, IAP_KW_RSP "=")) {
1023 n = pmc_parse_mask(iap_rsp_mask_sb_sbx_ib, p, &rsp);
1026 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_HASWELL ||
1027 cpu_info.pm_cputype == PMC_CPU_INTEL_HASWELL_XEON) {
1028 if (KWPREFIXMATCH(p, IAP_KW_RSP "=")) {
1029 n = pmc_parse_mask(iap_rsp_mask_haswell, p, &rsp);
1032 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_BROADWELL ||
1033 cpu_info.pm_cputype == PMC_CPU_INTEL_BROADWELL_XEON) {
1034 /* Broadwell is defined to use same mask as haswell */
1035 if (KWPREFIXMATCH(p, IAP_KW_RSP "=")) {
1036 n = pmc_parse_mask(iap_rsp_mask_haswell, p, &rsp);
1040 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_SKYLAKE) {
1041 if (KWPREFIXMATCH(p, IAP_KW_RSP "=")) {
1042 n = pmc_parse_mask(iap_rsp_mask_skylake, p, &rsp);
1049 if (n < 0) /* Parsing failed. */
1053 pmc_config->pm_md.pm_iap.pm_iap_config |= evmask;
1056 * If the event requires a 'cachestate' qualifier but was not
1057 * specified by the user, use a sensible default.
1060 case PMC_EV_IAP_EVENT_28H: /* Core, Core2, Atom */
1061 case PMC_EV_IAP_EVENT_29H: /* Core, Core2, Atom */
1062 case PMC_EV_IAP_EVENT_2AH: /* Core, Core2, Atom */
1063 case PMC_EV_IAP_EVENT_2BH: /* Atom, Core2 */
1064 case PMC_EV_IAP_EVENT_2EH: /* Core, Core2, Atom */
1065 case PMC_EV_IAP_EVENT_30H: /* Core, Core2, Atom */
1066 case PMC_EV_IAP_EVENT_32H: /* Core */
1067 case PMC_EV_IAP_EVENT_40H: /* Core */
1068 case PMC_EV_IAP_EVENT_41H: /* Core */
1069 case PMC_EV_IAP_EVENT_42H: /* Core, Core2, Atom */
1070 if (cachestate == 0)
1071 cachestate = (0xF << 8);
1073 case PMC_EV_IAP_EVENT_77H: /* Atom */
1074 /* IAP_EVENT_77H only accepts a cachestate qualifier on the
1077 if(cpu_info.pm_cputype == PMC_CPU_INTEL_ATOM && cachestate == 0)
1078 cachestate = (0xF << 8);
1084 pmc_config->pm_md.pm_iap.pm_iap_config |= cachestate;
1085 pmc_config->pm_md.pm_iap.pm_iap_rsp = rsp;
1095 ucf_allocate_pmc(enum pmc_event pe, char *ctrspec,
1096 struct pmc_op_pmcallocate *pmc_config)
1101 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1102 pmc_config->pm_md.pm_ucf.pm_ucf_flags = 0;
1107 #define UCP_KW_CMASK "cmask"
1108 #define UCP_KW_EDGE "edge"
1109 #define UCP_KW_INV "inv"
1112 ucp_allocate_pmc(enum pmc_event pe, char *ctrspec,
1113 struct pmc_op_pmcallocate *pmc_config)
1120 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE |
1122 pmc_config->pm_md.pm_ucp.pm_ucp_config = 0;
1124 /* Parse additional modifiers if present */
1125 while ((p = strsep(&ctrspec, ",")) != NULL) {
1128 if (KWPREFIXMATCH(p, UCP_KW_CMASK "=")) {
1130 if (*++q == '\0') /* skip '=' */
1132 count = strtol(q, &e, 0);
1133 if (e == q || *e != '\0')
1135 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
1136 pmc_config->pm_md.pm_ucp.pm_ucp_config |=
1138 } else if (KWMATCH(p, UCP_KW_EDGE)) {
1139 pmc_config->pm_caps |= PMC_CAP_EDGE;
1140 } else if (KWMATCH(p, UCP_KW_INV)) {
1141 pmc_config->pm_caps |= PMC_CAP_INVERT;
1145 if (n < 0) /* Parsing failed. */
1155 * These are very similar to AMD K7 PMCs, but support more kinds of
1159 static struct pmc_event_alias k8_aliases[] = {
1160 EV_ALIAS("branches", "k8-fr-retired-taken-branches"),
1161 EV_ALIAS("branch-mispredicts",
1162 "k8-fr-retired-taken-branches-mispredicted"),
1163 EV_ALIAS("cycles", "tsc"),
1164 EV_ALIAS("dc-misses", "k8-dc-miss"),
1165 EV_ALIAS("ic-misses", "k8-ic-miss"),
1166 EV_ALIAS("instructions", "k8-fr-retired-x86-instructions"),
1167 EV_ALIAS("interrupts", "k8-fr-taken-hardware-interrupts"),
1168 EV_ALIAS("unhalted-cycles", "k8-bu-cpu-clk-unhalted"),
1169 EV_ALIAS(NULL, NULL)
1172 #define __K8MASK(N,V) PMCMASK(N,(1 << (V)))
1178 /* fp dispatched fpu ops */
1179 static const struct pmc_masks k8_mask_fdfo[] = {
1180 __K8MASK(add-pipe-excluding-junk-ops, 0),
1181 __K8MASK(multiply-pipe-excluding-junk-ops, 1),
1182 __K8MASK(store-pipe-excluding-junk-ops, 2),
1183 __K8MASK(add-pipe-junk-ops, 3),
1184 __K8MASK(multiply-pipe-junk-ops, 4),
1185 __K8MASK(store-pipe-junk-ops, 5),
1189 /* ls segment register loads */
1190 static const struct pmc_masks k8_mask_lsrl[] = {
1201 /* ls locked operation */
1202 static const struct pmc_masks k8_mask_llo[] = {
1203 __K8MASK(locked-instructions, 0),
1204 __K8MASK(cycles-in-request, 1),
1205 __K8MASK(cycles-to-complete, 2),
1209 /* dc refill from {l2,system} and dc copyback */
1210 static const struct pmc_masks k8_mask_dc[] = {
1211 __K8MASK(invalid, 0),
1212 __K8MASK(shared, 1),
1213 __K8MASK(exclusive, 2),
1215 __K8MASK(modified, 4),
1219 /* dc one bit ecc error */
1220 static const struct pmc_masks k8_mask_dobee[] = {
1221 __K8MASK(scrubber, 0),
1222 __K8MASK(piggyback, 1),
1226 /* dc dispatched prefetch instructions */
1227 static const struct pmc_masks k8_mask_ddpi[] = {
1234 /* dc dcache accesses by locks */
1235 static const struct pmc_masks k8_mask_dabl[] = {
1236 __K8MASK(accesses, 0),
1237 __K8MASK(misses, 1),
1241 /* bu internal l2 request */
1242 static const struct pmc_masks k8_mask_bilr[] = {
1243 __K8MASK(ic-fill, 0),
1244 __K8MASK(dc-fill, 1),
1245 __K8MASK(tlb-reload, 2),
1246 __K8MASK(tag-snoop, 3),
1247 __K8MASK(cancelled, 4),
1251 /* bu fill request l2 miss */
1252 static const struct pmc_masks k8_mask_bfrlm[] = {
1253 __K8MASK(ic-fill, 0),
1254 __K8MASK(dc-fill, 1),
1255 __K8MASK(tlb-reload, 2),
1259 /* bu fill into l2 */
1260 static const struct pmc_masks k8_mask_bfil[] = {
1261 __K8MASK(dirty-l2-victim, 0),
1262 __K8MASK(victim-from-l2, 1),
1266 /* fr retired fpu instructions */
1267 static const struct pmc_masks k8_mask_frfi[] = {
1269 __K8MASK(mmx-3dnow, 1),
1270 __K8MASK(packed-sse-sse2, 2),
1271 __K8MASK(scalar-sse-sse2, 3),
1275 /* fr retired fastpath double op instructions */
1276 static const struct pmc_masks k8_mask_frfdoi[] = {
1277 __K8MASK(low-op-pos-0, 0),
1278 __K8MASK(low-op-pos-1, 1),
1279 __K8MASK(low-op-pos-2, 2),
1283 /* fr fpu exceptions */
1284 static const struct pmc_masks k8_mask_ffe[] = {
1285 __K8MASK(x87-reclass-microfaults, 0),
1286 __K8MASK(sse-retype-microfaults, 1),
1287 __K8MASK(sse-reclass-microfaults, 2),
1288 __K8MASK(sse-and-x87-microtraps, 3),
1292 /* nb memory controller page access event */
1293 static const struct pmc_masks k8_mask_nmcpae[] = {
1294 __K8MASK(page-hit, 0),
1295 __K8MASK(page-miss, 1),
1296 __K8MASK(page-conflict, 2),
1300 /* nb memory controller turnaround */
1301 static const struct pmc_masks k8_mask_nmct[] = {
1302 __K8MASK(dimm-turnaround, 0),
1303 __K8MASK(read-to-write-turnaround, 1),
1304 __K8MASK(write-to-read-turnaround, 2),
1308 /* nb memory controller bypass saturation */
1309 static const struct pmc_masks k8_mask_nmcbs[] = {
1310 __K8MASK(memory-controller-hi-pri-bypass, 0),
1311 __K8MASK(memory-controller-lo-pri-bypass, 1),
1312 __K8MASK(dram-controller-interface-bypass, 2),
1313 __K8MASK(dram-controller-queue-bypass, 3),
1317 /* nb sized commands */
1318 static const struct pmc_masks k8_mask_nsc[] = {
1319 __K8MASK(nonpostwrszbyte, 0),
1320 __K8MASK(nonpostwrszdword, 1),
1321 __K8MASK(postwrszbyte, 2),
1322 __K8MASK(postwrszdword, 3),
1323 __K8MASK(rdszbyte, 4),
1324 __K8MASK(rdszdword, 5),
1325 __K8MASK(rdmodwr, 6),
1329 /* nb probe result */
1330 static const struct pmc_masks k8_mask_npr[] = {
1331 __K8MASK(probe-miss, 0),
1332 __K8MASK(probe-hit, 1),
1333 __K8MASK(probe-hit-dirty-no-memory-cancel, 2),
1334 __K8MASK(probe-hit-dirty-with-memory-cancel, 3),
1338 /* nb hypertransport bus bandwidth */
1339 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
1340 __K8MASK(command, 0),
1342 __K8MASK(buffer-release, 2),
1349 #define K8_KW_COUNT "count"
1350 #define K8_KW_EDGE "edge"
1351 #define K8_KW_INV "inv"
1352 #define K8_KW_MASK "mask"
1353 #define K8_KW_OS "os"
1354 #define K8_KW_USR "usr"
1357 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
1358 struct pmc_op_pmcallocate *pmc_config)
1364 const struct pmc_masks *pm, *pmask;
1366 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1367 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
1372 #define __K8SETMASK(M) pmask = k8_mask_##M
1374 /* setup parsing tables */
1376 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
1379 case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
1382 case PMC_EV_K8_LS_LOCKED_OPERATION:
1385 case PMC_EV_K8_DC_REFILL_FROM_L2:
1386 case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
1387 case PMC_EV_K8_DC_COPYBACK:
1390 case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
1393 case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
1396 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
1399 case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
1402 case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
1405 case PMC_EV_K8_BU_FILL_INTO_L2:
1408 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
1411 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
1412 __K8SETMASK(frfdoi);
1414 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
1417 case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
1418 __K8SETMASK(nmcpae);
1420 case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
1423 case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
1426 case PMC_EV_K8_NB_SIZED_COMMANDS:
1429 case PMC_EV_K8_NB_PROBE_RESULT:
1432 case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
1433 case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
1434 case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
1439 break; /* no options defined */
1442 while ((p = strsep(&ctrspec, ",")) != NULL) {
1443 if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
1445 if (*++q == '\0') /* skip '=' */
1448 count = strtol(q, &e, 0);
1449 if (e == q || *e != '\0')
1452 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
1453 pmc_config->pm_md.pm_amd.pm_amd_config |=
1454 AMD_PMC_TO_COUNTER(count);
1456 } else if (KWMATCH(p, K8_KW_EDGE)) {
1457 pmc_config->pm_caps |= PMC_CAP_EDGE;
1458 } else if (KWMATCH(p, K8_KW_INV)) {
1459 pmc_config->pm_caps |= PMC_CAP_INVERT;
1460 } else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
1461 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
1463 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1464 } else if (KWMATCH(p, K8_KW_OS)) {
1465 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
1466 } else if (KWMATCH(p, K8_KW_USR)) {
1467 pmc_config->pm_caps |= PMC_CAP_USER;
1472 /* other post processing */
1474 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
1475 case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
1476 case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
1477 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
1478 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
1479 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
1480 /* XXX only available in rev B and later */
1482 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
1483 /* XXX only available in rev C and later */
1485 case PMC_EV_K8_LS_LOCKED_OPERATION:
1486 /* XXX CPU Rev A,B evmask is to be zero */
1487 if (evmask & (evmask - 1)) /* > 1 bit set */
1490 evmask = 0x01; /* Rev C and later: #instrs */
1491 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1495 if (evmask == 0 && pmask != NULL) {
1496 for (pm = pmask; pm->pm_name; pm++)
1497 evmask |= pm->pm_value;
1498 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1502 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
1503 pmc_config->pm_md.pm_amd.pm_amd_config =
1504 AMD_PMC_TO_UNITMASK(evmask);
1511 #if defined(__amd64__) || defined(__i386__)
1517 static struct pmc_event_alias p4_aliases[] = {
1518 EV_ALIAS("branches", "p4-branch-retired,mask=mmtp+mmtm"),
1519 EV_ALIAS("branch-mispredicts", "p4-mispred-branch-retired"),
1520 EV_ALIAS("cycles", "tsc"),
1521 EV_ALIAS("instructions",
1522 "p4-instr-retired,mask=nbogusntag+nbogustag"),
1523 EV_ALIAS("unhalted-cycles", "p4-global-power-events"),
1524 EV_ALIAS(NULL, NULL)
1527 #define P4_KW_ACTIVE "active"
1528 #define P4_KW_ACTIVE_ANY "any"
1529 #define P4_KW_ACTIVE_BOTH "both"
1530 #define P4_KW_ACTIVE_NONE "none"
1531 #define P4_KW_ACTIVE_SINGLE "single"
1532 #define P4_KW_BUSREQTYPE "busreqtype"
1533 #define P4_KW_CASCADE "cascade"
1534 #define P4_KW_EDGE "edge"
1535 #define P4_KW_INV "complement"
1536 #define P4_KW_OS "os"
1537 #define P4_KW_MASK "mask"
1538 #define P4_KW_PRECISE "precise"
1539 #define P4_KW_TAG "tag"
1540 #define P4_KW_THRESHOLD "threshold"
1541 #define P4_KW_USR "usr"
1543 #define __P4MASK(N,V) PMCMASK(N, (1 << (V)))
1545 static const struct pmc_masks p4_mask_tcdm[] = { /* tc deliver mode */
1557 static const struct pmc_masks p4_mask_bfr[] = { /* bpu fetch request */
1558 __P4MASK(tcmiss, 0),
1562 static const struct pmc_masks p4_mask_ir[] = { /* itlb reference */
1565 __P4MASK(hit-uc, 2),
1569 static const struct pmc_masks p4_mask_memcan[] = { /* memory cancel */
1570 __P4MASK(st-rb-full, 2),
1571 __P4MASK(64k-conf, 3),
1575 static const struct pmc_masks p4_mask_memcomp[] = { /* memory complete */
1581 static const struct pmc_masks p4_mask_lpr[] = { /* load port replay */
1582 __P4MASK(split-ld, 1),
1586 static const struct pmc_masks p4_mask_spr[] = { /* store port replay */
1587 __P4MASK(split-st, 1),
1591 static const struct pmc_masks p4_mask_mlr[] = { /* mob load replay */
1592 __P4MASK(no-sta, 1),
1593 __P4MASK(no-std, 3),
1594 __P4MASK(partial-data, 4),
1595 __P4MASK(unalgn-addr, 5),
1599 static const struct pmc_masks p4_mask_pwt[] = { /* page walk type */
1600 __P4MASK(dtmiss, 0),
1601 __P4MASK(itmiss, 1),
1605 static const struct pmc_masks p4_mask_bcr[] = { /* bsq cache reference */
1606 __P4MASK(rd-2ndl-hits, 0),
1607 __P4MASK(rd-2ndl-hite, 1),
1608 __P4MASK(rd-2ndl-hitm, 2),
1609 __P4MASK(rd-3rdl-hits, 3),
1610 __P4MASK(rd-3rdl-hite, 4),
1611 __P4MASK(rd-3rdl-hitm, 5),
1612 __P4MASK(rd-2ndl-miss, 8),
1613 __P4MASK(rd-3rdl-miss, 9),
1614 __P4MASK(wr-2ndl-miss, 10),
1618 static const struct pmc_masks p4_mask_ia[] = { /* ioq allocation */
1619 __P4MASK(all-read, 5),
1620 __P4MASK(all-write, 6),
1621 __P4MASK(mem-uc, 7),
1622 __P4MASK(mem-wc, 8),
1623 __P4MASK(mem-wt, 9),
1624 __P4MASK(mem-wp, 10),
1625 __P4MASK(mem-wb, 11),
1627 __P4MASK(other, 14),
1628 __P4MASK(prefetch, 15),
1632 static const struct pmc_masks p4_mask_iae[] = { /* ioq active entries */
1633 __P4MASK(all-read, 5),
1634 __P4MASK(all-write, 6),
1635 __P4MASK(mem-uc, 7),
1636 __P4MASK(mem-wc, 8),
1637 __P4MASK(mem-wt, 9),
1638 __P4MASK(mem-wp, 10),
1639 __P4MASK(mem-wb, 11),
1641 __P4MASK(other, 14),
1642 __P4MASK(prefetch, 15),
1646 static const struct pmc_masks p4_mask_fda[] = { /* fsb data activity */
1647 __P4MASK(drdy-drv, 0),
1648 __P4MASK(drdy-own, 1),
1649 __P4MASK(drdy-other, 2),
1650 __P4MASK(dbsy-drv, 3),
1651 __P4MASK(dbsy-own, 4),
1652 __P4MASK(dbsy-other, 5),
1656 static const struct pmc_masks p4_mask_ba[] = { /* bsq allocation */
1657 __P4MASK(req-type0, 0),
1658 __P4MASK(req-type1, 1),
1659 __P4MASK(req-len0, 2),
1660 __P4MASK(req-len1, 3),
1661 __P4MASK(req-io-type, 5),
1662 __P4MASK(req-lock-type, 6),
1663 __P4MASK(req-cache-type, 7),
1664 __P4MASK(req-split-type, 8),
1665 __P4MASK(req-dem-type, 9),
1666 __P4MASK(req-ord-type, 10),
1667 __P4MASK(mem-type0, 11),
1668 __P4MASK(mem-type1, 12),
1669 __P4MASK(mem-type2, 13),
1673 static const struct pmc_masks p4_mask_sia[] = { /* sse input assist */
1678 static const struct pmc_masks p4_mask_psu[] = { /* packed sp uop */
1683 static const struct pmc_masks p4_mask_pdu[] = { /* packed dp uop */
1688 static const struct pmc_masks p4_mask_ssu[] = { /* scalar sp uop */
1693 static const struct pmc_masks p4_mask_sdu[] = { /* scalar dp uop */
1698 static const struct pmc_masks p4_mask_64bmu[] = { /* 64 bit mmx uop */
1703 static const struct pmc_masks p4_mask_128bmu[] = { /* 128 bit mmx uop */
1708 static const struct pmc_masks p4_mask_xfu[] = { /* X87 fp uop */
1713 static const struct pmc_masks p4_mask_xsmu[] = { /* x87 simd moves uop */
1719 static const struct pmc_masks p4_mask_gpe[] = { /* global power events */
1720 __P4MASK(running, 0),
1724 static const struct pmc_masks p4_mask_tmx[] = { /* TC ms xfer */
1729 static const struct pmc_masks p4_mask_uqw[] = { /* uop queue writes */
1730 __P4MASK(from-tc-build, 0),
1731 __P4MASK(from-tc-deliver, 1),
1732 __P4MASK(from-rom, 2),
1736 static const struct pmc_masks p4_mask_rmbt[] = {
1737 /* retired mispred branch type */
1738 __P4MASK(conditional, 1),
1740 __P4MASK(return, 3),
1741 __P4MASK(indirect, 4),
1745 static const struct pmc_masks p4_mask_rbt[] = { /* retired branch type */
1746 __P4MASK(conditional, 1),
1748 __P4MASK(retired, 3),
1749 __P4MASK(indirect, 4),
1753 static const struct pmc_masks p4_mask_rs[] = { /* resource stall */
1754 __P4MASK(sbfull, 5),
1758 static const struct pmc_masks p4_mask_wb[] = { /* WC buffer */
1759 __P4MASK(wcb-evicts, 0),
1760 __P4MASK(wcb-full-evict, 1),
1764 static const struct pmc_masks p4_mask_fee[] = { /* front end event */
1765 __P4MASK(nbogus, 0),
1770 static const struct pmc_masks p4_mask_ee[] = { /* execution event */
1771 __P4MASK(nbogus0, 0),
1772 __P4MASK(nbogus1, 1),
1773 __P4MASK(nbogus2, 2),
1774 __P4MASK(nbogus3, 3),
1775 __P4MASK(bogus0, 4),
1776 __P4MASK(bogus1, 5),
1777 __P4MASK(bogus2, 6),
1778 __P4MASK(bogus3, 7),
1782 static const struct pmc_masks p4_mask_re[] = { /* replay event */
1783 __P4MASK(nbogus, 0),
1788 static const struct pmc_masks p4_mask_insret[] = { /* instr retired */
1789 __P4MASK(nbogusntag, 0),
1790 __P4MASK(nbogustag, 1),
1791 __P4MASK(bogusntag, 2),
1792 __P4MASK(bogustag, 3),
1796 static const struct pmc_masks p4_mask_ur[] = { /* uops retired */
1797 __P4MASK(nbogus, 0),
1802 static const struct pmc_masks p4_mask_ut[] = { /* uop type */
1803 __P4MASK(tagloads, 1),
1804 __P4MASK(tagstores, 2),
1808 static const struct pmc_masks p4_mask_br[] = { /* branch retired */
1816 static const struct pmc_masks p4_mask_mbr[] = { /* mispred branch retired */
1817 __P4MASK(nbogus, 0),
1821 static const struct pmc_masks p4_mask_xa[] = { /* x87 assist */
1830 static const struct pmc_masks p4_mask_machclr[] = { /* machine clear */
1832 __P4MASK(moclear, 2),
1833 __P4MASK(smclear, 3),
1837 /* P4 event parser */
1839 p4_allocate_pmc(enum pmc_event pe, char *ctrspec,
1840 struct pmc_op_pmcallocate *pmc_config)
1844 int count, has_tag, has_busreqtype, n;
1845 uint32_t cccractivemask;
1847 const struct pmc_masks *pm, *pmask;
1849 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1850 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig =
1851 pmc_config->pm_md.pm_p4.pm_p4_escrconfig = 0;
1855 cccractivemask = 0x3;
1856 has_tag = has_busreqtype = 0;
1858 #define __P4SETMASK(M) do { \
1859 pmask = p4_mask_##M; \
1863 case PMC_EV_P4_TC_DELIVER_MODE:
1866 case PMC_EV_P4_BPU_FETCH_REQUEST:
1869 case PMC_EV_P4_ITLB_REFERENCE:
1872 case PMC_EV_P4_MEMORY_CANCEL:
1873 __P4SETMASK(memcan);
1875 case PMC_EV_P4_MEMORY_COMPLETE:
1876 __P4SETMASK(memcomp);
1878 case PMC_EV_P4_LOAD_PORT_REPLAY:
1881 case PMC_EV_P4_STORE_PORT_REPLAY:
1884 case PMC_EV_P4_MOB_LOAD_REPLAY:
1887 case PMC_EV_P4_PAGE_WALK_TYPE:
1890 case PMC_EV_P4_BSQ_CACHE_REFERENCE:
1893 case PMC_EV_P4_IOQ_ALLOCATION:
1897 case PMC_EV_P4_IOQ_ACTIVE_ENTRIES:
1901 case PMC_EV_P4_FSB_DATA_ACTIVITY:
1904 case PMC_EV_P4_BSQ_ALLOCATION:
1907 case PMC_EV_P4_SSE_INPUT_ASSIST:
1910 case PMC_EV_P4_PACKED_SP_UOP:
1913 case PMC_EV_P4_PACKED_DP_UOP:
1916 case PMC_EV_P4_SCALAR_SP_UOP:
1919 case PMC_EV_P4_SCALAR_DP_UOP:
1922 case PMC_EV_P4_64BIT_MMX_UOP:
1925 case PMC_EV_P4_128BIT_MMX_UOP:
1926 __P4SETMASK(128bmu);
1928 case PMC_EV_P4_X87_FP_UOP:
1931 case PMC_EV_P4_X87_SIMD_MOVES_UOP:
1934 case PMC_EV_P4_GLOBAL_POWER_EVENTS:
1937 case PMC_EV_P4_TC_MS_XFER:
1940 case PMC_EV_P4_UOP_QUEUE_WRITES:
1943 case PMC_EV_P4_RETIRED_MISPRED_BRANCH_TYPE:
1946 case PMC_EV_P4_RETIRED_BRANCH_TYPE:
1949 case PMC_EV_P4_RESOURCE_STALL:
1952 case PMC_EV_P4_WC_BUFFER:
1955 case PMC_EV_P4_BSQ_ACTIVE_ENTRIES:
1956 case PMC_EV_P4_B2B_CYCLES:
1958 case PMC_EV_P4_SNOOP:
1959 case PMC_EV_P4_RESPONSE:
1961 case PMC_EV_P4_FRONT_END_EVENT:
1964 case PMC_EV_P4_EXECUTION_EVENT:
1967 case PMC_EV_P4_REPLAY_EVENT:
1970 case PMC_EV_P4_INSTR_RETIRED:
1971 __P4SETMASK(insret);
1973 case PMC_EV_P4_UOPS_RETIRED:
1976 case PMC_EV_P4_UOP_TYPE:
1979 case PMC_EV_P4_BRANCH_RETIRED:
1982 case PMC_EV_P4_MISPRED_BRANCH_RETIRED:
1985 case PMC_EV_P4_X87_ASSIST:
1988 case PMC_EV_P4_MACHINE_CLEAR:
1989 __P4SETMASK(machclr);
1995 /* process additional flags */
1996 while ((p = strsep(&ctrspec, ",")) != NULL) {
1997 if (KWPREFIXMATCH(p, P4_KW_ACTIVE)) {
1999 if (*++q == '\0') /* skip '=' */
2002 if (strcasecmp(q, P4_KW_ACTIVE_NONE) == 0)
2003 cccractivemask = 0x0;
2004 else if (strcasecmp(q, P4_KW_ACTIVE_SINGLE) == 0)
2005 cccractivemask = 0x1;
2006 else if (strcasecmp(q, P4_KW_ACTIVE_BOTH) == 0)
2007 cccractivemask = 0x2;
2008 else if (strcasecmp(q, P4_KW_ACTIVE_ANY) == 0)
2009 cccractivemask = 0x3;
2013 } else if (KWPREFIXMATCH(p, P4_KW_BUSREQTYPE)) {
2014 if (has_busreqtype == 0)
2018 if (*++q == '\0') /* skip '=' */
2021 count = strtol(q, &e, 0);
2022 if (e == q || *e != '\0')
2024 evmask = (evmask & ~0x1F) | (count & 0x1F);
2025 } else if (KWMATCH(p, P4_KW_CASCADE))
2026 pmc_config->pm_caps |= PMC_CAP_CASCADE;
2027 else if (KWMATCH(p, P4_KW_EDGE))
2028 pmc_config->pm_caps |= PMC_CAP_EDGE;
2029 else if (KWMATCH(p, P4_KW_INV))
2030 pmc_config->pm_caps |= PMC_CAP_INVERT;
2031 else if (KWPREFIXMATCH(p, P4_KW_MASK "=")) {
2032 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
2034 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2035 } else if (KWMATCH(p, P4_KW_OS))
2036 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2037 else if (KWMATCH(p, P4_KW_PRECISE))
2038 pmc_config->pm_caps |= PMC_CAP_PRECISE;
2039 else if (KWPREFIXMATCH(p, P4_KW_TAG "=")) {
2044 if (*++q == '\0') /* skip '=' */
2047 count = strtol(q, &e, 0);
2048 if (e == q || *e != '\0')
2051 pmc_config->pm_caps |= PMC_CAP_TAGGING;
2052 pmc_config->pm_md.pm_p4.pm_p4_escrconfig |=
2053 P4_ESCR_TO_TAG_VALUE(count);
2054 } else if (KWPREFIXMATCH(p, P4_KW_THRESHOLD "=")) {
2056 if (*++q == '\0') /* skip '=' */
2059 count = strtol(q, &e, 0);
2060 if (e == q || *e != '\0')
2063 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
2064 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig &=
2065 ~P4_CCCR_THRESHOLD_MASK;
2066 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig |=
2067 P4_CCCR_TO_THRESHOLD(count);
2068 } else if (KWMATCH(p, P4_KW_USR))
2069 pmc_config->pm_caps |= PMC_CAP_USER;
2074 /* other post processing */
2075 if (pe == PMC_EV_P4_IOQ_ALLOCATION ||
2076 pe == PMC_EV_P4_FSB_DATA_ACTIVITY ||
2077 pe == PMC_EV_P4_BSQ_ALLOCATION)
2078 pmc_config->pm_caps |= PMC_CAP_EDGE;
2080 /* fill in thread activity mask */
2081 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig |=
2082 P4_CCCR_TO_ACTIVE_THREAD(cccractivemask);
2085 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2088 case PMC_EV_P4_FSB_DATA_ACTIVITY:
2089 if ((evmask & 0x06) == 0x06 ||
2090 (evmask & 0x18) == 0x18)
2091 return (-1); /* can't have own+other bits together */
2092 if (evmask == 0) /* default:drdy-{drv,own}+dbsy{drv,own} */
2095 case PMC_EV_P4_MACHINE_CLEAR:
2096 /* only one bit is allowed to be set */
2097 if ((evmask & (evmask - 1)) != 0)
2100 evmask = 0x1; /* 'CLEAR' */
2101 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2105 if (evmask == 0 && pmask) {
2106 for (pm = pmask; pm->pm_name; pm++)
2107 evmask |= pm->pm_value;
2108 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2112 pmc_config->pm_md.pm_p4.pm_p4_escrconfig =
2113 P4_ESCR_TO_EVENT_MASK(evmask);
2120 #if defined(__i386__)
2123 * Pentium style PMCs
2126 static struct pmc_event_alias p5_aliases[] = {
2127 EV_ALIAS("branches", "p5-taken-branches"),
2128 EV_ALIAS("cycles", "tsc"),
2129 EV_ALIAS("dc-misses", "p5-data-read-miss-or-write-miss"),
2130 EV_ALIAS("ic-misses", "p5-code-cache-miss"),
2131 EV_ALIAS("instructions", "p5-instructions-executed"),
2132 EV_ALIAS("interrupts", "p5-hardware-interrupts"),
2133 EV_ALIAS("unhalted-cycles",
2134 "p5-number-of-cycles-not-in-halt-state"),
2135 EV_ALIAS(NULL, NULL)
2139 p5_allocate_pmc(enum pmc_event pe, char *ctrspec,
2140 struct pmc_op_pmcallocate *pmc_config)
2142 return (-1 || pe || ctrspec || pmc_config); /* shut up gcc */
2146 * Pentium Pro style PMCs. These PMCs are found in Pentium II, Pentium III,
2147 * and Pentium M CPUs.
2150 static struct pmc_event_alias p6_aliases[] = {
2151 EV_ALIAS("branches", "p6-br-inst-retired"),
2152 EV_ALIAS("branch-mispredicts", "p6-br-miss-pred-retired"),
2153 EV_ALIAS("cycles", "tsc"),
2154 EV_ALIAS("dc-misses", "p6-dcu-lines-in"),
2155 EV_ALIAS("ic-misses", "p6-ifu-fetch-miss"),
2156 EV_ALIAS("instructions", "p6-inst-retired"),
2157 EV_ALIAS("interrupts", "p6-hw-int-rx"),
2158 EV_ALIAS("unhalted-cycles", "p6-cpu-clk-unhalted"),
2159 EV_ALIAS(NULL, NULL)
2162 #define P6_KW_CMASK "cmask"
2163 #define P6_KW_EDGE "edge"
2164 #define P6_KW_INV "inv"
2165 #define P6_KW_OS "os"
2166 #define P6_KW_UMASK "umask"
2167 #define P6_KW_USR "usr"
2169 static struct pmc_masks p6_mask_mesi[] = {
2177 static struct pmc_masks p6_mask_mesihw[] = {
2182 PMCMASK(nonhw, 0x00),
2184 PMCMASK(both, 0x30),
2188 static struct pmc_masks p6_mask_hw[] = {
2189 PMCMASK(nonhw, 0x00),
2191 PMCMASK(both, 0x30),
2195 static struct pmc_masks p6_mask_any[] = {
2196 PMCMASK(self, 0x00),
2201 static struct pmc_masks p6_mask_ekp[] = {
2209 static struct pmc_masks p6_mask_pps[] = {
2210 PMCMASK(packed-and-scalar, 0x00),
2211 PMCMASK(scalar, 0x01),
2215 static struct pmc_masks p6_mask_mite[] = {
2216 PMCMASK(packed-multiply, 0x01),
2217 PMCMASK(packed-shift, 0x02),
2218 PMCMASK(pack, 0x04),
2219 PMCMASK(unpack, 0x08),
2220 PMCMASK(packed-logical, 0x10),
2221 PMCMASK(packed-arithmetic, 0x20),
2225 static struct pmc_masks p6_mask_fmt[] = {
2226 PMCMASK(mmxtofp, 0x00),
2227 PMCMASK(fptommx, 0x01),
2231 static struct pmc_masks p6_mask_sr[] = {
2239 static struct pmc_masks p6_mask_eet[] = {
2241 PMCMASK(freq, 0x02),
2245 static struct pmc_masks p6_mask_efur[] = {
2247 PMCMASK(loadop, 0x01),
2248 PMCMASK(stdsta, 0x02),
2252 static struct pmc_masks p6_mask_essir[] = {
2253 PMCMASK(sse-packed-single, 0x00),
2254 PMCMASK(sse-packed-single-scalar-single, 0x01),
2255 PMCMASK(sse2-packed-double, 0x02),
2256 PMCMASK(sse2-scalar-double, 0x03),
2260 static struct pmc_masks p6_mask_esscir[] = {
2261 PMCMASK(sse-packed-single, 0x00),
2262 PMCMASK(sse-scalar-single, 0x01),
2263 PMCMASK(sse2-packed-double, 0x02),
2264 PMCMASK(sse2-scalar-double, 0x03),
2268 /* P6 event parser */
2270 p6_allocate_pmc(enum pmc_event pe, char *ctrspec,
2271 struct pmc_op_pmcallocate *pmc_config)
2276 const struct pmc_masks *pm, *pmask;
2278 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2279 pmc_config->pm_md.pm_ppro.pm_ppro_config = 0;
2283 #define P6MASKSET(M) pmask = p6_mask_ ## M
2286 case PMC_EV_P6_L2_IFETCH: P6MASKSET(mesi); break;
2287 case PMC_EV_P6_L2_LD: P6MASKSET(mesi); break;
2288 case PMC_EV_P6_L2_ST: P6MASKSET(mesi); break;
2289 case PMC_EV_P6_L2_RQSTS: P6MASKSET(mesi); break;
2290 case PMC_EV_P6_BUS_DRDY_CLOCKS:
2291 case PMC_EV_P6_BUS_LOCK_CLOCKS:
2292 case PMC_EV_P6_BUS_TRAN_BRD:
2293 case PMC_EV_P6_BUS_TRAN_RFO:
2294 case PMC_EV_P6_BUS_TRANS_WB:
2295 case PMC_EV_P6_BUS_TRAN_IFETCH:
2296 case PMC_EV_P6_BUS_TRAN_INVAL:
2297 case PMC_EV_P6_BUS_TRAN_PWR:
2298 case PMC_EV_P6_BUS_TRANS_P:
2299 case PMC_EV_P6_BUS_TRANS_IO:
2300 case PMC_EV_P6_BUS_TRAN_DEF:
2301 case PMC_EV_P6_BUS_TRAN_BURST:
2302 case PMC_EV_P6_BUS_TRAN_ANY:
2303 case PMC_EV_P6_BUS_TRAN_MEM:
2304 P6MASKSET(any); break;
2305 case PMC_EV_P6_EMON_KNI_PREF_DISPATCHED:
2306 case PMC_EV_P6_EMON_KNI_PREF_MISS:
2307 P6MASKSET(ekp); break;
2308 case PMC_EV_P6_EMON_KNI_INST_RETIRED:
2309 case PMC_EV_P6_EMON_KNI_COMP_INST_RET:
2310 P6MASKSET(pps); break;
2311 case PMC_EV_P6_MMX_INSTR_TYPE_EXEC:
2312 P6MASKSET(mite); break;
2313 case PMC_EV_P6_FP_MMX_TRANS:
2314 P6MASKSET(fmt); break;
2315 case PMC_EV_P6_SEG_RENAME_STALLS:
2316 case PMC_EV_P6_SEG_REG_RENAMES:
2317 P6MASKSET(sr); break;
2318 case PMC_EV_P6_EMON_EST_TRANS:
2319 P6MASKSET(eet); break;
2320 case PMC_EV_P6_EMON_FUSED_UOPS_RET:
2321 P6MASKSET(efur); break;
2322 case PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED:
2323 P6MASKSET(essir); break;
2324 case PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED:
2325 P6MASKSET(esscir); break;
2331 /* Pentium M PMCs have a few events with different semantics */
2332 if (cpu_info.pm_cputype == PMC_CPU_INTEL_PM) {
2333 if (pe == PMC_EV_P6_L2_LD ||
2334 pe == PMC_EV_P6_L2_LINES_IN ||
2335 pe == PMC_EV_P6_L2_LINES_OUT)
2337 else if (pe == PMC_EV_P6_L2_M_LINES_OUTM)
2341 /* Parse additional modifiers if present */
2342 while ((p = strsep(&ctrspec, ",")) != NULL) {
2343 if (KWPREFIXMATCH(p, P6_KW_CMASK "=")) {
2345 if (*++q == '\0') /* skip '=' */
2347 count = strtol(q, &e, 0);
2348 if (e == q || *e != '\0')
2350 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
2351 pmc_config->pm_md.pm_ppro.pm_ppro_config |=
2352 P6_EVSEL_TO_CMASK(count);
2353 } else if (KWMATCH(p, P6_KW_EDGE)) {
2354 pmc_config->pm_caps |= PMC_CAP_EDGE;
2355 } else if (KWMATCH(p, P6_KW_INV)) {
2356 pmc_config->pm_caps |= PMC_CAP_INVERT;
2357 } else if (KWMATCH(p, P6_KW_OS)) {
2358 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2359 } else if (KWPREFIXMATCH(p, P6_KW_UMASK "=")) {
2361 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
2363 if ((pe == PMC_EV_P6_BUS_DRDY_CLOCKS ||
2364 pe == PMC_EV_P6_BUS_LOCK_CLOCKS ||
2365 pe == PMC_EV_P6_BUS_TRAN_BRD ||
2366 pe == PMC_EV_P6_BUS_TRAN_RFO ||
2367 pe == PMC_EV_P6_BUS_TRAN_IFETCH ||
2368 pe == PMC_EV_P6_BUS_TRAN_INVAL ||
2369 pe == PMC_EV_P6_BUS_TRAN_PWR ||
2370 pe == PMC_EV_P6_BUS_TRAN_DEF ||
2371 pe == PMC_EV_P6_BUS_TRAN_BURST ||
2372 pe == PMC_EV_P6_BUS_TRAN_ANY ||
2373 pe == PMC_EV_P6_BUS_TRAN_MEM ||
2374 pe == PMC_EV_P6_BUS_TRANS_IO ||
2375 pe == PMC_EV_P6_BUS_TRANS_P ||
2376 pe == PMC_EV_P6_BUS_TRANS_WB ||
2377 pe == PMC_EV_P6_EMON_EST_TRANS ||
2378 pe == PMC_EV_P6_EMON_FUSED_UOPS_RET ||
2379 pe == PMC_EV_P6_EMON_KNI_COMP_INST_RET ||
2380 pe == PMC_EV_P6_EMON_KNI_INST_RETIRED ||
2381 pe == PMC_EV_P6_EMON_KNI_PREF_DISPATCHED ||
2382 pe == PMC_EV_P6_EMON_KNI_PREF_MISS ||
2383 pe == PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED ||
2384 pe == PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED ||
2385 pe == PMC_EV_P6_FP_MMX_TRANS)
2386 && (n > 1)) /* Only one mask keyword is allowed. */
2388 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2389 } else if (KWMATCH(p, P6_KW_USR)) {
2390 pmc_config->pm_caps |= PMC_CAP_USER;
2395 /* post processing */
2399 * The following events default to an evmask of 0
2402 /* default => 'self' */
2403 case PMC_EV_P6_BUS_DRDY_CLOCKS:
2404 case PMC_EV_P6_BUS_LOCK_CLOCKS:
2405 case PMC_EV_P6_BUS_TRAN_BRD:
2406 case PMC_EV_P6_BUS_TRAN_RFO:
2407 case PMC_EV_P6_BUS_TRANS_WB:
2408 case PMC_EV_P6_BUS_TRAN_IFETCH:
2409 case PMC_EV_P6_BUS_TRAN_INVAL:
2410 case PMC_EV_P6_BUS_TRAN_PWR:
2411 case PMC_EV_P6_BUS_TRANS_P:
2412 case PMC_EV_P6_BUS_TRANS_IO:
2413 case PMC_EV_P6_BUS_TRAN_DEF:
2414 case PMC_EV_P6_BUS_TRAN_BURST:
2415 case PMC_EV_P6_BUS_TRAN_ANY:
2416 case PMC_EV_P6_BUS_TRAN_MEM:
2418 /* default => 'nta' */
2419 case PMC_EV_P6_EMON_KNI_PREF_DISPATCHED:
2420 case PMC_EV_P6_EMON_KNI_PREF_MISS:
2422 /* default => 'packed and scalar' */
2423 case PMC_EV_P6_EMON_KNI_INST_RETIRED:
2424 case PMC_EV_P6_EMON_KNI_COMP_INST_RET:
2426 /* default => 'mmx to fp transitions' */
2427 case PMC_EV_P6_FP_MMX_TRANS:
2429 /* default => 'SSE Packed Single' */
2430 case PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED:
2431 case PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED:
2433 /* default => 'all fused micro-ops' */
2434 case PMC_EV_P6_EMON_FUSED_UOPS_RET:
2436 /* default => 'all transitions' */
2437 case PMC_EV_P6_EMON_EST_TRANS:
2440 case PMC_EV_P6_MMX_UOPS_EXEC:
2441 evmask = 0x0F; /* only value allowed */
2446 * For all other events, set the default event mask
2447 * to a logical OR of all the allowed event mask bits.
2449 if (evmask == 0 && pmask) {
2450 for (pm = pmask; pm->pm_name; pm++)
2451 evmask |= pm->pm_value;
2452 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2458 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
2459 pmc_config->pm_md.pm_ppro.pm_ppro_config |=
2460 P6_EVSEL_TO_UMASK(evmask);
2467 #if defined(__i386__) || defined(__amd64__)
2469 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
2470 struct pmc_op_pmcallocate *pmc_config)
2472 if (pe != PMC_EV_TSC_TSC)
2475 /* TSC events must be unqualified. */
2476 if (ctrspec && *ctrspec != '\0')
2479 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
2480 pmc_config->pm_caps |= PMC_CAP_READ;
2486 static struct pmc_event_alias generic_aliases[] = {
2487 EV_ALIAS("instructions", "SOFT-CLOCK.HARD"),
2488 EV_ALIAS(NULL, NULL)
2492 soft_allocate_pmc(enum pmc_event pe, char *ctrspec,
2493 struct pmc_op_pmcallocate *pmc_config)
2498 if ((int)pe < PMC_EV_SOFT_FIRST || (int)pe > PMC_EV_SOFT_LAST)
2501 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2505 #if defined(__arm__)
2506 #if defined(__XSCALE__)
2508 static struct pmc_event_alias xscale_aliases[] = {
2509 EV_ALIAS("branches", "BRANCH_RETIRED"),
2510 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
2511 EV_ALIAS("dc-misses", "DC_MISS"),
2512 EV_ALIAS("ic-misses", "IC_MISS"),
2513 EV_ALIAS("instructions", "INSTR_RETIRED"),
2514 EV_ALIAS(NULL, NULL)
2517 xscale_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2518 struct pmc_op_pmcallocate *pmc_config __unused)
2529 static struct pmc_event_alias cortex_a8_aliases[] = {
2530 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
2531 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
2532 EV_ALIAS("instructions", "INSTR_EXECUTED"),
2533 EV_ALIAS(NULL, NULL)
2536 static struct pmc_event_alias cortex_a9_aliases[] = {
2537 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
2538 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
2539 EV_ALIAS("instructions", "INSTR_EXECUTED"),
2540 EV_ALIAS(NULL, NULL)
2544 armv7_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2545 struct pmc_op_pmcallocate *pmc_config __unused)
2556 #if defined(__aarch64__)
2557 static struct pmc_event_alias cortex_a53_aliases[] = {
2558 EV_ALIAS(NULL, NULL)
2560 static struct pmc_event_alias cortex_a57_aliases[] = {
2561 EV_ALIAS(NULL, NULL)
2564 arm64_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2565 struct pmc_op_pmcallocate *pmc_config __unused)
2576 #if defined(__mips__)
2578 static struct pmc_event_alias mips24k_aliases[] = {
2579 EV_ALIAS("instructions", "INSTR_EXECUTED"),
2580 EV_ALIAS("branches", "BRANCH_COMPLETED"),
2581 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
2582 EV_ALIAS(NULL, NULL)
2585 static struct pmc_event_alias mips74k_aliases[] = {
2586 EV_ALIAS("instructions", "INSTR_EXECUTED"),
2587 EV_ALIAS("branches", "BRANCH_INSNS"),
2588 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCH_INSNS"),
2589 EV_ALIAS(NULL, NULL)
2592 static struct pmc_event_alias octeon_aliases[] = {
2593 EV_ALIAS("instructions", "RET"),
2594 EV_ALIAS("branches", "BR"),
2595 EV_ALIAS("branch-mispredicts", "BRMIS"),
2596 EV_ALIAS(NULL, NULL)
2599 #define MIPS_KW_OS "os"
2600 #define MIPS_KW_USR "usr"
2601 #define MIPS_KW_ANYTHREAD "anythread"
2604 mips_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2605 struct pmc_op_pmcallocate *pmc_config __unused)
2611 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2613 while ((p = strsep(&ctrspec, ",")) != NULL) {
2614 if (KWMATCH(p, MIPS_KW_OS))
2615 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2616 else if (KWMATCH(p, MIPS_KW_USR))
2617 pmc_config->pm_caps |= PMC_CAP_USER;
2618 else if (KWMATCH(p, MIPS_KW_ANYTHREAD))
2619 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
2627 #endif /* __mips__ */
2629 #if defined(__powerpc__)
2631 static struct pmc_event_alias ppc7450_aliases[] = {
2632 EV_ALIAS("instructions", "INSTR_COMPLETED"),
2633 EV_ALIAS("branches", "BRANCHES_COMPLETED"),
2634 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCHES"),
2635 EV_ALIAS(NULL, NULL)
2638 static struct pmc_event_alias ppc970_aliases[] = {
2639 EV_ALIAS("instructions", "INSTR_COMPLETED"),
2640 EV_ALIAS("cycles", "CYCLES"),
2641 EV_ALIAS(NULL, NULL)
2644 static struct pmc_event_alias e500_aliases[] = {
2645 EV_ALIAS("instructions", "INSTR_COMPLETED"),
2646 EV_ALIAS("cycles", "CYCLES"),
2647 EV_ALIAS(NULL, NULL)
2650 #define POWERPC_KW_OS "os"
2651 #define POWERPC_KW_USR "usr"
2652 #define POWERPC_KW_ANYTHREAD "anythread"
2655 powerpc_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2656 struct pmc_op_pmcallocate *pmc_config __unused)
2662 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2664 while ((p = strsep(&ctrspec, ",")) != NULL) {
2665 if (KWMATCH(p, POWERPC_KW_OS))
2666 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2667 else if (KWMATCH(p, POWERPC_KW_USR))
2668 pmc_config->pm_caps |= PMC_CAP_USER;
2669 else if (KWMATCH(p, POWERPC_KW_ANYTHREAD))
2670 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
2678 #endif /* __powerpc__ */
2682 * Match an event name `name' with its canonical form.
2684 * Matches are case insensitive and spaces, periods, underscores and
2685 * hyphen characters are considered to match each other.
2687 * Returns 1 for a match, 0 otherwise.
2691 pmc_match_event_name(const char *name, const char *canonicalname)
2694 const unsigned char *c, *n;
2696 c = (const unsigned char *) canonicalname;
2697 n = (const unsigned char *) name;
2699 for (; (nc = *n) && (cc = *c); n++, c++) {
2701 if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
2702 (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
2705 if (toupper(nc) == toupper(cc))
2712 if (*n == '\0' && *c == '\0')
2719 * Match an event name against all the event named supported by a
2722 * Returns an event descriptor pointer on match or NULL otherwise.
2724 static const struct pmc_event_descr *
2725 pmc_match_event_class(const char *name,
2726 const struct pmc_class_descr *pcd)
2729 const struct pmc_event_descr *ev;
2731 ev = pcd->pm_evc_event_table;
2732 for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
2733 if (pmc_match_event_name(name, ev->pm_ev_name))
2740 pmc_mdep_is_compatible_class(enum pmc_class pc)
2744 for (n = 0; n < pmc_mdep_class_list_size; n++)
2745 if (pmc_mdep_class_list[n] == pc)
2755 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
2756 uint32_t flags, int cpu, pmc_id_t *pmcid)
2760 char *r, *spec_copy;
2761 const char *ctrname;
2762 const struct pmc_event_descr *ev;
2763 const struct pmc_event_alias *alias;
2764 struct pmc_op_pmcallocate pmc_config;
2765 const struct pmc_class_descr *pcd;
2770 if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
2771 mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
2776 /* replace an event alias with the canonical event specifier */
2777 if (pmc_mdep_event_aliases)
2778 for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
2779 if (!strcasecmp(ctrspec, alias->pm_alias)) {
2780 spec_copy = strdup(alias->pm_spec);
2784 if (spec_copy == NULL)
2785 spec_copy = strdup(ctrspec);
2788 ctrname = strsep(&r, ",");
2791 * If a explicit class prefix was given by the user, restrict the
2792 * search for the event to the specified PMC class.
2795 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
2796 pcd = pmc_class_table[n];
2797 if (pmc_mdep_is_compatible_class(pcd->pm_evc_class) &&
2798 strncasecmp(ctrname, pcd->pm_evc_name,
2799 pcd->pm_evc_name_size) == 0) {
2800 if ((ev = pmc_match_event_class(ctrname +
2801 pcd->pm_evc_name_size, pcd)) == NULL) {
2810 * Otherwise, search for this event in all compatible PMC
2813 for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
2814 pcd = pmc_class_table[n];
2815 if (pmc_mdep_is_compatible_class(pcd->pm_evc_class))
2816 ev = pmc_match_event_class(ctrname, pcd);
2824 bzero(&pmc_config, sizeof(pmc_config));
2825 pmc_config.pm_ev = ev->pm_ev_code;
2826 pmc_config.pm_class = pcd->pm_evc_class;
2827 pmc_config.pm_cpu = cpu;
2828 pmc_config.pm_mode = mode;
2829 pmc_config.pm_flags = flags;
2831 if (PMC_IS_SAMPLING_MODE(mode))
2832 pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
2834 if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
2839 if (PMC_CALL(PMCALLOCATE, &pmc_config) < 0)
2842 *pmcid = pmc_config.pm_pmcid;
2854 pmc_attach(pmc_id_t pmc, pid_t pid)
2856 struct pmc_op_pmcattach pmc_attach_args;
2858 pmc_attach_args.pm_pmc = pmc;
2859 pmc_attach_args.pm_pid = pid;
2861 return (PMC_CALL(PMCATTACH, &pmc_attach_args));
2865 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
2870 cl = PMC_ID_TO_CLASS(pmcid);
2871 for (i = 0; i < cpu_info.pm_nclass; i++)
2872 if (cpu_info.pm_classes[i].pm_class == cl) {
2873 *caps = cpu_info.pm_classes[i].pm_caps;
2881 pmc_configure_logfile(int fd)
2883 struct pmc_op_configurelog cla;
2886 if (PMC_CALL(CONFIGURELOG, &cla) < 0)
2892 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
2894 if (pmc_syscall == -1) {
2904 pmc_detach(pmc_id_t pmc, pid_t pid)
2906 struct pmc_op_pmcattach pmc_detach_args;
2908 pmc_detach_args.pm_pmc = pmc;
2909 pmc_detach_args.pm_pid = pid;
2910 return (PMC_CALL(PMCDETACH, &pmc_detach_args));
2914 pmc_disable(int cpu, int pmc)
2916 struct pmc_op_pmcadmin ssa;
2920 ssa.pm_state = PMC_STATE_DISABLED;
2921 return (PMC_CALL(PMCADMIN, &ssa));
2925 pmc_enable(int cpu, int pmc)
2927 struct pmc_op_pmcadmin ssa;
2931 ssa.pm_state = PMC_STATE_FREE;
2932 return (PMC_CALL(PMCADMIN, &ssa));
2936 * Return a list of events known to a given PMC class. 'cl' is the
2937 * PMC class identifier, 'eventnames' is the returned list of 'const
2938 * char *' pointers pointing to the names of the events. 'nevents' is
2939 * the number of event name pointers returned.
2941 * The space for 'eventnames' is allocated using malloc(3). The caller
2942 * is responsible for freeing this space when done.
2945 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
2950 const struct pmc_event_descr *ev;
2955 ev = iaf_event_table;
2956 count = PMC_EVENT_TABLE_SIZE(iaf);
2960 * Return the most appropriate set of event name
2961 * spellings for the current CPU.
2963 switch (cpu_info.pm_cputype) {
2965 case PMC_CPU_INTEL_ATOM:
2966 ev = atom_event_table;
2967 count = PMC_EVENT_TABLE_SIZE(atom);
2969 case PMC_CPU_INTEL_ATOM_SILVERMONT:
2970 ev = atom_silvermont_event_table;
2971 count = PMC_EVENT_TABLE_SIZE(atom_silvermont);
2973 case PMC_CPU_INTEL_CORE:
2974 ev = core_event_table;
2975 count = PMC_EVENT_TABLE_SIZE(core);
2977 case PMC_CPU_INTEL_CORE2:
2978 case PMC_CPU_INTEL_CORE2EXTREME:
2979 ev = core2_event_table;
2980 count = PMC_EVENT_TABLE_SIZE(core2);
2982 case PMC_CPU_INTEL_COREI7:
2983 ev = corei7_event_table;
2984 count = PMC_EVENT_TABLE_SIZE(corei7);
2986 case PMC_CPU_INTEL_NEHALEM_EX:
2987 ev = nehalem_ex_event_table;
2988 count = PMC_EVENT_TABLE_SIZE(nehalem_ex);
2990 case PMC_CPU_INTEL_HASWELL:
2991 ev = haswell_event_table;
2992 count = PMC_EVENT_TABLE_SIZE(haswell);
2994 case PMC_CPU_INTEL_HASWELL_XEON:
2995 ev = haswell_xeon_event_table;
2996 count = PMC_EVENT_TABLE_SIZE(haswell_xeon);
2998 case PMC_CPU_INTEL_BROADWELL:
2999 ev = broadwell_event_table;
3000 count = PMC_EVENT_TABLE_SIZE(broadwell);
3002 case PMC_CPU_INTEL_BROADWELL_XEON:
3003 ev = broadwell_xeon_event_table;
3004 count = PMC_EVENT_TABLE_SIZE(broadwell_xeon);
3006 case PMC_CPU_INTEL_SKYLAKE:
3007 ev = skylake_event_table;
3008 count = PMC_EVENT_TABLE_SIZE(skylake);
3010 case PMC_CPU_INTEL_IVYBRIDGE:
3011 ev = ivybridge_event_table;
3012 count = PMC_EVENT_TABLE_SIZE(ivybridge);
3014 case PMC_CPU_INTEL_IVYBRIDGE_XEON:
3015 ev = ivybridge_xeon_event_table;
3016 count = PMC_EVENT_TABLE_SIZE(ivybridge_xeon);
3018 case PMC_CPU_INTEL_SANDYBRIDGE:
3019 ev = sandybridge_event_table;
3020 count = PMC_EVENT_TABLE_SIZE(sandybridge);
3022 case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
3023 ev = sandybridge_xeon_event_table;
3024 count = PMC_EVENT_TABLE_SIZE(sandybridge_xeon);
3026 case PMC_CPU_INTEL_WESTMERE:
3027 ev = westmere_event_table;
3028 count = PMC_EVENT_TABLE_SIZE(westmere);
3030 case PMC_CPU_INTEL_WESTMERE_EX:
3031 ev = westmere_ex_event_table;
3032 count = PMC_EVENT_TABLE_SIZE(westmere_ex);
3037 ev = ucf_event_table;
3038 count = PMC_EVENT_TABLE_SIZE(ucf);
3042 * Return the most appropriate set of event name
3043 * spellings for the current CPU.
3045 switch (cpu_info.pm_cputype) {
3047 case PMC_CPU_INTEL_COREI7:
3048 ev = corei7uc_event_table;
3049 count = PMC_EVENT_TABLE_SIZE(corei7uc);
3051 case PMC_CPU_INTEL_HASWELL:
3052 ev = haswelluc_event_table;
3053 count = PMC_EVENT_TABLE_SIZE(haswelluc);
3055 case PMC_CPU_INTEL_BROADWELL:
3056 ev = broadwelluc_event_table;
3057 count = PMC_EVENT_TABLE_SIZE(broadwelluc);
3059 case PMC_CPU_INTEL_SANDYBRIDGE:
3060 ev = sandybridgeuc_event_table;
3061 count = PMC_EVENT_TABLE_SIZE(sandybridgeuc);
3063 case PMC_CPU_INTEL_WESTMERE:
3064 ev = westmereuc_event_table;
3065 count = PMC_EVENT_TABLE_SIZE(westmereuc);
3070 ev = tsc_event_table;
3071 count = PMC_EVENT_TABLE_SIZE(tsc);
3074 ev = k7_event_table;
3075 count = PMC_EVENT_TABLE_SIZE(k7);
3078 ev = k8_event_table;
3079 count = PMC_EVENT_TABLE_SIZE(k8);
3082 ev = p4_event_table;
3083 count = PMC_EVENT_TABLE_SIZE(p4);
3086 ev = p5_event_table;
3087 count = PMC_EVENT_TABLE_SIZE(p5);
3090 ev = p6_event_table;
3091 count = PMC_EVENT_TABLE_SIZE(p6);
3093 case PMC_CLASS_XSCALE:
3094 ev = xscale_event_table;
3095 count = PMC_EVENT_TABLE_SIZE(xscale);
3097 case PMC_CLASS_ARMV7:
3098 switch (cpu_info.pm_cputype) {
3100 case PMC_CPU_ARMV7_CORTEX_A8:
3101 ev = cortex_a8_event_table;
3102 count = PMC_EVENT_TABLE_SIZE(cortex_a8);
3104 case PMC_CPU_ARMV7_CORTEX_A9:
3105 ev = cortex_a9_event_table;
3106 count = PMC_EVENT_TABLE_SIZE(cortex_a9);
3110 case PMC_CLASS_ARMV8:
3111 switch (cpu_info.pm_cputype) {
3113 case PMC_CPU_ARMV8_CORTEX_A53:
3114 ev = cortex_a53_event_table;
3115 count = PMC_EVENT_TABLE_SIZE(cortex_a53);
3117 case PMC_CPU_ARMV8_CORTEX_A57:
3118 ev = cortex_a57_event_table;
3119 count = PMC_EVENT_TABLE_SIZE(cortex_a57);
3123 case PMC_CLASS_MIPS24K:
3124 ev = mips24k_event_table;
3125 count = PMC_EVENT_TABLE_SIZE(mips24k);
3127 case PMC_CLASS_MIPS74K:
3128 ev = mips74k_event_table;
3129 count = PMC_EVENT_TABLE_SIZE(mips74k);
3131 case PMC_CLASS_OCTEON:
3132 ev = octeon_event_table;
3133 count = PMC_EVENT_TABLE_SIZE(octeon);
3135 case PMC_CLASS_PPC7450:
3136 ev = ppc7450_event_table;
3137 count = PMC_EVENT_TABLE_SIZE(ppc7450);
3139 case PMC_CLASS_PPC970:
3140 ev = ppc970_event_table;
3141 count = PMC_EVENT_TABLE_SIZE(ppc970);
3143 case PMC_CLASS_E500:
3144 ev = e500_event_table;
3145 count = PMC_EVENT_TABLE_SIZE(e500);
3147 case PMC_CLASS_SOFT:
3148 ev = soft_event_table;
3149 count = soft_event_info.pm_nevent;
3156 if ((names = malloc(count * sizeof(const char *))) == NULL)
3159 *eventnames = names;
3162 for (;count--; ev++, names++)
3163 *names = ev->pm_ev_name;
3169 pmc_flush_logfile(void)
3171 return (PMC_CALL(FLUSHLOG,0));
3175 pmc_close_logfile(void)
3177 return (PMC_CALL(CLOSELOG,0));
3181 pmc_get_driver_stats(struct pmc_driverstats *ds)
3183 struct pmc_op_getdriverstats gms;
3185 if (PMC_CALL(GETDRIVERSTATS, &gms) < 0)
3188 /* copy out fields in the current userland<->library interface */
3189 ds->pm_intr_ignored = gms.pm_intr_ignored;
3190 ds->pm_intr_processed = gms.pm_intr_processed;
3191 ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
3192 ds->pm_syscalls = gms.pm_syscalls;
3193 ds->pm_syscall_errors = gms.pm_syscall_errors;
3194 ds->pm_buffer_requests = gms.pm_buffer_requests;
3195 ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
3196 ds->pm_log_sweeps = gms.pm_log_sweeps;
3201 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
3203 struct pmc_op_getmsr gm;
3206 if (PMC_CALL(PMCGETMSR, &gm) < 0)
3215 int error, pmc_mod_id;
3217 uint32_t abi_version;
3218 struct module_stat pmc_modstat;
3219 struct pmc_op_getcpuinfo op_cpu_info;
3220 #if defined(__amd64__) || defined(__i386__)
3221 int cpu_has_iaf_counters;
3225 if (pmc_syscall != -1) /* already inited */
3228 /* retrieve the system call number from the KLD */
3229 if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
3232 pmc_modstat.version = sizeof(struct module_stat);
3233 if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
3236 pmc_syscall = pmc_modstat.data.intval;
3238 /* check the kernel module's ABI against our compiled-in version */
3239 abi_version = PMC_VERSION;
3240 if (PMC_CALL(GETMODULEVERSION, &abi_version) < 0)
3241 return (pmc_syscall = -1);
3243 /* ignore patch & minor numbers for the comparison */
3244 if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
3245 errno = EPROGMISMATCH;
3246 return (pmc_syscall = -1);
3249 if (PMC_CALL(GETCPUINFO, &op_cpu_info) < 0)
3250 return (pmc_syscall = -1);
3252 cpu_info.pm_cputype = op_cpu_info.pm_cputype;
3253 cpu_info.pm_ncpu = op_cpu_info.pm_ncpu;
3254 cpu_info.pm_npmc = op_cpu_info.pm_npmc;
3255 cpu_info.pm_nclass = op_cpu_info.pm_nclass;
3256 for (n = 0; n < cpu_info.pm_nclass; n++)
3257 cpu_info.pm_classes[n] = op_cpu_info.pm_classes[n];
3259 pmc_class_table = malloc(PMC_CLASS_TABLE_SIZE *
3260 sizeof(struct pmc_class_descr *));
3262 if (pmc_class_table == NULL)
3265 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++)
3266 pmc_class_table[n] = NULL;
3269 * Get soft events list.
3271 soft_event_info.pm_class = PMC_CLASS_SOFT;
3272 if (PMC_CALL(GETDYNEVENTINFO, &soft_event_info) < 0)
3273 return (pmc_syscall = -1);
3275 /* Map soft events to static list. */
3276 for (n = 0; n < soft_event_info.pm_nevent; n++) {
3277 soft_event_table[n].pm_ev_name =
3278 soft_event_info.pm_events[n].pm_ev_name;
3279 soft_event_table[n].pm_ev_code =
3280 soft_event_info.pm_events[n].pm_ev_code;
3282 soft_class_table_descr.pm_evc_event_table_size = \
3283 soft_event_info.pm_nevent;
3284 soft_class_table_descr.pm_evc_event_table = \
3288 * Fill in the class table.
3292 /* Fill soft events information. */
3293 pmc_class_table[n++] = &soft_class_table_descr;
3294 #if defined(__amd64__) || defined(__i386__)
3295 if (cpu_info.pm_cputype != PMC_CPU_GENERIC)
3296 pmc_class_table[n++] = &tsc_class_table_descr;
3299 * Check if this CPU has fixed function counters.
3301 cpu_has_iaf_counters = 0;
3302 for (t = 0; t < cpu_info.pm_nclass; t++)
3303 if (cpu_info.pm_classes[t].pm_class == PMC_CLASS_IAF &&
3304 cpu_info.pm_classes[t].pm_num > 0)
3305 cpu_has_iaf_counters = 1;
3308 #define PMC_MDEP_INIT(C) do { \
3309 pmc_mdep_event_aliases = C##_aliases; \
3310 pmc_mdep_class_list = C##_pmc_classes; \
3311 pmc_mdep_class_list_size = \
3312 PMC_TABLE_SIZE(C##_pmc_classes); \
3315 #define PMC_MDEP_INIT_INTEL_V2(C) do { \
3317 pmc_class_table[n++] = &iaf_class_table_descr; \
3318 if (!cpu_has_iaf_counters) \
3319 pmc_mdep_event_aliases = \
3320 C##_aliases_without_iaf; \
3321 pmc_class_table[n] = &C##_class_table_descr; \
3324 /* Configure the event name parser. */
3325 switch (cpu_info.pm_cputype) {
3326 #if defined(__i386__)
3327 case PMC_CPU_AMD_K7:
3329 pmc_class_table[n] = &k7_class_table_descr;
3331 case PMC_CPU_INTEL_P5:
3333 pmc_class_table[n] = &p5_class_table_descr;
3335 case PMC_CPU_INTEL_P6: /* P6 ... Pentium M CPUs have */
3336 case PMC_CPU_INTEL_PII: /* similar PMCs. */
3337 case PMC_CPU_INTEL_PIII:
3338 case PMC_CPU_INTEL_PM:
3340 pmc_class_table[n] = &p6_class_table_descr;
3343 #if defined(__amd64__) || defined(__i386__)
3344 case PMC_CPU_AMD_K8:
3346 pmc_class_table[n] = &k8_class_table_descr;
3348 case PMC_CPU_INTEL_ATOM:
3349 PMC_MDEP_INIT_INTEL_V2(atom);
3351 case PMC_CPU_INTEL_ATOM_SILVERMONT:
3352 PMC_MDEP_INIT_INTEL_V2(atom_silvermont);
3354 case PMC_CPU_INTEL_CORE:
3355 PMC_MDEP_INIT(core);
3356 pmc_class_table[n] = &core_class_table_descr;
3358 case PMC_CPU_INTEL_CORE2:
3359 case PMC_CPU_INTEL_CORE2EXTREME:
3360 PMC_MDEP_INIT_INTEL_V2(core2);
3362 case PMC_CPU_INTEL_COREI7:
3363 pmc_class_table[n++] = &ucf_class_table_descr;
3364 pmc_class_table[n++] = &corei7uc_class_table_descr;
3365 PMC_MDEP_INIT_INTEL_V2(corei7);
3367 case PMC_CPU_INTEL_NEHALEM_EX:
3368 PMC_MDEP_INIT_INTEL_V2(nehalem_ex);
3370 case PMC_CPU_INTEL_HASWELL:
3371 pmc_class_table[n++] = &ucf_class_table_descr;
3372 pmc_class_table[n++] = &haswelluc_class_table_descr;
3373 PMC_MDEP_INIT_INTEL_V2(haswell);
3375 case PMC_CPU_INTEL_HASWELL_XEON:
3376 PMC_MDEP_INIT_INTEL_V2(haswell_xeon);
3378 case PMC_CPU_INTEL_BROADWELL:
3379 pmc_class_table[n++] = &ucf_class_table_descr;
3380 pmc_class_table[n++] = &broadwelluc_class_table_descr;
3381 PMC_MDEP_INIT_INTEL_V2(broadwell);
3383 case PMC_CPU_INTEL_BROADWELL_XEON:
3384 PMC_MDEP_INIT_INTEL_V2(broadwell_xeon);
3386 case PMC_CPU_INTEL_SKYLAKE:
3387 PMC_MDEP_INIT_INTEL_V2(skylake);
3389 case PMC_CPU_INTEL_IVYBRIDGE:
3390 PMC_MDEP_INIT_INTEL_V2(ivybridge);
3392 case PMC_CPU_INTEL_IVYBRIDGE_XEON:
3393 PMC_MDEP_INIT_INTEL_V2(ivybridge_xeon);
3395 case PMC_CPU_INTEL_SANDYBRIDGE:
3396 pmc_class_table[n++] = &ucf_class_table_descr;
3397 pmc_class_table[n++] = &sandybridgeuc_class_table_descr;
3398 PMC_MDEP_INIT_INTEL_V2(sandybridge);
3400 case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
3401 PMC_MDEP_INIT_INTEL_V2(sandybridge_xeon);
3403 case PMC_CPU_INTEL_WESTMERE:
3404 pmc_class_table[n++] = &ucf_class_table_descr;
3405 pmc_class_table[n++] = &westmereuc_class_table_descr;
3406 PMC_MDEP_INIT_INTEL_V2(westmere);
3408 case PMC_CPU_INTEL_WESTMERE_EX:
3409 PMC_MDEP_INIT_INTEL_V2(westmere_ex);
3411 case PMC_CPU_INTEL_PIV:
3413 pmc_class_table[n] = &p4_class_table_descr;
3416 case PMC_CPU_GENERIC:
3417 PMC_MDEP_INIT(generic);
3419 #if defined(__arm__)
3420 #if defined(__XSCALE__)
3421 case PMC_CPU_INTEL_XSCALE:
3422 PMC_MDEP_INIT(xscale);
3423 pmc_class_table[n] = &xscale_class_table_descr;
3426 case PMC_CPU_ARMV7_CORTEX_A8:
3427 PMC_MDEP_INIT(cortex_a8);
3428 pmc_class_table[n] = &cortex_a8_class_table_descr;
3430 case PMC_CPU_ARMV7_CORTEX_A9:
3431 PMC_MDEP_INIT(cortex_a9);
3432 pmc_class_table[n] = &cortex_a9_class_table_descr;
3435 #if defined(__aarch64__)
3436 case PMC_CPU_ARMV8_CORTEX_A53:
3437 PMC_MDEP_INIT(cortex_a53);
3438 pmc_class_table[n] = &cortex_a53_class_table_descr;
3440 case PMC_CPU_ARMV8_CORTEX_A57:
3441 PMC_MDEP_INIT(cortex_a57);
3442 pmc_class_table[n] = &cortex_a57_class_table_descr;
3445 #if defined(__mips__)
3446 case PMC_CPU_MIPS_24K:
3447 PMC_MDEP_INIT(mips24k);
3448 pmc_class_table[n] = &mips24k_class_table_descr;
3450 case PMC_CPU_MIPS_74K:
3451 PMC_MDEP_INIT(mips74k);
3452 pmc_class_table[n] = &mips74k_class_table_descr;
3454 case PMC_CPU_MIPS_OCTEON:
3455 PMC_MDEP_INIT(octeon);
3456 pmc_class_table[n] = &octeon_class_table_descr;
3458 #endif /* __mips__ */
3459 #if defined(__powerpc__)
3460 case PMC_CPU_PPC_7450:
3461 PMC_MDEP_INIT(ppc7450);
3462 pmc_class_table[n] = &ppc7450_class_table_descr;
3464 case PMC_CPU_PPC_970:
3465 PMC_MDEP_INIT(ppc970);
3466 pmc_class_table[n] = &ppc970_class_table_descr;
3468 case PMC_CPU_PPC_E500:
3469 PMC_MDEP_INIT(e500);
3470 pmc_class_table[n] = &e500_class_table_descr;
3475 * Some kind of CPU this version of the library knows nothing
3476 * about. This shouldn't happen since the abi version check
3477 * should have caught this.
3480 return (pmc_syscall = -1);
3487 pmc_name_of_capability(enum pmc_caps cap)
3492 * 'cap' should have a single bit set and should be in
3495 if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
3496 cap > PMC_CAP_LAST) {
3502 return (pmc_capability_names[i - 1]);
3506 pmc_name_of_class(enum pmc_class pc)
3510 for (n = 0; n < PMC_TABLE_SIZE(pmc_class_names); n++)
3511 if (pc == pmc_class_names[n].pm_class)
3512 return (pmc_class_names[n].pm_name);
3519 pmc_name_of_cputype(enum pmc_cputype cp)
3523 for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
3524 if (cp == pmc_cputype_names[n].pm_cputype)
3525 return (pmc_cputype_names[n].pm_name);
3532 pmc_name_of_disposition(enum pmc_disp pd)
3534 if ((int) pd >= PMC_DISP_FIRST &&
3535 pd <= PMC_DISP_LAST)
3536 return (pmc_disposition_names[pd]);
3543 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
3545 const struct pmc_event_descr *ev, *evfence;
3547 ev = evfence = NULL;
3548 if (pe >= PMC_EV_IAF_FIRST && pe <= PMC_EV_IAF_LAST) {
3549 ev = iaf_event_table;
3550 evfence = iaf_event_table + PMC_EVENT_TABLE_SIZE(iaf);
3551 } else if (pe >= PMC_EV_IAP_FIRST && pe <= PMC_EV_IAP_LAST) {
3553 case PMC_CPU_INTEL_ATOM:
3554 ev = atom_event_table;
3555 evfence = atom_event_table + PMC_EVENT_TABLE_SIZE(atom);
3557 case PMC_CPU_INTEL_ATOM_SILVERMONT:
3558 ev = atom_silvermont_event_table;
3559 evfence = atom_silvermont_event_table +
3560 PMC_EVENT_TABLE_SIZE(atom_silvermont);
3562 case PMC_CPU_INTEL_CORE:
3563 ev = core_event_table;
3564 evfence = core_event_table + PMC_EVENT_TABLE_SIZE(core);
3566 case PMC_CPU_INTEL_CORE2:
3567 case PMC_CPU_INTEL_CORE2EXTREME:
3568 ev = core2_event_table;
3569 evfence = core2_event_table + PMC_EVENT_TABLE_SIZE(core2);
3571 case PMC_CPU_INTEL_COREI7:
3572 ev = corei7_event_table;
3573 evfence = corei7_event_table + PMC_EVENT_TABLE_SIZE(corei7);
3575 case PMC_CPU_INTEL_NEHALEM_EX:
3576 ev = nehalem_ex_event_table;
3577 evfence = nehalem_ex_event_table +
3578 PMC_EVENT_TABLE_SIZE(nehalem_ex);
3580 case PMC_CPU_INTEL_HASWELL:
3581 ev = haswell_event_table;
3582 evfence = haswell_event_table + PMC_EVENT_TABLE_SIZE(haswell);
3584 case PMC_CPU_INTEL_HASWELL_XEON:
3585 ev = haswell_xeon_event_table;
3586 evfence = haswell_xeon_event_table + PMC_EVENT_TABLE_SIZE(haswell_xeon);
3588 case PMC_CPU_INTEL_BROADWELL:
3589 ev = broadwell_event_table;
3590 evfence = broadwell_event_table + PMC_EVENT_TABLE_SIZE(broadwell);
3592 case PMC_CPU_INTEL_BROADWELL_XEON:
3593 ev = broadwell_xeon_event_table;
3594 evfence = broadwell_xeon_event_table + PMC_EVENT_TABLE_SIZE(broadwell_xeon);
3596 case PMC_CPU_INTEL_SKYLAKE:
3597 ev = skylake_event_table;
3598 evfence = skylake_event_table + PMC_EVENT_TABLE_SIZE(skylake);
3600 case PMC_CPU_INTEL_IVYBRIDGE:
3601 ev = ivybridge_event_table;
3602 evfence = ivybridge_event_table + PMC_EVENT_TABLE_SIZE(ivybridge);
3604 case PMC_CPU_INTEL_IVYBRIDGE_XEON:
3605 ev = ivybridge_xeon_event_table;
3606 evfence = ivybridge_xeon_event_table + PMC_EVENT_TABLE_SIZE(ivybridge_xeon);
3608 case PMC_CPU_INTEL_SANDYBRIDGE:
3609 ev = sandybridge_event_table;
3610 evfence = sandybridge_event_table + PMC_EVENT_TABLE_SIZE(sandybridge);
3612 case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
3613 ev = sandybridge_xeon_event_table;
3614 evfence = sandybridge_xeon_event_table + PMC_EVENT_TABLE_SIZE(sandybridge_xeon);
3616 case PMC_CPU_INTEL_WESTMERE:
3617 ev = westmere_event_table;
3618 evfence = westmere_event_table + PMC_EVENT_TABLE_SIZE(westmere);
3620 case PMC_CPU_INTEL_WESTMERE_EX:
3621 ev = westmere_ex_event_table;
3622 evfence = westmere_ex_event_table +
3623 PMC_EVENT_TABLE_SIZE(westmere_ex);
3625 default: /* Unknown CPU type. */
3628 } else if (pe >= PMC_EV_UCF_FIRST && pe <= PMC_EV_UCF_LAST) {
3629 ev = ucf_event_table;
3630 evfence = ucf_event_table + PMC_EVENT_TABLE_SIZE(ucf);
3631 } else if (pe >= PMC_EV_UCP_FIRST && pe <= PMC_EV_UCP_LAST) {
3633 case PMC_CPU_INTEL_COREI7:
3634 ev = corei7uc_event_table;
3635 evfence = corei7uc_event_table + PMC_EVENT_TABLE_SIZE(corei7uc);
3637 case PMC_CPU_INTEL_SANDYBRIDGE:
3638 ev = sandybridgeuc_event_table;
3639 evfence = sandybridgeuc_event_table + PMC_EVENT_TABLE_SIZE(sandybridgeuc);
3641 case PMC_CPU_INTEL_WESTMERE:
3642 ev = westmereuc_event_table;
3643 evfence = westmereuc_event_table + PMC_EVENT_TABLE_SIZE(westmereuc);
3645 default: /* Unknown CPU type. */
3648 } else if (pe >= PMC_EV_K7_FIRST && pe <= PMC_EV_K7_LAST) {
3649 ev = k7_event_table;
3650 evfence = k7_event_table + PMC_EVENT_TABLE_SIZE(k7);
3651 } else if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
3652 ev = k8_event_table;
3653 evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
3654 } else if (pe >= PMC_EV_P4_FIRST && pe <= PMC_EV_P4_LAST) {
3655 ev = p4_event_table;
3656 evfence = p4_event_table + PMC_EVENT_TABLE_SIZE(p4);
3657 } else if (pe >= PMC_EV_P5_FIRST && pe <= PMC_EV_P5_LAST) {
3658 ev = p5_event_table;
3659 evfence = p5_event_table + PMC_EVENT_TABLE_SIZE(p5);
3660 } else if (pe >= PMC_EV_P6_FIRST && pe <= PMC_EV_P6_LAST) {
3661 ev = p6_event_table;
3662 evfence = p6_event_table + PMC_EVENT_TABLE_SIZE(p6);
3663 } else if (pe >= PMC_EV_XSCALE_FIRST && pe <= PMC_EV_XSCALE_LAST) {
3664 ev = xscale_event_table;
3665 evfence = xscale_event_table + PMC_EVENT_TABLE_SIZE(xscale);
3666 } else if (pe >= PMC_EV_ARMV7_FIRST && pe <= PMC_EV_ARMV7_LAST) {
3668 case PMC_CPU_ARMV7_CORTEX_A8:
3669 ev = cortex_a8_event_table;
3670 evfence = cortex_a8_event_table + PMC_EVENT_TABLE_SIZE(cortex_a8);
3672 case PMC_CPU_ARMV7_CORTEX_A9:
3673 ev = cortex_a9_event_table;
3674 evfence = cortex_a9_event_table + PMC_EVENT_TABLE_SIZE(cortex_a9);
3676 default: /* Unknown CPU type. */
3679 } else if (pe >= PMC_EV_ARMV8_FIRST && pe <= PMC_EV_ARMV8_LAST) {
3681 case PMC_CPU_ARMV8_CORTEX_A53:
3682 ev = cortex_a53_event_table;
3683 evfence = cortex_a53_event_table + PMC_EVENT_TABLE_SIZE(cortex_a53);
3685 case PMC_CPU_ARMV8_CORTEX_A57:
3686 ev = cortex_a57_event_table;
3687 evfence = cortex_a57_event_table + PMC_EVENT_TABLE_SIZE(cortex_a57);
3689 default: /* Unknown CPU type. */
3692 } else if (pe >= PMC_EV_MIPS24K_FIRST && pe <= PMC_EV_MIPS24K_LAST) {
3693 ev = mips24k_event_table;
3694 evfence = mips24k_event_table + PMC_EVENT_TABLE_SIZE(mips24k);
3695 } else if (pe >= PMC_EV_MIPS74K_FIRST && pe <= PMC_EV_MIPS74K_LAST) {
3696 ev = mips74k_event_table;
3697 evfence = mips74k_event_table + PMC_EVENT_TABLE_SIZE(mips74k);
3698 } else if (pe >= PMC_EV_OCTEON_FIRST && pe <= PMC_EV_OCTEON_LAST) {
3699 ev = octeon_event_table;
3700 evfence = octeon_event_table + PMC_EVENT_TABLE_SIZE(octeon);
3701 } else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
3702 ev = ppc7450_event_table;
3703 evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450);
3704 } else if (pe >= PMC_EV_PPC970_FIRST && pe <= PMC_EV_PPC970_LAST) {
3705 ev = ppc970_event_table;
3706 evfence = ppc970_event_table + PMC_EVENT_TABLE_SIZE(ppc970);
3707 } else if (pe >= PMC_EV_E500_FIRST && pe <= PMC_EV_E500_LAST) {
3708 ev = e500_event_table;
3709 evfence = e500_event_table + PMC_EVENT_TABLE_SIZE(e500);
3710 } else if (pe == PMC_EV_TSC_TSC) {
3711 ev = tsc_event_table;
3712 evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
3713 } else if ((int)pe >= PMC_EV_SOFT_FIRST && (int)pe <= PMC_EV_SOFT_LAST) {
3714 ev = soft_event_table;
3715 evfence = soft_event_table + soft_event_info.pm_nevent;
3718 for (; ev != evfence; ev++)
3719 if (pe == ev->pm_ev_code)
3720 return (ev->pm_ev_name);
3726 pmc_name_of_event(enum pmc_event pe)
3730 if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
3738 pmc_name_of_mode(enum pmc_mode pm)
3740 if ((int) pm >= PMC_MODE_FIRST &&
3741 pm <= PMC_MODE_LAST)
3742 return (pmc_mode_names[pm]);
3749 pmc_name_of_state(enum pmc_state ps)
3751 if ((int) ps >= PMC_STATE_FIRST &&
3752 ps <= PMC_STATE_LAST)
3753 return (pmc_state_names[ps]);
3762 if (pmc_syscall == -1) {
3767 return (cpu_info.pm_ncpu);
3773 if (pmc_syscall == -1) {
3778 if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
3783 return (cpu_info.pm_npmc);
3787 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
3790 struct pmc_op_getpmcinfo *pmci;
3792 if ((npmc = pmc_npmc(cpu)) < 0)
3795 nbytes = sizeof(struct pmc_op_getpmcinfo) +
3796 npmc * sizeof(struct pmc_info);
3798 if ((pmci = calloc(1, nbytes)) == NULL)
3803 if (PMC_CALL(GETPMCINFO, pmci) < 0) {
3808 /* kernel<->library, library<->userland interfaces are identical */
3809 *ppmci = (struct pmc_pmcinfo *) pmci;
3814 pmc_read(pmc_id_t pmc, pmc_value_t *value)
3816 struct pmc_op_pmcrw pmc_read_op;
3818 pmc_read_op.pm_pmcid = pmc;
3819 pmc_read_op.pm_flags = PMC_F_OLDVALUE;
3820 pmc_read_op.pm_value = -1;
3822 if (PMC_CALL(PMCRW, &pmc_read_op) < 0)
3825 *value = pmc_read_op.pm_value;
3830 pmc_release(pmc_id_t pmc)
3832 struct pmc_op_simple pmc_release_args;
3834 pmc_release_args.pm_pmcid = pmc;
3835 return (PMC_CALL(PMCRELEASE, &pmc_release_args));
3839 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
3841 struct pmc_op_pmcrw pmc_rw_op;
3843 pmc_rw_op.pm_pmcid = pmc;
3844 pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
3845 pmc_rw_op.pm_value = newvalue;
3847 if (PMC_CALL(PMCRW, &pmc_rw_op) < 0)
3850 *oldvaluep = pmc_rw_op.pm_value;
3855 pmc_set(pmc_id_t pmc, pmc_value_t value)
3857 struct pmc_op_pmcsetcount sc;
3860 sc.pm_count = value;
3862 if (PMC_CALL(PMCSETCOUNT, &sc) < 0)
3868 pmc_start(pmc_id_t pmc)
3870 struct pmc_op_simple pmc_start_args;
3872 pmc_start_args.pm_pmcid = pmc;
3873 return (PMC_CALL(PMCSTART, &pmc_start_args));
3877 pmc_stop(pmc_id_t pmc)
3879 struct pmc_op_simple pmc_stop_args;
3881 pmc_stop_args.pm_pmcid = pmc;
3882 return (PMC_CALL(PMCSTOP, &pmc_stop_args));
3886 pmc_width(pmc_id_t pmcid, uint32_t *width)
3891 cl = PMC_ID_TO_CLASS(pmcid);
3892 for (i = 0; i < cpu_info.pm_nclass; i++)
3893 if (cpu_info.pm_classes[i].pm_class == cl) {
3894 *width = cpu_info.pm_classes[i].pm_width;
3902 pmc_write(pmc_id_t pmc, pmc_value_t value)
3904 struct pmc_op_pmcrw pmc_write_op;
3906 pmc_write_op.pm_pmcid = pmc;
3907 pmc_write_op.pm_flags = PMC_F_NEWVALUE;
3908 pmc_write_op.pm_value = value;
3909 return (PMC_CALL(PMCRW, &pmc_write_op));
3913 pmc_writelog(uint32_t userdata)
3915 struct pmc_op_writelog wl;
3917 wl.pm_userdata = userdata;
3918 return (PMC_CALL(WRITELOG, &wl));