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);
82 static int mips24k_allocate_pmc(enum pmc_event _pe, char* ctrspec,
83 struct pmc_op_pmcallocate *_pmc_config);
86 #if defined(__powerpc__)
87 static int ppc7450_allocate_pmc(enum pmc_event _pe, char* ctrspec,
88 struct pmc_op_pmcallocate *_pmc_config);
89 #endif /* __powerpc__ */
91 #define PMC_CALL(cmd, params) \
92 syscall(pmc_syscall, PMC_OP_##cmd, (params))
95 * Event aliases provide a way for the user to ask for generic events
96 * like "cache-misses", or "instructions-retired". These aliases are
97 * mapped to the appropriate canonical event descriptions using a
100 struct pmc_event_alias {
101 const char *pm_alias;
105 static const struct pmc_event_alias *pmc_mdep_event_aliases;
108 * The pmc_event_descr structure maps symbolic names known to the user
109 * to integer codes used by the PMC KLD.
111 struct pmc_event_descr {
112 const char *pm_ev_name;
113 enum pmc_event pm_ev_code;
117 * The pmc_class_descr structure maps class name prefixes for
118 * event names to event tables and other PMC class data.
120 struct pmc_class_descr {
121 const char *pm_evc_name;
122 size_t pm_evc_name_size;
123 enum pmc_class pm_evc_class;
124 const struct pmc_event_descr *pm_evc_event_table;
125 size_t pm_evc_event_table_size;
126 int (*pm_evc_allocate_pmc)(enum pmc_event _pe,
127 char *_ctrspec, struct pmc_op_pmcallocate *_pa);
130 #define PMC_TABLE_SIZE(N) (sizeof(N)/sizeof(N[0]))
131 #define PMC_EVENT_TABLE_SIZE(N) PMC_TABLE_SIZE(N##_event_table)
134 #define __PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
137 * PMC_CLASSDEP_TABLE(NAME, CLASS)
139 * Define a table mapping event names and aliases to HWPMC event IDs.
141 #define PMC_CLASSDEP_TABLE(N, C) \
142 static const struct pmc_event_descr N##_event_table[] = \
147 PMC_CLASSDEP_TABLE(iaf, IAF);
148 PMC_CLASSDEP_TABLE(k7, K7);
149 PMC_CLASSDEP_TABLE(k8, K8);
150 PMC_CLASSDEP_TABLE(p4, P4);
151 PMC_CLASSDEP_TABLE(p5, P5);
152 PMC_CLASSDEP_TABLE(p6, P6);
153 PMC_CLASSDEP_TABLE(xscale, XSCALE);
154 PMC_CLASSDEP_TABLE(mips24k, MIPS24K);
155 PMC_CLASSDEP_TABLE(ucf, UCF);
156 PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
158 #undef __PMC_EV_ALIAS
159 #define __PMC_EV_ALIAS(N,CODE) { N, PMC_EV_##CODE },
161 static const struct pmc_event_descr atom_event_table[] =
163 __PMC_EV_ALIAS_ATOM()
166 static const struct pmc_event_descr core_event_table[] =
168 __PMC_EV_ALIAS_CORE()
172 static const struct pmc_event_descr core2_event_table[] =
174 __PMC_EV_ALIAS_CORE2()
177 static const struct pmc_event_descr corei7_event_table[] =
179 __PMC_EV_ALIAS_COREI7()
182 static const struct pmc_event_descr westmere_event_table[] =
184 __PMC_EV_ALIAS_WESTMERE()
187 static const struct pmc_event_descr corei7uc_event_table[] =
189 __PMC_EV_ALIAS_COREI7UC()
192 static const struct pmc_event_descr westmereuc_event_table[] =
194 __PMC_EV_ALIAS_WESTMEREUC()
198 * PMC_MDEP_TABLE(NAME, PRIMARYCLASS, ADDITIONAL_CLASSES...)
200 * Map a CPU to the PMC classes it supports.
202 #define PMC_MDEP_TABLE(N,C,...) \
203 static const enum pmc_class N##_pmc_classes[] = { \
204 PMC_CLASS_##C, __VA_ARGS__ \
207 PMC_MDEP_TABLE(atom, IAP, PMC_CLASS_IAF, PMC_CLASS_TSC);
208 PMC_MDEP_TABLE(core, IAP, PMC_CLASS_TSC);
209 PMC_MDEP_TABLE(core2, IAP, PMC_CLASS_IAF, PMC_CLASS_TSC);
210 PMC_MDEP_TABLE(corei7, IAP, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
211 PMC_MDEP_TABLE(westmere, IAP, PMC_CLASS_IAF, PMC_CLASS_TSC, PMC_CLASS_UCF, PMC_CLASS_UCP);
212 PMC_MDEP_TABLE(k7, K7, PMC_CLASS_TSC);
213 PMC_MDEP_TABLE(k8, K8, PMC_CLASS_TSC);
214 PMC_MDEP_TABLE(p4, P4, PMC_CLASS_TSC);
215 PMC_MDEP_TABLE(p5, P5, PMC_CLASS_TSC);
216 PMC_MDEP_TABLE(p6, P6, PMC_CLASS_TSC);
217 PMC_MDEP_TABLE(xscale, XSCALE, PMC_CLASS_XSCALE);
218 PMC_MDEP_TABLE(mips24k, MIPS24K, PMC_CLASS_MIPS24K);
219 PMC_MDEP_TABLE(ppc7450, PPC7450, PMC_CLASS_PPC7450);
221 static const struct pmc_event_descr tsc_event_table[] =
226 #undef PMC_CLASS_TABLE_DESC
227 #define PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR) \
228 static const struct pmc_class_descr NAME##_class_table_descr = \
230 .pm_evc_name = #CLASS "-", \
231 .pm_evc_name_size = sizeof(#CLASS "-") - 1, \
232 .pm_evc_class = PMC_CLASS_##CLASS , \
233 .pm_evc_event_table = EVENTS##_event_table , \
234 .pm_evc_event_table_size = \
235 PMC_EVENT_TABLE_SIZE(EVENTS), \
236 .pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc \
239 #if defined(__i386__) || defined(__amd64__)
240 PMC_CLASS_TABLE_DESC(iaf, IAF, iaf, iaf);
241 PMC_CLASS_TABLE_DESC(atom, IAP, atom, iap);
242 PMC_CLASS_TABLE_DESC(core, IAP, core, iap);
243 PMC_CLASS_TABLE_DESC(core2, IAP, core2, iap);
244 PMC_CLASS_TABLE_DESC(corei7, IAP, corei7, iap);
245 PMC_CLASS_TABLE_DESC(westmere, IAP, westmere, iap);
246 PMC_CLASS_TABLE_DESC(ucf, UCF, ucf, ucf);
247 PMC_CLASS_TABLE_DESC(corei7uc, UCP, corei7uc, ucp);
248 PMC_CLASS_TABLE_DESC(westmereuc, UCP, westmereuc, ucp);
250 #if defined(__i386__)
251 PMC_CLASS_TABLE_DESC(k7, K7, k7, k7);
253 #if defined(__i386__) || defined(__amd64__)
254 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
255 PMC_CLASS_TABLE_DESC(p4, P4, p4, p4);
257 #if defined(__i386__)
258 PMC_CLASS_TABLE_DESC(p5, P5, p5, p5);
259 PMC_CLASS_TABLE_DESC(p6, P6, p6, p6);
261 #if defined(__i386__) || defined(__amd64__)
262 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
264 #if defined(__XSCALE__)
265 PMC_CLASS_TABLE_DESC(xscale, XSCALE, xscale, xscale);
268 #if defined(__mips__)
269 PMC_CLASS_TABLE_DESC(mips24k, MIPS24K, mips24k, mips24k);
270 #endif /* __mips__ */
272 #if defined(__powerpc__)
273 PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, ppc7450);
276 #undef PMC_CLASS_TABLE_DESC
278 static const struct pmc_class_descr **pmc_class_table;
279 #define PMC_CLASS_TABLE_SIZE cpu_info.pm_nclass
281 static const enum pmc_class *pmc_mdep_class_list;
282 static size_t pmc_mdep_class_list_size;
285 * Mapping tables, mapping enumeration values to human readable
289 static const char * pmc_capability_names[] = {
291 #define __PMC_CAP(N,V,D) #N ,
295 static const char * pmc_class_names[] = {
297 #define __PMC_CLASS(C) #C ,
301 struct pmc_cputype_map {
302 enum pmc_cputype pm_cputype;
306 static const struct pmc_cputype_map pmc_cputype_names[] = {
308 #define __PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
312 static const char * pmc_disposition_names[] = {
314 #define __PMC_DISP(D) #D ,
318 static const char * pmc_mode_names[] = {
320 #define __PMC_MODE(M,N) #M ,
324 static const char * pmc_state_names[] = {
326 #define __PMC_STATE(S) #S ,
330 static int pmc_syscall = -1; /* filled in by pmc_init() */
332 static struct pmc_cpuinfo cpu_info; /* filled in by pmc_init() */
334 /* Event masks for events */
337 const uint32_t pm_value;
339 #define PMCMASK(N,V) { .pm_name = #N, .pm_value = (V) }
340 #define NULLMASK { .pm_name = NULL }
342 #if defined(__amd64__) || defined(__i386__)
344 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint32_t *evmask)
346 const struct pmc_masks *pm;
350 if (pmask == NULL) /* no mask keywords */
352 q = strchr(p, '='); /* skip '=' */
353 if (*++q == '\0') /* no more data */
355 c = 0; /* count of mask keywords seen */
356 while ((r = strsep(&q, "+")) != NULL) {
357 for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
360 if (pm->pm_name == NULL) /* not found */
362 *evmask |= pm->pm_value;
369 #define KWMATCH(p,kw) (strcasecmp((p), (kw)) == 0)
370 #define KWPREFIXMATCH(p,kw) (strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
371 #define EV_ALIAS(N,S) { .pm_alias = N, .pm_spec = S }
373 #if defined(__i386__)
376 * AMD K7 (Athlon) CPUs.
379 static struct pmc_event_alias k7_aliases[] = {
380 EV_ALIAS("branches", "k7-retired-branches"),
381 EV_ALIAS("branch-mispredicts", "k7-retired-branches-mispredicted"),
382 EV_ALIAS("cycles", "tsc"),
383 EV_ALIAS("dc-misses", "k7-dc-misses"),
384 EV_ALIAS("ic-misses", "k7-ic-misses"),
385 EV_ALIAS("instructions", "k7-retired-instructions"),
386 EV_ALIAS("interrupts", "k7-hardware-interrupts"),
390 #define K7_KW_COUNT "count"
391 #define K7_KW_EDGE "edge"
392 #define K7_KW_INV "inv"
393 #define K7_KW_OS "os"
394 #define K7_KW_UNITMASK "unitmask"
395 #define K7_KW_USR "usr"
398 k7_allocate_pmc(enum pmc_event pe, char *ctrspec,
399 struct pmc_op_pmcallocate *pmc_config)
403 uint32_t count, unitmask;
405 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
406 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
408 if (pe == PMC_EV_K7_DC_REFILLS_FROM_L2 ||
409 pe == PMC_EV_K7_DC_REFILLS_FROM_SYSTEM ||
410 pe == PMC_EV_K7_DC_WRITEBACKS) {
412 unitmask = AMD_PMC_UNITMASK_MOESI;
414 unitmask = has_unitmask = 0;
416 while ((p = strsep(&ctrspec, ",")) != NULL) {
417 if (KWPREFIXMATCH(p, K7_KW_COUNT "=")) {
419 if (*++q == '\0') /* skip '=' */
422 count = strtol(q, &e, 0);
423 if (e == q || *e != '\0')
426 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
427 pmc_config->pm_md.pm_amd.pm_amd_config |=
428 AMD_PMC_TO_COUNTER(count);
430 } else if (KWMATCH(p, K7_KW_EDGE)) {
431 pmc_config->pm_caps |= PMC_CAP_EDGE;
432 } else if (KWMATCH(p, K7_KW_INV)) {
433 pmc_config->pm_caps |= PMC_CAP_INVERT;
434 } else if (KWMATCH(p, K7_KW_OS)) {
435 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
436 } else if (KWPREFIXMATCH(p, K7_KW_UNITMASK "=")) {
437 if (has_unitmask == 0)
441 if (*++q == '\0') /* skip '=' */
444 while ((c = tolower(*q++)) != 0)
446 unitmask |= AMD_PMC_UNITMASK_M;
448 unitmask |= AMD_PMC_UNITMASK_O;
450 unitmask |= AMD_PMC_UNITMASK_E;
452 unitmask |= AMD_PMC_UNITMASK_S;
454 unitmask |= AMD_PMC_UNITMASK_I;
463 } else if (KWMATCH(p, K7_KW_USR)) {
464 pmc_config->pm_caps |= PMC_CAP_USER;
470 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
471 pmc_config->pm_md.pm_amd.pm_amd_config |=
472 AMD_PMC_TO_UNITMASK(unitmask);
481 #if defined(__amd64__) || defined(__i386__)
484 * Intel Core (Family 6, Model E) PMCs.
487 static struct pmc_event_alias core_aliases[] = {
488 EV_ALIAS("branches", "iap-br-instr-ret"),
489 EV_ALIAS("branch-mispredicts", "iap-br-mispred-ret"),
490 EV_ALIAS("cycles", "tsc-tsc"),
491 EV_ALIAS("ic-misses", "iap-icache-misses"),
492 EV_ALIAS("instructions", "iap-instr-ret"),
493 EV_ALIAS("interrupts", "iap-core-hw-int-rx"),
494 EV_ALIAS("unhalted-cycles", "iap-unhalted-core-cycles"),
499 * Intel Core2 (Family 6, Model F), Core2Extreme (Family 6, Model 17H)
500 * and Atom (Family 6, model 1CH) PMCs.
502 * We map aliases to events on the fixed-function counters if these
503 * are present. Note that not all CPUs in this family contain fixed-function
507 static struct pmc_event_alias core2_aliases[] = {
508 EV_ALIAS("branches", "iap-br-inst-retired.any"),
509 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
510 EV_ALIAS("cycles", "tsc-tsc"),
511 EV_ALIAS("ic-misses", "iap-l1i-misses"),
512 EV_ALIAS("instructions", "iaf-instr-retired.any"),
513 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
514 EV_ALIAS("unhalted-cycles", "iaf-cpu-clk-unhalted.core"),
518 static struct pmc_event_alias core2_aliases_without_iaf[] = {
519 EV_ALIAS("branches", "iap-br-inst-retired.any"),
520 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
521 EV_ALIAS("cycles", "tsc-tsc"),
522 EV_ALIAS("ic-misses", "iap-l1i-misses"),
523 EV_ALIAS("instructions", "iap-inst-retired.any_p"),
524 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
525 EV_ALIAS("unhalted-cycles", "iap-cpu-clk-unhalted.core_p"),
529 #define atom_aliases core2_aliases
530 #define atom_aliases_without_iaf core2_aliases_without_iaf
531 #define corei7_aliases core2_aliases
532 #define corei7_aliases_without_iaf core2_aliases_without_iaf
533 #define westmere_aliases core2_aliases
534 #define westmere_aliases_without_iaf core2_aliases_without_iaf
536 #define IAF_KW_OS "os"
537 #define IAF_KW_USR "usr"
538 #define IAF_KW_ANYTHREAD "anythread"
541 * Parse an event specifier for Intel fixed function counters.
544 iaf_allocate_pmc(enum pmc_event pe, char *ctrspec,
545 struct pmc_op_pmcallocate *pmc_config)
551 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
552 pmc_config->pm_md.pm_iaf.pm_iaf_flags = 0;
554 while ((p = strsep(&ctrspec, ",")) != NULL) {
555 if (KWMATCH(p, IAF_KW_OS))
556 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
557 else if (KWMATCH(p, IAF_KW_USR))
558 pmc_config->pm_caps |= PMC_CAP_USER;
559 else if (KWMATCH(p, IAF_KW_ANYTHREAD))
560 pmc_config->pm_md.pm_iaf.pm_iaf_flags |= IAF_ANY;
569 * Core/Core2 support.
572 #define IAP_KW_AGENT "agent"
573 #define IAP_KW_ANYTHREAD "anythread"
574 #define IAP_KW_CACHESTATE "cachestate"
575 #define IAP_KW_CMASK "cmask"
576 #define IAP_KW_CORE "core"
577 #define IAP_KW_EDGE "edge"
578 #define IAP_KW_INV "inv"
579 #define IAP_KW_OS "os"
580 #define IAP_KW_PREFETCH "prefetch"
581 #define IAP_KW_SNOOPRESPONSE "snoopresponse"
582 #define IAP_KW_SNOOPTYPE "snooptype"
583 #define IAP_KW_TRANSITION "trans"
584 #define IAP_KW_USR "usr"
585 #define IAP_KW_RSP "rsp"
587 static struct pmc_masks iap_core_mask[] = {
588 PMCMASK(all, (0x3 << 14)),
589 PMCMASK(this, (0x1 << 14)),
593 static struct pmc_masks iap_agent_mask[] = {
595 PMCMASK(any, (0x1 << 13)),
599 static struct pmc_masks iap_prefetch_mask[] = {
600 PMCMASK(both, (0x3 << 12)),
601 PMCMASK(only, (0x1 << 12)),
606 static struct pmc_masks iap_cachestate_mask[] = {
607 PMCMASK(i, (1 << 8)),
608 PMCMASK(s, (1 << 9)),
609 PMCMASK(e, (1 << 10)),
610 PMCMASK(m, (1 << 11)),
614 static struct pmc_masks iap_snoopresponse_mask[] = {
615 PMCMASK(clean, (1 << 8)),
616 PMCMASK(hit, (1 << 9)),
617 PMCMASK(hitm, (1 << 11)),
621 static struct pmc_masks iap_snooptype_mask[] = {
622 PMCMASK(cmp2s, (1 << 8)),
623 PMCMASK(cmp2i, (1 << 9)),
627 static struct pmc_masks iap_transition_mask[] = {
629 PMCMASK(frequency, 0x10),
633 static struct pmc_masks iap_rsp_mask[] = {
634 PMCMASK(DMND_DATA_RD, (1 << 0)),
635 PMCMASK(DMND_RFO, (1 << 1)),
636 PMCMASK(DMND_IFETCH, (1 << 2)),
637 PMCMASK(WB, (1 << 3)),
638 PMCMASK(PF_DATA_RD, (1 << 4)),
639 PMCMASK(PF_RFO, (1 << 5)),
640 PMCMASK(PF_IFETCH, (1 << 6)),
641 PMCMASK(OTHER, (1 << 7)),
642 PMCMASK(UNCORE_HIT, (1 << 8)),
643 PMCMASK(OTHER_CORE_HIT_SNP, (1 << 9)),
644 PMCMASK(OTHER_CORE_HITM, (1 << 10)),
645 PMCMASK(REMOTE_CACHE_FWD, (1 << 12)),
646 PMCMASK(REMOTE_DRAM, (1 << 13)),
647 PMCMASK(LOCAL_DRAM, (1 << 14)),
648 PMCMASK(NON_DRAM, (1 << 15)),
653 iap_allocate_pmc(enum pmc_event pe, char *ctrspec,
654 struct pmc_op_pmcallocate *pmc_config)
657 uint32_t cachestate, evmask, rsp;
660 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE |
662 pmc_config->pm_md.pm_iap.pm_iap_config = 0;
664 cachestate = evmask = rsp = 0;
666 /* Parse additional modifiers if present */
667 while ((p = strsep(&ctrspec, ",")) != NULL) {
670 if (KWPREFIXMATCH(p, IAP_KW_CMASK "=")) {
672 if (*++q == '\0') /* skip '=' */
674 count = strtol(q, &e, 0);
675 if (e == q || *e != '\0')
677 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
678 pmc_config->pm_md.pm_iap.pm_iap_config |=
680 } else if (KWMATCH(p, IAP_KW_EDGE)) {
681 pmc_config->pm_caps |= PMC_CAP_EDGE;
682 } else if (KWMATCH(p, IAP_KW_INV)) {
683 pmc_config->pm_caps |= PMC_CAP_INVERT;
684 } else if (KWMATCH(p, IAP_KW_OS)) {
685 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
686 } else if (KWMATCH(p, IAP_KW_USR)) {
687 pmc_config->pm_caps |= PMC_CAP_USER;
688 } else if (KWMATCH(p, IAP_KW_ANYTHREAD)) {
689 pmc_config->pm_md.pm_iap.pm_iap_config |= IAP_ANY;
690 } else if (KWPREFIXMATCH(p, IAP_KW_CORE "=")) {
691 n = pmc_parse_mask(iap_core_mask, p, &evmask);
694 } else if (KWPREFIXMATCH(p, IAP_KW_AGENT "=")) {
695 n = pmc_parse_mask(iap_agent_mask, p, &evmask);
698 } else if (KWPREFIXMATCH(p, IAP_KW_PREFETCH "=")) {
699 n = pmc_parse_mask(iap_prefetch_mask, p, &evmask);
702 } else if (KWPREFIXMATCH(p, IAP_KW_CACHESTATE "=")) {
703 n = pmc_parse_mask(iap_cachestate_mask, p, &cachestate);
704 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_CORE &&
705 KWPREFIXMATCH(p, IAP_KW_TRANSITION "=")) {
706 n = pmc_parse_mask(iap_transition_mask, p, &evmask);
709 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_ATOM ||
710 cpu_info.pm_cputype == PMC_CPU_INTEL_CORE2 ||
711 cpu_info.pm_cputype == PMC_CPU_INTEL_CORE2EXTREME) {
712 if (KWPREFIXMATCH(p, IAP_KW_SNOOPRESPONSE "=")) {
713 n = pmc_parse_mask(iap_snoopresponse_mask, p,
715 } else if (KWPREFIXMATCH(p, IAP_KW_SNOOPTYPE "=")) {
716 n = pmc_parse_mask(iap_snooptype_mask, p,
720 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_COREI7 ||
721 cpu_info.pm_cputype == PMC_CPU_INTEL_WESTMERE) {
722 if (KWPREFIXMATCH(p, IAP_KW_RSP "=")) {
723 n = pmc_parse_mask(iap_rsp_mask, p, &rsp);
729 if (n < 0) /* Parsing failed. */
733 pmc_config->pm_md.pm_iap.pm_iap_config |= evmask;
736 * If the event requires a 'cachestate' qualifier but was not
737 * specified by the user, use a sensible default.
740 case PMC_EV_IAP_EVENT_28H: /* Core, Core2, Atom */
741 case PMC_EV_IAP_EVENT_29H: /* Core, Core2, Atom */
742 case PMC_EV_IAP_EVENT_2AH: /* Core, Core2, Atom */
743 case PMC_EV_IAP_EVENT_2BH: /* Atom, Core2 */
744 case PMC_EV_IAP_EVENT_2EH: /* Core, Core2, Atom */
745 case PMC_EV_IAP_EVENT_30H: /* Core, Core2, Atom */
746 case PMC_EV_IAP_EVENT_32H: /* Core */
747 case PMC_EV_IAP_EVENT_40H: /* Core */
748 case PMC_EV_IAP_EVENT_41H: /* Core */
749 case PMC_EV_IAP_EVENT_42H: /* Core, Core2, Atom */
751 cachestate = (0xF << 8);
753 case PMC_EV_IAP_EVENT_77H: /* Atom */
754 /* IAP_EVENT_77H only accepts a cachestate qualifier on the
757 if(cpu_info.pm_cputype == PMC_CPU_INTEL_ATOM && cachestate == 0)
758 cachestate = (0xF << 8);
764 pmc_config->pm_md.pm_iap.pm_iap_config |= cachestate;
765 pmc_config->pm_md.pm_iap.pm_iap_rsp = rsp;
775 ucf_allocate_pmc(enum pmc_event pe, char *ctrspec,
776 struct pmc_op_pmcallocate *pmc_config)
781 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
782 pmc_config->pm_md.pm_ucf.pm_ucf_flags = 0;
787 #define UCP_KW_CMASK "cmask"
788 #define UCP_KW_EDGE "edge"
789 #define UCP_KW_INV "inv"
792 ucp_allocate_pmc(enum pmc_event pe, char *ctrspec,
793 struct pmc_op_pmcallocate *pmc_config)
800 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE |
802 pmc_config->pm_md.pm_ucp.pm_ucp_config = 0;
804 /* Parse additional modifiers if present */
805 while ((p = strsep(&ctrspec, ",")) != NULL) {
808 if (KWPREFIXMATCH(p, UCP_KW_CMASK "=")) {
810 if (*++q == '\0') /* skip '=' */
812 count = strtol(q, &e, 0);
813 if (e == q || *e != '\0')
815 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
816 pmc_config->pm_md.pm_ucp.pm_ucp_config |=
818 } else if (KWMATCH(p, UCP_KW_EDGE)) {
819 pmc_config->pm_caps |= PMC_CAP_EDGE;
820 } else if (KWMATCH(p, UCP_KW_INV)) {
821 pmc_config->pm_caps |= PMC_CAP_INVERT;
825 if (n < 0) /* Parsing failed. */
835 * These are very similar to AMD K7 PMCs, but support more kinds of
839 static struct pmc_event_alias k8_aliases[] = {
840 EV_ALIAS("branches", "k8-fr-retired-taken-branches"),
841 EV_ALIAS("branch-mispredicts",
842 "k8-fr-retired-taken-branches-mispredicted"),
843 EV_ALIAS("cycles", "tsc"),
844 EV_ALIAS("dc-misses", "k8-dc-miss"),
845 EV_ALIAS("ic-misses", "k8-ic-miss"),
846 EV_ALIAS("instructions", "k8-fr-retired-x86-instructions"),
847 EV_ALIAS("interrupts", "k8-fr-taken-hardware-interrupts"),
848 EV_ALIAS("unhalted-cycles", "k8-bu-cpu-clk-unhalted"),
852 #define __K8MASK(N,V) PMCMASK(N,(1 << (V)))
858 /* fp dispatched fpu ops */
859 static const struct pmc_masks k8_mask_fdfo[] = {
860 __K8MASK(add-pipe-excluding-junk-ops, 0),
861 __K8MASK(multiply-pipe-excluding-junk-ops, 1),
862 __K8MASK(store-pipe-excluding-junk-ops, 2),
863 __K8MASK(add-pipe-junk-ops, 3),
864 __K8MASK(multiply-pipe-junk-ops, 4),
865 __K8MASK(store-pipe-junk-ops, 5),
869 /* ls segment register loads */
870 static const struct pmc_masks k8_mask_lsrl[] = {
881 /* ls locked operation */
882 static const struct pmc_masks k8_mask_llo[] = {
883 __K8MASK(locked-instructions, 0),
884 __K8MASK(cycles-in-request, 1),
885 __K8MASK(cycles-to-complete, 2),
889 /* dc refill from {l2,system} and dc copyback */
890 static const struct pmc_masks k8_mask_dc[] = {
891 __K8MASK(invalid, 0),
893 __K8MASK(exclusive, 2),
895 __K8MASK(modified, 4),
899 /* dc one bit ecc error */
900 static const struct pmc_masks k8_mask_dobee[] = {
901 __K8MASK(scrubber, 0),
902 __K8MASK(piggyback, 1),
906 /* dc dispatched prefetch instructions */
907 static const struct pmc_masks k8_mask_ddpi[] = {
914 /* dc dcache accesses by locks */
915 static const struct pmc_masks k8_mask_dabl[] = {
916 __K8MASK(accesses, 0),
921 /* bu internal l2 request */
922 static const struct pmc_masks k8_mask_bilr[] = {
923 __K8MASK(ic-fill, 0),
924 __K8MASK(dc-fill, 1),
925 __K8MASK(tlb-reload, 2),
926 __K8MASK(tag-snoop, 3),
927 __K8MASK(cancelled, 4),
931 /* bu fill request l2 miss */
932 static const struct pmc_masks k8_mask_bfrlm[] = {
933 __K8MASK(ic-fill, 0),
934 __K8MASK(dc-fill, 1),
935 __K8MASK(tlb-reload, 2),
939 /* bu fill into l2 */
940 static const struct pmc_masks k8_mask_bfil[] = {
941 __K8MASK(dirty-l2-victim, 0),
942 __K8MASK(victim-from-l2, 1),
946 /* fr retired fpu instructions */
947 static const struct pmc_masks k8_mask_frfi[] = {
949 __K8MASK(mmx-3dnow, 1),
950 __K8MASK(packed-sse-sse2, 2),
951 __K8MASK(scalar-sse-sse2, 3),
955 /* fr retired fastpath double op instructions */
956 static const struct pmc_masks k8_mask_frfdoi[] = {
957 __K8MASK(low-op-pos-0, 0),
958 __K8MASK(low-op-pos-1, 1),
959 __K8MASK(low-op-pos-2, 2),
963 /* fr fpu exceptions */
964 static const struct pmc_masks k8_mask_ffe[] = {
965 __K8MASK(x87-reclass-microfaults, 0),
966 __K8MASK(sse-retype-microfaults, 1),
967 __K8MASK(sse-reclass-microfaults, 2),
968 __K8MASK(sse-and-x87-microtraps, 3),
972 /* nb memory controller page access event */
973 static const struct pmc_masks k8_mask_nmcpae[] = {
974 __K8MASK(page-hit, 0),
975 __K8MASK(page-miss, 1),
976 __K8MASK(page-conflict, 2),
980 /* nb memory controller turnaround */
981 static const struct pmc_masks k8_mask_nmct[] = {
982 __K8MASK(dimm-turnaround, 0),
983 __K8MASK(read-to-write-turnaround, 1),
984 __K8MASK(write-to-read-turnaround, 2),
988 /* nb memory controller bypass saturation */
989 static const struct pmc_masks k8_mask_nmcbs[] = {
990 __K8MASK(memory-controller-hi-pri-bypass, 0),
991 __K8MASK(memory-controller-lo-pri-bypass, 1),
992 __K8MASK(dram-controller-interface-bypass, 2),
993 __K8MASK(dram-controller-queue-bypass, 3),
997 /* nb sized commands */
998 static const struct pmc_masks k8_mask_nsc[] = {
999 __K8MASK(nonpostwrszbyte, 0),
1000 __K8MASK(nonpostwrszdword, 1),
1001 __K8MASK(postwrszbyte, 2),
1002 __K8MASK(postwrszdword, 3),
1003 __K8MASK(rdszbyte, 4),
1004 __K8MASK(rdszdword, 5),
1005 __K8MASK(rdmodwr, 6),
1009 /* nb probe result */
1010 static const struct pmc_masks k8_mask_npr[] = {
1011 __K8MASK(probe-miss, 0),
1012 __K8MASK(probe-hit, 1),
1013 __K8MASK(probe-hit-dirty-no-memory-cancel, 2),
1014 __K8MASK(probe-hit-dirty-with-memory-cancel, 3),
1018 /* nb hypertransport bus bandwidth */
1019 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
1020 __K8MASK(command, 0),
1022 __K8MASK(buffer-release, 2),
1029 #define K8_KW_COUNT "count"
1030 #define K8_KW_EDGE "edge"
1031 #define K8_KW_INV "inv"
1032 #define K8_KW_MASK "mask"
1033 #define K8_KW_OS "os"
1034 #define K8_KW_USR "usr"
1037 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
1038 struct pmc_op_pmcallocate *pmc_config)
1042 uint32_t count, evmask;
1043 const struct pmc_masks *pm, *pmask;
1045 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1046 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
1051 #define __K8SETMASK(M) pmask = k8_mask_##M
1053 /* setup parsing tables */
1055 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
1058 case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
1061 case PMC_EV_K8_LS_LOCKED_OPERATION:
1064 case PMC_EV_K8_DC_REFILL_FROM_L2:
1065 case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
1066 case PMC_EV_K8_DC_COPYBACK:
1069 case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
1072 case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
1075 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
1078 case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
1081 case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
1084 case PMC_EV_K8_BU_FILL_INTO_L2:
1087 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
1090 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
1091 __K8SETMASK(frfdoi);
1093 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
1096 case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
1097 __K8SETMASK(nmcpae);
1099 case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
1102 case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
1105 case PMC_EV_K8_NB_SIZED_COMMANDS:
1108 case PMC_EV_K8_NB_PROBE_RESULT:
1111 case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
1112 case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
1113 case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
1118 break; /* no options defined */
1121 while ((p = strsep(&ctrspec, ",")) != NULL) {
1122 if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
1124 if (*++q == '\0') /* skip '=' */
1127 count = strtol(q, &e, 0);
1128 if (e == q || *e != '\0')
1131 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
1132 pmc_config->pm_md.pm_amd.pm_amd_config |=
1133 AMD_PMC_TO_COUNTER(count);
1135 } else if (KWMATCH(p, K8_KW_EDGE)) {
1136 pmc_config->pm_caps |= PMC_CAP_EDGE;
1137 } else if (KWMATCH(p, K8_KW_INV)) {
1138 pmc_config->pm_caps |= PMC_CAP_INVERT;
1139 } else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
1140 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
1142 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1143 } else if (KWMATCH(p, K8_KW_OS)) {
1144 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
1145 } else if (KWMATCH(p, K8_KW_USR)) {
1146 pmc_config->pm_caps |= PMC_CAP_USER;
1151 /* other post processing */
1153 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
1154 case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
1155 case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
1156 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
1157 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
1158 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
1159 /* XXX only available in rev B and later */
1161 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
1162 /* XXX only available in rev C and later */
1164 case PMC_EV_K8_LS_LOCKED_OPERATION:
1165 /* XXX CPU Rev A,B evmask is to be zero */
1166 if (evmask & (evmask - 1)) /* > 1 bit set */
1169 evmask = 0x01; /* Rev C and later: #instrs */
1170 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1174 if (evmask == 0 && pmask != NULL) {
1175 for (pm = pmask; pm->pm_name; pm++)
1176 evmask |= pm->pm_value;
1177 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1181 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
1182 pmc_config->pm_md.pm_amd.pm_amd_config =
1183 AMD_PMC_TO_UNITMASK(evmask);
1190 #if defined(__amd64__) || defined(__i386__)
1196 static struct pmc_event_alias p4_aliases[] = {
1197 EV_ALIAS("branches", "p4-branch-retired,mask=mmtp+mmtm"),
1198 EV_ALIAS("branch-mispredicts", "p4-mispred-branch-retired"),
1199 EV_ALIAS("cycles", "tsc"),
1200 EV_ALIAS("instructions",
1201 "p4-instr-retired,mask=nbogusntag+nbogustag"),
1202 EV_ALIAS("unhalted-cycles", "p4-global-power-events"),
1203 EV_ALIAS(NULL, NULL)
1206 #define P4_KW_ACTIVE "active"
1207 #define P4_KW_ACTIVE_ANY "any"
1208 #define P4_KW_ACTIVE_BOTH "both"
1209 #define P4_KW_ACTIVE_NONE "none"
1210 #define P4_KW_ACTIVE_SINGLE "single"
1211 #define P4_KW_BUSREQTYPE "busreqtype"
1212 #define P4_KW_CASCADE "cascade"
1213 #define P4_KW_EDGE "edge"
1214 #define P4_KW_INV "complement"
1215 #define P4_KW_OS "os"
1216 #define P4_KW_MASK "mask"
1217 #define P4_KW_PRECISE "precise"
1218 #define P4_KW_TAG "tag"
1219 #define P4_KW_THRESHOLD "threshold"
1220 #define P4_KW_USR "usr"
1222 #define __P4MASK(N,V) PMCMASK(N, (1 << (V)))
1224 static const struct pmc_masks p4_mask_tcdm[] = { /* tc deliver mode */
1236 static const struct pmc_masks p4_mask_bfr[] = { /* bpu fetch request */
1237 __P4MASK(tcmiss, 0),
1241 static const struct pmc_masks p4_mask_ir[] = { /* itlb reference */
1244 __P4MASK(hit-uc, 2),
1248 static const struct pmc_masks p4_mask_memcan[] = { /* memory cancel */
1249 __P4MASK(st-rb-full, 2),
1250 __P4MASK(64k-conf, 3),
1254 static const struct pmc_masks p4_mask_memcomp[] = { /* memory complete */
1260 static const struct pmc_masks p4_mask_lpr[] = { /* load port replay */
1261 __P4MASK(split-ld, 1),
1265 static const struct pmc_masks p4_mask_spr[] = { /* store port replay */
1266 __P4MASK(split-st, 1),
1270 static const struct pmc_masks p4_mask_mlr[] = { /* mob load replay */
1271 __P4MASK(no-sta, 1),
1272 __P4MASK(no-std, 3),
1273 __P4MASK(partial-data, 4),
1274 __P4MASK(unalgn-addr, 5),
1278 static const struct pmc_masks p4_mask_pwt[] = { /* page walk type */
1279 __P4MASK(dtmiss, 0),
1280 __P4MASK(itmiss, 1),
1284 static const struct pmc_masks p4_mask_bcr[] = { /* bsq cache reference */
1285 __P4MASK(rd-2ndl-hits, 0),
1286 __P4MASK(rd-2ndl-hite, 1),
1287 __P4MASK(rd-2ndl-hitm, 2),
1288 __P4MASK(rd-3rdl-hits, 3),
1289 __P4MASK(rd-3rdl-hite, 4),
1290 __P4MASK(rd-3rdl-hitm, 5),
1291 __P4MASK(rd-2ndl-miss, 8),
1292 __P4MASK(rd-3rdl-miss, 9),
1293 __P4MASK(wr-2ndl-miss, 10),
1297 static const struct pmc_masks p4_mask_ia[] = { /* ioq allocation */
1298 __P4MASK(all-read, 5),
1299 __P4MASK(all-write, 6),
1300 __P4MASK(mem-uc, 7),
1301 __P4MASK(mem-wc, 8),
1302 __P4MASK(mem-wt, 9),
1303 __P4MASK(mem-wp, 10),
1304 __P4MASK(mem-wb, 11),
1306 __P4MASK(other, 14),
1307 __P4MASK(prefetch, 15),
1311 static const struct pmc_masks p4_mask_iae[] = { /* ioq active entries */
1312 __P4MASK(all-read, 5),
1313 __P4MASK(all-write, 6),
1314 __P4MASK(mem-uc, 7),
1315 __P4MASK(mem-wc, 8),
1316 __P4MASK(mem-wt, 9),
1317 __P4MASK(mem-wp, 10),
1318 __P4MASK(mem-wb, 11),
1320 __P4MASK(other, 14),
1321 __P4MASK(prefetch, 15),
1325 static const struct pmc_masks p4_mask_fda[] = { /* fsb data activity */
1326 __P4MASK(drdy-drv, 0),
1327 __P4MASK(drdy-own, 1),
1328 __P4MASK(drdy-other, 2),
1329 __P4MASK(dbsy-drv, 3),
1330 __P4MASK(dbsy-own, 4),
1331 __P4MASK(dbsy-other, 5),
1335 static const struct pmc_masks p4_mask_ba[] = { /* bsq allocation */
1336 __P4MASK(req-type0, 0),
1337 __P4MASK(req-type1, 1),
1338 __P4MASK(req-len0, 2),
1339 __P4MASK(req-len1, 3),
1340 __P4MASK(req-io-type, 5),
1341 __P4MASK(req-lock-type, 6),
1342 __P4MASK(req-cache-type, 7),
1343 __P4MASK(req-split-type, 8),
1344 __P4MASK(req-dem-type, 9),
1345 __P4MASK(req-ord-type, 10),
1346 __P4MASK(mem-type0, 11),
1347 __P4MASK(mem-type1, 12),
1348 __P4MASK(mem-type2, 13),
1352 static const struct pmc_masks p4_mask_sia[] = { /* sse input assist */
1357 static const struct pmc_masks p4_mask_psu[] = { /* packed sp uop */
1362 static const struct pmc_masks p4_mask_pdu[] = { /* packed dp uop */
1367 static const struct pmc_masks p4_mask_ssu[] = { /* scalar sp uop */
1372 static const struct pmc_masks p4_mask_sdu[] = { /* scalar dp uop */
1377 static const struct pmc_masks p4_mask_64bmu[] = { /* 64 bit mmx uop */
1382 static const struct pmc_masks p4_mask_128bmu[] = { /* 128 bit mmx uop */
1387 static const struct pmc_masks p4_mask_xfu[] = { /* X87 fp uop */
1392 static const struct pmc_masks p4_mask_xsmu[] = { /* x87 simd moves uop */
1398 static const struct pmc_masks p4_mask_gpe[] = { /* global power events */
1399 __P4MASK(running, 0),
1403 static const struct pmc_masks p4_mask_tmx[] = { /* TC ms xfer */
1408 static const struct pmc_masks p4_mask_uqw[] = { /* uop queue writes */
1409 __P4MASK(from-tc-build, 0),
1410 __P4MASK(from-tc-deliver, 1),
1411 __P4MASK(from-rom, 2),
1415 static const struct pmc_masks p4_mask_rmbt[] = {
1416 /* retired mispred branch type */
1417 __P4MASK(conditional, 1),
1419 __P4MASK(return, 3),
1420 __P4MASK(indirect, 4),
1424 static const struct pmc_masks p4_mask_rbt[] = { /* retired branch type */
1425 __P4MASK(conditional, 1),
1427 __P4MASK(retired, 3),
1428 __P4MASK(indirect, 4),
1432 static const struct pmc_masks p4_mask_rs[] = { /* resource stall */
1433 __P4MASK(sbfull, 5),
1437 static const struct pmc_masks p4_mask_wb[] = { /* WC buffer */
1438 __P4MASK(wcb-evicts, 0),
1439 __P4MASK(wcb-full-evict, 1),
1443 static const struct pmc_masks p4_mask_fee[] = { /* front end event */
1444 __P4MASK(nbogus, 0),
1449 static const struct pmc_masks p4_mask_ee[] = { /* execution event */
1450 __P4MASK(nbogus0, 0),
1451 __P4MASK(nbogus1, 1),
1452 __P4MASK(nbogus2, 2),
1453 __P4MASK(nbogus3, 3),
1454 __P4MASK(bogus0, 4),
1455 __P4MASK(bogus1, 5),
1456 __P4MASK(bogus2, 6),
1457 __P4MASK(bogus3, 7),
1461 static const struct pmc_masks p4_mask_re[] = { /* replay event */
1462 __P4MASK(nbogus, 0),
1467 static const struct pmc_masks p4_mask_insret[] = { /* instr retired */
1468 __P4MASK(nbogusntag, 0),
1469 __P4MASK(nbogustag, 1),
1470 __P4MASK(bogusntag, 2),
1471 __P4MASK(bogustag, 3),
1475 static const struct pmc_masks p4_mask_ur[] = { /* uops retired */
1476 __P4MASK(nbogus, 0),
1481 static const struct pmc_masks p4_mask_ut[] = { /* uop type */
1482 __P4MASK(tagloads, 1),
1483 __P4MASK(tagstores, 2),
1487 static const struct pmc_masks p4_mask_br[] = { /* branch retired */
1495 static const struct pmc_masks p4_mask_mbr[] = { /* mispred branch retired */
1496 __P4MASK(nbogus, 0),
1500 static const struct pmc_masks p4_mask_xa[] = { /* x87 assist */
1509 static const struct pmc_masks p4_mask_machclr[] = { /* machine clear */
1511 __P4MASK(moclear, 2),
1512 __P4MASK(smclear, 3),
1516 /* P4 event parser */
1518 p4_allocate_pmc(enum pmc_event pe, char *ctrspec,
1519 struct pmc_op_pmcallocate *pmc_config)
1523 int count, has_tag, has_busreqtype, n;
1524 uint32_t evmask, cccractivemask;
1525 const struct pmc_masks *pm, *pmask;
1527 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1528 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig =
1529 pmc_config->pm_md.pm_p4.pm_p4_escrconfig = 0;
1533 cccractivemask = 0x3;
1534 has_tag = has_busreqtype = 0;
1536 #define __P4SETMASK(M) do { \
1537 pmask = p4_mask_##M; \
1541 case PMC_EV_P4_TC_DELIVER_MODE:
1544 case PMC_EV_P4_BPU_FETCH_REQUEST:
1547 case PMC_EV_P4_ITLB_REFERENCE:
1550 case PMC_EV_P4_MEMORY_CANCEL:
1551 __P4SETMASK(memcan);
1553 case PMC_EV_P4_MEMORY_COMPLETE:
1554 __P4SETMASK(memcomp);
1556 case PMC_EV_P4_LOAD_PORT_REPLAY:
1559 case PMC_EV_P4_STORE_PORT_REPLAY:
1562 case PMC_EV_P4_MOB_LOAD_REPLAY:
1565 case PMC_EV_P4_PAGE_WALK_TYPE:
1568 case PMC_EV_P4_BSQ_CACHE_REFERENCE:
1571 case PMC_EV_P4_IOQ_ALLOCATION:
1575 case PMC_EV_P4_IOQ_ACTIVE_ENTRIES:
1579 case PMC_EV_P4_FSB_DATA_ACTIVITY:
1582 case PMC_EV_P4_BSQ_ALLOCATION:
1585 case PMC_EV_P4_SSE_INPUT_ASSIST:
1588 case PMC_EV_P4_PACKED_SP_UOP:
1591 case PMC_EV_P4_PACKED_DP_UOP:
1594 case PMC_EV_P4_SCALAR_SP_UOP:
1597 case PMC_EV_P4_SCALAR_DP_UOP:
1600 case PMC_EV_P4_64BIT_MMX_UOP:
1603 case PMC_EV_P4_128BIT_MMX_UOP:
1604 __P4SETMASK(128bmu);
1606 case PMC_EV_P4_X87_FP_UOP:
1609 case PMC_EV_P4_X87_SIMD_MOVES_UOP:
1612 case PMC_EV_P4_GLOBAL_POWER_EVENTS:
1615 case PMC_EV_P4_TC_MS_XFER:
1618 case PMC_EV_P4_UOP_QUEUE_WRITES:
1621 case PMC_EV_P4_RETIRED_MISPRED_BRANCH_TYPE:
1624 case PMC_EV_P4_RETIRED_BRANCH_TYPE:
1627 case PMC_EV_P4_RESOURCE_STALL:
1630 case PMC_EV_P4_WC_BUFFER:
1633 case PMC_EV_P4_BSQ_ACTIVE_ENTRIES:
1634 case PMC_EV_P4_B2B_CYCLES:
1636 case PMC_EV_P4_SNOOP:
1637 case PMC_EV_P4_RESPONSE:
1639 case PMC_EV_P4_FRONT_END_EVENT:
1642 case PMC_EV_P4_EXECUTION_EVENT:
1645 case PMC_EV_P4_REPLAY_EVENT:
1648 case PMC_EV_P4_INSTR_RETIRED:
1649 __P4SETMASK(insret);
1651 case PMC_EV_P4_UOPS_RETIRED:
1654 case PMC_EV_P4_UOP_TYPE:
1657 case PMC_EV_P4_BRANCH_RETIRED:
1660 case PMC_EV_P4_MISPRED_BRANCH_RETIRED:
1663 case PMC_EV_P4_X87_ASSIST:
1666 case PMC_EV_P4_MACHINE_CLEAR:
1667 __P4SETMASK(machclr);
1673 /* process additional flags */
1674 while ((p = strsep(&ctrspec, ",")) != NULL) {
1675 if (KWPREFIXMATCH(p, P4_KW_ACTIVE)) {
1677 if (*++q == '\0') /* skip '=' */
1680 if (strcasecmp(q, P4_KW_ACTIVE_NONE) == 0)
1681 cccractivemask = 0x0;
1682 else if (strcasecmp(q, P4_KW_ACTIVE_SINGLE) == 0)
1683 cccractivemask = 0x1;
1684 else if (strcasecmp(q, P4_KW_ACTIVE_BOTH) == 0)
1685 cccractivemask = 0x2;
1686 else if (strcasecmp(q, P4_KW_ACTIVE_ANY) == 0)
1687 cccractivemask = 0x3;
1691 } else if (KWPREFIXMATCH(p, P4_KW_BUSREQTYPE)) {
1692 if (has_busreqtype == 0)
1696 if (*++q == '\0') /* skip '=' */
1699 count = strtol(q, &e, 0);
1700 if (e == q || *e != '\0')
1702 evmask = (evmask & ~0x1F) | (count & 0x1F);
1703 } else if (KWMATCH(p, P4_KW_CASCADE))
1704 pmc_config->pm_caps |= PMC_CAP_CASCADE;
1705 else if (KWMATCH(p, P4_KW_EDGE))
1706 pmc_config->pm_caps |= PMC_CAP_EDGE;
1707 else if (KWMATCH(p, P4_KW_INV))
1708 pmc_config->pm_caps |= PMC_CAP_INVERT;
1709 else if (KWPREFIXMATCH(p, P4_KW_MASK "=")) {
1710 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
1712 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1713 } else if (KWMATCH(p, P4_KW_OS))
1714 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
1715 else if (KWMATCH(p, P4_KW_PRECISE))
1716 pmc_config->pm_caps |= PMC_CAP_PRECISE;
1717 else if (KWPREFIXMATCH(p, P4_KW_TAG "=")) {
1722 if (*++q == '\0') /* skip '=' */
1725 count = strtol(q, &e, 0);
1726 if (e == q || *e != '\0')
1729 pmc_config->pm_caps |= PMC_CAP_TAGGING;
1730 pmc_config->pm_md.pm_p4.pm_p4_escrconfig |=
1731 P4_ESCR_TO_TAG_VALUE(count);
1732 } else if (KWPREFIXMATCH(p, P4_KW_THRESHOLD "=")) {
1734 if (*++q == '\0') /* skip '=' */
1737 count = strtol(q, &e, 0);
1738 if (e == q || *e != '\0')
1741 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
1742 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig &=
1743 ~P4_CCCR_THRESHOLD_MASK;
1744 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig |=
1745 P4_CCCR_TO_THRESHOLD(count);
1746 } else if (KWMATCH(p, P4_KW_USR))
1747 pmc_config->pm_caps |= PMC_CAP_USER;
1752 /* other post processing */
1753 if (pe == PMC_EV_P4_IOQ_ALLOCATION ||
1754 pe == PMC_EV_P4_FSB_DATA_ACTIVITY ||
1755 pe == PMC_EV_P4_BSQ_ALLOCATION)
1756 pmc_config->pm_caps |= PMC_CAP_EDGE;
1758 /* fill in thread activity mask */
1759 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig |=
1760 P4_CCCR_TO_ACTIVE_THREAD(cccractivemask);
1763 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1766 case PMC_EV_P4_FSB_DATA_ACTIVITY:
1767 if ((evmask & 0x06) == 0x06 ||
1768 (evmask & 0x18) == 0x18)
1769 return (-1); /* can't have own+other bits together */
1770 if (evmask == 0) /* default:drdy-{drv,own}+dbsy{drv,own} */
1773 case PMC_EV_P4_MACHINE_CLEAR:
1774 /* only one bit is allowed to be set */
1775 if ((evmask & (evmask - 1)) != 0)
1778 evmask = 0x1; /* 'CLEAR' */
1779 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1783 if (evmask == 0 && pmask) {
1784 for (pm = pmask; pm->pm_name; pm++)
1785 evmask |= pm->pm_value;
1786 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1790 pmc_config->pm_md.pm_p4.pm_p4_escrconfig =
1791 P4_ESCR_TO_EVENT_MASK(evmask);
1798 #if defined(__i386__)
1801 * Pentium style PMCs
1804 static struct pmc_event_alias p5_aliases[] = {
1805 EV_ALIAS("branches", "p5-taken-branches"),
1806 EV_ALIAS("cycles", "tsc"),
1807 EV_ALIAS("dc-misses", "p5-data-read-miss-or-write-miss"),
1808 EV_ALIAS("ic-misses", "p5-code-cache-miss"),
1809 EV_ALIAS("instructions", "p5-instructions-executed"),
1810 EV_ALIAS("interrupts", "p5-hardware-interrupts"),
1811 EV_ALIAS("unhalted-cycles",
1812 "p5-number-of-cycles-not-in-halt-state"),
1813 EV_ALIAS(NULL, NULL)
1817 p5_allocate_pmc(enum pmc_event pe, char *ctrspec,
1818 struct pmc_op_pmcallocate *pmc_config)
1820 return (-1 || pe || ctrspec || pmc_config); /* shut up gcc */
1824 * Pentium Pro style PMCs. These PMCs are found in Pentium II, Pentium III,
1825 * and Pentium M CPUs.
1828 static struct pmc_event_alias p6_aliases[] = {
1829 EV_ALIAS("branches", "p6-br-inst-retired"),
1830 EV_ALIAS("branch-mispredicts", "p6-br-miss-pred-retired"),
1831 EV_ALIAS("cycles", "tsc"),
1832 EV_ALIAS("dc-misses", "p6-dcu-lines-in"),
1833 EV_ALIAS("ic-misses", "p6-ifu-fetch-miss"),
1834 EV_ALIAS("instructions", "p6-inst-retired"),
1835 EV_ALIAS("interrupts", "p6-hw-int-rx"),
1836 EV_ALIAS("unhalted-cycles", "p6-cpu-clk-unhalted"),
1837 EV_ALIAS(NULL, NULL)
1840 #define P6_KW_CMASK "cmask"
1841 #define P6_KW_EDGE "edge"
1842 #define P6_KW_INV "inv"
1843 #define P6_KW_OS "os"
1844 #define P6_KW_UMASK "umask"
1845 #define P6_KW_USR "usr"
1847 static struct pmc_masks p6_mask_mesi[] = {
1855 static struct pmc_masks p6_mask_mesihw[] = {
1860 PMCMASK(nonhw, 0x00),
1862 PMCMASK(both, 0x30),
1866 static struct pmc_masks p6_mask_hw[] = {
1867 PMCMASK(nonhw, 0x00),
1869 PMCMASK(both, 0x30),
1873 static struct pmc_masks p6_mask_any[] = {
1874 PMCMASK(self, 0x00),
1879 static struct pmc_masks p6_mask_ekp[] = {
1887 static struct pmc_masks p6_mask_pps[] = {
1888 PMCMASK(packed-and-scalar, 0x00),
1889 PMCMASK(scalar, 0x01),
1893 static struct pmc_masks p6_mask_mite[] = {
1894 PMCMASK(packed-multiply, 0x01),
1895 PMCMASK(packed-shift, 0x02),
1896 PMCMASK(pack, 0x04),
1897 PMCMASK(unpack, 0x08),
1898 PMCMASK(packed-logical, 0x10),
1899 PMCMASK(packed-arithmetic, 0x20),
1903 static struct pmc_masks p6_mask_fmt[] = {
1904 PMCMASK(mmxtofp, 0x00),
1905 PMCMASK(fptommx, 0x01),
1909 static struct pmc_masks p6_mask_sr[] = {
1917 static struct pmc_masks p6_mask_eet[] = {
1919 PMCMASK(freq, 0x02),
1923 static struct pmc_masks p6_mask_efur[] = {
1925 PMCMASK(loadop, 0x01),
1926 PMCMASK(stdsta, 0x02),
1930 static struct pmc_masks p6_mask_essir[] = {
1931 PMCMASK(sse-packed-single, 0x00),
1932 PMCMASK(sse-packed-single-scalar-single, 0x01),
1933 PMCMASK(sse2-packed-double, 0x02),
1934 PMCMASK(sse2-scalar-double, 0x03),
1938 static struct pmc_masks p6_mask_esscir[] = {
1939 PMCMASK(sse-packed-single, 0x00),
1940 PMCMASK(sse-scalar-single, 0x01),
1941 PMCMASK(sse2-packed-double, 0x02),
1942 PMCMASK(sse2-scalar-double, 0x03),
1946 /* P6 event parser */
1948 p6_allocate_pmc(enum pmc_event pe, char *ctrspec,
1949 struct pmc_op_pmcallocate *pmc_config)
1954 const struct pmc_masks *pm, *pmask;
1956 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1957 pmc_config->pm_md.pm_ppro.pm_ppro_config = 0;
1961 #define P6MASKSET(M) pmask = p6_mask_ ## M
1964 case PMC_EV_P6_L2_IFETCH: P6MASKSET(mesi); break;
1965 case PMC_EV_P6_L2_LD: P6MASKSET(mesi); break;
1966 case PMC_EV_P6_L2_ST: P6MASKSET(mesi); break;
1967 case PMC_EV_P6_L2_RQSTS: P6MASKSET(mesi); break;
1968 case PMC_EV_P6_BUS_DRDY_CLOCKS:
1969 case PMC_EV_P6_BUS_LOCK_CLOCKS:
1970 case PMC_EV_P6_BUS_TRAN_BRD:
1971 case PMC_EV_P6_BUS_TRAN_RFO:
1972 case PMC_EV_P6_BUS_TRANS_WB:
1973 case PMC_EV_P6_BUS_TRAN_IFETCH:
1974 case PMC_EV_P6_BUS_TRAN_INVAL:
1975 case PMC_EV_P6_BUS_TRAN_PWR:
1976 case PMC_EV_P6_BUS_TRANS_P:
1977 case PMC_EV_P6_BUS_TRANS_IO:
1978 case PMC_EV_P6_BUS_TRAN_DEF:
1979 case PMC_EV_P6_BUS_TRAN_BURST:
1980 case PMC_EV_P6_BUS_TRAN_ANY:
1981 case PMC_EV_P6_BUS_TRAN_MEM:
1982 P6MASKSET(any); break;
1983 case PMC_EV_P6_EMON_KNI_PREF_DISPATCHED:
1984 case PMC_EV_P6_EMON_KNI_PREF_MISS:
1985 P6MASKSET(ekp); break;
1986 case PMC_EV_P6_EMON_KNI_INST_RETIRED:
1987 case PMC_EV_P6_EMON_KNI_COMP_INST_RET:
1988 P6MASKSET(pps); break;
1989 case PMC_EV_P6_MMX_INSTR_TYPE_EXEC:
1990 P6MASKSET(mite); break;
1991 case PMC_EV_P6_FP_MMX_TRANS:
1992 P6MASKSET(fmt); break;
1993 case PMC_EV_P6_SEG_RENAME_STALLS:
1994 case PMC_EV_P6_SEG_REG_RENAMES:
1995 P6MASKSET(sr); break;
1996 case PMC_EV_P6_EMON_EST_TRANS:
1997 P6MASKSET(eet); break;
1998 case PMC_EV_P6_EMON_FUSED_UOPS_RET:
1999 P6MASKSET(efur); break;
2000 case PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED:
2001 P6MASKSET(essir); break;
2002 case PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED:
2003 P6MASKSET(esscir); break;
2009 /* Pentium M PMCs have a few events with different semantics */
2010 if (cpu_info.pm_cputype == PMC_CPU_INTEL_PM) {
2011 if (pe == PMC_EV_P6_L2_LD ||
2012 pe == PMC_EV_P6_L2_LINES_IN ||
2013 pe == PMC_EV_P6_L2_LINES_OUT)
2015 else if (pe == PMC_EV_P6_L2_M_LINES_OUTM)
2019 /* Parse additional modifiers if present */
2020 while ((p = strsep(&ctrspec, ",")) != NULL) {
2021 if (KWPREFIXMATCH(p, P6_KW_CMASK "=")) {
2023 if (*++q == '\0') /* skip '=' */
2025 count = strtol(q, &e, 0);
2026 if (e == q || *e != '\0')
2028 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
2029 pmc_config->pm_md.pm_ppro.pm_ppro_config |=
2030 P6_EVSEL_TO_CMASK(count);
2031 } else if (KWMATCH(p, P6_KW_EDGE)) {
2032 pmc_config->pm_caps |= PMC_CAP_EDGE;
2033 } else if (KWMATCH(p, P6_KW_INV)) {
2034 pmc_config->pm_caps |= PMC_CAP_INVERT;
2035 } else if (KWMATCH(p, P6_KW_OS)) {
2036 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2037 } else if (KWPREFIXMATCH(p, P6_KW_UMASK "=")) {
2039 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
2041 if ((pe == PMC_EV_P6_BUS_DRDY_CLOCKS ||
2042 pe == PMC_EV_P6_BUS_LOCK_CLOCKS ||
2043 pe == PMC_EV_P6_BUS_TRAN_BRD ||
2044 pe == PMC_EV_P6_BUS_TRAN_RFO ||
2045 pe == PMC_EV_P6_BUS_TRAN_IFETCH ||
2046 pe == PMC_EV_P6_BUS_TRAN_INVAL ||
2047 pe == PMC_EV_P6_BUS_TRAN_PWR ||
2048 pe == PMC_EV_P6_BUS_TRAN_DEF ||
2049 pe == PMC_EV_P6_BUS_TRAN_BURST ||
2050 pe == PMC_EV_P6_BUS_TRAN_ANY ||
2051 pe == PMC_EV_P6_BUS_TRAN_MEM ||
2052 pe == PMC_EV_P6_BUS_TRANS_IO ||
2053 pe == PMC_EV_P6_BUS_TRANS_P ||
2054 pe == PMC_EV_P6_BUS_TRANS_WB ||
2055 pe == PMC_EV_P6_EMON_EST_TRANS ||
2056 pe == PMC_EV_P6_EMON_FUSED_UOPS_RET ||
2057 pe == PMC_EV_P6_EMON_KNI_COMP_INST_RET ||
2058 pe == PMC_EV_P6_EMON_KNI_INST_RETIRED ||
2059 pe == PMC_EV_P6_EMON_KNI_PREF_DISPATCHED ||
2060 pe == PMC_EV_P6_EMON_KNI_PREF_MISS ||
2061 pe == PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED ||
2062 pe == PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED ||
2063 pe == PMC_EV_P6_FP_MMX_TRANS)
2064 && (n > 1)) /* Only one mask keyword is allowed. */
2066 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2067 } else if (KWMATCH(p, P6_KW_USR)) {
2068 pmc_config->pm_caps |= PMC_CAP_USER;
2073 /* post processing */
2077 * The following events default to an evmask of 0
2080 /* default => 'self' */
2081 case PMC_EV_P6_BUS_DRDY_CLOCKS:
2082 case PMC_EV_P6_BUS_LOCK_CLOCKS:
2083 case PMC_EV_P6_BUS_TRAN_BRD:
2084 case PMC_EV_P6_BUS_TRAN_RFO:
2085 case PMC_EV_P6_BUS_TRANS_WB:
2086 case PMC_EV_P6_BUS_TRAN_IFETCH:
2087 case PMC_EV_P6_BUS_TRAN_INVAL:
2088 case PMC_EV_P6_BUS_TRAN_PWR:
2089 case PMC_EV_P6_BUS_TRANS_P:
2090 case PMC_EV_P6_BUS_TRANS_IO:
2091 case PMC_EV_P6_BUS_TRAN_DEF:
2092 case PMC_EV_P6_BUS_TRAN_BURST:
2093 case PMC_EV_P6_BUS_TRAN_ANY:
2094 case PMC_EV_P6_BUS_TRAN_MEM:
2096 /* default => 'nta' */
2097 case PMC_EV_P6_EMON_KNI_PREF_DISPATCHED:
2098 case PMC_EV_P6_EMON_KNI_PREF_MISS:
2100 /* default => 'packed and scalar' */
2101 case PMC_EV_P6_EMON_KNI_INST_RETIRED:
2102 case PMC_EV_P6_EMON_KNI_COMP_INST_RET:
2104 /* default => 'mmx to fp transitions' */
2105 case PMC_EV_P6_FP_MMX_TRANS:
2107 /* default => 'SSE Packed Single' */
2108 case PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED:
2109 case PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED:
2111 /* default => 'all fused micro-ops' */
2112 case PMC_EV_P6_EMON_FUSED_UOPS_RET:
2114 /* default => 'all transitions' */
2115 case PMC_EV_P6_EMON_EST_TRANS:
2118 case PMC_EV_P6_MMX_UOPS_EXEC:
2119 evmask = 0x0F; /* only value allowed */
2124 * For all other events, set the default event mask
2125 * to a logical OR of all the allowed event mask bits.
2127 if (evmask == 0 && pmask) {
2128 for (pm = pmask; pm->pm_name; pm++)
2129 evmask |= pm->pm_value;
2130 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
2136 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
2137 pmc_config->pm_md.pm_ppro.pm_ppro_config |=
2138 P6_EVSEL_TO_UMASK(evmask);
2145 #if defined(__i386__) || defined(__amd64__)
2147 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
2148 struct pmc_op_pmcallocate *pmc_config)
2150 if (pe != PMC_EV_TSC_TSC)
2153 /* TSC events must be unqualified. */
2154 if (ctrspec && *ctrspec != '\0')
2157 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
2158 pmc_config->pm_caps |= PMC_CAP_READ;
2164 #if defined(__XSCALE__)
2166 static struct pmc_event_alias xscale_aliases[] = {
2167 EV_ALIAS("branches", "BRANCH_RETIRED"),
2168 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
2169 EV_ALIAS("dc-misses", "DC_MISS"),
2170 EV_ALIAS("ic-misses", "IC_MISS"),
2171 EV_ALIAS("instructions", "INSTR_RETIRED"),
2172 EV_ALIAS(NULL, NULL)
2175 xscale_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2176 struct pmc_op_pmcallocate *pmc_config __unused)
2187 #if defined(__mips__)
2189 static struct pmc_event_alias mips24k_aliases[] = {
2190 EV_ALIAS("instructions", "INSTR_EXECUTED"),
2191 EV_ALIAS("branches", "BRANCH_COMPLETED"),
2192 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
2193 EV_ALIAS(NULL, NULL)
2196 #define MIPS24K_KW_OS "os"
2197 #define MIPS24K_KW_USR "usr"
2198 #define MIPS24K_KW_ANYTHREAD "anythread"
2201 mips24k_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2202 struct pmc_op_pmcallocate *pmc_config __unused)
2208 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2210 while ((p = strsep(&ctrspec, ",")) != NULL) {
2211 if (KWMATCH(p, MIPS24K_KW_OS))
2212 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2213 else if (KWMATCH(p, MIPS24K_KW_USR))
2214 pmc_config->pm_caps |= PMC_CAP_USER;
2215 else if (KWMATCH(p, MIPS24K_KW_ANYTHREAD))
2216 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
2223 #endif /* __mips__ */
2225 #if defined(__powerpc__)
2227 static struct pmc_event_alias ppc7450_aliases[] = {
2228 EV_ALIAS("instructions", "INSTR_COMPLETED"),
2229 EV_ALIAS("branches", "BRANCHES_COMPLETED"),
2230 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCHES"),
2231 EV_ALIAS(NULL, NULL)
2234 #define PPC7450_KW_OS "os"
2235 #define PPC7450_KW_USR "usr"
2236 #define PPC7450_KW_ANYTHREAD "anythread"
2239 ppc7450_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
2240 struct pmc_op_pmcallocate *pmc_config __unused)
2246 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
2248 while ((p = strsep(&ctrspec, ",")) != NULL) {
2249 if (KWMATCH(p, PPC7450_KW_OS))
2250 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
2251 else if (KWMATCH(p, PPC7450_KW_USR))
2252 pmc_config->pm_caps |= PMC_CAP_USER;
2253 else if (KWMATCH(p, PPC7450_KW_ANYTHREAD))
2254 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
2261 #endif /* __powerpc__ */
2265 * Match an event name `name' with its canonical form.
2267 * Matches are case insensitive and spaces, periods, underscores and
2268 * hyphen characters are considered to match each other.
2270 * Returns 1 for a match, 0 otherwise.
2274 pmc_match_event_name(const char *name, const char *canonicalname)
2277 const unsigned char *c, *n;
2279 c = (const unsigned char *) canonicalname;
2280 n = (const unsigned char *) name;
2282 for (; (nc = *n) && (cc = *c); n++, c++) {
2284 if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
2285 (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
2288 if (toupper(nc) == toupper(cc))
2295 if (*n == '\0' && *c == '\0')
2302 * Match an event name against all the event named supported by a
2305 * Returns an event descriptor pointer on match or NULL otherwise.
2307 static const struct pmc_event_descr *
2308 pmc_match_event_class(const char *name,
2309 const struct pmc_class_descr *pcd)
2312 const struct pmc_event_descr *ev;
2314 ev = pcd->pm_evc_event_table;
2315 for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
2316 if (pmc_match_event_name(name, ev->pm_ev_name))
2323 pmc_mdep_is_compatible_class(enum pmc_class pc)
2327 for (n = 0; n < pmc_mdep_class_list_size; n++)
2328 if (pmc_mdep_class_list[n] == pc)
2338 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
2339 uint32_t flags, int cpu, pmc_id_t *pmcid)
2343 char *r, *spec_copy;
2344 const char *ctrname;
2345 const struct pmc_event_descr *ev;
2346 const struct pmc_event_alias *alias;
2347 struct pmc_op_pmcallocate pmc_config;
2348 const struct pmc_class_descr *pcd;
2353 if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
2354 mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
2359 /* replace an event alias with the canonical event specifier */
2360 if (pmc_mdep_event_aliases)
2361 for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
2362 if (!strcasecmp(ctrspec, alias->pm_alias)) {
2363 spec_copy = strdup(alias->pm_spec);
2367 if (spec_copy == NULL)
2368 spec_copy = strdup(ctrspec);
2371 ctrname = strsep(&r, ",");
2374 * If a explicit class prefix was given by the user, restrict the
2375 * search for the event to the specified PMC class.
2378 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
2379 pcd = pmc_class_table[n];
2380 if (pmc_mdep_is_compatible_class(pcd->pm_evc_class) &&
2381 strncasecmp(ctrname, pcd->pm_evc_name,
2382 pcd->pm_evc_name_size) == 0) {
2383 if ((ev = pmc_match_event_class(ctrname +
2384 pcd->pm_evc_name_size, pcd)) == NULL) {
2393 * Otherwise, search for this event in all compatible PMC
2396 for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
2397 pcd = pmc_class_table[n];
2398 if (pmc_mdep_is_compatible_class(pcd->pm_evc_class))
2399 ev = pmc_match_event_class(ctrname, pcd);
2407 bzero(&pmc_config, sizeof(pmc_config));
2408 pmc_config.pm_ev = ev->pm_ev_code;
2409 pmc_config.pm_class = pcd->pm_evc_class;
2410 pmc_config.pm_cpu = cpu;
2411 pmc_config.pm_mode = mode;
2412 pmc_config.pm_flags = flags;
2414 if (PMC_IS_SAMPLING_MODE(mode))
2415 pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
2417 if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
2422 if (PMC_CALL(PMCALLOCATE, &pmc_config) < 0)
2425 *pmcid = pmc_config.pm_pmcid;
2437 pmc_attach(pmc_id_t pmc, pid_t pid)
2439 struct pmc_op_pmcattach pmc_attach_args;
2441 pmc_attach_args.pm_pmc = pmc;
2442 pmc_attach_args.pm_pid = pid;
2444 return (PMC_CALL(PMCATTACH, &pmc_attach_args));
2448 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
2453 cl = PMC_ID_TO_CLASS(pmcid);
2454 for (i = 0; i < cpu_info.pm_nclass; i++)
2455 if (cpu_info.pm_classes[i].pm_class == cl) {
2456 *caps = cpu_info.pm_classes[i].pm_caps;
2464 pmc_configure_logfile(int fd)
2466 struct pmc_op_configurelog cla;
2469 if (PMC_CALL(CONFIGURELOG, &cla) < 0)
2475 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
2477 if (pmc_syscall == -1) {
2487 pmc_detach(pmc_id_t pmc, pid_t pid)
2489 struct pmc_op_pmcattach pmc_detach_args;
2491 pmc_detach_args.pm_pmc = pmc;
2492 pmc_detach_args.pm_pid = pid;
2493 return (PMC_CALL(PMCDETACH, &pmc_detach_args));
2497 pmc_disable(int cpu, int pmc)
2499 struct pmc_op_pmcadmin ssa;
2503 ssa.pm_state = PMC_STATE_DISABLED;
2504 return (PMC_CALL(PMCADMIN, &ssa));
2508 pmc_enable(int cpu, int pmc)
2510 struct pmc_op_pmcadmin ssa;
2514 ssa.pm_state = PMC_STATE_FREE;
2515 return (PMC_CALL(PMCADMIN, &ssa));
2519 * Return a list of events known to a given PMC class. 'cl' is the
2520 * PMC class identifier, 'eventnames' is the returned list of 'const
2521 * char *' pointers pointing to the names of the events. 'nevents' is
2522 * the number of event name pointers returned.
2524 * The space for 'eventnames' is allocated using malloc(3). The caller
2525 * is responsible for freeing this space when done.
2528 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
2533 const struct pmc_event_descr *ev;
2538 ev = iaf_event_table;
2539 count = PMC_EVENT_TABLE_SIZE(iaf);
2543 * Return the most appropriate set of event name
2544 * spellings for the current CPU.
2546 switch (cpu_info.pm_cputype) {
2548 case PMC_CPU_INTEL_ATOM:
2549 ev = atom_event_table;
2550 count = PMC_EVENT_TABLE_SIZE(atom);
2552 case PMC_CPU_INTEL_CORE:
2553 ev = core_event_table;
2554 count = PMC_EVENT_TABLE_SIZE(core);
2556 case PMC_CPU_INTEL_CORE2:
2557 case PMC_CPU_INTEL_CORE2EXTREME:
2558 ev = core2_event_table;
2559 count = PMC_EVENT_TABLE_SIZE(core2);
2561 case PMC_CPU_INTEL_COREI7:
2562 ev = corei7_event_table;
2563 count = PMC_EVENT_TABLE_SIZE(corei7);
2565 case PMC_CPU_INTEL_WESTMERE:
2566 ev = westmere_event_table;
2567 count = PMC_EVENT_TABLE_SIZE(westmere);
2572 ev = ucf_event_table;
2573 count = PMC_EVENT_TABLE_SIZE(ucf);
2577 * Return the most appropriate set of event name
2578 * spellings for the current CPU.
2580 switch (cpu_info.pm_cputype) {
2582 case PMC_CPU_INTEL_COREI7:
2583 ev = corei7uc_event_table;
2584 count = PMC_EVENT_TABLE_SIZE(corei7uc);
2586 case PMC_CPU_INTEL_WESTMERE:
2587 ev = westmereuc_event_table;
2588 count = PMC_EVENT_TABLE_SIZE(westmereuc);
2593 ev = tsc_event_table;
2594 count = PMC_EVENT_TABLE_SIZE(tsc);
2597 ev = k7_event_table;
2598 count = PMC_EVENT_TABLE_SIZE(k7);
2601 ev = k8_event_table;
2602 count = PMC_EVENT_TABLE_SIZE(k8);
2605 ev = p4_event_table;
2606 count = PMC_EVENT_TABLE_SIZE(p4);
2609 ev = p5_event_table;
2610 count = PMC_EVENT_TABLE_SIZE(p5);
2613 ev = p6_event_table;
2614 count = PMC_EVENT_TABLE_SIZE(p6);
2616 case PMC_CLASS_XSCALE:
2617 ev = xscale_event_table;
2618 count = PMC_EVENT_TABLE_SIZE(xscale);
2620 case PMC_CLASS_MIPS24K:
2621 ev = mips24k_event_table;
2622 count = PMC_EVENT_TABLE_SIZE(mips24k);
2624 case PMC_CLASS_PPC7450:
2625 ev = ppc7450_event_table;
2626 count = PMC_EVENT_TABLE_SIZE(ppc7450);
2633 if ((names = malloc(count * sizeof(const char *))) == NULL)
2636 *eventnames = names;
2639 for (;count--; ev++, names++)
2640 *names = ev->pm_ev_name;
2645 pmc_flush_logfile(void)
2647 return (PMC_CALL(FLUSHLOG,0));
2651 pmc_close_logfile(void)
2653 return (PMC_CALL(CLOSELOG,0));
2657 pmc_get_driver_stats(struct pmc_driverstats *ds)
2659 struct pmc_op_getdriverstats gms;
2661 if (PMC_CALL(GETDRIVERSTATS, &gms) < 0)
2664 /* copy out fields in the current userland<->library interface */
2665 ds->pm_intr_ignored = gms.pm_intr_ignored;
2666 ds->pm_intr_processed = gms.pm_intr_processed;
2667 ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
2668 ds->pm_syscalls = gms.pm_syscalls;
2669 ds->pm_syscall_errors = gms.pm_syscall_errors;
2670 ds->pm_buffer_requests = gms.pm_buffer_requests;
2671 ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
2672 ds->pm_log_sweeps = gms.pm_log_sweeps;
2677 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
2679 struct pmc_op_getmsr gm;
2682 if (PMC_CALL(PMCGETMSR, &gm) < 0)
2691 int error, pmc_mod_id;
2693 uint32_t abi_version;
2694 struct module_stat pmc_modstat;
2695 struct pmc_op_getcpuinfo op_cpu_info;
2696 #if defined(__amd64__) || defined(__i386__)
2697 int cpu_has_iaf_counters;
2701 if (pmc_syscall != -1) /* already inited */
2704 /* retrieve the system call number from the KLD */
2705 if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
2708 pmc_modstat.version = sizeof(struct module_stat);
2709 if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
2712 pmc_syscall = pmc_modstat.data.intval;
2714 /* check the kernel module's ABI against our compiled-in version */
2715 abi_version = PMC_VERSION;
2716 if (PMC_CALL(GETMODULEVERSION, &abi_version) < 0)
2717 return (pmc_syscall = -1);
2719 /* ignore patch & minor numbers for the comparision */
2720 if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
2721 errno = EPROGMISMATCH;
2722 return (pmc_syscall = -1);
2725 if (PMC_CALL(GETCPUINFO, &op_cpu_info) < 0)
2726 return (pmc_syscall = -1);
2728 cpu_info.pm_cputype = op_cpu_info.pm_cputype;
2729 cpu_info.pm_ncpu = op_cpu_info.pm_ncpu;
2730 cpu_info.pm_npmc = op_cpu_info.pm_npmc;
2731 cpu_info.pm_nclass = op_cpu_info.pm_nclass;
2732 for (n = 0; n < cpu_info.pm_nclass; n++)
2733 cpu_info.pm_classes[n] = op_cpu_info.pm_classes[n];
2735 pmc_class_table = malloc(PMC_CLASS_TABLE_SIZE *
2736 sizeof(struct pmc_class_descr *));
2738 if (pmc_class_table == NULL)
2741 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++)
2742 pmc_class_table[n] = NULL;
2745 * Fill in the class table.
2748 #if defined(__amd64__) || defined(__i386__)
2749 pmc_class_table[n++] = &tsc_class_table_descr;
2752 * Check if this CPU has fixed function counters.
2754 cpu_has_iaf_counters = 0;
2755 for (t = 0; t < cpu_info.pm_nclass; t++)
2756 if (cpu_info.pm_classes[t].pm_class == PMC_CLASS_IAF &&
2757 cpu_info.pm_classes[t].pm_num > 0)
2758 cpu_has_iaf_counters = 1;
2761 #define PMC_MDEP_INIT(C) do { \
2762 pmc_mdep_event_aliases = C##_aliases; \
2763 pmc_mdep_class_list = C##_pmc_classes; \
2764 pmc_mdep_class_list_size = \
2765 PMC_TABLE_SIZE(C##_pmc_classes); \
2768 #define PMC_MDEP_INIT_INTEL_V2(C) do { \
2770 pmc_class_table[n++] = &iaf_class_table_descr; \
2771 if (!cpu_has_iaf_counters) \
2772 pmc_mdep_event_aliases = \
2773 C##_aliases_without_iaf; \
2774 pmc_class_table[n] = &C##_class_table_descr; \
2777 /* Configure the event name parser. */
2778 switch (cpu_info.pm_cputype) {
2779 #if defined(__i386__)
2780 case PMC_CPU_AMD_K7:
2782 pmc_class_table[n] = &k7_class_table_descr;
2784 case PMC_CPU_INTEL_P5:
2786 pmc_class_table[n] = &p5_class_table_descr;
2788 case PMC_CPU_INTEL_P6: /* P6 ... Pentium M CPUs have */
2789 case PMC_CPU_INTEL_PII: /* similar PMCs. */
2790 case PMC_CPU_INTEL_PIII:
2791 case PMC_CPU_INTEL_PM:
2793 pmc_class_table[n] = &p6_class_table_descr;
2796 #if defined(__amd64__) || defined(__i386__)
2797 case PMC_CPU_AMD_K8:
2799 pmc_class_table[n] = &k8_class_table_descr;
2801 case PMC_CPU_INTEL_ATOM:
2802 PMC_MDEP_INIT_INTEL_V2(atom);
2804 case PMC_CPU_INTEL_CORE:
2805 PMC_MDEP_INIT(core);
2806 pmc_class_table[n] = &core_class_table_descr;
2808 case PMC_CPU_INTEL_CORE2:
2809 case PMC_CPU_INTEL_CORE2EXTREME:
2810 PMC_MDEP_INIT_INTEL_V2(core2);
2812 case PMC_CPU_INTEL_COREI7:
2813 pmc_class_table[n++] = &ucf_class_table_descr;
2814 pmc_class_table[n++] = &corei7uc_class_table_descr;
2815 PMC_MDEP_INIT_INTEL_V2(corei7);
2817 case PMC_CPU_INTEL_WESTMERE:
2818 pmc_class_table[n++] = &ucf_class_table_descr;
2819 pmc_class_table[n++] = &westmereuc_class_table_descr;
2820 PMC_MDEP_INIT_INTEL_V2(westmere);
2822 case PMC_CPU_INTEL_PIV:
2824 pmc_class_table[n] = &p4_class_table_descr;
2827 #if defined(__XSCALE__)
2828 case PMC_CPU_INTEL_XSCALE:
2829 PMC_MDEP_INIT(xscale);
2830 pmc_class_table[n] = &xscale_class_table_descr;
2833 #if defined(__mips__)
2834 case PMC_CPU_MIPS_24K:
2835 PMC_MDEP_INIT(mips24k);
2836 pmc_class_table[n] = &mips24k_class_table_descr;
2838 #endif /* __mips__ */
2839 #if defined(__powerpc__)
2840 case PMC_CPU_PPC_7450:
2841 PMC_MDEP_INIT(ppc7450);
2842 pmc_class_table[n] = &ppc7450_class_table_descr;
2847 * Some kind of CPU this version of the library knows nothing
2848 * about. This shouldn't happen since the abi version check
2849 * should have caught this.
2852 return (pmc_syscall = -1);
2859 pmc_name_of_capability(enum pmc_caps cap)
2864 * 'cap' should have a single bit set and should be in
2867 if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
2868 cap > PMC_CAP_LAST) {
2874 return (pmc_capability_names[i - 1]);
2878 pmc_name_of_class(enum pmc_class pc)
2880 if ((int) pc >= PMC_CLASS_FIRST &&
2881 pc <= PMC_CLASS_LAST)
2882 return (pmc_class_names[pc]);
2889 pmc_name_of_cputype(enum pmc_cputype cp)
2893 for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
2894 if (cp == pmc_cputype_names[n].pm_cputype)
2895 return (pmc_cputype_names[n].pm_name);
2902 pmc_name_of_disposition(enum pmc_disp pd)
2904 if ((int) pd >= PMC_DISP_FIRST &&
2905 pd <= PMC_DISP_LAST)
2906 return (pmc_disposition_names[pd]);
2913 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
2915 const struct pmc_event_descr *ev, *evfence;
2917 ev = evfence = NULL;
2918 if (pe >= PMC_EV_IAF_FIRST && pe <= PMC_EV_IAF_LAST) {
2919 ev = iaf_event_table;
2920 evfence = iaf_event_table + PMC_EVENT_TABLE_SIZE(iaf);
2921 } else if (pe >= PMC_EV_IAP_FIRST && pe <= PMC_EV_IAP_LAST) {
2923 case PMC_CPU_INTEL_ATOM:
2924 ev = atom_event_table;
2925 evfence = atom_event_table + PMC_EVENT_TABLE_SIZE(atom);
2927 case PMC_CPU_INTEL_CORE:
2928 ev = core_event_table;
2929 evfence = core_event_table + PMC_EVENT_TABLE_SIZE(core);
2931 case PMC_CPU_INTEL_CORE2:
2932 case PMC_CPU_INTEL_CORE2EXTREME:
2933 ev = core2_event_table;
2934 evfence = core2_event_table + PMC_EVENT_TABLE_SIZE(core2);
2936 case PMC_CPU_INTEL_COREI7:
2937 ev = corei7_event_table;
2938 evfence = corei7_event_table + PMC_EVENT_TABLE_SIZE(corei7);
2940 case PMC_CPU_INTEL_WESTMERE:
2941 ev = westmere_event_table;
2942 evfence = westmere_event_table + PMC_EVENT_TABLE_SIZE(westmere);
2944 default: /* Unknown CPU type. */
2947 } else if (pe >= PMC_EV_UCF_FIRST && pe <= PMC_EV_UCF_LAST) {
2948 ev = ucf_event_table;
2949 evfence = ucf_event_table + PMC_EVENT_TABLE_SIZE(ucf);
2950 } else if (pe >= PMC_EV_UCP_FIRST && pe <= PMC_EV_UCP_LAST) {
2952 case PMC_CPU_INTEL_COREI7:
2953 ev = corei7uc_event_table;
2954 evfence = corei7uc_event_table + PMC_EVENT_TABLE_SIZE(corei7uc);
2956 case PMC_CPU_INTEL_WESTMERE:
2957 ev = westmereuc_event_table;
2958 evfence = westmereuc_event_table + PMC_EVENT_TABLE_SIZE(westmereuc);
2960 default: /* Unknown CPU type. */
2963 } else if (pe >= PMC_EV_K7_FIRST && pe <= PMC_EV_K7_LAST) {
2964 ev = k7_event_table;
2965 evfence = k7_event_table + PMC_EVENT_TABLE_SIZE(k7);
2966 } else if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
2967 ev = k8_event_table;
2968 evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
2969 } else if (pe >= PMC_EV_P4_FIRST && pe <= PMC_EV_P4_LAST) {
2970 ev = p4_event_table;
2971 evfence = p4_event_table + PMC_EVENT_TABLE_SIZE(p4);
2972 } else if (pe >= PMC_EV_P5_FIRST && pe <= PMC_EV_P5_LAST) {
2973 ev = p5_event_table;
2974 evfence = p5_event_table + PMC_EVENT_TABLE_SIZE(p5);
2975 } else if (pe >= PMC_EV_P6_FIRST && pe <= PMC_EV_P6_LAST) {
2976 ev = p6_event_table;
2977 evfence = p6_event_table + PMC_EVENT_TABLE_SIZE(p6);
2978 } else if (pe >= PMC_EV_XSCALE_FIRST && pe <= PMC_EV_XSCALE_LAST) {
2979 ev = xscale_event_table;
2980 evfence = xscale_event_table + PMC_EVENT_TABLE_SIZE(xscale);
2981 } else if (pe >= PMC_EV_MIPS24K_FIRST && pe <= PMC_EV_MIPS24K_LAST) {
2982 ev = mips24k_event_table;
2983 evfence = mips24k_event_table + PMC_EVENT_TABLE_SIZE(mips24k
2985 } else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
2986 ev = ppc7450_event_table;
2987 evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450
2989 } else if (pe == PMC_EV_TSC_TSC) {
2990 ev = tsc_event_table;
2991 evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
2994 for (; ev != evfence; ev++)
2995 if (pe == ev->pm_ev_code)
2996 return (ev->pm_ev_name);
3002 pmc_name_of_event(enum pmc_event pe)
3006 if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
3014 pmc_name_of_mode(enum pmc_mode pm)
3016 if ((int) pm >= PMC_MODE_FIRST &&
3017 pm <= PMC_MODE_LAST)
3018 return (pmc_mode_names[pm]);
3025 pmc_name_of_state(enum pmc_state ps)
3027 if ((int) ps >= PMC_STATE_FIRST &&
3028 ps <= PMC_STATE_LAST)
3029 return (pmc_state_names[ps]);
3038 if (pmc_syscall == -1) {
3043 return (cpu_info.pm_ncpu);
3049 if (pmc_syscall == -1) {
3054 if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
3059 return (cpu_info.pm_npmc);
3063 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
3066 struct pmc_op_getpmcinfo *pmci;
3068 if ((npmc = pmc_npmc(cpu)) < 0)
3071 nbytes = sizeof(struct pmc_op_getpmcinfo) +
3072 npmc * sizeof(struct pmc_info);
3074 if ((pmci = calloc(1, nbytes)) == NULL)
3079 if (PMC_CALL(GETPMCINFO, pmci) < 0) {
3084 /* kernel<->library, library<->userland interfaces are identical */
3085 *ppmci = (struct pmc_pmcinfo *) pmci;
3090 pmc_read(pmc_id_t pmc, pmc_value_t *value)
3092 struct pmc_op_pmcrw pmc_read_op;
3094 pmc_read_op.pm_pmcid = pmc;
3095 pmc_read_op.pm_flags = PMC_F_OLDVALUE;
3096 pmc_read_op.pm_value = -1;
3098 if (PMC_CALL(PMCRW, &pmc_read_op) < 0)
3101 *value = pmc_read_op.pm_value;
3106 pmc_release(pmc_id_t pmc)
3108 struct pmc_op_simple pmc_release_args;
3110 pmc_release_args.pm_pmcid = pmc;
3111 return (PMC_CALL(PMCRELEASE, &pmc_release_args));
3115 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
3117 struct pmc_op_pmcrw pmc_rw_op;
3119 pmc_rw_op.pm_pmcid = pmc;
3120 pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
3121 pmc_rw_op.pm_value = newvalue;
3123 if (PMC_CALL(PMCRW, &pmc_rw_op) < 0)
3126 *oldvaluep = pmc_rw_op.pm_value;
3131 pmc_set(pmc_id_t pmc, pmc_value_t value)
3133 struct pmc_op_pmcsetcount sc;
3136 sc.pm_count = value;
3138 if (PMC_CALL(PMCSETCOUNT, &sc) < 0)
3144 pmc_start(pmc_id_t pmc)
3146 struct pmc_op_simple pmc_start_args;
3148 pmc_start_args.pm_pmcid = pmc;
3149 return (PMC_CALL(PMCSTART, &pmc_start_args));
3153 pmc_stop(pmc_id_t pmc)
3155 struct pmc_op_simple pmc_stop_args;
3157 pmc_stop_args.pm_pmcid = pmc;
3158 return (PMC_CALL(PMCSTOP, &pmc_stop_args));
3162 pmc_width(pmc_id_t pmcid, uint32_t *width)
3167 cl = PMC_ID_TO_CLASS(pmcid);
3168 for (i = 0; i < cpu_info.pm_nclass; i++)
3169 if (cpu_info.pm_classes[i].pm_class == cl) {
3170 *width = cpu_info.pm_classes[i].pm_width;
3178 pmc_write(pmc_id_t pmc, pmc_value_t value)
3180 struct pmc_op_pmcrw pmc_write_op;
3182 pmc_write_op.pm_pmcid = pmc;
3183 pmc_write_op.pm_flags = PMC_F_NEWVALUE;
3184 pmc_write_op.pm_value = value;
3185 return (PMC_CALL(PMCRW, &pmc_write_op));
3189 pmc_writelog(uint32_t userdata)
3191 struct pmc_op_writelog wl;
3193 wl.pm_userdata = userdata;
3194 return (PMC_CALL(WRITELOG, &wl));