xref: /freebsd/lib/libpmc/libpmc.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1ebccf1e3SJoseph Koshy /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
4484202faSJoseph Koshy  * Copyright (c) 2003-2008 Joseph Koshy
5ebccf1e3SJoseph Koshy  * All rights reserved.
6ebccf1e3SJoseph Koshy  *
7ebccf1e3SJoseph Koshy  * Redistribution and use in source and binary forms, with or without
8ebccf1e3SJoseph Koshy  * modification, are permitted provided that the following conditions
9ebccf1e3SJoseph Koshy  * are met:
10ebccf1e3SJoseph Koshy  * 1. Redistributions of source code must retain the above copyright
11ebccf1e3SJoseph Koshy  *    notice, this list of conditions and the following disclaimer.
12ebccf1e3SJoseph Koshy  * 2. Redistributions in binary form must reproduce the above copyright
13ebccf1e3SJoseph Koshy  *    notice, this list of conditions and the following disclaimer in the
14ebccf1e3SJoseph Koshy  *    documentation and/or other materials provided with the distribution.
15ebccf1e3SJoseph Koshy  *
16ebccf1e3SJoseph Koshy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ebccf1e3SJoseph Koshy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ebccf1e3SJoseph Koshy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ebccf1e3SJoseph Koshy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ebccf1e3SJoseph Koshy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ebccf1e3SJoseph Koshy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ebccf1e3SJoseph Koshy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ebccf1e3SJoseph Koshy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ebccf1e3SJoseph Koshy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ebccf1e3SJoseph Koshy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ebccf1e3SJoseph Koshy  * SUCH DAMAGE.
27ebccf1e3SJoseph Koshy  */
28ebccf1e3SJoseph Koshy 
29ebccf1e3SJoseph Koshy #include <sys/types.h>
30169dd953SJustin Hibbits #include <sys/param.h>
31ebccf1e3SJoseph Koshy #include <sys/module.h>
32ebccf1e3SJoseph Koshy #include <sys/pmc.h>
33ebccf1e3SJoseph Koshy #include <sys/syscall.h>
34ebccf1e3SJoseph Koshy 
35ebccf1e3SJoseph Koshy #include <ctype.h>
36ebccf1e3SJoseph Koshy #include <errno.h>
3712193215SMatt Macy #include <err.h>
38ebccf1e3SJoseph Koshy #include <fcntl.h>
39ebccf1e3SJoseph Koshy #include <pmc.h>
40ebccf1e3SJoseph Koshy #include <stdio.h>
41ebccf1e3SJoseph Koshy #include <stdlib.h>
42ebccf1e3SJoseph Koshy #include <string.h>
43ebccf1e3SJoseph Koshy #include <strings.h>
4412193215SMatt Macy #include <sysexits.h>
45ebccf1e3SJoseph Koshy #include <unistd.h>
46ebccf1e3SJoseph Koshy 
470cfab8ddSJoseph Koshy #include "libpmcinternal.h"
480cfab8ddSJoseph Koshy 
49ebccf1e3SJoseph Koshy /* Function prototypes */
5086a65549SJoseph Koshy #if defined(__amd64__) || defined(__i386__)
51f263522aSJoseph Koshy static int k8_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
52ebccf1e3SJoseph Koshy     struct pmc_op_pmcallocate *_pmc_config);
53ebccf1e3SJoseph Koshy #endif
54789140c0SJoseph Koshy #if defined(__amd64__) || defined(__i386__)
55789140c0SJoseph Koshy static int tsc_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
56789140c0SJoseph Koshy     struct pmc_op_pmcallocate *_pmc_config);
57789140c0SJoseph Koshy #endif
586411d14dSRuslan Bukin #if defined(__arm__)
596411d14dSRuslan Bukin static int armv7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
606411d14dSRuslan Bukin     struct pmc_op_pmcallocate *_pmc_config);
616411d14dSRuslan Bukin #endif
62bc88bb2bSRuslan Bukin #if defined(__aarch64__)
63bc88bb2bSRuslan Bukin static int arm64_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
64bc88bb2bSRuslan Bukin     struct pmc_op_pmcallocate *_pmc_config);
65e3572eb6SAleksandr Rybalko static int cmn600_pmu_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
66e3572eb6SAleksandr Rybalko     struct pmc_op_pmcallocate *_pmc_config);
67e3572eb6SAleksandr Rybalko static int dmc620_pmu_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
68e3572eb6SAleksandr Rybalko     struct pmc_op_pmcallocate *_pmc_config);
69bc88bb2bSRuslan Bukin #endif
70f5f9340bSFabien Thomas static int soft_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
71f5f9340bSFabien Thomas     struct pmc_op_pmcallocate *_pmc_config);
72660df75eSGeorge V. Neville-Neil 
737b25dccaSJustin Hibbits #if defined(__powerpc__)
74169dd953SJustin Hibbits static int powerpc_allocate_pmc(enum pmc_event _pe, char* ctrspec,
757b25dccaSJustin Hibbits 			     struct pmc_op_pmcallocate *_pmc_config);
767b25dccaSJustin Hibbits #endif /* __powerpc__ */
77660df75eSGeorge V. Neville-Neil 
78be75e578SMitchell Horne #define PMC_CALL(op, params)	syscall(pmc_syscall, (op), (params))
79ebccf1e3SJoseph Koshy 
80ebccf1e3SJoseph Koshy /*
81ebccf1e3SJoseph Koshy  * Event aliases provide a way for the user to ask for generic events
82ebccf1e3SJoseph Koshy  * like "cache-misses", or "instructions-retired".  These aliases are
83ebccf1e3SJoseph Koshy  * mapped to the appropriate canonical event descriptions using a
84ebccf1e3SJoseph Koshy  * lookup table.
85ebccf1e3SJoseph Koshy  */
86ebccf1e3SJoseph Koshy struct pmc_event_alias {
87ebccf1e3SJoseph Koshy 	const char	*pm_alias;
88ebccf1e3SJoseph Koshy 	const char	*pm_spec;
89ebccf1e3SJoseph Koshy };
90ebccf1e3SJoseph Koshy 
91ebccf1e3SJoseph Koshy static const struct pmc_event_alias *pmc_mdep_event_aliases;
92ebccf1e3SJoseph Koshy 
93ebccf1e3SJoseph Koshy /*
94789140c0SJoseph Koshy  * The pmc_event_descr structure maps symbolic names known to the user
95ebccf1e3SJoseph Koshy  * to integer codes used by the PMC KLD.
96ebccf1e3SJoseph Koshy  */
97ebccf1e3SJoseph Koshy struct pmc_event_descr {
98ebccf1e3SJoseph Koshy 	const char	*pm_ev_name;
99ebccf1e3SJoseph Koshy 	enum pmc_event	pm_ev_code;
100ebccf1e3SJoseph Koshy };
101ebccf1e3SJoseph Koshy 
102789140c0SJoseph Koshy /*
103789140c0SJoseph Koshy  * The pmc_class_descr structure maps class name prefixes for
104789140c0SJoseph Koshy  * event names to event tables and other PMC class data.
105789140c0SJoseph Koshy  */
106789140c0SJoseph Koshy struct pmc_class_descr {
107789140c0SJoseph Koshy 	const char	*pm_evc_name;
108789140c0SJoseph Koshy 	size_t		pm_evc_name_size;
109789140c0SJoseph Koshy 	enum pmc_class	pm_evc_class;
110789140c0SJoseph Koshy 	const struct pmc_event_descr *pm_evc_event_table;
111789140c0SJoseph Koshy 	size_t		pm_evc_event_table_size;
112789140c0SJoseph Koshy 	int		(*pm_evc_allocate_pmc)(enum pmc_event _pe,
113789140c0SJoseph Koshy 			    char *_ctrspec, struct pmc_op_pmcallocate *_pa);
114ebccf1e3SJoseph Koshy };
115ebccf1e3SJoseph Koshy 
116789140c0SJoseph Koshy #define	PMC_TABLE_SIZE(N)	(sizeof(N)/sizeof(N[0]))
117789140c0SJoseph Koshy #define	PMC_EVENT_TABLE_SIZE(N)	PMC_TABLE_SIZE(N##_event_table)
118789140c0SJoseph Koshy 
119789140c0SJoseph Koshy #undef	__PMC_EV
120789140c0SJoseph Koshy #define	__PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
121789140c0SJoseph Koshy 
122789140c0SJoseph Koshy /*
1230cfab8ddSJoseph Koshy  * PMC_CLASSDEP_TABLE(NAME, CLASS)
124789140c0SJoseph Koshy  *
1250cfab8ddSJoseph Koshy  * Define a table mapping event names and aliases to HWPMC event IDs.
126789140c0SJoseph Koshy  */
1270cfab8ddSJoseph Koshy #define	PMC_CLASSDEP_TABLE(N, C)				\
128789140c0SJoseph Koshy 	static const struct pmc_event_descr N##_event_table[] =	\
129789140c0SJoseph Koshy 	{							\
130789140c0SJoseph Koshy 		__PMC_EV_##C()					\
1310cfab8ddSJoseph Koshy 	}
1320cfab8ddSJoseph Koshy 
1330cfab8ddSJoseph Koshy PMC_CLASSDEP_TABLE(iaf, IAF);
1340cfab8ddSJoseph Koshy PMC_CLASSDEP_TABLE(k8, K8);
1356411d14dSRuslan Bukin PMC_CLASSDEP_TABLE(armv7, ARMV7);
136bc88bb2bSRuslan Bukin PMC_CLASSDEP_TABLE(armv8, ARMV8);
137e3572eb6SAleksandr Rybalko PMC_CLASSDEP_TABLE(cmn600_pmu, CMN600_PMU);
138e3572eb6SAleksandr Rybalko PMC_CLASSDEP_TABLE(dmc620_pmu_cd2, DMC620_PMU_CD2);
139e3572eb6SAleksandr Rybalko PMC_CLASSDEP_TABLE(dmc620_pmu_c, DMC620_PMU_C);
1407b25dccaSJustin Hibbits PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
141169dd953SJustin Hibbits PMC_CLASSDEP_TABLE(ppc970, PPC970);
142a7452468SJustin Hibbits PMC_CLASSDEP_TABLE(e500, E500);
1430cfab8ddSJoseph Koshy 
144f5f9340bSFabien Thomas static struct pmc_event_descr soft_event_table[PMC_EV_DYN_COUNT];
145f5f9340bSFabien Thomas 
1460cfab8ddSJoseph Koshy #undef	__PMC_EV_ALIAS
1470cfab8ddSJoseph Koshy #define	__PMC_EV_ALIAS(N,CODE) 	{ N, PMC_EV_##CODE },
1480cfab8ddSJoseph Koshy 
1499f22e095SJessica Clarke /*
1509f22e095SJessica Clarke  * TODO: Factor out the __PMC_EV_ARMV7/8 list into a single separate table
1519f22e095SJessica Clarke  * rather than duplicating for each core.
1529f22e095SJessica Clarke  */
1539f22e095SJessica Clarke 
1543e0bfdd8SRuslan Bukin static const struct pmc_event_descr cortex_a8_event_table[] =
1553e0bfdd8SRuslan Bukin {
1563e0bfdd8SRuslan Bukin 	__PMC_EV_ALIAS_ARMV7_CORTEX_A8()
1579f22e095SJessica Clarke 	__PMC_EV_ARMV7()
1583e0bfdd8SRuslan Bukin };
1593e0bfdd8SRuslan Bukin 
1603e0bfdd8SRuslan Bukin static const struct pmc_event_descr cortex_a9_event_table[] =
1613e0bfdd8SRuslan Bukin {
1623e0bfdd8SRuslan Bukin 	__PMC_EV_ALIAS_ARMV7_CORTEX_A9()
1639f22e095SJessica Clarke 	__PMC_EV_ARMV7()
1643e0bfdd8SRuslan Bukin };
1653e0bfdd8SRuslan Bukin 
166bc88bb2bSRuslan Bukin static const struct pmc_event_descr cortex_a53_event_table[] =
167bc88bb2bSRuslan Bukin {
168bc88bb2bSRuslan Bukin 	__PMC_EV_ALIAS_ARMV8_CORTEX_A53()
1699f22e095SJessica Clarke 	__PMC_EV_ARMV8()
170bc88bb2bSRuslan Bukin };
171bc88bb2bSRuslan Bukin 
172bc88bb2bSRuslan Bukin static const struct pmc_event_descr cortex_a57_event_table[] =
173bc88bb2bSRuslan Bukin {
174bc88bb2bSRuslan Bukin 	__PMC_EV_ALIAS_ARMV8_CORTEX_A57()
1759f22e095SJessica Clarke 	__PMC_EV_ARMV8()
176bc88bb2bSRuslan Bukin };
177bc88bb2bSRuslan Bukin 
178da11e1f9SAndrew Turner static const struct pmc_event_descr cortex_a76_event_table[] =
179da11e1f9SAndrew Turner {
180da11e1f9SAndrew Turner 	__PMC_EV_ALIAS_ARMV8_CORTEX_A76()
1819f22e095SJessica Clarke 	__PMC_EV_ARMV8()
182da11e1f9SAndrew Turner };
183da11e1f9SAndrew Turner 
184789140c0SJoseph Koshy static const struct pmc_event_descr tsc_event_table[] =
185789140c0SJoseph Koshy {
186937539e0SMitchell Horne 	__PMC_EV_ALIAS_TSC()
187789140c0SJoseph Koshy };
188789140c0SJoseph Koshy 
189789140c0SJoseph Koshy #undef	PMC_CLASS_TABLE_DESC
1900cfab8ddSJoseph Koshy #define	PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR)	\
1910cfab8ddSJoseph Koshy static const struct pmc_class_descr NAME##_class_table_descr =	\
1920cfab8ddSJoseph Koshy 	{							\
1930cfab8ddSJoseph Koshy 		.pm_evc_name  = #CLASS "-",			\
1940cfab8ddSJoseph Koshy 		.pm_evc_name_size = sizeof(#CLASS "-") - 1,	\
1950cfab8ddSJoseph Koshy 		.pm_evc_class = PMC_CLASS_##CLASS ,		\
1960cfab8ddSJoseph Koshy 		.pm_evc_event_table = EVENTS##_event_table ,	\
197789140c0SJoseph Koshy 		.pm_evc_event_table_size = 			\
1980cfab8ddSJoseph Koshy 			PMC_EVENT_TABLE_SIZE(EVENTS),		\
1990cfab8ddSJoseph Koshy 		.pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc	\
200789140c0SJoseph Koshy 	}
201789140c0SJoseph Koshy 
202789140c0SJoseph Koshy #if	defined(__i386__) || defined(__amd64__)
2030cfab8ddSJoseph Koshy PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
2040cfab8ddSJoseph Koshy #endif
2050cfab8ddSJoseph Koshy #if	defined(__i386__) || defined(__amd64__)
2060cfab8ddSJoseph Koshy PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
2070cfab8ddSJoseph Koshy #endif
2086411d14dSRuslan Bukin #if	defined(__arm__)
2093ed01392SBjoern A. Zeeb PMC_CLASS_TABLE_DESC(cortex_a8, ARMV7, cortex_a8, armv7);
2103e0bfdd8SRuslan Bukin PMC_CLASS_TABLE_DESC(cortex_a9, ARMV7, cortex_a9, armv7);
2116411d14dSRuslan Bukin #endif
212bc88bb2bSRuslan Bukin #if	defined(__aarch64__)
213bc88bb2bSRuslan Bukin PMC_CLASS_TABLE_DESC(cortex_a53, ARMV8, cortex_a53, arm64);
214bc88bb2bSRuslan Bukin PMC_CLASS_TABLE_DESC(cortex_a57, ARMV8, cortex_a57, arm64);
215da11e1f9SAndrew Turner PMC_CLASS_TABLE_DESC(cortex_a76, ARMV8, cortex_a76, arm64);
216e3572eb6SAleksandr Rybalko PMC_CLASS_TABLE_DESC(cmn600_pmu, CMN600_PMU, cmn600_pmu, cmn600_pmu);
217e3572eb6SAleksandr Rybalko PMC_CLASS_TABLE_DESC(dmc620_pmu_cd2, DMC620_PMU_CD2, dmc620_pmu_cd2, dmc620_pmu);
218e3572eb6SAleksandr Rybalko PMC_CLASS_TABLE_DESC(dmc620_pmu_c, DMC620_PMU_C, dmc620_pmu_c, dmc620_pmu);
219bc88bb2bSRuslan Bukin #endif
2207b25dccaSJustin Hibbits #if defined(__powerpc__)
221169dd953SJustin Hibbits PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, powerpc);
222169dd953SJustin Hibbits PMC_CLASS_TABLE_DESC(ppc970, PPC970, ppc970, powerpc);
223a7452468SJustin Hibbits PMC_CLASS_TABLE_DESC(e500, E500, e500, powerpc);
2247b25dccaSJustin Hibbits #endif
2257b25dccaSJustin Hibbits 
226f5f9340bSFabien Thomas static struct pmc_class_descr soft_class_table_descr =
227f5f9340bSFabien Thomas {
228f5f9340bSFabien Thomas 	.pm_evc_name  = "SOFT-",
229f5f9340bSFabien Thomas 	.pm_evc_name_size = sizeof("SOFT-") - 1,
230f5f9340bSFabien Thomas 	.pm_evc_class = PMC_CLASS_SOFT,
231f5f9340bSFabien Thomas 	.pm_evc_event_table = NULL,
232f5f9340bSFabien Thomas 	.pm_evc_event_table_size = 0,
233f5f9340bSFabien Thomas 	.pm_evc_allocate_pmc = soft_allocate_pmc
234f5f9340bSFabien Thomas };
235f5f9340bSFabien Thomas 
236789140c0SJoseph Koshy #undef	PMC_CLASS_TABLE_DESC
237789140c0SJoseph Koshy 
2380cfab8ddSJoseph Koshy static const struct pmc_class_descr **pmc_class_table;
2390cfab8ddSJoseph Koshy #define	PMC_CLASS_TABLE_SIZE	cpu_info.pm_nclass
2400cfab8ddSJoseph Koshy 
241ebccf1e3SJoseph Koshy /*
242ebccf1e3SJoseph Koshy  * Mapping tables, mapping enumeration values to human readable
243ebccf1e3SJoseph Koshy  * strings.
244ebccf1e3SJoseph Koshy  */
245ebccf1e3SJoseph Koshy 
246ebccf1e3SJoseph Koshy static const char * pmc_capability_names[] = {
247ebccf1e3SJoseph Koshy #undef	__PMC_CAP
248ebccf1e3SJoseph Koshy #define	__PMC_CAP(N,V,D)	#N ,
249ebccf1e3SJoseph Koshy 	__PMC_CAPS()
250ebccf1e3SJoseph Koshy };
251ebccf1e3SJoseph Koshy 
2520ceb54c2SJohn Baldwin struct pmc_class_map {
2530ceb54c2SJohn Baldwin 	enum pmc_class	pm_class;
2540ceb54c2SJohn Baldwin 	const char	*pm_name;
2550ceb54c2SJohn Baldwin };
2560ceb54c2SJohn Baldwin 
2570ceb54c2SJohn Baldwin static const struct pmc_class_map pmc_class_names[] = {
258ebccf1e3SJoseph Koshy #undef	__PMC_CLASS
2590ceb54c2SJohn Baldwin #define __PMC_CLASS(S,V,D) { .pm_class = PMC_CLASS_##S, .pm_name = #S } ,
260ebccf1e3SJoseph Koshy 	__PMC_CLASSES()
261ebccf1e3SJoseph Koshy };
262ebccf1e3SJoseph Koshy 
263789140c0SJoseph Koshy struct pmc_cputype_map {
264562fc14bSDimitry Andric 	enum pmc_cputype pm_cputype;
265789140c0SJoseph Koshy 	const char	*pm_name;
266789140c0SJoseph Koshy };
267789140c0SJoseph Koshy 
268789140c0SJoseph Koshy static const struct pmc_cputype_map pmc_cputype_names[] = {
269ebccf1e3SJoseph Koshy #undef	__PMC_CPU
270789140c0SJoseph Koshy #define	__PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
271ebccf1e3SJoseph Koshy 	__PMC_CPUS()
272ebccf1e3SJoseph Koshy };
273ebccf1e3SJoseph Koshy 
274ebccf1e3SJoseph Koshy static const char * pmc_disposition_names[] = {
275ebccf1e3SJoseph Koshy #undef	__PMC_DISP
276ebccf1e3SJoseph Koshy #define	__PMC_DISP(D)	#D ,
277ebccf1e3SJoseph Koshy 	__PMC_DISPOSITIONS()
278ebccf1e3SJoseph Koshy };
279ebccf1e3SJoseph Koshy 
280ebccf1e3SJoseph Koshy static const char * pmc_mode_names[] = {
281ebccf1e3SJoseph Koshy #undef  __PMC_MODE
282ebccf1e3SJoseph Koshy #define __PMC_MODE(M,N)	#M ,
283ebccf1e3SJoseph Koshy 	__PMC_MODES()
284ebccf1e3SJoseph Koshy };
285ebccf1e3SJoseph Koshy 
286ebccf1e3SJoseph Koshy static const char * pmc_state_names[] = {
287ebccf1e3SJoseph Koshy #undef  __PMC_STATE
288ebccf1e3SJoseph Koshy #define __PMC_STATE(S) #S ,
289ebccf1e3SJoseph Koshy 	__PMC_STATES()
290ebccf1e3SJoseph Koshy };
291ebccf1e3SJoseph Koshy 
292f5f9340bSFabien Thomas /*
293f5f9340bSFabien Thomas  * Filled in by pmc_init().
294f5f9340bSFabien Thomas  */
295f5f9340bSFabien Thomas static int pmc_syscall = -1;
296f5f9340bSFabien Thomas static struct pmc_cpuinfo cpu_info;
297f5f9340bSFabien Thomas static struct pmc_op_getdyneventinfo soft_event_info;
2981455fcd3SJoseph Koshy 
299ebccf1e3SJoseph Koshy /* Event masks for events */
300ebccf1e3SJoseph Koshy struct pmc_masks {
301ebccf1e3SJoseph Koshy 	const char	*pm_name;
3021e862e5aSFabien Thomas 	const uint64_t	pm_value;
303ebccf1e3SJoseph Koshy };
304ebccf1e3SJoseph Koshy #define	PMCMASK(N,V)	{ .pm_name = #N, .pm_value = (V) }
3051fa7f10bSFabien Thomas #define	NULLMASK	{ .pm_name = NULL }
306ebccf1e3SJoseph Koshy 
30786a65549SJoseph Koshy #if defined(__amd64__) || defined(__i386__)
308ebccf1e3SJoseph Koshy static int
pmc_parse_mask(const struct pmc_masks * pmask,char * p,uint64_t * evmask)3091e862e5aSFabien Thomas pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint64_t *evmask)
310ebccf1e3SJoseph Koshy {
311ebccf1e3SJoseph Koshy 	const struct pmc_masks *pm;
312ebccf1e3SJoseph Koshy 	char *q, *r;
313ebccf1e3SJoseph Koshy 	int c;
314ebccf1e3SJoseph Koshy 
315ebccf1e3SJoseph Koshy 	if (pmask == NULL)	/* no mask keywords */
316aa342b1fSJoseph Koshy 		return (-1);
317ebccf1e3SJoseph Koshy 	q = strchr(p, '=');	/* skip '=' */
318ebccf1e3SJoseph Koshy 	if (*++q == '\0')	/* no more data */
319aa342b1fSJoseph Koshy 		return (-1);
320ebccf1e3SJoseph Koshy 	c = 0;			/* count of mask keywords seen */
321ebccf1e3SJoseph Koshy 	while ((r = strsep(&q, "+")) != NULL) {
322789140c0SJoseph Koshy 		for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
323789140c0SJoseph Koshy 		    pm++)
324ebccf1e3SJoseph Koshy 			;
325ebccf1e3SJoseph Koshy 		if (pm->pm_name == NULL) /* not found */
326aa342b1fSJoseph Koshy 			return (-1);
327ebccf1e3SJoseph Koshy 		*evmask |= pm->pm_value;
328ebccf1e3SJoseph Koshy 		c++;
329ebccf1e3SJoseph Koshy 	}
330aa342b1fSJoseph Koshy 	return (c);
331ebccf1e3SJoseph Koshy }
33204e9feb0SMarcel Moolenaar #endif
333ebccf1e3SJoseph Koshy 
334ebccf1e3SJoseph Koshy #define	KWMATCH(p,kw)		(strcasecmp((p), (kw)) == 0)
335ebccf1e3SJoseph Koshy #define	KWPREFIXMATCH(p,kw)	(strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
336ebccf1e3SJoseph Koshy #define	EV_ALIAS(N,S)		{ .pm_alias = N, .pm_spec = S }
337ebccf1e3SJoseph Koshy 
33886a65549SJoseph Koshy #if defined(__amd64__) || defined(__i386__)
3390cfab8ddSJoseph Koshy /*
340f263522aSJoseph Koshy  * AMD K8 PMCs.
341f263522aSJoseph Koshy  *
342f263522aSJoseph Koshy  */
343f263522aSJoseph Koshy 
344f263522aSJoseph Koshy static struct pmc_event_alias k8_aliases[] = {
345f263522aSJoseph Koshy 	EV_ALIAS("branches",		"k8-fr-retired-taken-branches"),
346f263522aSJoseph Koshy 	EV_ALIAS("branch-mispredicts",
347f263522aSJoseph Koshy 	    "k8-fr-retired-taken-branches-mispredicted"),
348f263522aSJoseph Koshy 	EV_ALIAS("cycles",		"tsc"),
349f263522aSJoseph Koshy 	EV_ALIAS("dc-misses",		"k8-dc-miss"),
350f263522aSJoseph Koshy 	EV_ALIAS("ic-misses",		"k8-ic-miss"),
351f263522aSJoseph Koshy 	EV_ALIAS("instructions",	"k8-fr-retired-x86-instructions"),
352f263522aSJoseph Koshy 	EV_ALIAS("interrupts",		"k8-fr-taken-hardware-interrupts"),
353177a2f22SJoseph Koshy 	EV_ALIAS("unhalted-cycles",	"k8-bu-cpu-clk-unhalted"),
354f263522aSJoseph Koshy 	EV_ALIAS(NULL, NULL)
355f263522aSJoseph Koshy };
356f263522aSJoseph Koshy 
357f263522aSJoseph Koshy #define	__K8MASK(N,V) PMCMASK(N,(1 << (V)))
358f263522aSJoseph Koshy 
359f263522aSJoseph Koshy /*
360f263522aSJoseph Koshy  * Parsing tables
361f263522aSJoseph Koshy  */
362f263522aSJoseph Koshy 
363f263522aSJoseph Koshy /* fp dispatched fpu ops */
364f263522aSJoseph Koshy static const struct pmc_masks k8_mask_fdfo[] = {
365f263522aSJoseph Koshy 	__K8MASK(add-pipe-excluding-junk-ops,	0),
366f263522aSJoseph Koshy 	__K8MASK(multiply-pipe-excluding-junk-ops,	1),
367f263522aSJoseph Koshy 	__K8MASK(store-pipe-excluding-junk-ops,	2),
368f263522aSJoseph Koshy 	__K8MASK(add-pipe-junk-ops,		3),
369f263522aSJoseph Koshy 	__K8MASK(multiply-pipe-junk-ops,	4),
370f263522aSJoseph Koshy 	__K8MASK(store-pipe-junk-ops,		5),
371f263522aSJoseph Koshy 	NULLMASK
372f263522aSJoseph Koshy };
373f263522aSJoseph Koshy 
374f263522aSJoseph Koshy /* ls segment register loads */
375f263522aSJoseph Koshy static const struct pmc_masks k8_mask_lsrl[] = {
376f263522aSJoseph Koshy 	__K8MASK(es,	0),
377f263522aSJoseph Koshy 	__K8MASK(cs,	1),
378f263522aSJoseph Koshy 	__K8MASK(ss,	2),
379f263522aSJoseph Koshy 	__K8MASK(ds,	3),
380f263522aSJoseph Koshy 	__K8MASK(fs,	4),
381f263522aSJoseph Koshy 	__K8MASK(gs,	5),
382f263522aSJoseph Koshy 	__K8MASK(hs,	6),
383f263522aSJoseph Koshy 	NULLMASK
384f263522aSJoseph Koshy };
385f263522aSJoseph Koshy 
386f263522aSJoseph Koshy /* ls locked operation */
387f263522aSJoseph Koshy static const struct pmc_masks k8_mask_llo[] = {
388f263522aSJoseph Koshy 	__K8MASK(locked-instructions,	0),
389f263522aSJoseph Koshy 	__K8MASK(cycles-in-request,	1),
390f263522aSJoseph Koshy 	__K8MASK(cycles-to-complete,	2),
391f263522aSJoseph Koshy 	NULLMASK
392f263522aSJoseph Koshy };
393f263522aSJoseph Koshy 
394f263522aSJoseph Koshy /* dc refill from {l2,system} and dc copyback */
395f263522aSJoseph Koshy static const struct pmc_masks k8_mask_dc[] = {
396f263522aSJoseph Koshy 	__K8MASK(invalid,	0),
397f263522aSJoseph Koshy 	__K8MASK(shared,	1),
398f263522aSJoseph Koshy 	__K8MASK(exclusive,	2),
399f263522aSJoseph Koshy 	__K8MASK(owner,		3),
400f263522aSJoseph Koshy 	__K8MASK(modified,	4),
401f263522aSJoseph Koshy 	NULLMASK
402f263522aSJoseph Koshy };
403f263522aSJoseph Koshy 
404f263522aSJoseph Koshy /* dc one bit ecc error */
405f263522aSJoseph Koshy static const struct pmc_masks k8_mask_dobee[] = {
406f263522aSJoseph Koshy 	__K8MASK(scrubber,	0),
407f263522aSJoseph Koshy 	__K8MASK(piggyback,	1),
408f263522aSJoseph Koshy 	NULLMASK
409f263522aSJoseph Koshy };
410f263522aSJoseph Koshy 
411f263522aSJoseph Koshy /* dc dispatched prefetch instructions */
412f263522aSJoseph Koshy static const struct pmc_masks k8_mask_ddpi[] = {
413f263522aSJoseph Koshy 	__K8MASK(load,	0),
414f263522aSJoseph Koshy 	__K8MASK(store,	1),
415f263522aSJoseph Koshy 	__K8MASK(nta,	2),
416f263522aSJoseph Koshy 	NULLMASK
417f263522aSJoseph Koshy };
418f263522aSJoseph Koshy 
419f263522aSJoseph Koshy /* dc dcache accesses by locks */
420f263522aSJoseph Koshy static const struct pmc_masks k8_mask_dabl[] = {
421f263522aSJoseph Koshy 	__K8MASK(accesses,	0),
422f263522aSJoseph Koshy 	__K8MASK(misses,	1),
423f263522aSJoseph Koshy 	NULLMASK
424f263522aSJoseph Koshy };
425f263522aSJoseph Koshy 
426f263522aSJoseph Koshy /* bu internal l2 request */
427f263522aSJoseph Koshy static const struct pmc_masks k8_mask_bilr[] = {
428f263522aSJoseph Koshy 	__K8MASK(ic-fill,	0),
429f263522aSJoseph Koshy 	__K8MASK(dc-fill,	1),
430f263522aSJoseph Koshy 	__K8MASK(tlb-reload,	2),
431f263522aSJoseph Koshy 	__K8MASK(tag-snoop,	3),
432f263522aSJoseph Koshy 	__K8MASK(cancelled,	4),
433f263522aSJoseph Koshy 	NULLMASK
434f263522aSJoseph Koshy };
435f263522aSJoseph Koshy 
436f263522aSJoseph Koshy /* bu fill request l2 miss */
437f263522aSJoseph Koshy static const struct pmc_masks k8_mask_bfrlm[] = {
438f263522aSJoseph Koshy 	__K8MASK(ic-fill,	0),
439f263522aSJoseph Koshy 	__K8MASK(dc-fill,	1),
440f263522aSJoseph Koshy 	__K8MASK(tlb-reload,	2),
441f263522aSJoseph Koshy 	NULLMASK
442f263522aSJoseph Koshy };
443f263522aSJoseph Koshy 
444f263522aSJoseph Koshy /* bu fill into l2 */
445f263522aSJoseph Koshy static const struct pmc_masks k8_mask_bfil[] = {
446f263522aSJoseph Koshy 	__K8MASK(dirty-l2-victim,	0),
447f263522aSJoseph Koshy 	__K8MASK(victim-from-l2,	1),
448f263522aSJoseph Koshy 	NULLMASK
449f263522aSJoseph Koshy };
450f263522aSJoseph Koshy 
451f263522aSJoseph Koshy /* fr retired fpu instructions */
452f263522aSJoseph Koshy static const struct pmc_masks k8_mask_frfi[] = {
453f263522aSJoseph Koshy 	__K8MASK(x87,			0),
454f263522aSJoseph Koshy 	__K8MASK(mmx-3dnow,		1),
455f263522aSJoseph Koshy 	__K8MASK(packed-sse-sse2,	2),
456f263522aSJoseph Koshy 	__K8MASK(scalar-sse-sse2,	3),
457f263522aSJoseph Koshy 	NULLMASK
458f263522aSJoseph Koshy };
459f263522aSJoseph Koshy 
460f263522aSJoseph Koshy /* fr retired fastpath double op instructions */
461f263522aSJoseph Koshy static const struct pmc_masks k8_mask_frfdoi[] = {
462f263522aSJoseph Koshy 	__K8MASK(low-op-pos-0,		0),
463f263522aSJoseph Koshy 	__K8MASK(low-op-pos-1,		1),
464f263522aSJoseph Koshy 	__K8MASK(low-op-pos-2,		2),
465f263522aSJoseph Koshy 	NULLMASK
466f263522aSJoseph Koshy };
467f263522aSJoseph Koshy 
468f263522aSJoseph Koshy /* fr fpu exceptions */
469f263522aSJoseph Koshy static const struct pmc_masks k8_mask_ffe[] = {
470f263522aSJoseph Koshy 	__K8MASK(x87-reclass-microfaults,	0),
471f263522aSJoseph Koshy 	__K8MASK(sse-retype-microfaults,	1),
472f263522aSJoseph Koshy 	__K8MASK(sse-reclass-microfaults,	2),
473f263522aSJoseph Koshy 	__K8MASK(sse-and-x87-microtraps,	3),
474f263522aSJoseph Koshy 	NULLMASK
475f263522aSJoseph Koshy };
476f263522aSJoseph Koshy 
477f263522aSJoseph Koshy /* nb memory controller page access event */
478f263522aSJoseph Koshy static const struct pmc_masks k8_mask_nmcpae[] = {
479f263522aSJoseph Koshy 	__K8MASK(page-hit,	0),
480f263522aSJoseph Koshy 	__K8MASK(page-miss,	1),
481f263522aSJoseph Koshy 	__K8MASK(page-conflict,	2),
482f263522aSJoseph Koshy 	NULLMASK
483f263522aSJoseph Koshy };
484f263522aSJoseph Koshy 
485f263522aSJoseph Koshy /* nb memory controller turnaround */
486f263522aSJoseph Koshy static const struct pmc_masks k8_mask_nmct[] = {
487f263522aSJoseph Koshy 	__K8MASK(dimm-turnaround,		0),
488f263522aSJoseph Koshy 	__K8MASK(read-to-write-turnaround,	1),
489f263522aSJoseph Koshy 	__K8MASK(write-to-read-turnaround,	2),
490f263522aSJoseph Koshy 	NULLMASK
491f263522aSJoseph Koshy };
492f263522aSJoseph Koshy 
493f263522aSJoseph Koshy /* nb memory controller bypass saturation */
494f263522aSJoseph Koshy static const struct pmc_masks k8_mask_nmcbs[] = {
495f263522aSJoseph Koshy 	__K8MASK(memory-controller-hi-pri-bypass,	0),
496f263522aSJoseph Koshy 	__K8MASK(memory-controller-lo-pri-bypass,	1),
497f263522aSJoseph Koshy 	__K8MASK(dram-controller-interface-bypass,	2),
498f263522aSJoseph Koshy 	__K8MASK(dram-controller-queue-bypass,		3),
499f263522aSJoseph Koshy 	NULLMASK
500f263522aSJoseph Koshy };
501f263522aSJoseph Koshy 
502f263522aSJoseph Koshy /* nb sized commands */
503f263522aSJoseph Koshy static const struct pmc_masks k8_mask_nsc[] = {
504f263522aSJoseph Koshy 	__K8MASK(nonpostwrszbyte,	0),
505f263522aSJoseph Koshy 	__K8MASK(nonpostwrszdword,	1),
506f263522aSJoseph Koshy 	__K8MASK(postwrszbyte,		2),
507f263522aSJoseph Koshy 	__K8MASK(postwrszdword,		3),
508f263522aSJoseph Koshy 	__K8MASK(rdszbyte,		4),
509f263522aSJoseph Koshy 	__K8MASK(rdszdword,		5),
510f263522aSJoseph Koshy 	__K8MASK(rdmodwr,		6),
511f263522aSJoseph Koshy 	NULLMASK
512f263522aSJoseph Koshy };
513f263522aSJoseph Koshy 
514f263522aSJoseph Koshy /* nb probe result */
515f263522aSJoseph Koshy static const struct pmc_masks k8_mask_npr[] = {
516f263522aSJoseph Koshy 	__K8MASK(probe-miss,		0),
517f263522aSJoseph Koshy 	__K8MASK(probe-hit,		1),
518f263522aSJoseph Koshy 	__K8MASK(probe-hit-dirty-no-memory-cancel, 2),
519f263522aSJoseph Koshy 	__K8MASK(probe-hit-dirty-with-memory-cancel, 3),
520f263522aSJoseph Koshy 	NULLMASK
521f263522aSJoseph Koshy };
522f263522aSJoseph Koshy 
523f263522aSJoseph Koshy /* nb hypertransport bus bandwidth */
524f263522aSJoseph Koshy static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
525f263522aSJoseph Koshy 	__K8MASK(command,	0),
526f263522aSJoseph Koshy 	__K8MASK(data,	1),
527f263522aSJoseph Koshy 	__K8MASK(buffer-release, 2),
528f263522aSJoseph Koshy 	__K8MASK(nop,	3),
529f263522aSJoseph Koshy 	NULLMASK
530f263522aSJoseph Koshy };
531f263522aSJoseph Koshy 
532f263522aSJoseph Koshy #undef	__K8MASK
533f263522aSJoseph Koshy 
534f263522aSJoseph Koshy #define	K8_KW_COUNT	"count"
535f263522aSJoseph Koshy #define	K8_KW_EDGE	"edge"
536f263522aSJoseph Koshy #define	K8_KW_INV	"inv"
537f263522aSJoseph Koshy #define	K8_KW_MASK	"mask"
538f263522aSJoseph Koshy #define	K8_KW_OS	"os"
539f263522aSJoseph Koshy #define	K8_KW_USR	"usr"
540f263522aSJoseph Koshy 
541f263522aSJoseph Koshy static int
k8_allocate_pmc(enum pmc_event pe,char * ctrspec,struct pmc_op_pmcallocate * pmc_config)542f263522aSJoseph Koshy k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
543f263522aSJoseph Koshy     struct pmc_op_pmcallocate *pmc_config)
544f263522aSJoseph Koshy {
545f263522aSJoseph Koshy 	char		*e, *p, *q;
546f263522aSJoseph Koshy 	int		n;
5471e862e5aSFabien Thomas 	uint32_t	count;
5481e862e5aSFabien Thomas 	uint64_t	evmask;
549f263522aSJoseph Koshy 	const struct pmc_masks	*pm, *pmask;
550f263522aSJoseph Koshy 
551789140c0SJoseph Koshy 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
552f263522aSJoseph Koshy 	pmc_config->pm_md.pm_amd.pm_amd_config = 0;
553f263522aSJoseph Koshy 
554f263522aSJoseph Koshy 	pmask = NULL;
555f263522aSJoseph Koshy 	evmask = 0;
556f263522aSJoseph Koshy 
557f263522aSJoseph Koshy #define	__K8SETMASK(M) pmask = k8_mask_##M
558f263522aSJoseph Koshy 
559f263522aSJoseph Koshy 	/* setup parsing tables */
560f263522aSJoseph Koshy 	switch (pe) {
561f263522aSJoseph Koshy 	case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
562f263522aSJoseph Koshy 		__K8SETMASK(fdfo);
563f263522aSJoseph Koshy 		break;
564f263522aSJoseph Koshy 	case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
565f263522aSJoseph Koshy 		__K8SETMASK(lsrl);
566f263522aSJoseph Koshy 		break;
567f263522aSJoseph Koshy 	case PMC_EV_K8_LS_LOCKED_OPERATION:
568f263522aSJoseph Koshy 		__K8SETMASK(llo);
569f263522aSJoseph Koshy 		break;
570f263522aSJoseph Koshy 	case PMC_EV_K8_DC_REFILL_FROM_L2:
571f263522aSJoseph Koshy 	case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
572f263522aSJoseph Koshy 	case PMC_EV_K8_DC_COPYBACK:
573f263522aSJoseph Koshy 		__K8SETMASK(dc);
574f263522aSJoseph Koshy 		break;
575f263522aSJoseph Koshy 	case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
576f263522aSJoseph Koshy 		__K8SETMASK(dobee);
577f263522aSJoseph Koshy 		break;
578f263522aSJoseph Koshy 	case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
579f263522aSJoseph Koshy 		__K8SETMASK(ddpi);
580f263522aSJoseph Koshy 		break;
581f263522aSJoseph Koshy 	case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
582f263522aSJoseph Koshy 		__K8SETMASK(dabl);
583f263522aSJoseph Koshy 		break;
584f263522aSJoseph Koshy 	case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
585f263522aSJoseph Koshy 		__K8SETMASK(bilr);
586f263522aSJoseph Koshy 		break;
587f263522aSJoseph Koshy 	case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
588f263522aSJoseph Koshy 		__K8SETMASK(bfrlm);
589f263522aSJoseph Koshy 		break;
590f263522aSJoseph Koshy 	case PMC_EV_K8_BU_FILL_INTO_L2:
591f263522aSJoseph Koshy 		__K8SETMASK(bfil);
592f263522aSJoseph Koshy 		break;
593f263522aSJoseph Koshy 	case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
594f263522aSJoseph Koshy 		__K8SETMASK(frfi);
595f263522aSJoseph Koshy 		break;
596f263522aSJoseph Koshy 	case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
597f263522aSJoseph Koshy 		__K8SETMASK(frfdoi);
598f263522aSJoseph Koshy 		break;
599f263522aSJoseph Koshy 	case PMC_EV_K8_FR_FPU_EXCEPTIONS:
600f263522aSJoseph Koshy 		__K8SETMASK(ffe);
601f263522aSJoseph Koshy 		break;
602f263522aSJoseph Koshy 	case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
603f263522aSJoseph Koshy 		__K8SETMASK(nmcpae);
604f263522aSJoseph Koshy 		break;
605f263522aSJoseph Koshy 	case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
606f263522aSJoseph Koshy 		__K8SETMASK(nmct);
607f263522aSJoseph Koshy 		break;
608f263522aSJoseph Koshy 	case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
609f263522aSJoseph Koshy 		__K8SETMASK(nmcbs);
610f263522aSJoseph Koshy 		break;
611f263522aSJoseph Koshy 	case PMC_EV_K8_NB_SIZED_COMMANDS:
612f263522aSJoseph Koshy 		__K8SETMASK(nsc);
613f263522aSJoseph Koshy 		break;
614f263522aSJoseph Koshy 	case PMC_EV_K8_NB_PROBE_RESULT:
615f263522aSJoseph Koshy 		__K8SETMASK(npr);
616f263522aSJoseph Koshy 		break;
617f263522aSJoseph Koshy 	case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
618f263522aSJoseph Koshy 	case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
619f263522aSJoseph Koshy 	case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
620f263522aSJoseph Koshy 		__K8SETMASK(nhbb);
621f263522aSJoseph Koshy 		break;
622f263522aSJoseph Koshy 
623f263522aSJoseph Koshy 	default:
624f263522aSJoseph Koshy 		break;		/* no options defined */
625f263522aSJoseph Koshy 	}
626f263522aSJoseph Koshy 
627f263522aSJoseph Koshy 	while ((p = strsep(&ctrspec, ",")) != NULL) {
628f263522aSJoseph Koshy 		if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
629f263522aSJoseph Koshy 			q = strchr(p, '=');
630f263522aSJoseph Koshy 			if (*++q == '\0') /* skip '=' */
631aa342b1fSJoseph Koshy 				return (-1);
632f263522aSJoseph Koshy 
633f263522aSJoseph Koshy 			count = strtol(q, &e, 0);
634f263522aSJoseph Koshy 			if (e == q || *e != '\0')
635aa342b1fSJoseph Koshy 				return (-1);
636f263522aSJoseph Koshy 
637f263522aSJoseph Koshy 			pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
638f263522aSJoseph Koshy 			pmc_config->pm_md.pm_amd.pm_amd_config |=
639f263522aSJoseph Koshy 			    AMD_PMC_TO_COUNTER(count);
640f263522aSJoseph Koshy 
641f263522aSJoseph Koshy 		} else if (KWMATCH(p, K8_KW_EDGE)) {
642f263522aSJoseph Koshy 			pmc_config->pm_caps |= PMC_CAP_EDGE;
643f263522aSJoseph Koshy 		} else if (KWMATCH(p, K8_KW_INV)) {
644f263522aSJoseph Koshy 			pmc_config->pm_caps |= PMC_CAP_INVERT;
645f263522aSJoseph Koshy 		} else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
646f263522aSJoseph Koshy 			if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
647aa342b1fSJoseph Koshy 				return (-1);
648f263522aSJoseph Koshy 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
649f263522aSJoseph Koshy 		} else if (KWMATCH(p, K8_KW_OS)) {
650f263522aSJoseph Koshy 			pmc_config->pm_caps |= PMC_CAP_SYSTEM;
651f263522aSJoseph Koshy 		} else if (KWMATCH(p, K8_KW_USR)) {
652f263522aSJoseph Koshy 			pmc_config->pm_caps |= PMC_CAP_USER;
653f263522aSJoseph Koshy 		} else
654aa342b1fSJoseph Koshy 			return (-1);
655f263522aSJoseph Koshy 	}
656f263522aSJoseph Koshy 
657f263522aSJoseph Koshy 	/* other post processing */
658f263522aSJoseph Koshy 	switch (pe) {
659f263522aSJoseph Koshy 	case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
660f263522aSJoseph Koshy 	case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
661f263522aSJoseph Koshy 	case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
662f263522aSJoseph Koshy 	case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
663f263522aSJoseph Koshy 	case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
664f263522aSJoseph Koshy 	case PMC_EV_K8_FR_FPU_EXCEPTIONS:
665f263522aSJoseph Koshy 		/* XXX only available in rev B and later */
666f263522aSJoseph Koshy 		break;
667f263522aSJoseph Koshy 	case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
668f263522aSJoseph Koshy 		/* XXX only available in rev C and later */
669f263522aSJoseph Koshy 		break;
670f263522aSJoseph Koshy 	case PMC_EV_K8_LS_LOCKED_OPERATION:
671f263522aSJoseph Koshy 		/* XXX CPU Rev A,B evmask is to be zero */
672f263522aSJoseph Koshy 		if (evmask & (evmask - 1)) /* > 1 bit set */
673aa342b1fSJoseph Koshy 			return (-1);
674f263522aSJoseph Koshy 		if (evmask == 0) {
675f263522aSJoseph Koshy 			evmask = 0x01; /* Rev C and later: #instrs */
676f263522aSJoseph Koshy 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
677f263522aSJoseph Koshy 		}
678f263522aSJoseph Koshy 		break;
679f263522aSJoseph Koshy 	default:
680f263522aSJoseph Koshy 		if (evmask == 0 && pmask != NULL) {
681f263522aSJoseph Koshy 			for (pm = pmask; pm->pm_name; pm++)
682f263522aSJoseph Koshy 				evmask |= pm->pm_value;
683f263522aSJoseph Koshy 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
684f263522aSJoseph Koshy 		}
685f263522aSJoseph Koshy 	}
686f263522aSJoseph Koshy 
687f263522aSJoseph Koshy 	if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
688f263522aSJoseph Koshy 		pmc_config->pm_md.pm_amd.pm_amd_config =
689f263522aSJoseph Koshy 		    AMD_PMC_TO_UNITMASK(evmask);
690f263522aSJoseph Koshy 
691aa342b1fSJoseph Koshy 	return (0);
692f263522aSJoseph Koshy }
693f263522aSJoseph Koshy 
694f263522aSJoseph Koshy #endif
695f263522aSJoseph Koshy 
696789140c0SJoseph Koshy #if	defined(__i386__) || defined(__amd64__)
697789140c0SJoseph Koshy static int
tsc_allocate_pmc(enum pmc_event pe,char * ctrspec,struct pmc_op_pmcallocate * pmc_config)698789140c0SJoseph Koshy tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
699789140c0SJoseph Koshy     struct pmc_op_pmcallocate *pmc_config)
700789140c0SJoseph Koshy {
701789140c0SJoseph Koshy 	if (pe != PMC_EV_TSC_TSC)
702789140c0SJoseph Koshy 		return (-1);
703789140c0SJoseph Koshy 
704789140c0SJoseph Koshy 	/* TSC events must be unqualified. */
705789140c0SJoseph Koshy 	if (ctrspec && *ctrspec != '\0')
706789140c0SJoseph Koshy 		return (-1);
707789140c0SJoseph Koshy 
708789140c0SJoseph Koshy 	pmc_config->pm_md.pm_amd.pm_amd_config = 0;
709789140c0SJoseph Koshy 	pmc_config->pm_caps |= PMC_CAP_READ;
710789140c0SJoseph Koshy 
711789140c0SJoseph Koshy 	return (0);
712789140c0SJoseph Koshy }
713789140c0SJoseph Koshy #endif
714789140c0SJoseph Koshy 
715f5f9340bSFabien Thomas static struct pmc_event_alias generic_aliases[] = {
716f5f9340bSFabien Thomas 	EV_ALIAS("instructions",		"SOFT-CLOCK.HARD"),
717f5f9340bSFabien Thomas 	EV_ALIAS(NULL, NULL)
718f5f9340bSFabien Thomas };
719f5f9340bSFabien Thomas 
720f5f9340bSFabien Thomas static int
soft_allocate_pmc(enum pmc_event pe,char * ctrspec,struct pmc_op_pmcallocate * pmc_config)721f5f9340bSFabien Thomas soft_allocate_pmc(enum pmc_event pe, char *ctrspec,
722f5f9340bSFabien Thomas     struct pmc_op_pmcallocate *pmc_config)
723f5f9340bSFabien Thomas {
724f5f9340bSFabien Thomas 	(void)ctrspec;
725f5f9340bSFabien Thomas 	(void)pmc_config;
726f5f9340bSFabien Thomas 
727f0bbe9aaSDimitry Andric 	if ((int)pe < PMC_EV_SOFT_FIRST || (int)pe > PMC_EV_SOFT_LAST)
728f5f9340bSFabien Thomas 		return (-1);
729f5f9340bSFabien Thomas 
730f5f9340bSFabien Thomas 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
731f5f9340bSFabien Thomas 	return (0);
732f5f9340bSFabien Thomas }
733f5f9340bSFabien Thomas 
7346411d14dSRuslan Bukin #if	defined(__arm__)
7353e0bfdd8SRuslan Bukin static struct pmc_event_alias cortex_a8_aliases[] = {
7366411d14dSRuslan Bukin 	EV_ALIAS("dc-misses",		"L1_DCACHE_REFILL"),
7376411d14dSRuslan Bukin 	EV_ALIAS("ic-misses",		"L1_ICACHE_REFILL"),
7386411d14dSRuslan Bukin 	EV_ALIAS("instructions",	"INSTR_EXECUTED"),
7396411d14dSRuslan Bukin 	EV_ALIAS(NULL, NULL)
7406411d14dSRuslan Bukin };
7413e0bfdd8SRuslan Bukin 
7423e0bfdd8SRuslan Bukin static struct pmc_event_alias cortex_a9_aliases[] = {
7433e0bfdd8SRuslan Bukin 	EV_ALIAS("dc-misses",		"L1_DCACHE_REFILL"),
7443e0bfdd8SRuslan Bukin 	EV_ALIAS("ic-misses",		"L1_ICACHE_REFILL"),
7453e0bfdd8SRuslan Bukin 	EV_ALIAS("instructions",	"INSTR_EXECUTED"),
7463e0bfdd8SRuslan Bukin 	EV_ALIAS(NULL, NULL)
7473e0bfdd8SRuslan Bukin };
7483e0bfdd8SRuslan Bukin 
7496411d14dSRuslan Bukin static int
armv7_allocate_pmc(enum pmc_event pe,char * ctrspec __unused,struct pmc_op_pmcallocate * pmc_config __unused)7506411d14dSRuslan Bukin armv7_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
7516411d14dSRuslan Bukin     struct pmc_op_pmcallocate *pmc_config __unused)
7526411d14dSRuslan Bukin {
7536411d14dSRuslan Bukin 	switch (pe) {
7546411d14dSRuslan Bukin 	default:
7556411d14dSRuslan Bukin 		break;
7566411d14dSRuslan Bukin 	}
7576411d14dSRuslan Bukin 
7586411d14dSRuslan Bukin 	return (0);
7596411d14dSRuslan Bukin }
7606411d14dSRuslan Bukin #endif
7616411d14dSRuslan Bukin 
762bc88bb2bSRuslan Bukin #if	defined(__aarch64__)
763bc88bb2bSRuslan Bukin static struct pmc_event_alias cortex_a53_aliases[] = {
764bc88bb2bSRuslan Bukin 	EV_ALIAS(NULL, NULL)
765bc88bb2bSRuslan Bukin };
766bc88bb2bSRuslan Bukin static struct pmc_event_alias cortex_a57_aliases[] = {
767bc88bb2bSRuslan Bukin 	EV_ALIAS(NULL, NULL)
768bc88bb2bSRuslan Bukin };
769da11e1f9SAndrew Turner static struct pmc_event_alias cortex_a76_aliases[] = {
770da11e1f9SAndrew Turner 	EV_ALIAS(NULL, NULL)
771da11e1f9SAndrew Turner };
7726bb7ba4aSJohn Baldwin 
773bc88bb2bSRuslan Bukin static int
arm64_allocate_pmc(enum pmc_event pe,char * ctrspec,struct pmc_op_pmcallocate * pmc_config)7746bb7ba4aSJohn Baldwin arm64_allocate_pmc(enum pmc_event pe, char *ctrspec,
7756bb7ba4aSJohn Baldwin     struct pmc_op_pmcallocate *pmc_config)
776bc88bb2bSRuslan Bukin {
7776bb7ba4aSJohn Baldwin 	char *p;
7786bb7ba4aSJohn Baldwin 
7796bb7ba4aSJohn Baldwin 	while ((p = strsep(&ctrspec, ",")) != NULL) {
7806bb7ba4aSJohn Baldwin 		if (KWMATCH(p, "os"))
7816bb7ba4aSJohn Baldwin 			pmc_config->pm_caps |= PMC_CAP_SYSTEM;
7826bb7ba4aSJohn Baldwin 		else if (KWMATCH(p, "usr"))
7836bb7ba4aSJohn Baldwin 			pmc_config->pm_caps |= PMC_CAP_USER;
7846bb7ba4aSJohn Baldwin 		else
7856bb7ba4aSJohn Baldwin 			return (-1);
786bc88bb2bSRuslan Bukin 	}
787bc88bb2bSRuslan Bukin 
788bc88bb2bSRuslan Bukin 	return (0);
789bc88bb2bSRuslan Bukin }
790e3572eb6SAleksandr Rybalko 
791e3572eb6SAleksandr Rybalko static int
cmn600_pmu_allocate_pmc(enum pmc_event pe,char * ctrspec,struct pmc_op_pmcallocate * pmc_config)792e3572eb6SAleksandr Rybalko cmn600_pmu_allocate_pmc(enum pmc_event pe, char *ctrspec,
793e3572eb6SAleksandr Rybalko     struct pmc_op_pmcallocate *pmc_config)
794e3572eb6SAleksandr Rybalko {
795e3572eb6SAleksandr Rybalko 	uint32_t nodeid, occupancy, xpport, xpchannel;
796e3572eb6SAleksandr Rybalko 	char *e, *p, *q;
797e3572eb6SAleksandr Rybalko 	unsigned int i;
798e3572eb6SAleksandr Rybalko 	char *xpport_names[] = { "East", "West", "North", "South", "devport0",
799e3572eb6SAleksandr Rybalko 	    "devport1" };
800e3572eb6SAleksandr Rybalko 	char *xpchannel_names[] = { "REQ", "RSP", "SNP", "DAT" };
801e3572eb6SAleksandr Rybalko 
802e3572eb6SAleksandr Rybalko 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
803e3572eb6SAleksandr Rybalko 	pmc_config->pm_caps |= PMC_CAP_SYSTEM;
804e3572eb6SAleksandr Rybalko 	pmc_config->pm_md.pm_cmn600.pma_cmn600_config = 0;
805e3572eb6SAleksandr Rybalko 	/*
806e3572eb6SAleksandr Rybalko 	 * CMN600 extra fields:
807e3572eb6SAleksandr Rybalko 	 * * nodeid - node coordinates x[2-3],y[2-3],p[1],s[2]
808e3572eb6SAleksandr Rybalko 	 * 		width of x and y fields depend on matrix size.
809e3572eb6SAleksandr Rybalko 	 * * occupancy - numeric value to select desired filter.
810e3572eb6SAleksandr Rybalko 	 * * xpport - East, West, North, South, devport0, devport1 (or 0, 1, ..., 5)
811e3572eb6SAleksandr Rybalko 	 * * xpchannel - REQ, RSP, SNP, DAT (or 0, 1, 2, 3)
812e3572eb6SAleksandr Rybalko 	 */
813e3572eb6SAleksandr Rybalko 
814e3572eb6SAleksandr Rybalko 	while ((p = strsep(&ctrspec, ",")) != NULL) {
815e3572eb6SAleksandr Rybalko 		if (KWPREFIXMATCH(p, "nodeid=")) {
816e3572eb6SAleksandr Rybalko 			q = strchr(p, '=');
817e3572eb6SAleksandr Rybalko 			if (*++q == '\0') /* skip '=' */
818e3572eb6SAleksandr Rybalko 				return (-1);
819e3572eb6SAleksandr Rybalko 
820e3572eb6SAleksandr Rybalko 			nodeid = strtol(q, &e, 0);
821e3572eb6SAleksandr Rybalko 			if (e == q || *e != '\0')
822e3572eb6SAleksandr Rybalko 				return (-1);
823e3572eb6SAleksandr Rybalko 
824e3572eb6SAleksandr Rybalko 			pmc_config->pm_md.pm_cmn600.pma_cmn600_nodeid |= nodeid;
825e3572eb6SAleksandr Rybalko 
826e3572eb6SAleksandr Rybalko 		} else if (KWPREFIXMATCH(p, "occupancy=")) {
827e3572eb6SAleksandr Rybalko 			q = strchr(p, '=');
828e3572eb6SAleksandr Rybalko 			if (*++q == '\0') /* skip '=' */
829e3572eb6SAleksandr Rybalko 				return (-1);
830e3572eb6SAleksandr Rybalko 
831e3572eb6SAleksandr Rybalko 			occupancy = strtol(q, &e, 0);
832e3572eb6SAleksandr Rybalko 			if (e == q || *e != '\0')
833e3572eb6SAleksandr Rybalko 				return (-1);
834e3572eb6SAleksandr Rybalko 
835e3572eb6SAleksandr Rybalko 			pmc_config->pm_md.pm_cmn600.pma_cmn600_occupancy = occupancy;
836e3572eb6SAleksandr Rybalko 		} else if (KWPREFIXMATCH(p, "xpport=")) {
837e3572eb6SAleksandr Rybalko 			q = strchr(p, '=');
838e3572eb6SAleksandr Rybalko 			if (*++q == '\0') /* skip '=' */
839e3572eb6SAleksandr Rybalko 				return (-1);
840e3572eb6SAleksandr Rybalko 
841e3572eb6SAleksandr Rybalko 			xpport = strtol(q, &e, 0);
842e3572eb6SAleksandr Rybalko 			if (e == q || *e != '\0') {
843e3572eb6SAleksandr Rybalko 				for (i = 0; i < nitems(xpport_names); i++) {
844e3572eb6SAleksandr Rybalko 					if (strcasecmp(xpport_names[i], q) == 0) {
845e3572eb6SAleksandr Rybalko 						xpport = i;
846e3572eb6SAleksandr Rybalko 						break;
847e3572eb6SAleksandr Rybalko 					}
848e3572eb6SAleksandr Rybalko 				}
849e3572eb6SAleksandr Rybalko 				if (i == nitems(xpport_names))
850e3572eb6SAleksandr Rybalko 					return (-1);
851e3572eb6SAleksandr Rybalko 			}
852e3572eb6SAleksandr Rybalko 
853e3572eb6SAleksandr Rybalko 			pmc_config->pm_md.pm_cmn600.pma_cmn600_config |= xpport << 2;
854e3572eb6SAleksandr Rybalko 		} else if (KWPREFIXMATCH(p, "xpchannel=")) {
855e3572eb6SAleksandr Rybalko 			q = strchr(p, '=');
856e3572eb6SAleksandr Rybalko 			if (*++q == '\0') /* skip '=' */
857e3572eb6SAleksandr Rybalko 				return (-1);
858e3572eb6SAleksandr Rybalko 
859e3572eb6SAleksandr Rybalko 			xpchannel = strtol(q, &e, 0);
860e3572eb6SAleksandr Rybalko 			if (e == q || *e != '\0') {
861e3572eb6SAleksandr Rybalko 				for (i = 0; i < nitems(xpchannel_names); i++) {
862e3572eb6SAleksandr Rybalko 					if (strcasecmp(xpchannel_names[i], q) == 0) {
863e3572eb6SAleksandr Rybalko 						xpchannel = i;
864e3572eb6SAleksandr Rybalko 						break;
865e3572eb6SAleksandr Rybalko 					}
866e3572eb6SAleksandr Rybalko 				}
867e3572eb6SAleksandr Rybalko 				if (i == nitems(xpchannel_names))
868e3572eb6SAleksandr Rybalko 					return (-1);
869e3572eb6SAleksandr Rybalko 			}
870e3572eb6SAleksandr Rybalko 
871e3572eb6SAleksandr Rybalko 			pmc_config->pm_md.pm_cmn600.pma_cmn600_config |= xpchannel << 5;
872e3572eb6SAleksandr Rybalko 		} else
873e3572eb6SAleksandr Rybalko 			return (-1);
874e3572eb6SAleksandr Rybalko 	}
875e3572eb6SAleksandr Rybalko 
876e3572eb6SAleksandr Rybalko 	return (0);
877e3572eb6SAleksandr Rybalko }
878e3572eb6SAleksandr Rybalko 
879e3572eb6SAleksandr Rybalko static int
dmc620_pmu_allocate_pmc(enum pmc_event pe,char * ctrspec,struct pmc_op_pmcallocate * pmc_config)880e3572eb6SAleksandr Rybalko dmc620_pmu_allocate_pmc(enum pmc_event pe, char *ctrspec,
881e3572eb6SAleksandr Rybalko     struct pmc_op_pmcallocate *pmc_config)
882e3572eb6SAleksandr Rybalko {
883e3572eb6SAleksandr Rybalko 	char		*e, *p, *q;
884e3572eb6SAleksandr Rybalko 	uint64_t	match, mask;
885e3572eb6SAleksandr Rybalko 	uint32_t	count;
886e3572eb6SAleksandr Rybalko 
887e3572eb6SAleksandr Rybalko 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
888e3572eb6SAleksandr Rybalko 	pmc_config->pm_caps |= PMC_CAP_SYSTEM;
889e3572eb6SAleksandr Rybalko 	pmc_config->pm_md.pm_dmc620.pm_dmc620_config = 0;
890e3572eb6SAleksandr Rybalko 
891e3572eb6SAleksandr Rybalko 	while ((p = strsep(&ctrspec, ",")) != NULL) {
892e3572eb6SAleksandr Rybalko 		if (KWPREFIXMATCH(p, "count=")) {
893e3572eb6SAleksandr Rybalko 			q = strchr(p, '=');
894e3572eb6SAleksandr Rybalko 			if (*++q == '\0') /* skip '=' */
895e3572eb6SAleksandr Rybalko 				return (-1);
896e3572eb6SAleksandr Rybalko 
897e3572eb6SAleksandr Rybalko 			count = strtol(q, &e, 0);
898e3572eb6SAleksandr Rybalko 			if (e == q || *e != '\0')
899e3572eb6SAleksandr Rybalko 				return (-1);
900e3572eb6SAleksandr Rybalko 
901e3572eb6SAleksandr Rybalko 			pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
902e3572eb6SAleksandr Rybalko 			pmc_config->pm_md.pm_dmc620.pm_dmc620_config |= count;
903e3572eb6SAleksandr Rybalko 
904e3572eb6SAleksandr Rybalko 		} else if (KWMATCH(p, "inv")) {
905e3572eb6SAleksandr Rybalko 			pmc_config->pm_caps |= PMC_CAP_INVERT;
906e3572eb6SAleksandr Rybalko 		} else if (KWPREFIXMATCH(p, "match=")) {
907e3572eb6SAleksandr Rybalko 			match = strtol(q, &e, 0);
908e3572eb6SAleksandr Rybalko 			if (e == q || *e != '\0')
909e3572eb6SAleksandr Rybalko 				return (-1);
910e3572eb6SAleksandr Rybalko 
911e3572eb6SAleksandr Rybalko 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
912e3572eb6SAleksandr Rybalko 			pmc_config->pm_md.pm_dmc620.pm_dmc620_match = match;
913e3572eb6SAleksandr Rybalko 		} else if (KWPREFIXMATCH(p, "mask=")) {
914e3572eb6SAleksandr Rybalko 			q = strchr(p, '=');
915e3572eb6SAleksandr Rybalko 			if (*++q == '\0') /* skip '=' */
916e3572eb6SAleksandr Rybalko 				return (-1);
917e3572eb6SAleksandr Rybalko 
918e3572eb6SAleksandr Rybalko 			mask = strtol(q, &e, 0);
919e3572eb6SAleksandr Rybalko 			if (e == q || *e != '\0')
920e3572eb6SAleksandr Rybalko 				return (-1);
921e3572eb6SAleksandr Rybalko 
922e3572eb6SAleksandr Rybalko 			pmc_config->pm_md.pm_dmc620.pm_dmc620_mask = mask;
923e3572eb6SAleksandr Rybalko 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
924e3572eb6SAleksandr Rybalko 		} else
925e3572eb6SAleksandr Rybalko 			return (-1);
926e3572eb6SAleksandr Rybalko 	}
927e3572eb6SAleksandr Rybalko 
928e3572eb6SAleksandr Rybalko 	return (0);
929e3572eb6SAleksandr Rybalko }
930bc88bb2bSRuslan Bukin #endif
931bc88bb2bSRuslan Bukin 
9327b25dccaSJustin Hibbits #if defined(__powerpc__)
9337b25dccaSJustin Hibbits 
9347b25dccaSJustin Hibbits static struct pmc_event_alias ppc7450_aliases[] = {
9357b25dccaSJustin Hibbits 	EV_ALIAS("instructions",	"INSTR_COMPLETED"),
9367b25dccaSJustin Hibbits 	EV_ALIAS("branches",		"BRANCHES_COMPLETED"),
9377b25dccaSJustin Hibbits 	EV_ALIAS("branch-mispredicts",	"MISPREDICTED_BRANCHES"),
9387b25dccaSJustin Hibbits 	EV_ALIAS(NULL, NULL)
9397b25dccaSJustin Hibbits };
9407b25dccaSJustin Hibbits 
941169dd953SJustin Hibbits static struct pmc_event_alias ppc970_aliases[] = {
942169dd953SJustin Hibbits 	EV_ALIAS("instructions", "INSTR_COMPLETED"),
943169dd953SJustin Hibbits 	EV_ALIAS("cycles",       "CYCLES"),
944169dd953SJustin Hibbits 	EV_ALIAS(NULL, NULL)
945169dd953SJustin Hibbits };
946169dd953SJustin Hibbits 
947a7452468SJustin Hibbits static struct pmc_event_alias e500_aliases[] = {
948a7452468SJustin Hibbits 	EV_ALIAS("instructions", "INSTR_COMPLETED"),
949a7452468SJustin Hibbits 	EV_ALIAS("cycles",       "CYCLES"),
950a7452468SJustin Hibbits 	EV_ALIAS(NULL, NULL)
951a7452468SJustin Hibbits };
952a7452468SJustin Hibbits 
953169dd953SJustin Hibbits #define	POWERPC_KW_OS		"os"
954169dd953SJustin Hibbits #define	POWERPC_KW_USR		"usr"
955169dd953SJustin Hibbits #define	POWERPC_KW_ANYTHREAD	"anythread"
9567b25dccaSJustin Hibbits 
9577b25dccaSJustin Hibbits static int
powerpc_allocate_pmc(enum pmc_event pe,char * ctrspec __unused,struct pmc_op_pmcallocate * pmc_config __unused)958169dd953SJustin Hibbits powerpc_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
9597b25dccaSJustin Hibbits 		     struct pmc_op_pmcallocate *pmc_config __unused)
9607b25dccaSJustin Hibbits {
9617b25dccaSJustin Hibbits 	char *p;
9627b25dccaSJustin Hibbits 
9637b25dccaSJustin Hibbits 	(void) pe;
9647b25dccaSJustin Hibbits 
9657b25dccaSJustin Hibbits 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
9667b25dccaSJustin Hibbits 
9677b25dccaSJustin Hibbits 	while ((p = strsep(&ctrspec, ",")) != NULL) {
968169dd953SJustin Hibbits 		if (KWMATCH(p, POWERPC_KW_OS))
9697b25dccaSJustin Hibbits 			pmc_config->pm_caps |= PMC_CAP_SYSTEM;
970169dd953SJustin Hibbits 		else if (KWMATCH(p, POWERPC_KW_USR))
9717b25dccaSJustin Hibbits 			pmc_config->pm_caps |= PMC_CAP_USER;
972169dd953SJustin Hibbits 		else if (KWMATCH(p, POWERPC_KW_ANYTHREAD))
9737b25dccaSJustin Hibbits 			pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
9747b25dccaSJustin Hibbits 		else
9757b25dccaSJustin Hibbits 			return (-1);
9767b25dccaSJustin Hibbits 	}
9777b25dccaSJustin Hibbits 
9787b25dccaSJustin Hibbits 	return (0);
9797b25dccaSJustin Hibbits }
980169dd953SJustin Hibbits 
9817b25dccaSJustin Hibbits #endif /* __powerpc__ */
9827b25dccaSJustin Hibbits 
983660df75eSGeorge V. Neville-Neil 
984789140c0SJoseph Koshy /*
985789140c0SJoseph Koshy  * Match an event name `name' with its canonical form.
986789140c0SJoseph Koshy  *
9870cfab8ddSJoseph Koshy  * Matches are case insensitive and spaces, periods, underscores and
9880cfab8ddSJoseph Koshy  * hyphen characters are considered to match each other.
989789140c0SJoseph Koshy  *
990789140c0SJoseph Koshy  * Returns 1 for a match, 0 otherwise.
991789140c0SJoseph Koshy  */
992789140c0SJoseph Koshy 
993789140c0SJoseph Koshy static int
pmc_match_event_name(const char * name,const char * canonicalname)994789140c0SJoseph Koshy pmc_match_event_name(const char *name, const char *canonicalname)
995789140c0SJoseph Koshy {
996789140c0SJoseph Koshy 	int cc, nc;
997789140c0SJoseph Koshy 	const unsigned char *c, *n;
998789140c0SJoseph Koshy 
999789140c0SJoseph Koshy 	c = (const unsigned char *) canonicalname;
1000789140c0SJoseph Koshy 	n = (const unsigned char *) name;
1001789140c0SJoseph Koshy 
1002789140c0SJoseph Koshy 	for (; (nc = *n) && (cc = *c); n++, c++) {
1003789140c0SJoseph Koshy 
10040cfab8ddSJoseph Koshy 		if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
10050cfab8ddSJoseph Koshy 		    (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
1006789140c0SJoseph Koshy 			continue;
1007789140c0SJoseph Koshy 
10080cfab8ddSJoseph Koshy 		if (toupper(nc) == toupper(cc))
1009789140c0SJoseph Koshy 			continue;
1010789140c0SJoseph Koshy 
10110cfab8ddSJoseph Koshy 
1012789140c0SJoseph Koshy 		return (0);
1013789140c0SJoseph Koshy 	}
1014789140c0SJoseph Koshy 
1015789140c0SJoseph Koshy 	if (*n == '\0' && *c == '\0')
1016789140c0SJoseph Koshy 		return (1);
1017789140c0SJoseph Koshy 
1018789140c0SJoseph Koshy 	return (0);
1019789140c0SJoseph Koshy }
1020789140c0SJoseph Koshy 
1021789140c0SJoseph Koshy /*
1022789140c0SJoseph Koshy  * Match an event name against all the event named supported by a
1023789140c0SJoseph Koshy  * PMC class.
1024789140c0SJoseph Koshy  *
1025789140c0SJoseph Koshy  * Returns an event descriptor pointer on match or NULL otherwise.
1026789140c0SJoseph Koshy  */
1027789140c0SJoseph Koshy static const struct pmc_event_descr *
pmc_match_event_class(const char * name,const struct pmc_class_descr * pcd)1028789140c0SJoseph Koshy pmc_match_event_class(const char *name,
1029789140c0SJoseph Koshy     const struct pmc_class_descr *pcd)
1030789140c0SJoseph Koshy {
1031789140c0SJoseph Koshy 	size_t n;
1032789140c0SJoseph Koshy 	const struct pmc_event_descr *ev;
1033789140c0SJoseph Koshy 
1034789140c0SJoseph Koshy 	ev = pcd->pm_evc_event_table;
1035789140c0SJoseph Koshy 	for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
1036789140c0SJoseph Koshy 		if (pmc_match_event_name(name, ev->pm_ev_name))
1037789140c0SJoseph Koshy 			return (ev);
1038789140c0SJoseph Koshy 
1039789140c0SJoseph Koshy 	return (NULL);
1040789140c0SJoseph Koshy }
1041789140c0SJoseph Koshy 
1042ebccf1e3SJoseph Koshy /*
1043ebccf1e3SJoseph Koshy  * API entry points
1044ebccf1e3SJoseph Koshy  */
1045ebccf1e3SJoseph Koshy 
1046ebccf1e3SJoseph Koshy int
pmc_allocate(const char * ctrspec,enum pmc_mode mode,uint32_t flags,int cpu,pmc_id_t * pmcid,uint64_t count)1047ebccf1e3SJoseph Koshy pmc_allocate(const char *ctrspec, enum pmc_mode mode,
1048b2ca2e50SMatt Macy     uint32_t flags, int cpu, pmc_id_t *pmcid,
1049b2ca2e50SMatt Macy     uint64_t count)
1050ebccf1e3SJoseph Koshy {
1051789140c0SJoseph Koshy 	size_t n;
1052ebccf1e3SJoseph Koshy 	int retval;
1053ebccf1e3SJoseph Koshy 	char *r, *spec_copy;
1054ebccf1e3SJoseph Koshy 	const char *ctrname;
1055789140c0SJoseph Koshy 	const struct pmc_event_descr *ev;
1056789140c0SJoseph Koshy 	const struct pmc_event_alias *alias;
1057ebccf1e3SJoseph Koshy 	struct pmc_op_pmcallocate pmc_config;
1058789140c0SJoseph Koshy 	const struct pmc_class_descr *pcd;
1059ebccf1e3SJoseph Koshy 
1060ebccf1e3SJoseph Koshy 	spec_copy = NULL;
1061ebccf1e3SJoseph Koshy 	retval    = -1;
1062ebccf1e3SJoseph Koshy 
1063ebccf1e3SJoseph Koshy 	if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
1064ebccf1e3SJoseph Koshy 	    mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
10654f9b4ac4SEric van Gyzen 		errno = EINVAL;
10664f9b4ac4SEric van Gyzen 		goto out;
1067959826caSMatt Macy 	}
1068959826caSMatt Macy 	bzero(&pmc_config, sizeof(pmc_config));
1069959826caSMatt Macy 	pmc_config.pm_cpu   = cpu;
1070959826caSMatt Macy 	pmc_config.pm_mode  = mode;
1071959826caSMatt Macy 	pmc_config.pm_flags = flags;
1072b2ca2e50SMatt Macy 	pmc_config.pm_count = count;
1073959826caSMatt Macy 	if (PMC_IS_SAMPLING_MODE(mode))
1074959826caSMatt Macy 		pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
1075dfdc57e8SMitchell Horne 
1076959826caSMatt Macy 	/*
1077dfdc57e8SMitchell Horne 	 * Try to pull the raw event ID directly from the pmu-events table. If
1078dfdc57e8SMitchell Horne 	 * this is unsupported on the platform, or the event is not found,
1079dfdc57e8SMitchell Horne 	 * continue with searching the regular event tables.
1080959826caSMatt Macy 	 */
1081959826caSMatt Macy 	r = spec_copy = strdup(ctrspec);
1082959826caSMatt Macy 	ctrname = strsep(&r, ",");
108312193215SMatt Macy 	if (pmc_pmu_enabled()) {
1084*c190fb35SMitchell Horne 		if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0)
1085dfdc57e8SMitchell Horne 			goto found;
108621f7397aSJessica Clarke 	}
1087959826caSMatt Macy 	free(spec_copy);
1088959826caSMatt Macy 	spec_copy = NULL;
1089ebccf1e3SJoseph Koshy 
1090ebccf1e3SJoseph Koshy 	/* replace an event alias with the canonical event specifier */
1091ebccf1e3SJoseph Koshy 	if (pmc_mdep_event_aliases)
1092789140c0SJoseph Koshy 		for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
1093789140c0SJoseph Koshy 			if (!strcasecmp(ctrspec, alias->pm_alias)) {
1094789140c0SJoseph Koshy 				spec_copy = strdup(alias->pm_spec);
1095ebccf1e3SJoseph Koshy 				break;
1096ebccf1e3SJoseph Koshy 			}
1097ebccf1e3SJoseph Koshy 
1098ebccf1e3SJoseph Koshy 	if (spec_copy == NULL)
1099ebccf1e3SJoseph Koshy 		spec_copy = strdup(ctrspec);
1100ebccf1e3SJoseph Koshy 
1101ebccf1e3SJoseph Koshy 	r = spec_copy;
1102ebccf1e3SJoseph Koshy 	ctrname = strsep(&r, ",");
1103ebccf1e3SJoseph Koshy 
1104789140c0SJoseph Koshy 	/*
1105789140c0SJoseph Koshy 	 * If a explicit class prefix was given by the user, restrict the
1106789140c0SJoseph Koshy 	 * search for the event to the specified PMC class.
1107789140c0SJoseph Koshy 	 */
1108789140c0SJoseph Koshy 	ev = NULL;
11090cfab8ddSJoseph Koshy 	for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
11100cfab8ddSJoseph Koshy 		pcd = pmc_class_table[n];
1111da13ef6aSMitchell Horne 		if (pcd != NULL && strncasecmp(ctrname, pcd->pm_evc_name,
1112789140c0SJoseph Koshy 		    pcd->pm_evc_name_size) == 0) {
1113789140c0SJoseph Koshy 			if ((ev = pmc_match_event_class(ctrname +
1114789140c0SJoseph Koshy 			    pcd->pm_evc_name_size, pcd)) == NULL) {
1115789140c0SJoseph Koshy 				errno = EINVAL;
1116789140c0SJoseph Koshy 				goto out;
1117789140c0SJoseph Koshy 			}
1118ebccf1e3SJoseph Koshy 			break;
1119789140c0SJoseph Koshy 		}
1120789140c0SJoseph Koshy 	}
1121ebccf1e3SJoseph Koshy 
1122789140c0SJoseph Koshy 	/*
1123789140c0SJoseph Koshy 	 * Otherwise, search for this event in all compatible PMC
1124789140c0SJoseph Koshy 	 * classes.
1125789140c0SJoseph Koshy 	 */
11260cfab8ddSJoseph Koshy 	for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
11270cfab8ddSJoseph Koshy 		pcd = pmc_class_table[n];
1128da13ef6aSMitchell Horne 		if (pcd != NULL)
1129789140c0SJoseph Koshy 			ev = pmc_match_event_class(ctrname, pcd);
1130789140c0SJoseph Koshy 	}
1131789140c0SJoseph Koshy 
1132789140c0SJoseph Koshy 	if (ev == NULL) {
1133ebccf1e3SJoseph Koshy 		errno = EINVAL;
1134ebccf1e3SJoseph Koshy 		goto out;
1135ebccf1e3SJoseph Koshy 	}
1136ebccf1e3SJoseph Koshy 
1137789140c0SJoseph Koshy 	pmc_config.pm_ev    = ev->pm_ev_code;
1138789140c0SJoseph Koshy 	pmc_config.pm_class = pcd->pm_evc_class;
1139ebccf1e3SJoseph Koshy 
1140789140c0SJoseph Koshy  	if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
1141ebccf1e3SJoseph Koshy 		errno = EINVAL;
1142ebccf1e3SJoseph Koshy 		goto out;
1143ebccf1e3SJoseph Koshy 	}
1144ebccf1e3SJoseph Koshy 
1145dfdc57e8SMitchell Horne found:
1146be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_PMCALLOCATE, &pmc_config) == 0) {
1147ebccf1e3SJoseph Koshy 		*pmcid = pmc_config.pm_pmcid;
1148ebccf1e3SJoseph Koshy 		retval = 0;
1149dfdc57e8SMitchell Horne 	}
1150ebccf1e3SJoseph Koshy out:
1151ebccf1e3SJoseph Koshy 	if (spec_copy)
1152ebccf1e3SJoseph Koshy 		free(spec_copy);
1153ebccf1e3SJoseph Koshy 
1154aa342b1fSJoseph Koshy 	return (retval);
1155ebccf1e3SJoseph Koshy }
1156ebccf1e3SJoseph Koshy 
1157ebccf1e3SJoseph Koshy int
pmc_attach(pmc_id_t pmc,pid_t pid)1158ebccf1e3SJoseph Koshy pmc_attach(pmc_id_t pmc, pid_t pid)
1159ebccf1e3SJoseph Koshy {
1160ebccf1e3SJoseph Koshy 	struct pmc_op_pmcattach pmc_attach_args;
1161ebccf1e3SJoseph Koshy 
1162ebccf1e3SJoseph Koshy 	pmc_attach_args.pm_pmc = pmc;
1163ebccf1e3SJoseph Koshy 	pmc_attach_args.pm_pid = pid;
1164ebccf1e3SJoseph Koshy 
1165be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_PMCATTACH, &pmc_attach_args));
1166ebccf1e3SJoseph Koshy }
1167ebccf1e3SJoseph Koshy 
1168ebccf1e3SJoseph Koshy int
pmc_capabilities(pmc_id_t pmcid,uint32_t * caps)1169c5153e19SJoseph Koshy pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
1170c5153e19SJoseph Koshy {
1171c5153e19SJoseph Koshy 	unsigned int i;
1172c5153e19SJoseph Koshy 	enum pmc_class cl;
1173c5153e19SJoseph Koshy 
1174c5153e19SJoseph Koshy 	cl = PMC_ID_TO_CLASS(pmcid);
1175c5153e19SJoseph Koshy 	for (i = 0; i < cpu_info.pm_nclass; i++)
1176c5153e19SJoseph Koshy 		if (cpu_info.pm_classes[i].pm_class == cl) {
1177c5153e19SJoseph Koshy 			*caps = cpu_info.pm_classes[i].pm_caps;
1178aa342b1fSJoseph Koshy 			return (0);
1179c5153e19SJoseph Koshy 		}
1180484202faSJoseph Koshy 	errno = EINVAL;
1181484202faSJoseph Koshy 	return (-1);
1182c5153e19SJoseph Koshy }
1183c5153e19SJoseph Koshy 
1184f263522aSJoseph Koshy int
pmc_configure_logfile(int fd)1185f263522aSJoseph Koshy pmc_configure_logfile(int fd)
1186ebccf1e3SJoseph Koshy {
1187f263522aSJoseph Koshy 	struct pmc_op_configurelog cla;
1188f263522aSJoseph Koshy 
11898e63e787SJessica Clarke 	cla.pm_flags = 0;
1190f263522aSJoseph Koshy 	cla.pm_logfd = fd;
1191be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_CONFIGURELOG, &cla) < 0)
1192aa342b1fSJoseph Koshy 		return (-1);
1193aa342b1fSJoseph Koshy 	return (0);
1194ebccf1e3SJoseph Koshy }
1195ebccf1e3SJoseph Koshy 
1196f263522aSJoseph Koshy int
pmc_cpuinfo(const struct pmc_cpuinfo ** pci)1197f263522aSJoseph Koshy pmc_cpuinfo(const struct pmc_cpuinfo **pci)
1198ebccf1e3SJoseph Koshy {
1199f263522aSJoseph Koshy 	if (pmc_syscall == -1) {
1200f263522aSJoseph Koshy 		errno = ENXIO;
1201aa342b1fSJoseph Koshy 		return (-1);
1202ebccf1e3SJoseph Koshy 	}
1203ebccf1e3SJoseph Koshy 
12041455fcd3SJoseph Koshy 	*pci = &cpu_info;
1205aa342b1fSJoseph Koshy 	return (0);
1206ebccf1e3SJoseph Koshy }
1207ebccf1e3SJoseph Koshy 
1208f263522aSJoseph Koshy int
pmc_detach(pmc_id_t pmc,pid_t pid)1209f263522aSJoseph Koshy pmc_detach(pmc_id_t pmc, pid_t pid)
1210ebccf1e3SJoseph Koshy {
1211f263522aSJoseph Koshy 	struct pmc_op_pmcattach pmc_detach_args;
1212ebccf1e3SJoseph Koshy 
1213f263522aSJoseph Koshy 	pmc_detach_args.pm_pmc = pmc;
1214f263522aSJoseph Koshy 	pmc_detach_args.pm_pid = pid;
1215be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_PMCDETACH, &pmc_detach_args));
1216ebccf1e3SJoseph Koshy }
1217ebccf1e3SJoseph Koshy 
1218f263522aSJoseph Koshy int
pmc_disable(int cpu,int pmc)1219f263522aSJoseph Koshy pmc_disable(int cpu, int pmc)
1220ebccf1e3SJoseph Koshy {
1221f263522aSJoseph Koshy 	struct pmc_op_pmcadmin ssa;
1222ebccf1e3SJoseph Koshy 
1223f263522aSJoseph Koshy 	ssa.pm_cpu = cpu;
1224f263522aSJoseph Koshy 	ssa.pm_pmc = pmc;
1225f263522aSJoseph Koshy 	ssa.pm_state = PMC_STATE_DISABLED;
1226be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_PMCADMIN, &ssa));
1227ebccf1e3SJoseph Koshy }
1228ebccf1e3SJoseph Koshy 
1229f263522aSJoseph Koshy int
pmc_enable(int cpu,int pmc)1230f263522aSJoseph Koshy pmc_enable(int cpu, int pmc)
1231ebccf1e3SJoseph Koshy {
1232f263522aSJoseph Koshy 	struct pmc_op_pmcadmin ssa;
1233ebccf1e3SJoseph Koshy 
1234f263522aSJoseph Koshy 	ssa.pm_cpu = cpu;
1235f263522aSJoseph Koshy 	ssa.pm_pmc = pmc;
1236f263522aSJoseph Koshy 	ssa.pm_state = PMC_STATE_FREE;
1237be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_PMCADMIN, &ssa));
1238ebccf1e3SJoseph Koshy }
1239ebccf1e3SJoseph Koshy 
1240ebccf1e3SJoseph Koshy /*
1241ebccf1e3SJoseph Koshy  * Return a list of events known to a given PMC class.  'cl' is the
1242ebccf1e3SJoseph Koshy  * PMC class identifier, 'eventnames' is the returned list of 'const
1243ebccf1e3SJoseph Koshy  * char *' pointers pointing to the names of the events. 'nevents' is
1244ebccf1e3SJoseph Koshy  * the number of event name pointers returned.
1245ebccf1e3SJoseph Koshy  *
1246ebccf1e3SJoseph Koshy  * The space for 'eventnames' is allocated using malloc(3).  The caller
1247ebccf1e3SJoseph Koshy  * is responsible for freeing this space when done.
1248ebccf1e3SJoseph Koshy  */
1249ebccf1e3SJoseph Koshy int
pmc_event_names_of_class(enum pmc_class cl,const char *** eventnames,int * nevents)1250ebccf1e3SJoseph Koshy pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
1251ebccf1e3SJoseph Koshy     int *nevents)
1252ebccf1e3SJoseph Koshy {
1253ebccf1e3SJoseph Koshy 	int count;
1254ebccf1e3SJoseph Koshy 	const char **names;
1255ebccf1e3SJoseph Koshy 	const struct pmc_event_descr *ev;
1256ebccf1e3SJoseph Koshy 
1257ebccf1e3SJoseph Koshy 	switch (cl)
1258ebccf1e3SJoseph Koshy 	{
12590cfab8ddSJoseph Koshy 	case PMC_CLASS_IAF:
12600cfab8ddSJoseph Koshy 		ev = iaf_event_table;
12610cfab8ddSJoseph Koshy 		count = PMC_EVENT_TABLE_SIZE(iaf);
12620cfab8ddSJoseph Koshy 		break;
1263ebccf1e3SJoseph Koshy 	case PMC_CLASS_TSC:
1264789140c0SJoseph Koshy 		ev = tsc_event_table;
1265789140c0SJoseph Koshy 		count = PMC_EVENT_TABLE_SIZE(tsc);
1266ebccf1e3SJoseph Koshy 		break;
1267ebccf1e3SJoseph Koshy 	case PMC_CLASS_K8:
1268789140c0SJoseph Koshy 		ev = k8_event_table;
1269789140c0SJoseph Koshy 		count = PMC_EVENT_TABLE_SIZE(k8);
1270ebccf1e3SJoseph Koshy 		break;
12716411d14dSRuslan Bukin 	case PMC_CLASS_ARMV7:
12723e0bfdd8SRuslan Bukin 		switch (cpu_info.pm_cputype) {
12733e0bfdd8SRuslan Bukin 		default:
12743e0bfdd8SRuslan Bukin 		case PMC_CPU_ARMV7_CORTEX_A8:
12753e0bfdd8SRuslan Bukin 			ev = cortex_a8_event_table;
12763e0bfdd8SRuslan Bukin 			count = PMC_EVENT_TABLE_SIZE(cortex_a8);
12773e0bfdd8SRuslan Bukin 			break;
12783e0bfdd8SRuslan Bukin 		case PMC_CPU_ARMV7_CORTEX_A9:
12793e0bfdd8SRuslan Bukin 			ev = cortex_a9_event_table;
12803e0bfdd8SRuslan Bukin 			count = PMC_EVENT_TABLE_SIZE(cortex_a9);
12813e0bfdd8SRuslan Bukin 			break;
12823e0bfdd8SRuslan Bukin 		}
12836411d14dSRuslan Bukin 		break;
1284bc88bb2bSRuslan Bukin 	case PMC_CLASS_ARMV8:
1285bc88bb2bSRuslan Bukin 		switch (cpu_info.pm_cputype) {
1286bc88bb2bSRuslan Bukin 		default:
1287bc88bb2bSRuslan Bukin 		case PMC_CPU_ARMV8_CORTEX_A53:
1288bc88bb2bSRuslan Bukin 			ev = cortex_a53_event_table;
1289bc88bb2bSRuslan Bukin 			count = PMC_EVENT_TABLE_SIZE(cortex_a53);
1290bc88bb2bSRuslan Bukin 			break;
1291bc88bb2bSRuslan Bukin 		case PMC_CPU_ARMV8_CORTEX_A57:
1292bc88bb2bSRuslan Bukin 			ev = cortex_a57_event_table;
1293bc88bb2bSRuslan Bukin 			count = PMC_EVENT_TABLE_SIZE(cortex_a57);
1294bc88bb2bSRuslan Bukin 			break;
1295da11e1f9SAndrew Turner 		case PMC_CPU_ARMV8_CORTEX_A76:
1296da11e1f9SAndrew Turner 			ev = cortex_a76_event_table;
1297da11e1f9SAndrew Turner 			count = PMC_EVENT_TABLE_SIZE(cortex_a76);
1298da11e1f9SAndrew Turner 			break;
1299bc88bb2bSRuslan Bukin 		}
1300bc88bb2bSRuslan Bukin 		break;
1301e3572eb6SAleksandr Rybalko 	case PMC_CLASS_CMN600_PMU:
1302e3572eb6SAleksandr Rybalko 		ev = cmn600_pmu_event_table;
1303e3572eb6SAleksandr Rybalko 		count = PMC_EVENT_TABLE_SIZE(cmn600_pmu);
1304e3572eb6SAleksandr Rybalko 		break;
1305e3572eb6SAleksandr Rybalko 	case PMC_CLASS_DMC620_PMU_CD2:
1306e3572eb6SAleksandr Rybalko 		ev = dmc620_pmu_cd2_event_table;
1307e3572eb6SAleksandr Rybalko 		count = PMC_EVENT_TABLE_SIZE(dmc620_pmu_cd2);
1308e3572eb6SAleksandr Rybalko 		break;
1309e3572eb6SAleksandr Rybalko 	case PMC_CLASS_DMC620_PMU_C:
1310e3572eb6SAleksandr Rybalko 		ev = dmc620_pmu_c_event_table;
1311e3572eb6SAleksandr Rybalko 		count = PMC_EVENT_TABLE_SIZE(dmc620_pmu_c);
1312e3572eb6SAleksandr Rybalko 		break;
13137b25dccaSJustin Hibbits 	case PMC_CLASS_PPC7450:
13147b25dccaSJustin Hibbits 		ev = ppc7450_event_table;
13157b25dccaSJustin Hibbits 		count = PMC_EVENT_TABLE_SIZE(ppc7450);
13167b25dccaSJustin Hibbits 		break;
1317169dd953SJustin Hibbits 	case PMC_CLASS_PPC970:
1318169dd953SJustin Hibbits 		ev = ppc970_event_table;
1319169dd953SJustin Hibbits 		count = PMC_EVENT_TABLE_SIZE(ppc970);
1320169dd953SJustin Hibbits 		break;
1321a7452468SJustin Hibbits 	case PMC_CLASS_E500:
1322a7452468SJustin Hibbits 		ev = e500_event_table;
1323a7452468SJustin Hibbits 		count = PMC_EVENT_TABLE_SIZE(e500);
1324a7452468SJustin Hibbits 		break;
1325f5f9340bSFabien Thomas 	case PMC_CLASS_SOFT:
1326f5f9340bSFabien Thomas 		ev = soft_event_table;
1327f5f9340bSFabien Thomas 		count = soft_event_info.pm_nevent;
1328f5f9340bSFabien Thomas 		break;
1329ebccf1e3SJoseph Koshy 	default:
1330ebccf1e3SJoseph Koshy 		errno = EINVAL;
1331aa342b1fSJoseph Koshy 		return (-1);
1332ebccf1e3SJoseph Koshy 	}
1333ebccf1e3SJoseph Koshy 
1334ebccf1e3SJoseph Koshy 	if ((names = malloc(count * sizeof(const char *))) == NULL)
1335aa342b1fSJoseph Koshy 		return (-1);
1336ebccf1e3SJoseph Koshy 
1337ebccf1e3SJoseph Koshy 	*eventnames = names;
1338ebccf1e3SJoseph Koshy 	*nevents = count;
1339ebccf1e3SJoseph Koshy 
1340ebccf1e3SJoseph Koshy 	for (;count--; ev++, names++)
1341ebccf1e3SJoseph Koshy 		*names = ev->pm_ev_name;
1342f5f9340bSFabien Thomas 
1343aa342b1fSJoseph Koshy 	return (0);
1344ebccf1e3SJoseph Koshy }
1345ebccf1e3SJoseph Koshy 
1346f263522aSJoseph Koshy int
pmc_flush_logfile(void)1347f263522aSJoseph Koshy pmc_flush_logfile(void)
1348f263522aSJoseph Koshy {
1349be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_FLUSHLOG, 0));
1350f263522aSJoseph Koshy }
1351ebccf1e3SJoseph Koshy 
1352ebccf1e3SJoseph Koshy int
pmc_close_logfile(void)1353dceed24aSFabien Thomas pmc_close_logfile(void)
1354dceed24aSFabien Thomas {
1355be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_CLOSELOG, 0));
1356dceed24aSFabien Thomas }
1357dceed24aSFabien Thomas 
1358dceed24aSFabien Thomas int
pmc_get_driver_stats(struct pmc_driverstats * ds)1359f263522aSJoseph Koshy pmc_get_driver_stats(struct pmc_driverstats *ds)
1360ebccf1e3SJoseph Koshy {
1361f263522aSJoseph Koshy 	struct pmc_op_getdriverstats gms;
1362f263522aSJoseph Koshy 
1363be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_GETDRIVERSTATS, &gms) < 0)
1364aa342b1fSJoseph Koshy 		return (-1);
1365f263522aSJoseph Koshy 
1366f263522aSJoseph Koshy 	/* copy out fields in the current userland<->library interface */
1367f263522aSJoseph Koshy 	ds->pm_intr_ignored    = gms.pm_intr_ignored;
1368f263522aSJoseph Koshy 	ds->pm_intr_processed  = gms.pm_intr_processed;
1369f263522aSJoseph Koshy 	ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
1370f263522aSJoseph Koshy 	ds->pm_syscalls        = gms.pm_syscalls;
1371f263522aSJoseph Koshy 	ds->pm_syscall_errors  = gms.pm_syscall_errors;
1372f263522aSJoseph Koshy 	ds->pm_buffer_requests = gms.pm_buffer_requests;
1373f263522aSJoseph Koshy 	ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
1374f263522aSJoseph Koshy 	ds->pm_log_sweeps      = gms.pm_log_sweeps;
1375aa342b1fSJoseph Koshy 	return (0);
1376f263522aSJoseph Koshy }
1377f263522aSJoseph Koshy 
1378f263522aSJoseph Koshy int
pmc_get_msr(pmc_id_t pmc,uint32_t * msr)1379f263522aSJoseph Koshy pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
1380f263522aSJoseph Koshy {
1381f263522aSJoseph Koshy 	struct pmc_op_getmsr gm;
1382ebccf1e3SJoseph Koshy 
1383ebccf1e3SJoseph Koshy 	gm.pm_pmcid = pmc;
1384be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_PMCGETMSR, &gm) < 0)
1385aa342b1fSJoseph Koshy 		return (-1);
1386ebccf1e3SJoseph Koshy 	*msr = gm.pm_msr;
1387aa342b1fSJoseph Koshy 	return (0);
1388ebccf1e3SJoseph Koshy }
1389ebccf1e3SJoseph Koshy 
1390f263522aSJoseph Koshy int
pmc_init(void)1391f263522aSJoseph Koshy pmc_init(void)
1392f263522aSJoseph Koshy {
1393f263522aSJoseph Koshy 	int error, pmc_mod_id;
13941455fcd3SJoseph Koshy 	unsigned int n;
1395f263522aSJoseph Koshy 	uint32_t abi_version;
1396f263522aSJoseph Koshy 	struct module_stat pmc_modstat;
13971455fcd3SJoseph Koshy 	struct pmc_op_getcpuinfo op_cpu_info;
1398f263522aSJoseph Koshy 
1399f263522aSJoseph Koshy 	if (pmc_syscall != -1) /* already inited */
1400aa342b1fSJoseph Koshy 		return (0);
1401f263522aSJoseph Koshy 
1402f263522aSJoseph Koshy 	/* retrieve the system call number from the KLD */
1403f263522aSJoseph Koshy 	if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
1404aa342b1fSJoseph Koshy 		return (-1);
1405f263522aSJoseph Koshy 
1406f263522aSJoseph Koshy 	pmc_modstat.version = sizeof(struct module_stat);
1407f263522aSJoseph Koshy 	if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
1408aa342b1fSJoseph Koshy 		return (-1);
1409f263522aSJoseph Koshy 
1410f263522aSJoseph Koshy 	pmc_syscall = pmc_modstat.data.intval;
1411f263522aSJoseph Koshy 
1412f263522aSJoseph Koshy 	/* check the kernel module's ABI against our compiled-in version */
1413f263522aSJoseph Koshy 	abi_version = PMC_VERSION;
1414be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_GETMODULEVERSION, &abi_version) < 0)
1415f263522aSJoseph Koshy 		return (pmc_syscall = -1);
1416f263522aSJoseph Koshy 
141775f46cf6SPedro F. Giffuni 	/* ignore patch & minor numbers for the comparison */
1418f263522aSJoseph Koshy 	if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
1419f263522aSJoseph Koshy 		errno  = EPROGMISMATCH;
1420f263522aSJoseph Koshy 		return (pmc_syscall = -1);
1421f263522aSJoseph Koshy 	}
1422f263522aSJoseph Koshy 
14238ffdc5dfSMatt Macy 	bzero(&op_cpu_info, sizeof(op_cpu_info));
1424be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_GETCPUINFO, &op_cpu_info) < 0)
1425f263522aSJoseph Koshy 		return (pmc_syscall = -1);
1426f263522aSJoseph Koshy 
14271455fcd3SJoseph Koshy 	cpu_info.pm_cputype = op_cpu_info.pm_cputype;
14281455fcd3SJoseph Koshy 	cpu_info.pm_ncpu    = op_cpu_info.pm_ncpu;
14291455fcd3SJoseph Koshy 	cpu_info.pm_npmc    = op_cpu_info.pm_npmc;
14301455fcd3SJoseph Koshy 	cpu_info.pm_nclass  = op_cpu_info.pm_nclass;
14318ffdc5dfSMatt Macy 	for (n = 0; n < op_cpu_info.pm_nclass; n++)
1432b83aeb5cSJustin Hibbits 		memcpy(&cpu_info.pm_classes[n], &op_cpu_info.pm_classes[n],
1433b83aeb5cSJustin Hibbits 		    sizeof(cpu_info.pm_classes[n]));
14341455fcd3SJoseph Koshy 
14351f567956SToomas Soome 	pmc_class_table = calloc(PMC_CLASS_TABLE_SIZE,
14360cfab8ddSJoseph Koshy 	    sizeof(struct pmc_class_descr *));
14370cfab8ddSJoseph Koshy 
14380cfab8ddSJoseph Koshy 	if (pmc_class_table == NULL)
14390cfab8ddSJoseph Koshy 		return (-1);
14400cfab8ddSJoseph Koshy 
14410cfab8ddSJoseph Koshy 	/*
1442f5f9340bSFabien Thomas 	 * Get soft events list.
1443f5f9340bSFabien Thomas 	 */
1444f5f9340bSFabien Thomas 	soft_event_info.pm_class = PMC_CLASS_SOFT;
1445be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_GETDYNEVENTINFO, &soft_event_info) < 0)
1446f5f9340bSFabien Thomas 		return (pmc_syscall = -1);
1447f5f9340bSFabien Thomas 
1448f5f9340bSFabien Thomas 	/* Map soft events to static list. */
1449f5f9340bSFabien Thomas 	for (n = 0; n < soft_event_info.pm_nevent; n++) {
1450f5f9340bSFabien Thomas 		soft_event_table[n].pm_ev_name =
1451f5f9340bSFabien Thomas 		    soft_event_info.pm_events[n].pm_ev_name;
1452f5f9340bSFabien Thomas 		soft_event_table[n].pm_ev_code =
1453f5f9340bSFabien Thomas 		    soft_event_info.pm_events[n].pm_ev_code;
1454f5f9340bSFabien Thomas 	}
1455f5f9340bSFabien Thomas 	soft_class_table_descr.pm_evc_event_table_size = \
1456f5f9340bSFabien Thomas 	    soft_event_info.pm_nevent;
1457f5f9340bSFabien Thomas 	soft_class_table_descr.pm_evc_event_table = \
1458f5f9340bSFabien Thomas 	    soft_event_table;
1459f5f9340bSFabien Thomas 
1460f5f9340bSFabien Thomas 	/*
14610cfab8ddSJoseph Koshy 	 * Fill in the class table.
14620cfab8ddSJoseph Koshy 	 */
14630cfab8ddSJoseph Koshy 	n = 0;
14641f567956SToomas Soome 	for (unsigned i = 0; i < PMC_CLASS_TABLE_SIZE; i++) {
14651f567956SToomas Soome 		switch (cpu_info.pm_classes[i].pm_class) {
14661f567956SToomas Soome #if defined(__amd64__) || defined(__i386__)
14671f567956SToomas Soome 		case PMC_CLASS_TSC:
14681f567956SToomas Soome 			pmc_class_table[n++] = &tsc_class_table_descr;
14691f567956SToomas Soome 			break;
1470f5f9340bSFabien Thomas 
14711f567956SToomas Soome 		case PMC_CLASS_K8:
14721f567956SToomas Soome 			pmc_class_table[n++] = &k8_class_table_descr;
14731f567956SToomas Soome 			break;
14741f567956SToomas Soome #endif
14751f567956SToomas Soome 
14761f567956SToomas Soome 		case PMC_CLASS_SOFT:
1477f5f9340bSFabien Thomas 			pmc_class_table[n++] = &soft_class_table_descr;
14781f567956SToomas Soome 			break;
14791f567956SToomas Soome 
14801f567956SToomas Soome #if defined(__arm__)
14811f567956SToomas Soome 		case PMC_CLASS_ARMV7:
14821f567956SToomas Soome 			switch (cpu_info.pm_cputype) {
14831f567956SToomas Soome 			case PMC_CPU_ARMV7_CORTEX_A8:
14841f567956SToomas Soome 				pmc_class_table[n++] =
14851f567956SToomas Soome 				    &cortex_a8_class_table_descr;
14861f567956SToomas Soome 				break;
14871f567956SToomas Soome 			case PMC_CPU_ARMV7_CORTEX_A9:
14881f567956SToomas Soome 				pmc_class_table[n++] =
14891f567956SToomas Soome 				    &cortex_a9_class_table_descr;
14901f567956SToomas Soome 				break;
14911f567956SToomas Soome 			default:
14921f567956SToomas Soome 				errno = ENXIO;
14931f567956SToomas Soome 				return (pmc_syscall = -1);
14941f567956SToomas Soome 			}
14951f567956SToomas Soome 			break;
14961f567956SToomas Soome #endif
1497e3572eb6SAleksandr Rybalko 
1498dd2fd621SBjoern A. Zeeb #if defined(__aarch64__)
14991f567956SToomas Soome 		case PMC_CLASS_ARMV8:
15001f567956SToomas Soome 			switch (cpu_info.pm_cputype) {
15011f567956SToomas Soome 			case PMC_CPU_ARMV8_CORTEX_A53:
15021f567956SToomas Soome 				pmc_class_table[n++] =
15031f567956SToomas Soome 				    &cortex_a53_class_table_descr;
15041f567956SToomas Soome 				break;
15051f567956SToomas Soome 			case PMC_CPU_ARMV8_CORTEX_A57:
15061f567956SToomas Soome 				pmc_class_table[n++] =
15071f567956SToomas Soome 				    &cortex_a57_class_table_descr;
15081f567956SToomas Soome 				break;
15091f567956SToomas Soome 			case PMC_CPU_ARMV8_CORTEX_A76:
15101f567956SToomas Soome 				pmc_class_table[n++] =
15111f567956SToomas Soome 				    &cortex_a76_class_table_descr;
15121f567956SToomas Soome 				break;
15131f567956SToomas Soome 			default:
15141f567956SToomas Soome 				errno = ENXIO;
15151f567956SToomas Soome 				return (pmc_syscall = -1);
15161f567956SToomas Soome 			}
15171f567956SToomas Soome 			break;
15181f567956SToomas Soome 
15191f567956SToomas Soome 		case PMC_CLASS_DMC620_PMU_CD2:
15201f567956SToomas Soome 			pmc_class_table[n++] =
15211f567956SToomas Soome 			    &dmc620_pmu_cd2_class_table_descr;
15221f567956SToomas Soome 			break;
15231f567956SToomas Soome 
15241f567956SToomas Soome 		case PMC_CLASS_DMC620_PMU_C:
1525e3572eb6SAleksandr Rybalko 			pmc_class_table[n++] = &dmc620_pmu_c_class_table_descr;
15261f567956SToomas Soome 			break;
15271f567956SToomas Soome 
15281f567956SToomas Soome 		case PMC_CLASS_CMN600_PMU:
15291f567956SToomas Soome 			pmc_class_table[n++] = &cmn600_pmu_class_table_descr;
15301f567956SToomas Soome 			break;
1531dd2fd621SBjoern A. Zeeb #endif
15321f567956SToomas Soome 
15331f567956SToomas Soome #if defined(__powerpc__)
15341f567956SToomas Soome 		case PMC_CLASS_PPC7450:
15351f567956SToomas Soome 			pmc_class_table[n++] = &ppc7450_class_table_descr;
15361f567956SToomas Soome 			break;
15371f567956SToomas Soome 
15381f567956SToomas Soome 		case PMC_CLASS_PPC970:
15391f567956SToomas Soome 			pmc_class_table[n++] = &ppc970_class_table_descr;
15401f567956SToomas Soome 			break;
15411f567956SToomas Soome 
15421f567956SToomas Soome 		case PMC_CLASS_E500:
15431f567956SToomas Soome 			pmc_class_table[n++] = &e500_class_table_descr;
15441f567956SToomas Soome 			break;
15450cfab8ddSJoseph Koshy #endif
15460cfab8ddSJoseph Koshy 
15471f567956SToomas Soome 		default:
15481f567956SToomas Soome #if defined(DEBUG)
15491f567956SToomas Soome 			printf("pm_class: 0x%x\n",
15501f567956SToomas Soome 			    cpu_info.pm_classes[i].pm_class);
15511f567956SToomas Soome #endif
15521f567956SToomas Soome 			break;
15531f567956SToomas Soome 		}
15541f567956SToomas Soome 	}
15551f567956SToomas Soome 
1556da13ef6aSMitchell Horne #define	PMC_MDEP_INIT(C) pmc_mdep_event_aliases = C##_aliases
1557789140c0SJoseph Koshy 
1558789140c0SJoseph Koshy 	/* Configure the event name parser. */
1559f263522aSJoseph Koshy 	switch (cpu_info.pm_cputype) {
156086a65549SJoseph Koshy #if defined(__amd64__) || defined(__i386__)
1561f263522aSJoseph Koshy 	case PMC_CPU_AMD_K8:
1562789140c0SJoseph Koshy 		PMC_MDEP_INIT(k8);
15630cfab8ddSJoseph Koshy 		break;
1564ebccf1e3SJoseph Koshy #endif
1565f5f9340bSFabien Thomas 	case PMC_CPU_GENERIC:
1566f5f9340bSFabien Thomas 		PMC_MDEP_INIT(generic);
1567f5f9340bSFabien Thomas 		break;
15686411d14dSRuslan Bukin #if defined(__arm__)
15693e0bfdd8SRuslan Bukin 	case PMC_CPU_ARMV7_CORTEX_A8:
15703e0bfdd8SRuslan Bukin 		PMC_MDEP_INIT(cortex_a8);
15713e0bfdd8SRuslan Bukin 		break;
15723e0bfdd8SRuslan Bukin 	case PMC_CPU_ARMV7_CORTEX_A9:
15733e0bfdd8SRuslan Bukin 		PMC_MDEP_INIT(cortex_a9);
15746411d14dSRuslan Bukin 		break;
15756411d14dSRuslan Bukin #endif
1576bc88bb2bSRuslan Bukin #if defined(__aarch64__)
1577bc88bb2bSRuslan Bukin 	case PMC_CPU_ARMV8_CORTEX_A53:
1578bc88bb2bSRuslan Bukin 		PMC_MDEP_INIT(cortex_a53);
1579bc88bb2bSRuslan Bukin 		break;
1580bc88bb2bSRuslan Bukin 	case PMC_CPU_ARMV8_CORTEX_A57:
1581bc88bb2bSRuslan Bukin 		PMC_MDEP_INIT(cortex_a57);
1582bc88bb2bSRuslan Bukin 		break;
1583da11e1f9SAndrew Turner 	case PMC_CPU_ARMV8_CORTEX_A76:
1584da11e1f9SAndrew Turner 		PMC_MDEP_INIT(cortex_a76);
1585da11e1f9SAndrew Turner 		break;
1586bc88bb2bSRuslan Bukin #endif
15877b25dccaSJustin Hibbits #if defined(__powerpc__)
15887b25dccaSJustin Hibbits 	case PMC_CPU_PPC_7450:
15897b25dccaSJustin Hibbits 		PMC_MDEP_INIT(ppc7450);
15907b25dccaSJustin Hibbits 		break;
1591169dd953SJustin Hibbits 	case PMC_CPU_PPC_970:
1592169dd953SJustin Hibbits 		PMC_MDEP_INIT(ppc970);
1593169dd953SJustin Hibbits 		break;
1594a7452468SJustin Hibbits 	case PMC_CPU_PPC_E500:
1595a7452468SJustin Hibbits 		PMC_MDEP_INIT(e500);
1596a7452468SJustin Hibbits 		break;
15977b25dccaSJustin Hibbits #endif
1598f263522aSJoseph Koshy 	default:
1599f263522aSJoseph Koshy 		/*
1600f263522aSJoseph Koshy 		 * Some kind of CPU this version of the library knows nothing
1601f263522aSJoseph Koshy 		 * about.  This shouldn't happen since the abi version check
1602f263522aSJoseph Koshy 		 * should have caught this.
1603f263522aSJoseph Koshy 		 */
160495d619a8SLeandro Lupori #if defined(__amd64__) || defined(__i386__) || defined(__powerpc64__)
1605cfb0dfa3SMatt Macy 		break;
1606cfb0dfa3SMatt Macy #endif
1607f263522aSJoseph Koshy 		errno = ENXIO;
1608f263522aSJoseph Koshy 		return (pmc_syscall = -1);
1609f263522aSJoseph Koshy 	}
1610f263522aSJoseph Koshy 
1611aa342b1fSJoseph Koshy 	return (0);
1612f263522aSJoseph Koshy }
1613f263522aSJoseph Koshy 
1614f263522aSJoseph Koshy const char *
pmc_name_of_capability(enum pmc_caps cap)1615f263522aSJoseph Koshy pmc_name_of_capability(enum pmc_caps cap)
1616f263522aSJoseph Koshy {
1617f263522aSJoseph Koshy 	int i;
1618f263522aSJoseph Koshy 
1619f263522aSJoseph Koshy 	/*
1620f263522aSJoseph Koshy 	 * 'cap' should have a single bit set and should be in
1621f263522aSJoseph Koshy 	 * range.
1622f263522aSJoseph Koshy 	 */
1623f263522aSJoseph Koshy 	if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
1624f263522aSJoseph Koshy 	    cap > PMC_CAP_LAST) {
1625f263522aSJoseph Koshy 		errno = EINVAL;
1626aa342b1fSJoseph Koshy 		return (NULL);
1627f263522aSJoseph Koshy 	}
1628f263522aSJoseph Koshy 
1629f263522aSJoseph Koshy 	i = ffs(cap);
1630aa342b1fSJoseph Koshy 	return (pmc_capability_names[i - 1]);
1631f263522aSJoseph Koshy }
1632f263522aSJoseph Koshy 
1633f263522aSJoseph Koshy const char *
pmc_name_of_class(enum pmc_class pc)1634f263522aSJoseph Koshy pmc_name_of_class(enum pmc_class pc)
1635f263522aSJoseph Koshy {
16360ceb54c2SJohn Baldwin 	size_t n;
16370ceb54c2SJohn Baldwin 
16380ceb54c2SJohn Baldwin 	for (n = 0; n < PMC_TABLE_SIZE(pmc_class_names); n++)
16390ceb54c2SJohn Baldwin 		if (pc == pmc_class_names[n].pm_class)
16400ceb54c2SJohn Baldwin 			return (pmc_class_names[n].pm_name);
1641f263522aSJoseph Koshy 
1642f263522aSJoseph Koshy 	errno = EINVAL;
1643aa342b1fSJoseph Koshy 	return (NULL);
1644f263522aSJoseph Koshy }
1645f263522aSJoseph Koshy 
1646f263522aSJoseph Koshy const char *
pmc_name_of_cputype(enum pmc_cputype cp)1647f263522aSJoseph Koshy pmc_name_of_cputype(enum pmc_cputype cp)
1648f263522aSJoseph Koshy {
1649789140c0SJoseph Koshy 	size_t n;
1650789140c0SJoseph Koshy 
1651789140c0SJoseph Koshy 	for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
1652789140c0SJoseph Koshy 		if (cp == pmc_cputype_names[n].pm_cputype)
1653789140c0SJoseph Koshy 			return (pmc_cputype_names[n].pm_name);
1654789140c0SJoseph Koshy 
1655f263522aSJoseph Koshy 	errno = EINVAL;
1656aa342b1fSJoseph Koshy 	return (NULL);
1657f263522aSJoseph Koshy }
1658f263522aSJoseph Koshy 
1659f263522aSJoseph Koshy const char *
pmc_name_of_disposition(enum pmc_disp pd)1660f263522aSJoseph Koshy pmc_name_of_disposition(enum pmc_disp pd)
1661f263522aSJoseph Koshy {
1662f263522aSJoseph Koshy 	if ((int) pd >= PMC_DISP_FIRST &&
1663f263522aSJoseph Koshy 	    pd <= PMC_DISP_LAST)
1664aa342b1fSJoseph Koshy 		return (pmc_disposition_names[pd]);
1665f263522aSJoseph Koshy 
1666f263522aSJoseph Koshy 	errno = EINVAL;
1667aa342b1fSJoseph Koshy 	return (NULL);
1668f263522aSJoseph Koshy }
1669f263522aSJoseph Koshy 
1670f263522aSJoseph Koshy const char *
_pmc_name_of_event(enum pmc_event pe,enum pmc_cputype cpu)16710cfab8ddSJoseph Koshy _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
1672f263522aSJoseph Koshy {
1673789140c0SJoseph Koshy 	const struct pmc_event_descr *ev, *evfence;
1674789140c0SJoseph Koshy 
1675789140c0SJoseph Koshy 	ev = evfence = NULL;
1676e92a1350SMatt Macy 	if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
1677789140c0SJoseph Koshy 		ev = k8_event_table;
1678789140c0SJoseph Koshy 		evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
1679aa76f0c3SMichal Meloun 
16806411d14dSRuslan Bukin 	} else if (pe >= PMC_EV_ARMV7_FIRST && pe <= PMC_EV_ARMV7_LAST) {
16813e0bfdd8SRuslan Bukin 		switch (cpu) {
16823e0bfdd8SRuslan Bukin 		case PMC_CPU_ARMV7_CORTEX_A8:
16833e0bfdd8SRuslan Bukin 			ev = cortex_a8_event_table;
16843e0bfdd8SRuslan Bukin 			evfence = cortex_a8_event_table + PMC_EVENT_TABLE_SIZE(cortex_a8);
16853e0bfdd8SRuslan Bukin 			break;
16863e0bfdd8SRuslan Bukin 		case PMC_CPU_ARMV7_CORTEX_A9:
16873e0bfdd8SRuslan Bukin 			ev = cortex_a9_event_table;
16883e0bfdd8SRuslan Bukin 			evfence = cortex_a9_event_table + PMC_EVENT_TABLE_SIZE(cortex_a9);
16893e0bfdd8SRuslan Bukin 			break;
16903e0bfdd8SRuslan Bukin 		default:	/* Unknown CPU type. */
16913e0bfdd8SRuslan Bukin 			break;
16923e0bfdd8SRuslan Bukin 		}
1693bc88bb2bSRuslan Bukin 	} else if (pe >= PMC_EV_ARMV8_FIRST && pe <= PMC_EV_ARMV8_LAST) {
1694bc88bb2bSRuslan Bukin 		switch (cpu) {
1695bc88bb2bSRuslan Bukin 		case PMC_CPU_ARMV8_CORTEX_A53:
1696bc88bb2bSRuslan Bukin 			ev = cortex_a53_event_table;
1697bc88bb2bSRuslan Bukin 			evfence = cortex_a53_event_table + PMC_EVENT_TABLE_SIZE(cortex_a53);
1698bc88bb2bSRuslan Bukin 			break;
1699bc88bb2bSRuslan Bukin 		case PMC_CPU_ARMV8_CORTEX_A57:
1700bc88bb2bSRuslan Bukin 			ev = cortex_a57_event_table;
1701bc88bb2bSRuslan Bukin 			evfence = cortex_a57_event_table + PMC_EVENT_TABLE_SIZE(cortex_a57);
1702bc88bb2bSRuslan Bukin 			break;
1703da11e1f9SAndrew Turner 		case PMC_CPU_ARMV8_CORTEX_A76:
1704da11e1f9SAndrew Turner 			ev = cortex_a76_event_table;
1705da11e1f9SAndrew Turner 			evfence = cortex_a76_event_table + PMC_EVENT_TABLE_SIZE(cortex_a76);
1706da11e1f9SAndrew Turner 			break;
1707bc88bb2bSRuslan Bukin 		default:	/* Unknown CPU type. */
1708bc88bb2bSRuslan Bukin 			break;
1709bc88bb2bSRuslan Bukin 		}
1710e3572eb6SAleksandr Rybalko 	} else if (pe >= PMC_EV_CMN600_PMU_FIRST &&
1711e3572eb6SAleksandr Rybalko 	    pe <= PMC_EV_CMN600_PMU_LAST) {
1712e3572eb6SAleksandr Rybalko 		ev = cmn600_pmu_event_table;
1713e3572eb6SAleksandr Rybalko 		evfence = cmn600_pmu_event_table +
1714e3572eb6SAleksandr Rybalko 		    PMC_EVENT_TABLE_SIZE(cmn600_pmu);
1715e3572eb6SAleksandr Rybalko 	} else if (pe >= PMC_EV_DMC620_PMU_CD2_FIRST &&
1716e3572eb6SAleksandr Rybalko 	    pe <= PMC_EV_DMC620_PMU_CD2_LAST) {
1717e3572eb6SAleksandr Rybalko 		ev = dmc620_pmu_cd2_event_table;
1718e3572eb6SAleksandr Rybalko 		evfence = dmc620_pmu_cd2_event_table +
1719e3572eb6SAleksandr Rybalko 		    PMC_EVENT_TABLE_SIZE(dmc620_pmu_cd2);
1720e3572eb6SAleksandr Rybalko 	} else if (pe >= PMC_EV_DMC620_PMU_C_FIRST &&
1721e3572eb6SAleksandr Rybalko 	    pe <= PMC_EV_DMC620_PMU_C_LAST) {
1722e3572eb6SAleksandr Rybalko 		ev = dmc620_pmu_c_event_table;
1723e3572eb6SAleksandr Rybalko 		evfence = dmc620_pmu_c_event_table +
1724e3572eb6SAleksandr Rybalko 		    PMC_EVENT_TABLE_SIZE(dmc620_pmu_c);
17257b25dccaSJustin Hibbits 	} else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
17267b25dccaSJustin Hibbits 		ev = ppc7450_event_table;
1727f5f9340bSFabien Thomas 		evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450);
1728169dd953SJustin Hibbits 	} else if (pe >= PMC_EV_PPC970_FIRST && pe <= PMC_EV_PPC970_LAST) {
1729169dd953SJustin Hibbits 		ev = ppc970_event_table;
1730169dd953SJustin Hibbits 		evfence = ppc970_event_table + PMC_EVENT_TABLE_SIZE(ppc970);
1731a7452468SJustin Hibbits 	} else if (pe >= PMC_EV_E500_FIRST && pe <= PMC_EV_E500_LAST) {
1732a7452468SJustin Hibbits 		ev = e500_event_table;
1733a7452468SJustin Hibbits 		evfence = e500_event_table + PMC_EVENT_TABLE_SIZE(e500);
1734789140c0SJoseph Koshy 	} else if (pe == PMC_EV_TSC_TSC) {
1735789140c0SJoseph Koshy 		ev = tsc_event_table;
1736789140c0SJoseph Koshy 		evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
1737f0bbe9aaSDimitry Andric 	} else if ((int)pe >= PMC_EV_SOFT_FIRST && (int)pe <= PMC_EV_SOFT_LAST) {
1738f5f9340bSFabien Thomas 		ev = soft_event_table;
1739f5f9340bSFabien Thomas 		evfence = soft_event_table + soft_event_info.pm_nevent;
1740789140c0SJoseph Koshy 	}
1741789140c0SJoseph Koshy 
1742789140c0SJoseph Koshy 	for (; ev != evfence; ev++)
1743789140c0SJoseph Koshy 		if (pe == ev->pm_ev_code)
1744789140c0SJoseph Koshy 			return (ev->pm_ev_name);
1745f263522aSJoseph Koshy 
17460cfab8ddSJoseph Koshy 	return (NULL);
17470cfab8ddSJoseph Koshy }
17480cfab8ddSJoseph Koshy 
17490cfab8ddSJoseph Koshy const char *
pmc_name_of_event(enum pmc_event pe)17500cfab8ddSJoseph Koshy pmc_name_of_event(enum pmc_event pe)
17510cfab8ddSJoseph Koshy {
17520cfab8ddSJoseph Koshy 	const char *n;
17530cfab8ddSJoseph Koshy 
17540cfab8ddSJoseph Koshy 	if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
17550cfab8ddSJoseph Koshy 		return (n);
17560cfab8ddSJoseph Koshy 
1757f263522aSJoseph Koshy 	errno = EINVAL;
1758aa342b1fSJoseph Koshy 	return (NULL);
1759f263522aSJoseph Koshy }
1760f263522aSJoseph Koshy 
1761f263522aSJoseph Koshy const char *
pmc_name_of_mode(enum pmc_mode pm)1762f263522aSJoseph Koshy pmc_name_of_mode(enum pmc_mode pm)
1763f263522aSJoseph Koshy {
1764f263522aSJoseph Koshy 	if ((int) pm >= PMC_MODE_FIRST &&
1765f263522aSJoseph Koshy 	    pm <= PMC_MODE_LAST)
1766aa342b1fSJoseph Koshy 		return (pmc_mode_names[pm]);
1767f263522aSJoseph Koshy 
1768f263522aSJoseph Koshy 	errno = EINVAL;
1769aa342b1fSJoseph Koshy 	return (NULL);
1770f263522aSJoseph Koshy }
1771f263522aSJoseph Koshy 
1772f263522aSJoseph Koshy const char *
pmc_name_of_state(enum pmc_state ps)1773f263522aSJoseph Koshy pmc_name_of_state(enum pmc_state ps)
1774f263522aSJoseph Koshy {
1775f263522aSJoseph Koshy 	if ((int) ps >= PMC_STATE_FIRST &&
1776f263522aSJoseph Koshy 	    ps <= PMC_STATE_LAST)
1777aa342b1fSJoseph Koshy 		return (pmc_state_names[ps]);
1778f263522aSJoseph Koshy 
1779f263522aSJoseph Koshy 	errno = EINVAL;
1780aa342b1fSJoseph Koshy 	return (NULL);
1781f263522aSJoseph Koshy }
1782f263522aSJoseph Koshy 
1783f263522aSJoseph Koshy int
pmc_ncpu(void)1784f263522aSJoseph Koshy pmc_ncpu(void)
1785f263522aSJoseph Koshy {
1786f263522aSJoseph Koshy 	if (pmc_syscall == -1) {
1787f263522aSJoseph Koshy 		errno = ENXIO;
1788aa342b1fSJoseph Koshy 		return (-1);
1789f263522aSJoseph Koshy 	}
1790f263522aSJoseph Koshy 
1791aa342b1fSJoseph Koshy 	return (cpu_info.pm_ncpu);
1792f263522aSJoseph Koshy }
1793f263522aSJoseph Koshy 
1794f263522aSJoseph Koshy int
pmc_npmc(int cpu)1795f263522aSJoseph Koshy pmc_npmc(int cpu)
1796f263522aSJoseph Koshy {
1797f263522aSJoseph Koshy 	if (pmc_syscall == -1) {
1798f263522aSJoseph Koshy 		errno = ENXIO;
1799aa342b1fSJoseph Koshy 		return (-1);
1800f263522aSJoseph Koshy 	}
1801f263522aSJoseph Koshy 
1802f263522aSJoseph Koshy 	if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
1803f263522aSJoseph Koshy 		errno = EINVAL;
1804aa342b1fSJoseph Koshy 		return (-1);
1805f263522aSJoseph Koshy 	}
1806f263522aSJoseph Koshy 
1807aa342b1fSJoseph Koshy 	return (cpu_info.pm_npmc);
1808f263522aSJoseph Koshy }
1809f263522aSJoseph Koshy 
1810f263522aSJoseph Koshy int
pmc_pmcinfo(int cpu,struct pmc_pmcinfo ** ppmci)1811f263522aSJoseph Koshy pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
1812f263522aSJoseph Koshy {
1813f263522aSJoseph Koshy 	int nbytes, npmc;
1814f263522aSJoseph Koshy 	struct pmc_op_getpmcinfo *pmci;
1815f263522aSJoseph Koshy 
1816f263522aSJoseph Koshy 	if ((npmc = pmc_npmc(cpu)) < 0)
1817aa342b1fSJoseph Koshy 		return (-1);
1818f263522aSJoseph Koshy 
1819f263522aSJoseph Koshy 	nbytes = sizeof(struct pmc_op_getpmcinfo) +
1820f263522aSJoseph Koshy 	    npmc * sizeof(struct pmc_info);
1821f263522aSJoseph Koshy 
1822f263522aSJoseph Koshy 	if ((pmci = calloc(1, nbytes)) == NULL)
1823aa342b1fSJoseph Koshy 		return (-1);
1824f263522aSJoseph Koshy 
1825f263522aSJoseph Koshy 	pmci->pm_cpu  = cpu;
1826f263522aSJoseph Koshy 
1827be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_GETPMCINFO, pmci) < 0) {
1828f263522aSJoseph Koshy 		free(pmci);
1829aa342b1fSJoseph Koshy 		return (-1);
1830f263522aSJoseph Koshy 	}
1831f263522aSJoseph Koshy 
1832f263522aSJoseph Koshy 	/* kernel<->library, library<->userland interfaces are identical */
1833f263522aSJoseph Koshy 	*ppmci = (struct pmc_pmcinfo *) pmci;
1834aa342b1fSJoseph Koshy 	return (0);
1835f263522aSJoseph Koshy }
1836f263522aSJoseph Koshy 
1837f263522aSJoseph Koshy int
pmc_read(pmc_id_t pmc,pmc_value_t * value)1838f263522aSJoseph Koshy pmc_read(pmc_id_t pmc, pmc_value_t *value)
1839f263522aSJoseph Koshy {
1840f263522aSJoseph Koshy 	struct pmc_op_pmcrw pmc_read_op;
1841f263522aSJoseph Koshy 
1842f263522aSJoseph Koshy 	pmc_read_op.pm_pmcid = pmc;
1843f263522aSJoseph Koshy 	pmc_read_op.pm_flags = PMC_F_OLDVALUE;
1844f263522aSJoseph Koshy 	pmc_read_op.pm_value = -1;
1845f263522aSJoseph Koshy 
1846be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_PMCRW, &pmc_read_op) < 0)
1847aa342b1fSJoseph Koshy 		return (-1);
1848f263522aSJoseph Koshy 
1849f263522aSJoseph Koshy 	*value = pmc_read_op.pm_value;
1850aa342b1fSJoseph Koshy 	return (0);
1851f263522aSJoseph Koshy }
1852f263522aSJoseph Koshy 
1853f263522aSJoseph Koshy int
pmc_release(pmc_id_t pmc)1854f263522aSJoseph Koshy pmc_release(pmc_id_t pmc)
1855f263522aSJoseph Koshy {
1856f263522aSJoseph Koshy 	struct pmc_op_simple	pmc_release_args;
1857f263522aSJoseph Koshy 
1858f263522aSJoseph Koshy 	pmc_release_args.pm_pmcid = pmc;
1859be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_PMCRELEASE, &pmc_release_args));
1860f263522aSJoseph Koshy }
1861f263522aSJoseph Koshy 
1862f263522aSJoseph Koshy int
pmc_rw(pmc_id_t pmc,pmc_value_t newvalue,pmc_value_t * oldvaluep)1863f263522aSJoseph Koshy pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
1864f263522aSJoseph Koshy {
1865f263522aSJoseph Koshy 	struct pmc_op_pmcrw pmc_rw_op;
1866f263522aSJoseph Koshy 
1867f263522aSJoseph Koshy 	pmc_rw_op.pm_pmcid = pmc;
1868f263522aSJoseph Koshy 	pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
1869f263522aSJoseph Koshy 	pmc_rw_op.pm_value = newvalue;
1870f263522aSJoseph Koshy 
1871be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_PMCRW, &pmc_rw_op) < 0)
1872aa342b1fSJoseph Koshy 		return (-1);
1873f263522aSJoseph Koshy 
1874f263522aSJoseph Koshy 	*oldvaluep = pmc_rw_op.pm_value;
1875aa342b1fSJoseph Koshy 	return (0);
1876f263522aSJoseph Koshy }
1877f263522aSJoseph Koshy 
1878f263522aSJoseph Koshy int
pmc_set(pmc_id_t pmc,pmc_value_t value)1879f263522aSJoseph Koshy pmc_set(pmc_id_t pmc, pmc_value_t value)
1880f263522aSJoseph Koshy {
1881f263522aSJoseph Koshy 	struct pmc_op_pmcsetcount sc;
1882f263522aSJoseph Koshy 
1883f263522aSJoseph Koshy 	sc.pm_pmcid = pmc;
1884f263522aSJoseph Koshy 	sc.pm_count = value;
1885f263522aSJoseph Koshy 
1886be75e578SMitchell Horne 	if (PMC_CALL(PMC_OP_PMCSETCOUNT, &sc) < 0)
1887aa342b1fSJoseph Koshy 		return (-1);
1888aa342b1fSJoseph Koshy 	return (0);
1889f263522aSJoseph Koshy }
1890f263522aSJoseph Koshy 
1891f263522aSJoseph Koshy int
pmc_start(pmc_id_t pmc)1892f263522aSJoseph Koshy pmc_start(pmc_id_t pmc)
1893f263522aSJoseph Koshy {
1894f263522aSJoseph Koshy 	struct pmc_op_simple	pmc_start_args;
1895f263522aSJoseph Koshy 
1896f263522aSJoseph Koshy 	pmc_start_args.pm_pmcid = pmc;
1897be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_PMCSTART, &pmc_start_args));
1898f263522aSJoseph Koshy }
1899f263522aSJoseph Koshy 
1900f263522aSJoseph Koshy int
pmc_stop(pmc_id_t pmc)1901f263522aSJoseph Koshy pmc_stop(pmc_id_t pmc)
1902f263522aSJoseph Koshy {
1903f263522aSJoseph Koshy 	struct pmc_op_simple	pmc_stop_args;
1904f263522aSJoseph Koshy 
1905f263522aSJoseph Koshy 	pmc_stop_args.pm_pmcid = pmc;
1906be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_PMCSTOP, &pmc_stop_args));
1907f263522aSJoseph Koshy }
1908f263522aSJoseph Koshy 
1909f263522aSJoseph Koshy int
pmc_width(pmc_id_t pmcid,uint32_t * width)1910f263522aSJoseph Koshy pmc_width(pmc_id_t pmcid, uint32_t *width)
1911f263522aSJoseph Koshy {
1912f263522aSJoseph Koshy 	unsigned int i;
1913f263522aSJoseph Koshy 	enum pmc_class cl;
1914f263522aSJoseph Koshy 
1915f263522aSJoseph Koshy 	cl = PMC_ID_TO_CLASS(pmcid);
1916f263522aSJoseph Koshy 	for (i = 0; i < cpu_info.pm_nclass; i++)
1917f263522aSJoseph Koshy 		if (cpu_info.pm_classes[i].pm_class == cl) {
1918f263522aSJoseph Koshy 			*width = cpu_info.pm_classes[i].pm_width;
1919aa342b1fSJoseph Koshy 			return (0);
1920f263522aSJoseph Koshy 		}
1921484202faSJoseph Koshy 	errno = EINVAL;
1922484202faSJoseph Koshy 	return (-1);
1923f263522aSJoseph Koshy }
1924f263522aSJoseph Koshy 
1925f263522aSJoseph Koshy int
pmc_write(pmc_id_t pmc,pmc_value_t value)1926f263522aSJoseph Koshy pmc_write(pmc_id_t pmc, pmc_value_t value)
1927f263522aSJoseph Koshy {
1928f263522aSJoseph Koshy 	struct pmc_op_pmcrw pmc_write_op;
1929f263522aSJoseph Koshy 
1930f263522aSJoseph Koshy 	pmc_write_op.pm_pmcid = pmc;
1931f263522aSJoseph Koshy 	pmc_write_op.pm_flags = PMC_F_NEWVALUE;
1932f263522aSJoseph Koshy 	pmc_write_op.pm_value = value;
1933be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_PMCRW, &pmc_write_op));
1934f263522aSJoseph Koshy }
1935f263522aSJoseph Koshy 
1936f263522aSJoseph Koshy int
pmc_writelog(uint32_t userdata)1937f263522aSJoseph Koshy pmc_writelog(uint32_t userdata)
1938f263522aSJoseph Koshy {
1939f263522aSJoseph Koshy 	struct pmc_op_writelog wl;
1940f263522aSJoseph Koshy 
1941f263522aSJoseph Koshy 	wl.pm_userdata = userdata;
1942be75e578SMitchell Horne 	return (PMC_CALL(PMC_OP_WRITELOG, &wl));
1943f263522aSJoseph Koshy }
1944