1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef	_LIBCPC_IMPL_H
28*0Sstevel@tonic-gate #define	_LIBCPC_IMPL_H
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <libcpc.h>
33*0Sstevel@tonic-gate #include <inttypes.h>
34*0Sstevel@tonic-gate #include <thread.h>
35*0Sstevel@tonic-gate #include <synch.h>
36*0Sstevel@tonic-gate #include <sys/types.h>
37*0Sstevel@tonic-gate #include <sys/cpc_impl.h>
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #ifdef	__cplusplus
40*0Sstevel@tonic-gate extern "C" {
41*0Sstevel@tonic-gate #endif
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #define	CPC_VER_1 1
44*0Sstevel@tonic-gate #define	CPC1_BUFSIZE (2 * sizeof (uint64_t))
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate struct _cpc_attr {
47*0Sstevel@tonic-gate 	char			ca_name[CPC_MAX_ATTR_LEN];
48*0Sstevel@tonic-gate 	uint64_t		ca_val;
49*0Sstevel@tonic-gate };
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate typedef struct __cpc_request cpc_request_t;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate struct __cpc_request {
54*0Sstevel@tonic-gate 	char			cr_event[CPC_MAX_EVENT_LEN];
55*0Sstevel@tonic-gate 	uint64_t		cr_preset;	/* Initial value */
56*0Sstevel@tonic-gate 	uint16_t		cr_index;	/* Index of request in data */
57*0Sstevel@tonic-gate 	uint_t			cr_flags;
58*0Sstevel@tonic-gate 	uint_t			cr_nattrs;	/* # CPU-specific attrs */
59*0Sstevel@tonic-gate 	kcpc_attr_t		*cr_attr;
60*0Sstevel@tonic-gate 	cpc_request_t		*cr_next;	/* next request in set */
61*0Sstevel@tonic-gate };
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate struct __cpc_buf {
64*0Sstevel@tonic-gate 	uint64_t		*cb_data;	/* Pointer to data store */
65*0Sstevel@tonic-gate 	hrtime_t		cb_hrtime;	/* hrtime at last sample */
66*0Sstevel@tonic-gate 	uint64_t		cb_tick;	/* virtualized tsc/tick */
67*0Sstevel@tonic-gate 	size_t			cb_size;	/* Size of data store, bytes */
68*0Sstevel@tonic-gate 	cpc_buf_t		*cb_next;	/* List of all bufs */
69*0Sstevel@tonic-gate };
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * Possible cpc_set_t states:
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate typedef enum {
75*0Sstevel@tonic-gate 	CS_UNBOUND,		/* Set is not currently bound */
76*0Sstevel@tonic-gate 	CS_BOUND_CURLWP,	/* Set has been bound to curlwp */
77*0Sstevel@tonic-gate 	CS_BOUND_PCTX,		/* Set has been bound via libpctx */
78*0Sstevel@tonic-gate 	CS_BOUND_CPU		/* Set has been bound to a CPU */
79*0Sstevel@tonic-gate } __cpc_state_t;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate struct __cpc_set {
82*0Sstevel@tonic-gate 	cpc_request_t		*cs_request;	/* linked list of requests */
83*0Sstevel@tonic-gate 	__cpc_state_t		cs_state;	/* State of this set */
84*0Sstevel@tonic-gate 	int			cs_nreqs;	/* Number of requests in set */
85*0Sstevel@tonic-gate 	int			cs_fd;		/* file descriptor of cpc dev */
86*0Sstevel@tonic-gate 	processorid_t		cs_obind;	/* previous proc binding */
87*0Sstevel@tonic-gate 	pctx_t			*cs_pctx;	/* pctx of process bound to */
88*0Sstevel@tonic-gate 	id_t			cs_id;		/* lwp ID of pctx binding */
89*0Sstevel@tonic-gate 	thread_t		cs_thr;		/* thread ID which bound set */
90*0Sstevel@tonic-gate 	cpc_set_t		*cs_next;	/* Linked list of all sets */
91*0Sstevel@tonic-gate };
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate struct __cpc {
94*0Sstevel@tonic-gate 	cpc_set_t		*cpc_sets;	/* List of existing sets */
95*0Sstevel@tonic-gate 	cpc_buf_t		*cpc_bufs;	/* List of existing bufs */
96*0Sstevel@tonic-gate 	cpc_errhndlr_t		*cpc_errfn;	/* Handles library errors */
97*0Sstevel@tonic-gate 	mutex_t			cpc_lock;	/* Protect various ops */
98*0Sstevel@tonic-gate 	char			*cpc_attrlist;	/* List of supported attrs */
99*0Sstevel@tonic-gate 	char			**cpc_evlist;	/* List of events per pic */
100*0Sstevel@tonic-gate 	char			cpc_cpuref[CPC_MAX_CPUREF];
101*0Sstevel@tonic-gate 	char			cpc_cciname[CPC_MAX_IMPL_NAME];
102*0Sstevel@tonic-gate 	uint_t			cpc_caps;
103*0Sstevel@tonic-gate 	uint_t			cpc_npic;
104*0Sstevel@tonic-gate };
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate  * cpc_t handle for CPCv1 clients.
108*0Sstevel@tonic-gate  */
109*0Sstevel@tonic-gate extern cpc_t *__cpc;
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /*PRINTFLIKE2*/
112*0Sstevel@tonic-gate extern void __cpc_error(const char *fn, const char *fmt, ...);
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate extern const char *__cpc_reg_to_name(int cpuver, int regno, uint8_t bits);
115*0Sstevel@tonic-gate extern int __cpc_name_to_reg(int cpuver, int regno,
116*0Sstevel@tonic-gate     const char *name, uint8_t *bits);
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate extern uint_t __cpc_workver;
119*0Sstevel@tonic-gate extern int __cpc_v1_cpuver;
120*0Sstevel@tonic-gate #ifdef __sparc
121*0Sstevel@tonic-gate extern uint64_t __cpc_v1_pcr;
122*0Sstevel@tonic-gate #else
123*0Sstevel@tonic-gate extern uint32_t __cpc_v1_pes[2];
124*0Sstevel@tonic-gate #endif /* __sparc */
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate extern char *__cpc_pack_set(cpc_set_t *set, uint_t flags, size_t *buflen);
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate typedef struct __cpc_strhash cpc_strhash_t;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate struct __cpc_strhash {
131*0Sstevel@tonic-gate 	char *str;
132*0Sstevel@tonic-gate 	struct __cpc_strhash *cur;
133*0Sstevel@tonic-gate 	struct __cpc_strhash *next;
134*0Sstevel@tonic-gate };
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate extern cpc_strhash_t *__cpc_strhash_alloc(void);
137*0Sstevel@tonic-gate extern void __cpc_strhash_free(cpc_strhash_t *hash);
138*0Sstevel@tonic-gate extern int __cpc_strhash_add(cpc_strhash_t *hash, char *key);
139*0Sstevel@tonic-gate extern char *__cpc_strhash_next(cpc_strhash_t *hash);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /*
142*0Sstevel@tonic-gate  * Implementation-private system call used by libcpc
143*0Sstevel@tonic-gate  */
144*0Sstevel@tonic-gate struct __cpc;
145*0Sstevel@tonic-gate extern int __pctx_cpc(pctx_t *pctx, struct __cpc *cpc, int cmd, id_t lwpid,
146*0Sstevel@tonic-gate     void *data1, void *data2, void *data3, int bufsize);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate #define	CPUDRV				"/devices/pseudo/cpc@0"
149*0Sstevel@tonic-gate #define	CPUDRV_SHARED			CPUDRV":shared"
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate #if defined(__sparc) || defined(__i386)
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate  * These two are only used for backwards compatibility to the Obsolete CPCv1.
154*0Sstevel@tonic-gate  */
155*0Sstevel@tonic-gate extern int __cpc_init(void);
156*0Sstevel@tonic-gate extern cpc_set_t *__cpc_eventtoset(cpc_t *cpc, cpc_event_t *event, int flags);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * ce_cpuver values
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate #define	CPC_ULTRA1		1000
162*0Sstevel@tonic-gate #define	CPC_ULTRA2		1001	/* same as ultra1 for these purposes */
163*0Sstevel@tonic-gate #define	CPC_ULTRA3		1002
164*0Sstevel@tonic-gate #define	CPC_ULTRA3_PLUS		1003
165*0Sstevel@tonic-gate #define	CPC_ULTRA3_I		1004
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate #define	CPC_PENTIUM		2000
168*0Sstevel@tonic-gate #define	CPC_PENTIUM_MMX		2001
169*0Sstevel@tonic-gate #define	CPC_PENTIUM_PRO		2002
170*0Sstevel@tonic-gate #define	CPC_PENTIUM_PRO_MMX	2003
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate #define	CPC_SPARC64_III		3000
173*0Sstevel@tonic-gate #define	CPC_SPARC64_V		3002
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate #endif /* __sparc || __i386 */
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
178*0Sstevel@tonic-gate /*
179*0Sstevel@tonic-gate  * This is common between i386 and amd64, because amd64 implements %tick.
180*0Sstevel@tonic-gate  * Currently only used by the cpc tools to print the label atop the CPU ticks
181*0Sstevel@tonic-gate  * column on amd64.
182*0Sstevel@tonic-gate  */
183*0Sstevel@tonic-gate #define	CPC_TICKREG_NAME	"tsc"
184*0Sstevel@tonic-gate #endif /* __i386 || __amd64 */
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate #if defined(__sparc)
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate /*
189*0Sstevel@tonic-gate  * UltraSPARC I, II and III processors
190*0Sstevel@tonic-gate  *
191*0Sstevel@tonic-gate  * The performance counters on these processors allow up to two 32-bit
192*0Sstevel@tonic-gate  * performance events to be captured simultaneously from a selection
193*0Sstevel@tonic-gate  * of metrics.   The metrics are selected by writing to the performance
194*0Sstevel@tonic-gate  * control register, and subsequent values collected by reading from the
195*0Sstevel@tonic-gate  * performance instrumentation counter registers.  Both registers are
196*0Sstevel@tonic-gate  * priviliged by default, and implemented as ASRs.
197*0Sstevel@tonic-gate  */
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate struct _cpc_event {
200*0Sstevel@tonic-gate 	int ce_cpuver;
201*0Sstevel@tonic-gate 	hrtime_t ce_hrt;	/* gethrtime() */
202*0Sstevel@tonic-gate 	uint64_t ce_tick;	/* virtualized %tick */
203*0Sstevel@tonic-gate 	uint64_t ce_pic[2];	/* virtualized %pic */
204*0Sstevel@tonic-gate 	uint64_t ce_pcr;	/* %pcr */
205*0Sstevel@tonic-gate };
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate #define	CPC_TICKREG(ev)		((ev)->ce_tick)
208*0Sstevel@tonic-gate #define	CPC_TICKREG_NAME	"%tick"
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate /*
211*0Sstevel@tonic-gate  * "Well known" bitfields in the UltraSPARC %pcr register
212*0Sstevel@tonic-gate  * The interfaces in libcpc should make these #defines uninteresting.
213*0Sstevel@tonic-gate  */
214*0Sstevel@tonic-gate #define	CPC_ULTRA_PCR_USR		2
215*0Sstevel@tonic-gate #define	CPC_ULTRA_PCR_SYS		1
216*0Sstevel@tonic-gate #define	CPC_ULTRA_PCR_PRIVPIC		0
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate #define	CPC_ULTRA_PCR_PIC0_SHIFT	4
219*0Sstevel@tonic-gate #define	CPC_ULTRA2_PCR_PIC0_MASK	UINT64_C(0xf)
220*0Sstevel@tonic-gate #define	CPC_ULTRA3_PCR_PIC0_MASK	UINT64_C(0x3f)
221*0Sstevel@tonic-gate #define	CPC_ULTRA_PCR_PIC1_SHIFT	11
222*0Sstevel@tonic-gate #define	CPC_ULTRA2_PCR_PIC1_MASK	UINT64_C(0xf)
223*0Sstevel@tonic-gate #define	CPC_ULTRA3_PCR_PIC1_MASK	UINT64_C(0x3f)
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate #elif defined(__i386)
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate /*
228*0Sstevel@tonic-gate  * Pentium I, II and III processors
229*0Sstevel@tonic-gate  *
230*0Sstevel@tonic-gate  * These CPUs allow pairs of events to captured.
231*0Sstevel@tonic-gate  * The hardware counters count up to 40-bits of significance, but
232*0Sstevel@tonic-gate  * only allow 32 (signed) bits to be programmed into them.
233*0Sstevel@tonic-gate  * Pentium I and Pentium II processors are programmed differently, but
234*0Sstevel@tonic-gate  * the resulting counters and timestamps can be handled portably.
235*0Sstevel@tonic-gate  */
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate struct _cpc_event {
238*0Sstevel@tonic-gate 	int ce_cpuver;
239*0Sstevel@tonic-gate 	hrtime_t ce_hrt;	/* gethrtime() */
240*0Sstevel@tonic-gate 	uint64_t ce_tsc;	/* virtualized rdtsc value */
241*0Sstevel@tonic-gate 	uint64_t ce_pic[2];	/* virtualized PerfCtr[01] */
242*0Sstevel@tonic-gate 	uint32_t ce_pes[2];	/* Pentium II */
243*0Sstevel@tonic-gate #define	ce_cesr	ce_pes[0]	/* Pentium I */
244*0Sstevel@tonic-gate };
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate #define	CPC_TICKREG(ev)		((ev)->ce_tsc)
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate /*
249*0Sstevel@tonic-gate  * "Well known" bit fields in the Pentium CES register
250*0Sstevel@tonic-gate  * The interfaces in libcpc should make these #defines uninteresting.
251*0Sstevel@tonic-gate  */
252*0Sstevel@tonic-gate #define	CPC_P5_CESR_ES0_SHIFT	0
253*0Sstevel@tonic-gate #define	CPC_P5_CESR_ES0_MASK	0x3f
254*0Sstevel@tonic-gate #define	CPC_P5_CESR_ES1_SHIFT	16
255*0Sstevel@tonic-gate #define	CPC_P5_CESR_ES1_MASK	0x3f
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate #define	CPC_P5_CESR_OS0		6
258*0Sstevel@tonic-gate #define	CPC_P5_CESR_USR0	7
259*0Sstevel@tonic-gate #define	CPC_P5_CESR_CLK0	8
260*0Sstevel@tonic-gate #define	CPC_P5_CESR_PC0		9
261*0Sstevel@tonic-gate #define	CPC_P5_CESR_OS1		(CPC_P5_CESR_OS0 + 16)
262*0Sstevel@tonic-gate #define	CPC_P5_CESR_USR1	(CPC_P5_CESR_USR0 + 16)
263*0Sstevel@tonic-gate #define	CPC_P5_CESR_CLK1	(CPC_P5_CESR_CLK0 + 16)
264*0Sstevel@tonic-gate #define	CPC_P5_CESR_PC1		(CPC_P5_CESR_PC0 + 16)
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate /*
267*0Sstevel@tonic-gate  * "Well known" bit fields in the Pentium Pro PerfEvtSel registers
268*0Sstevel@tonic-gate  * The interfaces in libcpc should make these #defines uninteresting.
269*0Sstevel@tonic-gate  */
270*0Sstevel@tonic-gate #define	CPC_P6_PES_INV		23
271*0Sstevel@tonic-gate #define	CPC_P6_PES_EN		22
272*0Sstevel@tonic-gate #define	CPC_P6_PES_INT		20
273*0Sstevel@tonic-gate #define	CPC_P6_PES_PC		19
274*0Sstevel@tonic-gate #define	CPC_P6_PES_E		18
275*0Sstevel@tonic-gate #define	CPC_P6_PES_OS		17
276*0Sstevel@tonic-gate #define	CPC_P6_PES_USR		16
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate #define	CPC_P6_PES_UMASK_SHIFT	8
279*0Sstevel@tonic-gate #define	CPC_P6_PES_UMASK_MASK	(0xffu)
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate #define	CPC_P6_PES_CMASK_SHIFT	24
282*0Sstevel@tonic-gate #define	CPC_P6_PES_CMASK_MASK	(0xffu)
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate #define	CPC_P6_PES_PIC0_MASK	(0xffu)
285*0Sstevel@tonic-gate #define	CPC_P6_PES_PIC1_MASK	(0xffu)
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate #endif /* __i386 */
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate #ifdef	__cplusplus
290*0Sstevel@tonic-gate }
291*0Sstevel@tonic-gate #endif
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate #endif	/* _LIBCPC_IMPL_H */
294