2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2008 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/module.h>
36 #include <sys/syscall.h>
50 #include "libpmcinternal.h"
52 /* Function prototypes */
53 #if defined(__amd64__) || defined(__i386__)
54 static int k8_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
55 struct pmc_op_pmcallocate *_pmc_config);
57 #if defined(__amd64__) || defined(__i386__)
58 static int tsc_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
59 struct pmc_op_pmcallocate *_pmc_config);
62 static int armv7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
63 struct pmc_op_pmcallocate *_pmc_config);
65 #if defined(__aarch64__)
66 static int arm64_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
67 struct pmc_op_pmcallocate *_pmc_config);
70 static int mips_allocate_pmc(enum pmc_event _pe, char* ctrspec,
71 struct pmc_op_pmcallocate *_pmc_config);
73 static int soft_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
74 struct pmc_op_pmcallocate *_pmc_config);
76 #if defined(__powerpc__)
77 static int powerpc_allocate_pmc(enum pmc_event _pe, char* ctrspec,
78 struct pmc_op_pmcallocate *_pmc_config);
79 #endif /* __powerpc__ */
81 #define PMC_CALL(cmd, params) \
82 syscall(pmc_syscall, PMC_OP_##cmd, (params))
85 * Event aliases provide a way for the user to ask for generic events
86 * like "cache-misses", or "instructions-retired". These aliases are
87 * mapped to the appropriate canonical event descriptions using a
90 struct pmc_event_alias {
95 static const struct pmc_event_alias *pmc_mdep_event_aliases;
98 * The pmc_event_descr structure maps symbolic names known to the user
99 * to integer codes used by the PMC KLD.
101 struct pmc_event_descr {
102 const char *pm_ev_name;
103 enum pmc_event pm_ev_code;
107 * The pmc_class_descr structure maps class name prefixes for
108 * event names to event tables and other PMC class data.
110 struct pmc_class_descr {
111 const char *pm_evc_name;
112 size_t pm_evc_name_size;
113 enum pmc_class pm_evc_class;
114 const struct pmc_event_descr *pm_evc_event_table;
115 size_t pm_evc_event_table_size;
116 int (*pm_evc_allocate_pmc)(enum pmc_event _pe,
117 char *_ctrspec, struct pmc_op_pmcallocate *_pa);
120 #define PMC_TABLE_SIZE(N) (sizeof(N)/sizeof(N[0]))
121 #define PMC_EVENT_TABLE_SIZE(N) PMC_TABLE_SIZE(N##_event_table)
124 #define __PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
127 * PMC_CLASSDEP_TABLE(NAME, CLASS)
129 * Define a table mapping event names and aliases to HWPMC event IDs.
131 #define PMC_CLASSDEP_TABLE(N, C) \
132 static const struct pmc_event_descr N##_event_table[] = \
137 PMC_CLASSDEP_TABLE(iaf, IAF);
138 PMC_CLASSDEP_TABLE(k8, K8);
139 PMC_CLASSDEP_TABLE(armv7, ARMV7);
140 PMC_CLASSDEP_TABLE(armv8, ARMV8);
141 PMC_CLASSDEP_TABLE(beri, BERI);
142 PMC_CLASSDEP_TABLE(mips24k, MIPS24K);
143 PMC_CLASSDEP_TABLE(mips74k, MIPS74K);
144 PMC_CLASSDEP_TABLE(octeon, OCTEON);
145 PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
146 PMC_CLASSDEP_TABLE(ppc970, PPC970);
147 PMC_CLASSDEP_TABLE(power8, POWER8);
148 PMC_CLASSDEP_TABLE(e500, E500);
150 static struct pmc_event_descr soft_event_table[PMC_EV_DYN_COUNT];
152 #undef __PMC_EV_ALIAS
153 #define __PMC_EV_ALIAS(N,CODE) { N, PMC_EV_##CODE },
156 * TODO: Factor out the __PMC_EV_ARMV7/8 list into a single separate table
157 * rather than duplicating for each core.
160 static const struct pmc_event_descr cortex_a8_event_table[] =
162 __PMC_EV_ALIAS_ARMV7_CORTEX_A8()
166 static const struct pmc_event_descr cortex_a9_event_table[] =
168 __PMC_EV_ALIAS_ARMV7_CORTEX_A9()
172 static const struct pmc_event_descr cortex_a53_event_table[] =
174 __PMC_EV_ALIAS_ARMV8_CORTEX_A53()
178 static const struct pmc_event_descr cortex_a57_event_table[] =
180 __PMC_EV_ALIAS_ARMV8_CORTEX_A57()
184 static const struct pmc_event_descr cortex_a76_event_table[] =
186 __PMC_EV_ALIAS_ARMV8_CORTEX_A76()
190 static const struct pmc_event_descr tsc_event_table[] =
195 #undef PMC_CLASS_TABLE_DESC
196 #define PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR) \
197 static const struct pmc_class_descr NAME##_class_table_descr = \
199 .pm_evc_name = #CLASS "-", \
200 .pm_evc_name_size = sizeof(#CLASS "-") - 1, \
201 .pm_evc_class = PMC_CLASS_##CLASS , \
202 .pm_evc_event_table = EVENTS##_event_table , \
203 .pm_evc_event_table_size = \
204 PMC_EVENT_TABLE_SIZE(EVENTS), \
205 .pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc \
208 #if defined(__i386__) || defined(__amd64__)
209 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
211 #if defined(__i386__) || defined(__amd64__)
212 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
215 PMC_CLASS_TABLE_DESC(cortex_a8, ARMV7, cortex_a8, armv7);
216 PMC_CLASS_TABLE_DESC(cortex_a9, ARMV7, cortex_a9, armv7);
218 #if defined(__aarch64__)
219 PMC_CLASS_TABLE_DESC(cortex_a53, ARMV8, cortex_a53, arm64);
220 PMC_CLASS_TABLE_DESC(cortex_a57, ARMV8, cortex_a57, arm64);
221 PMC_CLASS_TABLE_DESC(cortex_a76, ARMV8, cortex_a76, arm64);
223 #if defined(__mips__)
224 PMC_CLASS_TABLE_DESC(beri, BERI, beri, mips);
225 PMC_CLASS_TABLE_DESC(mips24k, MIPS24K, mips24k, mips);
226 PMC_CLASS_TABLE_DESC(mips74k, MIPS74K, mips74k, mips);
227 PMC_CLASS_TABLE_DESC(octeon, OCTEON, octeon, mips);
228 #endif /* __mips__ */
229 #if defined(__powerpc__)
230 PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, powerpc);
231 PMC_CLASS_TABLE_DESC(ppc970, PPC970, ppc970, powerpc);
232 PMC_CLASS_TABLE_DESC(power8, POWER8, power8, powerpc);
233 PMC_CLASS_TABLE_DESC(e500, E500, e500, powerpc);
236 static struct pmc_class_descr soft_class_table_descr =
238 .pm_evc_name = "SOFT-",
239 .pm_evc_name_size = sizeof("SOFT-") - 1,
240 .pm_evc_class = PMC_CLASS_SOFT,
241 .pm_evc_event_table = NULL,
242 .pm_evc_event_table_size = 0,
243 .pm_evc_allocate_pmc = soft_allocate_pmc
246 #undef PMC_CLASS_TABLE_DESC
248 static const struct pmc_class_descr **pmc_class_table;
249 #define PMC_CLASS_TABLE_SIZE cpu_info.pm_nclass
252 * Mapping tables, mapping enumeration values to human readable
256 static const char * pmc_capability_names[] = {
258 #define __PMC_CAP(N,V,D) #N ,
262 struct pmc_class_map {
263 enum pmc_class pm_class;
267 static const struct pmc_class_map pmc_class_names[] = {
269 #define __PMC_CLASS(S,V,D) { .pm_class = PMC_CLASS_##S, .pm_name = #S } ,
273 struct pmc_cputype_map {
274 enum pmc_cputype pm_cputype;
278 static const struct pmc_cputype_map pmc_cputype_names[] = {
280 #define __PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
284 static const char * pmc_disposition_names[] = {
286 #define __PMC_DISP(D) #D ,
290 static const char * pmc_mode_names[] = {
292 #define __PMC_MODE(M,N) #M ,
296 static const char * pmc_state_names[] = {
298 #define __PMC_STATE(S) #S ,
303 * Filled in by pmc_init().
305 static int pmc_syscall = -1;
306 static struct pmc_cpuinfo cpu_info;
307 static struct pmc_op_getdyneventinfo soft_event_info;
309 /* Event masks for events */
312 const uint64_t pm_value;
314 #define PMCMASK(N,V) { .pm_name = #N, .pm_value = (V) }
315 #define NULLMASK { .pm_name = NULL }
317 #if defined(__amd64__) || defined(__i386__)
319 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint64_t *evmask)
321 const struct pmc_masks *pm;
325 if (pmask == NULL) /* no mask keywords */
327 q = strchr(p, '='); /* skip '=' */
328 if (*++q == '\0') /* no more data */
330 c = 0; /* count of mask keywords seen */
331 while ((r = strsep(&q, "+")) != NULL) {
332 for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
335 if (pm->pm_name == NULL) /* not found */
337 *evmask |= pm->pm_value;
344 #define KWMATCH(p,kw) (strcasecmp((p), (kw)) == 0)
345 #define KWPREFIXMATCH(p,kw) (strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
346 #define EV_ALIAS(N,S) { .pm_alias = N, .pm_spec = S }
348 #if defined(__amd64__) || defined(__i386__)
354 static struct pmc_event_alias k8_aliases[] = {
355 EV_ALIAS("branches", "k8-fr-retired-taken-branches"),
356 EV_ALIAS("branch-mispredicts",
357 "k8-fr-retired-taken-branches-mispredicted"),
358 EV_ALIAS("cycles", "tsc"),
359 EV_ALIAS("dc-misses", "k8-dc-miss"),
360 EV_ALIAS("ic-misses", "k8-ic-miss"),
361 EV_ALIAS("instructions", "k8-fr-retired-x86-instructions"),
362 EV_ALIAS("interrupts", "k8-fr-taken-hardware-interrupts"),
363 EV_ALIAS("unhalted-cycles", "k8-bu-cpu-clk-unhalted"),
367 #define __K8MASK(N,V) PMCMASK(N,(1 << (V)))
373 /* fp dispatched fpu ops */
374 static const struct pmc_masks k8_mask_fdfo[] = {
375 __K8MASK(add-pipe-excluding-junk-ops, 0),
376 __K8MASK(multiply-pipe-excluding-junk-ops, 1),
377 __K8MASK(store-pipe-excluding-junk-ops, 2),
378 __K8MASK(add-pipe-junk-ops, 3),
379 __K8MASK(multiply-pipe-junk-ops, 4),
380 __K8MASK(store-pipe-junk-ops, 5),
384 /* ls segment register loads */
385 static const struct pmc_masks k8_mask_lsrl[] = {
396 /* ls locked operation */
397 static const struct pmc_masks k8_mask_llo[] = {
398 __K8MASK(locked-instructions, 0),
399 __K8MASK(cycles-in-request, 1),
400 __K8MASK(cycles-to-complete, 2),
404 /* dc refill from {l2,system} and dc copyback */
405 static const struct pmc_masks k8_mask_dc[] = {
406 __K8MASK(invalid, 0),
408 __K8MASK(exclusive, 2),
410 __K8MASK(modified, 4),
414 /* dc one bit ecc error */
415 static const struct pmc_masks k8_mask_dobee[] = {
416 __K8MASK(scrubber, 0),
417 __K8MASK(piggyback, 1),
421 /* dc dispatched prefetch instructions */
422 static const struct pmc_masks k8_mask_ddpi[] = {
429 /* dc dcache accesses by locks */
430 static const struct pmc_masks k8_mask_dabl[] = {
431 __K8MASK(accesses, 0),
436 /* bu internal l2 request */
437 static const struct pmc_masks k8_mask_bilr[] = {
438 __K8MASK(ic-fill, 0),
439 __K8MASK(dc-fill, 1),
440 __K8MASK(tlb-reload, 2),
441 __K8MASK(tag-snoop, 3),
442 __K8MASK(cancelled, 4),
446 /* bu fill request l2 miss */
447 static const struct pmc_masks k8_mask_bfrlm[] = {
448 __K8MASK(ic-fill, 0),
449 __K8MASK(dc-fill, 1),
450 __K8MASK(tlb-reload, 2),
454 /* bu fill into l2 */
455 static const struct pmc_masks k8_mask_bfil[] = {
456 __K8MASK(dirty-l2-victim, 0),
457 __K8MASK(victim-from-l2, 1),
461 /* fr retired fpu instructions */
462 static const struct pmc_masks k8_mask_frfi[] = {
464 __K8MASK(mmx-3dnow, 1),
465 __K8MASK(packed-sse-sse2, 2),
466 __K8MASK(scalar-sse-sse2, 3),
470 /* fr retired fastpath double op instructions */
471 static const struct pmc_masks k8_mask_frfdoi[] = {
472 __K8MASK(low-op-pos-0, 0),
473 __K8MASK(low-op-pos-1, 1),
474 __K8MASK(low-op-pos-2, 2),
478 /* fr fpu exceptions */
479 static const struct pmc_masks k8_mask_ffe[] = {
480 __K8MASK(x87-reclass-microfaults, 0),
481 __K8MASK(sse-retype-microfaults, 1),
482 __K8MASK(sse-reclass-microfaults, 2),
483 __K8MASK(sse-and-x87-microtraps, 3),
487 /* nb memory controller page access event */
488 static const struct pmc_masks k8_mask_nmcpae[] = {
489 __K8MASK(page-hit, 0),
490 __K8MASK(page-miss, 1),
491 __K8MASK(page-conflict, 2),
495 /* nb memory controller turnaround */
496 static const struct pmc_masks k8_mask_nmct[] = {
497 __K8MASK(dimm-turnaround, 0),
498 __K8MASK(read-to-write-turnaround, 1),
499 __K8MASK(write-to-read-turnaround, 2),
503 /* nb memory controller bypass saturation */
504 static const struct pmc_masks k8_mask_nmcbs[] = {
505 __K8MASK(memory-controller-hi-pri-bypass, 0),
506 __K8MASK(memory-controller-lo-pri-bypass, 1),
507 __K8MASK(dram-controller-interface-bypass, 2),
508 __K8MASK(dram-controller-queue-bypass, 3),
512 /* nb sized commands */
513 static const struct pmc_masks k8_mask_nsc[] = {
514 __K8MASK(nonpostwrszbyte, 0),
515 __K8MASK(nonpostwrszdword, 1),
516 __K8MASK(postwrszbyte, 2),
517 __K8MASK(postwrszdword, 3),
518 __K8MASK(rdszbyte, 4),
519 __K8MASK(rdszdword, 5),
520 __K8MASK(rdmodwr, 6),
524 /* nb probe result */
525 static const struct pmc_masks k8_mask_npr[] = {
526 __K8MASK(probe-miss, 0),
527 __K8MASK(probe-hit, 1),
528 __K8MASK(probe-hit-dirty-no-memory-cancel, 2),
529 __K8MASK(probe-hit-dirty-with-memory-cancel, 3),
533 /* nb hypertransport bus bandwidth */
534 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
535 __K8MASK(command, 0),
537 __K8MASK(buffer-release, 2),
544 #define K8_KW_COUNT "count"
545 #define K8_KW_EDGE "edge"
546 #define K8_KW_INV "inv"
547 #define K8_KW_MASK "mask"
548 #define K8_KW_OS "os"
549 #define K8_KW_USR "usr"
552 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
553 struct pmc_op_pmcallocate *pmc_config)
559 const struct pmc_masks *pm, *pmask;
561 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
562 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
567 #define __K8SETMASK(M) pmask = k8_mask_##M
569 /* setup parsing tables */
571 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
574 case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
577 case PMC_EV_K8_LS_LOCKED_OPERATION:
580 case PMC_EV_K8_DC_REFILL_FROM_L2:
581 case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
582 case PMC_EV_K8_DC_COPYBACK:
585 case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
588 case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
591 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
594 case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
597 case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
600 case PMC_EV_K8_BU_FILL_INTO_L2:
603 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
606 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
609 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
612 case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
615 case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
618 case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
621 case PMC_EV_K8_NB_SIZED_COMMANDS:
624 case PMC_EV_K8_NB_PROBE_RESULT:
627 case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
628 case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
629 case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
634 break; /* no options defined */
637 while ((p = strsep(&ctrspec, ",")) != NULL) {
638 if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
640 if (*++q == '\0') /* skip '=' */
643 count = strtol(q, &e, 0);
644 if (e == q || *e != '\0')
647 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
648 pmc_config->pm_md.pm_amd.pm_amd_config |=
649 AMD_PMC_TO_COUNTER(count);
651 } else if (KWMATCH(p, K8_KW_EDGE)) {
652 pmc_config->pm_caps |= PMC_CAP_EDGE;
653 } else if (KWMATCH(p, K8_KW_INV)) {
654 pmc_config->pm_caps |= PMC_CAP_INVERT;
655 } else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
656 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
658 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
659 } else if (KWMATCH(p, K8_KW_OS)) {
660 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
661 } else if (KWMATCH(p, K8_KW_USR)) {
662 pmc_config->pm_caps |= PMC_CAP_USER;
667 /* other post processing */
669 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
670 case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
671 case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
672 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
673 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
674 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
675 /* XXX only available in rev B and later */
677 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
678 /* XXX only available in rev C and later */
680 case PMC_EV_K8_LS_LOCKED_OPERATION:
681 /* XXX CPU Rev A,B evmask is to be zero */
682 if (evmask & (evmask - 1)) /* > 1 bit set */
685 evmask = 0x01; /* Rev C and later: #instrs */
686 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
690 if (evmask == 0 && pmask != NULL) {
691 for (pm = pmask; pm->pm_name; pm++)
692 evmask |= pm->pm_value;
693 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
697 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
698 pmc_config->pm_md.pm_amd.pm_amd_config =
699 AMD_PMC_TO_UNITMASK(evmask);
706 #if defined(__i386__) || defined(__amd64__)
708 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
709 struct pmc_op_pmcallocate *pmc_config)
711 if (pe != PMC_EV_TSC_TSC)
714 /* TSC events must be unqualified. */
715 if (ctrspec && *ctrspec != '\0')
718 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
719 pmc_config->pm_caps |= PMC_CAP_READ;
725 static struct pmc_event_alias generic_aliases[] = {
726 EV_ALIAS("instructions", "SOFT-CLOCK.HARD"),
731 soft_allocate_pmc(enum pmc_event pe, char *ctrspec,
732 struct pmc_op_pmcallocate *pmc_config)
737 if ((int)pe < PMC_EV_SOFT_FIRST || (int)pe > PMC_EV_SOFT_LAST)
740 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
745 static struct pmc_event_alias cortex_a8_aliases[] = {
746 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
747 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
748 EV_ALIAS("instructions", "INSTR_EXECUTED"),
752 static struct pmc_event_alias cortex_a9_aliases[] = {
753 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
754 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
755 EV_ALIAS("instructions", "INSTR_EXECUTED"),
760 armv7_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
761 struct pmc_op_pmcallocate *pmc_config __unused)
772 #if defined(__aarch64__)
773 static struct pmc_event_alias cortex_a53_aliases[] = {
776 static struct pmc_event_alias cortex_a57_aliases[] = {
779 static struct pmc_event_alias cortex_a76_aliases[] = {
783 arm64_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
784 struct pmc_op_pmcallocate *pmc_config __unused)
795 #if defined(__mips__)
797 static struct pmc_event_alias beri_aliases[] = {
798 EV_ALIAS("instructions", "INST"),
802 static struct pmc_event_alias mips24k_aliases[] = {
803 EV_ALIAS("instructions", "INSTR_EXECUTED"),
804 EV_ALIAS("branches", "BRANCH_COMPLETED"),
805 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
809 static struct pmc_event_alias mips74k_aliases[] = {
810 EV_ALIAS("instructions", "INSTR_EXECUTED"),
811 EV_ALIAS("branches", "BRANCH_INSNS"),
812 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCH_INSNS"),
816 static struct pmc_event_alias octeon_aliases[] = {
817 EV_ALIAS("instructions", "RET"),
818 EV_ALIAS("branches", "BR"),
819 EV_ALIAS("branch-mispredicts", "BRMIS"),
823 #define MIPS_KW_OS "os"
824 #define MIPS_KW_USR "usr"
825 #define MIPS_KW_ANYTHREAD "anythread"
828 mips_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
829 struct pmc_op_pmcallocate *pmc_config __unused)
835 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
837 while ((p = strsep(&ctrspec, ",")) != NULL) {
838 if (KWMATCH(p, MIPS_KW_OS))
839 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
840 else if (KWMATCH(p, MIPS_KW_USR))
841 pmc_config->pm_caps |= PMC_CAP_USER;
842 else if (KWMATCH(p, MIPS_KW_ANYTHREAD))
843 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
851 #endif /* __mips__ */
853 #if defined(__powerpc__)
855 static struct pmc_event_alias ppc7450_aliases[] = {
856 EV_ALIAS("instructions", "INSTR_COMPLETED"),
857 EV_ALIAS("branches", "BRANCHES_COMPLETED"),
858 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCHES"),
862 static struct pmc_event_alias ppc970_aliases[] = {
863 EV_ALIAS("instructions", "INSTR_COMPLETED"),
864 EV_ALIAS("cycles", "CYCLES"),
868 static struct pmc_event_alias power8_aliases[] = {
869 EV_ALIAS("instructions", "INSTR_COMPLETED"),
870 EV_ALIAS("cycles", "CYCLES"),
874 static struct pmc_event_alias e500_aliases[] = {
875 EV_ALIAS("instructions", "INSTR_COMPLETED"),
876 EV_ALIAS("cycles", "CYCLES"),
880 #define POWERPC_KW_OS "os"
881 #define POWERPC_KW_USR "usr"
882 #define POWERPC_KW_ANYTHREAD "anythread"
885 powerpc_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
886 struct pmc_op_pmcallocate *pmc_config __unused)
892 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
894 while ((p = strsep(&ctrspec, ",")) != NULL) {
895 if (KWMATCH(p, POWERPC_KW_OS))
896 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
897 else if (KWMATCH(p, POWERPC_KW_USR))
898 pmc_config->pm_caps |= PMC_CAP_USER;
899 else if (KWMATCH(p, POWERPC_KW_ANYTHREAD))
900 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
908 #endif /* __powerpc__ */
912 * Match an event name `name' with its canonical form.
914 * Matches are case insensitive and spaces, periods, underscores and
915 * hyphen characters are considered to match each other.
917 * Returns 1 for a match, 0 otherwise.
921 pmc_match_event_name(const char *name, const char *canonicalname)
924 const unsigned char *c, *n;
926 c = (const unsigned char *) canonicalname;
927 n = (const unsigned char *) name;
929 for (; (nc = *n) && (cc = *c); n++, c++) {
931 if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
932 (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
935 if (toupper(nc) == toupper(cc))
942 if (*n == '\0' && *c == '\0')
949 * Match an event name against all the event named supported by a
952 * Returns an event descriptor pointer on match or NULL otherwise.
954 static const struct pmc_event_descr *
955 pmc_match_event_class(const char *name,
956 const struct pmc_class_descr *pcd)
959 const struct pmc_event_descr *ev;
961 ev = pcd->pm_evc_event_table;
962 for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
963 if (pmc_match_event_name(name, ev->pm_ev_name))
974 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
975 uint32_t flags, int cpu, pmc_id_t *pmcid,
982 const struct pmc_event_descr *ev;
983 const struct pmc_event_alias *alias;
984 struct pmc_op_pmcallocate pmc_config;
985 const struct pmc_class_descr *pcd;
990 if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
991 mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
995 bzero(&pmc_config, sizeof(pmc_config));
996 pmc_config.pm_cpu = cpu;
997 pmc_config.pm_mode = mode;
998 pmc_config.pm_flags = flags;
999 pmc_config.pm_count = count;
1000 if (PMC_IS_SAMPLING_MODE(mode))
1001 pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
1004 * Try to pull the raw event ID directly from the pmu-events table. If
1005 * this is unsupported on the platform, or the event is not found,
1006 * continue with searching the regular event tables.
1008 r = spec_copy = strdup(ctrspec);
1009 ctrname = strsep(&r, ",");
1010 if (pmc_pmu_enabled()) {
1011 if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0)
1014 /* Otherwise, reset any changes */
1015 pmc_config.pm_ev = 0;
1016 pmc_config.pm_caps = 0;
1017 pmc_config.pm_class = 0;
1022 /* replace an event alias with the canonical event specifier */
1023 if (pmc_mdep_event_aliases)
1024 for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
1025 if (!strcasecmp(ctrspec, alias->pm_alias)) {
1026 spec_copy = strdup(alias->pm_spec);
1030 if (spec_copy == NULL)
1031 spec_copy = strdup(ctrspec);
1034 ctrname = strsep(&r, ",");
1037 * If a explicit class prefix was given by the user, restrict the
1038 * search for the event to the specified PMC class.
1041 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
1042 pcd = pmc_class_table[n];
1043 if (pcd != NULL && strncasecmp(ctrname, pcd->pm_evc_name,
1044 pcd->pm_evc_name_size) == 0) {
1045 if ((ev = pmc_match_event_class(ctrname +
1046 pcd->pm_evc_name_size, pcd)) == NULL) {
1055 * Otherwise, search for this event in all compatible PMC
1058 for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
1059 pcd = pmc_class_table[n];
1061 ev = pmc_match_event_class(ctrname, pcd);
1069 pmc_config.pm_ev = ev->pm_ev_code;
1070 pmc_config.pm_class = pcd->pm_evc_class;
1072 if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
1078 if (PMC_CALL(PMCALLOCATE, &pmc_config) == 0) {
1079 *pmcid = pmc_config.pm_pmcid;
1090 pmc_attach(pmc_id_t pmc, pid_t pid)
1092 struct pmc_op_pmcattach pmc_attach_args;
1094 pmc_attach_args.pm_pmc = pmc;
1095 pmc_attach_args.pm_pid = pid;
1097 return (PMC_CALL(PMCATTACH, &pmc_attach_args));
1101 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
1106 cl = PMC_ID_TO_CLASS(pmcid);
1107 for (i = 0; i < cpu_info.pm_nclass; i++)
1108 if (cpu_info.pm_classes[i].pm_class == cl) {
1109 *caps = cpu_info.pm_classes[i].pm_caps;
1117 pmc_configure_logfile(int fd)
1119 struct pmc_op_configurelog cla;
1122 if (PMC_CALL(CONFIGURELOG, &cla) < 0)
1128 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
1130 if (pmc_syscall == -1) {
1140 pmc_detach(pmc_id_t pmc, pid_t pid)
1142 struct pmc_op_pmcattach pmc_detach_args;
1144 pmc_detach_args.pm_pmc = pmc;
1145 pmc_detach_args.pm_pid = pid;
1146 return (PMC_CALL(PMCDETACH, &pmc_detach_args));
1150 pmc_disable(int cpu, int pmc)
1152 struct pmc_op_pmcadmin ssa;
1156 ssa.pm_state = PMC_STATE_DISABLED;
1157 return (PMC_CALL(PMCADMIN, &ssa));
1161 pmc_enable(int cpu, int pmc)
1163 struct pmc_op_pmcadmin ssa;
1167 ssa.pm_state = PMC_STATE_FREE;
1168 return (PMC_CALL(PMCADMIN, &ssa));
1172 * Return a list of events known to a given PMC class. 'cl' is the
1173 * PMC class identifier, 'eventnames' is the returned list of 'const
1174 * char *' pointers pointing to the names of the events. 'nevents' is
1175 * the number of event name pointers returned.
1177 * The space for 'eventnames' is allocated using malloc(3). The caller
1178 * is responsible for freeing this space when done.
1181 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
1186 const struct pmc_event_descr *ev;
1191 ev = iaf_event_table;
1192 count = PMC_EVENT_TABLE_SIZE(iaf);
1195 ev = tsc_event_table;
1196 count = PMC_EVENT_TABLE_SIZE(tsc);
1199 ev = k8_event_table;
1200 count = PMC_EVENT_TABLE_SIZE(k8);
1202 case PMC_CLASS_ARMV7:
1203 switch (cpu_info.pm_cputype) {
1205 case PMC_CPU_ARMV7_CORTEX_A8:
1206 ev = cortex_a8_event_table;
1207 count = PMC_EVENT_TABLE_SIZE(cortex_a8);
1209 case PMC_CPU_ARMV7_CORTEX_A9:
1210 ev = cortex_a9_event_table;
1211 count = PMC_EVENT_TABLE_SIZE(cortex_a9);
1215 case PMC_CLASS_ARMV8:
1216 switch (cpu_info.pm_cputype) {
1218 case PMC_CPU_ARMV8_CORTEX_A53:
1219 ev = cortex_a53_event_table;
1220 count = PMC_EVENT_TABLE_SIZE(cortex_a53);
1222 case PMC_CPU_ARMV8_CORTEX_A57:
1223 ev = cortex_a57_event_table;
1224 count = PMC_EVENT_TABLE_SIZE(cortex_a57);
1226 case PMC_CPU_ARMV8_CORTEX_A76:
1227 ev = cortex_a76_event_table;
1228 count = PMC_EVENT_TABLE_SIZE(cortex_a76);
1232 case PMC_CLASS_BERI:
1233 ev = beri_event_table;
1234 count = PMC_EVENT_TABLE_SIZE(beri);
1236 case PMC_CLASS_MIPS24K:
1237 ev = mips24k_event_table;
1238 count = PMC_EVENT_TABLE_SIZE(mips24k);
1240 case PMC_CLASS_MIPS74K:
1241 ev = mips74k_event_table;
1242 count = PMC_EVENT_TABLE_SIZE(mips74k);
1244 case PMC_CLASS_OCTEON:
1245 ev = octeon_event_table;
1246 count = PMC_EVENT_TABLE_SIZE(octeon);
1248 case PMC_CLASS_PPC7450:
1249 ev = ppc7450_event_table;
1250 count = PMC_EVENT_TABLE_SIZE(ppc7450);
1252 case PMC_CLASS_PPC970:
1253 ev = ppc970_event_table;
1254 count = PMC_EVENT_TABLE_SIZE(ppc970);
1256 case PMC_CLASS_POWER8:
1257 ev = power8_event_table;
1258 count = PMC_EVENT_TABLE_SIZE(power8);
1260 case PMC_CLASS_E500:
1261 ev = e500_event_table;
1262 count = PMC_EVENT_TABLE_SIZE(e500);
1264 case PMC_CLASS_SOFT:
1265 ev = soft_event_table;
1266 count = soft_event_info.pm_nevent;
1273 if ((names = malloc(count * sizeof(const char *))) == NULL)
1276 *eventnames = names;
1279 for (;count--; ev++, names++)
1280 *names = ev->pm_ev_name;
1286 pmc_flush_logfile(void)
1288 return (PMC_CALL(FLUSHLOG,0));
1292 pmc_close_logfile(void)
1294 return (PMC_CALL(CLOSELOG,0));
1298 pmc_get_driver_stats(struct pmc_driverstats *ds)
1300 struct pmc_op_getdriverstats gms;
1302 if (PMC_CALL(GETDRIVERSTATS, &gms) < 0)
1305 /* copy out fields in the current userland<->library interface */
1306 ds->pm_intr_ignored = gms.pm_intr_ignored;
1307 ds->pm_intr_processed = gms.pm_intr_processed;
1308 ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
1309 ds->pm_syscalls = gms.pm_syscalls;
1310 ds->pm_syscall_errors = gms.pm_syscall_errors;
1311 ds->pm_buffer_requests = gms.pm_buffer_requests;
1312 ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
1313 ds->pm_log_sweeps = gms.pm_log_sweeps;
1318 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
1320 struct pmc_op_getmsr gm;
1323 if (PMC_CALL(PMCGETMSR, &gm) < 0)
1332 int error, pmc_mod_id;
1334 uint32_t abi_version;
1335 struct module_stat pmc_modstat;
1336 struct pmc_op_getcpuinfo op_cpu_info;
1338 if (pmc_syscall != -1) /* already inited */
1341 /* retrieve the system call number from the KLD */
1342 if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
1345 pmc_modstat.version = sizeof(struct module_stat);
1346 if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
1349 pmc_syscall = pmc_modstat.data.intval;
1351 /* check the kernel module's ABI against our compiled-in version */
1352 abi_version = PMC_VERSION;
1353 if (PMC_CALL(GETMODULEVERSION, &abi_version) < 0)
1354 return (pmc_syscall = -1);
1356 /* ignore patch & minor numbers for the comparison */
1357 if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
1358 errno = EPROGMISMATCH;
1359 return (pmc_syscall = -1);
1362 bzero(&op_cpu_info, sizeof(op_cpu_info));
1363 if (PMC_CALL(GETCPUINFO, &op_cpu_info) < 0)
1364 return (pmc_syscall = -1);
1366 cpu_info.pm_cputype = op_cpu_info.pm_cputype;
1367 cpu_info.pm_ncpu = op_cpu_info.pm_ncpu;
1368 cpu_info.pm_npmc = op_cpu_info.pm_npmc;
1369 cpu_info.pm_nclass = op_cpu_info.pm_nclass;
1370 for (n = 0; n < op_cpu_info.pm_nclass; n++)
1371 memcpy(&cpu_info.pm_classes[n], &op_cpu_info.pm_classes[n],
1372 sizeof(cpu_info.pm_classes[n]));
1374 pmc_class_table = malloc(PMC_CLASS_TABLE_SIZE *
1375 sizeof(struct pmc_class_descr *));
1377 if (pmc_class_table == NULL)
1380 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++)
1381 pmc_class_table[n] = NULL;
1384 * Get soft events list.
1386 soft_event_info.pm_class = PMC_CLASS_SOFT;
1387 if (PMC_CALL(GETDYNEVENTINFO, &soft_event_info) < 0)
1388 return (pmc_syscall = -1);
1390 /* Map soft events to static list. */
1391 for (n = 0; n < soft_event_info.pm_nevent; n++) {
1392 soft_event_table[n].pm_ev_name =
1393 soft_event_info.pm_events[n].pm_ev_name;
1394 soft_event_table[n].pm_ev_code =
1395 soft_event_info.pm_events[n].pm_ev_code;
1397 soft_class_table_descr.pm_evc_event_table_size = \
1398 soft_event_info.pm_nevent;
1399 soft_class_table_descr.pm_evc_event_table = \
1403 * Fill in the class table.
1407 /* Fill soft events information. */
1408 pmc_class_table[n++] = &soft_class_table_descr;
1409 #if defined(__amd64__) || defined(__i386__)
1410 if (cpu_info.pm_cputype != PMC_CPU_GENERIC)
1411 pmc_class_table[n++] = &tsc_class_table_descr;
1414 #define PMC_MDEP_INIT(C) pmc_mdep_event_aliases = C##_aliases
1416 /* Configure the event name parser. */
1417 switch (cpu_info.pm_cputype) {
1418 #if defined(__amd64__) || defined(__i386__)
1419 case PMC_CPU_AMD_K8:
1421 pmc_class_table[n] = &k8_class_table_descr;
1424 case PMC_CPU_GENERIC:
1425 PMC_MDEP_INIT(generic);
1427 #if defined(__arm__)
1428 case PMC_CPU_ARMV7_CORTEX_A8:
1429 PMC_MDEP_INIT(cortex_a8);
1430 pmc_class_table[n] = &cortex_a8_class_table_descr;
1432 case PMC_CPU_ARMV7_CORTEX_A9:
1433 PMC_MDEP_INIT(cortex_a9);
1434 pmc_class_table[n] = &cortex_a9_class_table_descr;
1437 #if defined(__aarch64__)
1438 case PMC_CPU_ARMV8_CORTEX_A53:
1439 PMC_MDEP_INIT(cortex_a53);
1440 pmc_class_table[n] = &cortex_a53_class_table_descr;
1442 case PMC_CPU_ARMV8_CORTEX_A57:
1443 PMC_MDEP_INIT(cortex_a57);
1444 pmc_class_table[n] = &cortex_a57_class_table_descr;
1446 case PMC_CPU_ARMV8_CORTEX_A76:
1447 PMC_MDEP_INIT(cortex_a76);
1448 pmc_class_table[n] = &cortex_a76_class_table_descr;
1451 #if defined(__mips__)
1452 case PMC_CPU_MIPS_BERI:
1453 PMC_MDEP_INIT(beri);
1454 pmc_class_table[n] = &beri_class_table_descr;
1456 case PMC_CPU_MIPS_24K:
1457 PMC_MDEP_INIT(mips24k);
1458 pmc_class_table[n] = &mips24k_class_table_descr;
1460 case PMC_CPU_MIPS_74K:
1461 PMC_MDEP_INIT(mips74k);
1462 pmc_class_table[n] = &mips74k_class_table_descr;
1464 case PMC_CPU_MIPS_OCTEON:
1465 PMC_MDEP_INIT(octeon);
1466 pmc_class_table[n] = &octeon_class_table_descr;
1468 #endif /* __mips__ */
1469 #if defined(__powerpc__)
1470 case PMC_CPU_PPC_7450:
1471 PMC_MDEP_INIT(ppc7450);
1472 pmc_class_table[n] = &ppc7450_class_table_descr;
1474 case PMC_CPU_PPC_970:
1475 PMC_MDEP_INIT(ppc970);
1476 pmc_class_table[n] = &ppc970_class_table_descr;
1478 case PMC_CPU_PPC_POWER8:
1479 PMC_MDEP_INIT(power8);
1480 pmc_class_table[n] = &power8_class_table_descr;
1482 case PMC_CPU_PPC_E500:
1483 PMC_MDEP_INIT(e500);
1484 pmc_class_table[n] = &e500_class_table_descr;
1489 * Some kind of CPU this version of the library knows nothing
1490 * about. This shouldn't happen since the abi version check
1491 * should have caught this.
1493 #if defined(__amd64__) || defined(__i386__)
1497 return (pmc_syscall = -1);
1504 pmc_name_of_capability(enum pmc_caps cap)
1509 * 'cap' should have a single bit set and should be in
1512 if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
1513 cap > PMC_CAP_LAST) {
1519 return (pmc_capability_names[i - 1]);
1523 pmc_name_of_class(enum pmc_class pc)
1527 for (n = 0; n < PMC_TABLE_SIZE(pmc_class_names); n++)
1528 if (pc == pmc_class_names[n].pm_class)
1529 return (pmc_class_names[n].pm_name);
1536 pmc_name_of_cputype(enum pmc_cputype cp)
1540 for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
1541 if (cp == pmc_cputype_names[n].pm_cputype)
1542 return (pmc_cputype_names[n].pm_name);
1549 pmc_name_of_disposition(enum pmc_disp pd)
1551 if ((int) pd >= PMC_DISP_FIRST &&
1552 pd <= PMC_DISP_LAST)
1553 return (pmc_disposition_names[pd]);
1560 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
1562 const struct pmc_event_descr *ev, *evfence;
1564 ev = evfence = NULL;
1565 if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
1566 ev = k8_event_table;
1567 evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
1569 } else if (pe >= PMC_EV_ARMV7_FIRST && pe <= PMC_EV_ARMV7_LAST) {
1571 case PMC_CPU_ARMV7_CORTEX_A8:
1572 ev = cortex_a8_event_table;
1573 evfence = cortex_a8_event_table + PMC_EVENT_TABLE_SIZE(cortex_a8);
1575 case PMC_CPU_ARMV7_CORTEX_A9:
1576 ev = cortex_a9_event_table;
1577 evfence = cortex_a9_event_table + PMC_EVENT_TABLE_SIZE(cortex_a9);
1579 default: /* Unknown CPU type. */
1582 } else if (pe >= PMC_EV_ARMV8_FIRST && pe <= PMC_EV_ARMV8_LAST) {
1584 case PMC_CPU_ARMV8_CORTEX_A53:
1585 ev = cortex_a53_event_table;
1586 evfence = cortex_a53_event_table + PMC_EVENT_TABLE_SIZE(cortex_a53);
1588 case PMC_CPU_ARMV8_CORTEX_A57:
1589 ev = cortex_a57_event_table;
1590 evfence = cortex_a57_event_table + PMC_EVENT_TABLE_SIZE(cortex_a57);
1592 case PMC_CPU_ARMV8_CORTEX_A76:
1593 ev = cortex_a76_event_table;
1594 evfence = cortex_a76_event_table + PMC_EVENT_TABLE_SIZE(cortex_a76);
1596 default: /* Unknown CPU type. */
1599 } else if (pe >= PMC_EV_BERI_FIRST && pe <= PMC_EV_BERI_LAST) {
1600 ev = beri_event_table;
1601 evfence = beri_event_table + PMC_EVENT_TABLE_SIZE(beri);
1602 } else if (pe >= PMC_EV_MIPS24K_FIRST && pe <= PMC_EV_MIPS24K_LAST) {
1603 ev = mips24k_event_table;
1604 evfence = mips24k_event_table + PMC_EVENT_TABLE_SIZE(mips24k);
1605 } else if (pe >= PMC_EV_MIPS74K_FIRST && pe <= PMC_EV_MIPS74K_LAST) {
1606 ev = mips74k_event_table;
1607 evfence = mips74k_event_table + PMC_EVENT_TABLE_SIZE(mips74k);
1608 } else if (pe >= PMC_EV_OCTEON_FIRST && pe <= PMC_EV_OCTEON_LAST) {
1609 ev = octeon_event_table;
1610 evfence = octeon_event_table + PMC_EVENT_TABLE_SIZE(octeon);
1611 } else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
1612 ev = ppc7450_event_table;
1613 evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450);
1614 } else if (pe >= PMC_EV_PPC970_FIRST && pe <= PMC_EV_PPC970_LAST) {
1615 ev = ppc970_event_table;
1616 evfence = ppc970_event_table + PMC_EVENT_TABLE_SIZE(ppc970);
1617 } else if (pe >= PMC_EV_POWER8_FIRST && pe <= PMC_EV_POWER8_LAST) {
1618 ev = power8_event_table;
1619 evfence = power8_event_table + PMC_EVENT_TABLE_SIZE(power8);
1620 } else if (pe >= PMC_EV_E500_FIRST && pe <= PMC_EV_E500_LAST) {
1621 ev = e500_event_table;
1622 evfence = e500_event_table + PMC_EVENT_TABLE_SIZE(e500);
1623 } else if (pe == PMC_EV_TSC_TSC) {
1624 ev = tsc_event_table;
1625 evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
1626 } else if ((int)pe >= PMC_EV_SOFT_FIRST && (int)pe <= PMC_EV_SOFT_LAST) {
1627 ev = soft_event_table;
1628 evfence = soft_event_table + soft_event_info.pm_nevent;
1631 for (; ev != evfence; ev++)
1632 if (pe == ev->pm_ev_code)
1633 return (ev->pm_ev_name);
1639 pmc_name_of_event(enum pmc_event pe)
1643 if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
1651 pmc_name_of_mode(enum pmc_mode pm)
1653 if ((int) pm >= PMC_MODE_FIRST &&
1654 pm <= PMC_MODE_LAST)
1655 return (pmc_mode_names[pm]);
1662 pmc_name_of_state(enum pmc_state ps)
1664 if ((int) ps >= PMC_STATE_FIRST &&
1665 ps <= PMC_STATE_LAST)
1666 return (pmc_state_names[ps]);
1675 if (pmc_syscall == -1) {
1680 return (cpu_info.pm_ncpu);
1686 if (pmc_syscall == -1) {
1691 if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
1696 return (cpu_info.pm_npmc);
1700 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
1703 struct pmc_op_getpmcinfo *pmci;
1705 if ((npmc = pmc_npmc(cpu)) < 0)
1708 nbytes = sizeof(struct pmc_op_getpmcinfo) +
1709 npmc * sizeof(struct pmc_info);
1711 if ((pmci = calloc(1, nbytes)) == NULL)
1716 if (PMC_CALL(GETPMCINFO, pmci) < 0) {
1721 /* kernel<->library, library<->userland interfaces are identical */
1722 *ppmci = (struct pmc_pmcinfo *) pmci;
1727 pmc_read(pmc_id_t pmc, pmc_value_t *value)
1729 struct pmc_op_pmcrw pmc_read_op;
1731 pmc_read_op.pm_pmcid = pmc;
1732 pmc_read_op.pm_flags = PMC_F_OLDVALUE;
1733 pmc_read_op.pm_value = -1;
1735 if (PMC_CALL(PMCRW, &pmc_read_op) < 0)
1738 *value = pmc_read_op.pm_value;
1743 pmc_release(pmc_id_t pmc)
1745 struct pmc_op_simple pmc_release_args;
1747 pmc_release_args.pm_pmcid = pmc;
1748 return (PMC_CALL(PMCRELEASE, &pmc_release_args));
1752 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
1754 struct pmc_op_pmcrw pmc_rw_op;
1756 pmc_rw_op.pm_pmcid = pmc;
1757 pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
1758 pmc_rw_op.pm_value = newvalue;
1760 if (PMC_CALL(PMCRW, &pmc_rw_op) < 0)
1763 *oldvaluep = pmc_rw_op.pm_value;
1768 pmc_set(pmc_id_t pmc, pmc_value_t value)
1770 struct pmc_op_pmcsetcount sc;
1773 sc.pm_count = value;
1775 if (PMC_CALL(PMCSETCOUNT, &sc) < 0)
1781 pmc_start(pmc_id_t pmc)
1783 struct pmc_op_simple pmc_start_args;
1785 pmc_start_args.pm_pmcid = pmc;
1786 return (PMC_CALL(PMCSTART, &pmc_start_args));
1790 pmc_stop(pmc_id_t pmc)
1792 struct pmc_op_simple pmc_stop_args;
1794 pmc_stop_args.pm_pmcid = pmc;
1795 return (PMC_CALL(PMCSTOP, &pmc_stop_args));
1799 pmc_width(pmc_id_t pmcid, uint32_t *width)
1804 cl = PMC_ID_TO_CLASS(pmcid);
1805 for (i = 0; i < cpu_info.pm_nclass; i++)
1806 if (cpu_info.pm_classes[i].pm_class == cl) {
1807 *width = cpu_info.pm_classes[i].pm_width;
1815 pmc_write(pmc_id_t pmc, pmc_value_t value)
1817 struct pmc_op_pmcrw pmc_write_op;
1819 pmc_write_op.pm_pmcid = pmc;
1820 pmc_write_op.pm_flags = PMC_F_NEWVALUE;
1821 pmc_write_op.pm_value = value;
1822 return (PMC_CALL(PMCRW, &pmc_write_op));
1826 pmc_writelog(uint32_t userdata)
1828 struct pmc_op_writelog wl;
1830 wl.pm_userdata = userdata;
1831 return (PMC_CALL(WRITELOG, &wl));