xref: /freebsd/lib/libpmc/libpmc.c (revision 4e8d558c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2008 Joseph Koshy
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/module.h>
35 #include <sys/pmc.h>
36 #include <sys/syscall.h>
37 
38 #include <assert.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <pmc.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <strings.h>
48 #include <sysexits.h>
49 #include <unistd.h>
50 
51 #include "libpmcinternal.h"
52 
53 /* Function prototypes */
54 #if defined(__amd64__) || defined(__i386__)
55 static int k8_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
56     struct pmc_op_pmcallocate *_pmc_config);
57 #endif
58 #if defined(__amd64__) || defined(__i386__)
59 static int tsc_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
60     struct pmc_op_pmcallocate *_pmc_config);
61 #endif
62 #if defined(__arm__)
63 static int armv7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
64     struct pmc_op_pmcallocate *_pmc_config);
65 #endif
66 #if defined(__aarch64__)
67 static int arm64_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
68     struct pmc_op_pmcallocate *_pmc_config);
69 static int cmn600_pmu_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
70     struct pmc_op_pmcallocate *_pmc_config);
71 static int dmc620_pmu_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
72     struct pmc_op_pmcallocate *_pmc_config);
73 #endif
74 static int soft_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
75     struct pmc_op_pmcallocate *_pmc_config);
76 
77 #if defined(__powerpc__)
78 static int powerpc_allocate_pmc(enum pmc_event _pe, char* ctrspec,
79 			     struct pmc_op_pmcallocate *_pmc_config);
80 #endif /* __powerpc__ */
81 
82 #define PMC_CALL(op, params)	syscall(pmc_syscall, (op), (params))
83 
84 /*
85  * Event aliases provide a way for the user to ask for generic events
86  * like "cache-misses", or "instructions-retired".  These aliases are
87  * mapped to the appropriate canonical event descriptions using a
88  * lookup table.
89  */
90 struct pmc_event_alias {
91 	const char	*pm_alias;
92 	const char	*pm_spec;
93 };
94 
95 static const struct pmc_event_alias *pmc_mdep_event_aliases;
96 
97 /*
98  * The pmc_event_descr structure maps symbolic names known to the user
99  * to integer codes used by the PMC KLD.
100  */
101 struct pmc_event_descr {
102 	const char	*pm_ev_name;
103 	enum pmc_event	pm_ev_code;
104 };
105 
106 /*
107  * The pmc_class_descr structure maps class name prefixes for
108  * event names to event tables and other PMC class data.
109  */
110 struct pmc_class_descr {
111 	const char	*pm_evc_name;
112 	size_t		pm_evc_name_size;
113 	enum pmc_class	pm_evc_class;
114 	const struct pmc_event_descr *pm_evc_event_table;
115 	size_t		pm_evc_event_table_size;
116 	int		(*pm_evc_allocate_pmc)(enum pmc_event _pe,
117 			    char *_ctrspec, struct pmc_op_pmcallocate *_pa);
118 };
119 
120 #define	PMC_TABLE_SIZE(N)	(sizeof(N)/sizeof(N[0]))
121 #define	PMC_EVENT_TABLE_SIZE(N)	PMC_TABLE_SIZE(N##_event_table)
122 
123 #undef	__PMC_EV
124 #define	__PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
125 
126 /*
127  * PMC_CLASSDEP_TABLE(NAME, CLASS)
128  *
129  * Define a table mapping event names and aliases to HWPMC event IDs.
130  */
131 #define	PMC_CLASSDEP_TABLE(N, C)				\
132 	static const struct pmc_event_descr N##_event_table[] =	\
133 	{							\
134 		__PMC_EV_##C()					\
135 	}
136 
137 PMC_CLASSDEP_TABLE(iaf, IAF);
138 PMC_CLASSDEP_TABLE(k8, K8);
139 PMC_CLASSDEP_TABLE(armv7, ARMV7);
140 PMC_CLASSDEP_TABLE(armv8, ARMV8);
141 PMC_CLASSDEP_TABLE(cmn600_pmu, CMN600_PMU);
142 PMC_CLASSDEP_TABLE(dmc620_pmu_cd2, DMC620_PMU_CD2);
143 PMC_CLASSDEP_TABLE(dmc620_pmu_c, DMC620_PMU_C);
144 PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
145 PMC_CLASSDEP_TABLE(ppc970, PPC970);
146 PMC_CLASSDEP_TABLE(e500, E500);
147 
148 static struct pmc_event_descr soft_event_table[PMC_EV_DYN_COUNT];
149 
150 #undef	__PMC_EV_ALIAS
151 #define	__PMC_EV_ALIAS(N,CODE) 	{ N, PMC_EV_##CODE },
152 
153 /*
154  * TODO: Factor out the __PMC_EV_ARMV7/8 list into a single separate table
155  * rather than duplicating for each core.
156  */
157 
158 static const struct pmc_event_descr cortex_a8_event_table[] =
159 {
160 	__PMC_EV_ALIAS_ARMV7_CORTEX_A8()
161 	__PMC_EV_ARMV7()
162 };
163 
164 static const struct pmc_event_descr cortex_a9_event_table[] =
165 {
166 	__PMC_EV_ALIAS_ARMV7_CORTEX_A9()
167 	__PMC_EV_ARMV7()
168 };
169 
170 static const struct pmc_event_descr cortex_a53_event_table[] =
171 {
172 	__PMC_EV_ALIAS_ARMV8_CORTEX_A53()
173 	__PMC_EV_ARMV8()
174 };
175 
176 static const struct pmc_event_descr cortex_a57_event_table[] =
177 {
178 	__PMC_EV_ALIAS_ARMV8_CORTEX_A57()
179 	__PMC_EV_ARMV8()
180 };
181 
182 static const struct pmc_event_descr cortex_a76_event_table[] =
183 {
184 	__PMC_EV_ALIAS_ARMV8_CORTEX_A76()
185 	__PMC_EV_ARMV8()
186 };
187 
188 static const struct pmc_event_descr tsc_event_table[] =
189 {
190 	__PMC_EV_ALIAS_TSC()
191 };
192 
193 #undef	PMC_CLASS_TABLE_DESC
194 #define	PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR)	\
195 static const struct pmc_class_descr NAME##_class_table_descr =	\
196 	{							\
197 		.pm_evc_name  = #CLASS "-",			\
198 		.pm_evc_name_size = sizeof(#CLASS "-") - 1,	\
199 		.pm_evc_class = PMC_CLASS_##CLASS ,		\
200 		.pm_evc_event_table = EVENTS##_event_table ,	\
201 		.pm_evc_event_table_size = 			\
202 			PMC_EVENT_TABLE_SIZE(EVENTS),		\
203 		.pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc	\
204 	}
205 
206 #if	defined(__i386__) || defined(__amd64__)
207 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
208 #endif
209 #if	defined(__i386__) || defined(__amd64__)
210 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
211 #endif
212 #if	defined(__arm__)
213 PMC_CLASS_TABLE_DESC(cortex_a8, ARMV7, cortex_a8, armv7);
214 PMC_CLASS_TABLE_DESC(cortex_a9, ARMV7, cortex_a9, armv7);
215 #endif
216 #if	defined(__aarch64__)
217 PMC_CLASS_TABLE_DESC(cortex_a53, ARMV8, cortex_a53, arm64);
218 PMC_CLASS_TABLE_DESC(cortex_a57, ARMV8, cortex_a57, arm64);
219 PMC_CLASS_TABLE_DESC(cortex_a76, ARMV8, cortex_a76, arm64);
220 PMC_CLASS_TABLE_DESC(cmn600_pmu, CMN600_PMU, cmn600_pmu, cmn600_pmu);
221 PMC_CLASS_TABLE_DESC(dmc620_pmu_cd2, DMC620_PMU_CD2, dmc620_pmu_cd2, dmc620_pmu);
222 PMC_CLASS_TABLE_DESC(dmc620_pmu_c, DMC620_PMU_C, dmc620_pmu_c, dmc620_pmu);
223 #endif
224 #if defined(__powerpc__)
225 PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, powerpc);
226 PMC_CLASS_TABLE_DESC(ppc970, PPC970, ppc970, powerpc);
227 PMC_CLASS_TABLE_DESC(e500, E500, e500, powerpc);
228 #endif
229 
230 static struct pmc_class_descr soft_class_table_descr =
231 {
232 	.pm_evc_name  = "SOFT-",
233 	.pm_evc_name_size = sizeof("SOFT-") - 1,
234 	.pm_evc_class = PMC_CLASS_SOFT,
235 	.pm_evc_event_table = NULL,
236 	.pm_evc_event_table_size = 0,
237 	.pm_evc_allocate_pmc = soft_allocate_pmc
238 };
239 
240 #undef	PMC_CLASS_TABLE_DESC
241 
242 static const struct pmc_class_descr **pmc_class_table;
243 #define	PMC_CLASS_TABLE_SIZE	cpu_info.pm_nclass
244 
245 /*
246  * Mapping tables, mapping enumeration values to human readable
247  * strings.
248  */
249 
250 static const char * pmc_capability_names[] = {
251 #undef	__PMC_CAP
252 #define	__PMC_CAP(N,V,D)	#N ,
253 	__PMC_CAPS()
254 };
255 
256 struct pmc_class_map {
257 	enum pmc_class	pm_class;
258 	const char	*pm_name;
259 };
260 
261 static const struct pmc_class_map pmc_class_names[] = {
262 #undef	__PMC_CLASS
263 #define __PMC_CLASS(S,V,D) { .pm_class = PMC_CLASS_##S, .pm_name = #S } ,
264 	__PMC_CLASSES()
265 };
266 
267 struct pmc_cputype_map {
268 	enum pmc_cputype pm_cputype;
269 	const char	*pm_name;
270 };
271 
272 static const struct pmc_cputype_map pmc_cputype_names[] = {
273 #undef	__PMC_CPU
274 #define	__PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
275 	__PMC_CPUS()
276 };
277 
278 static const char * pmc_disposition_names[] = {
279 #undef	__PMC_DISP
280 #define	__PMC_DISP(D)	#D ,
281 	__PMC_DISPOSITIONS()
282 };
283 
284 static const char * pmc_mode_names[] = {
285 #undef  __PMC_MODE
286 #define __PMC_MODE(M,N)	#M ,
287 	__PMC_MODES()
288 };
289 
290 static const char * pmc_state_names[] = {
291 #undef  __PMC_STATE
292 #define __PMC_STATE(S) #S ,
293 	__PMC_STATES()
294 };
295 
296 /*
297  * Filled in by pmc_init().
298  */
299 static int pmc_syscall = -1;
300 static struct pmc_cpuinfo cpu_info;
301 static struct pmc_op_getdyneventinfo soft_event_info;
302 
303 /* Event masks for events */
304 struct pmc_masks {
305 	const char	*pm_name;
306 	const uint64_t	pm_value;
307 };
308 #define	PMCMASK(N,V)	{ .pm_name = #N, .pm_value = (V) }
309 #define	NULLMASK	{ .pm_name = NULL }
310 
311 #if defined(__amd64__) || defined(__i386__)
312 static int
313 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint64_t *evmask)
314 {
315 	const struct pmc_masks *pm;
316 	char *q, *r;
317 	int c;
318 
319 	if (pmask == NULL)	/* no mask keywords */
320 		return (-1);
321 	q = strchr(p, '=');	/* skip '=' */
322 	if (*++q == '\0')	/* no more data */
323 		return (-1);
324 	c = 0;			/* count of mask keywords seen */
325 	while ((r = strsep(&q, "+")) != NULL) {
326 		for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
327 		    pm++)
328 			;
329 		if (pm->pm_name == NULL) /* not found */
330 			return (-1);
331 		*evmask |= pm->pm_value;
332 		c++;
333 	}
334 	return (c);
335 }
336 #endif
337 
338 #define	KWMATCH(p,kw)		(strcasecmp((p), (kw)) == 0)
339 #define	KWPREFIXMATCH(p,kw)	(strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
340 #define	EV_ALIAS(N,S)		{ .pm_alias = N, .pm_spec = S }
341 
342 #if defined(__amd64__) || defined(__i386__)
343 /*
344  * AMD K8 PMCs.
345  *
346  */
347 
348 static struct pmc_event_alias k8_aliases[] = {
349 	EV_ALIAS("branches",		"k8-fr-retired-taken-branches"),
350 	EV_ALIAS("branch-mispredicts",
351 	    "k8-fr-retired-taken-branches-mispredicted"),
352 	EV_ALIAS("cycles",		"tsc"),
353 	EV_ALIAS("dc-misses",		"k8-dc-miss"),
354 	EV_ALIAS("ic-misses",		"k8-ic-miss"),
355 	EV_ALIAS("instructions",	"k8-fr-retired-x86-instructions"),
356 	EV_ALIAS("interrupts",		"k8-fr-taken-hardware-interrupts"),
357 	EV_ALIAS("unhalted-cycles",	"k8-bu-cpu-clk-unhalted"),
358 	EV_ALIAS(NULL, NULL)
359 };
360 
361 #define	__K8MASK(N,V) PMCMASK(N,(1 << (V)))
362 
363 /*
364  * Parsing tables
365  */
366 
367 /* fp dispatched fpu ops */
368 static const struct pmc_masks k8_mask_fdfo[] = {
369 	__K8MASK(add-pipe-excluding-junk-ops,	0),
370 	__K8MASK(multiply-pipe-excluding-junk-ops,	1),
371 	__K8MASK(store-pipe-excluding-junk-ops,	2),
372 	__K8MASK(add-pipe-junk-ops,		3),
373 	__K8MASK(multiply-pipe-junk-ops,	4),
374 	__K8MASK(store-pipe-junk-ops,		5),
375 	NULLMASK
376 };
377 
378 /* ls segment register loads */
379 static const struct pmc_masks k8_mask_lsrl[] = {
380 	__K8MASK(es,	0),
381 	__K8MASK(cs,	1),
382 	__K8MASK(ss,	2),
383 	__K8MASK(ds,	3),
384 	__K8MASK(fs,	4),
385 	__K8MASK(gs,	5),
386 	__K8MASK(hs,	6),
387 	NULLMASK
388 };
389 
390 /* ls locked operation */
391 static const struct pmc_masks k8_mask_llo[] = {
392 	__K8MASK(locked-instructions,	0),
393 	__K8MASK(cycles-in-request,	1),
394 	__K8MASK(cycles-to-complete,	2),
395 	NULLMASK
396 };
397 
398 /* dc refill from {l2,system} and dc copyback */
399 static const struct pmc_masks k8_mask_dc[] = {
400 	__K8MASK(invalid,	0),
401 	__K8MASK(shared,	1),
402 	__K8MASK(exclusive,	2),
403 	__K8MASK(owner,		3),
404 	__K8MASK(modified,	4),
405 	NULLMASK
406 };
407 
408 /* dc one bit ecc error */
409 static const struct pmc_masks k8_mask_dobee[] = {
410 	__K8MASK(scrubber,	0),
411 	__K8MASK(piggyback,	1),
412 	NULLMASK
413 };
414 
415 /* dc dispatched prefetch instructions */
416 static const struct pmc_masks k8_mask_ddpi[] = {
417 	__K8MASK(load,	0),
418 	__K8MASK(store,	1),
419 	__K8MASK(nta,	2),
420 	NULLMASK
421 };
422 
423 /* dc dcache accesses by locks */
424 static const struct pmc_masks k8_mask_dabl[] = {
425 	__K8MASK(accesses,	0),
426 	__K8MASK(misses,	1),
427 	NULLMASK
428 };
429 
430 /* bu internal l2 request */
431 static const struct pmc_masks k8_mask_bilr[] = {
432 	__K8MASK(ic-fill,	0),
433 	__K8MASK(dc-fill,	1),
434 	__K8MASK(tlb-reload,	2),
435 	__K8MASK(tag-snoop,	3),
436 	__K8MASK(cancelled,	4),
437 	NULLMASK
438 };
439 
440 /* bu fill request l2 miss */
441 static const struct pmc_masks k8_mask_bfrlm[] = {
442 	__K8MASK(ic-fill,	0),
443 	__K8MASK(dc-fill,	1),
444 	__K8MASK(tlb-reload,	2),
445 	NULLMASK
446 };
447 
448 /* bu fill into l2 */
449 static const struct pmc_masks k8_mask_bfil[] = {
450 	__K8MASK(dirty-l2-victim,	0),
451 	__K8MASK(victim-from-l2,	1),
452 	NULLMASK
453 };
454 
455 /* fr retired fpu instructions */
456 static const struct pmc_masks k8_mask_frfi[] = {
457 	__K8MASK(x87,			0),
458 	__K8MASK(mmx-3dnow,		1),
459 	__K8MASK(packed-sse-sse2,	2),
460 	__K8MASK(scalar-sse-sse2,	3),
461 	NULLMASK
462 };
463 
464 /* fr retired fastpath double op instructions */
465 static const struct pmc_masks k8_mask_frfdoi[] = {
466 	__K8MASK(low-op-pos-0,		0),
467 	__K8MASK(low-op-pos-1,		1),
468 	__K8MASK(low-op-pos-2,		2),
469 	NULLMASK
470 };
471 
472 /* fr fpu exceptions */
473 static const struct pmc_masks k8_mask_ffe[] = {
474 	__K8MASK(x87-reclass-microfaults,	0),
475 	__K8MASK(sse-retype-microfaults,	1),
476 	__K8MASK(sse-reclass-microfaults,	2),
477 	__K8MASK(sse-and-x87-microtraps,	3),
478 	NULLMASK
479 };
480 
481 /* nb memory controller page access event */
482 static const struct pmc_masks k8_mask_nmcpae[] = {
483 	__K8MASK(page-hit,	0),
484 	__K8MASK(page-miss,	1),
485 	__K8MASK(page-conflict,	2),
486 	NULLMASK
487 };
488 
489 /* nb memory controller turnaround */
490 static const struct pmc_masks k8_mask_nmct[] = {
491 	__K8MASK(dimm-turnaround,		0),
492 	__K8MASK(read-to-write-turnaround,	1),
493 	__K8MASK(write-to-read-turnaround,	2),
494 	NULLMASK
495 };
496 
497 /* nb memory controller bypass saturation */
498 static const struct pmc_masks k8_mask_nmcbs[] = {
499 	__K8MASK(memory-controller-hi-pri-bypass,	0),
500 	__K8MASK(memory-controller-lo-pri-bypass,	1),
501 	__K8MASK(dram-controller-interface-bypass,	2),
502 	__K8MASK(dram-controller-queue-bypass,		3),
503 	NULLMASK
504 };
505 
506 /* nb sized commands */
507 static const struct pmc_masks k8_mask_nsc[] = {
508 	__K8MASK(nonpostwrszbyte,	0),
509 	__K8MASK(nonpostwrszdword,	1),
510 	__K8MASK(postwrszbyte,		2),
511 	__K8MASK(postwrszdword,		3),
512 	__K8MASK(rdszbyte,		4),
513 	__K8MASK(rdszdword,		5),
514 	__K8MASK(rdmodwr,		6),
515 	NULLMASK
516 };
517 
518 /* nb probe result */
519 static const struct pmc_masks k8_mask_npr[] = {
520 	__K8MASK(probe-miss,		0),
521 	__K8MASK(probe-hit,		1),
522 	__K8MASK(probe-hit-dirty-no-memory-cancel, 2),
523 	__K8MASK(probe-hit-dirty-with-memory-cancel, 3),
524 	NULLMASK
525 };
526 
527 /* nb hypertransport bus bandwidth */
528 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
529 	__K8MASK(command,	0),
530 	__K8MASK(data,	1),
531 	__K8MASK(buffer-release, 2),
532 	__K8MASK(nop,	3),
533 	NULLMASK
534 };
535 
536 #undef	__K8MASK
537 
538 #define	K8_KW_COUNT	"count"
539 #define	K8_KW_EDGE	"edge"
540 #define	K8_KW_INV	"inv"
541 #define	K8_KW_MASK	"mask"
542 #define	K8_KW_OS	"os"
543 #define	K8_KW_USR	"usr"
544 
545 static int
546 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
547     struct pmc_op_pmcallocate *pmc_config)
548 {
549 	char		*e, *p, *q;
550 	int		n;
551 	uint32_t	count;
552 	uint64_t	evmask;
553 	const struct pmc_masks	*pm, *pmask;
554 
555 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
556 	pmc_config->pm_md.pm_amd.pm_amd_config = 0;
557 
558 	pmask = NULL;
559 	evmask = 0;
560 
561 #define	__K8SETMASK(M) pmask = k8_mask_##M
562 
563 	/* setup parsing tables */
564 	switch (pe) {
565 	case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
566 		__K8SETMASK(fdfo);
567 		break;
568 	case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
569 		__K8SETMASK(lsrl);
570 		break;
571 	case PMC_EV_K8_LS_LOCKED_OPERATION:
572 		__K8SETMASK(llo);
573 		break;
574 	case PMC_EV_K8_DC_REFILL_FROM_L2:
575 	case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
576 	case PMC_EV_K8_DC_COPYBACK:
577 		__K8SETMASK(dc);
578 		break;
579 	case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
580 		__K8SETMASK(dobee);
581 		break;
582 	case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
583 		__K8SETMASK(ddpi);
584 		break;
585 	case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
586 		__K8SETMASK(dabl);
587 		break;
588 	case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
589 		__K8SETMASK(bilr);
590 		break;
591 	case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
592 		__K8SETMASK(bfrlm);
593 		break;
594 	case PMC_EV_K8_BU_FILL_INTO_L2:
595 		__K8SETMASK(bfil);
596 		break;
597 	case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
598 		__K8SETMASK(frfi);
599 		break;
600 	case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
601 		__K8SETMASK(frfdoi);
602 		break;
603 	case PMC_EV_K8_FR_FPU_EXCEPTIONS:
604 		__K8SETMASK(ffe);
605 		break;
606 	case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
607 		__K8SETMASK(nmcpae);
608 		break;
609 	case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
610 		__K8SETMASK(nmct);
611 		break;
612 	case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
613 		__K8SETMASK(nmcbs);
614 		break;
615 	case PMC_EV_K8_NB_SIZED_COMMANDS:
616 		__K8SETMASK(nsc);
617 		break;
618 	case PMC_EV_K8_NB_PROBE_RESULT:
619 		__K8SETMASK(npr);
620 		break;
621 	case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
622 	case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
623 	case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
624 		__K8SETMASK(nhbb);
625 		break;
626 
627 	default:
628 		break;		/* no options defined */
629 	}
630 
631 	while ((p = strsep(&ctrspec, ",")) != NULL) {
632 		if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
633 			q = strchr(p, '=');
634 			if (*++q == '\0') /* skip '=' */
635 				return (-1);
636 
637 			count = strtol(q, &e, 0);
638 			if (e == q || *e != '\0')
639 				return (-1);
640 
641 			pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
642 			pmc_config->pm_md.pm_amd.pm_amd_config |=
643 			    AMD_PMC_TO_COUNTER(count);
644 
645 		} else if (KWMATCH(p, K8_KW_EDGE)) {
646 			pmc_config->pm_caps |= PMC_CAP_EDGE;
647 		} else if (KWMATCH(p, K8_KW_INV)) {
648 			pmc_config->pm_caps |= PMC_CAP_INVERT;
649 		} else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
650 			if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
651 				return (-1);
652 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
653 		} else if (KWMATCH(p, K8_KW_OS)) {
654 			pmc_config->pm_caps |= PMC_CAP_SYSTEM;
655 		} else if (KWMATCH(p, K8_KW_USR)) {
656 			pmc_config->pm_caps |= PMC_CAP_USER;
657 		} else
658 			return (-1);
659 	}
660 
661 	/* other post processing */
662 	switch (pe) {
663 	case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
664 	case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
665 	case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
666 	case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
667 	case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
668 	case PMC_EV_K8_FR_FPU_EXCEPTIONS:
669 		/* XXX only available in rev B and later */
670 		break;
671 	case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
672 		/* XXX only available in rev C and later */
673 		break;
674 	case PMC_EV_K8_LS_LOCKED_OPERATION:
675 		/* XXX CPU Rev A,B evmask is to be zero */
676 		if (evmask & (evmask - 1)) /* > 1 bit set */
677 			return (-1);
678 		if (evmask == 0) {
679 			evmask = 0x01; /* Rev C and later: #instrs */
680 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
681 		}
682 		break;
683 	default:
684 		if (evmask == 0 && pmask != NULL) {
685 			for (pm = pmask; pm->pm_name; pm++)
686 				evmask |= pm->pm_value;
687 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
688 		}
689 	}
690 
691 	if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
692 		pmc_config->pm_md.pm_amd.pm_amd_config =
693 		    AMD_PMC_TO_UNITMASK(evmask);
694 
695 	return (0);
696 }
697 
698 #endif
699 
700 #if	defined(__i386__) || defined(__amd64__)
701 static int
702 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
703     struct pmc_op_pmcallocate *pmc_config)
704 {
705 	if (pe != PMC_EV_TSC_TSC)
706 		return (-1);
707 
708 	/* TSC events must be unqualified. */
709 	if (ctrspec && *ctrspec != '\0')
710 		return (-1);
711 
712 	pmc_config->pm_md.pm_amd.pm_amd_config = 0;
713 	pmc_config->pm_caps |= PMC_CAP_READ;
714 
715 	return (0);
716 }
717 #endif
718 
719 static struct pmc_event_alias generic_aliases[] = {
720 	EV_ALIAS("instructions",		"SOFT-CLOCK.HARD"),
721 	EV_ALIAS(NULL, NULL)
722 };
723 
724 static int
725 soft_allocate_pmc(enum pmc_event pe, char *ctrspec,
726     struct pmc_op_pmcallocate *pmc_config)
727 {
728 	(void)ctrspec;
729 	(void)pmc_config;
730 
731 	if ((int)pe < PMC_EV_SOFT_FIRST || (int)pe > PMC_EV_SOFT_LAST)
732 		return (-1);
733 
734 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
735 	return (0);
736 }
737 
738 #if	defined(__arm__)
739 static struct pmc_event_alias cortex_a8_aliases[] = {
740 	EV_ALIAS("dc-misses",		"L1_DCACHE_REFILL"),
741 	EV_ALIAS("ic-misses",		"L1_ICACHE_REFILL"),
742 	EV_ALIAS("instructions",	"INSTR_EXECUTED"),
743 	EV_ALIAS(NULL, NULL)
744 };
745 
746 static struct pmc_event_alias cortex_a9_aliases[] = {
747 	EV_ALIAS("dc-misses",		"L1_DCACHE_REFILL"),
748 	EV_ALIAS("ic-misses",		"L1_ICACHE_REFILL"),
749 	EV_ALIAS("instructions",	"INSTR_EXECUTED"),
750 	EV_ALIAS(NULL, NULL)
751 };
752 
753 static int
754 armv7_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
755     struct pmc_op_pmcallocate *pmc_config __unused)
756 {
757 	switch (pe) {
758 	default:
759 		break;
760 	}
761 
762 	return (0);
763 }
764 #endif
765 
766 #if	defined(__aarch64__)
767 static struct pmc_event_alias cortex_a53_aliases[] = {
768 	EV_ALIAS(NULL, NULL)
769 };
770 static struct pmc_event_alias cortex_a57_aliases[] = {
771 	EV_ALIAS(NULL, NULL)
772 };
773 static struct pmc_event_alias cortex_a76_aliases[] = {
774 	EV_ALIAS(NULL, NULL)
775 };
776 
777 static int
778 arm64_allocate_pmc(enum pmc_event pe, char *ctrspec,
779     struct pmc_op_pmcallocate *pmc_config)
780 {
781 	char *p;
782 
783 	while ((p = strsep(&ctrspec, ",")) != NULL) {
784 		if (KWMATCH(p, "os"))
785 			pmc_config->pm_caps |= PMC_CAP_SYSTEM;
786 		else if (KWMATCH(p, "usr"))
787 			pmc_config->pm_caps |= PMC_CAP_USER;
788 		else
789 			return (-1);
790 	}
791 
792 	return (0);
793 }
794 
795 static int
796 cmn600_pmu_allocate_pmc(enum pmc_event pe, char *ctrspec,
797     struct pmc_op_pmcallocate *pmc_config)
798 {
799 	uint32_t nodeid, occupancy, xpport, xpchannel;
800 	char *e, *p, *q;
801 	unsigned int i;
802 	char *xpport_names[] = { "East", "West", "North", "South", "devport0",
803 	    "devport1" };
804 	char *xpchannel_names[] = { "REQ", "RSP", "SNP", "DAT" };
805 
806 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
807 	pmc_config->pm_caps |= PMC_CAP_SYSTEM;
808 	pmc_config->pm_md.pm_cmn600.pma_cmn600_config = 0;
809 	/*
810 	 * CMN600 extra fields:
811 	 * * nodeid - node coordinates x[2-3],y[2-3],p[1],s[2]
812 	 * 		width of x and y fields depend on matrix size.
813 	 * * occupancy - numeric value to select desired filter.
814 	 * * xpport - East, West, North, South, devport0, devport1 (or 0, 1, ..., 5)
815 	 * * xpchannel - REQ, RSP, SNP, DAT (or 0, 1, 2, 3)
816 	 */
817 
818 	while ((p = strsep(&ctrspec, ",")) != NULL) {
819 		if (KWPREFIXMATCH(p, "nodeid=")) {
820 			q = strchr(p, '=');
821 			if (*++q == '\0') /* skip '=' */
822 				return (-1);
823 
824 			nodeid = strtol(q, &e, 0);
825 			if (e == q || *e != '\0')
826 				return (-1);
827 
828 			pmc_config->pm_md.pm_cmn600.pma_cmn600_nodeid |= nodeid;
829 
830 		} else if (KWPREFIXMATCH(p, "occupancy=")) {
831 			q = strchr(p, '=');
832 			if (*++q == '\0') /* skip '=' */
833 				return (-1);
834 
835 			occupancy = strtol(q, &e, 0);
836 			if (e == q || *e != '\0')
837 				return (-1);
838 
839 			pmc_config->pm_md.pm_cmn600.pma_cmn600_occupancy = occupancy;
840 		} else if (KWPREFIXMATCH(p, "xpport=")) {
841 			q = strchr(p, '=');
842 			if (*++q == '\0') /* skip '=' */
843 				return (-1);
844 
845 			xpport = strtol(q, &e, 0);
846 			if (e == q || *e != '\0') {
847 				for (i = 0; i < nitems(xpport_names); i++) {
848 					if (strcasecmp(xpport_names[i], q) == 0) {
849 						xpport = i;
850 						break;
851 					}
852 				}
853 				if (i == nitems(xpport_names))
854 					return (-1);
855 			}
856 
857 			pmc_config->pm_md.pm_cmn600.pma_cmn600_config |= xpport << 2;
858 		} else if (KWPREFIXMATCH(p, "xpchannel=")) {
859 			q = strchr(p, '=');
860 			if (*++q == '\0') /* skip '=' */
861 				return (-1);
862 
863 			xpchannel = strtol(q, &e, 0);
864 			if (e == q || *e != '\0') {
865 				for (i = 0; i < nitems(xpchannel_names); i++) {
866 					if (strcasecmp(xpchannel_names[i], q) == 0) {
867 						xpchannel = i;
868 						break;
869 					}
870 				}
871 				if (i == nitems(xpchannel_names))
872 					return (-1);
873 			}
874 
875 			pmc_config->pm_md.pm_cmn600.pma_cmn600_config |= xpchannel << 5;
876 		} else
877 			return (-1);
878 	}
879 
880 	return (0);
881 }
882 
883 static int
884 dmc620_pmu_allocate_pmc(enum pmc_event pe, char *ctrspec,
885     struct pmc_op_pmcallocate *pmc_config)
886 {
887 	char		*e, *p, *q;
888 	uint64_t	match, mask;
889 	uint32_t	count;
890 
891 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
892 	pmc_config->pm_caps |= PMC_CAP_SYSTEM;
893 	pmc_config->pm_md.pm_dmc620.pm_dmc620_config = 0;
894 
895 	while ((p = strsep(&ctrspec, ",")) != NULL) {
896 		if (KWPREFIXMATCH(p, "count=")) {
897 			q = strchr(p, '=');
898 			if (*++q == '\0') /* skip '=' */
899 				return (-1);
900 
901 			count = strtol(q, &e, 0);
902 			if (e == q || *e != '\0')
903 				return (-1);
904 
905 			pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
906 			pmc_config->pm_md.pm_dmc620.pm_dmc620_config |= count;
907 
908 		} else if (KWMATCH(p, "inv")) {
909 			pmc_config->pm_caps |= PMC_CAP_INVERT;
910 		} else if (KWPREFIXMATCH(p, "match=")) {
911 			match = strtol(q, &e, 0);
912 			if (e == q || *e != '\0')
913 				return (-1);
914 
915 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
916 			pmc_config->pm_md.pm_dmc620.pm_dmc620_match = match;
917 		} else if (KWPREFIXMATCH(p, "mask=")) {
918 			q = strchr(p, '=');
919 			if (*++q == '\0') /* skip '=' */
920 				return (-1);
921 
922 			mask = strtol(q, &e, 0);
923 			if (e == q || *e != '\0')
924 				return (-1);
925 
926 			pmc_config->pm_md.pm_dmc620.pm_dmc620_mask = mask;
927 			pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
928 		} else
929 			return (-1);
930 	}
931 
932 	return (0);
933 }
934 #endif
935 
936 #if defined(__powerpc__)
937 
938 static struct pmc_event_alias ppc7450_aliases[] = {
939 	EV_ALIAS("instructions",	"INSTR_COMPLETED"),
940 	EV_ALIAS("branches",		"BRANCHES_COMPLETED"),
941 	EV_ALIAS("branch-mispredicts",	"MISPREDICTED_BRANCHES"),
942 	EV_ALIAS(NULL, NULL)
943 };
944 
945 static struct pmc_event_alias ppc970_aliases[] = {
946 	EV_ALIAS("instructions", "INSTR_COMPLETED"),
947 	EV_ALIAS("cycles",       "CYCLES"),
948 	EV_ALIAS(NULL, NULL)
949 };
950 
951 static struct pmc_event_alias e500_aliases[] = {
952 	EV_ALIAS("instructions", "INSTR_COMPLETED"),
953 	EV_ALIAS("cycles",       "CYCLES"),
954 	EV_ALIAS(NULL, NULL)
955 };
956 
957 #define	POWERPC_KW_OS		"os"
958 #define	POWERPC_KW_USR		"usr"
959 #define	POWERPC_KW_ANYTHREAD	"anythread"
960 
961 static int
962 powerpc_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
963 		     struct pmc_op_pmcallocate *pmc_config __unused)
964 {
965 	char *p;
966 
967 	(void) pe;
968 
969 	pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
970 
971 	while ((p = strsep(&ctrspec, ",")) != NULL) {
972 		if (KWMATCH(p, POWERPC_KW_OS))
973 			pmc_config->pm_caps |= PMC_CAP_SYSTEM;
974 		else if (KWMATCH(p, POWERPC_KW_USR))
975 			pmc_config->pm_caps |= PMC_CAP_USER;
976 		else if (KWMATCH(p, POWERPC_KW_ANYTHREAD))
977 			pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
978 		else
979 			return (-1);
980 	}
981 
982 	return (0);
983 }
984 
985 #endif /* __powerpc__ */
986 
987 
988 /*
989  * Match an event name `name' with its canonical form.
990  *
991  * Matches are case insensitive and spaces, periods, underscores and
992  * hyphen characters are considered to match each other.
993  *
994  * Returns 1 for a match, 0 otherwise.
995  */
996 
997 static int
998 pmc_match_event_name(const char *name, const char *canonicalname)
999 {
1000 	int cc, nc;
1001 	const unsigned char *c, *n;
1002 
1003 	c = (const unsigned char *) canonicalname;
1004 	n = (const unsigned char *) name;
1005 
1006 	for (; (nc = *n) && (cc = *c); n++, c++) {
1007 
1008 		if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
1009 		    (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
1010 			continue;
1011 
1012 		if (toupper(nc) == toupper(cc))
1013 			continue;
1014 
1015 
1016 		return (0);
1017 	}
1018 
1019 	if (*n == '\0' && *c == '\0')
1020 		return (1);
1021 
1022 	return (0);
1023 }
1024 
1025 /*
1026  * Match an event name against all the event named supported by a
1027  * PMC class.
1028  *
1029  * Returns an event descriptor pointer on match or NULL otherwise.
1030  */
1031 static const struct pmc_event_descr *
1032 pmc_match_event_class(const char *name,
1033     const struct pmc_class_descr *pcd)
1034 {
1035 	size_t n;
1036 	const struct pmc_event_descr *ev;
1037 
1038 	ev = pcd->pm_evc_event_table;
1039 	for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
1040 		if (pmc_match_event_name(name, ev->pm_ev_name))
1041 			return (ev);
1042 
1043 	return (NULL);
1044 }
1045 
1046 /*
1047  * API entry points
1048  */
1049 
1050 int
1051 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
1052     uint32_t flags, int cpu, pmc_id_t *pmcid,
1053     uint64_t count)
1054 {
1055 	size_t n;
1056 	int retval;
1057 	char *r, *spec_copy;
1058 	const char *ctrname;
1059 	const struct pmc_event_descr *ev;
1060 	const struct pmc_event_alias *alias;
1061 	struct pmc_op_pmcallocate pmc_config;
1062 	const struct pmc_class_descr *pcd;
1063 
1064 	spec_copy = NULL;
1065 	retval    = -1;
1066 
1067 	if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
1068 	    mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
1069 		errno = EINVAL;
1070 		goto out;
1071 	}
1072 	bzero(&pmc_config, sizeof(pmc_config));
1073 	pmc_config.pm_cpu   = cpu;
1074 	pmc_config.pm_mode  = mode;
1075 	pmc_config.pm_flags = flags;
1076 	pmc_config.pm_count = count;
1077 	if (PMC_IS_SAMPLING_MODE(mode))
1078 		pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
1079 
1080 	/*
1081 	 * Try to pull the raw event ID directly from the pmu-events table. If
1082 	 * this is unsupported on the platform, or the event is not found,
1083 	 * continue with searching the regular event tables.
1084 	 */
1085 	r = spec_copy = strdup(ctrspec);
1086 	ctrname = strsep(&r, ",");
1087 	if (pmc_pmu_enabled()) {
1088 		if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0) {
1089 			/*
1090 			 * XXX: pmclog_get_event exploits this to disambiguate
1091 			 *      PMU from PMC event codes in PMCALLOCATE events.
1092 			 */
1093 			assert(pmc_config.pm_ev < PMC_EVENT_FIRST);
1094 			goto found;
1095 		}
1096 
1097 		/* Otherwise, reset any changes */
1098 		pmc_config.pm_ev = 0;
1099 		pmc_config.pm_caps = 0;
1100 		pmc_config.pm_class = 0;
1101 	}
1102 	free(spec_copy);
1103 	spec_copy = NULL;
1104 
1105 	/* replace an event alias with the canonical event specifier */
1106 	if (pmc_mdep_event_aliases)
1107 		for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
1108 			if (!strcasecmp(ctrspec, alias->pm_alias)) {
1109 				spec_copy = strdup(alias->pm_spec);
1110 				break;
1111 			}
1112 
1113 	if (spec_copy == NULL)
1114 		spec_copy = strdup(ctrspec);
1115 
1116 	r = spec_copy;
1117 	ctrname = strsep(&r, ",");
1118 
1119 	/*
1120 	 * If a explicit class prefix was given by the user, restrict the
1121 	 * search for the event to the specified PMC class.
1122 	 */
1123 	ev = NULL;
1124 	for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
1125 		pcd = pmc_class_table[n];
1126 		if (pcd != NULL && strncasecmp(ctrname, pcd->pm_evc_name,
1127 		    pcd->pm_evc_name_size) == 0) {
1128 			if ((ev = pmc_match_event_class(ctrname +
1129 			    pcd->pm_evc_name_size, pcd)) == NULL) {
1130 				errno = EINVAL;
1131 				goto out;
1132 			}
1133 			break;
1134 		}
1135 	}
1136 
1137 	/*
1138 	 * Otherwise, search for this event in all compatible PMC
1139 	 * classes.
1140 	 */
1141 	for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
1142 		pcd = pmc_class_table[n];
1143 		if (pcd != NULL)
1144 			ev = pmc_match_event_class(ctrname, pcd);
1145 	}
1146 
1147 	if (ev == NULL) {
1148 		errno = EINVAL;
1149 		goto out;
1150 	}
1151 
1152 	pmc_config.pm_ev    = ev->pm_ev_code;
1153 	pmc_config.pm_class = pcd->pm_evc_class;
1154 
1155  	if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
1156 		errno = EINVAL;
1157 		goto out;
1158 	}
1159 
1160 found:
1161 	if (PMC_CALL(PMC_OP_PMCALLOCATE, &pmc_config) == 0) {
1162 		*pmcid = pmc_config.pm_pmcid;
1163 		retval = 0;
1164 	}
1165 out:
1166 	if (spec_copy)
1167 		free(spec_copy);
1168 
1169 	return (retval);
1170 }
1171 
1172 int
1173 pmc_attach(pmc_id_t pmc, pid_t pid)
1174 {
1175 	struct pmc_op_pmcattach pmc_attach_args;
1176 
1177 	pmc_attach_args.pm_pmc = pmc;
1178 	pmc_attach_args.pm_pid = pid;
1179 
1180 	return (PMC_CALL(PMC_OP_PMCATTACH, &pmc_attach_args));
1181 }
1182 
1183 int
1184 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
1185 {
1186 	unsigned int i;
1187 	enum pmc_class cl;
1188 
1189 	cl = PMC_ID_TO_CLASS(pmcid);
1190 	for (i = 0; i < cpu_info.pm_nclass; i++)
1191 		if (cpu_info.pm_classes[i].pm_class == cl) {
1192 			*caps = cpu_info.pm_classes[i].pm_caps;
1193 			return (0);
1194 		}
1195 	errno = EINVAL;
1196 	return (-1);
1197 }
1198 
1199 int
1200 pmc_configure_logfile(int fd)
1201 {
1202 	struct pmc_op_configurelog cla;
1203 
1204 	cla.pm_flags = 0;
1205 	cla.pm_logfd = fd;
1206 	if (PMC_CALL(PMC_OP_CONFIGURELOG, &cla) < 0)
1207 		return (-1);
1208 	return (0);
1209 }
1210 
1211 int
1212 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
1213 {
1214 	if (pmc_syscall == -1) {
1215 		errno = ENXIO;
1216 		return (-1);
1217 	}
1218 
1219 	*pci = &cpu_info;
1220 	return (0);
1221 }
1222 
1223 int
1224 pmc_detach(pmc_id_t pmc, pid_t pid)
1225 {
1226 	struct pmc_op_pmcattach pmc_detach_args;
1227 
1228 	pmc_detach_args.pm_pmc = pmc;
1229 	pmc_detach_args.pm_pid = pid;
1230 	return (PMC_CALL(PMC_OP_PMCDETACH, &pmc_detach_args));
1231 }
1232 
1233 int
1234 pmc_disable(int cpu, int pmc)
1235 {
1236 	struct pmc_op_pmcadmin ssa;
1237 
1238 	ssa.pm_cpu = cpu;
1239 	ssa.pm_pmc = pmc;
1240 	ssa.pm_state = PMC_STATE_DISABLED;
1241 	return (PMC_CALL(PMC_OP_PMCADMIN, &ssa));
1242 }
1243 
1244 int
1245 pmc_enable(int cpu, int pmc)
1246 {
1247 	struct pmc_op_pmcadmin ssa;
1248 
1249 	ssa.pm_cpu = cpu;
1250 	ssa.pm_pmc = pmc;
1251 	ssa.pm_state = PMC_STATE_FREE;
1252 	return (PMC_CALL(PMC_OP_PMCADMIN, &ssa));
1253 }
1254 
1255 /*
1256  * Return a list of events known to a given PMC class.  'cl' is the
1257  * PMC class identifier, 'eventnames' is the returned list of 'const
1258  * char *' pointers pointing to the names of the events. 'nevents' is
1259  * the number of event name pointers returned.
1260  *
1261  * The space for 'eventnames' is allocated using malloc(3).  The caller
1262  * is responsible for freeing this space when done.
1263  */
1264 int
1265 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
1266     int *nevents)
1267 {
1268 	int count;
1269 	const char **names;
1270 	const struct pmc_event_descr *ev;
1271 
1272 	switch (cl)
1273 	{
1274 	case PMC_CLASS_IAF:
1275 		ev = iaf_event_table;
1276 		count = PMC_EVENT_TABLE_SIZE(iaf);
1277 		break;
1278 	case PMC_CLASS_TSC:
1279 		ev = tsc_event_table;
1280 		count = PMC_EVENT_TABLE_SIZE(tsc);
1281 		break;
1282 	case PMC_CLASS_K8:
1283 		ev = k8_event_table;
1284 		count = PMC_EVENT_TABLE_SIZE(k8);
1285 		break;
1286 	case PMC_CLASS_ARMV7:
1287 		switch (cpu_info.pm_cputype) {
1288 		default:
1289 		case PMC_CPU_ARMV7_CORTEX_A8:
1290 			ev = cortex_a8_event_table;
1291 			count = PMC_EVENT_TABLE_SIZE(cortex_a8);
1292 			break;
1293 		case PMC_CPU_ARMV7_CORTEX_A9:
1294 			ev = cortex_a9_event_table;
1295 			count = PMC_EVENT_TABLE_SIZE(cortex_a9);
1296 			break;
1297 		}
1298 		break;
1299 	case PMC_CLASS_ARMV8:
1300 		switch (cpu_info.pm_cputype) {
1301 		default:
1302 		case PMC_CPU_ARMV8_CORTEX_A53:
1303 			ev = cortex_a53_event_table;
1304 			count = PMC_EVENT_TABLE_SIZE(cortex_a53);
1305 			break;
1306 		case PMC_CPU_ARMV8_CORTEX_A57:
1307 			ev = cortex_a57_event_table;
1308 			count = PMC_EVENT_TABLE_SIZE(cortex_a57);
1309 			break;
1310 		case PMC_CPU_ARMV8_CORTEX_A76:
1311 			ev = cortex_a76_event_table;
1312 			count = PMC_EVENT_TABLE_SIZE(cortex_a76);
1313 			break;
1314 		}
1315 		break;
1316 	case PMC_CLASS_CMN600_PMU:
1317 		ev = cmn600_pmu_event_table;
1318 		count = PMC_EVENT_TABLE_SIZE(cmn600_pmu);
1319 		break;
1320 	case PMC_CLASS_DMC620_PMU_CD2:
1321 		ev = dmc620_pmu_cd2_event_table;
1322 		count = PMC_EVENT_TABLE_SIZE(dmc620_pmu_cd2);
1323 		break;
1324 	case PMC_CLASS_DMC620_PMU_C:
1325 		ev = dmc620_pmu_c_event_table;
1326 		count = PMC_EVENT_TABLE_SIZE(dmc620_pmu_c);
1327 		break;
1328 	case PMC_CLASS_PPC7450:
1329 		ev = ppc7450_event_table;
1330 		count = PMC_EVENT_TABLE_SIZE(ppc7450);
1331 		break;
1332 	case PMC_CLASS_PPC970:
1333 		ev = ppc970_event_table;
1334 		count = PMC_EVENT_TABLE_SIZE(ppc970);
1335 		break;
1336 	case PMC_CLASS_E500:
1337 		ev = e500_event_table;
1338 		count = PMC_EVENT_TABLE_SIZE(e500);
1339 		break;
1340 	case PMC_CLASS_SOFT:
1341 		ev = soft_event_table;
1342 		count = soft_event_info.pm_nevent;
1343 		break;
1344 	default:
1345 		errno = EINVAL;
1346 		return (-1);
1347 	}
1348 
1349 	if ((names = malloc(count * sizeof(const char *))) == NULL)
1350 		return (-1);
1351 
1352 	*eventnames = names;
1353 	*nevents = count;
1354 
1355 	for (;count--; ev++, names++)
1356 		*names = ev->pm_ev_name;
1357 
1358 	return (0);
1359 }
1360 
1361 int
1362 pmc_flush_logfile(void)
1363 {
1364 	return (PMC_CALL(PMC_OP_FLUSHLOG, 0));
1365 }
1366 
1367 int
1368 pmc_close_logfile(void)
1369 {
1370 	return (PMC_CALL(PMC_OP_CLOSELOG, 0));
1371 }
1372 
1373 int
1374 pmc_get_driver_stats(struct pmc_driverstats *ds)
1375 {
1376 	struct pmc_op_getdriverstats gms;
1377 
1378 	if (PMC_CALL(PMC_OP_GETDRIVERSTATS, &gms) < 0)
1379 		return (-1);
1380 
1381 	/* copy out fields in the current userland<->library interface */
1382 	ds->pm_intr_ignored    = gms.pm_intr_ignored;
1383 	ds->pm_intr_processed  = gms.pm_intr_processed;
1384 	ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
1385 	ds->pm_syscalls        = gms.pm_syscalls;
1386 	ds->pm_syscall_errors  = gms.pm_syscall_errors;
1387 	ds->pm_buffer_requests = gms.pm_buffer_requests;
1388 	ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
1389 	ds->pm_log_sweeps      = gms.pm_log_sweeps;
1390 	return (0);
1391 }
1392 
1393 int
1394 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
1395 {
1396 	struct pmc_op_getmsr gm;
1397 
1398 	gm.pm_pmcid = pmc;
1399 	if (PMC_CALL(PMC_OP_PMCGETMSR, &gm) < 0)
1400 		return (-1);
1401 	*msr = gm.pm_msr;
1402 	return (0);
1403 }
1404 
1405 int
1406 pmc_init(void)
1407 {
1408 	int error, pmc_mod_id;
1409 	unsigned int n;
1410 	uint32_t abi_version;
1411 	struct module_stat pmc_modstat;
1412 	struct pmc_op_getcpuinfo op_cpu_info;
1413 
1414 	if (pmc_syscall != -1) /* already inited */
1415 		return (0);
1416 
1417 	/* retrieve the system call number from the KLD */
1418 	if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
1419 		return (-1);
1420 
1421 	pmc_modstat.version = sizeof(struct module_stat);
1422 	if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
1423 		return (-1);
1424 
1425 	pmc_syscall = pmc_modstat.data.intval;
1426 
1427 	/* check the kernel module's ABI against our compiled-in version */
1428 	abi_version = PMC_VERSION;
1429 	if (PMC_CALL(PMC_OP_GETMODULEVERSION, &abi_version) < 0)
1430 		return (pmc_syscall = -1);
1431 
1432 	/* ignore patch & minor numbers for the comparison */
1433 	if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
1434 		errno  = EPROGMISMATCH;
1435 		return (pmc_syscall = -1);
1436 	}
1437 
1438 	bzero(&op_cpu_info, sizeof(op_cpu_info));
1439 	if (PMC_CALL(PMC_OP_GETCPUINFO, &op_cpu_info) < 0)
1440 		return (pmc_syscall = -1);
1441 
1442 	cpu_info.pm_cputype = op_cpu_info.pm_cputype;
1443 	cpu_info.pm_ncpu    = op_cpu_info.pm_ncpu;
1444 	cpu_info.pm_npmc    = op_cpu_info.pm_npmc;
1445 	cpu_info.pm_nclass  = op_cpu_info.pm_nclass;
1446 	for (n = 0; n < op_cpu_info.pm_nclass; n++)
1447 		memcpy(&cpu_info.pm_classes[n], &op_cpu_info.pm_classes[n],
1448 		    sizeof(cpu_info.pm_classes[n]));
1449 
1450 	pmc_class_table = calloc(PMC_CLASS_TABLE_SIZE,
1451 	    sizeof(struct pmc_class_descr *));
1452 
1453 	if (pmc_class_table == NULL)
1454 		return (-1);
1455 
1456 	/*
1457 	 * Get soft events list.
1458 	 */
1459 	soft_event_info.pm_class = PMC_CLASS_SOFT;
1460 	if (PMC_CALL(PMC_OP_GETDYNEVENTINFO, &soft_event_info) < 0)
1461 		return (pmc_syscall = -1);
1462 
1463 	/* Map soft events to static list. */
1464 	for (n = 0; n < soft_event_info.pm_nevent; n++) {
1465 		soft_event_table[n].pm_ev_name =
1466 		    soft_event_info.pm_events[n].pm_ev_name;
1467 		soft_event_table[n].pm_ev_code =
1468 		    soft_event_info.pm_events[n].pm_ev_code;
1469 	}
1470 	soft_class_table_descr.pm_evc_event_table_size = \
1471 	    soft_event_info.pm_nevent;
1472 	soft_class_table_descr.pm_evc_event_table = \
1473 	    soft_event_table;
1474 
1475 	/*
1476 	 * Fill in the class table.
1477 	 */
1478 	n = 0;
1479 	for (unsigned i = 0; i < PMC_CLASS_TABLE_SIZE; i++) {
1480 		switch (cpu_info.pm_classes[i].pm_class) {
1481 #if defined(__amd64__) || defined(__i386__)
1482 		case PMC_CLASS_TSC:
1483 			pmc_class_table[n++] = &tsc_class_table_descr;
1484 			break;
1485 
1486 		case PMC_CLASS_K8:
1487 			pmc_class_table[n++] = &k8_class_table_descr;
1488 			break;
1489 #endif
1490 
1491 		case PMC_CLASS_SOFT:
1492 			pmc_class_table[n++] = &soft_class_table_descr;
1493 			break;
1494 
1495 #if defined(__arm__)
1496 		case PMC_CLASS_ARMV7:
1497 			switch (cpu_info.pm_cputype) {
1498 			case PMC_CPU_ARMV7_CORTEX_A8:
1499 				pmc_class_table[n++] =
1500 				    &cortex_a8_class_table_descr;
1501 				break;
1502 			case PMC_CPU_ARMV7_CORTEX_A9:
1503 				pmc_class_table[n++] =
1504 				    &cortex_a9_class_table_descr;
1505 				break;
1506 			default:
1507 				errno = ENXIO;
1508 				return (pmc_syscall = -1);
1509 			}
1510 			break;
1511 #endif
1512 
1513 #if defined(__aarch64__)
1514 		case PMC_CLASS_ARMV8:
1515 			switch (cpu_info.pm_cputype) {
1516 			case PMC_CPU_ARMV8_CORTEX_A53:
1517 				pmc_class_table[n++] =
1518 				    &cortex_a53_class_table_descr;
1519 				break;
1520 			case PMC_CPU_ARMV8_CORTEX_A57:
1521 				pmc_class_table[n++] =
1522 				    &cortex_a57_class_table_descr;
1523 				break;
1524 			case PMC_CPU_ARMV8_CORTEX_A76:
1525 				pmc_class_table[n++] =
1526 				    &cortex_a76_class_table_descr;
1527 				break;
1528 			default:
1529 				errno = ENXIO;
1530 				return (pmc_syscall = -1);
1531 			}
1532 			break;
1533 
1534 		case PMC_CLASS_DMC620_PMU_CD2:
1535 			pmc_class_table[n++] =
1536 			    &dmc620_pmu_cd2_class_table_descr;
1537 			break;
1538 
1539 		case PMC_CLASS_DMC620_PMU_C:
1540 			pmc_class_table[n++] = &dmc620_pmu_c_class_table_descr;
1541 			break;
1542 
1543 		case PMC_CLASS_CMN600_PMU:
1544 			pmc_class_table[n++] = &cmn600_pmu_class_table_descr;
1545 			break;
1546 #endif
1547 
1548 #if defined(__powerpc__)
1549 		case PMC_CLASS_PPC7450:
1550 			pmc_class_table[n++] = &ppc7450_class_table_descr;
1551 			break;
1552 
1553 		case PMC_CLASS_PPC970:
1554 			pmc_class_table[n++] = &ppc970_class_table_descr;
1555 			break;
1556 
1557 		case PMC_CLASS_E500:
1558 			pmc_class_table[n++] = &e500_class_table_descr;
1559 			break;
1560 #endif
1561 
1562 		default:
1563 #if defined(DEBUG)
1564 			printf("pm_class: 0x%x\n",
1565 			    cpu_info.pm_classes[i].pm_class);
1566 #endif
1567 			break;
1568 		}
1569 	}
1570 
1571 #define	PMC_MDEP_INIT(C) pmc_mdep_event_aliases = C##_aliases
1572 
1573 	/* Configure the event name parser. */
1574 	switch (cpu_info.pm_cputype) {
1575 #if defined(__amd64__) || defined(__i386__)
1576 	case PMC_CPU_AMD_K8:
1577 		PMC_MDEP_INIT(k8);
1578 		break;
1579 #endif
1580 	case PMC_CPU_GENERIC:
1581 		PMC_MDEP_INIT(generic);
1582 		break;
1583 #if defined(__arm__)
1584 	case PMC_CPU_ARMV7_CORTEX_A8:
1585 		PMC_MDEP_INIT(cortex_a8);
1586 		break;
1587 	case PMC_CPU_ARMV7_CORTEX_A9:
1588 		PMC_MDEP_INIT(cortex_a9);
1589 		break;
1590 #endif
1591 #if defined(__aarch64__)
1592 	case PMC_CPU_ARMV8_CORTEX_A53:
1593 		PMC_MDEP_INIT(cortex_a53);
1594 		break;
1595 	case PMC_CPU_ARMV8_CORTEX_A57:
1596 		PMC_MDEP_INIT(cortex_a57);
1597 		break;
1598 	case PMC_CPU_ARMV8_CORTEX_A76:
1599 		PMC_MDEP_INIT(cortex_a76);
1600 		break;
1601 #endif
1602 #if defined(__powerpc__)
1603 	case PMC_CPU_PPC_7450:
1604 		PMC_MDEP_INIT(ppc7450);
1605 		break;
1606 	case PMC_CPU_PPC_970:
1607 		PMC_MDEP_INIT(ppc970);
1608 		break;
1609 	case PMC_CPU_PPC_E500:
1610 		PMC_MDEP_INIT(e500);
1611 		break;
1612 #endif
1613 	default:
1614 		/*
1615 		 * Some kind of CPU this version of the library knows nothing
1616 		 * about.  This shouldn't happen since the abi version check
1617 		 * should have caught this.
1618 		 */
1619 #if defined(__amd64__) || defined(__i386__) || defined(__powerpc64__)
1620 		break;
1621 #endif
1622 		errno = ENXIO;
1623 		return (pmc_syscall = -1);
1624 	}
1625 
1626 	return (0);
1627 }
1628 
1629 const char *
1630 pmc_name_of_capability(enum pmc_caps cap)
1631 {
1632 	int i;
1633 
1634 	/*
1635 	 * 'cap' should have a single bit set and should be in
1636 	 * range.
1637 	 */
1638 	if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
1639 	    cap > PMC_CAP_LAST) {
1640 		errno = EINVAL;
1641 		return (NULL);
1642 	}
1643 
1644 	i = ffs(cap);
1645 	return (pmc_capability_names[i - 1]);
1646 }
1647 
1648 const char *
1649 pmc_name_of_class(enum pmc_class pc)
1650 {
1651 	size_t n;
1652 
1653 	for (n = 0; n < PMC_TABLE_SIZE(pmc_class_names); n++)
1654 		if (pc == pmc_class_names[n].pm_class)
1655 			return (pmc_class_names[n].pm_name);
1656 
1657 	errno = EINVAL;
1658 	return (NULL);
1659 }
1660 
1661 const char *
1662 pmc_name_of_cputype(enum pmc_cputype cp)
1663 {
1664 	size_t n;
1665 
1666 	for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
1667 		if (cp == pmc_cputype_names[n].pm_cputype)
1668 			return (pmc_cputype_names[n].pm_name);
1669 
1670 	errno = EINVAL;
1671 	return (NULL);
1672 }
1673 
1674 const char *
1675 pmc_name_of_disposition(enum pmc_disp pd)
1676 {
1677 	if ((int) pd >= PMC_DISP_FIRST &&
1678 	    pd <= PMC_DISP_LAST)
1679 		return (pmc_disposition_names[pd]);
1680 
1681 	errno = EINVAL;
1682 	return (NULL);
1683 }
1684 
1685 const char *
1686 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
1687 {
1688 	const struct pmc_event_descr *ev, *evfence;
1689 
1690 	ev = evfence = NULL;
1691 	if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
1692 		ev = k8_event_table;
1693 		evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
1694 
1695 	} else if (pe >= PMC_EV_ARMV7_FIRST && pe <= PMC_EV_ARMV7_LAST) {
1696 		switch (cpu) {
1697 		case PMC_CPU_ARMV7_CORTEX_A8:
1698 			ev = cortex_a8_event_table;
1699 			evfence = cortex_a8_event_table + PMC_EVENT_TABLE_SIZE(cortex_a8);
1700 			break;
1701 		case PMC_CPU_ARMV7_CORTEX_A9:
1702 			ev = cortex_a9_event_table;
1703 			evfence = cortex_a9_event_table + PMC_EVENT_TABLE_SIZE(cortex_a9);
1704 			break;
1705 		default:	/* Unknown CPU type. */
1706 			break;
1707 		}
1708 	} else if (pe >= PMC_EV_ARMV8_FIRST && pe <= PMC_EV_ARMV8_LAST) {
1709 		switch (cpu) {
1710 		case PMC_CPU_ARMV8_CORTEX_A53:
1711 			ev = cortex_a53_event_table;
1712 			evfence = cortex_a53_event_table + PMC_EVENT_TABLE_SIZE(cortex_a53);
1713 			break;
1714 		case PMC_CPU_ARMV8_CORTEX_A57:
1715 			ev = cortex_a57_event_table;
1716 			evfence = cortex_a57_event_table + PMC_EVENT_TABLE_SIZE(cortex_a57);
1717 			break;
1718 		case PMC_CPU_ARMV8_CORTEX_A76:
1719 			ev = cortex_a76_event_table;
1720 			evfence = cortex_a76_event_table + PMC_EVENT_TABLE_SIZE(cortex_a76);
1721 			break;
1722 		default:	/* Unknown CPU type. */
1723 			break;
1724 		}
1725 	} else if (pe >= PMC_EV_CMN600_PMU_FIRST &&
1726 	    pe <= PMC_EV_CMN600_PMU_LAST) {
1727 		ev = cmn600_pmu_event_table;
1728 		evfence = cmn600_pmu_event_table +
1729 		    PMC_EVENT_TABLE_SIZE(cmn600_pmu);
1730 	} else if (pe >= PMC_EV_DMC620_PMU_CD2_FIRST &&
1731 	    pe <= PMC_EV_DMC620_PMU_CD2_LAST) {
1732 		ev = dmc620_pmu_cd2_event_table;
1733 		evfence = dmc620_pmu_cd2_event_table +
1734 		    PMC_EVENT_TABLE_SIZE(dmc620_pmu_cd2);
1735 	} else if (pe >= PMC_EV_DMC620_PMU_C_FIRST &&
1736 	    pe <= PMC_EV_DMC620_PMU_C_LAST) {
1737 		ev = dmc620_pmu_c_event_table;
1738 		evfence = dmc620_pmu_c_event_table +
1739 		    PMC_EVENT_TABLE_SIZE(dmc620_pmu_c);
1740 	} else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
1741 		ev = ppc7450_event_table;
1742 		evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450);
1743 	} else if (pe >= PMC_EV_PPC970_FIRST && pe <= PMC_EV_PPC970_LAST) {
1744 		ev = ppc970_event_table;
1745 		evfence = ppc970_event_table + PMC_EVENT_TABLE_SIZE(ppc970);
1746 	} else if (pe >= PMC_EV_E500_FIRST && pe <= PMC_EV_E500_LAST) {
1747 		ev = e500_event_table;
1748 		evfence = e500_event_table + PMC_EVENT_TABLE_SIZE(e500);
1749 	} else if (pe == PMC_EV_TSC_TSC) {
1750 		ev = tsc_event_table;
1751 		evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
1752 	} else if ((int)pe >= PMC_EV_SOFT_FIRST && (int)pe <= PMC_EV_SOFT_LAST) {
1753 		ev = soft_event_table;
1754 		evfence = soft_event_table + soft_event_info.pm_nevent;
1755 	}
1756 
1757 	for (; ev != evfence; ev++)
1758 		if (pe == ev->pm_ev_code)
1759 			return (ev->pm_ev_name);
1760 
1761 	return (NULL);
1762 }
1763 
1764 const char *
1765 pmc_name_of_event(enum pmc_event pe)
1766 {
1767 	const char *n;
1768 
1769 	if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
1770 		return (n);
1771 
1772 	errno = EINVAL;
1773 	return (NULL);
1774 }
1775 
1776 const char *
1777 pmc_name_of_mode(enum pmc_mode pm)
1778 {
1779 	if ((int) pm >= PMC_MODE_FIRST &&
1780 	    pm <= PMC_MODE_LAST)
1781 		return (pmc_mode_names[pm]);
1782 
1783 	errno = EINVAL;
1784 	return (NULL);
1785 }
1786 
1787 const char *
1788 pmc_name_of_state(enum pmc_state ps)
1789 {
1790 	if ((int) ps >= PMC_STATE_FIRST &&
1791 	    ps <= PMC_STATE_LAST)
1792 		return (pmc_state_names[ps]);
1793 
1794 	errno = EINVAL;
1795 	return (NULL);
1796 }
1797 
1798 int
1799 pmc_ncpu(void)
1800 {
1801 	if (pmc_syscall == -1) {
1802 		errno = ENXIO;
1803 		return (-1);
1804 	}
1805 
1806 	return (cpu_info.pm_ncpu);
1807 }
1808 
1809 int
1810 pmc_npmc(int cpu)
1811 {
1812 	if (pmc_syscall == -1) {
1813 		errno = ENXIO;
1814 		return (-1);
1815 	}
1816 
1817 	if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
1818 		errno = EINVAL;
1819 		return (-1);
1820 	}
1821 
1822 	return (cpu_info.pm_npmc);
1823 }
1824 
1825 int
1826 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
1827 {
1828 	int nbytes, npmc;
1829 	struct pmc_op_getpmcinfo *pmci;
1830 
1831 	if ((npmc = pmc_npmc(cpu)) < 0)
1832 		return (-1);
1833 
1834 	nbytes = sizeof(struct pmc_op_getpmcinfo) +
1835 	    npmc * sizeof(struct pmc_info);
1836 
1837 	if ((pmci = calloc(1, nbytes)) == NULL)
1838 		return (-1);
1839 
1840 	pmci->pm_cpu  = cpu;
1841 
1842 	if (PMC_CALL(PMC_OP_GETPMCINFO, pmci) < 0) {
1843 		free(pmci);
1844 		return (-1);
1845 	}
1846 
1847 	/* kernel<->library, library<->userland interfaces are identical */
1848 	*ppmci = (struct pmc_pmcinfo *) pmci;
1849 	return (0);
1850 }
1851 
1852 int
1853 pmc_read(pmc_id_t pmc, pmc_value_t *value)
1854 {
1855 	struct pmc_op_pmcrw pmc_read_op;
1856 
1857 	pmc_read_op.pm_pmcid = pmc;
1858 	pmc_read_op.pm_flags = PMC_F_OLDVALUE;
1859 	pmc_read_op.pm_value = -1;
1860 
1861 	if (PMC_CALL(PMC_OP_PMCRW, &pmc_read_op) < 0)
1862 		return (-1);
1863 
1864 	*value = pmc_read_op.pm_value;
1865 	return (0);
1866 }
1867 
1868 int
1869 pmc_release(pmc_id_t pmc)
1870 {
1871 	struct pmc_op_simple	pmc_release_args;
1872 
1873 	pmc_release_args.pm_pmcid = pmc;
1874 	return (PMC_CALL(PMC_OP_PMCRELEASE, &pmc_release_args));
1875 }
1876 
1877 int
1878 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
1879 {
1880 	struct pmc_op_pmcrw pmc_rw_op;
1881 
1882 	pmc_rw_op.pm_pmcid = pmc;
1883 	pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
1884 	pmc_rw_op.pm_value = newvalue;
1885 
1886 	if (PMC_CALL(PMC_OP_PMCRW, &pmc_rw_op) < 0)
1887 		return (-1);
1888 
1889 	*oldvaluep = pmc_rw_op.pm_value;
1890 	return (0);
1891 }
1892 
1893 int
1894 pmc_set(pmc_id_t pmc, pmc_value_t value)
1895 {
1896 	struct pmc_op_pmcsetcount sc;
1897 
1898 	sc.pm_pmcid = pmc;
1899 	sc.pm_count = value;
1900 
1901 	if (PMC_CALL(PMC_OP_PMCSETCOUNT, &sc) < 0)
1902 		return (-1);
1903 	return (0);
1904 }
1905 
1906 int
1907 pmc_start(pmc_id_t pmc)
1908 {
1909 	struct pmc_op_simple	pmc_start_args;
1910 
1911 	pmc_start_args.pm_pmcid = pmc;
1912 	return (PMC_CALL(PMC_OP_PMCSTART, &pmc_start_args));
1913 }
1914 
1915 int
1916 pmc_stop(pmc_id_t pmc)
1917 {
1918 	struct pmc_op_simple	pmc_stop_args;
1919 
1920 	pmc_stop_args.pm_pmcid = pmc;
1921 	return (PMC_CALL(PMC_OP_PMCSTOP, &pmc_stop_args));
1922 }
1923 
1924 int
1925 pmc_width(pmc_id_t pmcid, uint32_t *width)
1926 {
1927 	unsigned int i;
1928 	enum pmc_class cl;
1929 
1930 	cl = PMC_ID_TO_CLASS(pmcid);
1931 	for (i = 0; i < cpu_info.pm_nclass; i++)
1932 		if (cpu_info.pm_classes[i].pm_class == cl) {
1933 			*width = cpu_info.pm_classes[i].pm_width;
1934 			return (0);
1935 		}
1936 	errno = EINVAL;
1937 	return (-1);
1938 }
1939 
1940 int
1941 pmc_write(pmc_id_t pmc, pmc_value_t value)
1942 {
1943 	struct pmc_op_pmcrw pmc_write_op;
1944 
1945 	pmc_write_op.pm_pmcid = pmc;
1946 	pmc_write_op.pm_flags = PMC_F_NEWVALUE;
1947 	pmc_write_op.pm_value = value;
1948 	return (PMC_CALL(PMC_OP_PMCRW, &pmc_write_op));
1949 }
1950 
1951 int
1952 pmc_writelog(uint32_t userdata)
1953 {
1954 	struct pmc_op_writelog wl;
1955 
1956 	wl.pm_userdata = userdata;
1957 	return (PMC_CALL(PMC_OP_WRITELOG, &wl));
1958 }
1959