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 #if defined(__XSCALE__)
63 static int xscale_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
64 struct pmc_op_pmcallocate *_pmc_config);
66 static int armv7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
67 struct pmc_op_pmcallocate *_pmc_config);
69 #if defined(__aarch64__)
70 static int arm64_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
71 struct pmc_op_pmcallocate *_pmc_config);
74 static int mips_allocate_pmc(enum pmc_event _pe, char* ctrspec,
75 struct pmc_op_pmcallocate *_pmc_config);
77 static int soft_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
78 struct pmc_op_pmcallocate *_pmc_config);
80 #if defined(__powerpc__)
81 static int powerpc_allocate_pmc(enum pmc_event _pe, char* ctrspec,
82 struct pmc_op_pmcallocate *_pmc_config);
83 #endif /* __powerpc__ */
85 #define PMC_CALL(cmd, params) \
86 syscall(pmc_syscall, PMC_OP_##cmd, (params))
89 * Event aliases provide a way for the user to ask for generic events
90 * like "cache-misses", or "instructions-retired". These aliases are
91 * mapped to the appropriate canonical event descriptions using a
94 struct pmc_event_alias {
99 static const struct pmc_event_alias *pmc_mdep_event_aliases;
102 * The pmc_event_descr structure maps symbolic names known to the user
103 * to integer codes used by the PMC KLD.
105 struct pmc_event_descr {
106 const char *pm_ev_name;
107 enum pmc_event pm_ev_code;
111 * The pmc_class_descr structure maps class name prefixes for
112 * event names to event tables and other PMC class data.
114 struct pmc_class_descr {
115 const char *pm_evc_name;
116 size_t pm_evc_name_size;
117 enum pmc_class pm_evc_class;
118 const struct pmc_event_descr *pm_evc_event_table;
119 size_t pm_evc_event_table_size;
120 int (*pm_evc_allocate_pmc)(enum pmc_event _pe,
121 char *_ctrspec, struct pmc_op_pmcallocate *_pa);
124 #define PMC_TABLE_SIZE(N) (sizeof(N)/sizeof(N[0]))
125 #define PMC_EVENT_TABLE_SIZE(N) PMC_TABLE_SIZE(N##_event_table)
128 #define __PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
131 * PMC_CLASSDEP_TABLE(NAME, CLASS)
133 * Define a table mapping event names and aliases to HWPMC event IDs.
135 #define PMC_CLASSDEP_TABLE(N, C) \
136 static const struct pmc_event_descr N##_event_table[] = \
141 PMC_CLASSDEP_TABLE(iaf, IAF);
142 PMC_CLASSDEP_TABLE(k8, K8);
143 PMC_CLASSDEP_TABLE(xscale, XSCALE);
144 PMC_CLASSDEP_TABLE(armv7, ARMV7);
145 PMC_CLASSDEP_TABLE(armv8, ARMV8);
146 PMC_CLASSDEP_TABLE(mips24k, MIPS24K);
147 PMC_CLASSDEP_TABLE(mips74k, MIPS74K);
148 PMC_CLASSDEP_TABLE(octeon, OCTEON);
149 PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
150 PMC_CLASSDEP_TABLE(ppc970, PPC970);
151 PMC_CLASSDEP_TABLE(e500, E500);
153 static struct pmc_event_descr soft_event_table[PMC_EV_DYN_COUNT];
155 #undef __PMC_EV_ALIAS
156 #define __PMC_EV_ALIAS(N,CODE) { N, PMC_EV_##CODE },
158 static const struct pmc_event_descr cortex_a8_event_table[] =
160 __PMC_EV_ALIAS_ARMV7_CORTEX_A8()
163 static const struct pmc_event_descr cortex_a9_event_table[] =
165 __PMC_EV_ALIAS_ARMV7_CORTEX_A9()
168 static const struct pmc_event_descr cortex_a53_event_table[] =
170 __PMC_EV_ALIAS_ARMV8_CORTEX_A53()
173 static const struct pmc_event_descr cortex_a57_event_table[] =
175 __PMC_EV_ALIAS_ARMV8_CORTEX_A57()
179 * PMC_MDEP_TABLE(NAME, PRIMARYCLASS, ADDITIONAL_CLASSES...)
181 * Map a CPU to the PMC classes it supports.
183 #define PMC_MDEP_TABLE(N,C,...) \
184 static const enum pmc_class N##_pmc_classes[] = { \
185 PMC_CLASS_##C, __VA_ARGS__ \
188 PMC_MDEP_TABLE(k8, K8, PMC_CLASS_SOFT, PMC_CLASS_TSC);
189 PMC_MDEP_TABLE(xscale, XSCALE, PMC_CLASS_SOFT, PMC_CLASS_XSCALE);
190 PMC_MDEP_TABLE(cortex_a8, ARMV7, PMC_CLASS_SOFT, PMC_CLASS_ARMV7);
191 PMC_MDEP_TABLE(cortex_a9, ARMV7, PMC_CLASS_SOFT, PMC_CLASS_ARMV7);
192 PMC_MDEP_TABLE(cortex_a53, ARMV8, PMC_CLASS_SOFT, PMC_CLASS_ARMV8);
193 PMC_MDEP_TABLE(cortex_a57, ARMV8, PMC_CLASS_SOFT, PMC_CLASS_ARMV8);
194 PMC_MDEP_TABLE(mips24k, MIPS24K, PMC_CLASS_SOFT, PMC_CLASS_MIPS24K);
195 PMC_MDEP_TABLE(mips74k, MIPS74K, PMC_CLASS_SOFT, PMC_CLASS_MIPS74K);
196 PMC_MDEP_TABLE(octeon, OCTEON, PMC_CLASS_SOFT, PMC_CLASS_OCTEON);
197 PMC_MDEP_TABLE(ppc7450, PPC7450, PMC_CLASS_SOFT, PMC_CLASS_PPC7450, PMC_CLASS_TSC);
198 PMC_MDEP_TABLE(ppc970, PPC970, PMC_CLASS_SOFT, PMC_CLASS_PPC970, PMC_CLASS_TSC);
199 PMC_MDEP_TABLE(e500, E500, PMC_CLASS_SOFT, PMC_CLASS_E500, PMC_CLASS_TSC);
200 PMC_MDEP_TABLE(generic, SOFT, PMC_CLASS_SOFT);
202 static const struct pmc_event_descr tsc_event_table[] =
207 #undef PMC_CLASS_TABLE_DESC
208 #define PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR) \
209 static const struct pmc_class_descr NAME##_class_table_descr = \
211 .pm_evc_name = #CLASS "-", \
212 .pm_evc_name_size = sizeof(#CLASS "-") - 1, \
213 .pm_evc_class = PMC_CLASS_##CLASS , \
214 .pm_evc_event_table = EVENTS##_event_table , \
215 .pm_evc_event_table_size = \
216 PMC_EVENT_TABLE_SIZE(EVENTS), \
217 .pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc \
220 #if defined(__i386__) || defined(__amd64__)
221 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
223 #if defined(__i386__) || defined(__amd64__)
224 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
227 #if defined(__XSCALE__)
228 PMC_CLASS_TABLE_DESC(xscale, XSCALE, xscale, xscale);
230 PMC_CLASS_TABLE_DESC(cortex_a8, ARMV7, cortex_a8, armv7);
231 PMC_CLASS_TABLE_DESC(cortex_a9, ARMV7, cortex_a9, armv7);
233 #if defined(__aarch64__)
234 PMC_CLASS_TABLE_DESC(cortex_a53, ARMV8, cortex_a53, arm64);
235 PMC_CLASS_TABLE_DESC(cortex_a57, ARMV8, cortex_a57, arm64);
237 #if defined(__mips__)
238 PMC_CLASS_TABLE_DESC(mips24k, MIPS24K, mips24k, mips);
239 PMC_CLASS_TABLE_DESC(mips74k, MIPS74K, mips74k, mips);
240 PMC_CLASS_TABLE_DESC(octeon, OCTEON, octeon, mips);
241 #endif /* __mips__ */
242 #if defined(__powerpc__)
243 PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, powerpc);
244 PMC_CLASS_TABLE_DESC(ppc970, PPC970, ppc970, powerpc);
245 PMC_CLASS_TABLE_DESC(e500, E500, e500, powerpc);
248 static struct pmc_class_descr soft_class_table_descr =
250 .pm_evc_name = "SOFT-",
251 .pm_evc_name_size = sizeof("SOFT-") - 1,
252 .pm_evc_class = PMC_CLASS_SOFT,
253 .pm_evc_event_table = NULL,
254 .pm_evc_event_table_size = 0,
255 .pm_evc_allocate_pmc = soft_allocate_pmc
258 #undef PMC_CLASS_TABLE_DESC
260 static const struct pmc_class_descr **pmc_class_table;
261 #define PMC_CLASS_TABLE_SIZE cpu_info.pm_nclass
263 static const enum pmc_class *pmc_mdep_class_list;
264 static size_t pmc_mdep_class_list_size;
267 * Mapping tables, mapping enumeration values to human readable
271 static const char * pmc_capability_names[] = {
273 #define __PMC_CAP(N,V,D) #N ,
277 struct pmc_class_map {
278 enum pmc_class pm_class;
282 static const struct pmc_class_map pmc_class_names[] = {
284 #define __PMC_CLASS(S,V,D) { .pm_class = PMC_CLASS_##S, .pm_name = #S } ,
288 struct pmc_cputype_map {
289 enum pmc_cputype pm_cputype;
293 static const struct pmc_cputype_map pmc_cputype_names[] = {
295 #define __PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
299 static const char * pmc_disposition_names[] = {
301 #define __PMC_DISP(D) #D ,
305 static const char * pmc_mode_names[] = {
307 #define __PMC_MODE(M,N) #M ,
311 static const char * pmc_state_names[] = {
313 #define __PMC_STATE(S) #S ,
318 * Filled in by pmc_init().
320 static int pmc_syscall = -1;
321 static struct pmc_cpuinfo cpu_info;
322 static struct pmc_op_getdyneventinfo soft_event_info;
324 /* Event masks for events */
327 const uint64_t pm_value;
329 #define PMCMASK(N,V) { .pm_name = #N, .pm_value = (V) }
330 #define NULLMASK { .pm_name = NULL }
332 #if defined(__amd64__) || defined(__i386__)
334 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint64_t *evmask)
336 const struct pmc_masks *pm;
340 if (pmask == NULL) /* no mask keywords */
342 q = strchr(p, '='); /* skip '=' */
343 if (*++q == '\0') /* no more data */
345 c = 0; /* count of mask keywords seen */
346 while ((r = strsep(&q, "+")) != NULL) {
347 for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
350 if (pm->pm_name == NULL) /* not found */
352 *evmask |= pm->pm_value;
359 #define KWMATCH(p,kw) (strcasecmp((p), (kw)) == 0)
360 #define KWPREFIXMATCH(p,kw) (strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
361 #define EV_ALIAS(N,S) { .pm_alias = N, .pm_spec = S }
363 #if defined(__amd64__) || defined(__i386__)
369 static struct pmc_event_alias k8_aliases[] = {
370 EV_ALIAS("branches", "k8-fr-retired-taken-branches"),
371 EV_ALIAS("branch-mispredicts",
372 "k8-fr-retired-taken-branches-mispredicted"),
373 EV_ALIAS("cycles", "tsc"),
374 EV_ALIAS("dc-misses", "k8-dc-miss"),
375 EV_ALIAS("ic-misses", "k8-ic-miss"),
376 EV_ALIAS("instructions", "k8-fr-retired-x86-instructions"),
377 EV_ALIAS("interrupts", "k8-fr-taken-hardware-interrupts"),
378 EV_ALIAS("unhalted-cycles", "k8-bu-cpu-clk-unhalted"),
382 #define __K8MASK(N,V) PMCMASK(N,(1 << (V)))
388 /* fp dispatched fpu ops */
389 static const struct pmc_masks k8_mask_fdfo[] = {
390 __K8MASK(add-pipe-excluding-junk-ops, 0),
391 __K8MASK(multiply-pipe-excluding-junk-ops, 1),
392 __K8MASK(store-pipe-excluding-junk-ops, 2),
393 __K8MASK(add-pipe-junk-ops, 3),
394 __K8MASK(multiply-pipe-junk-ops, 4),
395 __K8MASK(store-pipe-junk-ops, 5),
399 /* ls segment register loads */
400 static const struct pmc_masks k8_mask_lsrl[] = {
411 /* ls locked operation */
412 static const struct pmc_masks k8_mask_llo[] = {
413 __K8MASK(locked-instructions, 0),
414 __K8MASK(cycles-in-request, 1),
415 __K8MASK(cycles-to-complete, 2),
419 /* dc refill from {l2,system} and dc copyback */
420 static const struct pmc_masks k8_mask_dc[] = {
421 __K8MASK(invalid, 0),
423 __K8MASK(exclusive, 2),
425 __K8MASK(modified, 4),
429 /* dc one bit ecc error */
430 static const struct pmc_masks k8_mask_dobee[] = {
431 __K8MASK(scrubber, 0),
432 __K8MASK(piggyback, 1),
436 /* dc dispatched prefetch instructions */
437 static const struct pmc_masks k8_mask_ddpi[] = {
444 /* dc dcache accesses by locks */
445 static const struct pmc_masks k8_mask_dabl[] = {
446 __K8MASK(accesses, 0),
451 /* bu internal l2 request */
452 static const struct pmc_masks k8_mask_bilr[] = {
453 __K8MASK(ic-fill, 0),
454 __K8MASK(dc-fill, 1),
455 __K8MASK(tlb-reload, 2),
456 __K8MASK(tag-snoop, 3),
457 __K8MASK(cancelled, 4),
461 /* bu fill request l2 miss */
462 static const struct pmc_masks k8_mask_bfrlm[] = {
463 __K8MASK(ic-fill, 0),
464 __K8MASK(dc-fill, 1),
465 __K8MASK(tlb-reload, 2),
469 /* bu fill into l2 */
470 static const struct pmc_masks k8_mask_bfil[] = {
471 __K8MASK(dirty-l2-victim, 0),
472 __K8MASK(victim-from-l2, 1),
476 /* fr retired fpu instructions */
477 static const struct pmc_masks k8_mask_frfi[] = {
479 __K8MASK(mmx-3dnow, 1),
480 __K8MASK(packed-sse-sse2, 2),
481 __K8MASK(scalar-sse-sse2, 3),
485 /* fr retired fastpath double op instructions */
486 static const struct pmc_masks k8_mask_frfdoi[] = {
487 __K8MASK(low-op-pos-0, 0),
488 __K8MASK(low-op-pos-1, 1),
489 __K8MASK(low-op-pos-2, 2),
493 /* fr fpu exceptions */
494 static const struct pmc_masks k8_mask_ffe[] = {
495 __K8MASK(x87-reclass-microfaults, 0),
496 __K8MASK(sse-retype-microfaults, 1),
497 __K8MASK(sse-reclass-microfaults, 2),
498 __K8MASK(sse-and-x87-microtraps, 3),
502 /* nb memory controller page access event */
503 static const struct pmc_masks k8_mask_nmcpae[] = {
504 __K8MASK(page-hit, 0),
505 __K8MASK(page-miss, 1),
506 __K8MASK(page-conflict, 2),
510 /* nb memory controller turnaround */
511 static const struct pmc_masks k8_mask_nmct[] = {
512 __K8MASK(dimm-turnaround, 0),
513 __K8MASK(read-to-write-turnaround, 1),
514 __K8MASK(write-to-read-turnaround, 2),
518 /* nb memory controller bypass saturation */
519 static const struct pmc_masks k8_mask_nmcbs[] = {
520 __K8MASK(memory-controller-hi-pri-bypass, 0),
521 __K8MASK(memory-controller-lo-pri-bypass, 1),
522 __K8MASK(dram-controller-interface-bypass, 2),
523 __K8MASK(dram-controller-queue-bypass, 3),
527 /* nb sized commands */
528 static const struct pmc_masks k8_mask_nsc[] = {
529 __K8MASK(nonpostwrszbyte, 0),
530 __K8MASK(nonpostwrszdword, 1),
531 __K8MASK(postwrszbyte, 2),
532 __K8MASK(postwrszdword, 3),
533 __K8MASK(rdszbyte, 4),
534 __K8MASK(rdszdword, 5),
535 __K8MASK(rdmodwr, 6),
539 /* nb probe result */
540 static const struct pmc_masks k8_mask_npr[] = {
541 __K8MASK(probe-miss, 0),
542 __K8MASK(probe-hit, 1),
543 __K8MASK(probe-hit-dirty-no-memory-cancel, 2),
544 __K8MASK(probe-hit-dirty-with-memory-cancel, 3),
548 /* nb hypertransport bus bandwidth */
549 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
550 __K8MASK(command, 0),
552 __K8MASK(buffer-release, 2),
559 #define K8_KW_COUNT "count"
560 #define K8_KW_EDGE "edge"
561 #define K8_KW_INV "inv"
562 #define K8_KW_MASK "mask"
563 #define K8_KW_OS "os"
564 #define K8_KW_USR "usr"
567 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
568 struct pmc_op_pmcallocate *pmc_config)
574 const struct pmc_masks *pm, *pmask;
576 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
577 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
582 #define __K8SETMASK(M) pmask = k8_mask_##M
584 /* setup parsing tables */
586 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
589 case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
592 case PMC_EV_K8_LS_LOCKED_OPERATION:
595 case PMC_EV_K8_DC_REFILL_FROM_L2:
596 case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
597 case PMC_EV_K8_DC_COPYBACK:
600 case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
603 case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
606 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
609 case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
612 case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
615 case PMC_EV_K8_BU_FILL_INTO_L2:
618 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
621 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
624 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
627 case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
630 case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
633 case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
636 case PMC_EV_K8_NB_SIZED_COMMANDS:
639 case PMC_EV_K8_NB_PROBE_RESULT:
642 case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
643 case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
644 case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
649 break; /* no options defined */
652 while ((p = strsep(&ctrspec, ",")) != NULL) {
653 if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
655 if (*++q == '\0') /* skip '=' */
658 count = strtol(q, &e, 0);
659 if (e == q || *e != '\0')
662 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
663 pmc_config->pm_md.pm_amd.pm_amd_config |=
664 AMD_PMC_TO_COUNTER(count);
666 } else if (KWMATCH(p, K8_KW_EDGE)) {
667 pmc_config->pm_caps |= PMC_CAP_EDGE;
668 } else if (KWMATCH(p, K8_KW_INV)) {
669 pmc_config->pm_caps |= PMC_CAP_INVERT;
670 } else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
671 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
673 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
674 } else if (KWMATCH(p, K8_KW_OS)) {
675 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
676 } else if (KWMATCH(p, K8_KW_USR)) {
677 pmc_config->pm_caps |= PMC_CAP_USER;
682 /* other post processing */
684 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
685 case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
686 case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
687 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
688 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
689 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
690 /* XXX only available in rev B and later */
692 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
693 /* XXX only available in rev C and later */
695 case PMC_EV_K8_LS_LOCKED_OPERATION:
696 /* XXX CPU Rev A,B evmask is to be zero */
697 if (evmask & (evmask - 1)) /* > 1 bit set */
700 evmask = 0x01; /* Rev C and later: #instrs */
701 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
705 if (evmask == 0 && pmask != NULL) {
706 for (pm = pmask; pm->pm_name; pm++)
707 evmask |= pm->pm_value;
708 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
712 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
713 pmc_config->pm_md.pm_amd.pm_amd_config =
714 AMD_PMC_TO_UNITMASK(evmask);
721 #if defined(__i386__) || defined(__amd64__)
723 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
724 struct pmc_op_pmcallocate *pmc_config)
726 if (pe != PMC_EV_TSC_TSC)
729 /* TSC events must be unqualified. */
730 if (ctrspec && *ctrspec != '\0')
733 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
734 pmc_config->pm_caps |= PMC_CAP_READ;
740 static struct pmc_event_alias generic_aliases[] = {
741 EV_ALIAS("instructions", "SOFT-CLOCK.HARD"),
746 soft_allocate_pmc(enum pmc_event pe, char *ctrspec,
747 struct pmc_op_pmcallocate *pmc_config)
752 if ((int)pe < PMC_EV_SOFT_FIRST || (int)pe > PMC_EV_SOFT_LAST)
755 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
760 #if defined(__XSCALE__)
762 static struct pmc_event_alias xscale_aliases[] = {
763 EV_ALIAS("branches", "BRANCH_RETIRED"),
764 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
765 EV_ALIAS("dc-misses", "DC_MISS"),
766 EV_ALIAS("ic-misses", "IC_MISS"),
767 EV_ALIAS("instructions", "INSTR_RETIRED"),
771 xscale_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
772 struct pmc_op_pmcallocate *pmc_config __unused)
783 static struct pmc_event_alias cortex_a8_aliases[] = {
784 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
785 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
786 EV_ALIAS("instructions", "INSTR_EXECUTED"),
790 static struct pmc_event_alias cortex_a9_aliases[] = {
791 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
792 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
793 EV_ALIAS("instructions", "INSTR_EXECUTED"),
798 armv7_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
799 struct pmc_op_pmcallocate *pmc_config __unused)
810 #if defined(__aarch64__)
811 static struct pmc_event_alias cortex_a53_aliases[] = {
814 static struct pmc_event_alias cortex_a57_aliases[] = {
818 arm64_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
819 struct pmc_op_pmcallocate *pmc_config __unused)
830 #if defined(__mips__)
832 static struct pmc_event_alias mips24k_aliases[] = {
833 EV_ALIAS("instructions", "INSTR_EXECUTED"),
834 EV_ALIAS("branches", "BRANCH_COMPLETED"),
835 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
839 static struct pmc_event_alias mips74k_aliases[] = {
840 EV_ALIAS("instructions", "INSTR_EXECUTED"),
841 EV_ALIAS("branches", "BRANCH_INSNS"),
842 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCH_INSNS"),
846 static struct pmc_event_alias octeon_aliases[] = {
847 EV_ALIAS("instructions", "RET"),
848 EV_ALIAS("branches", "BR"),
849 EV_ALIAS("branch-mispredicts", "BRMIS"),
853 #define MIPS_KW_OS "os"
854 #define MIPS_KW_USR "usr"
855 #define MIPS_KW_ANYTHREAD "anythread"
858 mips_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
859 struct pmc_op_pmcallocate *pmc_config __unused)
865 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
867 while ((p = strsep(&ctrspec, ",")) != NULL) {
868 if (KWMATCH(p, MIPS_KW_OS))
869 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
870 else if (KWMATCH(p, MIPS_KW_USR))
871 pmc_config->pm_caps |= PMC_CAP_USER;
872 else if (KWMATCH(p, MIPS_KW_ANYTHREAD))
873 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
881 #endif /* __mips__ */
883 #if defined(__powerpc__)
885 static struct pmc_event_alias ppc7450_aliases[] = {
886 EV_ALIAS("instructions", "INSTR_COMPLETED"),
887 EV_ALIAS("branches", "BRANCHES_COMPLETED"),
888 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCHES"),
892 static struct pmc_event_alias ppc970_aliases[] = {
893 EV_ALIAS("instructions", "INSTR_COMPLETED"),
894 EV_ALIAS("cycles", "CYCLES"),
898 static struct pmc_event_alias e500_aliases[] = {
899 EV_ALIAS("instructions", "INSTR_COMPLETED"),
900 EV_ALIAS("cycles", "CYCLES"),
904 #define POWERPC_KW_OS "os"
905 #define POWERPC_KW_USR "usr"
906 #define POWERPC_KW_ANYTHREAD "anythread"
909 powerpc_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
910 struct pmc_op_pmcallocate *pmc_config __unused)
916 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
918 while ((p = strsep(&ctrspec, ",")) != NULL) {
919 if (KWMATCH(p, POWERPC_KW_OS))
920 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
921 else if (KWMATCH(p, POWERPC_KW_USR))
922 pmc_config->pm_caps |= PMC_CAP_USER;
923 else if (KWMATCH(p, POWERPC_KW_ANYTHREAD))
924 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
932 #endif /* __powerpc__ */
936 * Match an event name `name' with its canonical form.
938 * Matches are case insensitive and spaces, periods, underscores and
939 * hyphen characters are considered to match each other.
941 * Returns 1 for a match, 0 otherwise.
945 pmc_match_event_name(const char *name, const char *canonicalname)
948 const unsigned char *c, *n;
950 c = (const unsigned char *) canonicalname;
951 n = (const unsigned char *) name;
953 for (; (nc = *n) && (cc = *c); n++, c++) {
955 if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
956 (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
959 if (toupper(nc) == toupper(cc))
966 if (*n == '\0' && *c == '\0')
973 * Match an event name against all the event named supported by a
976 * Returns an event descriptor pointer on match or NULL otherwise.
978 static const struct pmc_event_descr *
979 pmc_match_event_class(const char *name,
980 const struct pmc_class_descr *pcd)
983 const struct pmc_event_descr *ev;
985 ev = pcd->pm_evc_event_table;
986 for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
987 if (pmc_match_event_name(name, ev->pm_ev_name))
994 pmc_mdep_is_compatible_class(enum pmc_class pc)
998 for (n = 0; n < pmc_mdep_class_list_size; n++)
999 if (pmc_mdep_class_list[n] == pc)
1009 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
1010 uint32_t flags, int cpu, pmc_id_t *pmcid)
1014 char *r, *spec_copy;
1015 const char *ctrname;
1016 const struct pmc_event_descr *ev;
1017 const struct pmc_event_alias *alias;
1018 struct pmc_op_pmcallocate pmc_config;
1019 const struct pmc_class_descr *pcd;
1024 if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
1025 mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
1029 bzero(&pmc_config, sizeof(pmc_config));
1030 pmc_config.pm_cpu = cpu;
1031 pmc_config.pm_mode = mode;
1032 pmc_config.pm_flags = flags;
1033 if (PMC_IS_SAMPLING_MODE(mode))
1034 pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
1036 * Can we pull this straight from the pmu table?
1038 r = spec_copy = strdup(ctrspec);
1039 ctrname = strsep(&r, ",");
1040 if (pmc_pmu_enabled()) {
1041 if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0) {
1042 if (PMC_CALL(PMCALLOCATE, &pmc_config) < 0) {
1046 *pmcid = pmc_config.pm_pmcid;
1049 errx(EX_USAGE, "ERROR: pmc_pmu_allocate failed, check for ctrname %s\n", ctrname);
1055 /* replace an event alias with the canonical event specifier */
1056 if (pmc_mdep_event_aliases)
1057 for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
1058 if (!strcasecmp(ctrspec, alias->pm_alias)) {
1059 spec_copy = strdup(alias->pm_spec);
1063 if (spec_copy == NULL)
1064 spec_copy = strdup(ctrspec);
1067 ctrname = strsep(&r, ",");
1070 * If a explicit class prefix was given by the user, restrict the
1071 * search for the event to the specified PMC class.
1074 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
1075 pcd = pmc_class_table[n];
1076 if (pcd && pmc_mdep_is_compatible_class(pcd->pm_evc_class) &&
1077 strncasecmp(ctrname, pcd->pm_evc_name,
1078 pcd->pm_evc_name_size) == 0) {
1079 if ((ev = pmc_match_event_class(ctrname +
1080 pcd->pm_evc_name_size, pcd)) == NULL) {
1089 * Otherwise, search for this event in all compatible PMC
1092 for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
1093 pcd = pmc_class_table[n];
1094 if (pcd && pmc_mdep_is_compatible_class(pcd->pm_evc_class))
1095 ev = pmc_match_event_class(ctrname, pcd);
1103 pmc_config.pm_ev = ev->pm_ev_code;
1104 pmc_config.pm_class = pcd->pm_evc_class;
1106 if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
1111 if (PMC_CALL(PMCALLOCATE, &pmc_config) < 0)
1114 *pmcid = pmc_config.pm_pmcid;
1126 pmc_attach(pmc_id_t pmc, pid_t pid)
1128 struct pmc_op_pmcattach pmc_attach_args;
1130 pmc_attach_args.pm_pmc = pmc;
1131 pmc_attach_args.pm_pid = pid;
1133 return (PMC_CALL(PMCATTACH, &pmc_attach_args));
1137 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
1142 cl = PMC_ID_TO_CLASS(pmcid);
1143 for (i = 0; i < cpu_info.pm_nclass; i++)
1144 if (cpu_info.pm_classes[i].pm_class == cl) {
1145 *caps = cpu_info.pm_classes[i].pm_caps;
1153 pmc_configure_logfile(int fd)
1155 struct pmc_op_configurelog cla;
1158 if (PMC_CALL(CONFIGURELOG, &cla) < 0)
1164 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
1166 if (pmc_syscall == -1) {
1176 pmc_detach(pmc_id_t pmc, pid_t pid)
1178 struct pmc_op_pmcattach pmc_detach_args;
1180 pmc_detach_args.pm_pmc = pmc;
1181 pmc_detach_args.pm_pid = pid;
1182 return (PMC_CALL(PMCDETACH, &pmc_detach_args));
1186 pmc_disable(int cpu, int pmc)
1188 struct pmc_op_pmcadmin ssa;
1192 ssa.pm_state = PMC_STATE_DISABLED;
1193 return (PMC_CALL(PMCADMIN, &ssa));
1197 pmc_enable(int cpu, int pmc)
1199 struct pmc_op_pmcadmin ssa;
1203 ssa.pm_state = PMC_STATE_FREE;
1204 return (PMC_CALL(PMCADMIN, &ssa));
1208 * Return a list of events known to a given PMC class. 'cl' is the
1209 * PMC class identifier, 'eventnames' is the returned list of 'const
1210 * char *' pointers pointing to the names of the events. 'nevents' is
1211 * the number of event name pointers returned.
1213 * The space for 'eventnames' is allocated using malloc(3). The caller
1214 * is responsible for freeing this space when done.
1217 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
1222 const struct pmc_event_descr *ev;
1227 ev = iaf_event_table;
1228 count = PMC_EVENT_TABLE_SIZE(iaf);
1231 ev = tsc_event_table;
1232 count = PMC_EVENT_TABLE_SIZE(tsc);
1235 ev = k8_event_table;
1236 count = PMC_EVENT_TABLE_SIZE(k8);
1238 case PMC_CLASS_XSCALE:
1239 ev = xscale_event_table;
1240 count = PMC_EVENT_TABLE_SIZE(xscale);
1242 case PMC_CLASS_ARMV7:
1243 switch (cpu_info.pm_cputype) {
1245 case PMC_CPU_ARMV7_CORTEX_A8:
1246 ev = cortex_a8_event_table;
1247 count = PMC_EVENT_TABLE_SIZE(cortex_a8);
1249 case PMC_CPU_ARMV7_CORTEX_A9:
1250 ev = cortex_a9_event_table;
1251 count = PMC_EVENT_TABLE_SIZE(cortex_a9);
1255 case PMC_CLASS_ARMV8:
1256 switch (cpu_info.pm_cputype) {
1258 case PMC_CPU_ARMV8_CORTEX_A53:
1259 ev = cortex_a53_event_table;
1260 count = PMC_EVENT_TABLE_SIZE(cortex_a53);
1262 case PMC_CPU_ARMV8_CORTEX_A57:
1263 ev = cortex_a57_event_table;
1264 count = PMC_EVENT_TABLE_SIZE(cortex_a57);
1268 case PMC_CLASS_MIPS24K:
1269 ev = mips24k_event_table;
1270 count = PMC_EVENT_TABLE_SIZE(mips24k);
1272 case PMC_CLASS_MIPS74K:
1273 ev = mips74k_event_table;
1274 count = PMC_EVENT_TABLE_SIZE(mips74k);
1276 case PMC_CLASS_OCTEON:
1277 ev = octeon_event_table;
1278 count = PMC_EVENT_TABLE_SIZE(octeon);
1280 case PMC_CLASS_PPC7450:
1281 ev = ppc7450_event_table;
1282 count = PMC_EVENT_TABLE_SIZE(ppc7450);
1284 case PMC_CLASS_PPC970:
1285 ev = ppc970_event_table;
1286 count = PMC_EVENT_TABLE_SIZE(ppc970);
1288 case PMC_CLASS_E500:
1289 ev = e500_event_table;
1290 count = PMC_EVENT_TABLE_SIZE(e500);
1292 case PMC_CLASS_SOFT:
1293 ev = soft_event_table;
1294 count = soft_event_info.pm_nevent;
1301 if ((names = malloc(count * sizeof(const char *))) == NULL)
1304 *eventnames = names;
1307 for (;count--; ev++, names++)
1308 *names = ev->pm_ev_name;
1314 pmc_flush_logfile(void)
1316 return (PMC_CALL(FLUSHLOG,0));
1320 pmc_close_logfile(void)
1322 return (PMC_CALL(CLOSELOG,0));
1326 pmc_get_driver_stats(struct pmc_driverstats *ds)
1328 struct pmc_op_getdriverstats gms;
1330 if (PMC_CALL(GETDRIVERSTATS, &gms) < 0)
1333 /* copy out fields in the current userland<->library interface */
1334 ds->pm_intr_ignored = gms.pm_intr_ignored;
1335 ds->pm_intr_processed = gms.pm_intr_processed;
1336 ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
1337 ds->pm_syscalls = gms.pm_syscalls;
1338 ds->pm_syscall_errors = gms.pm_syscall_errors;
1339 ds->pm_buffer_requests = gms.pm_buffer_requests;
1340 ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
1341 ds->pm_log_sweeps = gms.pm_log_sweeps;
1346 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
1348 struct pmc_op_getmsr gm;
1351 if (PMC_CALL(PMCGETMSR, &gm) < 0)
1360 int error, pmc_mod_id;
1362 uint32_t abi_version;
1363 struct module_stat pmc_modstat;
1364 struct pmc_op_getcpuinfo op_cpu_info;
1365 #if defined(__amd64__) || defined(__i386__)
1366 int cpu_has_iaf_counters;
1370 if (pmc_syscall != -1) /* already inited */
1373 /* retrieve the system call number from the KLD */
1374 if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
1377 pmc_modstat.version = sizeof(struct module_stat);
1378 if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
1381 pmc_syscall = pmc_modstat.data.intval;
1383 /* check the kernel module's ABI against our compiled-in version */
1384 abi_version = PMC_VERSION;
1385 if (PMC_CALL(GETMODULEVERSION, &abi_version) < 0)
1386 return (pmc_syscall = -1);
1388 /* ignore patch & minor numbers for the comparison */
1389 if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
1390 errno = EPROGMISMATCH;
1391 return (pmc_syscall = -1);
1394 bzero(&op_cpu_info, sizeof(op_cpu_info));
1395 if (PMC_CALL(GETCPUINFO, &op_cpu_info) < 0)
1396 return (pmc_syscall = -1);
1398 cpu_info.pm_cputype = op_cpu_info.pm_cputype;
1399 cpu_info.pm_ncpu = op_cpu_info.pm_ncpu;
1400 cpu_info.pm_npmc = op_cpu_info.pm_npmc;
1401 cpu_info.pm_nclass = op_cpu_info.pm_nclass;
1402 for (n = 0; n < op_cpu_info.pm_nclass; n++)
1403 memcpy(&cpu_info.pm_classes[n], &op_cpu_info.pm_classes[n],
1404 sizeof(cpu_info.pm_classes[n]));
1406 pmc_class_table = malloc(PMC_CLASS_TABLE_SIZE *
1407 sizeof(struct pmc_class_descr *));
1409 if (pmc_class_table == NULL)
1412 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++)
1413 pmc_class_table[n] = NULL;
1416 * Get soft events list.
1418 soft_event_info.pm_class = PMC_CLASS_SOFT;
1419 if (PMC_CALL(GETDYNEVENTINFO, &soft_event_info) < 0)
1420 return (pmc_syscall = -1);
1422 /* Map soft events to static list. */
1423 for (n = 0; n < soft_event_info.pm_nevent; n++) {
1424 soft_event_table[n].pm_ev_name =
1425 soft_event_info.pm_events[n].pm_ev_name;
1426 soft_event_table[n].pm_ev_code =
1427 soft_event_info.pm_events[n].pm_ev_code;
1429 soft_class_table_descr.pm_evc_event_table_size = \
1430 soft_event_info.pm_nevent;
1431 soft_class_table_descr.pm_evc_event_table = \
1435 * Fill in the class table.
1439 /* Fill soft events information. */
1440 pmc_class_table[n++] = &soft_class_table_descr;
1441 #if defined(__amd64__) || defined(__i386__)
1442 if (cpu_info.pm_cputype != PMC_CPU_GENERIC)
1443 pmc_class_table[n++] = &tsc_class_table_descr;
1446 * Check if this CPU has fixed function counters.
1448 cpu_has_iaf_counters = 0;
1449 for (t = 0; t < cpu_info.pm_nclass; t++)
1450 if (cpu_info.pm_classes[t].pm_class == PMC_CLASS_IAF &&
1451 cpu_info.pm_classes[t].pm_num > 0)
1452 cpu_has_iaf_counters = 1;
1455 #define PMC_MDEP_INIT(C) do { \
1456 pmc_mdep_event_aliases = C##_aliases; \
1457 pmc_mdep_class_list = C##_pmc_classes; \
1458 pmc_mdep_class_list_size = \
1459 PMC_TABLE_SIZE(C##_pmc_classes); \
1462 #define PMC_MDEP_INIT_INTEL_V2(C) do { \
1464 pmc_class_table[n++] = &iaf_class_table_descr; \
1465 if (!cpu_has_iaf_counters) \
1466 pmc_mdep_event_aliases = \
1467 C##_aliases_without_iaf; \
1468 pmc_class_table[n] = &C##_class_table_descr; \
1471 /* Configure the event name parser. */
1472 switch (cpu_info.pm_cputype) {
1473 #if defined(__amd64__) || defined(__i386__)
1474 case PMC_CPU_AMD_K8:
1476 pmc_class_table[n] = &k8_class_table_descr;
1479 case PMC_CPU_GENERIC:
1480 PMC_MDEP_INIT(generic);
1482 #if defined(__arm__)
1483 #if defined(__XSCALE__)
1484 case PMC_CPU_INTEL_XSCALE:
1485 PMC_MDEP_INIT(xscale);
1486 pmc_class_table[n] = &xscale_class_table_descr;
1489 case PMC_CPU_ARMV7_CORTEX_A8:
1490 PMC_MDEP_INIT(cortex_a8);
1491 pmc_class_table[n] = &cortex_a8_class_table_descr;
1493 case PMC_CPU_ARMV7_CORTEX_A9:
1494 PMC_MDEP_INIT(cortex_a9);
1495 pmc_class_table[n] = &cortex_a9_class_table_descr;
1498 #if defined(__aarch64__)
1499 case PMC_CPU_ARMV8_CORTEX_A53:
1500 PMC_MDEP_INIT(cortex_a53);
1501 pmc_class_table[n] = &cortex_a53_class_table_descr;
1503 case PMC_CPU_ARMV8_CORTEX_A57:
1504 PMC_MDEP_INIT(cortex_a57);
1505 pmc_class_table[n] = &cortex_a57_class_table_descr;
1508 #if defined(__mips__)
1509 case PMC_CPU_MIPS_24K:
1510 PMC_MDEP_INIT(mips24k);
1511 pmc_class_table[n] = &mips24k_class_table_descr;
1513 case PMC_CPU_MIPS_74K:
1514 PMC_MDEP_INIT(mips74k);
1515 pmc_class_table[n] = &mips74k_class_table_descr;
1517 case PMC_CPU_MIPS_OCTEON:
1518 PMC_MDEP_INIT(octeon);
1519 pmc_class_table[n] = &octeon_class_table_descr;
1521 #endif /* __mips__ */
1522 #if defined(__powerpc__)
1523 case PMC_CPU_PPC_7450:
1524 PMC_MDEP_INIT(ppc7450);
1525 pmc_class_table[n] = &ppc7450_class_table_descr;
1527 case PMC_CPU_PPC_970:
1528 PMC_MDEP_INIT(ppc970);
1529 pmc_class_table[n] = &ppc970_class_table_descr;
1531 case PMC_CPU_PPC_E500:
1532 PMC_MDEP_INIT(e500);
1533 pmc_class_table[n] = &e500_class_table_descr;
1538 * Some kind of CPU this version of the library knows nothing
1539 * about. This shouldn't happen since the abi version check
1540 * should have caught this.
1542 #if defined(__amd64__) || defined(__i386__)
1546 return (pmc_syscall = -1);
1553 pmc_name_of_capability(enum pmc_caps cap)
1558 * 'cap' should have a single bit set and should be in
1561 if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
1562 cap > PMC_CAP_LAST) {
1568 return (pmc_capability_names[i - 1]);
1572 pmc_name_of_class(enum pmc_class pc)
1576 for (n = 0; n < PMC_TABLE_SIZE(pmc_class_names); n++)
1577 if (pc == pmc_class_names[n].pm_class)
1578 return (pmc_class_names[n].pm_name);
1585 pmc_name_of_cputype(enum pmc_cputype cp)
1589 for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
1590 if (cp == pmc_cputype_names[n].pm_cputype)
1591 return (pmc_cputype_names[n].pm_name);
1598 pmc_name_of_disposition(enum pmc_disp pd)
1600 if ((int) pd >= PMC_DISP_FIRST &&
1601 pd <= PMC_DISP_LAST)
1602 return (pmc_disposition_names[pd]);
1609 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
1611 const struct pmc_event_descr *ev, *evfence;
1613 ev = evfence = NULL;
1614 if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
1615 ev = k8_event_table;
1616 evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
1617 } else if (pe >= PMC_EV_XSCALE_FIRST && pe <= PMC_EV_XSCALE_LAST) {
1618 ev = xscale_event_table;
1619 evfence = xscale_event_table + PMC_EVENT_TABLE_SIZE(xscale);
1620 } else if (pe >= PMC_EV_ARMV7_FIRST && pe <= PMC_EV_ARMV7_LAST) {
1622 case PMC_CPU_ARMV7_CORTEX_A8:
1623 ev = cortex_a8_event_table;
1624 evfence = cortex_a8_event_table + PMC_EVENT_TABLE_SIZE(cortex_a8);
1626 case PMC_CPU_ARMV7_CORTEX_A9:
1627 ev = cortex_a9_event_table;
1628 evfence = cortex_a9_event_table + PMC_EVENT_TABLE_SIZE(cortex_a9);
1630 default: /* Unknown CPU type. */
1633 } else if (pe >= PMC_EV_ARMV8_FIRST && pe <= PMC_EV_ARMV8_LAST) {
1635 case PMC_CPU_ARMV8_CORTEX_A53:
1636 ev = cortex_a53_event_table;
1637 evfence = cortex_a53_event_table + PMC_EVENT_TABLE_SIZE(cortex_a53);
1639 case PMC_CPU_ARMV8_CORTEX_A57:
1640 ev = cortex_a57_event_table;
1641 evfence = cortex_a57_event_table + PMC_EVENT_TABLE_SIZE(cortex_a57);
1643 default: /* Unknown CPU type. */
1646 } else if (pe >= PMC_EV_MIPS24K_FIRST && pe <= PMC_EV_MIPS24K_LAST) {
1647 ev = mips24k_event_table;
1648 evfence = mips24k_event_table + PMC_EVENT_TABLE_SIZE(mips24k);
1649 } else if (pe >= PMC_EV_MIPS74K_FIRST && pe <= PMC_EV_MIPS74K_LAST) {
1650 ev = mips74k_event_table;
1651 evfence = mips74k_event_table + PMC_EVENT_TABLE_SIZE(mips74k);
1652 } else if (pe >= PMC_EV_OCTEON_FIRST && pe <= PMC_EV_OCTEON_LAST) {
1653 ev = octeon_event_table;
1654 evfence = octeon_event_table + PMC_EVENT_TABLE_SIZE(octeon);
1655 } else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
1656 ev = ppc7450_event_table;
1657 evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450);
1658 } else if (pe >= PMC_EV_PPC970_FIRST && pe <= PMC_EV_PPC970_LAST) {
1659 ev = ppc970_event_table;
1660 evfence = ppc970_event_table + PMC_EVENT_TABLE_SIZE(ppc970);
1661 } else if (pe >= PMC_EV_E500_FIRST && pe <= PMC_EV_E500_LAST) {
1662 ev = e500_event_table;
1663 evfence = e500_event_table + PMC_EVENT_TABLE_SIZE(e500);
1664 } else if (pe == PMC_EV_TSC_TSC) {
1665 ev = tsc_event_table;
1666 evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
1667 } else if ((int)pe >= PMC_EV_SOFT_FIRST && (int)pe <= PMC_EV_SOFT_LAST) {
1668 ev = soft_event_table;
1669 evfence = soft_event_table + soft_event_info.pm_nevent;
1672 for (; ev != evfence; ev++)
1673 if (pe == ev->pm_ev_code)
1674 return (ev->pm_ev_name);
1680 pmc_name_of_event(enum pmc_event pe)
1684 if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
1692 pmc_name_of_mode(enum pmc_mode pm)
1694 if ((int) pm >= PMC_MODE_FIRST &&
1695 pm <= PMC_MODE_LAST)
1696 return (pmc_mode_names[pm]);
1703 pmc_name_of_state(enum pmc_state ps)
1705 if ((int) ps >= PMC_STATE_FIRST &&
1706 ps <= PMC_STATE_LAST)
1707 return (pmc_state_names[ps]);
1716 if (pmc_syscall == -1) {
1721 return (cpu_info.pm_ncpu);
1727 if (pmc_syscall == -1) {
1732 if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
1737 return (cpu_info.pm_npmc);
1741 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
1744 struct pmc_op_getpmcinfo *pmci;
1746 if ((npmc = pmc_npmc(cpu)) < 0)
1749 nbytes = sizeof(struct pmc_op_getpmcinfo) +
1750 npmc * sizeof(struct pmc_info);
1752 if ((pmci = calloc(1, nbytes)) == NULL)
1757 if (PMC_CALL(GETPMCINFO, pmci) < 0) {
1762 /* kernel<->library, library<->userland interfaces are identical */
1763 *ppmci = (struct pmc_pmcinfo *) pmci;
1768 pmc_read(pmc_id_t pmc, pmc_value_t *value)
1770 struct pmc_op_pmcrw pmc_read_op;
1772 pmc_read_op.pm_pmcid = pmc;
1773 pmc_read_op.pm_flags = PMC_F_OLDVALUE;
1774 pmc_read_op.pm_value = -1;
1776 if (PMC_CALL(PMCRW, &pmc_read_op) < 0)
1779 *value = pmc_read_op.pm_value;
1784 pmc_release(pmc_id_t pmc)
1786 struct pmc_op_simple pmc_release_args;
1788 pmc_release_args.pm_pmcid = pmc;
1789 return (PMC_CALL(PMCRELEASE, &pmc_release_args));
1793 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
1795 struct pmc_op_pmcrw pmc_rw_op;
1797 pmc_rw_op.pm_pmcid = pmc;
1798 pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
1799 pmc_rw_op.pm_value = newvalue;
1801 if (PMC_CALL(PMCRW, &pmc_rw_op) < 0)
1804 *oldvaluep = pmc_rw_op.pm_value;
1809 pmc_set(pmc_id_t pmc, pmc_value_t value)
1811 struct pmc_op_pmcsetcount sc;
1814 sc.pm_count = value;
1816 if (PMC_CALL(PMCSETCOUNT, &sc) < 0)
1822 pmc_start(pmc_id_t pmc)
1824 struct pmc_op_simple pmc_start_args;
1826 pmc_start_args.pm_pmcid = pmc;
1827 return (PMC_CALL(PMCSTART, &pmc_start_args));
1831 pmc_stop(pmc_id_t pmc)
1833 struct pmc_op_simple pmc_stop_args;
1835 pmc_stop_args.pm_pmcid = pmc;
1836 return (PMC_CALL(PMCSTOP, &pmc_stop_args));
1840 pmc_width(pmc_id_t pmcid, uint32_t *width)
1845 cl = PMC_ID_TO_CLASS(pmcid);
1846 for (i = 0; i < cpu_info.pm_nclass; i++)
1847 if (cpu_info.pm_classes[i].pm_class == cl) {
1848 *width = cpu_info.pm_classes[i].pm_width;
1856 pmc_write(pmc_id_t pmc, pmc_value_t value)
1858 struct pmc_op_pmcrw pmc_write_op;
1860 pmc_write_op.pm_pmcid = pmc;
1861 pmc_write_op.pm_flags = PMC_F_NEWVALUE;
1862 pmc_write_op.pm_value = value;
1863 return (PMC_CALL(PMCRW, &pmc_write_op));
1867 pmc_writelog(uint32_t userdata)
1869 struct pmc_op_writelog wl;
1871 wl.pm_userdata = userdata;
1872 return (PMC_CALL(WRITELOG, &wl));