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/module.h>
33 #include <sys/syscall.h>
45 #include "libpmcinternal.h"
47 /* Function prototypes */
49 static int k7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
50 struct pmc_op_pmcallocate *_pmc_config);
52 #if defined(__amd64__) || defined(__i386__)
53 static int iaf_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
54 struct pmc_op_pmcallocate *_pmc_config);
55 static int iap_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
56 struct pmc_op_pmcallocate *_pmc_config);
57 static int ucf_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
58 struct pmc_op_pmcallocate *_pmc_config);
59 static int ucp_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
60 struct pmc_op_pmcallocate *_pmc_config);
61 static int k8_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
62 struct pmc_op_pmcallocate *_pmc_config);
63 static int p4_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
64 struct pmc_op_pmcallocate *_pmc_config);
67 static int p5_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
68 struct pmc_op_pmcallocate *_pmc_config);
69 static int p6_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
70 struct pmc_op_pmcallocate *_pmc_config);
72 #if defined(__amd64__) || defined(__i386__)
73 static int tsc_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
74 struct pmc_op_pmcallocate *_pmc_config);
76 #if defined(__XSCALE__)
77 static int xscale_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
78 struct pmc_op_pmcallocate *_pmc_config);
81 static int mips24k_allocate_pmc(enum pmc_event _pe, char* ctrspec,
82 struct pmc_op_pmcallocate *_pmc_config);
84 static int soft_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
85 struct pmc_op_pmcallocate *_pmc_config);
87 #if defined(__powerpc__)
88 static int ppc7450_allocate_pmc(enum pmc_event _pe, char* ctrspec,
89 struct pmc_op_pmcallocate *_pmc_config);
90 #endif /* __powerpc__ */
92 #define PMC_CALL(cmd, params) \
93 syscall(pmc_syscall, PMC_OP_##cmd, (params))
96 * Event aliases provide a way for the user to ask for generic events
97 * like "cache-misses", or "instructions-retired". These aliases are
98 * mapped to the appropriate canonical event descriptions using a
101 struct pmc_event_alias {
102 const char *pm_alias;
106 static const struct pmc_event_alias *pmc_mdep_event_aliases;
109 * The pmc_event_descr structure maps symbolic names known to the user
110 * to integer codes used by the PMC KLD.
112 struct pmc_event_descr {
113 const char *pm_ev_name;
114 enum pmc_event pm_ev_code;
118 * The pmc_class_descr structure maps class name prefixes for
119 * event names to event tables and other PMC class data.
121 struct pmc_class_descr {
122 const char *pm_evc_name;
123 size_t pm_evc_name_size;
124 enum pmc_class pm_evc_class;
125 const struct pmc_event_descr *pm_evc_event_table;
126 size_t pm_evc_event_table_size;
127 int (*pm_evc_allocate_pmc)(enum pmc_event _pe,
128 char *_ctrspec, struct pmc_op_pmcallocate *_pa);
131 #define PMC_TABLE_SIZE(N) (sizeof(N)/sizeof(N[0]))
132 #define PMC_EVENT_TABLE_SIZE(N) PMC_TABLE_SIZE(N##_event_table)
135 #define __PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
138 * PMC_CLASSDEP_TABLE(NAME, CLASS)
140 * Define a table mapping event names and aliases to HWPMC event IDs.
142 #define PMC_CLASSDEP_TABLE(N, C) \
143 static const struct pmc_event_descr N##_event_table[] = \
148 PMC_CLASSDEP_TABLE(iaf, IAF);
149 PMC_CLASSDEP_TABLE(k7, K7);
150 PMC_CLASSDEP_TABLE(k8, K8);
151 PMC_CLASSDEP_TABLE(p4, P4);
152 PMC_CLASSDEP_TABLE(p5, P5);
153 PMC_CLASSDEP_TABLE(p6, P6);
154 PMC_CLASSDEP_TABLE(xscale, XSCALE);
155 PMC_CLASSDEP_TABLE(mips24k, MIPS24K);
156 PMC_CLASSDEP_TABLE(ucf, UCF);
157 PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
159 static struct pmc_event_descr soft_event_table[PMC_EV_DYN_COUNT];
161 #undef __PMC_EV_ALIAS
162 #define __PMC_EV_ALIAS(N,CODE) { N, PMC_EV_##CODE },
164 static const struct pmc_event_descr atom_event_table[] =
166 __PMC_EV_ALIAS_ATOM()
169 static const struct pmc_event_descr core_event_table[] =
171 __PMC_EV_ALIAS_CORE()
175 static const struct pmc_event_descr core2_event_table[] =
177 __PMC_EV_ALIAS_CORE2()
180 static const struct pmc_event_descr corei7_event_table[] =
182 __PMC_EV_ALIAS_COREI7()
185 static const struct pmc_event_descr ivybridge_event_table[] =
187 __PMC_EV_ALIAS_IVYBRIDGE()
190 static const struct pmc_event_descr sandybridge_event_table[] =
192 __PMC_EV_ALIAS_SANDYBRIDGE()
195 static const struct pmc_event_descr sandybridge_xeon_event_table[] =
197 __PMC_EV_ALIAS_SANDYBRIDGE_XEON()
200 static const struct pmc_event_descr westmere_event_table[] =
202 __PMC_EV_ALIAS_WESTMERE()
205 static const struct pmc_event_descr corei7uc_event_table[] =
207 __PMC_EV_ALIAS_COREI7UC()
210 static const struct pmc_event_descr sandybridgeuc_event_table[] =
212 __PMC_EV_ALIAS_SANDYBRIDGEUC()
215 static const struct pmc_event_descr westmereuc_event_table[] =
217 __PMC_EV_ALIAS_WESTMEREUC()
221 * PMC_MDEP_TABLE(NAME, PRIMARYCLASS, ADDITIONAL_CLASSES...)
223 * Map a CPU to the PMC classes it supports.
225 #define PMC_MDEP_TABLE(N,C,...) \
226 static const enum pmc_class N##_pmc_classes[] = { \
227 PMC_CLASS_##C, __VA_ARGS__ \
230 PMC_MDEP_TABLE(atom, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
231 PMC_MDEP_TABLE(core, IAP, PMC_CLASS_SOFT, PMC_CLASS_TSC);
232 PMC_MDEP_TABLE(core2, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
233 PMC_MDEP_TABLE(corei7, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
234 PMC_MDEP_TABLE(ivybridge, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
235 PMC_MDEP_TABLE(sandybridge, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
236 PMC_MDEP_TABLE(sandybridge_xeon, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC);
237 PMC_MDEP_TABLE(westmere, IAP, PMC_CLASS_SOFT, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
238 PMC_MDEP_TABLE(k7, K7, PMC_CLASS_SOFT, PMC_CLASS_TSC);
239 PMC_MDEP_TABLE(k8, K8, PMC_CLASS_SOFT, PMC_CLASS_TSC);
240 PMC_MDEP_TABLE(p4, P4, PMC_CLASS_SOFT, PMC_CLASS_TSC);
241 PMC_MDEP_TABLE(p5, P5, PMC_CLASS_SOFT, PMC_CLASS_TSC);
242 PMC_MDEP_TABLE(p6, P6, PMC_CLASS_SOFT, PMC_CLASS_TSC);
243 PMC_MDEP_TABLE(xscale, XSCALE, PMC_CLASS_SOFT, PMC_CLASS_XSCALE);
244 PMC_MDEP_TABLE(mips24k, MIPS24K, PMC_CLASS_SOFT, PMC_CLASS_MIPS24K);
245 PMC_MDEP_TABLE(ppc7450, PPC7450, PMC_CLASS_SOFT, PMC_CLASS_PPC7450);
246 PMC_MDEP_TABLE(generic, SOFT, PMC_CLASS_SOFT);
248 static const struct pmc_event_descr tsc_event_table[] =
253 #undef PMC_CLASS_TABLE_DESC
254 #define PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR) \
255 static const struct pmc_class_descr NAME##_class_table_descr = \
257 .pm_evc_name = #CLASS "-", \
258 .pm_evc_name_size = sizeof(#CLASS "-") - 1, \
259 .pm_evc_class = PMC_CLASS_##CLASS , \
260 .pm_evc_event_table = EVENTS##_event_table , \
261 .pm_evc_event_table_size = \
262 PMC_EVENT_TABLE_SIZE(EVENTS), \
263 .pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc \
266 #if defined(__i386__) || defined(__amd64__)
267 PMC_CLASS_TABLE_DESC(iaf, IAF, iaf, iaf);
268 PMC_CLASS_TABLE_DESC(atom, IAP, atom, iap);
269 PMC_CLASS_TABLE_DESC(core, IAP, core, iap);
270 PMC_CLASS_TABLE_DESC(core2, IAP, core2, iap);
271 PMC_CLASS_TABLE_DESC(corei7, IAP, corei7, iap);
272 PMC_CLASS_TABLE_DESC(ivybridge, IAP, ivybridge, iap);
273 PMC_CLASS_TABLE_DESC(sandybridge, IAP, sandybridge, iap);
274 PMC_CLASS_TABLE_DESC(sandybridge_xeon, IAP, sandybridge_xeon, iap);
275 PMC_CLASS_TABLE_DESC(westmere, IAP, westmere, iap);
276 PMC_CLASS_TABLE_DESC(ucf, UCF, ucf, ucf);
277 PMC_CLASS_TABLE_DESC(corei7uc, UCP, corei7uc, ucp);
278 PMC_CLASS_TABLE_DESC(sandybridgeuc, UCP, sandybridgeuc, ucp);
279 PMC_CLASS_TABLE_DESC(westmereuc, UCP, westmereuc, ucp);
281 #if defined(__i386__)
282 PMC_CLASS_TABLE_DESC(k7, K7, k7, k7);
284 #if defined(__i386__) || defined(__amd64__)
285 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
286 PMC_CLASS_TABLE_DESC(p4, P4, p4, p4);
288 #if defined(__i386__)
289 PMC_CLASS_TABLE_DESC(p5, P5, p5, p5);
290 PMC_CLASS_TABLE_DESC(p6, P6, p6, p6);
292 #if defined(__i386__) || defined(__amd64__)
293 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
295 #if defined(__XSCALE__)
296 PMC_CLASS_TABLE_DESC(xscale, XSCALE, xscale, xscale);
298 #if defined(__mips__)
299 PMC_CLASS_TABLE_DESC(mips24k, MIPS24K, mips24k, mips24k);
300 #endif /* __mips__ */
301 #if defined(__powerpc__)
302 PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, ppc7450);
305 static struct pmc_class_descr soft_class_table_descr =
307 .pm_evc_name = "SOFT-",
308 .pm_evc_name_size = sizeof("SOFT-") - 1,
309 .pm_evc_class = PMC_CLASS_SOFT,
310 .pm_evc_event_table = NULL,
311 .pm_evc_event_table_size = 0,
312 .pm_evc_allocate_pmc = soft_allocate_pmc
315 #undef PMC_CLASS_TABLE_DESC
317 static const struct pmc_class_descr **pmc_class_table;
318 #define PMC_CLASS_TABLE_SIZE cpu_info.pm_nclass
320 static const enum pmc_class *pmc_mdep_class_list;
321 static size_t pmc_mdep_class_list_size;
324 * Mapping tables, mapping enumeration values to human readable
328 static const char * pmc_capability_names[] = {
330 #define __PMC_CAP(N,V,D) #N ,
334 static const char * pmc_class_names[] = {
336 #define __PMC_CLASS(C) #C ,
340 struct pmc_cputype_map {
341 enum pmc_cputype pm_cputype;
345 static const struct pmc_cputype_map pmc_cputype_names[] = {
347 #define __PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
351 static const char * pmc_disposition_names[] = {
353 #define __PMC_DISP(D) #D ,
357 static const char * pmc_mode_names[] = {
359 #define __PMC_MODE(M,N) #M ,
363 static const char * pmc_state_names[] = {
365 #define __PMC_STATE(S) #S ,
370 * Filled in by pmc_init().
372 static int pmc_syscall = -1;
373 static struct pmc_cpuinfo cpu_info;
374 static struct pmc_op_getdyneventinfo soft_event_info;
376 /* Event masks for events */
379 const uint64_t pm_value;
381 #define PMCMASK(N,V) { .pm_name = #N, .pm_value = (V) }
382 #define NULLMASK { .pm_name = NULL }
384 #if defined(__amd64__) || defined(__i386__)
386 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint64_t *evmask)
388 const struct pmc_masks *pm;
392 if (pmask == NULL) /* no mask keywords */
394 q = strchr(p, '='); /* skip '=' */
395 if (*++q == '\0') /* no more data */
397 c = 0; /* count of mask keywords seen */
398 while ((r = strsep(&q, "+")) != NULL) {
399 for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
402 if (pm->pm_name == NULL) /* not found */
404 *evmask |= pm->pm_value;
411 #define KWMATCH(p,kw) (strcasecmp((p), (kw)) == 0)
412 #define KWPREFIXMATCH(p,kw) (strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
413 #define EV_ALIAS(N,S) { .pm_alias = N, .pm_spec = S }
415 #if defined(__i386__)
418 * AMD K7 (Athlon) CPUs.
421 static struct pmc_event_alias k7_aliases[] = {
422 EV_ALIAS("branches", "k7-retired-branches"),
423 EV_ALIAS("branch-mispredicts", "k7-retired-branches-mispredicted"),
424 EV_ALIAS("cycles", "tsc"),
425 EV_ALIAS("dc-misses", "k7-dc-misses"),
426 EV_ALIAS("ic-misses", "k7-ic-misses"),
427 EV_ALIAS("instructions", "k7-retired-instructions"),
428 EV_ALIAS("interrupts", "k7-hardware-interrupts"),
432 #define K7_KW_COUNT "count"
433 #define K7_KW_EDGE "edge"
434 #define K7_KW_INV "inv"
435 #define K7_KW_OS "os"
436 #define K7_KW_UNITMASK "unitmask"
437 #define K7_KW_USR "usr"
440 k7_allocate_pmc(enum pmc_event pe, char *ctrspec,
441 struct pmc_op_pmcallocate *pmc_config)
445 uint32_t count, unitmask;
447 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
448 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
450 if (pe == PMC_EV_K7_DC_REFILLS_FROM_L2 ||
451 pe == PMC_EV_K7_DC_REFILLS_FROM_SYSTEM ||
452 pe == PMC_EV_K7_DC_WRITEBACKS) {
454 unitmask = AMD_PMC_UNITMASK_MOESI;
456 unitmask = has_unitmask = 0;
458 while ((p = strsep(&ctrspec, ",")) != NULL) {
459 if (KWPREFIXMATCH(p, K7_KW_COUNT "=")) {
461 if (*++q == '\0') /* skip '=' */
464 count = strtol(q, &e, 0);
465 if (e == q || *e != '\0')
468 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
469 pmc_config->pm_md.pm_amd.pm_amd_config |=
470 AMD_PMC_TO_COUNTER(count);
472 } else if (KWMATCH(p, K7_KW_EDGE)) {
473 pmc_config->pm_caps |= PMC_CAP_EDGE;
474 } else if (KWMATCH(p, K7_KW_INV)) {
475 pmc_config->pm_caps |= PMC_CAP_INVERT;
476 } else if (KWMATCH(p, K7_KW_OS)) {
477 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
478 } else if (KWPREFIXMATCH(p, K7_KW_UNITMASK "=")) {
479 if (has_unitmask == 0)
483 if (*++q == '\0') /* skip '=' */
486 while ((c = tolower(*q++)) != 0)
488 unitmask |= AMD_PMC_UNITMASK_M;
490 unitmask |= AMD_PMC_UNITMASK_O;
492 unitmask |= AMD_PMC_UNITMASK_E;
494 unitmask |= AMD_PMC_UNITMASK_S;
496 unitmask |= AMD_PMC_UNITMASK_I;
505 } else if (KWMATCH(p, K7_KW_USR)) {
506 pmc_config->pm_caps |= PMC_CAP_USER;
512 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
513 pmc_config->pm_md.pm_amd.pm_amd_config |=
514 AMD_PMC_TO_UNITMASK(unitmask);
523 #if defined(__amd64__) || defined(__i386__)
526 * Intel Core (Family 6, Model E) PMCs.
529 static struct pmc_event_alias core_aliases[] = {
530 EV_ALIAS("branches", "iap-br-instr-ret"),
531 EV_ALIAS("branch-mispredicts", "iap-br-mispred-ret"),
532 EV_ALIAS("cycles", "tsc-tsc"),
533 EV_ALIAS("ic-misses", "iap-icache-misses"),
534 EV_ALIAS("instructions", "iap-instr-ret"),
535 EV_ALIAS("interrupts", "iap-core-hw-int-rx"),
536 EV_ALIAS("unhalted-cycles", "iap-unhalted-core-cycles"),
541 * Intel Core2 (Family 6, Model F), Core2Extreme (Family 6, Model 17H)
542 * and Atom (Family 6, model 1CH) PMCs.
544 * We map aliases to events on the fixed-function counters if these
545 * are present. Note that not all CPUs in this family contain fixed-function
549 static struct pmc_event_alias core2_aliases[] = {
550 EV_ALIAS("branches", "iap-br-inst-retired.any"),
551 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
552 EV_ALIAS("cycles", "tsc-tsc"),
553 EV_ALIAS("ic-misses", "iap-l1i-misses"),
554 EV_ALIAS("instructions", "iaf-instr-retired.any"),
555 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
556 EV_ALIAS("unhalted-cycles", "iaf-cpu-clk-unhalted.core"),
560 static struct pmc_event_alias core2_aliases_without_iaf[] = {
561 EV_ALIAS("branches", "iap-br-inst-retired.any"),
562 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
563 EV_ALIAS("cycles", "tsc-tsc"),
564 EV_ALIAS("ic-misses", "iap-l1i-misses"),
565 EV_ALIAS("instructions", "iap-inst-retired.any_p"),
566 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
567 EV_ALIAS("unhalted-cycles", "iap-cpu-clk-unhalted.core_p"),
571 #define atom_aliases core2_aliases
572 #define atom_aliases_without_iaf core2_aliases_without_iaf
573 #define corei7_aliases core2_aliases
574 #define corei7_aliases_without_iaf core2_aliases_without_iaf
575 #define ivybridge_aliases core2_aliases
576 #define ivybridge_aliases_without_iaf core2_aliases_without_iaf
577 #define sandybridge_aliases core2_aliases
578 #define sandybridge_aliases_without_iaf core2_aliases_without_iaf
579 #define sandybridge_xeon_aliases core2_aliases
580 #define sandybridge_xeon_aliases_without_iaf core2_aliases_without_iaf
581 #define westmere_aliases core2_aliases
582 #define westmere_aliases_without_iaf core2_aliases_without_iaf
584 #define IAF_KW_OS "os"
585 #define IAF_KW_USR "usr"
586 #define IAF_KW_ANYTHREAD "anythread"
589 * Parse an event specifier for Intel fixed function counters.
592 iaf_allocate_pmc(enum pmc_event pe, char *ctrspec,
593 struct pmc_op_pmcallocate *pmc_config)
599 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
600 pmc_config->pm_md.pm_iaf.pm_iaf_flags = 0;
602 while ((p = strsep(&ctrspec, ",")) != NULL) {
603 if (KWMATCH(p, IAF_KW_OS))
604 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
605 else if (KWMATCH(p, IAF_KW_USR))
606 pmc_config->pm_caps |= PMC_CAP_USER;
607 else if (KWMATCH(p, IAF_KW_ANYTHREAD))
608 pmc_config->pm_md.pm_iaf.pm_iaf_flags |= IAF_ANY;
617 * Core/Core2 support.
620 #define IAP_KW_AGENT "agent"
621 #define IAP_KW_ANYTHREAD "anythread"
622 #define IAP_KW_CACHESTATE "cachestate"
623 #define IAP_KW_CMASK "cmask"
624 #define IAP_KW_CORE "core"
625 #define IAP_KW_EDGE "edge"
626 #define IAP_KW_INV "inv"
627 #define IAP_KW_OS "os"
628 #define IAP_KW_PREFETCH "prefetch"
629 #define IAP_KW_SNOOPRESPONSE "snoopresponse"
630 #define IAP_KW_SNOOPTYPE "snooptype"
631 #define IAP_KW_TRANSITION "trans"
632 #define IAP_KW_USR "usr"
633 #define IAP_KW_RSP "rsp"
635 static struct pmc_masks iap_core_mask[] = {
636 PMCMASK(all, (0x3 << 14)),
637 PMCMASK(this, (0x1 << 14)),
641 static struct pmc_masks iap_agent_mask[] = {
643 PMCMASK(any, (0x1 << 13)),
647 static struct pmc_masks iap_prefetch_mask[] = {
648 PMCMASK(both, (0x3 << 12)),
649 PMCMASK(only, (0x1 << 12)),
654 static struct pmc_masks iap_cachestate_mask[] = {
655 PMCMASK(i, (1 << 8)),
656 PMCMASK(s, (1 << 9)),
657 PMCMASK(e, (1 << 10)),
658 PMCMASK(m, (1 << 11)),
662 static struct pmc_masks iap_snoopresponse_mask[] = {
663 PMCMASK(clean, (1 << 8)),
664 PMCMASK(hit, (1 << 9)),
665 PMCMASK(hitm, (1 << 11)),
669 static struct pmc_masks iap_snooptype_mask[] = {
670 PMCMASK(cmp2s, (1 << 8)),
671 PMCMASK(cmp2i, (1 << 9)),
675 static struct pmc_masks iap_transition_mask[] = {
677 PMCMASK(frequency, 0x10),
681 static struct pmc_masks iap_rsp_mask_i7_wm[] = {
682 PMCMASK(DMND_DATA_RD, (1 << 0)),
683 PMCMASK(DMND_RFO, (1 << 1)),
684 PMCMASK(DMND_IFETCH, (1 << 2)),
685 PMCMASK(WB, (1 << 3)),
686 PMCMASK(PF_DATA_RD, (1 << 4)),
687 PMCMASK(PF_RFO, (1 << 5)),
688 PMCMASK(PF_IFETCH, (1 << 6)),
689 PMCMASK(OTHER, (1 << 7)),
690 PMCMASK(UNCORE_HIT, (1 << 8)),
691 PMCMASK(OTHER_CORE_HIT_SNP, (1 << 9)),
692 PMCMASK(OTHER_CORE_HITM, (1 << 10)),
693 PMCMASK(REMOTE_CACHE_FWD, (1 << 12)),
694 PMCMASK(REMOTE_DRAM, (1 << 13)),
695 PMCMASK(LOCAL_DRAM, (1 << 14)),
696 PMCMASK(NON_DRAM, (1 << 15)),
700 static struct pmc_masks iap_rsp_mask_sb_sbx_ib[] = {
701 PMCMASK(REQ_DMND_DATA_RD, (1ULL << 0)),
702 PMCMASK(REQ_DMND_RFO, (1ULL << 1)),
703 PMCMASK(REQ_DMND_IFETCH, (1ULL << 2)),
704 PMCMASK(REQ_WB, (1ULL << 3)),
705 PMCMASK(REQ_PF_DATA_RD, (1ULL << 4)),
706 PMCMASK(REQ_PF_RFO, (1ULL << 5)),
707 PMCMASK(REQ_PF_IFETCH, (1ULL << 6)),
708 PMCMASK(REQ_PF_LLC_DATA_RD, (1ULL << 7)),
709 PMCMASK(REQ_PF_LLC_RFO, (1ULL << 8)),
710 PMCMASK(REQ_PF_LLC_IFETCH, (1ULL << 9)),
711 PMCMASK(REQ_BUS_LOCKS, (1ULL << 10)),
712 PMCMASK(REQ_STRM_ST, (1ULL << 11)),
713 PMCMASK(REQ_OTHER, (1ULL << 15)),
714 PMCMASK(RES_ANY, (1ULL << 16)),
715 PMCMASK(RES_SUPPLIER_SUPP, (1ULL << 17)),
716 PMCMASK(RES_SUPPLIER_LLC_HITM, (1ULL << 18)),
717 PMCMASK(RES_SUPPLIER_LLC_HITE, (1ULL << 19)),
718 PMCMASK(RES_SUPPLIER_LLC_HITS, (1ULL << 20)),
719 PMCMASK(RES_SUPPLIER_LLC_HITF, (1ULL << 21)),
720 PMCMASK(RES_SUPPLIER_LOCAL, (1ULL << 22)),
721 PMCMASK(RES_SNOOP_SNPI_NONE, (1ULL << 31)),
722 PMCMASK(RES_SNOOP_SNP_NO_NEEDED,(1ULL << 32)),
723 PMCMASK(RES_SNOOP_SNP_MISS, (1ULL << 33)),
724 PMCMASK(RES_SNOOP_HIT_NO_FWD, (1ULL << 34)),
725 PMCMASK(RES_SNOOP_HIT_FWD, (1ULL << 35)),
726 PMCMASK(RES_SNOOP_HITM, (1ULL << 36)),
727 PMCMASK(RES_NON_DRAM, (1ULL << 37)),
732 iap_allocate_pmc(enum pmc_event pe, char *ctrspec,
733 struct pmc_op_pmcallocate *pmc_config)
736 uint64_t cachestate, evmask, rsp;
739 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE |
741 pmc_config->pm_md.pm_iap.pm_iap_config = 0;
743 cachestate = evmask = rsp = 0;
745 /* Parse additional modifiers if present */
746 while ((p = strsep(&ctrspec, ",")) != NULL) {
749 if (KWPREFIXMATCH(p, IAP_KW_CMASK "=")) {
751 if (*++q == '\0') /* skip '=' */
753 count = strtol(q, &e, 0);
754 if (e == q || *e != '\0')
756 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
757 pmc_config->pm_md.pm_iap.pm_iap_config |=
759 } else if (KWMATCH(p, IAP_KW_EDGE)) {
760 pmc_config->pm_caps |= PMC_CAP_EDGE;
761 } else if (KWMATCH(p, IAP_KW_INV)) {
762 pmc_config->pm_caps |= PMC_CAP_INVERT;
763 } else if (KWMATCH(p, IAP_KW_OS)) {
764 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
765 } else if (KWMATCH(p, IAP_KW_USR)) {
766 pmc_config->pm_caps |= PMC_CAP_USER;
767 } else if (KWMATCH(p, IAP_KW_ANYTHREAD)) {
768 pmc_config->pm_md.pm_iap.pm_iap_config |= IAP_ANY;
769 } else if (KWPREFIXMATCH(p, IAP_KW_CORE "=")) {
770 n = pmc_parse_mask(iap_core_mask, p, &evmask);
773 } else if (KWPREFIXMATCH(p, IAP_KW_AGENT "=")) {
774 n = pmc_parse_mask(iap_agent_mask, p, &evmask);
777 } else if (KWPREFIXMATCH(p, IAP_KW_PREFETCH "=")) {
778 n = pmc_parse_mask(iap_prefetch_mask, p, &evmask);
781 } else if (KWPREFIXMATCH(p, IAP_KW_CACHESTATE "=")) {
782 n = pmc_parse_mask(iap_cachestate_mask, p, &cachestate);
783 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_CORE &&
784 KWPREFIXMATCH(p, IAP_KW_TRANSITION "=")) {
785 n = pmc_parse_mask(iap_transition_mask, p, &evmask);
788 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_ATOM ||
789 cpu_info.pm_cputype == PMC_CPU_INTEL_CORE2 ||
790 cpu_info.pm_cputype == PMC_CPU_INTEL_CORE2EXTREME) {
791 if (KWPREFIXMATCH(p, IAP_KW_SNOOPRESPONSE "=")) {
792 n = pmc_parse_mask(iap_snoopresponse_mask, p,
794 } else if (KWPREFIXMATCH(p, IAP_KW_SNOOPTYPE "=")) {
795 n = pmc_parse_mask(iap_snooptype_mask, p,
799 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_COREI7 ||
800 cpu_info.pm_cputype == PMC_CPU_INTEL_WESTMERE) {
801 if (KWPREFIXMATCH(p, IAP_KW_RSP "=")) {
802 n = pmc_parse_mask(iap_rsp_mask_i7_wm, p, &rsp);
805 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_SANDYBRIDGE ||
806 cpu_info.pm_cputype == PMC_CPU_INTEL_SANDYBRIDGE_XEON ||
807 cpu_info.pm_cputype == PMC_CPU_INTEL_IVYBRIDGE) {
808 if (KWPREFIXMATCH(p, IAP_KW_RSP "=")) {
809 n = pmc_parse_mask(iap_rsp_mask_sb_sbx_ib, p, &rsp);
815 if (n < 0) /* Parsing failed. */
819 pmc_config->pm_md.pm_iap.pm_iap_config |= evmask;
822 * If the event requires a 'cachestate' qualifier but was not
823 * specified by the user, use a sensible default.
826 case PMC_EV_IAP_EVENT_28H: /* Core, Core2, Atom */
827 case PMC_EV_IAP_EVENT_29H: /* Core, Core2, Atom */
828 case PMC_EV_IAP_EVENT_2AH: /* Core, Core2, Atom */
829 case PMC_EV_IAP_EVENT_2BH: /* Atom, Core2 */
830 case PMC_EV_IAP_EVENT_2EH: /* Core, Core2, Atom */
831 case PMC_EV_IAP_EVENT_30H: /* Core, Core2, Atom */
832 case PMC_EV_IAP_EVENT_32H: /* Core */
833 case PMC_EV_IAP_EVENT_40H: /* Core */
834 case PMC_EV_IAP_EVENT_41H: /* Core */
835 case PMC_EV_IAP_EVENT_42H: /* Core, Core2, Atom */
837 cachestate = (0xF << 8);
839 case PMC_EV_IAP_EVENT_77H: /* Atom */
840 /* IAP_EVENT_77H only accepts a cachestate qualifier on the
843 if(cpu_info.pm_cputype == PMC_CPU_INTEL_ATOM && cachestate == 0)
844 cachestate = (0xF << 8);
850 pmc_config->pm_md.pm_iap.pm_iap_config |= cachestate;
851 pmc_config->pm_md.pm_iap.pm_iap_rsp = rsp;
861 ucf_allocate_pmc(enum pmc_event pe, char *ctrspec,
862 struct pmc_op_pmcallocate *pmc_config)
867 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
868 pmc_config->pm_md.pm_ucf.pm_ucf_flags = 0;
873 #define UCP_KW_CMASK "cmask"
874 #define UCP_KW_EDGE "edge"
875 #define UCP_KW_INV "inv"
878 ucp_allocate_pmc(enum pmc_event pe, char *ctrspec,
879 struct pmc_op_pmcallocate *pmc_config)
886 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE |
888 pmc_config->pm_md.pm_ucp.pm_ucp_config = 0;
890 /* Parse additional modifiers if present */
891 while ((p = strsep(&ctrspec, ",")) != NULL) {
894 if (KWPREFIXMATCH(p, UCP_KW_CMASK "=")) {
896 if (*++q == '\0') /* skip '=' */
898 count = strtol(q, &e, 0);
899 if (e == q || *e != '\0')
901 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
902 pmc_config->pm_md.pm_ucp.pm_ucp_config |=
904 } else if (KWMATCH(p, UCP_KW_EDGE)) {
905 pmc_config->pm_caps |= PMC_CAP_EDGE;
906 } else if (KWMATCH(p, UCP_KW_INV)) {
907 pmc_config->pm_caps |= PMC_CAP_INVERT;
911 if (n < 0) /* Parsing failed. */
921 * These are very similar to AMD K7 PMCs, but support more kinds of
925 static struct pmc_event_alias k8_aliases[] = {
926 EV_ALIAS("branches", "k8-fr-retired-taken-branches"),
927 EV_ALIAS("branch-mispredicts",
928 "k8-fr-retired-taken-branches-mispredicted"),
929 EV_ALIAS("cycles", "tsc"),
930 EV_ALIAS("dc-misses", "k8-dc-miss"),
931 EV_ALIAS("ic-misses", "k8-ic-miss"),
932 EV_ALIAS("instructions", "k8-fr-retired-x86-instructions"),
933 EV_ALIAS("interrupts", "k8-fr-taken-hardware-interrupts"),
934 EV_ALIAS("unhalted-cycles", "k8-bu-cpu-clk-unhalted"),
938 #define __K8MASK(N,V) PMCMASK(N,(1 << (V)))
944 /* fp dispatched fpu ops */
945 static const struct pmc_masks k8_mask_fdfo[] = {
946 __K8MASK(add-pipe-excluding-junk-ops, 0),
947 __K8MASK(multiply-pipe-excluding-junk-ops, 1),
948 __K8MASK(store-pipe-excluding-junk-ops, 2),
949 __K8MASK(add-pipe-junk-ops, 3),
950 __K8MASK(multiply-pipe-junk-ops, 4),
951 __K8MASK(store-pipe-junk-ops, 5),
955 /* ls segment register loads */
956 static const struct pmc_masks k8_mask_lsrl[] = {
967 /* ls locked operation */
968 static const struct pmc_masks k8_mask_llo[] = {
969 __K8MASK(locked-instructions, 0),
970 __K8MASK(cycles-in-request, 1),
971 __K8MASK(cycles-to-complete, 2),
975 /* dc refill from {l2,system} and dc copyback */
976 static const struct pmc_masks k8_mask_dc[] = {
977 __K8MASK(invalid, 0),
979 __K8MASK(exclusive, 2),
981 __K8MASK(modified, 4),
985 /* dc one bit ecc error */
986 static const struct pmc_masks k8_mask_dobee[] = {
987 __K8MASK(scrubber, 0),
988 __K8MASK(piggyback, 1),
992 /* dc dispatched prefetch instructions */
993 static const struct pmc_masks k8_mask_ddpi[] = {
1000 /* dc dcache accesses by locks */
1001 static const struct pmc_masks k8_mask_dabl[] = {
1002 __K8MASK(accesses, 0),
1003 __K8MASK(misses, 1),
1007 /* bu internal l2 request */
1008 static const struct pmc_masks k8_mask_bilr[] = {
1009 __K8MASK(ic-fill, 0),
1010 __K8MASK(dc-fill, 1),
1011 __K8MASK(tlb-reload, 2),
1012 __K8MASK(tag-snoop, 3),
1013 __K8MASK(cancelled, 4),
1017 /* bu fill request l2 miss */
1018 static const struct pmc_masks k8_mask_bfrlm[] = {
1019 __K8MASK(ic-fill, 0),
1020 __K8MASK(dc-fill, 1),
1021 __K8MASK(tlb-reload, 2),
1025 /* bu fill into l2 */
1026 static const struct pmc_masks k8_mask_bfil[] = {
1027 __K8MASK(dirty-l2-victim, 0),
1028 __K8MASK(victim-from-l2, 1),
1032 /* fr retired fpu instructions */
1033 static const struct pmc_masks k8_mask_frfi[] = {
1035 __K8MASK(mmx-3dnow, 1),
1036 __K8MASK(packed-sse-sse2, 2),
1037 __K8MASK(scalar-sse-sse2, 3),
1041 /* fr retired fastpath double op instructions */
1042 static const struct pmc_masks k8_mask_frfdoi[] = {
1043 __K8MASK(low-op-pos-0, 0),
1044 __K8MASK(low-op-pos-1, 1),
1045 __K8MASK(low-op-pos-2, 2),
1049 /* fr fpu exceptions */
1050 static const struct pmc_masks k8_mask_ffe[] = {
1051 __K8MASK(x87-reclass-microfaults, 0),
1052 __K8MASK(sse-retype-microfaults, 1),
1053 __K8MASK(sse-reclass-microfaults, 2),
1054 __K8MASK(sse-and-x87-microtraps, 3),
1058 /* nb memory controller page access event */
1059 static const struct pmc_masks k8_mask_nmcpae[] = {
1060 __K8MASK(page-hit, 0),
1061 __K8MASK(page-miss, 1),
1062 __K8MASK(page-conflict, 2),
1066 /* nb memory controller turnaround */
1067 static const struct pmc_masks k8_mask_nmct[] = {
1068 __K8MASK(dimm-turnaround, 0),
1069 __K8MASK(read-to-write-turnaround, 1),
1070 __K8MASK(write-to-read-turnaround, 2),
1074 /* nb memory controller bypass saturation */
1075 static const struct pmc_masks k8_mask_nmcbs[] = {
1076 __K8MASK(memory-controller-hi-pri-bypass, 0),
1077 __K8MASK(memory-controller-lo-pri-bypass, 1),
1078 __K8MASK(dram-controller-interface-bypass, 2),
1079 __K8MASK(dram-controller-queue-bypass, 3),
1083 /* nb sized commands */
1084 static const struct pmc_masks k8_mask_nsc[] = {
1085 __K8MASK(nonpostwrszbyte, 0),
1086 __K8MASK(nonpostwrszdword, 1),
1087 __K8MASK(postwrszbyte, 2),
1088 __K8MASK(postwrszdword, 3),
1089 __K8MASK(rdszbyte, 4),
1090 __K8MASK(rdszdword, 5),
1091 __K8MASK(rdmodwr, 6),
1095 /* nb probe result */
1096 static const struct pmc_masks k8_mask_npr[] = {
1097 __K8MASK(probe-miss, 0),
1098 __K8MASK(probe-hit, 1),
1099 __K8MASK(probe-hit-dirty-no-memory-cancel, 2),
1100 __K8MASK(probe-hit-dirty-with-memory-cancel, 3),
1104 /* nb hypertransport bus bandwidth */
1105 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
1106 __K8MASK(command, 0),
1108 __K8MASK(buffer-release, 2),
1115 #define K8_KW_COUNT "count"
1116 #define K8_KW_EDGE "edge"
1117 #define K8_KW_INV "inv"
1118 #define K8_KW_MASK "mask"
1119 #define K8_KW_OS "os"
1120 #define K8_KW_USR "usr"
1123 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
1124 struct pmc_op_pmcallocate *pmc_config)
1130 const struct pmc_masks *pm, *pmask;
1132 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1133 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
1138 #define __K8SETMASK(M) pmask = k8_mask_##M
1140 /* setup parsing tables */
1142 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
1145 case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
1148 case PMC_EV_K8_LS_LOCKED_OPERATION:
1151 case PMC_EV_K8_DC_REFILL_FROM_L2:
1152 case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
1153 case PMC_EV_K8_DC_COPYBACK:
1156 case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
1159 case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
1162 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
1165 case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
1168 case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
1171 case PMC_EV_K8_BU_FILL_INTO_L2:
1174 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
1177 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
1178 __K8SETMASK(frfdoi);
1180 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
1183 case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
1184 __K8SETMASK(nmcpae);
1186 case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
1189 case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
1192 case PMC_EV_K8_NB_SIZED_COMMANDS:
1195 case PMC_EV_K8_NB_PROBE_RESULT:
1198 case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
1199 case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
1200 case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
1205 break; /* no options defined */
1208 while ((p = strsep(&ctrspec, ",")) != NULL) {
1209 if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
1211 if (*++q == '\0') /* skip '=' */
1214 count = strtol(q, &e, 0);
1215 if (e == q || *e != '\0')
1218 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
1219 pmc_config->pm_md.pm_amd.pm_amd_config |=
1220 AMD_PMC_TO_COUNTER(count);
1222 } else if (KWMATCH(p, K8_KW_EDGE)) {
1223 pmc_config->pm_caps |= PMC_CAP_EDGE;
1224 } else if (KWMATCH(p, K8_KW_INV)) {
1225 pmc_config->pm_caps |= PMC_CAP_INVERT;
1226 } else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
1227 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
1229 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1230 } else if (KWMATCH(p, K8_KW_OS)) {
1231 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
1232 } else if (KWMATCH(p, K8_KW_USR)) {
1233 pmc_config->pm_caps |= PMC_CAP_USER;
1238 /* other post processing */
1240 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
1241 case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
1242 case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
1243 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
1244 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
1245 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
1246 /* XXX only available in rev B and later */
1248 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
1249 /* XXX only available in rev C and later */
1251 case PMC_EV_K8_LS_LOCKED_OPERATION:
1252 /* XXX CPU Rev A,B evmask is to be zero */
1253 if (evmask & (evmask - 1)) /* > 1 bit set */
1256 evmask = 0x01; /* Rev C and later: #instrs */
1257 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1261 if (evmask == 0 && pmask != NULL) {
1262 for (pm = pmask; pm->pm_name; pm++)
1263 evmask |= pm->pm_value;
1264 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1268 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
1269 pmc_config->pm_md.pm_amd.pm_amd_config =
1270 AMD_PMC_TO_UNITMASK(evmask);
1277 #if defined(__amd64__) || defined(__i386__)
1283 static struct pmc_event_alias p4_aliases[] = {
1284 EV_ALIAS("branches", "p4-branch-retired,mask=mmtp+mmtm"),
1285 EV_ALIAS("branch-mispredicts", "p4-mispred-branch-retired"),
1286 EV_ALIAS("cycles", "tsc"),
1287 EV_ALIAS("instructions",
1288 "p4-instr-retired,mask=nbogusntag+nbogustag"),
1289 EV_ALIAS("unhalted-cycles", "p4-global-power-events"),
1290 EV_ALIAS(NULL, NULL)
1293 #define P4_KW_ACTIVE "active"
1294 #define P4_KW_ACTIVE_ANY "any"
1295 #define P4_KW_ACTIVE_BOTH "both"
1296 #define P4_KW_ACTIVE_NONE "none"
1297 #define P4_KW_ACTIVE_SINGLE "single"
1298 #define P4_KW_BUSREQTYPE "busreqtype"
1299 #define P4_KW_CASCADE "cascade"
1300 #define P4_KW_EDGE "edge"
1301 #define P4_KW_INV "complement"
1302 #define P4_KW_OS "os"
1303 #define P4_KW_MASK "mask"
1304 #define P4_KW_PRECISE "precise"
1305 #define P4_KW_TAG "tag"
1306 #define P4_KW_THRESHOLD "threshold"
1307 #define P4_KW_USR "usr"
1309 #define __P4MASK(N,V) PMCMASK(N, (1 << (V)))
1311 static const struct pmc_masks p4_mask_tcdm[] = { /* tc deliver mode */
1323 static const struct pmc_masks p4_mask_bfr[] = { /* bpu fetch request */
1324 __P4MASK(tcmiss, 0),
1328 static const struct pmc_masks p4_mask_ir[] = { /* itlb reference */
1331 __P4MASK(hit-uc, 2),
1335 static const struct pmc_masks p4_mask_memcan[] = { /* memory cancel */
1336 __P4MASK(st-rb-full, 2),
1337 __P4MASK(64k-conf, 3),
1341 static const struct pmc_masks p4_mask_memcomp[] = { /* memory complete */
1347 static const struct pmc_masks p4_mask_lpr[] = { /* load port replay */
1348 __P4MASK(split-ld, 1),
1352 static const struct pmc_masks p4_mask_spr[] = { /* store port replay */
1353 __P4MASK(split-st, 1),
1357 static const struct pmc_masks p4_mask_mlr[] = { /* mob load replay */
1358 __P4MASK(no-sta, 1),
1359 __P4MASK(no-std, 3),
1360 __P4MASK(partial-data, 4),
1361 __P4MASK(unalgn-addr, 5),
1365 static const struct pmc_masks p4_mask_pwt[] = { /* page walk type */
1366 __P4MASK(dtmiss, 0),
1367 __P4MASK(itmiss, 1),
1371 static const struct pmc_masks p4_mask_bcr[] = { /* bsq cache reference */
1372 __P4MASK(rd-2ndl-hits, 0),
1373 __P4MASK(rd-2ndl-hite, 1),
1374 __P4MASK(rd-2ndl-hitm, 2),
1375 __P4MASK(rd-3rdl-hits, 3),
1376 __P4MASK(rd-3rdl-hite, 4),
1377 __P4MASK(rd-3rdl-hitm, 5),
1378 __P4MASK(rd-2ndl-miss, 8),
1379 __P4MASK(rd-3rdl-miss, 9),
1380 __P4MASK(wr-2ndl-miss, 10),
1384 static const struct pmc_masks p4_mask_ia[] = { /* ioq allocation */
1385 __P4MASK(all-read, 5),
1386 __P4MASK(all-write, 6),
1387 __P4MASK(mem-uc, 7),
1388 __P4MASK(mem-wc, 8),
1389 __P4MASK(mem-wt, 9),
1390 __P4MASK(mem-wp, 10),
1391 __P4MASK(mem-wb, 11),
1393 __P4MASK(other, 14),
1394 __P4MASK(prefetch, 15),
1398 static const struct pmc_masks p4_mask_iae[] = { /* ioq active entries */
1399 __P4MASK(all-read, 5),
1400 __P4MASK(all-write, 6),
1401 __P4MASK(mem-uc, 7),
1402 __P4MASK(mem-wc, 8),
1403 __P4MASK(mem-wt, 9),
1404 __P4MASK(mem-wp, 10),
1405 __P4MASK(mem-wb, 11),
1407 __P4MASK(other, 14),
1408 __P4MASK(prefetch, 15),
1412 static const struct pmc_masks p4_mask_fda[] = { /* fsb data activity */
1413 __P4MASK(drdy-drv, 0),
1414 __P4MASK(drdy-own, 1),
1415 __P4MASK(drdy-other, 2),
1416 __P4MASK(dbsy-drv, 3),
1417 __P4MASK(dbsy-own, 4),
1418 __P4MASK(dbsy-other, 5),
1422 static const struct pmc_masks p4_mask_ba[] = { /* bsq allocation */
1423 __P4MASK(req-type0, 0),
1424 __P4MASK(req-type1, 1),
1425 __P4MASK(req-len0, 2),
1426 __P4MASK(req-len1, 3),
1427 __P4MASK(req-io-type, 5),
1428 __P4MASK(req-lock-type, 6),
1429 __P4MASK(req-cache-type, 7),
1430 __P4MASK(req-split-type, 8),
1431 __P4MASK(req-dem-type, 9),
1432 __P4MASK(req-ord-type, 10),
1433 __P4MASK(mem-type0, 11),
1434 __P4MASK(mem-type1, 12),
1435 __P4MASK(mem-type2, 13),
1439 static const struct pmc_masks p4_mask_sia[] = { /* sse input assist */
1444 static const struct pmc_masks p4_mask_psu[] = { /* packed sp uop */
1449 static const struct pmc_masks p4_mask_pdu[] = { /* packed dp uop */
1454 static const struct pmc_masks p4_mask_ssu[] = { /* scalar sp uop */
1459 static const struct pmc_masks p4_mask_sdu[] = { /* scalar dp uop */
1464 static const struct pmc_masks p4_mask_64bmu[] = { /* 64 bit mmx uop */
1469 static const struct pmc_masks p4_mask_128bmu[] = { /* 128 bit mmx uop */
1474 static const struct pmc_masks p4_mask_xfu[] = { /* X87 fp uop */
1479 static const struct pmc_masks p4_mask_xsmu[] = { /* x87 simd moves uop */
1485 static const struct pmc_masks p4_mask_gpe[] = { /* global power events */
1486 __P4MASK(running, 0),
1490 static const struct pmc_masks p4_mask_tmx[] = { /* TC ms xfer */
1495 static const struct pmc_masks p4_mask_uqw[] = { /* uop queue writes */
1496 __P4MASK(from-tc-build, 0),
1497 __P4MASK(from-tc-deliver, 1),
1498 __P4MASK(from-rom, 2),
1502 static const struct pmc_masks p4_mask_rmbt[] = {
1503 /* retired mispred branch type */
1504 __P4MASK(conditional, 1),
1506 __P4MASK(return, 3),
1507 __P4MASK(indirect, 4),
1511 static const struct pmc_masks p4_mask_rbt[] = { /* retired branch type */
1512 __P4MASK(conditional, 1),
1514 __P4MASK(retired, 3),
1515 __P4MASK(indirect, 4),
1519 static const struct pmc_masks p4_mask_rs[] = { /* resource stall */
1520 __P4MASK(sbfull, 5),
1524 static const struct pmc_masks p4_mask_wb[] = { /* WC buffer */
1525 __P4MASK(wcb-evicts, 0),
1526 __P4MASK(wcb-full-evict, 1),
1530 static const struct pmc_masks p4_mask_fee[] = { /* front end event */
1531 __P4MASK(nbogus, 0),
1536 static const struct pmc_masks p4_mask_ee[] = { /* execution event */
1537 __P4MASK(nbogus0, 0),
1538 __P4MASK(nbogus1, 1),
1539 __P4MASK(nbogus2, 2),
1540 __P4MASK(nbogus3, 3),
1541 __P4MASK(bogus0, 4),
1542 __P4MASK(bogus1, 5),
1543 __P4MASK(bogus2, 6),
1544 __P4MASK(bogus3, 7),
1548 static const struct pmc_masks p4_mask_re[] = { /* replay event */
1549 __P4MASK(nbogus, 0),
1554 static const struct pmc_masks p4_mask_insret[] = { /* instr retired */
1555 __P4MASK(nbogusntag, 0),
1556 __P4MASK(nbogustag, 1),
1557 __P4MASK(bogusntag, 2),
1558 __P4MASK(bogustag, 3),
1562 static const struct pmc_masks p4_mask_ur[] = { /* uops retired */
1563 __P4MASK(nbogus, 0),
1568 static const struct pmc_masks p4_mask_ut[] = { /* uop type */
1569 __P4MASK(tagloads, 1),
1570 __P4MASK(tagstores, 2),
1574 static const struct pmc_masks p4_mask_br[] = { /* branch retired */
1582 static const struct pmc_masks p4_mask_mbr[] = { /* mispred branch retired */
1583 __P4MASK(nbogus, 0),
1587 static const struct pmc_masks p4_mask_xa[] = { /* x87 assist */
1596 static const struct pmc_masks p4_mask_machclr[] = { /* machine clear */
1598 __P4MASK(moclear, 2),
1599 __P4MASK(smclear, 3),
1603 /* P4 event parser */
1605 p4_allocate_pmc(enum pmc_event pe, char *ctrspec,
1606 struct pmc_op_pmcallocate *pmc_config)
1610 int count, has_tag, has_busreqtype, n;
1611 uint32_t cccractivemask;
1613 const struct pmc_masks *pm, *pmask;
1615 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1616 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig =
1617 pmc_config->pm_md.pm_p4.pm_p4_escrconfig = 0;
1621 cccractivemask = 0x3;
1622 has_tag = has_busreqtype = 0;
1624 #define __P4SETMASK(M) do { \
1625 pmask = p4_mask_##M; \
1629 case PMC_EV_P4_TC_DELIVER_MODE:
1632 case PMC_EV_P4_BPU_FETCH_REQUEST:
1635 case PMC_EV_P4_ITLB_REFERENCE:
1638 case PMC_EV_P4_MEMORY_CANCEL:
1639 __P4SETMASK(memcan);
1641 case PMC_EV_P4_MEMORY_COMPLETE:
1642 __P4SETMASK(memcomp);
1644 case PMC_EV_P4_LOAD_PORT_REPLAY:
1647 case PMC_EV_P4_STORE_PORT_REPLAY:
1650 case PMC_EV_P4_MOB_LOAD_REPLAY:
1653 case PMC_EV_P4_PAGE_WALK_TYPE:
1656 case PMC_EV_P4_BSQ_CACHE_REFERENCE:
1659 case PMC_EV_P4_IOQ_ALLOCATION:
1663 case PMC_EV_P4_IOQ_ACTIVE_ENTRIES:
1667 case PMC_EV_P4_FSB_DATA_ACTIVITY:
1670 case PMC_EV_P4_BSQ_ALLOCATION:
1673 case PMC_EV_P4_SSE_INPUT_ASSIST:
1676 case PMC_EV_P4_PACKED_SP_UOP:
1679 case PMC_EV_P4_PACKED_DP_UOP:
1682 case PMC_EV_P4_SCALAR_SP_UOP:
1685 case PMC_EV_P4_SCALAR_DP_UOP:
1688 case PMC_EV_P4_64BIT_MMX_UOP:
1691 case PMC_EV_P4_128BIT_MMX_UOP:
1692 __P4SETMASK(128bmu);
1694 case PMC_EV_P4_X87_FP_UOP:
1697 case PMC_EV_P4_X87_SIMD_MOVES_UOP:
1700 case PMC_EV_P4_GLOBAL_POWER_EVENTS:
1703 case PMC_EV_P4_TC_MS_XFER:
1706 case PMC_EV_P4_UOP_QUEUE_WRITES:
1709 case PMC_EV_P4_RETIRED_MISPRED_BRANCH_TYPE:
1712 case PMC_EV_P4_RETIRED_BRANCH_TYPE:
1715 case PMC_EV_P4_RESOURCE_STALL:
1718 case PMC_EV_P4_WC_BUFFER:
1721 case PMC_EV_P4_BSQ_ACTIVE_ENTRIES:
1722 case PMC_EV_P4_B2B_CYCLES:
1724 case PMC_EV_P4_SNOOP:
1725 case PMC_EV_P4_RESPONSE:
1727 case PMC_EV_P4_FRONT_END_EVENT:
1730 case PMC_EV_P4_EXECUTION_EVENT:
1733 case PMC_EV_P4_REPLAY_EVENT:
1736 case PMC_EV_P4_INSTR_RETIRED:
1737 __P4SETMASK(insret);
1739 case PMC_EV_P4_UOPS_RETIRED:
1742 case PMC_EV_P4_UOP_TYPE:
1745 case PMC_EV_P4_BRANCH_RETIRED:
1748 case PMC_EV_P4_MISPRED_BRANCH_RETIRED:
1751 case PMC_EV_P4_X87_ASSIST:
1754 case PMC_EV_P4_MACHINE_CLEAR:
1755 __P4SETMASK(machclr);
1761 /* process additional flags */
1762 while ((p = strsep(&ctrspec, ",")) != NULL) {
1763 if (KWPREFIXMATCH(p, P4_KW_ACTIVE)) {
1765 if (*++q == '\0') /* skip '=' */
1768 if (strcasecmp(q, P4_KW_ACTIVE_NONE) == 0)
1769 cccractivemask = 0x0;
1770 else if (strcasecmp(q, P4_KW_ACTIVE_SINGLE) == 0)
1771 cccractivemask = 0x1;
1772 else if (strcasecmp(q, P4_KW_ACTIVE_BOTH) == 0)
1773 cccractivemask = 0x2;
1774 else if (strcasecmp(q, P4_KW_ACTIVE_ANY) == 0)
1775 cccractivemask = 0x3;
1779 } else if (KWPREFIXMATCH(p, P4_KW_BUSREQTYPE)) {
1780 if (has_busreqtype == 0)
1784 if (*++q == '\0') /* skip '=' */
1787 count = strtol(q, &e, 0);
1788 if (e == q || *e != '\0')
1790 evmask = (evmask & ~0x1F) | (count & 0x1F);
1791 } else if (KWMATCH(p, P4_KW_CASCADE))
1792 pmc_config->pm_caps |= PMC_CAP_CASCADE;
1793 else if (KWMATCH(p, P4_KW_EDGE))
1794 pmc_config->pm_caps |= PMC_CAP_EDGE;
1795 else if (KWMATCH(p, P4_KW_INV))
1796 pmc_config->pm_caps |= PMC_CAP_INVERT;
1797 else if (KWPREFIXMATCH(p, P4_KW_MASK "=")) {
1798 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
1800 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1801 } else if (KWMATCH(p, P4_KW_OS))
1802 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
1803 else if (KWMATCH(p, P4_KW_PRECISE))
1804 pmc_config->pm_caps |= PMC_CAP_PRECISE;
1805 else if (KWPREFIXMATCH(p, P4_KW_TAG "=")) {
1810 if (*++q == '\0') /* skip '=' */
1813 count = strtol(q, &e, 0);
1814 if (e == q || *e != '\0')
1817 pmc_config->pm_caps |= PMC_CAP_TAGGING;
1818 pmc_config->pm_md.pm_p4.pm_p4_escrconfig |=
1819 P4_ESCR_TO_TAG_VALUE(count);
1820 } else if (KWPREFIXMATCH(p, P4_KW_THRESHOLD "=")) {
1822 if (*++q == '\0') /* skip '=' */
1825 count = strtol(q, &e, 0);
1826 if (e == q || *e != '\0')
1829 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
1830 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig &=
1831 ~P4_CCCR_THRESHOLD_MASK;
1832 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig |=
1833 P4_CCCR_TO_THRESHOLD(count);
1834 } else if (KWMATCH(p, P4_KW_USR))
1835 pmc_config->pm_caps |= PMC_CAP_USER;
1840 /* other post processing */
1841 if (pe == PMC_EV_P4_IOQ_ALLOCATION ||
1842 pe == PMC_EV_P4_FSB_DATA_ACTIVITY ||
1843 pe == PMC_EV_P4_BSQ_ALLOCATION)
1844 pmc_config->pm_caps |= PMC_CAP_EDGE;
1846 /* fill in thread activity mask */
1847 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig |=
1848 P4_CCCR_TO_ACTIVE_THREAD(cccractivemask);
1851 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1854 case PMC_EV_P4_FSB_DATA_ACTIVITY:
1855 if ((evmask & 0x06) == 0x06 ||
1856 (evmask & 0x18) == 0x18)
1857 return (-1); /* can't have own+other bits together */
1858 if (evmask == 0) /* default:drdy-{drv,own}+dbsy{drv,own} */
1861 case PMC_EV_P4_MACHINE_CLEAR:
1862 /* only one bit is allowed to be set */
1863 if ((evmask & (evmask - 1)) != 0)
1866 evmask = 0x1; /* 'CLEAR' */
1867 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1871 if (evmask == 0 && pmask) {
1872 for (pm = pmask; pm->pm_name; pm++)
1873 evmask |= pm->pm_value;
1874 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1878 pmc_config->pm_md.pm_p4.pm_p4_escrconfig =
1879 P4_ESCR_TO_EVENT_MASK(evmask);
1886 #if defined(__i386__)
1889 * Pentium style PMCs
1892 static struct pmc_event_alias p5_aliases[] = {
1893 EV_ALIAS("branches", "p5-taken-branches"),
1894 EV_ALIAS("cycles", "tsc"),
1895 EV_ALIAS("dc-misses", "p5-data-read-miss-or-write-miss"),
1896 EV_ALIAS("ic-misses", "p5-code-cache-miss"),
1897 EV_ALIAS("instructions", "p5-instructions-executed"),
1898 EV_ALIAS("interrupts", "p5-hardware-interrupts"),
1899 EV_ALIAS("unhalted-cycles",
1900 "p5-number-of-cycles-not-in-halt-state"),
1901 EV_ALIAS(NULL, NULL)
1905 p5_allocate_pmc(enum pmc_event pe, char *ctrspec,
1906 struct pmc_op_pmcallocate *pmc_config)
1908 return (-1 || pe || ctrspec || pmc_config); /* shut up gcc */
1912 * Pentium Pro style PMCs. These PMCs are found in Pentium II, Pentium III,
1913 * and Pentium M CPUs.
1916 static struct pmc_event_alias p6_aliases[] = {
1917 EV_ALIAS("branches", "p6-br-inst-retired"),
1918 EV_ALIAS("branch-mispredicts", "p6-br-miss-pred-retired"),
1919 EV_ALIAS("cycles", "tsc"),
1920 EV_ALIAS("dc-misses", "p6-dcu-lines-in"),
1921 EV_ALIAS("ic-misses", "p6-ifu-fetch-miss"),
1922 EV_ALIAS("instructions", "p6-inst-retired"),
1923 EV_ALIAS("interrupts", "p6-hw-int-rx"),
1924 EV_ALIAS("unhalted-cycles", "p6-cpu-clk-unhalted"),
1925 EV_ALIAS(NULL, NULL)
1928 #define P6_KW_CMASK "cmask"
1929 #define P6_KW_EDGE "edge"
1930 #define P6_KW_INV "inv"
1931 #define P6_KW_OS "os"
1932 #define P6_KW_UMASK "umask"
1933 #define P6_KW_USR "usr"
1935 static struct pmc_masks p6_mask_mesi[] = {
1943 static struct pmc_masks p6_mask_mesihw[] = {
1948 PMCMASK(nonhw, 0x00),
1950 PMCMASK(both, 0x30),
1954 static struct pmc_masks p6_mask_hw[] = {
1955 PMCMASK(nonhw, 0x00),
1957 PMCMASK(both, 0x30),
1961 static struct pmc_masks p6_mask_any[] = {
1962 PMCMASK(self, 0x00),
1967 static struct pmc_masks p6_mask_ekp[] = {
1975 static struct pmc_masks p6_mask_pps[] = {
1976 PMCMASK(packed-and-scalar, 0x00),
1977 PMCMASK(scalar, 0x01),
1981 static struct pmc_masks p6_mask_mite[] = {
1982 PMCMASK(packed-multiply, 0x01),
1983 PMCMASK(packed-shift, 0x02),
1984 PMCMASK(pack, 0x04),
1985 PMCMASK(unpack, 0x08),
1986 PMCMASK(packed-logical, 0x10),
1987 PMCMASK(packed-arithmetic, 0x20),
1991 static struct pmc_masks p6_mask_fmt[] = {
1992 PMCMASK(mmxtofp, 0x00),
1993 PMCMASK(fptommx, 0x01),
1997 static struct pmc_masks p6_mask_sr[] = {
2005 static struct pmc_masks p6_mask_eet[] = {
2007 PMCMASK(freq, 0x02),
2011 static struct pmc_masks p6_mask_efur[] = {
2013 PMCMASK(loadop, 0x01),
2014 PMCMASK(stdsta, 0x02),
2018 static struct pmc_masks p6_mask_essir[] = {
2019 PMCMASK(sse-packed-single, 0x00),
2020 PMCMASK(sse-packed-single-scalar-single, 0x01),
2021 PMCMASK(sse2-packed-double, 0x02),
2022 PMCMASK(sse2-scalar-double, 0x03),
2026 static struct pmc_masks p6_mask_esscir[] = {
2027 PMCMASK(sse-packed-single, 0x00),
2028 PMCMASK(sse-scalar-single, 0x01),
2029 PMCMASK(sse2-packed-double, 0x02),
2030 PMCMASK(sse2-scalar-double, 0x03),
2034 /* P6 event parser */
2036 p6_allocate_pmc(enum pmc_event pe, char *ctrspec,
2037 struct pmc_op_pmcallocate *pmc_config)
2042 const struct pmc_masks *pm, *pmask;
2044 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2045 pmc_config->pm_md.pm_ppro.pm_ppro_config = 0;
2049 #define P6MASKSET(M) pmask = p6_mask_ ## M
2052 case PMC_EV_P6_L2_IFETCH: P6MASKSET(mesi); break;
2053 case PMC_EV_P6_L2_LD: P6MASKSET(mesi); break;
2054 case PMC_EV_P6_L2_ST: P6MASKSET(mesi); break;
2055 case PMC_EV_P6_L2_RQSTS: P6MASKSET(mesi); break;
2056 case PMC_EV_P6_BUS_DRDY_CLOCKS:
2057 case PMC_EV_P6_BUS_LOCK_CLOCKS:
2058 case PMC_EV_P6_BUS_TRAN_BRD:
2059 case PMC_EV_P6_BUS_TRAN_RFO:
2060 case PMC_EV_P6_BUS_TRANS_WB:
2061 case PMC_EV_P6_BUS_TRAN_IFETCH:
2062 case PMC_EV_P6_BUS_TRAN_INVAL:
2063 case PMC_EV_P6_BUS_TRAN_PWR:
2064 case PMC_EV_P6_BUS_TRANS_P:
2065 case PMC_EV_P6_BUS_TRANS_IO:
2066 case PMC_EV_P6_BUS_TRAN_DEF:
2067 case PMC_EV_P6_BUS_TRAN_BURST:
2068 case PMC_EV_P6_BUS_TRAN_ANY:
2069 case PMC_EV_P6_BUS_TRAN_MEM:
2070 P6MASKSET(any); break;
2071 case PMC_EV_P6_EMON_KNI_PREF_DISPATCHED:
2072 case PMC_EV_P6_EMON_KNI_PREF_MISS:
2073 P6MASKSET(ekp); break;
2074 case PMC_EV_P6_EMON_KNI_INST_RETIRED:
2075 case PMC_EV_P6_EMON_KNI_COMP_INST_RET:
2076 P6MASKSET(pps); break;
2077 case PMC_EV_P6_MMX_INSTR_TYPE_EXEC:
2078 P6MASKSET(mite); break;
2079 case PMC_EV_P6_FP_MMX_TRANS:
2080 P6MASKSET(fmt); break;
2081 case PMC_EV_P6_SEG_RENAME_STALLS:
2082 case PMC_EV_P6_SEG_REG_RENAMES:
2083 P6MASKSET(sr); break;
2084 case PMC_EV_P6_EMON_EST_TRANS:
2085 P6MASKSET(eet); break;
2086 case PMC_EV_P6_EMON_FUSED_UOPS_RET:
2087 P6MASKSET(efur); break;
2088 case PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED:
2089 P6MASKSET(essir); break;
2090 case PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED:
2091 P6MASKSET(esscir); break;
2097 /* Pentium M PMCs have a few events with different semantics */
2098 if (cpu_info.pm_cputype == PMC_CPU_INTEL_PM) {
2099 if (pe == PMC_EV_P6_L2_LD ||
2100 pe == PMC_EV_P6_L2_LINES_IN ||
2101 pe == PMC_EV_P6_L2_LINES_OUT)
2103 else if (pe == PMC_EV_P6_L2_M_LINES_OUTM)
2107 /* Parse additional modifiers if present */
2108 while ((p = strsep(&ctrspec, ",")) != NULL) {
2109 if (KWPREFIXMATCH(p, P6_KW_CMASK "=")) {
2111 if (*++q == '\0') /* skip '=' */
2113 count = strtol(q, &e, 0);
2114 if (e == q || *e != '\0')
2116 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
2117 pmc_config->pm_md.pm_ppro.pm_ppro_config |=
2118 P6_EVSEL_TO_CMASK(count);
2119 } else if (KWMATCH(p, P6_KW_EDGE)) {
2120 pmc_config->pm_caps |= PMC_CAP_EDGE;
2121 } else if (KWMATCH(p, P6_KW_INV)) {
2122 pmc_config->pm_caps |= PMC_CAP_INVERT;
2123 } else if (KWMATCH(p, P6_KW_OS)) {
2124 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2125 } else if (KWPREFIXMATCH(p, P6_KW_UMASK "=")) {
2127 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
2129 if ((pe == PMC_EV_P6_BUS_DRDY_CLOCKS ||
2130 pe == PMC_EV_P6_BUS_LOCK_CLOCKS ||
2131 pe == PMC_EV_P6_BUS_TRAN_BRD ||
2132 pe == PMC_EV_P6_BUS_TRAN_RFO ||
2133 pe == PMC_EV_P6_BUS_TRAN_IFETCH ||
2134 pe == PMC_EV_P6_BUS_TRAN_INVAL ||
2135 pe == PMC_EV_P6_BUS_TRAN_PWR ||
2136 pe == PMC_EV_P6_BUS_TRAN_DEF ||
2137 pe == PMC_EV_P6_BUS_TRAN_BURST ||
2138 pe == PMC_EV_P6_BUS_TRAN_ANY ||
2139 pe == PMC_EV_P6_BUS_TRAN_MEM ||
2140 pe == PMC_EV_P6_BUS_TRANS_IO ||
2141 pe == PMC_EV_P6_BUS_TRANS_P ||
2142 pe == PMC_EV_P6_BUS_TRANS_WB ||
2143 pe == PMC_EV_P6_EMON_EST_TRANS ||
2144 pe == PMC_EV_P6_EMON_FUSED_UOPS_RET ||
2145 pe == PMC_EV_P6_EMON_KNI_COMP_INST_RET ||
2146 pe == PMC_EV_P6_EMON_KNI_INST_RETIRED ||
2147 pe == PMC_EV_P6_EMON_KNI_PREF_DISPATCHED ||
2148 pe == PMC_EV_P6_EMON_KNI_PREF_MISS ||
2149 pe == PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED ||
2150 pe == PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED ||
2151 pe == PMC_EV_P6_FP_MMX_TRANS)
2152 && (n > 1)) /* Only one mask keyword is allowed. */
2154 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2155 } else if (KWMATCH(p, P6_KW_USR)) {
2156 pmc_config->pm_caps |= PMC_CAP_USER;
2161 /* post processing */
2165 * The following events default to an evmask of 0
2168 /* default => 'self' */
2169 case PMC_EV_P6_BUS_DRDY_CLOCKS:
2170 case PMC_EV_P6_BUS_LOCK_CLOCKS:
2171 case PMC_EV_P6_BUS_TRAN_BRD:
2172 case PMC_EV_P6_BUS_TRAN_RFO:
2173 case PMC_EV_P6_BUS_TRANS_WB:
2174 case PMC_EV_P6_BUS_TRAN_IFETCH:
2175 case PMC_EV_P6_BUS_TRAN_INVAL:
2176 case PMC_EV_P6_BUS_TRAN_PWR:
2177 case PMC_EV_P6_BUS_TRANS_P:
2178 case PMC_EV_P6_BUS_TRANS_IO:
2179 case PMC_EV_P6_BUS_TRAN_DEF:
2180 case PMC_EV_P6_BUS_TRAN_BURST:
2181 case PMC_EV_P6_BUS_TRAN_ANY:
2182 case PMC_EV_P6_BUS_TRAN_MEM:
2184 /* default => 'nta' */
2185 case PMC_EV_P6_EMON_KNI_PREF_DISPATCHED:
2186 case PMC_EV_P6_EMON_KNI_PREF_MISS:
2188 /* default => 'packed and scalar' */
2189 case PMC_EV_P6_EMON_KNI_INST_RETIRED:
2190 case PMC_EV_P6_EMON_KNI_COMP_INST_RET:
2192 /* default => 'mmx to fp transitions' */
2193 case PMC_EV_P6_FP_MMX_TRANS:
2195 /* default => 'SSE Packed Single' */
2196 case PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED:
2197 case PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED:
2199 /* default => 'all fused micro-ops' */
2200 case PMC_EV_P6_EMON_FUSED_UOPS_RET:
2202 /* default => 'all transitions' */
2203 case PMC_EV_P6_EMON_EST_TRANS:
2206 case PMC_EV_P6_MMX_UOPS_EXEC:
2207 evmask = 0x0F; /* only value allowed */
2212 * For all other events, set the default event mask
2213 * to a logical OR of all the allowed event mask bits.
2215 if (evmask == 0 && pmask) {
2216 for (pm = pmask; pm->pm_name; pm++)
2217 evmask |= pm->pm_value;
2218 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2224 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
2225 pmc_config->pm_md.pm_ppro.pm_ppro_config |=
2226 P6_EVSEL_TO_UMASK(evmask);
2233 #if defined(__i386__) || defined(__amd64__)
2235 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
2236 struct pmc_op_pmcallocate *pmc_config)
2238 if (pe != PMC_EV_TSC_TSC)
2241 /* TSC events must be unqualified. */
2242 if (ctrspec && *ctrspec != '\0')
2245 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
2246 pmc_config->pm_caps |= PMC_CAP_READ;
2252 static struct pmc_event_alias generic_aliases[] = {
2253 EV_ALIAS("instructions", "SOFT-CLOCK.HARD"),
2254 EV_ALIAS(NULL, NULL)
2258 soft_allocate_pmc(enum pmc_event pe, char *ctrspec,
2259 struct pmc_op_pmcallocate *pmc_config)
2264 if (pe < PMC_EV_SOFT_FIRST || pe > PMC_EV_SOFT_LAST)
2267 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2271 #if defined(__XSCALE__)
2273 static struct pmc_event_alias xscale_aliases[] = {
2274 EV_ALIAS("branches", "BRANCH_RETIRED"),
2275 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
2276 EV_ALIAS("dc-misses", "DC_MISS"),
2277 EV_ALIAS("ic-misses", "IC_MISS"),
2278 EV_ALIAS("instructions", "INSTR_RETIRED"),
2279 EV_ALIAS(NULL, NULL)
2282 xscale_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2283 struct pmc_op_pmcallocate *pmc_config __unused)
2294 #if defined(__mips__)
2296 static struct pmc_event_alias mips24k_aliases[] = {
2297 EV_ALIAS("instructions", "INSTR_EXECUTED"),
2298 EV_ALIAS("branches", "BRANCH_COMPLETED"),
2299 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
2300 EV_ALIAS(NULL, NULL)
2303 #define MIPS24K_KW_OS "os"
2304 #define MIPS24K_KW_USR "usr"
2305 #define MIPS24K_KW_ANYTHREAD "anythread"
2308 mips24k_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2309 struct pmc_op_pmcallocate *pmc_config __unused)
2315 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2317 while ((p = strsep(&ctrspec, ",")) != NULL) {
2318 if (KWMATCH(p, MIPS24K_KW_OS))
2319 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2320 else if (KWMATCH(p, MIPS24K_KW_USR))
2321 pmc_config->pm_caps |= PMC_CAP_USER;
2322 else if (KWMATCH(p, MIPS24K_KW_ANYTHREAD))
2323 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
2330 #endif /* __mips__ */
2332 #if defined(__powerpc__)
2334 static struct pmc_event_alias ppc7450_aliases[] = {
2335 EV_ALIAS("instructions", "INSTR_COMPLETED"),
2336 EV_ALIAS("branches", "BRANCHES_COMPLETED"),
2337 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCHES"),
2338 EV_ALIAS(NULL, NULL)
2341 #define PPC7450_KW_OS "os"
2342 #define PPC7450_KW_USR "usr"
2343 #define PPC7450_KW_ANYTHREAD "anythread"
2346 ppc7450_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2347 struct pmc_op_pmcallocate *pmc_config __unused)
2353 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2355 while ((p = strsep(&ctrspec, ",")) != NULL) {
2356 if (KWMATCH(p, PPC7450_KW_OS))
2357 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2358 else if (KWMATCH(p, PPC7450_KW_USR))
2359 pmc_config->pm_caps |= PMC_CAP_USER;
2360 else if (KWMATCH(p, PPC7450_KW_ANYTHREAD))
2361 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
2368 #endif /* __powerpc__ */
2372 * Match an event name `name' with its canonical form.
2374 * Matches are case insensitive and spaces, periods, underscores and
2375 * hyphen characters are considered to match each other.
2377 * Returns 1 for a match, 0 otherwise.
2381 pmc_match_event_name(const char *name, const char *canonicalname)
2384 const unsigned char *c, *n;
2386 c = (const unsigned char *) canonicalname;
2387 n = (const unsigned char *) name;
2389 for (; (nc = *n) && (cc = *c); n++, c++) {
2391 if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
2392 (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
2395 if (toupper(nc) == toupper(cc))
2402 if (*n == '\0' && *c == '\0')
2409 * Match an event name against all the event named supported by a
2412 * Returns an event descriptor pointer on match or NULL otherwise.
2414 static const struct pmc_event_descr *
2415 pmc_match_event_class(const char *name,
2416 const struct pmc_class_descr *pcd)
2419 const struct pmc_event_descr *ev;
2421 ev = pcd->pm_evc_event_table;
2422 for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
2423 if (pmc_match_event_name(name, ev->pm_ev_name))
2430 pmc_mdep_is_compatible_class(enum pmc_class pc)
2434 for (n = 0; n < pmc_mdep_class_list_size; n++)
2435 if (pmc_mdep_class_list[n] == pc)
2445 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
2446 uint32_t flags, int cpu, pmc_id_t *pmcid)
2450 char *r, *spec_copy;
2451 const char *ctrname;
2452 const struct pmc_event_descr *ev;
2453 const struct pmc_event_alias *alias;
2454 struct pmc_op_pmcallocate pmc_config;
2455 const struct pmc_class_descr *pcd;
2460 if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
2461 mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
2466 /* replace an event alias with the canonical event specifier */
2467 if (pmc_mdep_event_aliases)
2468 for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
2469 if (!strcasecmp(ctrspec, alias->pm_alias)) {
2470 spec_copy = strdup(alias->pm_spec);
2474 if (spec_copy == NULL)
2475 spec_copy = strdup(ctrspec);
2478 ctrname = strsep(&r, ",");
2481 * If a explicit class prefix was given by the user, restrict the
2482 * search for the event to the specified PMC class.
2485 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
2486 pcd = pmc_class_table[n];
2487 if (pmc_mdep_is_compatible_class(pcd->pm_evc_class) &&
2488 strncasecmp(ctrname, pcd->pm_evc_name,
2489 pcd->pm_evc_name_size) == 0) {
2490 if ((ev = pmc_match_event_class(ctrname +
2491 pcd->pm_evc_name_size, pcd)) == NULL) {
2500 * Otherwise, search for this event in all compatible PMC
2503 for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
2504 pcd = pmc_class_table[n];
2505 if (pmc_mdep_is_compatible_class(pcd->pm_evc_class))
2506 ev = pmc_match_event_class(ctrname, pcd);
2514 bzero(&pmc_config, sizeof(pmc_config));
2515 pmc_config.pm_ev = ev->pm_ev_code;
2516 pmc_config.pm_class = pcd->pm_evc_class;
2517 pmc_config.pm_cpu = cpu;
2518 pmc_config.pm_mode = mode;
2519 pmc_config.pm_flags = flags;
2521 if (PMC_IS_SAMPLING_MODE(mode))
2522 pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
2524 if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
2529 if (PMC_CALL(PMCALLOCATE, &pmc_config) < 0)
2532 *pmcid = pmc_config.pm_pmcid;
2544 pmc_attach(pmc_id_t pmc, pid_t pid)
2546 struct pmc_op_pmcattach pmc_attach_args;
2548 pmc_attach_args.pm_pmc = pmc;
2549 pmc_attach_args.pm_pid = pid;
2551 return (PMC_CALL(PMCATTACH, &pmc_attach_args));
2555 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
2560 cl = PMC_ID_TO_CLASS(pmcid);
2561 for (i = 0; i < cpu_info.pm_nclass; i++)
2562 if (cpu_info.pm_classes[i].pm_class == cl) {
2563 *caps = cpu_info.pm_classes[i].pm_caps;
2571 pmc_configure_logfile(int fd)
2573 struct pmc_op_configurelog cla;
2576 if (PMC_CALL(CONFIGURELOG, &cla) < 0)
2582 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
2584 if (pmc_syscall == -1) {
2594 pmc_detach(pmc_id_t pmc, pid_t pid)
2596 struct pmc_op_pmcattach pmc_detach_args;
2598 pmc_detach_args.pm_pmc = pmc;
2599 pmc_detach_args.pm_pid = pid;
2600 return (PMC_CALL(PMCDETACH, &pmc_detach_args));
2604 pmc_disable(int cpu, int pmc)
2606 struct pmc_op_pmcadmin ssa;
2610 ssa.pm_state = PMC_STATE_DISABLED;
2611 return (PMC_CALL(PMCADMIN, &ssa));
2615 pmc_enable(int cpu, int pmc)
2617 struct pmc_op_pmcadmin ssa;
2621 ssa.pm_state = PMC_STATE_FREE;
2622 return (PMC_CALL(PMCADMIN, &ssa));
2626 * Return a list of events known to a given PMC class. 'cl' is the
2627 * PMC class identifier, 'eventnames' is the returned list of 'const
2628 * char *' pointers pointing to the names of the events. 'nevents' is
2629 * the number of event name pointers returned.
2631 * The space for 'eventnames' is allocated using malloc(3). The caller
2632 * is responsible for freeing this space when done.
2635 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
2640 const struct pmc_event_descr *ev;
2645 ev = iaf_event_table;
2646 count = PMC_EVENT_TABLE_SIZE(iaf);
2650 * Return the most appropriate set of event name
2651 * spellings for the current CPU.
2653 switch (cpu_info.pm_cputype) {
2655 case PMC_CPU_INTEL_ATOM:
2656 ev = atom_event_table;
2657 count = PMC_EVENT_TABLE_SIZE(atom);
2659 case PMC_CPU_INTEL_CORE:
2660 ev = core_event_table;
2661 count = PMC_EVENT_TABLE_SIZE(core);
2663 case PMC_CPU_INTEL_CORE2:
2664 case PMC_CPU_INTEL_CORE2EXTREME:
2665 ev = core2_event_table;
2666 count = PMC_EVENT_TABLE_SIZE(core2);
2668 case PMC_CPU_INTEL_COREI7:
2669 ev = corei7_event_table;
2670 count = PMC_EVENT_TABLE_SIZE(corei7);
2672 case PMC_CPU_INTEL_IVYBRIDGE:
2673 ev = ivybridge_event_table;
2674 count = PMC_EVENT_TABLE_SIZE(ivybridge);
2676 case PMC_CPU_INTEL_SANDYBRIDGE:
2677 ev = sandybridge_event_table;
2678 count = PMC_EVENT_TABLE_SIZE(sandybridge);
2680 case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
2681 ev = sandybridge_xeon_event_table;
2682 count = PMC_EVENT_TABLE_SIZE(sandybridge_xeon);
2684 case PMC_CPU_INTEL_WESTMERE:
2685 ev = westmere_event_table;
2686 count = PMC_EVENT_TABLE_SIZE(westmere);
2691 ev = ucf_event_table;
2692 count = PMC_EVENT_TABLE_SIZE(ucf);
2696 * Return the most appropriate set of event name
2697 * spellings for the current CPU.
2699 switch (cpu_info.pm_cputype) {
2701 case PMC_CPU_INTEL_COREI7:
2702 ev = corei7uc_event_table;
2703 count = PMC_EVENT_TABLE_SIZE(corei7uc);
2705 case PMC_CPU_INTEL_SANDYBRIDGE:
2706 ev = sandybridgeuc_event_table;
2707 count = PMC_EVENT_TABLE_SIZE(sandybridgeuc);
2709 case PMC_CPU_INTEL_WESTMERE:
2710 ev = westmereuc_event_table;
2711 count = PMC_EVENT_TABLE_SIZE(westmereuc);
2716 ev = tsc_event_table;
2717 count = PMC_EVENT_TABLE_SIZE(tsc);
2720 ev = k7_event_table;
2721 count = PMC_EVENT_TABLE_SIZE(k7);
2724 ev = k8_event_table;
2725 count = PMC_EVENT_TABLE_SIZE(k8);
2728 ev = p4_event_table;
2729 count = PMC_EVENT_TABLE_SIZE(p4);
2732 ev = p5_event_table;
2733 count = PMC_EVENT_TABLE_SIZE(p5);
2736 ev = p6_event_table;
2737 count = PMC_EVENT_TABLE_SIZE(p6);
2739 case PMC_CLASS_XSCALE:
2740 ev = xscale_event_table;
2741 count = PMC_EVENT_TABLE_SIZE(xscale);
2743 case PMC_CLASS_MIPS24K:
2744 ev = mips24k_event_table;
2745 count = PMC_EVENT_TABLE_SIZE(mips24k);
2747 case PMC_CLASS_PPC7450:
2748 ev = ppc7450_event_table;
2749 count = PMC_EVENT_TABLE_SIZE(ppc7450);
2751 case PMC_CLASS_SOFT:
2752 ev = soft_event_table;
2753 count = soft_event_info.pm_nevent;
2760 if ((names = malloc(count * sizeof(const char *))) == NULL)
2763 *eventnames = names;
2766 for (;count--; ev++, names++)
2767 *names = ev->pm_ev_name;
2773 pmc_flush_logfile(void)
2775 return (PMC_CALL(FLUSHLOG,0));
2779 pmc_close_logfile(void)
2781 return (PMC_CALL(CLOSELOG,0));
2785 pmc_get_driver_stats(struct pmc_driverstats *ds)
2787 struct pmc_op_getdriverstats gms;
2789 if (PMC_CALL(GETDRIVERSTATS, &gms) < 0)
2792 /* copy out fields in the current userland<->library interface */
2793 ds->pm_intr_ignored = gms.pm_intr_ignored;
2794 ds->pm_intr_processed = gms.pm_intr_processed;
2795 ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
2796 ds->pm_syscalls = gms.pm_syscalls;
2797 ds->pm_syscall_errors = gms.pm_syscall_errors;
2798 ds->pm_buffer_requests = gms.pm_buffer_requests;
2799 ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
2800 ds->pm_log_sweeps = gms.pm_log_sweeps;
2805 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
2807 struct pmc_op_getmsr gm;
2810 if (PMC_CALL(PMCGETMSR, &gm) < 0)
2819 int error, pmc_mod_id;
2821 uint32_t abi_version;
2822 struct module_stat pmc_modstat;
2823 struct pmc_op_getcpuinfo op_cpu_info;
2824 #if defined(__amd64__) || defined(__i386__)
2825 int cpu_has_iaf_counters;
2829 if (pmc_syscall != -1) /* already inited */
2832 /* retrieve the system call number from the KLD */
2833 if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
2836 pmc_modstat.version = sizeof(struct module_stat);
2837 if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
2840 pmc_syscall = pmc_modstat.data.intval;
2842 /* check the kernel module's ABI against our compiled-in version */
2843 abi_version = PMC_VERSION;
2844 if (PMC_CALL(GETMODULEVERSION, &abi_version) < 0)
2845 return (pmc_syscall = -1);
2847 /* ignore patch & minor numbers for the comparision */
2848 if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
2849 errno = EPROGMISMATCH;
2850 return (pmc_syscall = -1);
2853 if (PMC_CALL(GETCPUINFO, &op_cpu_info) < 0)
2854 return (pmc_syscall = -1);
2856 cpu_info.pm_cputype = op_cpu_info.pm_cputype;
2857 cpu_info.pm_ncpu = op_cpu_info.pm_ncpu;
2858 cpu_info.pm_npmc = op_cpu_info.pm_npmc;
2859 cpu_info.pm_nclass = op_cpu_info.pm_nclass;
2860 for (n = 0; n < cpu_info.pm_nclass; n++)
2861 cpu_info.pm_classes[n] = op_cpu_info.pm_classes[n];
2863 pmc_class_table = malloc(PMC_CLASS_TABLE_SIZE *
2864 sizeof(struct pmc_class_descr *));
2866 if (pmc_class_table == NULL)
2869 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++)
2870 pmc_class_table[n] = NULL;
2873 * Get soft events list.
2875 soft_event_info.pm_class = PMC_CLASS_SOFT;
2876 if (PMC_CALL(GETDYNEVENTINFO, &soft_event_info) < 0)
2877 return (pmc_syscall = -1);
2879 /* Map soft events to static list. */
2880 for (n = 0; n < soft_event_info.pm_nevent; n++) {
2881 soft_event_table[n].pm_ev_name =
2882 soft_event_info.pm_events[n].pm_ev_name;
2883 soft_event_table[n].pm_ev_code =
2884 soft_event_info.pm_events[n].pm_ev_code;
2886 soft_class_table_descr.pm_evc_event_table_size = \
2887 soft_event_info.pm_nevent;
2888 soft_class_table_descr.pm_evc_event_table = \
2892 * Fill in the class table.
2896 /* Fill soft events information. */
2897 pmc_class_table[n++] = &soft_class_table_descr;
2898 #if defined(__amd64__) || defined(__i386__)
2899 if (cpu_info.pm_cputype != PMC_CPU_GENERIC)
2900 pmc_class_table[n++] = &tsc_class_table_descr;
2903 * Check if this CPU has fixed function counters.
2905 cpu_has_iaf_counters = 0;
2906 for (t = 0; t < cpu_info.pm_nclass; t++)
2907 if (cpu_info.pm_classes[t].pm_class == PMC_CLASS_IAF &&
2908 cpu_info.pm_classes[t].pm_num > 0)
2909 cpu_has_iaf_counters = 1;
2912 #define PMC_MDEP_INIT(C) do { \
2913 pmc_mdep_event_aliases = C##_aliases; \
2914 pmc_mdep_class_list = C##_pmc_classes; \
2915 pmc_mdep_class_list_size = \
2916 PMC_TABLE_SIZE(C##_pmc_classes); \
2919 #define PMC_MDEP_INIT_INTEL_V2(C) do { \
2921 pmc_class_table[n++] = &iaf_class_table_descr; \
2922 if (!cpu_has_iaf_counters) \
2923 pmc_mdep_event_aliases = \
2924 C##_aliases_without_iaf; \
2925 pmc_class_table[n] = &C##_class_table_descr; \
2928 /* Configure the event name parser. */
2929 switch (cpu_info.pm_cputype) {
2930 #if defined(__i386__)
2931 case PMC_CPU_AMD_K7:
2933 pmc_class_table[n] = &k7_class_table_descr;
2935 case PMC_CPU_INTEL_P5:
2937 pmc_class_table[n] = &p5_class_table_descr;
2939 case PMC_CPU_INTEL_P6: /* P6 ... Pentium M CPUs have */
2940 case PMC_CPU_INTEL_PII: /* similar PMCs. */
2941 case PMC_CPU_INTEL_PIII:
2942 case PMC_CPU_INTEL_PM:
2944 pmc_class_table[n] = &p6_class_table_descr;
2947 #if defined(__amd64__) || defined(__i386__)
2948 case PMC_CPU_AMD_K8:
2950 pmc_class_table[n] = &k8_class_table_descr;
2952 case PMC_CPU_INTEL_ATOM:
2953 PMC_MDEP_INIT_INTEL_V2(atom);
2955 case PMC_CPU_INTEL_CORE:
2956 PMC_MDEP_INIT(core);
2957 pmc_class_table[n] = &core_class_table_descr;
2959 case PMC_CPU_INTEL_CORE2:
2960 case PMC_CPU_INTEL_CORE2EXTREME:
2961 PMC_MDEP_INIT_INTEL_V2(core2);
2963 case PMC_CPU_INTEL_COREI7:
2964 pmc_class_table[n++] = &ucf_class_table_descr;
2965 pmc_class_table[n++] = &corei7uc_class_table_descr;
2966 PMC_MDEP_INIT_INTEL_V2(corei7);
2968 case PMC_CPU_INTEL_IVYBRIDGE:
2969 PMC_MDEP_INIT_INTEL_V2(ivybridge);
2971 case PMC_CPU_INTEL_SANDYBRIDGE:
2972 pmc_class_table[n++] = &ucf_class_table_descr;
2973 pmc_class_table[n++] = &sandybridgeuc_class_table_descr;
2974 PMC_MDEP_INIT_INTEL_V2(sandybridge);
2976 case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
2977 PMC_MDEP_INIT_INTEL_V2(sandybridge_xeon);
2979 case PMC_CPU_INTEL_WESTMERE:
2980 pmc_class_table[n++] = &ucf_class_table_descr;
2981 pmc_class_table[n++] = &westmereuc_class_table_descr;
2982 PMC_MDEP_INIT_INTEL_V2(westmere);
2984 case PMC_CPU_INTEL_PIV:
2986 pmc_class_table[n] = &p4_class_table_descr;
2989 case PMC_CPU_GENERIC:
2990 PMC_MDEP_INIT(generic);
2992 #if defined(__XSCALE__)
2993 case PMC_CPU_INTEL_XSCALE:
2994 PMC_MDEP_INIT(xscale);
2995 pmc_class_table[n] = &xscale_class_table_descr;
2998 #if defined(__mips__)
2999 case PMC_CPU_MIPS_24K:
3000 PMC_MDEP_INIT(mips24k);
3001 pmc_class_table[n] = &mips24k_class_table_descr;
3003 #endif /* __mips__ */
3004 #if defined(__powerpc__)
3005 case PMC_CPU_PPC_7450:
3006 PMC_MDEP_INIT(ppc7450);
3007 pmc_class_table[n] = &ppc7450_class_table_descr;
3012 * Some kind of CPU this version of the library knows nothing
3013 * about. This shouldn't happen since the abi version check
3014 * should have caught this.
3017 return (pmc_syscall = -1);
3024 pmc_name_of_capability(enum pmc_caps cap)
3029 * 'cap' should have a single bit set and should be in
3032 if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
3033 cap > PMC_CAP_LAST) {
3039 return (pmc_capability_names[i - 1]);
3043 pmc_name_of_class(enum pmc_class pc)
3045 if ((int) pc >= PMC_CLASS_FIRST &&
3046 pc <= PMC_CLASS_LAST)
3047 return (pmc_class_names[pc]);
3054 pmc_name_of_cputype(enum pmc_cputype cp)
3058 for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
3059 if (cp == pmc_cputype_names[n].pm_cputype)
3060 return (pmc_cputype_names[n].pm_name);
3067 pmc_name_of_disposition(enum pmc_disp pd)
3069 if ((int) pd >= PMC_DISP_FIRST &&
3070 pd <= PMC_DISP_LAST)
3071 return (pmc_disposition_names[pd]);
3078 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
3080 const struct pmc_event_descr *ev, *evfence;
3082 ev = evfence = NULL;
3083 if (pe >= PMC_EV_IAF_FIRST && pe <= PMC_EV_IAF_LAST) {
3084 ev = iaf_event_table;
3085 evfence = iaf_event_table + PMC_EVENT_TABLE_SIZE(iaf);
3086 } else if (pe >= PMC_EV_IAP_FIRST && pe <= PMC_EV_IAP_LAST) {
3088 case PMC_CPU_INTEL_ATOM:
3089 ev = atom_event_table;
3090 evfence = atom_event_table + PMC_EVENT_TABLE_SIZE(atom);
3092 case PMC_CPU_INTEL_CORE:
3093 ev = core_event_table;
3094 evfence = core_event_table + PMC_EVENT_TABLE_SIZE(core);
3096 case PMC_CPU_INTEL_CORE2:
3097 case PMC_CPU_INTEL_CORE2EXTREME:
3098 ev = core2_event_table;
3099 evfence = core2_event_table + PMC_EVENT_TABLE_SIZE(core2);
3101 case PMC_CPU_INTEL_COREI7:
3102 ev = corei7_event_table;
3103 evfence = corei7_event_table + PMC_EVENT_TABLE_SIZE(corei7);
3105 case PMC_CPU_INTEL_IVYBRIDGE:
3106 ev = ivybridge_event_table;
3107 evfence = ivybridge_event_table + PMC_EVENT_TABLE_SIZE(ivybridge);
3109 case PMC_CPU_INTEL_SANDYBRIDGE:
3110 ev = sandybridge_event_table;
3111 evfence = sandybridge_event_table + PMC_EVENT_TABLE_SIZE(sandybridge);
3113 case PMC_CPU_INTEL_SANDYBRIDGE_XEON:
3114 ev = sandybridge_xeon_event_table;
3115 evfence = sandybridge_xeon_event_table + PMC_EVENT_TABLE_SIZE(sandybridge_xeon);
3117 case PMC_CPU_INTEL_WESTMERE:
3118 ev = westmere_event_table;
3119 evfence = westmere_event_table + PMC_EVENT_TABLE_SIZE(westmere);
3121 default: /* Unknown CPU type. */
3124 } else if (pe >= PMC_EV_UCF_FIRST && pe <= PMC_EV_UCF_LAST) {
3125 ev = ucf_event_table;
3126 evfence = ucf_event_table + PMC_EVENT_TABLE_SIZE(ucf);
3127 } else if (pe >= PMC_EV_UCP_FIRST && pe <= PMC_EV_UCP_LAST) {
3129 case PMC_CPU_INTEL_COREI7:
3130 ev = corei7uc_event_table;
3131 evfence = corei7uc_event_table + PMC_EVENT_TABLE_SIZE(corei7uc);
3133 case PMC_CPU_INTEL_SANDYBRIDGE:
3134 ev = sandybridgeuc_event_table;
3135 evfence = sandybridgeuc_event_table + PMC_EVENT_TABLE_SIZE(sandybridgeuc);
3137 case PMC_CPU_INTEL_WESTMERE:
3138 ev = westmereuc_event_table;
3139 evfence = westmereuc_event_table + PMC_EVENT_TABLE_SIZE(westmereuc);
3141 default: /* Unknown CPU type. */
3144 } else if (pe >= PMC_EV_K7_FIRST && pe <= PMC_EV_K7_LAST) {
3145 ev = k7_event_table;
3146 evfence = k7_event_table + PMC_EVENT_TABLE_SIZE(k7);
3147 } else if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
3148 ev = k8_event_table;
3149 evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
3150 } else if (pe >= PMC_EV_P4_FIRST && pe <= PMC_EV_P4_LAST) {
3151 ev = p4_event_table;
3152 evfence = p4_event_table + PMC_EVENT_TABLE_SIZE(p4);
3153 } else if (pe >= PMC_EV_P5_FIRST && pe <= PMC_EV_P5_LAST) {
3154 ev = p5_event_table;
3155 evfence = p5_event_table + PMC_EVENT_TABLE_SIZE(p5);
3156 } else if (pe >= PMC_EV_P6_FIRST && pe <= PMC_EV_P6_LAST) {
3157 ev = p6_event_table;
3158 evfence = p6_event_table + PMC_EVENT_TABLE_SIZE(p6);
3159 } else if (pe >= PMC_EV_XSCALE_FIRST && pe <= PMC_EV_XSCALE_LAST) {
3160 ev = xscale_event_table;
3161 evfence = xscale_event_table + PMC_EVENT_TABLE_SIZE(xscale);
3162 } else if (pe >= PMC_EV_MIPS24K_FIRST && pe <= PMC_EV_MIPS24K_LAST) {
3163 ev = mips24k_event_table;
3164 evfence = mips24k_event_table + PMC_EVENT_TABLE_SIZE(mips24k);
3165 } else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
3166 ev = ppc7450_event_table;
3167 evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450);
3168 } else if (pe == PMC_EV_TSC_TSC) {
3169 ev = tsc_event_table;
3170 evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
3171 } else if (pe >= PMC_EV_SOFT_FIRST && pe <= PMC_EV_SOFT_LAST) {
3172 ev = soft_event_table;
3173 evfence = soft_event_table + soft_event_info.pm_nevent;
3176 for (; ev != evfence; ev++)
3177 if (pe == ev->pm_ev_code)
3178 return (ev->pm_ev_name);
3184 pmc_name_of_event(enum pmc_event pe)
3188 if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
3196 pmc_name_of_mode(enum pmc_mode pm)
3198 if ((int) pm >= PMC_MODE_FIRST &&
3199 pm <= PMC_MODE_LAST)
3200 return (pmc_mode_names[pm]);
3207 pmc_name_of_state(enum pmc_state ps)
3209 if ((int) ps >= PMC_STATE_FIRST &&
3210 ps <= PMC_STATE_LAST)
3211 return (pmc_state_names[ps]);
3220 if (pmc_syscall == -1) {
3225 return (cpu_info.pm_ncpu);
3231 if (pmc_syscall == -1) {
3236 if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
3241 return (cpu_info.pm_npmc);
3245 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
3248 struct pmc_op_getpmcinfo *pmci;
3250 if ((npmc = pmc_npmc(cpu)) < 0)
3253 nbytes = sizeof(struct pmc_op_getpmcinfo) +
3254 npmc * sizeof(struct pmc_info);
3256 if ((pmci = calloc(1, nbytes)) == NULL)
3261 if (PMC_CALL(GETPMCINFO, pmci) < 0) {
3266 /* kernel<->library, library<->userland interfaces are identical */
3267 *ppmci = (struct pmc_pmcinfo *) pmci;
3272 pmc_read(pmc_id_t pmc, pmc_value_t *value)
3274 struct pmc_op_pmcrw pmc_read_op;
3276 pmc_read_op.pm_pmcid = pmc;
3277 pmc_read_op.pm_flags = PMC_F_OLDVALUE;
3278 pmc_read_op.pm_value = -1;
3280 if (PMC_CALL(PMCRW, &pmc_read_op) < 0)
3283 *value = pmc_read_op.pm_value;
3288 pmc_release(pmc_id_t pmc)
3290 struct pmc_op_simple pmc_release_args;
3292 pmc_release_args.pm_pmcid = pmc;
3293 return (PMC_CALL(PMCRELEASE, &pmc_release_args));
3297 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
3299 struct pmc_op_pmcrw pmc_rw_op;
3301 pmc_rw_op.pm_pmcid = pmc;
3302 pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
3303 pmc_rw_op.pm_value = newvalue;
3305 if (PMC_CALL(PMCRW, &pmc_rw_op) < 0)
3308 *oldvaluep = pmc_rw_op.pm_value;
3313 pmc_set(pmc_id_t pmc, pmc_value_t value)
3315 struct pmc_op_pmcsetcount sc;
3318 sc.pm_count = value;
3320 if (PMC_CALL(PMCSETCOUNT, &sc) < 0)
3326 pmc_start(pmc_id_t pmc)
3328 struct pmc_op_simple pmc_start_args;
3330 pmc_start_args.pm_pmcid = pmc;
3331 return (PMC_CALL(PMCSTART, &pmc_start_args));
3335 pmc_stop(pmc_id_t pmc)
3337 struct pmc_op_simple pmc_stop_args;
3339 pmc_stop_args.pm_pmcid = pmc;
3340 return (PMC_CALL(PMCSTOP, &pmc_stop_args));
3344 pmc_width(pmc_id_t pmcid, uint32_t *width)
3349 cl = PMC_ID_TO_CLASS(pmcid);
3350 for (i = 0; i < cpu_info.pm_nclass; i++)
3351 if (cpu_info.pm_classes[i].pm_class == cl) {
3352 *width = cpu_info.pm_classes[i].pm_width;
3360 pmc_write(pmc_id_t pmc, pmc_value_t value)
3362 struct pmc_op_pmcrw pmc_write_op;
3364 pmc_write_op.pm_pmcid = pmc;
3365 pmc_write_op.pm_flags = PMC_F_NEWVALUE;
3366 pmc_write_op.pm_value = value;
3367 return (PMC_CALL(PMCRW, &pmc_write_op));
3371 pmc_writelog(uint32_t userdata)
3373 struct pmc_op_writelog wl;
3375 wl.pm_userdata = userdata;
3376 return (PMC_CALL(WRITELOG, &wl));