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);
68 static int cmn600_pmu_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
69 struct pmc_op_pmcallocate *_pmc_config);
70 static int dmc620_pmu_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(cmn600_pmu, CMN600_PMU);
142 PMC_CLASSDEP_TABLE(dmc620_pmu_cd2, DMC620_PMU_CD2);
143 PMC_CLASSDEP_TABLE(dmc620_pmu_c, DMC620_PMU_C);
144 PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
145 PMC_CLASSDEP_TABLE(ppc970, PPC970);
146 PMC_CLASSDEP_TABLE(e500, E500);
148 static struct pmc_event_descr soft_event_table[PMC_EV_DYN_COUNT];
150 #undef __PMC_EV_ALIAS
151 #define __PMC_EV_ALIAS(N,CODE) { N, PMC_EV_##CODE },
154 * TODO: Factor out the __PMC_EV_ARMV7/8 list into a single separate table
155 * rather than duplicating for each core.
158 static const struct pmc_event_descr cortex_a8_event_table[] =
160 __PMC_EV_ALIAS_ARMV7_CORTEX_A8()
164 static const struct pmc_event_descr cortex_a9_event_table[] =
166 __PMC_EV_ALIAS_ARMV7_CORTEX_A9()
170 static const struct pmc_event_descr cortex_a53_event_table[] =
172 __PMC_EV_ALIAS_ARMV8_CORTEX_A53()
176 static const struct pmc_event_descr cortex_a57_event_table[] =
178 __PMC_EV_ALIAS_ARMV8_CORTEX_A57()
182 static const struct pmc_event_descr cortex_a76_event_table[] =
184 __PMC_EV_ALIAS_ARMV8_CORTEX_A76()
188 static const struct pmc_event_descr tsc_event_table[] =
193 #undef PMC_CLASS_TABLE_DESC
194 #define PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR) \
195 static const struct pmc_class_descr NAME##_class_table_descr = \
197 .pm_evc_name = #CLASS "-", \
198 .pm_evc_name_size = sizeof(#CLASS "-") - 1, \
199 .pm_evc_class = PMC_CLASS_##CLASS , \
200 .pm_evc_event_table = EVENTS##_event_table , \
201 .pm_evc_event_table_size = \
202 PMC_EVENT_TABLE_SIZE(EVENTS), \
203 .pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc \
206 #if defined(__i386__) || defined(__amd64__)
207 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
209 #if defined(__i386__) || defined(__amd64__)
210 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
213 PMC_CLASS_TABLE_DESC(cortex_a8, ARMV7, cortex_a8, armv7);
214 PMC_CLASS_TABLE_DESC(cortex_a9, ARMV7, cortex_a9, armv7);
216 #if defined(__aarch64__)
217 PMC_CLASS_TABLE_DESC(cortex_a53, ARMV8, cortex_a53, arm64);
218 PMC_CLASS_TABLE_DESC(cortex_a57, ARMV8, cortex_a57, arm64);
219 PMC_CLASS_TABLE_DESC(cortex_a76, ARMV8, cortex_a76, arm64);
220 PMC_CLASS_TABLE_DESC(cmn600_pmu, CMN600_PMU, cmn600_pmu, cmn600_pmu);
221 PMC_CLASS_TABLE_DESC(dmc620_pmu_cd2, DMC620_PMU_CD2, dmc620_pmu_cd2, dmc620_pmu);
222 PMC_CLASS_TABLE_DESC(dmc620_pmu_c, DMC620_PMU_C, dmc620_pmu_c, dmc620_pmu);
224 #if defined(__powerpc__)
225 PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, powerpc);
226 PMC_CLASS_TABLE_DESC(ppc970, PPC970, ppc970, powerpc);
227 PMC_CLASS_TABLE_DESC(e500, E500, e500, powerpc);
230 static struct pmc_class_descr soft_class_table_descr =
232 .pm_evc_name = "SOFT-",
233 .pm_evc_name_size = sizeof("SOFT-") - 1,
234 .pm_evc_class = PMC_CLASS_SOFT,
235 .pm_evc_event_table = NULL,
236 .pm_evc_event_table_size = 0,
237 .pm_evc_allocate_pmc = soft_allocate_pmc
240 #undef PMC_CLASS_TABLE_DESC
242 static const struct pmc_class_descr **pmc_class_table;
243 #define PMC_CLASS_TABLE_SIZE cpu_info.pm_nclass
246 * Mapping tables, mapping enumeration values to human readable
250 static const char * pmc_capability_names[] = {
252 #define __PMC_CAP(N,V,D) #N ,
256 struct pmc_class_map {
257 enum pmc_class pm_class;
261 static const struct pmc_class_map pmc_class_names[] = {
263 #define __PMC_CLASS(S,V,D) { .pm_class = PMC_CLASS_##S, .pm_name = #S } ,
267 struct pmc_cputype_map {
268 enum pmc_cputype pm_cputype;
272 static const struct pmc_cputype_map pmc_cputype_names[] = {
274 #define __PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
278 static const char * pmc_disposition_names[] = {
280 #define __PMC_DISP(D) #D ,
284 static const char * pmc_mode_names[] = {
286 #define __PMC_MODE(M,N) #M ,
290 static const char * pmc_state_names[] = {
292 #define __PMC_STATE(S) #S ,
297 * Filled in by pmc_init().
299 static int pmc_syscall = -1;
300 static struct pmc_cpuinfo cpu_info;
301 static struct pmc_op_getdyneventinfo soft_event_info;
303 /* Event masks for events */
306 const uint64_t pm_value;
308 #define PMCMASK(N,V) { .pm_name = #N, .pm_value = (V) }
309 #define NULLMASK { .pm_name = NULL }
311 #if defined(__amd64__) || defined(__i386__)
313 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint64_t *evmask)
315 const struct pmc_masks *pm;
319 if (pmask == NULL) /* no mask keywords */
321 q = strchr(p, '='); /* skip '=' */
322 if (*++q == '\0') /* no more data */
324 c = 0; /* count of mask keywords seen */
325 while ((r = strsep(&q, "+")) != NULL) {
326 for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
329 if (pm->pm_name == NULL) /* not found */
331 *evmask |= pm->pm_value;
338 #define KWMATCH(p,kw) (strcasecmp((p), (kw)) == 0)
339 #define KWPREFIXMATCH(p,kw) (strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
340 #define EV_ALIAS(N,S) { .pm_alias = N, .pm_spec = S }
342 #if defined(__amd64__) || defined(__i386__)
348 static struct pmc_event_alias k8_aliases[] = {
349 EV_ALIAS("branches", "k8-fr-retired-taken-branches"),
350 EV_ALIAS("branch-mispredicts",
351 "k8-fr-retired-taken-branches-mispredicted"),
352 EV_ALIAS("cycles", "tsc"),
353 EV_ALIAS("dc-misses", "k8-dc-miss"),
354 EV_ALIAS("ic-misses", "k8-ic-miss"),
355 EV_ALIAS("instructions", "k8-fr-retired-x86-instructions"),
356 EV_ALIAS("interrupts", "k8-fr-taken-hardware-interrupts"),
357 EV_ALIAS("unhalted-cycles", "k8-bu-cpu-clk-unhalted"),
361 #define __K8MASK(N,V) PMCMASK(N,(1 << (V)))
367 /* fp dispatched fpu ops */
368 static const struct pmc_masks k8_mask_fdfo[] = {
369 __K8MASK(add-pipe-excluding-junk-ops, 0),
370 __K8MASK(multiply-pipe-excluding-junk-ops, 1),
371 __K8MASK(store-pipe-excluding-junk-ops, 2),
372 __K8MASK(add-pipe-junk-ops, 3),
373 __K8MASK(multiply-pipe-junk-ops, 4),
374 __K8MASK(store-pipe-junk-ops, 5),
378 /* ls segment register loads */
379 static const struct pmc_masks k8_mask_lsrl[] = {
390 /* ls locked operation */
391 static const struct pmc_masks k8_mask_llo[] = {
392 __K8MASK(locked-instructions, 0),
393 __K8MASK(cycles-in-request, 1),
394 __K8MASK(cycles-to-complete, 2),
398 /* dc refill from {l2,system} and dc copyback */
399 static const struct pmc_masks k8_mask_dc[] = {
400 __K8MASK(invalid, 0),
402 __K8MASK(exclusive, 2),
404 __K8MASK(modified, 4),
408 /* dc one bit ecc error */
409 static const struct pmc_masks k8_mask_dobee[] = {
410 __K8MASK(scrubber, 0),
411 __K8MASK(piggyback, 1),
415 /* dc dispatched prefetch instructions */
416 static const struct pmc_masks k8_mask_ddpi[] = {
423 /* dc dcache accesses by locks */
424 static const struct pmc_masks k8_mask_dabl[] = {
425 __K8MASK(accesses, 0),
430 /* bu internal l2 request */
431 static const struct pmc_masks k8_mask_bilr[] = {
432 __K8MASK(ic-fill, 0),
433 __K8MASK(dc-fill, 1),
434 __K8MASK(tlb-reload, 2),
435 __K8MASK(tag-snoop, 3),
436 __K8MASK(cancelled, 4),
440 /* bu fill request l2 miss */
441 static const struct pmc_masks k8_mask_bfrlm[] = {
442 __K8MASK(ic-fill, 0),
443 __K8MASK(dc-fill, 1),
444 __K8MASK(tlb-reload, 2),
448 /* bu fill into l2 */
449 static const struct pmc_masks k8_mask_bfil[] = {
450 __K8MASK(dirty-l2-victim, 0),
451 __K8MASK(victim-from-l2, 1),
455 /* fr retired fpu instructions */
456 static const struct pmc_masks k8_mask_frfi[] = {
458 __K8MASK(mmx-3dnow, 1),
459 __K8MASK(packed-sse-sse2, 2),
460 __K8MASK(scalar-sse-sse2, 3),
464 /* fr retired fastpath double op instructions */
465 static const struct pmc_masks k8_mask_frfdoi[] = {
466 __K8MASK(low-op-pos-0, 0),
467 __K8MASK(low-op-pos-1, 1),
468 __K8MASK(low-op-pos-2, 2),
472 /* fr fpu exceptions */
473 static const struct pmc_masks k8_mask_ffe[] = {
474 __K8MASK(x87-reclass-microfaults, 0),
475 __K8MASK(sse-retype-microfaults, 1),
476 __K8MASK(sse-reclass-microfaults, 2),
477 __K8MASK(sse-and-x87-microtraps, 3),
481 /* nb memory controller page access event */
482 static const struct pmc_masks k8_mask_nmcpae[] = {
483 __K8MASK(page-hit, 0),
484 __K8MASK(page-miss, 1),
485 __K8MASK(page-conflict, 2),
489 /* nb memory controller turnaround */
490 static const struct pmc_masks k8_mask_nmct[] = {
491 __K8MASK(dimm-turnaround, 0),
492 __K8MASK(read-to-write-turnaround, 1),
493 __K8MASK(write-to-read-turnaround, 2),
497 /* nb memory controller bypass saturation */
498 static const struct pmc_masks k8_mask_nmcbs[] = {
499 __K8MASK(memory-controller-hi-pri-bypass, 0),
500 __K8MASK(memory-controller-lo-pri-bypass, 1),
501 __K8MASK(dram-controller-interface-bypass, 2),
502 __K8MASK(dram-controller-queue-bypass, 3),
506 /* nb sized commands */
507 static const struct pmc_masks k8_mask_nsc[] = {
508 __K8MASK(nonpostwrszbyte, 0),
509 __K8MASK(nonpostwrszdword, 1),
510 __K8MASK(postwrszbyte, 2),
511 __K8MASK(postwrszdword, 3),
512 __K8MASK(rdszbyte, 4),
513 __K8MASK(rdszdword, 5),
514 __K8MASK(rdmodwr, 6),
518 /* nb probe result */
519 static const struct pmc_masks k8_mask_npr[] = {
520 __K8MASK(probe-miss, 0),
521 __K8MASK(probe-hit, 1),
522 __K8MASK(probe-hit-dirty-no-memory-cancel, 2),
523 __K8MASK(probe-hit-dirty-with-memory-cancel, 3),
527 /* nb hypertransport bus bandwidth */
528 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
529 __K8MASK(command, 0),
531 __K8MASK(buffer-release, 2),
538 #define K8_KW_COUNT "count"
539 #define K8_KW_EDGE "edge"
540 #define K8_KW_INV "inv"
541 #define K8_KW_MASK "mask"
542 #define K8_KW_OS "os"
543 #define K8_KW_USR "usr"
546 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
547 struct pmc_op_pmcallocate *pmc_config)
553 const struct pmc_masks *pm, *pmask;
555 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
556 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
561 #define __K8SETMASK(M) pmask = k8_mask_##M
563 /* setup parsing tables */
565 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
568 case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
571 case PMC_EV_K8_LS_LOCKED_OPERATION:
574 case PMC_EV_K8_DC_REFILL_FROM_L2:
575 case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
576 case PMC_EV_K8_DC_COPYBACK:
579 case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
582 case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
585 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
588 case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
591 case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
594 case PMC_EV_K8_BU_FILL_INTO_L2:
597 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
600 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
603 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
606 case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
609 case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
612 case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
615 case PMC_EV_K8_NB_SIZED_COMMANDS:
618 case PMC_EV_K8_NB_PROBE_RESULT:
621 case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
622 case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
623 case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
628 break; /* no options defined */
631 while ((p = strsep(&ctrspec, ",")) != NULL) {
632 if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
634 if (*++q == '\0') /* skip '=' */
637 count = strtol(q, &e, 0);
638 if (e == q || *e != '\0')
641 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
642 pmc_config->pm_md.pm_amd.pm_amd_config |=
643 AMD_PMC_TO_COUNTER(count);
645 } else if (KWMATCH(p, K8_KW_EDGE)) {
646 pmc_config->pm_caps |= PMC_CAP_EDGE;
647 } else if (KWMATCH(p, K8_KW_INV)) {
648 pmc_config->pm_caps |= PMC_CAP_INVERT;
649 } else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
650 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
652 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
653 } else if (KWMATCH(p, K8_KW_OS)) {
654 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
655 } else if (KWMATCH(p, K8_KW_USR)) {
656 pmc_config->pm_caps |= PMC_CAP_USER;
661 /* other post processing */
663 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
664 case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
665 case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
666 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
667 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
668 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
669 /* XXX only available in rev B and later */
671 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
672 /* XXX only available in rev C and later */
674 case PMC_EV_K8_LS_LOCKED_OPERATION:
675 /* XXX CPU Rev A,B evmask is to be zero */
676 if (evmask & (evmask - 1)) /* > 1 bit set */
679 evmask = 0x01; /* Rev C and later: #instrs */
680 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
684 if (evmask == 0 && pmask != NULL) {
685 for (pm = pmask; pm->pm_name; pm++)
686 evmask |= pm->pm_value;
687 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
691 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
692 pmc_config->pm_md.pm_amd.pm_amd_config =
693 AMD_PMC_TO_UNITMASK(evmask);
700 #if defined(__i386__) || defined(__amd64__)
702 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
703 struct pmc_op_pmcallocate *pmc_config)
705 if (pe != PMC_EV_TSC_TSC)
708 /* TSC events must be unqualified. */
709 if (ctrspec && *ctrspec != '\0')
712 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
713 pmc_config->pm_caps |= PMC_CAP_READ;
719 static struct pmc_event_alias generic_aliases[] = {
720 EV_ALIAS("instructions", "SOFT-CLOCK.HARD"),
725 soft_allocate_pmc(enum pmc_event pe, char *ctrspec,
726 struct pmc_op_pmcallocate *pmc_config)
731 if ((int)pe < PMC_EV_SOFT_FIRST || (int)pe > PMC_EV_SOFT_LAST)
734 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
739 static struct pmc_event_alias cortex_a8_aliases[] = {
740 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
741 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
742 EV_ALIAS("instructions", "INSTR_EXECUTED"),
746 static struct pmc_event_alias cortex_a9_aliases[] = {
747 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
748 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
749 EV_ALIAS("instructions", "INSTR_EXECUTED"),
754 armv7_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
755 struct pmc_op_pmcallocate *pmc_config __unused)
766 #if defined(__aarch64__)
767 static struct pmc_event_alias cortex_a53_aliases[] = {
770 static struct pmc_event_alias cortex_a57_aliases[] = {
773 static struct pmc_event_alias cortex_a76_aliases[] = {
778 arm64_allocate_pmc(enum pmc_event pe, char *ctrspec,
779 struct pmc_op_pmcallocate *pmc_config)
783 while ((p = strsep(&ctrspec, ",")) != NULL) {
784 if (KWMATCH(p, "os"))
785 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
786 else if (KWMATCH(p, "usr"))
787 pmc_config->pm_caps |= PMC_CAP_USER;
796 cmn600_pmu_allocate_pmc(enum pmc_event pe, char *ctrspec,
797 struct pmc_op_pmcallocate *pmc_config)
799 uint32_t nodeid, occupancy, xpport, xpchannel;
802 char *xpport_names[] = { "East", "West", "North", "South", "devport0",
804 char *xpchannel_names[] = { "REQ", "RSP", "SNP", "DAT" };
806 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
807 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
808 pmc_config->pm_md.pm_cmn600.pma_cmn600_config = 0;
810 * CMN600 extra fields:
811 * * nodeid - node coordinates x[2-3],y[2-3],p[1],s[2]
812 * width of x and y fields depend on matrix size.
813 * * occupancy - numeric value to select desired filter.
814 * * xpport - East, West, North, South, devport0, devport1 (or 0, 1, ..., 5)
815 * * xpchannel - REQ, RSP, SNP, DAT (or 0, 1, 2, 3)
818 while ((p = strsep(&ctrspec, ",")) != NULL) {
819 if (KWPREFIXMATCH(p, "nodeid=")) {
821 if (*++q == '\0') /* skip '=' */
824 nodeid = strtol(q, &e, 0);
825 if (e == q || *e != '\0')
828 pmc_config->pm_md.pm_cmn600.pma_cmn600_nodeid |= nodeid;
830 } else if (KWPREFIXMATCH(p, "occupancy=")) {
832 if (*++q == '\0') /* skip '=' */
835 occupancy = strtol(q, &e, 0);
836 if (e == q || *e != '\0')
839 pmc_config->pm_md.pm_cmn600.pma_cmn600_occupancy = occupancy;
840 } else if (KWPREFIXMATCH(p, "xpport=")) {
842 if (*++q == '\0') /* skip '=' */
845 xpport = strtol(q, &e, 0);
846 if (e == q || *e != '\0') {
847 for (i = 0; i < nitems(xpport_names); i++) {
848 if (strcasecmp(xpport_names[i], q) == 0) {
853 if (i == nitems(xpport_names))
857 pmc_config->pm_md.pm_cmn600.pma_cmn600_config |= xpport << 2;
858 } else if (KWPREFIXMATCH(p, "xpchannel=")) {
860 if (*++q == '\0') /* skip '=' */
863 xpchannel = strtol(q, &e, 0);
864 if (e == q || *e != '\0') {
865 for (i = 0; i < nitems(xpchannel_names); i++) {
866 if (strcasecmp(xpchannel_names[i], q) == 0) {
871 if (i == nitems(xpchannel_names))
875 pmc_config->pm_md.pm_cmn600.pma_cmn600_config |= xpchannel << 5;
884 dmc620_pmu_allocate_pmc(enum pmc_event pe, char *ctrspec,
885 struct pmc_op_pmcallocate *pmc_config)
888 uint64_t match, mask;
891 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
892 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
893 pmc_config->pm_md.pm_dmc620.pm_dmc620_config = 0;
895 while ((p = strsep(&ctrspec, ",")) != NULL) {
896 if (KWPREFIXMATCH(p, "count=")) {
898 if (*++q == '\0') /* skip '=' */
901 count = strtol(q, &e, 0);
902 if (e == q || *e != '\0')
905 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
906 pmc_config->pm_md.pm_dmc620.pm_dmc620_config |= count;
908 } else if (KWMATCH(p, "inv")) {
909 pmc_config->pm_caps |= PMC_CAP_INVERT;
910 } else if (KWPREFIXMATCH(p, "match=")) {
911 match = strtol(q, &e, 0);
912 if (e == q || *e != '\0')
915 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
916 pmc_config->pm_md.pm_dmc620.pm_dmc620_match = match;
917 } else if (KWPREFIXMATCH(p, "mask=")) {
919 if (*++q == '\0') /* skip '=' */
922 mask = strtol(q, &e, 0);
923 if (e == q || *e != '\0')
926 pmc_config->pm_md.pm_dmc620.pm_dmc620_mask = mask;
927 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
936 #if defined(__powerpc__)
938 static struct pmc_event_alias ppc7450_aliases[] = {
939 EV_ALIAS("instructions", "INSTR_COMPLETED"),
940 EV_ALIAS("branches", "BRANCHES_COMPLETED"),
941 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCHES"),
945 static struct pmc_event_alias ppc970_aliases[] = {
946 EV_ALIAS("instructions", "INSTR_COMPLETED"),
947 EV_ALIAS("cycles", "CYCLES"),
951 static struct pmc_event_alias e500_aliases[] = {
952 EV_ALIAS("instructions", "INSTR_COMPLETED"),
953 EV_ALIAS("cycles", "CYCLES"),
957 #define POWERPC_KW_OS "os"
958 #define POWERPC_KW_USR "usr"
959 #define POWERPC_KW_ANYTHREAD "anythread"
962 powerpc_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
963 struct pmc_op_pmcallocate *pmc_config __unused)
969 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
971 while ((p = strsep(&ctrspec, ",")) != NULL) {
972 if (KWMATCH(p, POWERPC_KW_OS))
973 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
974 else if (KWMATCH(p, POWERPC_KW_USR))
975 pmc_config->pm_caps |= PMC_CAP_USER;
976 else if (KWMATCH(p, POWERPC_KW_ANYTHREAD))
977 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
985 #endif /* __powerpc__ */
989 * Match an event name `name' with its canonical form.
991 * Matches are case insensitive and spaces, periods, underscores and
992 * hyphen characters are considered to match each other.
994 * Returns 1 for a match, 0 otherwise.
998 pmc_match_event_name(const char *name, const char *canonicalname)
1001 const unsigned char *c, *n;
1003 c = (const unsigned char *) canonicalname;
1004 n = (const unsigned char *) name;
1006 for (; (nc = *n) && (cc = *c); n++, c++) {
1008 if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
1009 (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
1012 if (toupper(nc) == toupper(cc))
1019 if (*n == '\0' && *c == '\0')
1026 * Match an event name against all the event named supported by a
1029 * Returns an event descriptor pointer on match or NULL otherwise.
1031 static const struct pmc_event_descr *
1032 pmc_match_event_class(const char *name,
1033 const struct pmc_class_descr *pcd)
1036 const struct pmc_event_descr *ev;
1038 ev = pcd->pm_evc_event_table;
1039 for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
1040 if (pmc_match_event_name(name, ev->pm_ev_name))
1051 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
1052 uint32_t flags, int cpu, pmc_id_t *pmcid,
1057 char *r, *spec_copy;
1058 const char *ctrname;
1059 const struct pmc_event_descr *ev;
1060 const struct pmc_event_alias *alias;
1061 struct pmc_op_pmcallocate pmc_config;
1062 const struct pmc_class_descr *pcd;
1067 if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
1068 mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
1072 bzero(&pmc_config, sizeof(pmc_config));
1073 pmc_config.pm_cpu = cpu;
1074 pmc_config.pm_mode = mode;
1075 pmc_config.pm_flags = flags;
1076 pmc_config.pm_count = count;
1077 if (PMC_IS_SAMPLING_MODE(mode))
1078 pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
1081 * Try to pull the raw event ID directly from the pmu-events table. If
1082 * this is unsupported on the platform, or the event is not found,
1083 * continue with searching the regular event tables.
1085 r = spec_copy = strdup(ctrspec);
1086 ctrname = strsep(&r, ",");
1087 if (pmc_pmu_enabled()) {
1088 if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0)
1091 /* Otherwise, reset any changes */
1092 pmc_config.pm_ev = 0;
1093 pmc_config.pm_caps = 0;
1094 pmc_config.pm_class = 0;
1099 /* replace an event alias with the canonical event specifier */
1100 if (pmc_mdep_event_aliases)
1101 for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
1102 if (!strcasecmp(ctrspec, alias->pm_alias)) {
1103 spec_copy = strdup(alias->pm_spec);
1107 if (spec_copy == NULL)
1108 spec_copy = strdup(ctrspec);
1111 ctrname = strsep(&r, ",");
1114 * If a explicit class prefix was given by the user, restrict the
1115 * search for the event to the specified PMC class.
1118 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
1119 pcd = pmc_class_table[n];
1120 if (pcd != NULL && strncasecmp(ctrname, pcd->pm_evc_name,
1121 pcd->pm_evc_name_size) == 0) {
1122 if ((ev = pmc_match_event_class(ctrname +
1123 pcd->pm_evc_name_size, pcd)) == NULL) {
1132 * Otherwise, search for this event in all compatible PMC
1135 for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
1136 pcd = pmc_class_table[n];
1138 ev = pmc_match_event_class(ctrname, pcd);
1146 pmc_config.pm_ev = ev->pm_ev_code;
1147 pmc_config.pm_class = pcd->pm_evc_class;
1149 if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
1155 if (PMC_CALL(PMCALLOCATE, &pmc_config) == 0) {
1156 *pmcid = pmc_config.pm_pmcid;
1167 pmc_attach(pmc_id_t pmc, pid_t pid)
1169 struct pmc_op_pmcattach pmc_attach_args;
1171 pmc_attach_args.pm_pmc = pmc;
1172 pmc_attach_args.pm_pid = pid;
1174 return (PMC_CALL(PMCATTACH, &pmc_attach_args));
1178 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
1183 cl = PMC_ID_TO_CLASS(pmcid);
1184 for (i = 0; i < cpu_info.pm_nclass; i++)
1185 if (cpu_info.pm_classes[i].pm_class == cl) {
1186 *caps = cpu_info.pm_classes[i].pm_caps;
1194 pmc_configure_logfile(int fd)
1196 struct pmc_op_configurelog cla;
1199 if (PMC_CALL(CONFIGURELOG, &cla) < 0)
1205 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
1207 if (pmc_syscall == -1) {
1217 pmc_detach(pmc_id_t pmc, pid_t pid)
1219 struct pmc_op_pmcattach pmc_detach_args;
1221 pmc_detach_args.pm_pmc = pmc;
1222 pmc_detach_args.pm_pid = pid;
1223 return (PMC_CALL(PMCDETACH, &pmc_detach_args));
1227 pmc_disable(int cpu, int pmc)
1229 struct pmc_op_pmcadmin ssa;
1233 ssa.pm_state = PMC_STATE_DISABLED;
1234 return (PMC_CALL(PMCADMIN, &ssa));
1238 pmc_enable(int cpu, int pmc)
1240 struct pmc_op_pmcadmin ssa;
1244 ssa.pm_state = PMC_STATE_FREE;
1245 return (PMC_CALL(PMCADMIN, &ssa));
1249 * Return a list of events known to a given PMC class. 'cl' is the
1250 * PMC class identifier, 'eventnames' is the returned list of 'const
1251 * char *' pointers pointing to the names of the events. 'nevents' is
1252 * the number of event name pointers returned.
1254 * The space for 'eventnames' is allocated using malloc(3). The caller
1255 * is responsible for freeing this space when done.
1258 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
1263 const struct pmc_event_descr *ev;
1268 ev = iaf_event_table;
1269 count = PMC_EVENT_TABLE_SIZE(iaf);
1272 ev = tsc_event_table;
1273 count = PMC_EVENT_TABLE_SIZE(tsc);
1276 ev = k8_event_table;
1277 count = PMC_EVENT_TABLE_SIZE(k8);
1279 case PMC_CLASS_ARMV7:
1280 switch (cpu_info.pm_cputype) {
1282 case PMC_CPU_ARMV7_CORTEX_A8:
1283 ev = cortex_a8_event_table;
1284 count = PMC_EVENT_TABLE_SIZE(cortex_a8);
1286 case PMC_CPU_ARMV7_CORTEX_A9:
1287 ev = cortex_a9_event_table;
1288 count = PMC_EVENT_TABLE_SIZE(cortex_a9);
1292 case PMC_CLASS_ARMV8:
1293 switch (cpu_info.pm_cputype) {
1295 case PMC_CPU_ARMV8_CORTEX_A53:
1296 ev = cortex_a53_event_table;
1297 count = PMC_EVENT_TABLE_SIZE(cortex_a53);
1299 case PMC_CPU_ARMV8_CORTEX_A57:
1300 ev = cortex_a57_event_table;
1301 count = PMC_EVENT_TABLE_SIZE(cortex_a57);
1303 case PMC_CPU_ARMV8_CORTEX_A76:
1304 ev = cortex_a76_event_table;
1305 count = PMC_EVENT_TABLE_SIZE(cortex_a76);
1309 case PMC_CLASS_CMN600_PMU:
1310 ev = cmn600_pmu_event_table;
1311 count = PMC_EVENT_TABLE_SIZE(cmn600_pmu);
1313 case PMC_CLASS_DMC620_PMU_CD2:
1314 ev = dmc620_pmu_cd2_event_table;
1315 count = PMC_EVENT_TABLE_SIZE(dmc620_pmu_cd2);
1317 case PMC_CLASS_DMC620_PMU_C:
1318 ev = dmc620_pmu_c_event_table;
1319 count = PMC_EVENT_TABLE_SIZE(dmc620_pmu_c);
1321 case PMC_CLASS_PPC7450:
1322 ev = ppc7450_event_table;
1323 count = PMC_EVENT_TABLE_SIZE(ppc7450);
1325 case PMC_CLASS_PPC970:
1326 ev = ppc970_event_table;
1327 count = PMC_EVENT_TABLE_SIZE(ppc970);
1329 case PMC_CLASS_E500:
1330 ev = e500_event_table;
1331 count = PMC_EVENT_TABLE_SIZE(e500);
1333 case PMC_CLASS_SOFT:
1334 ev = soft_event_table;
1335 count = soft_event_info.pm_nevent;
1342 if ((names = malloc(count * sizeof(const char *))) == NULL)
1345 *eventnames = names;
1348 for (;count--; ev++, names++)
1349 *names = ev->pm_ev_name;
1355 pmc_flush_logfile(void)
1357 return (PMC_CALL(FLUSHLOG,0));
1361 pmc_close_logfile(void)
1363 return (PMC_CALL(CLOSELOG,0));
1367 pmc_get_driver_stats(struct pmc_driverstats *ds)
1369 struct pmc_op_getdriverstats gms;
1371 if (PMC_CALL(GETDRIVERSTATS, &gms) < 0)
1374 /* copy out fields in the current userland<->library interface */
1375 ds->pm_intr_ignored = gms.pm_intr_ignored;
1376 ds->pm_intr_processed = gms.pm_intr_processed;
1377 ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
1378 ds->pm_syscalls = gms.pm_syscalls;
1379 ds->pm_syscall_errors = gms.pm_syscall_errors;
1380 ds->pm_buffer_requests = gms.pm_buffer_requests;
1381 ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
1382 ds->pm_log_sweeps = gms.pm_log_sweeps;
1387 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
1389 struct pmc_op_getmsr gm;
1392 if (PMC_CALL(PMCGETMSR, &gm) < 0)
1401 int error, pmc_mod_id;
1403 uint32_t abi_version;
1404 struct module_stat pmc_modstat;
1405 struct pmc_op_getcpuinfo op_cpu_info;
1407 if (pmc_syscall != -1) /* already inited */
1410 /* retrieve the system call number from the KLD */
1411 if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
1414 pmc_modstat.version = sizeof(struct module_stat);
1415 if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
1418 pmc_syscall = pmc_modstat.data.intval;
1420 /* check the kernel module's ABI against our compiled-in version */
1421 abi_version = PMC_VERSION;
1422 if (PMC_CALL(GETMODULEVERSION, &abi_version) < 0)
1423 return (pmc_syscall = -1);
1425 /* ignore patch & minor numbers for the comparison */
1426 if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
1427 errno = EPROGMISMATCH;
1428 return (pmc_syscall = -1);
1431 bzero(&op_cpu_info, sizeof(op_cpu_info));
1432 if (PMC_CALL(GETCPUINFO, &op_cpu_info) < 0)
1433 return (pmc_syscall = -1);
1435 cpu_info.pm_cputype = op_cpu_info.pm_cputype;
1436 cpu_info.pm_ncpu = op_cpu_info.pm_ncpu;
1437 cpu_info.pm_npmc = op_cpu_info.pm_npmc;
1438 cpu_info.pm_nclass = op_cpu_info.pm_nclass;
1439 for (n = 0; n < op_cpu_info.pm_nclass; n++)
1440 memcpy(&cpu_info.pm_classes[n], &op_cpu_info.pm_classes[n],
1441 sizeof(cpu_info.pm_classes[n]));
1443 pmc_class_table = malloc(PMC_CLASS_TABLE_SIZE *
1444 sizeof(struct pmc_class_descr *));
1446 if (pmc_class_table == NULL)
1449 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++)
1450 pmc_class_table[n] = NULL;
1453 * Get soft events list.
1455 soft_event_info.pm_class = PMC_CLASS_SOFT;
1456 if (PMC_CALL(GETDYNEVENTINFO, &soft_event_info) < 0)
1457 return (pmc_syscall = -1);
1459 /* Map soft events to static list. */
1460 for (n = 0; n < soft_event_info.pm_nevent; n++) {
1461 soft_event_table[n].pm_ev_name =
1462 soft_event_info.pm_events[n].pm_ev_name;
1463 soft_event_table[n].pm_ev_code =
1464 soft_event_info.pm_events[n].pm_ev_code;
1466 soft_class_table_descr.pm_evc_event_table_size = \
1467 soft_event_info.pm_nevent;
1468 soft_class_table_descr.pm_evc_event_table = \
1472 * Fill in the class table.
1476 /* Fill soft events information. */
1477 pmc_class_table[n++] = &soft_class_table_descr;
1479 #if defined(__aarch64__)
1480 pmc_class_table[n++] = &cmn600_pmu_class_table_descr;
1481 pmc_class_table[n++] = &dmc620_pmu_cd2_class_table_descr;
1482 pmc_class_table[n++] = &dmc620_pmu_c_class_table_descr;
1484 #if defined(__amd64__) || defined(__i386__)
1485 if (cpu_info.pm_cputype != PMC_CPU_GENERIC)
1486 pmc_class_table[n++] = &tsc_class_table_descr;
1489 #define PMC_MDEP_INIT(C) pmc_mdep_event_aliases = C##_aliases
1491 /* Configure the event name parser. */
1492 switch (cpu_info.pm_cputype) {
1493 #if defined(__amd64__) || defined(__i386__)
1494 case PMC_CPU_AMD_K8:
1496 pmc_class_table[n] = &k8_class_table_descr;
1499 case PMC_CPU_GENERIC:
1500 PMC_MDEP_INIT(generic);
1502 #if defined(__arm__)
1503 case PMC_CPU_ARMV7_CORTEX_A8:
1504 PMC_MDEP_INIT(cortex_a8);
1505 pmc_class_table[n] = &cortex_a8_class_table_descr;
1507 case PMC_CPU_ARMV7_CORTEX_A9:
1508 PMC_MDEP_INIT(cortex_a9);
1509 pmc_class_table[n] = &cortex_a9_class_table_descr;
1512 #if defined(__aarch64__)
1513 case PMC_CPU_ARMV8_CORTEX_A53:
1514 PMC_MDEP_INIT(cortex_a53);
1515 pmc_class_table[n] = &cortex_a53_class_table_descr;
1517 case PMC_CPU_ARMV8_CORTEX_A57:
1518 PMC_MDEP_INIT(cortex_a57);
1519 pmc_class_table[n] = &cortex_a57_class_table_descr;
1521 case PMC_CPU_ARMV8_CORTEX_A76:
1522 PMC_MDEP_INIT(cortex_a76);
1523 pmc_class_table[n] = &cortex_a76_class_table_descr;
1526 #if defined(__powerpc__)
1527 case PMC_CPU_PPC_7450:
1528 PMC_MDEP_INIT(ppc7450);
1529 pmc_class_table[n] = &ppc7450_class_table_descr;
1531 case PMC_CPU_PPC_970:
1532 PMC_MDEP_INIT(ppc970);
1533 pmc_class_table[n] = &ppc970_class_table_descr;
1535 case PMC_CPU_PPC_E500:
1536 PMC_MDEP_INIT(e500);
1537 pmc_class_table[n] = &e500_class_table_descr;
1542 * Some kind of CPU this version of the library knows nothing
1543 * about. This shouldn't happen since the abi version check
1544 * should have caught this.
1546 #if defined(__amd64__) || defined(__i386__) || defined(__powerpc64__)
1550 return (pmc_syscall = -1);
1557 pmc_name_of_capability(enum pmc_caps cap)
1562 * 'cap' should have a single bit set and should be in
1565 if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
1566 cap > PMC_CAP_LAST) {
1572 return (pmc_capability_names[i - 1]);
1576 pmc_name_of_class(enum pmc_class pc)
1580 for (n = 0; n < PMC_TABLE_SIZE(pmc_class_names); n++)
1581 if (pc == pmc_class_names[n].pm_class)
1582 return (pmc_class_names[n].pm_name);
1589 pmc_name_of_cputype(enum pmc_cputype cp)
1593 for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
1594 if (cp == pmc_cputype_names[n].pm_cputype)
1595 return (pmc_cputype_names[n].pm_name);
1602 pmc_name_of_disposition(enum pmc_disp pd)
1604 if ((int) pd >= PMC_DISP_FIRST &&
1605 pd <= PMC_DISP_LAST)
1606 return (pmc_disposition_names[pd]);
1613 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
1615 const struct pmc_event_descr *ev, *evfence;
1617 ev = evfence = NULL;
1618 if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
1619 ev = k8_event_table;
1620 evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
1622 } else if (pe >= PMC_EV_ARMV7_FIRST && pe <= PMC_EV_ARMV7_LAST) {
1624 case PMC_CPU_ARMV7_CORTEX_A8:
1625 ev = cortex_a8_event_table;
1626 evfence = cortex_a8_event_table + PMC_EVENT_TABLE_SIZE(cortex_a8);
1628 case PMC_CPU_ARMV7_CORTEX_A9:
1629 ev = cortex_a9_event_table;
1630 evfence = cortex_a9_event_table + PMC_EVENT_TABLE_SIZE(cortex_a9);
1632 default: /* Unknown CPU type. */
1635 } else if (pe >= PMC_EV_ARMV8_FIRST && pe <= PMC_EV_ARMV8_LAST) {
1637 case PMC_CPU_ARMV8_CORTEX_A53:
1638 ev = cortex_a53_event_table;
1639 evfence = cortex_a53_event_table + PMC_EVENT_TABLE_SIZE(cortex_a53);
1641 case PMC_CPU_ARMV8_CORTEX_A57:
1642 ev = cortex_a57_event_table;
1643 evfence = cortex_a57_event_table + PMC_EVENT_TABLE_SIZE(cortex_a57);
1645 case PMC_CPU_ARMV8_CORTEX_A76:
1646 ev = cortex_a76_event_table;
1647 evfence = cortex_a76_event_table + PMC_EVENT_TABLE_SIZE(cortex_a76);
1649 default: /* Unknown CPU type. */
1652 } else if (pe >= PMC_EV_CMN600_PMU_FIRST &&
1653 pe <= PMC_EV_CMN600_PMU_LAST) {
1654 ev = cmn600_pmu_event_table;
1655 evfence = cmn600_pmu_event_table +
1656 PMC_EVENT_TABLE_SIZE(cmn600_pmu);
1657 } else if (pe >= PMC_EV_DMC620_PMU_CD2_FIRST &&
1658 pe <= PMC_EV_DMC620_PMU_CD2_LAST) {
1659 ev = dmc620_pmu_cd2_event_table;
1660 evfence = dmc620_pmu_cd2_event_table +
1661 PMC_EVENT_TABLE_SIZE(dmc620_pmu_cd2);
1662 } else if (pe >= PMC_EV_DMC620_PMU_C_FIRST &&
1663 pe <= PMC_EV_DMC620_PMU_C_LAST) {
1664 ev = dmc620_pmu_c_event_table;
1665 evfence = dmc620_pmu_c_event_table +
1666 PMC_EVENT_TABLE_SIZE(dmc620_pmu_c);
1667 } else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
1668 ev = ppc7450_event_table;
1669 evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450);
1670 } else if (pe >= PMC_EV_PPC970_FIRST && pe <= PMC_EV_PPC970_LAST) {
1671 ev = ppc970_event_table;
1672 evfence = ppc970_event_table + PMC_EVENT_TABLE_SIZE(ppc970);
1673 } else if (pe >= PMC_EV_E500_FIRST && pe <= PMC_EV_E500_LAST) {
1674 ev = e500_event_table;
1675 evfence = e500_event_table + PMC_EVENT_TABLE_SIZE(e500);
1676 } else if (pe == PMC_EV_TSC_TSC) {
1677 ev = tsc_event_table;
1678 evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
1679 } else if ((int)pe >= PMC_EV_SOFT_FIRST && (int)pe <= PMC_EV_SOFT_LAST) {
1680 ev = soft_event_table;
1681 evfence = soft_event_table + soft_event_info.pm_nevent;
1684 for (; ev != evfence; ev++)
1685 if (pe == ev->pm_ev_code)
1686 return (ev->pm_ev_name);
1692 pmc_name_of_event(enum pmc_event pe)
1696 if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
1704 pmc_name_of_mode(enum pmc_mode pm)
1706 if ((int) pm >= PMC_MODE_FIRST &&
1707 pm <= PMC_MODE_LAST)
1708 return (pmc_mode_names[pm]);
1715 pmc_name_of_state(enum pmc_state ps)
1717 if ((int) ps >= PMC_STATE_FIRST &&
1718 ps <= PMC_STATE_LAST)
1719 return (pmc_state_names[ps]);
1728 if (pmc_syscall == -1) {
1733 return (cpu_info.pm_ncpu);
1739 if (pmc_syscall == -1) {
1744 if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
1749 return (cpu_info.pm_npmc);
1753 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
1756 struct pmc_op_getpmcinfo *pmci;
1758 if ((npmc = pmc_npmc(cpu)) < 0)
1761 nbytes = sizeof(struct pmc_op_getpmcinfo) +
1762 npmc * sizeof(struct pmc_info);
1764 if ((pmci = calloc(1, nbytes)) == NULL)
1769 if (PMC_CALL(GETPMCINFO, pmci) < 0) {
1774 /* kernel<->library, library<->userland interfaces are identical */
1775 *ppmci = (struct pmc_pmcinfo *) pmci;
1780 pmc_read(pmc_id_t pmc, pmc_value_t *value)
1782 struct pmc_op_pmcrw pmc_read_op;
1784 pmc_read_op.pm_pmcid = pmc;
1785 pmc_read_op.pm_flags = PMC_F_OLDVALUE;
1786 pmc_read_op.pm_value = -1;
1788 if (PMC_CALL(PMCRW, &pmc_read_op) < 0)
1791 *value = pmc_read_op.pm_value;
1796 pmc_release(pmc_id_t pmc)
1798 struct pmc_op_simple pmc_release_args;
1800 pmc_release_args.pm_pmcid = pmc;
1801 return (PMC_CALL(PMCRELEASE, &pmc_release_args));
1805 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
1807 struct pmc_op_pmcrw pmc_rw_op;
1809 pmc_rw_op.pm_pmcid = pmc;
1810 pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
1811 pmc_rw_op.pm_value = newvalue;
1813 if (PMC_CALL(PMCRW, &pmc_rw_op) < 0)
1816 *oldvaluep = pmc_rw_op.pm_value;
1821 pmc_set(pmc_id_t pmc, pmc_value_t value)
1823 struct pmc_op_pmcsetcount sc;
1826 sc.pm_count = value;
1828 if (PMC_CALL(PMCSETCOUNT, &sc) < 0)
1834 pmc_start(pmc_id_t pmc)
1836 struct pmc_op_simple pmc_start_args;
1838 pmc_start_args.pm_pmcid = pmc;
1839 return (PMC_CALL(PMCSTART, &pmc_start_args));
1843 pmc_stop(pmc_id_t pmc)
1845 struct pmc_op_simple pmc_stop_args;
1847 pmc_stop_args.pm_pmcid = pmc;
1848 return (PMC_CALL(PMCSTOP, &pmc_stop_args));
1852 pmc_width(pmc_id_t pmcid, uint32_t *width)
1857 cl = PMC_ID_TO_CLASS(pmcid);
1858 for (i = 0; i < cpu_info.pm_nclass; i++)
1859 if (cpu_info.pm_classes[i].pm_class == cl) {
1860 *width = cpu_info.pm_classes[i].pm_width;
1868 pmc_write(pmc_id_t pmc, pmc_value_t value)
1870 struct pmc_op_pmcrw pmc_write_op;
1872 pmc_write_op.pm_pmcid = pmc;
1873 pmc_write_op.pm_flags = PMC_F_NEWVALUE;
1874 pmc_write_op.pm_value = value;
1875 return (PMC_CALL(PMCRW, &pmc_write_op));
1879 pmc_writelog(uint32_t userdata)
1881 struct pmc_op_writelog wl;
1883 wl.pm_userdata = userdata;
1884 return (PMC_CALL(WRITELOG, &wl));