17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5346af85bScwb  * Common Development and Distribution License (the "License").
6346af85bScwb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22843e1988Sjohnlev  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Performance Counter Back-End for AMD Opteron and AMD Athlon 64 processors.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/cpc_pcbe.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
387c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
397c478bd9Sstevel@tonic-gate #include <sys/errno.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
427c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
437c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
445d3a5ad8Srab #include <sys/ddi.h>
455d3a5ad8Srab #include <sys/sunddi.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static int opt_pcbe_init(void);
487c478bd9Sstevel@tonic-gate static uint_t opt_pcbe_ncounters(void);
497c478bd9Sstevel@tonic-gate static const char *opt_pcbe_impl_name(void);
507c478bd9Sstevel@tonic-gate static const char *opt_pcbe_cpuref(void);
517c478bd9Sstevel@tonic-gate static char *opt_pcbe_list_events(uint_t picnum);
527c478bd9Sstevel@tonic-gate static char *opt_pcbe_list_attrs(void);
537c478bd9Sstevel@tonic-gate static uint64_t opt_pcbe_event_coverage(char *event);
547c478bd9Sstevel@tonic-gate static uint64_t opt_pcbe_overflow_bitmap(void);
557c478bd9Sstevel@tonic-gate static int opt_pcbe_configure(uint_t picnum, char *event, uint64_t preset,
567c478bd9Sstevel@tonic-gate     uint32_t flags, uint_t nattrs, kcpc_attr_t *attrs, void **data,
577c478bd9Sstevel@tonic-gate     void *token);
587c478bd9Sstevel@tonic-gate static void opt_pcbe_program(void *token);
597c478bd9Sstevel@tonic-gate static void opt_pcbe_allstop(void);
607c478bd9Sstevel@tonic-gate static void opt_pcbe_sample(void *token);
617c478bd9Sstevel@tonic-gate static void opt_pcbe_free(void *config);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static pcbe_ops_t opt_pcbe_ops = {
647c478bd9Sstevel@tonic-gate 	PCBE_VER_1,
657c478bd9Sstevel@tonic-gate 	CPC_CAP_OVERFLOW_INTERRUPT,
667c478bd9Sstevel@tonic-gate 	opt_pcbe_ncounters,
677c478bd9Sstevel@tonic-gate 	opt_pcbe_impl_name,
687c478bd9Sstevel@tonic-gate 	opt_pcbe_cpuref,
697c478bd9Sstevel@tonic-gate 	opt_pcbe_list_events,
707c478bd9Sstevel@tonic-gate 	opt_pcbe_list_attrs,
717c478bd9Sstevel@tonic-gate 	opt_pcbe_event_coverage,
727c478bd9Sstevel@tonic-gate 	opt_pcbe_overflow_bitmap,
737c478bd9Sstevel@tonic-gate 	opt_pcbe_configure,
747c478bd9Sstevel@tonic-gate 	opt_pcbe_program,
757c478bd9Sstevel@tonic-gate 	opt_pcbe_allstop,
767c478bd9Sstevel@tonic-gate 	opt_pcbe_sample,
777c478bd9Sstevel@tonic-gate 	opt_pcbe_free
787c478bd9Sstevel@tonic-gate };
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Define offsets and masks for the fields in the Performance
827c478bd9Sstevel@tonic-gate  * Event-Select (PES) registers.
837c478bd9Sstevel@tonic-gate  */
84*31725658Sksadhukh #define	OPT_PES_HOST_SHIFT	41
85*31725658Sksadhukh #define	OPT_PES_GUEST_SHIFT	40
867c478bd9Sstevel@tonic-gate #define	OPT_PES_CMASK_SHIFT	24
877c478bd9Sstevel@tonic-gate #define	OPT_PES_CMASK_MASK	0xFF
887c478bd9Sstevel@tonic-gate #define	OPT_PES_INV_SHIFT	23
897c478bd9Sstevel@tonic-gate #define	OPT_PES_ENABLE_SHIFT	22
907c478bd9Sstevel@tonic-gate #define	OPT_PES_INT_SHIFT	20
917c478bd9Sstevel@tonic-gate #define	OPT_PES_PC_SHIFT	19
927c478bd9Sstevel@tonic-gate #define	OPT_PES_EDGE_SHIFT	18
937c478bd9Sstevel@tonic-gate #define	OPT_PES_OS_SHIFT	17
947c478bd9Sstevel@tonic-gate #define	OPT_PES_USR_SHIFT	16
957c478bd9Sstevel@tonic-gate #define	OPT_PES_UMASK_SHIFT	8
967c478bd9Sstevel@tonic-gate #define	OPT_PES_UMASK_MASK	0xFF
977c478bd9Sstevel@tonic-gate 
98*31725658Sksadhukh #define	OPT_PES_INV		(1ULL << OPT_PES_INV_SHIFT)
99*31725658Sksadhukh #define	OPT_PES_ENABLE		(1ULL << OPT_PES_ENABLE_SHIFT)
100*31725658Sksadhukh #define	OPT_PES_INT		(1ULL << OPT_PES_INT_SHIFT)
101*31725658Sksadhukh #define	OPT_PES_PC		(1ULL << OPT_PES_PC_SHIFT)
102*31725658Sksadhukh #define	OPT_PES_EDGE		(1ULL << OPT_PES_EDGE_SHIFT)
103*31725658Sksadhukh #define	OPT_PES_OS		(1ULL << OPT_PES_OS_SHIFT)
104*31725658Sksadhukh #define	OPT_PES_USR		(1ULL << OPT_PES_USR_SHIFT)
105*31725658Sksadhukh #define	OPT_PES_HOST		(1ULL << OPT_PES_HOST_SHIFT)
106*31725658Sksadhukh #define	OPT_PES_GUEST		(1ULL << OPT_PES_GUEST_SHIFT)
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate typedef struct _opt_pcbe_config {
1097c478bd9Sstevel@tonic-gate 	uint8_t		opt_picno;	/* Counter number: 0, 1, 2, or 3 */
1107c478bd9Sstevel@tonic-gate 	uint64_t	opt_evsel;	/* Event Selection register */
1117c478bd9Sstevel@tonic-gate 	uint64_t	opt_rawpic;	/* Raw counter value */
1127c478bd9Sstevel@tonic-gate } opt_pcbe_config_t;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate opt_pcbe_config_t nullcfgs[4] = {
1157c478bd9Sstevel@tonic-gate 	{ 0, 0, 0 },
1167c478bd9Sstevel@tonic-gate 	{ 1, 0, 0 },
1177c478bd9Sstevel@tonic-gate 	{ 2, 0, 0 },
1187c478bd9Sstevel@tonic-gate 	{ 3, 0, 0 }
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
121*31725658Sksadhukh typedef struct _amd_event {
1227c478bd9Sstevel@tonic-gate 	char		*name;
123*31725658Sksadhukh 	uint16_t	emask;		/* Event mask setting */
1247c478bd9Sstevel@tonic-gate 	uint8_t		umask_valid;	/* Mask of unreserved UNIT_MASK bits */
125*31725658Sksadhukh } amd_event_t;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * Base MSR addresses for the PerfEvtSel registers and the counters themselves.
1297c478bd9Sstevel@tonic-gate  * Add counter number to base address to get corresponding MSR address.
1307c478bd9Sstevel@tonic-gate  */
1317c478bd9Sstevel@tonic-gate #define	PES_BASE_ADDR	0xC0010000
1327c478bd9Sstevel@tonic-gate #define	PIC_BASE_ADDR	0xC0010004
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	MASK48		0xFFFFFFFFFFFF
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	EV_END {NULL, 0, 0}
1377c478bd9Sstevel@tonic-gate 
138*31725658Sksadhukh #define	AMD_cmn_events							\
139*31725658Sksadhukh 	{ "FP_dispatched_fpu_ops",			0x0, 0x3F },	\
140fb47e43fSjhaslam 	{ "FP_cycles_no_fpu_ops_retired",		0x1, 0x0 },	\
141fb47e43fSjhaslam 	{ "FP_dispatched_fpu_ops_ff",			0x2, 0x0 },	\
142fb47e43fSjhaslam 	{ "LS_seg_reg_load",				0x20, 0x7F },	\
143fb47e43fSjhaslam 	{ "LS_uarch_resync_self_modify",		0x21, 0x0 },	\
144fb47e43fSjhaslam 	{ "LS_uarch_resync_snoop",			0x22, 0x0 },	\
145fb47e43fSjhaslam 	{ "LS_buffer_2_full",				0x23, 0x0 },	\
146fb47e43fSjhaslam 	{ "LS_retired_cflush",				0x26, 0x0 },	\
147fb47e43fSjhaslam 	{ "LS_retired_cpuid",				0x27, 0x0 },	\
148fb47e43fSjhaslam 	{ "DC_access",					0x40, 0x0 },	\
149fb47e43fSjhaslam 	{ "DC_miss",					0x41, 0x0 },	\
150fb47e43fSjhaslam 	{ "DC_refill_from_L2",				0x42, 0x1F },	\
151fb47e43fSjhaslam 	{ "DC_refill_from_system",			0x43, 0x1F },	\
152fb47e43fSjhaslam 	{ "DC_misaligned_data_ref",			0x47, 0x0 },	\
153fb47e43fSjhaslam 	{ "DC_uarch_late_cancel_access",		0x48, 0x0 },	\
154fb47e43fSjhaslam 	{ "DC_uarch_early_cancel_access",		0x49, 0x0 },	\
155fb47e43fSjhaslam 	{ "DC_dispatched_prefetch_instr",		0x4B, 0x7 },	\
156fb47e43fSjhaslam 	{ "DC_dcache_accesses_by_locks",		0x4C, 0x2 },	\
157fb47e43fSjhaslam 	{ "BU_memory_requests",				0x65, 0x83},	\
158fb47e43fSjhaslam 	{ "BU_data_prefetch",				0x67, 0x3 },	\
159fb47e43fSjhaslam 	{ "BU_cpu_clk_unhalted",			0x76, 0x0 },	\
160fb47e43fSjhaslam 	{ "IC_fetch",					0x80, 0x0 },	\
161fb47e43fSjhaslam 	{ "IC_miss",					0x81, 0x0 },	\
162fb47e43fSjhaslam 	{ "IC_refill_from_L2",				0x82, 0x0 },	\
163fb47e43fSjhaslam 	{ "IC_refill_from_system",			0x83, 0x0 },	\
164fb47e43fSjhaslam 	{ "IC_itlb_L1_miss_L2_hit",			0x84, 0x0 },	\
165fb47e43fSjhaslam 	{ "IC_uarch_resync_snoop",			0x86, 0x0 },	\
166fb47e43fSjhaslam 	{ "IC_instr_fetch_stall",			0x87, 0x0 },	\
167fb47e43fSjhaslam 	{ "IC_return_stack_hit",			0x88, 0x0 },	\
168fb47e43fSjhaslam 	{ "IC_return_stack_overflow",			0x89, 0x0 },	\
169fb47e43fSjhaslam 	{ "FR_retired_x86_instr_w_excp_intr",		0xC0, 0x0 },	\
170fb47e43fSjhaslam 	{ "FR_retired_uops",				0xC1, 0x0 },	\
171fb47e43fSjhaslam 	{ "FR_retired_branches_w_excp_intr",		0xC2, 0x0 },	\
172fb47e43fSjhaslam 	{ "FR_retired_branches_mispred",		0xC3, 0x0 },	\
173fb47e43fSjhaslam 	{ "FR_retired_taken_branches",			0xC4, 0x0 },	\
174fb47e43fSjhaslam 	{ "FR_retired_taken_branches_mispred",		0xC5, 0x0 },	\
175fb47e43fSjhaslam 	{ "FR_retired_far_ctl_transfer",		0xC6, 0x0 },	\
176fb47e43fSjhaslam 	{ "FR_retired_resyncs",				0xC7, 0x0 },	\
177fb47e43fSjhaslam 	{ "FR_retired_near_rets",			0xC8, 0x0 },	\
178fb47e43fSjhaslam 	{ "FR_retired_near_rets_mispred",		0xC9, 0x0 },	\
179fb47e43fSjhaslam 	{ "FR_retired_taken_branches_mispred_addr_miscop",	0xCA, 0x0 },\
180fb47e43fSjhaslam 	{ "FR_retired_fastpath_double_op_instr",	0xCC, 0x7 },	\
181fb47e43fSjhaslam 	{ "FR_intr_masked_cycles",			0xCD, 0x0 },	\
182fb47e43fSjhaslam 	{ "FR_intr_masked_while_pending_cycles",	0xCE, 0x0 },	\
183fb47e43fSjhaslam 	{ "FR_taken_hardware_intrs",			0xCF, 0x0 },	\
184fb47e43fSjhaslam 	{ "FR_nothing_to_dispatch",			0xD0, 0x0 },	\
185fb47e43fSjhaslam 	{ "FR_dispatch_stalls",				0xD1, 0x0 },	\
186fb47e43fSjhaslam 	{ "FR_dispatch_stall_branch_abort_to_retire",	0xD2, 0x0 },	\
187fb47e43fSjhaslam 	{ "FR_dispatch_stall_serialization",		0xD3, 0x0 },	\
188fb47e43fSjhaslam 	{ "FR_dispatch_stall_segment_load",		0xD4, 0x0 },	\
189fb47e43fSjhaslam 	{ "FR_dispatch_stall_reorder_buffer_full",	0xD5, 0x0 },	\
190fb47e43fSjhaslam 	{ "FR_dispatch_stall_resv_stations_full",	0xD6, 0x0 },	\
191fb47e43fSjhaslam 	{ "FR_dispatch_stall_fpu_full",			0xD7, 0x0 },	\
192fb47e43fSjhaslam 	{ "FR_dispatch_stall_ls_full",			0xD8, 0x0 },	\
193fb47e43fSjhaslam 	{ "FR_dispatch_stall_waiting_all_quiet",	0xD9, 0x0 },	\
194fb47e43fSjhaslam 	{ "FR_dispatch_stall_far_ctl_trsfr_resync_branc_pend",	0xDA, 0x0 },\
195fb47e43fSjhaslam 	{ "FR_fpu_exception",				0xDB, 0xF },	\
196fb47e43fSjhaslam 	{ "FR_num_brkpts_dr0",				0xDC, 0x0 },	\
197fb47e43fSjhaslam 	{ "FR_num_brkpts_dr1",				0xDD, 0x0 },	\
198fb47e43fSjhaslam 	{ "FR_num_brkpts_dr2",				0xDE, 0x0 },	\
199fb47e43fSjhaslam 	{ "FR_num_brkpts_dr3",				0xDF, 0x0 },	\
200*31725658Sksadhukh 	{ "NB_mem_ctrlr_bypass_counter_saturation",	0xE4, 0xF }
201*31725658Sksadhukh 
202*31725658Sksadhukh #define	OPT_events							\
203*31725658Sksadhukh 	{ "LS_locked_operation",			0x24, 0x7 },	\
204*31725658Sksadhukh 	{ "DC_copyback",				0x44, 0x1F },	\
205*31725658Sksadhukh 	{ "DC_dtlb_L1_miss_L2_hit",			0x45, 0x0 },	\
206*31725658Sksadhukh 	{ "DC_dtlb_L1_miss_L2_miss",			0x46, 0x0 },	\
207*31725658Sksadhukh 	{ "DC_1bit_ecc_error_found",			0x4A, 0x3 },	\
208*31725658Sksadhukh 	{ "BU_system_read_responses",			0x6C, 0x7 },	\
209*31725658Sksadhukh 	{ "BU_quadwords_written_to_system",		0x6D, 0x1 },	\
210*31725658Sksadhukh 	{ "BU_internal_L2_req",				0x7D, 0x1F },	\
211*31725658Sksadhukh 	{ "BU_fill_req_missed_L2",			0x7E, 0x7 },	\
212*31725658Sksadhukh 	{ "BU_fill_into_L2",				0x7F, 0x1 },	\
213*31725658Sksadhukh 	{ "IC_itlb_L1_miss_L2_miss",			0x85, 0x0 },	\
214*31725658Sksadhukh 	{ "FR_retired_fpu_instr",			0xCB, 0xF },	\
215fb47e43fSjhaslam 	{ "NB_mem_ctrlr_page_access",			0xE0, 0x7 },	\
216fb47e43fSjhaslam 	{ "NB_mem_ctrlr_page_table_overflow",		0xE1, 0x0 },	\
217fb47e43fSjhaslam 	{ "NB_mem_ctrlr_turnaround",			0xE3, 0x7 },	\
218fb47e43fSjhaslam 	{ "NB_ECC_errors",				0xE8, 0x80},	\
219fb47e43fSjhaslam 	{ "NB_sized_commands",				0xEB, 0x7F },	\
220fb47e43fSjhaslam 	{ "NB_probe_result",				0xEC, 0x7F},	\
221fb47e43fSjhaslam 	{ "NB_gart_events",				0xEE, 0x7 },	\
222fb47e43fSjhaslam 	{ "NB_ht_bus0_bandwidth",			0xF6, 0xF },	\
223fb47e43fSjhaslam 	{ "NB_ht_bus1_bandwidth",			0xF7, 0xF },	\
224fb47e43fSjhaslam 	{ "NB_ht_bus2_bandwidth",			0xF8, 0xF }
225fb47e43fSjhaslam 
226fb47e43fSjhaslam #define	OPT_RevD_events							\
227fb47e43fSjhaslam 	{ "NB_sized_blocks",				0xE5, 0x3C }
228fb47e43fSjhaslam 
229fb47e43fSjhaslam #define	OPT_RevE_events							\
230fb47e43fSjhaslam 	{ "NB_cpu_io_to_mem_io",			0xE9, 0xFF},	\
231fb47e43fSjhaslam 	{ "NB_cache_block_commands",			0xEA, 0x3D}
232fb47e43fSjhaslam 
233*31725658Sksadhukh #define	AMD_FAMILY_10h_cmn_events					\
234*31725658Sksadhukh 	{ "FP_retired_sse_ops",				0x3,   0x7F},	\
235*31725658Sksadhukh 	{ "FP_retired_move_ops",			0x4,   0xF},	\
236*31725658Sksadhukh 	{ "FP_retired_serialize_ops",			0x5,   0xF},	\
237*31725658Sksadhukh 	{ "FP_serialize_ops_cycles",			0x6,   0x3},	\
238*31725658Sksadhukh 	{ "DC_copyback",				0x44,  0x7F },	\
239*31725658Sksadhukh 	{ "DC_dtlb_L1_miss_L2_hit",			0x45,  0x3 },	\
240*31725658Sksadhukh 	{ "DC_dtlb_L1_miss_L2_miss",			0x46,  0x7 },	\
241*31725658Sksadhukh 	{ "DC_1bit_ecc_error_found",			0x4A,  0xF },	\
242*31725658Sksadhukh 	{ "DC_dtlb_L1_hit",				0x4D,  0x7 },	\
243*31725658Sksadhukh 	{ "BU_system_read_responses",			0x6C,  0x17 },	\
244*31725658Sksadhukh 	{ "BU_octwords_written_to_system",		0x6D,  0x1 },	\
245*31725658Sksadhukh 	{ "BU_internal_L2_req",				0x7D,  0x3F },	\
246*31725658Sksadhukh 	{ "BU_fill_req_missed_L2",			0x7E,  0xF },	\
247*31725658Sksadhukh 	{ "BU_fill_into_L2",				0x7F,  0x3 },	\
248*31725658Sksadhukh 	{ "IC_itlb_L1_miss_L2_miss",			0x85,  0x3 },	\
249*31725658Sksadhukh 	{ "IC_eviction",				0x8B,  0x0 },	\
250*31725658Sksadhukh 	{ "IC_cache_lines_invalidate",			0x8C,  0xF },	\
251*31725658Sksadhukh 	{ "IC_itlb_reload",				0x99,  0x0 },	\
252*31725658Sksadhukh 	{ "IC_itlb_reload_aborted",			0x9A,  0x0 },	\
253*31725658Sksadhukh 	{ "FR_retired_mmx_sse_fp_instr",		0xCB,  0x7 },	\
254*31725658Sksadhukh 	{ "NB_mem_ctrlr_page_access",			0xE0,  0xFF },	\
255*31725658Sksadhukh 	{ "NB_mem_ctrlr_page_table_overflow",		0xE1,  0x3 },	\
256*31725658Sksadhukh 	{ "NB_mem_ctrlr_turnaround",			0xE3,  0x3F },	\
257*31725658Sksadhukh 	{ "NB_thermal_status",				0xE8,  0x7C},	\
258*31725658Sksadhukh 	{ "NB_sized_commands",				0xEB,  0x3F },	\
259*31725658Sksadhukh 	{ "NB_probe_results_upstream_req",		0xEC,  0xFF},	\
260*31725658Sksadhukh 	{ "NB_gart_events",				0xEE,  0xFF },	\
261*31725658Sksadhukh 	{ "NB_ht_bus0_bandwidth",			0xF6,  0xBF },	\
262*31725658Sksadhukh 	{ "NB_ht_bus1_bandwidth",			0xF7,  0xBF },	\
263*31725658Sksadhukh 	{ "NB_ht_bus2_bandwidth",			0xF8,  0xBF },	\
264*31725658Sksadhukh 	{ "NB_ht_bus3_bandwidth",			0x1F9, 0xBF },	\
265*31725658Sksadhukh 	{ "LS_locked_operation",			0x24,  0xF },	\
266*31725658Sksadhukh 	{ "LS_cancelled_store_to_load_fwd_ops",		0x2A,  0x7 },	\
267*31725658Sksadhukh 	{ "LS_smi_received",				0x2B,  0x0 },	\
268*31725658Sksadhukh 	{ "LS_ineffective_prefetch",			0x52,  0x9 },	\
269*31725658Sksadhukh 	{ "LS_global_tlb_flush",			0x54,  0x0 },	\
270*31725658Sksadhukh 	{ "NB_mem_ctrlr_dram_cmd_slots_missed",		0xE2,  0x3 },	\
271*31725658Sksadhukh 	{ "NB_mem_ctrlr_req",				0x1F0, 0xFF },	\
272*31725658Sksadhukh 	{ "CB_cpu_to_dram_req_to_target",		0x1E0, 0xFF },	\
273*31725658Sksadhukh 	{ "CB_io_to_dram_req_to_target",		0x1E1, 0xFF },	\
274*31725658Sksadhukh 	{ "CB_cpu_read_cmd_latency_to_target_0_to_3",	0x1E2, 0xFF },	\
275*31725658Sksadhukh 	{ "CB_cpu_read_cmd_req_to_target_0_to_3",	0x1E3, 0xFF },	\
276*31725658Sksadhukh 	{ "CB_cpu_read_cmd_latency_to_target_4_to_7",	0x1E4, 0xFF },	\
277*31725658Sksadhukh 	{ "CB_cpu_read_cmd_req_to_target_4_to_7",	0x1E5, 0xFF },	\
278*31725658Sksadhukh 	{ "CB_cpu_cmd_latency_to_target_0_to_7",	0x1E6, 0xFF },	\
279*31725658Sksadhukh 	{ "CB_cpu_req_to_target_0_to_7",		0x1E7, 0xFF },	\
280*31725658Sksadhukh 	{ "L3_read_req",				0x4E0, 0xF7 },	\
281*31725658Sksadhukh 	{ "L3_miss",					0x4E1, 0xF7 },	\
282*31725658Sksadhukh 	{ "L3_l2_eviction_l3_fill",			0x4E2, 0xFF },	\
283*31725658Sksadhukh 	{ "L3_eviction",				0x4E3, 0xF  }
284*31725658Sksadhukh 
285*31725658Sksadhukh static amd_event_t opt_events[] = {
286*31725658Sksadhukh 	AMD_cmn_events,
287*31725658Sksadhukh 	OPT_events,
288fb47e43fSjhaslam 	EV_END
289fb47e43fSjhaslam };
290fb47e43fSjhaslam 
291*31725658Sksadhukh static amd_event_t opt_events_rev_D[] = {
292*31725658Sksadhukh 	AMD_cmn_events,
293*31725658Sksadhukh 	OPT_events,
294fb47e43fSjhaslam 	OPT_RevD_events,
295fb47e43fSjhaslam 	EV_END
296fb47e43fSjhaslam };
297fb47e43fSjhaslam 
298*31725658Sksadhukh static amd_event_t opt_events_rev_E[] = {
299*31725658Sksadhukh 	AMD_cmn_events,
300*31725658Sksadhukh 	OPT_events,
301fb47e43fSjhaslam 	OPT_RevD_events,
302fb47e43fSjhaslam 	OPT_RevE_events,
3037c478bd9Sstevel@tonic-gate 	EV_END
3047c478bd9Sstevel@tonic-gate };
3057c478bd9Sstevel@tonic-gate 
306*31725658Sksadhukh static amd_event_t family_10h_events[] = {
307*31725658Sksadhukh 	AMD_cmn_events,
308*31725658Sksadhukh 	OPT_RevE_events,
309*31725658Sksadhukh 	AMD_FAMILY_10h_cmn_events,
310*31725658Sksadhukh 	EV_END
311*31725658Sksadhukh };
312*31725658Sksadhukh 
3137c478bd9Sstevel@tonic-gate static char	*evlist;
3147c478bd9Sstevel@tonic-gate static size_t	evlist_sz;
315*31725658Sksadhukh static amd_event_t *amd_events = NULL;
316*31725658Sksadhukh static uint_t amd_family;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate #define	BITS(v, u, l)   \
3197c478bd9Sstevel@tonic-gate 	(((v) >> (l)) & ((1 << (1 + (u) - (l))) - 1))
3207c478bd9Sstevel@tonic-gate 
321*31725658Sksadhukh #define	OPTERON_FAMILY	0xf
322*31725658Sksadhukh #define	AMD_FAMILY_10H	0x10
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate static int
3257c478bd9Sstevel@tonic-gate opt_pcbe_init(void)
3267c478bd9Sstevel@tonic-gate {
327*31725658Sksadhukh 	amd_event_t		*evp;
328fb47e43fSjhaslam 	uint32_t		rev;
3297c478bd9Sstevel@tonic-gate 
330*31725658Sksadhukh 	amd_family = cpuid_getfamily(CPU);
331*31725658Sksadhukh 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Make sure this really _is_ an Opteron or Athlon 64 system. The kernel
3347c478bd9Sstevel@tonic-gate 	 * loads this module based on its name in the module directory, but it
3357c478bd9Sstevel@tonic-gate 	 * could have been renamed.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	if (cpuid_getvendor(CPU) != X86_VENDOR_AMD ||
338*31725658Sksadhukh 	    (amd_family != OPTERON_FAMILY && amd_family != AMD_FAMILY_10H))
3397c478bd9Sstevel@tonic-gate 		return (-1);
3407c478bd9Sstevel@tonic-gate 
341fb47e43fSjhaslam 	/*
342fb47e43fSjhaslam 	 * Figure out processor revision here and assign appropriate
343fb47e43fSjhaslam 	 * event configuration.
344fb47e43fSjhaslam 	 */
345fb47e43fSjhaslam 
346fb47e43fSjhaslam 	rev = cpuid_getchiprev(CPU);
347fb47e43fSjhaslam 
348*31725658Sksadhukh 	if (amd_family == OPTERON_FAMILY) {
349*31725658Sksadhukh 		if (!X86_CHIPREV_ATLEAST(rev, X86_CHIPREV_AMD_F_REV_D)) {
350*31725658Sksadhukh 			amd_events = opt_events;
351*31725658Sksadhukh 		} else if X86_CHIPREV_MATCH(rev, X86_CHIPREV_AMD_F_REV_D) {
352*31725658Sksadhukh 			amd_events = opt_events_rev_D;
353*31725658Sksadhukh 		} else if (X86_CHIPREV_MATCH(rev, X86_CHIPREV_AMD_F_REV_E) ||
354*31725658Sksadhukh 		    X86_CHIPREV_MATCH(rev, X86_CHIPREV_AMD_F_REV_F) ||
355*31725658Sksadhukh 		    X86_CHIPREV_MATCH(rev, X86_CHIPREV_AMD_F_REV_G)) {
356*31725658Sksadhukh 			amd_events = opt_events_rev_E;
357*31725658Sksadhukh 		} else {
358*31725658Sksadhukh 			amd_events = opt_events;
359*31725658Sksadhukh 		}
360*31725658Sksadhukh 	} else {
361*31725658Sksadhukh 		amd_events = family_10h_events;
362*31725658Sksadhukh 	}
363fb47e43fSjhaslam 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * Construct event list.
3667c478bd9Sstevel@tonic-gate 	 *
3677c478bd9Sstevel@tonic-gate 	 * First pass:  Calculate size needed. We'll need an additional byte
3687c478bd9Sstevel@tonic-gate 	 *		for the NULL pointer during the last strcat.
3697c478bd9Sstevel@tonic-gate 	 *
3707c478bd9Sstevel@tonic-gate 	 * Second pass: Copy strings.
3717c478bd9Sstevel@tonic-gate 	 */
372*31725658Sksadhukh 	for (evp = amd_events; evp->name != NULL; evp++)
3737c478bd9Sstevel@tonic-gate 		evlist_sz += strlen(evp->name) + 1;
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	evlist = kmem_alloc(evlist_sz + 1, KM_SLEEP);
3767c478bd9Sstevel@tonic-gate 	evlist[0] = '\0';
3777c478bd9Sstevel@tonic-gate 
378*31725658Sksadhukh 	for (evp = amd_events; evp->name != NULL; evp++) {
3797c478bd9Sstevel@tonic-gate 		(void) strcat(evlist, evp->name);
3807c478bd9Sstevel@tonic-gate 		(void) strcat(evlist, ",");
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate 	/*
3837c478bd9Sstevel@tonic-gate 	 * Remove trailing comma.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 	evlist[evlist_sz - 1] = '\0';
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	return (0);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate static uint_t
3917c478bd9Sstevel@tonic-gate opt_pcbe_ncounters(void)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	return (4);
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate static const char *
3977c478bd9Sstevel@tonic-gate opt_pcbe_impl_name(void)
3987c478bd9Sstevel@tonic-gate {
399*31725658Sksadhukh 	if (amd_family == OPTERON_FAMILY) {
400*31725658Sksadhukh 		return ("AMD Opteron & Athlon64");
401*31725658Sksadhukh 	} else if (amd_family == AMD_FAMILY_10H) {
402*31725658Sksadhukh 		return ("AMD Family 10h");
403*31725658Sksadhukh 	} else {
404*31725658Sksadhukh 		return ("Unknown AMD processor");
405*31725658Sksadhukh 	}
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate static const char *
4097c478bd9Sstevel@tonic-gate opt_pcbe_cpuref(void)
4107c478bd9Sstevel@tonic-gate {
411*31725658Sksadhukh 	if (amd_family == OPTERON_FAMILY) {
412*31725658Sksadhukh 		return ("See Chapter 10 of the \"BIOS and Kernel Developer's"
413*31725658Sksadhukh 		" Guide for the AMD Athlon 64 and AMD Opteron Processors,\" "
414*31725658Sksadhukh 		"AMD publication #26094");
415*31725658Sksadhukh 	} else if (amd_family == AMD_FAMILY_10H) {
416*31725658Sksadhukh 		return ("See section 3.15 of the \"BIOS and Kernel "
417*31725658Sksadhukh 		"Developer's Guide (BKDG) For AMD Family 10h Processors,\" "
418*31725658Sksadhukh 		"AMD publication #31116");
419*31725658Sksadhukh 	} else {
420*31725658Sksadhukh 		return ("Unknown AMD processor");
421*31725658Sksadhukh 	}
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4257c478bd9Sstevel@tonic-gate static char *
4267c478bd9Sstevel@tonic-gate opt_pcbe_list_events(uint_t picnum)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	return (evlist);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate static char *
4327c478bd9Sstevel@tonic-gate opt_pcbe_list_attrs(void)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	return ("edge,pc,inv,cmask,umask");
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4387c478bd9Sstevel@tonic-gate static uint64_t
4397c478bd9Sstevel@tonic-gate opt_pcbe_event_coverage(char *event)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	/*
4427c478bd9Sstevel@tonic-gate 	 * Fortunately, all counters can count all events.
4437c478bd9Sstevel@tonic-gate 	 */
4447c478bd9Sstevel@tonic-gate 	return (0xF);
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate static uint64_t
4487c478bd9Sstevel@tonic-gate opt_pcbe_overflow_bitmap(void)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	/*
4517c478bd9Sstevel@tonic-gate 	 * Unfortunately, this chip cannot detect which counter overflowed, so
4527c478bd9Sstevel@tonic-gate 	 * we must act as if they all did.
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	return (0xF);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
457*31725658Sksadhukh static amd_event_t *
4587c478bd9Sstevel@tonic-gate find_event(char *name)
4597c478bd9Sstevel@tonic-gate {
460*31725658Sksadhukh 	amd_event_t	*evp;
4617c478bd9Sstevel@tonic-gate 
462*31725658Sksadhukh 	for (evp = amd_events; evp->name != NULL; evp++)
4637c478bd9Sstevel@tonic-gate 		if (strcmp(name, evp->name) == 0)
4647c478bd9Sstevel@tonic-gate 			return (evp);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	return (NULL);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4707c478bd9Sstevel@tonic-gate static int
4717c478bd9Sstevel@tonic-gate opt_pcbe_configure(uint_t picnum, char *event, uint64_t preset, uint32_t flags,
4727c478bd9Sstevel@tonic-gate     uint_t nattrs, kcpc_attr_t *attrs, void **data, void *token)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	opt_pcbe_config_t	*cfg;
475*31725658Sksadhukh 	amd_event_t		*evp;
476*31725658Sksadhukh 	amd_event_t		ev_raw = { "raw", 0, 0xFF };
4777c478bd9Sstevel@tonic-gate 	int			i;
478*31725658Sksadhukh 	uint64_t		evsel = 0, evsel_tmp = 0;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * If we've been handed an existing configuration, we need only preset
4827c478bd9Sstevel@tonic-gate 	 * the counter value.
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	if (*data != NULL) {
4857c478bd9Sstevel@tonic-gate 		cfg = *data;
4867c478bd9Sstevel@tonic-gate 		cfg->opt_rawpic = preset & MASK48;
4877c478bd9Sstevel@tonic-gate 		return (0);
4887c478bd9Sstevel@tonic-gate 	}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if (picnum >= 4)
4917c478bd9Sstevel@tonic-gate 		return (CPC_INVALID_PICNUM);
4927c478bd9Sstevel@tonic-gate 
4935d3a5ad8Srab 	if ((evp = find_event(event)) == NULL) {
4945d3a5ad8Srab 		long tmp;
4955d3a5ad8Srab 
4965d3a5ad8Srab 		/*
4975d3a5ad8Srab 		 * If ddi_strtol() likes this event, use it as a raw event code.
4985d3a5ad8Srab 		 */
4995d3a5ad8Srab 		if (ddi_strtol(event, NULL, 0, &tmp) != 0)
5005d3a5ad8Srab 			return (CPC_INVALID_EVENT);
5015d3a5ad8Srab 
5025d3a5ad8Srab 		ev_raw.emask = tmp;
5035d3a5ad8Srab 		evp = &ev_raw;
5045d3a5ad8Srab 	}
5057c478bd9Sstevel@tonic-gate 
506*31725658Sksadhukh 	/*
507*31725658Sksadhukh 	 * Configuration of EventSelect register for family 10h processors.
508*31725658Sksadhukh 	 */
509*31725658Sksadhukh 	if (amd_family == AMD_FAMILY_10H) {
510*31725658Sksadhukh 
511*31725658Sksadhukh 		/* Set GuestOnly bit to 0 and HostOnly bit to 1 */
512*31725658Sksadhukh 		evsel &= ~OPT_PES_HOST;
513*31725658Sksadhukh 		evsel &= ~OPT_PES_GUEST;
514*31725658Sksadhukh 
515*31725658Sksadhukh 		/* Set bits [35:32] for extended part of Event Select field */
516*31725658Sksadhukh 		evsel_tmp = evp->emask & 0x0f00;
517*31725658Sksadhukh 		evsel |= evsel_tmp << 24;
518*31725658Sksadhukh 	}
519*31725658Sksadhukh 
520*31725658Sksadhukh 	evsel |= evp->emask & 0x00ff;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (flags & CPC_COUNT_USER)
5237c478bd9Sstevel@tonic-gate 		evsel |= OPT_PES_USR;
5247c478bd9Sstevel@tonic-gate 	if (flags & CPC_COUNT_SYSTEM)
5257c478bd9Sstevel@tonic-gate 		evsel |= OPT_PES_OS;
5267c478bd9Sstevel@tonic-gate 	if (flags & CPC_OVF_NOTIFY_EMT)
5277c478bd9Sstevel@tonic-gate 		evsel |= OPT_PES_INT;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	for (i = 0; i < nattrs; i++) {
5307c478bd9Sstevel@tonic-gate 		if (strcmp(attrs[i].ka_name, "edge") == 0) {
5317c478bd9Sstevel@tonic-gate 			if (attrs[i].ka_val != 0)
5327c478bd9Sstevel@tonic-gate 				evsel |= OPT_PES_EDGE;
5337c478bd9Sstevel@tonic-gate 		} else if (strcmp(attrs[i].ka_name, "pc") == 0) {
5347c478bd9Sstevel@tonic-gate 			if (attrs[i].ka_val != 0)
5357c478bd9Sstevel@tonic-gate 				evsel |= OPT_PES_PC;
5367c478bd9Sstevel@tonic-gate 		} else if (strcmp(attrs[i].ka_name, "inv") == 0) {
5377c478bd9Sstevel@tonic-gate 			if (attrs[i].ka_val != 0)
5387c478bd9Sstevel@tonic-gate 				evsel |= OPT_PES_INV;
5397c478bd9Sstevel@tonic-gate 		} else if (strcmp(attrs[i].ka_name, "cmask") == 0) {
5407c478bd9Sstevel@tonic-gate 			if ((attrs[i].ka_val | OPT_PES_CMASK_MASK) !=
5417c478bd9Sstevel@tonic-gate 			    OPT_PES_CMASK_MASK)
5427c478bd9Sstevel@tonic-gate 				return (CPC_ATTRIBUTE_OUT_OF_RANGE);
5437c478bd9Sstevel@tonic-gate 			evsel |= attrs[i].ka_val << OPT_PES_CMASK_SHIFT;
5447c478bd9Sstevel@tonic-gate 		} else if (strcmp(attrs[i].ka_name, "umask") == 0) {
5457c478bd9Sstevel@tonic-gate 			if ((attrs[i].ka_val | evp->umask_valid) !=
5467c478bd9Sstevel@tonic-gate 			    evp->umask_valid)
5477c478bd9Sstevel@tonic-gate 				return (CPC_ATTRIBUTE_OUT_OF_RANGE);
5487c478bd9Sstevel@tonic-gate 			evsel |= attrs[i].ka_val << OPT_PES_UMASK_SHIFT;
5497c478bd9Sstevel@tonic-gate 		} else
5507c478bd9Sstevel@tonic-gate 			return (CPC_INVALID_ATTRIBUTE);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	cfg = kmem_alloc(sizeof (*cfg), KM_SLEEP);
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	cfg->opt_picno = picnum;
5567c478bd9Sstevel@tonic-gate 	cfg->opt_evsel = evsel;
5577c478bd9Sstevel@tonic-gate 	cfg->opt_rawpic = preset & MASK48;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	*data = cfg;
5607c478bd9Sstevel@tonic-gate 	return (0);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate static void
5647c478bd9Sstevel@tonic-gate opt_pcbe_program(void *token)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	opt_pcbe_config_t	*cfgs[4] = { &nullcfgs[0], &nullcfgs[1],
5677c478bd9Sstevel@tonic-gate 						&nullcfgs[2], &nullcfgs[3] };
5687c478bd9Sstevel@tonic-gate 	opt_pcbe_config_t	*pcfg = NULL;
5697c478bd9Sstevel@tonic-gate 	int			i;
570843e1988Sjohnlev 	ulong_t			curcr4 = getcr4();
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	/*
5737c478bd9Sstevel@tonic-gate 	 * Allow nonprivileged code to read the performance counters if desired.
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 	if (kcpc_allow_nonpriv(token))
5767c478bd9Sstevel@tonic-gate 		setcr4(curcr4 | CR4_PCE);
5777c478bd9Sstevel@tonic-gate 	else
5787c478bd9Sstevel@tonic-gate 		setcr4(curcr4 & ~CR4_PCE);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
5817c478bd9Sstevel@tonic-gate 	 * Query kernel for all configs which will be co-programmed.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	do {
5847c478bd9Sstevel@tonic-gate 		pcfg = (opt_pcbe_config_t *)kcpc_next_config(token, pcfg, NULL);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		if (pcfg != NULL) {
5877c478bd9Sstevel@tonic-gate 			ASSERT(pcfg->opt_picno < 4);
5887c478bd9Sstevel@tonic-gate 			cfgs[pcfg->opt_picno] = pcfg;
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 	} while (pcfg != NULL);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * Program in two loops. The first configures and presets the counter,
5947c478bd9Sstevel@tonic-gate 	 * and the second loop enables the counters. This ensures that the
5957c478bd9Sstevel@tonic-gate 	 * counters are all enabled as closely together in time as possible.
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
5990ac7d7d8Skucharsk 		wrmsr(PES_BASE_ADDR + i, cfgs[i]->opt_evsel);
6000ac7d7d8Skucharsk 		wrmsr(PIC_BASE_ADDR + i, cfgs[i]->opt_rawpic);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
6040ac7d7d8Skucharsk 		wrmsr(PES_BASE_ADDR + i, cfgs[i]->opt_evsel |
6050ac7d7d8Skucharsk 		    (uint64_t)(uintptr_t)OPT_PES_ENABLE);
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate static void
6107c478bd9Sstevel@tonic-gate opt_pcbe_allstop(void)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	int		i;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
6150ac7d7d8Skucharsk 		wrmsr(PES_BASE_ADDR + i, 0ULL);
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/*
6187c478bd9Sstevel@tonic-gate 	 * Disable non-privileged access to the counter registers.
6197c478bd9Sstevel@tonic-gate 	 */
620843e1988Sjohnlev 	setcr4(getcr4() & ~CR4_PCE);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate static void
6247c478bd9Sstevel@tonic-gate opt_pcbe_sample(void *token)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	opt_pcbe_config_t	*cfgs[4] = { NULL, NULL, NULL, NULL };
6277c478bd9Sstevel@tonic-gate 	opt_pcbe_config_t	*pcfg = NULL;
6287c478bd9Sstevel@tonic-gate 	int			i;
6297c478bd9Sstevel@tonic-gate 	uint64_t		curpic[4];
6307c478bd9Sstevel@tonic-gate 	uint64_t		*addrs[4];
6317c478bd9Sstevel@tonic-gate 	uint64_t		*tmp;
6327c478bd9Sstevel@tonic-gate 	int64_t			diff;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++)
63593d449f8Skucharsk 		curpic[i] = rdmsr(PIC_BASE_ADDR + i);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * Query kernel for all configs which are co-programmed.
6397c478bd9Sstevel@tonic-gate 	 */
6407c478bd9Sstevel@tonic-gate 	do {
6417c478bd9Sstevel@tonic-gate 		pcfg = (opt_pcbe_config_t *)kcpc_next_config(token, pcfg, &tmp);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		if (pcfg != NULL) {
6447c478bd9Sstevel@tonic-gate 			ASSERT(pcfg->opt_picno < 4);
6457c478bd9Sstevel@tonic-gate 			cfgs[pcfg->opt_picno] = pcfg;
6467c478bd9Sstevel@tonic-gate 			addrs[pcfg->opt_picno] = tmp;
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 	} while (pcfg != NULL);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
6517c478bd9Sstevel@tonic-gate 		if (cfgs[i] == NULL)
6527c478bd9Sstevel@tonic-gate 			continue;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		diff = (curpic[i] - cfgs[i]->opt_rawpic) & MASK48;
6557c478bd9Sstevel@tonic-gate 		*addrs[i] += diff;
6567c478bd9Sstevel@tonic-gate 		DTRACE_PROBE4(opt__pcbe__sample, int, i, uint64_t, *addrs[i],
6577c478bd9Sstevel@tonic-gate 		    uint64_t, curpic[i], uint64_t, cfgs[i]->opt_rawpic);
6587c478bd9Sstevel@tonic-gate 		cfgs[i]->opt_rawpic = *addrs[i] & MASK48;
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate static void
6637c478bd9Sstevel@tonic-gate opt_pcbe_free(void *config)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	kmem_free(config, sizeof (opt_pcbe_config_t));
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate static struct modlpcbe modlpcbe = {
6707c478bd9Sstevel@tonic-gate 	&mod_pcbeops,
6717c478bd9Sstevel@tonic-gate 	"AMD Performance Counters v%I%",
6727c478bd9Sstevel@tonic-gate 	&opt_pcbe_ops
6737c478bd9Sstevel@tonic-gate };
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate static struct modlinkage modl = {
6767c478bd9Sstevel@tonic-gate 	MODREV_1,
6777c478bd9Sstevel@tonic-gate 	&modlpcbe,
6787c478bd9Sstevel@tonic-gate };
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate int
6817c478bd9Sstevel@tonic-gate _init(void)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate 	int ret;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	if (opt_pcbe_init() != 0)
6867c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if ((ret = mod_install(&modl)) != 0)
6897c478bd9Sstevel@tonic-gate 		kmem_free(evlist, evlist_sz + 1);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	return (ret);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate int
6957c478bd9Sstevel@tonic-gate _fini(void)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate 	int ret;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if ((ret = mod_remove(&modl)) == 0)
7007c478bd9Sstevel@tonic-gate 		kmem_free(evlist, evlist_sz + 1);
7017c478bd9Sstevel@tonic-gate 	return (ret);
7027c478bd9Sstevel@tonic-gate }
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate int
7057c478bd9Sstevel@tonic-gate _info(struct modinfo *mi)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	return (mod_info(&modl, mi));
7087c478bd9Sstevel@tonic-gate }
709