Home | History | Annotate | Line # | Download | only in arch
tprof_x86.c revision 1.14
      1 /*	$NetBSD: tprof_x86.c,v 1.14 2022/12/08 02:12:18 msaitoh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2018-2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Maxime Villard.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <stdbool.h>
     36 #include <string.h>
     37 #include <unistd.h>
     38 #include <err.h>
     39 #include <machine/specialreg.h>
     40 #include <dev/tprof/tprof_ioctl.h>
     41 #include "../tprof.h"
     42 
     43 int tprof_event_init(uint32_t);
     44 void tprof_event_list(void);
     45 void tprof_event_lookup(const char *, struct tprof_param *);
     46 
     47 struct name_to_event {
     48 	const char *name;
     49 	uint64_t event;
     50 	uint64_t unit;
     51 	bool enabled;
     52 };
     53 
     54 struct event_table {
     55 	const char *tablename;
     56 	struct name_to_event *names;
     57 	size_t nevents;
     58 	struct event_table *next;
     59 };
     60 
     61 static struct event_table *cpuevents = NULL;
     62 
     63 static void x86_cpuid(unsigned int *eax, unsigned int *ebx,
     64     unsigned int *ecx, unsigned int *edx)
     65 {
     66 	asm volatile("cpuid"
     67 	    : "=a" (*eax),
     68 	      "=b" (*ebx),
     69 	      "=c" (*ecx),
     70 	      "=d" (*edx)
     71 	    : "0" (*eax), "2" (*ecx));
     72 }
     73 
     74 /* ------------------------------------------------------------------------- */
     75 
     76 /*
     77  * Intel Architectural Version 1.
     78  */
     79 static struct name_to_event intel_arch1_names[] = {
     80 	/* Event Name - Event Select - UMask */
     81 	{ "unhalted-core-cycles",	0x3c, 0x00, true },
     82 	{ "instruction-retired",	0xc0, 0x00, true },
     83 	{ "unhalted-reference-cycles",	0x3c, 0x01, true },
     84 	{ "llc-reference",		0x2e, 0x4f, true },
     85 	{ "llc-misses",			0x2e, 0x41, true },
     86 	{ "branch-instruction-retired",	0xc4, 0x00, true },
     87 	{ "branch-misses-retired",	0xc5, 0x00, true },
     88 	{ "topdown-slots",		0xa4, 0x01, true },
     89 };
     90 
     91 static struct event_table intel_arch1 = {
     92 	.tablename = "Intel Architectural Version 1",
     93 	.names = intel_arch1_names,
     94 	.nevents = sizeof(intel_arch1_names) /
     95 	    sizeof(struct name_to_event),
     96 	.next = NULL
     97 };
     98 
     99 static struct event_table *
    100 init_intel_arch1(void)
    101 {
    102 	unsigned int eax, ebx, ecx, edx, vectorlen;
    103 	struct event_table *table;
    104 	size_t i;
    105 
    106 	eax = 0x0a;
    107 	ebx = 0;
    108 	ecx = 0;
    109 	edx = 0;
    110 	x86_cpuid(&eax, &ebx, &ecx, &edx);
    111 
    112 	vectorlen = __SHIFTOUT(eax, CPUID_PERF_BVECLEN);
    113 
    114 	table = &intel_arch1;
    115 	for (i = 0; i < table->nevents; i++) {
    116 		/*
    117 		 * Disable the unsupported events from:
    118 		 * a) the bit vector length in EAX.
    119 		 * b) the disable bit in EBX.
    120 		 */
    121 		if (i >= vectorlen)
    122 			table->names[i].enabled = false;
    123 		if ((ebx & (i << 1)) != 0)
    124 			table->names[i].enabled = false;
    125 	}
    126 
    127 	return table;
    128 }
    129 
    130 /*
    131  * Intel Silvermont/Airmont.
    132  */
    133 static struct name_to_event intel_silvermont_airmont_names[] = {
    134 	{ "REHABQ.LD_BLOCK_ST_FORWARD",		0x03, 0x01, true },
    135 	{ "REHABQ.LD_BLOCK_STD_NOTREADY",	0x03, 0x02, true },
    136 	{ "REHABQ.ST_SPLITS",			0x03, 0x04, true },
    137 	{ "REHABQ.LD_SPLITS",			0x03, 0x08, true },
    138 	{ "REHABQ.LOCK",			0x03, 0x10, true },
    139 	{ "REHABQ.STA_FULL",			0x03, 0x20, true },
    140 	{ "REHABQ.ANY_LD",			0x03, 0x40, true },
    141 	{ "REHABQ.ANY_ST",			0x03, 0x80, true },
    142 	{ "MEM_UOPS_RETIRED.L1_MISS_LOADS",	0x04, 0x01, true },
    143 	{ "MEM_UOPS_RETIRED.L2_HIT_LOADS",	0x04, 0x02, true },
    144 	{ "MEM_UOPS_RETIRED.L2_MISS_LOADS",	0x04, 0x04, true },
    145 	{ "MEM_UOPS_RETIRED.DTLB_MISS_LOADS",	0x04, 0x08, true },
    146 	{ "MEM_UOPS_RETIRED.UTLB_MISS",		0x04, 0x10, true },
    147 	{ "MEM_UOPS_RETIRED.HITM",		0x04, 0x20, true },
    148 	{ "MEM_UOPS_RETIRED.ALL_LOADS",		0x04, 0x40, true },
    149 	{ "MEM_UOP_RETIRED.ALL_STORES",		0x04, 0x80, true },
    150 	{ "PAGE_WALKS.D_SIDE_CYCLES",		0x05, 0x01, true },
    151 	{ "PAGE_WALKS.I_SIDE_CYCLES",		0x05, 0x02, true },
    152 	{ "PAGE_WALKS.WALKS",			0x05, 0x03, true },
    153 	{ "LONGEST_LAT_CACHE.MISS",		0x2e, 0x41, true },
    154 	{ "LONGEST_LAT_CACHE.REFERENCE",	0x2e, 0x4f, true },
    155 	{ "L2_REJECT_XQ.ALL",			0x30, 0x00, true },
    156 	{ "CORE_REJECT_L2Q.ALL",		0x31, 0x00, true },
    157 	{ "CPU_CLK_UNHALTED.CORE_P",		0x3c, 0x00, true },
    158 	{ "CPU_CLK_UNHALTED.REF_P",		0x3c, 0x01, true },
    159 	{ "ICACHE.HIT",				0x80, 0x01, true },
    160 	{ "ICACHE.MISSES",			0x80, 0x02, true },
    161 	{ "ICACHE.ACCESSES",			0x80, 0x03, true },
    162 	{ "OFFCORE_RESPONSE_0",			0xb7, 0x01, true },
    163 	{ "OFFCORE_RESPONSE_1",			0xb7, 0x02, true },
    164 	{ "INST_RETIRED.ANY_P",			0xc0, 0x00, true },
    165 	{ "UOPS_RETIRED.MS",			0xc2, 0x01, true },
    166 	{ "UOPS_RETIRED.ALL",			0xc2, 0x10, true },
    167 	{ "MACHINE_CLEARS.SMC",			0xc3, 0x01, true },
    168 	{ "MACHINE_CLEARS.MEMORY_ORDERING",	0xc3, 0x02, true },
    169 	{ "MACHINE_CLEARS.FP_ASSIST",		0xc3, 0x04, true },
    170 	{ "MACHINE_CLEARS.ALL",			0xc3, 0x08, true },
    171 	{ "BR_INST_RETIRED.ALL_BRANCHES",	0xc4, 0x00, true },
    172 	{ "BR_INST_RETIRED.JCC",		0xc4, 0x7e, true },
    173 	{ "BR_INST_RETIRED.FAR_BRANCH",		0xc4, 0xbf, true },
    174 	{ "BR_INST_RETIRED.NON_RETURN_IND",	0xc4, 0xeb, true },
    175 	{ "BR_INST_RETIRED.RETURN",		0xc4, 0xf7, true },
    176 	{ "BR_INST_RETIRED.CALL",		0xc4, 0xf9, true },
    177 	{ "BR_INST_RETIRED.IND_CALL",		0xc4, 0xfb, true },
    178 	{ "BR_INST_RETIRED.REL_CALL",		0xc4, 0xfd, true },
    179 	{ "BR_INST_RETIRED.TAKEN_JCC",		0xc4, 0xfe, true },
    180 	{ "BR_MISP_RETIRED.ALL_BRANCHES",	0xc5, 0x00, true },
    181 	{ "BR_MISP_RETIRED.JCC",		0xc5, 0x7e, true },
    182 	{ "BR_MISP_RETIRED.FAR",		0xc5, 0xbf, true },
    183 	{ "BR_MISP_RETIRED.NON_RETURN_IND",	0xc5, 0xeb, true },
    184 	{ "BR_MISP_RETIRED.RETURN",		0xc5, 0xf7, true },
    185 	{ "BR_MISP_RETIRED.CALL",		0xc5, 0xf9, true },
    186 	{ "BR_MISP_RETIRED.IND_CALL",		0xc5, 0xfb, true },
    187 	{ "BR_MISP_RETIRED.REL_CALL",		0xc5, 0xfd, true },
    188 	{ "BR_MISP_RETIRED.TAKEN_JCC",		0xc5, 0xfe, true },
    189 	{ "NO_ALLOC_CYCLES.ROB_FULL",		0xca, 0x01, true },
    190 	{ "NO_ALLOC_CYCLES.RAT_STALL",		0xca, 0x20, true },
    191 	{ "NO_ALLOC_CYCLES.ALL",		0xca, 0x3f, true },
    192 	{ "NO_ALLOC_CYCLES.NOT_DELIVERED",	0xca, 0x50, true },
    193 	{ "RS_FULL_STALL.MEC",			0xcb, 0x01, true },
    194 	{ "RS_FULL_STALL.ALL",			0xcb, 0x1f, true },
    195 	{ "CYCLES_DIV_BUSY.ANY",		0xcd, 0x01, true },
    196 	{ "BACLEARS.ALL",			0xe6, 0x01, true },
    197 	{ "BACLEARS.RETURN",			0xe6, 0x08, true },
    198 	{ "BACLEARS.COND",			0xe6, 0x10, true },
    199 	{ "MS_DECODED.MS_ENTRY",		0xe7, 0x01, true },
    200 };
    201 
    202 static struct event_table intel_silvermont_airmont = {
    203 	.tablename = "Intel Silvermont/Airmont",
    204 	.names = intel_silvermont_airmont_names,
    205 	.nevents = sizeof(intel_silvermont_airmont_names) /
    206 	    sizeof(struct name_to_event),
    207 	.next = NULL
    208 };
    209 
    210 static struct event_table *
    211 init_intel_silvermont_airmont(void)
    212 {
    213 
    214 	return &intel_silvermont_airmont;
    215 }
    216 
    217 /*
    218  * Intel Goldmont
    219  */
    220 static struct name_to_event intel_goldmont_names[] = {
    221 	{ "LD_BLOCKS.ALL_BLOCK",			0x03,	0x10, true },
    222 	{ "LD_BLOCKS.UTLB_MISS",			0x03,	0x08, true },
    223 	{ "LD_BLOCKS.STORE_FORWARD",			0x03,	0x02, true },
    224 	{ "LD_BLOCKS.DATA_UNKNOWN",			0x03,	0x01, true },
    225 	{ "LD_BLOCKS.4K_ALIAS",				0x03,	0x04, true },
    226 	{ "PAGE_WALKS.D_SIDE_CYCLES",			0x05,	0x01, true },
    227 	{ "PAGE_WALKS.I_SIDE_CYCLES",			0x05,	0x02, true },
    228 	{ "PAGE_WALKS.CYCLES",				0x05,	0x03, true },
    229 	{ "UOPS_ISSUED.ANY",				0x0e,	0x00, true },
    230 	{ "MISALIGN_MEM_REF.LOAD_PAGE_SPLIT",		0x13,	0x02, true },
    231 	{ "MISALIGN_MEM_REF.STORE_PAGE_SPLIT",		0x13,	0x04, true },
    232 	{ "LONGEST_LAT_CACHE.REFERENCE",		0x2e,	0x4f, true },
    233 	{ "LONGEST_LAT_CACHE.MISS",			0x2e,	0x41, true },
    234 	{ "L2_REJECT_XQ.ALL",				0x30,	0x00, true },
    235 	{ "CORE_REJECT_L2Q.ALL",			0x31,	0x00, true },
    236 	{ "CPU_CLK_UNHALTED.CORE_P",			0x3c,	0x00, true },
    237 	{ "CPU_CLK_UNHALTED.REF",			0x3c,	0x01, true },
    238 	{ "DL1.DIRTY_EVICTION",				0x51,	0x01, true },
    239 	{ "ICACHE.HIT",					0x80,	0x01, true },
    240 	{ "ICACHE.MISSES",				0x80,	0x02, true },
    241 	{ "ICACHE.ACCESSES",				0x80,	0x03, true },
    242 	{ "ITLB.MISS",					0x81,	0x04, true },
    243 	{ "FETCH_STALL.ALL",				0x86,	0x00, true },
    244 	{ "FETCH_STALL.ITLB_FILL_PENDING_CYCLES",	0x86,	0x01, true },
    245 	{ "FETCH_STALL.ICACHE_FILL_PENDING_CYCLES",	0x86,	0x02, true },
    246 	{ "UOPS_NOT_DELIVERED.ANY",			0x9c,	0x00, true },
    247 	{ "OFFCORE_RESPONSE.0",				0xb7,	0x01, true },
    248 	{ "OFFCORE_RESPONSE.1",				0xb7,	0x02, true },
    249 	{ "INST_RETIRED.ANY_P",				0xc0,	0x00, true },
    250 	{ "UOPS_RETIRED.ANY",				0xc2,	0x00, true },
    251 	{ "UOPS_RETIRED.MS",				0xc2,	0x01, true },
    252 	{ "UOPS_RETIRED.FPDIV",				0xc2,	0x08, true },
    253 	{ "UOPS_RETIRED.IDIV",				0xc2,	0x10, true },
    254 	{ "MACHINE_CLEARS.SMC",				0xc3,	0x01, true },
    255 	{ "MACHINE_CLEARS.MEMORY_ORDERING",		0xc3,	0x02, true },
    256 	{ "MACHINE_CLEARS.FP_ASSIST",			0xc3,	0x04, true },
    257 	{ "MACHINE_CLEARS.DISAMBIGUATION",		0xc3,	0x08, true },
    258 	{ "MACHINE_CLEARS.ALL",				0xc3,	0x00, true },
    259 	{ "BR_INST_RETIRED.ALL_BRANCHES",		0xc4,	0x00, true },
    260 	{ "BR_INST_RETIRED.JCC",			0xc4,	0x7e, true },
    261 	{ "BR_INST_RETIRED.ALL_TAKEN_BRANCHES",		0xc4,	0x80, true },
    262 	{ "BR_INST_RETIRED.TAKEN_JCC",			0xc4,	0xfe, true },
    263 	{ "BR_INST_RETIRED.CALL",			0xc4,	0xf9, true },
    264 	{ "BR_INST_RETIRED.REL_CALL",			0xc4,	0xfd, true },
    265 	{ "BR_INST_RETIRED.IND_CALL",			0xc4,	0xfb, true },
    266 	{ "BR_INST_RETIRED.RETURN",			0xc4,	0xf7, true },
    267 	{ "BR_INST_RETIRED.NON_RETURN_IND",		0xc4,	0xeb, true },
    268 	{ "BR_INST_RETIRED.FAR_BRANCH",			0xc4,	0xbf, true },
    269 	{ "BR_MISP_RETIRED.ALL_BRANCHES",		0xc5,	0x00, true },
    270 	{ "BR_MISP_RETIRED.JCC",			0xc5,	0x7e, true },
    271 	{ "BR_MISP_RETIRED.TAKEN_JCC",			0xc5,	0xfe, true },
    272 	{ "BR_MISP_RETIRED.IND_CALL",			0xc5,	0xfb, true },
    273 	{ "BR_MISP_RETIRED.RETURN",			0xc5,	0xf7, true },
    274 	{ "BR_MISP_RETIRED.NON_RETURN_IND",		0xc5,	0xeb, true },
    275 	{ "ISSUE_SLOTS_NOT_CONSUMED.RESOURCE_FULL",	0xca,	0x01, true },
    276 	{ "ISSUE_SLOTS_NOT_CONSUMED.RECOVERY",		0xca,	0x02, true },
    277 	{ "ISSUE_SLOTS_NOT_CONSUMED.ANY",		0xca,	0x00, true },
    278 	{ "HW_INTERRUPTS.RECEIVED",			0xcb,	0x01, true },
    279 	{ "HW_INTERRUPTS.MASKED",			0xcb,	0x02, true },
    280 	{ "HW_INTERRUPTS.PENDING_AND_MASKED",		0xcb,	0x04, true },
    281 	{ "CYCLES_DIV_BUSY.ALL",			0xcd,	0x00, true },
    282 	{ "CYCLES_DIV_BUSY.IDIV",			0xcd,	0x01, true },
    283 	{ "CYCLES_DIV_BUSY.FPDIV",			0xcd,	0x02, true },
    284 	{ "MEM_UOPS_RETIRED.ALL_LOADS",			0xd0,	0x81, true },
    285 	{ "MEM_UOPS_RETIRED.ALL_STORES",		0xd0,	0x82, true },
    286 	{ "MEM_UOPS_RETIRED.ALL",			0xd0,	0x83, true },
    287 	{ "MEM_UOPS_RETIRED.DTLB_MISS_LOADS",		0xd0,	0x11, true },
    288 	{ "MEM_UOPS_RETIRED.DTLB_MISS_STORES",		0xd0,	0x12, true },
    289 	{ "MEM_UOPS_RETIRED.DTLB_MISS",			0xd0,	0x13, true },
    290 	{ "MEM_UOPS_RETIRED.LOCK_LOADS",		0xd0,	0x21, true },
    291 	{ "MEM_UOPS_RETIRED.SPLIT_LOADS",		0xd0,	0x41, true },
    292 	{ "MEM_UOPS_RETIRED.SPLIT_STORES",		0xd0,	0x42, true },
    293 	{ "MEM_UOPS_RETIRED.SPLIT",			0xd0,	0x43, true },
    294 	{ "MEM_LOAD_UOPS_RETIRED.L1_HIT",		0xd1,	0x01, true },
    295 	{ "MEM_LOAD_UOPS_RETIRED.L1_MISS",		0xd1,	0x08, true },
    296 	{ "MEM_LOAD_UOPS_RETIRED.L2_HIT",		0xd1,	0x02, true },
    297 	{ "MEM_LOAD_UOPS_RETIRED.L2_MISS",		0xd1,	0x10, true },
    298 	{ "MEM_LOAD_UOPS_RETIRED.HITM",			0xd1,	0x20, true },
    299 	{ "MEM_LOAD_UOPS_RETIRED.WCB_HIT",		0xd1,	0x40, true },
    300 	{ "MEM_LOAD_UOPS_RETIRED.DRAM_HIT",		0xd1,	0x80, true },
    301 	{ "BACLEARS.ALL",				0xe6,	0x01, true },
    302 	{ "BACLEARS.RETURN",				0xe6,	0x08, true },
    303 	{ "BACLEAR.CONDS",				0xe6,	0x10, true },
    304 	{ "MS_DECODED.MS_ENTRY",			0xe7,	0x01, true },
    305 	{ "DECODED_RESTRICTION.PREDECODE_WRONG",	0xe9,	0x01, true },
    306 };
    307 
    308 static struct event_table intel_goldmont = {
    309 	.tablename = "Intel Goldmont",
    310 	.names = intel_goldmont_names,
    311 	.nevents = sizeof(intel_goldmont_names) /
    312 	    sizeof(struct name_to_event),
    313 	.next = NULL
    314 };
    315 
    316 static struct event_table *
    317 init_intel_goldmont(void)
    318 {
    319 
    320 	return &intel_goldmont;
    321 }
    322 
    323 /*
    324  * Intel Goldmont Plus (Additions from Goldmont)
    325  */
    326 static struct name_to_event intel_goldmontplus_names[] = {
    327 	{ "INST_RETIRED.ANY",				0x00,	0x01, true },
    328 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_4K",		0x08,	0x02, true },
    329 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_2M_4M",	0x08,	0x04, true },
    330 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_1GB",	0x08,	0x08, true },
    331 	{ "DTLB_LOAD_MISSES.WALK_PENDING",		0x08,	0x10, true },
    332 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_4K",	0x49,	0x02, true },
    333 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_2M_4M",	0x49,	0x04, true },
    334 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_1GB",	0x49,	0x08, true },
    335 	{ "DTLB_STORE_MISSES.WALK_PENDING",		0x49,	0x10, true },
    336 	{ "EPT.WALK_PENDING",				0x4f,	0x10, true },
    337 	{ "ITLB_MISSES.WALK_COMPLETED_4K",		0x85,	0x08, true },
    338 	{ "ITLB_MISSES.WALK_COMPLETED_2M_4M",		0x85,	0x04, true },
    339 	{ "ITLB_MISSES.WALK_COMPLETED_1GB",		0x85,	0x08, true },
    340 	{ "ITLB_MISSES.WALK_PENDING",			0x85,	0x10, true },
    341 	{ "TLB_FLUSHES.STLB_ANY",			0xbd,	0x20, true },
    342 	{ "MACHINE_CLEARS.PAGE_FAULT",			0xc3,	0x20, true },
    343 };
    344 
    345 static struct event_table intel_goldmontplus = {
    346 	.tablename = "Intel Goldmont Plus",
    347 	.names = intel_goldmontplus_names,
    348 	.nevents = sizeof(intel_goldmontplus_names) /
    349 	    sizeof(struct name_to_event),
    350 	.next = NULL
    351 };
    352 
    353 static struct event_table *
    354 init_intel_goldmontplus(void)
    355 {
    356 
    357 	intel_goldmont.next = &intel_goldmontplus;
    358 
    359 	return &intel_goldmont;
    360 }
    361 
    362 /*
    363  * Intel Skylake/Kabylake.
    364  *
    365  * The events that are not listed, because they are of little interest or
    366  * require extra configuration:
    367  *     TX_*
    368  *     FRONTEND_RETIRED.*
    369  *     FP_ARITH_INST_RETIRED.*
    370  *     HLE_RETIRED.*
    371  *     RTM_RETIRED.*
    372  *     MEM_TRANS_RETIRED.*
    373  *     UOPS_DISPATCHED_PORT.*
    374  */
    375 static struct name_to_event intel_skylake_kabylake_names[] = {
    376 	/* Event Name - Event Select - UMask */
    377 	{ "LD_BLOCKS.STORE_FORWARD",			0x03, 0x02, true },
    378 	{ "LD_BLOCKS.NO_SR",				0x03, 0x08, true },
    379 	{ "LD_BLOCKS_PARTIAL.ADDRESS_ALIAS",		0x07, 0x01, true },
    380 	{ "DTLB_LOAD_MISSES.MISS_CAUSES_A_WALK",	0x08, 0x01, true },
    381 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_4K",		0x08, 0x02, true },
    382 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_2M_4M",	0x08, 0x04, true },
    383 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED_1G",		0x08, 0x08, true },
    384 	{ "DTLB_LOAD_MISSES.WALK_COMPLETED",		0x08, 0x0e, true },
    385 	{ "DTLB_LOAD_MISSES.WALK_PENDING",		0x08, 0x10, true },
    386 	{ "DTLB_LOAD_MISSES.STLB_HIT",			0x08, 0x20, true },
    387 	{ "INT_MISC.RECOVERY_CYCLES",			0x0d, 0x01, true },
    388 	{ "INT_MISC.CLEAR_RESTEER_CYCLES",		0x0d, 0x80, true },
    389 	{ "UOPS_ISSUED.ANY",				0x0e, 0x01, true },
    390 	{ "UOPS_ISSUED.VECTOR_WIDTH_MISMATCH",		0x0e, 0x02, true },
    391 	{ "UOPS_ISSUED.SLOW_LEA",			0x0e, 0x20, true },
    392 	{ "L2_RQSTS.DEMAND_DATA_RD_MISS",		0x24, 0x21, true },
    393 	{ "L2_RQSTS.RFO_MISS",				0x24, 0x22, true },
    394 	{ "L2_RQSTS.CODE_RD_MISS",			0x24, 0x24, true },
    395 	{ "L2_RQSTS.ALL_DEMAND_MISS",			0x24, 0x27, true },
    396 	{ "L2_RQSTS.PF_MISS",				0x24, 0x38, true },
    397 	{ "L2_RQSTS.MISS",				0x24, 0x3f, true },
    398 	{ "L2_RQSTS.DEMAND_DATA_RD_HIT",		0x24, 0x41, true },
    399 	{ "L2_RQSTS.RFO_HIT",				0x24, 0x42, true },
    400 	{ "L2_RQSTS.CODE_RD_HIT",			0x24, 0x44, true },
    401 	{ "L2_RQSTS.PF_HIT",				0x24, 0xd8, true },
    402 	{ "L2_RQSTS.ALL_DEMAND_DATA_RD",		0x24, 0xe1, true },
    403 	{ "L2_RQSTS.ALL_RFO",				0x24, 0xe2, true },
    404 	{ "L2_RQSTS.ALL_CODE_RD",			0x24, 0xe4, true },
    405 	{ "L2_RQSTS.ALL_DEMAND_REFERENCES",		0x24, 0xe7, true },
    406 	{ "L2_RQSTS.ALL_PF",				0x24, 0xf8, true },
    407 	{ "L2_RQSTS.REFERENCES",			0x24, 0xff, true },
    408 	{ "SW_PREFETCH_ACCESS.NTA",			0x32, 0x01, true },
    409 	{ "SW_PREFETCH_ACCESS.T0",			0x32, 0x02, true },
    410 	{ "SW_PREFETCH_ACCESS.T1_T2",			0x32, 0x04, true },
    411 	{ "SW_PREFETCH_ACCESS.PREFETCHW",		0x32, 0x08, true },
    412 	{ "CPU_CLK_THREAD_UNHALTED.ONE_THREAD_ACTIVE",	0x3c, 0x02, true },
    413 	{ "CPU_CLK_UNHALTED.ONE_THREAD_ACTIVE",		0x3c, 0x02, true },
    414 	{ "L1D_PEND_MISS.PENDING",			0x48, 0x01, true },
    415 	{ "L1D_PEND_MISS.FB_FULL",			0x48, 0x02, true },
    416 	{ "DTLB_STORE_MISSES.MISS_CAUSES_A_WALK",	0x49, 0x01, true },
    417 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_4K",	0x49, 0x02, true },
    418 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_2M_4M",	0x49, 0x04, true },
    419 	{ "DTLB_STORE_MISSES.WALK_COMPLETED_1G",	0x49, 0x08, true },
    420 	{ "DTLB_STORE_MISSES.WALK_COMPLETED",		0x49, 0x0e, true },
    421 	{ "DTLB_STORE_MISSES.WALK_PENDING",		0x49, 0x10, true },
    422 	{ "DTLB_STORE_MISSES.STLB_HIT",			0x49, 0x20, true },
    423 	{ "LOAD_HIT_PRE.SW_PF",				0x4c, 0x01, true },
    424 	{ "EPT.WALK_PENDING",				0x4f, 0x10, true },
    425 	{ "L1D.REPLACEMENT",				0x51, 0x01, true },
    426 	{ "RS_EVENTS.EMPTY_CYCLES",			0x5e, 0x01, true },
    427 	{ "OFFCORE_REQUESTS_OUTSTANDING.DEMAND_DATA_RD", 0x60, 0x01, true },
    428 	{ "OFFCORE_REQUESTS_OUTSTANDING.DEMAND_CODE_RD", 0x60, 0x02, true },
    429 	{ "OFFCORE_REQUESTS_OUTSTANDING.DEMAND_RFO",	0x60, 0x04, true },
    430 	{ "OFFCORE_REQUESTS_OUTSTANDING.ALL_DATA_RD",	0x60, 0x08, true },
    431 	{ "OFFCORE_REQUESTS_OUTSTANDING.L3_MISS_DEMAND_DATA_RD",
    432 	  						0x60, 0x10, true },
    433 	{ "IDQ.MITE_UOPS",				0x79, 0x04, true },
    434 	{ "IDQ.DSB_UOPS",				0x79, 0x08, true },
    435 	{ "IDQ.MS_MITE_UOPS",				0x79, 0x20, true },
    436 	{ "IDQ.MS_UOPS",				0x79, 0x30, true },
    437 	{ "ICACHE_16B.IFDATA_STALL",			0x80, 0x04, true },
    438 	{ "ICACHE_64B.IFTAG_HIT",			0x83, 0x01, true },
    439 	{ "ICACHE_64B.IFTAG_MISS",			0x83, 0x02, true },
    440 	{ "ICACHE_64B.IFTAG_STALL",			0x83, 0x04, true },
    441 	{ "ITLB_MISSES.MISS_CAUSES_A_WALK",		0x85, 0x01, true },
    442 	{ "ITLB_MISSES.WALK_COMPLETED_4K",		0x85, 0x02, true },
    443 	{ "ITLB_MISSES.WALK_COMPLETED_2M_4M",		0x85, 0x04, true },
    444 	{ "ITLB_MISSES.WALK_COMPLETED_1G",		0x85, 0x08, true },
    445 	{ "ITLB_MISSES.WALK_COMPLETED",			0x85, 0x0e, true },
    446 	{ "ITLB_MISSES.WALK_PENDING",			0x85, 0x10, true },
    447 	{ "ITLB_MISSES.STLB_HIT",			0x85, 0x20, true },
    448 	{ "ILD_STALL.LCP",				0x87, 0x01, true },
    449 	{ "IDQ_UOPS_NOT_DELIVERED.CORE",		0x9c, 0x01, true },
    450 	{ "RESOURCE_STALLS.ANY",			0xa2, 0x01, true },
    451 	{ "RESOURCE_STALLS.SB",				0xa2, 0x08, true },
    452 	{ "EXE_ACTIVITY.EXE_BOUND_0_PORTS",		0xa6, 0x01, true },
    453 	{ "EXE_ACTIVITY.1_PORTS_UTIL",			0xa6, 0x02, true },
    454 	{ "EXE_ACTIVITY.2_PORTS_UTIL",			0xa6, 0x04, true },
    455 	{ "EXE_ACTIVITY.3_PORTS_UTIL",			0xa6, 0x08, true },
    456 	{ "EXE_ACTIVITY.4_PORTS_UTIL",			0xa6, 0x10, true },
    457 	{ "EXE_ACTIVITY.BOUND_ON_STORES",		0xa6, 0x40, true },
    458 	{ "LSD.UOPS",					0xa8, 0x01, true },
    459 	{ "DSB2MITE_SWITCHES.PENALTY_CYCLES",		0xab, 0x02, true },
    460 	{ "ITLB.ITLB_FLUSH",				0xae, 0x01, true },
    461 	{ "OFFCORE_REQUESTS.DEMAND_DATA_RD",		0xb0, 0x01, true },
    462 	{ "OFFCORE_REQUESTS.DEMAND_CODE_RD",		0xb0, 0x02, true },
    463 	{ "OFFCORE_REQUESTS.DEMAND_RFO",		0xb0, 0x04, true },
    464 	{ "OFFCORE_REQUESTS.ALL_DATA_RD",		0xb0, 0x08, true },
    465 	{ "OFFCORE_REQUESTS.L3_MISS_DEMAND_DATA_RD",	0xb0, 0x10, true },
    466 	{ "OFFCORE_REQUESTS.ALL_REQUESTS",		0xb0, 0x80, true },
    467 	{ "UOPS_EXECUTED.THREAD",			0xb1, 0x01, true },
    468 	{ "UOPS_EXECUTED.CORE",				0xb1, 0x02, true },
    469 	{ "UOPS_EXECUTED.X87",				0xb1, 0x10, true },
    470 	{ "OFFCORE_REQUESTS_BUFFER.SQ_FULL",		0xb2, 0x01, true },
    471 	{ "TLB_FLUSH.DTLB_THREAD",			0xbd, 0x01, true },
    472 	{ "TLB_FLUSH.STLB_ANY",				0xbd, 0x20, true },
    473 	{ "INST_RETIRED.PREC_DIST",			0xc0, 0x01, true },
    474 	{ "OTHER_ASSISTS.ANY",				0xc1, 0x3f, true },
    475 	{ "UOPS_RETIRED.RETIRE_SLOTS",			0xc2, 0x02, true },
    476 	{ "MACHINE_CLEARS.MEMORY_ORDERING",		0xc3, 0x02, true },
    477 	{ "MACHINE_CLEARS.SMC",				0xc3, 0x04, true },
    478 	{ "BR_INST_RETIRED.CONDITIONAL",		0xc4, 0x01, true },
    479 	{ "BR_INST_RETIRED.NEAR_CALL",			0xc4, 0x02, true },
    480 	{ "BR_INST_RETIRED.NEAR_RETURN",		0xc4, 0x08, true },
    481 	{ "BR_INST_RETIRED.NOT_TAKEN",			0xc4, 0x10, true },
    482 	{ "BR_INST_RETIRED.NEAR_TAKEN",			0xc4, 0x20, true },
    483 	{ "BR_INST_RETIRED.FAR_BRANCH",			0xc4, 0x40, true },
    484 	{ "BR_MISP_RETIRED.CONDITIONAL",		0xc5, 0x01, true },
    485 	{ "BR_MISP_RETIRED.NEAR_CALL",			0xc5, 0x02, true },
    486 	{ "BR_MISP_RETIRED.NEAR_TAKEN",			0xc5, 0x20, true },
    487 	{ "HW_INTERRUPTS.RECEIVED",			0xcb, 0x01, true },
    488 	{ "MEM_INST_RETIRED.STLB_MISS_LOADS",		0xd0, 0x11, true },
    489 	{ "MEM_INST_RETIRED.STLB_MISS_STORES",		0xd0, 0x12, true },
    490 	{ "MEM_INST_RETIRED.LOCK_LOADS",		0xd0, 0x21, true },
    491 	{ "MEM_INST_RETIRED.SPLIT_LOADS",		0xd0, 0x41, true },
    492 	{ "MEM_INST_RETIRED.SPLIT_STORES",		0xd0, 0x42, true },
    493 	{ "MEM_INST_RETIRED.ALL_LOADS",			0xd0, 0x81, true },
    494 	{ "MEM_INST_RETIRED.ALL_STORES",		0xd0, 0x82, true },
    495 	{ "MEM_LOAD_RETIRED.L1_HIT",			0xd1, 0x01, true },
    496 	{ "MEM_LOAD_RETIRED.L2_HIT",			0xd1, 0x02, true },
    497 	{ "MEM_LOAD_RETIRED.L3_HIT",			0xd1, 0x04, true },
    498 	{ "MEM_LOAD_RETIRED.L1_MISS",			0xd1, 0x08, true },
    499 	{ "MEM_LOAD_RETIRED.L2_MISS",			0xd1, 0x10, true },
    500 	{ "MEM_LOAD_RETIRED.L3_MISS",			0xd1, 0x20, true },
    501 	{ "MEM_LOAD_RETIRED.FB_HIT",			0xd1, 0x40, true },
    502 	{ "MEM_LOAD_L3_HIT_RETIRED.XSNP_MISS",		0xd2, 0x01, true },
    503 	{ "MEM_LOAD_L3_HIT_RETIRED.XSNP_HIT",		0xd2, 0x02, true },
    504 	{ "MEM_LOAD_L3_HIT_RETIRED.XSNP_HITM",		0xd2, 0x04, true },
    505 	{ "MEM_LOAD_L3_HIT_RETIRED.XSNP_NONE",		0xd2, 0x08, true },
    506 	{ "MEM_LOAD_MISC_RETIRED.UC",			0xd4, 0x04, true },
    507 	{ "BACLEARS.ANY",				0xe6, 0x01, true },
    508 	{ "L2_TRANS.L2_WB",				0xf0, 0x40, true },
    509 	{ "L2_LINES_IN.ALL",				0xf1, 0x1f, true },
    510 	{ "L2_LINES_OUT.SILENT",			0xf2, 0x01, true },
    511 	{ "L2_LINES_OUT.NON_SILENT",			0xf2, 0x02, true },
    512 	{ "L2_LINES_OUT.USELESS_HWPF",			0xf2, 0x04, true },
    513 	{ "SQ_MISC.SPLIT_LOCK",				0xf4, 0x10, true },
    514 };
    515 
    516 static struct event_table intel_skylake_kabylake = {
    517 	.tablename = "Intel Skylake/Kabylake",
    518 	.names = intel_skylake_kabylake_names,
    519 	.nevents = sizeof(intel_skylake_kabylake_names) /
    520 	    sizeof(struct name_to_event),
    521 	.next = NULL
    522 };
    523 
    524 static struct event_table *
    525 init_intel_skylake_kabylake(void)
    526 {
    527 
    528 	return &intel_skylake_kabylake;
    529 }
    530 
    531 static struct event_table *
    532 init_intel_generic(void)
    533 {
    534 	unsigned int eax, ebx, ecx, edx;
    535 	struct event_table *table;
    536 
    537 	/*
    538 	 * The kernel made sure the Architectural Version 1 PMCs were
    539 	 * present.
    540 	 */
    541 	table = init_intel_arch1();
    542 
    543 	/*
    544 	 * Now query the additional (non-architectural) events. They
    545 	 * depend on the CPU model.
    546 	 */
    547 	eax = 0x01;
    548 	ebx = 0;
    549 	ecx = 0;
    550 	edx = 0;
    551 	x86_cpuid(&eax, &ebx, &ecx, &edx);
    552 
    553 	if (CPUID_TO_FAMILY(eax) == 6) {
    554 		switch (CPUID_TO_MODEL(eax)) {
    555 		case 0x37: /* Silvermont (Bay Trail) */
    556 		case 0x4a: /* Silvermont (Tangier) */
    557 		case 0x4c: /* Airmont (Braswell, Cherry Trail) */
    558 		case 0x4d: /* Silvermont (Avoton, Rangeley) */
    559 		case 0x5a: /* Silvermont (Anniedale) */
    560 		case 0x5d: /* Silvermont (SoFIA) */
    561 			table->next = init_intel_silvermont_airmont();
    562 			break;
    563 		case 0x5c: /* Goldmont (Apollo Lake) */
    564 		case 0x5f: /* Goldmont (Denverton) */
    565 			table->next = init_intel_goldmont();
    566 			break;
    567 		case 0x7a: /* Goldmont Plus (Gemini Lake) */
    568 			table->next = init_intel_goldmontplus();
    569 			break;
    570 		case 0x4e: /* Skylake */
    571 		case 0x5e: /* Skylake */
    572 		case 0x8e: /* Kabylake */
    573 		case 0x9e: /* Kabylake */
    574 			table->next = init_intel_skylake_kabylake();
    575 			break;
    576 		}
    577 	}
    578 
    579 	return table;
    580 }
    581 
    582 /* ------------------------------------------------------------------------- */
    583 
    584 /*
    585  * AMD Family 10h
    586  */
    587 static struct name_to_event amd_f10h_names[] = {
    588 	{ "seg-load-all",		0x20, 0x7f, true },
    589 	{ "seg-load-es",		0x20, 0x01, true },
    590 	{ "seg-load-cs",		0x20, 0x02, true },
    591 	{ "seg-load-ss",		0x20, 0x04, true },
    592 	{ "seg-load-ds",		0x20, 0x08, true },
    593 	{ "seg-load-fs",		0x20, 0x10, true },
    594 	{ "seg-load-gs",		0x20, 0x20, true },
    595 	{ "seg-load-hs",		0x20, 0x40, true },
    596 	{ "l1cache-access",		0x40, 0x00, true },
    597 	{ "l1cache-miss",		0x41, 0x00, true },
    598 	{ "l1cache-refill",		0x42, 0x1f, true },
    599 	{ "l1cache-refill-invalid",	0x42, 0x01, true },
    600 	{ "l1cache-refill-shared",	0x42, 0x02, true },
    601 	{ "l1cache-refill-exclusive",	0x42, 0x04, true },
    602 	{ "l1cache-refill-owner",	0x42, 0x08, true },
    603 	{ "l1cache-refill-modified",	0x42, 0x10, true },
    604 	{ "l1cache-load",		0x43, 0x1f, true },
    605 	{ "l1cache-load-invalid",	0x43, 0x01, true },
    606 	{ "l1cache-load-shared",	0x43, 0x02, true },
    607 	{ "l1cache-load-exclusive",	0x43, 0x04, true },
    608 	{ "l1cache-load-owner",		0x43, 0x08, true },
    609 	{ "l1cache-load-modified",	0x43, 0x10, true },
    610 	{ "l1cache-writeback",		0x44, 0x1f, true },
    611 	{ "l1cache-writeback-invalid",	0x44, 0x01, true },
    612 	{ "l1cache-writeback-shared",	0x44, 0x02, true },
    613 	{ "l1cache-writeback-exclusive",0x44, 0x04, true },
    614 	{ "l1cache-writeback-owner",	0x44, 0x08, true },
    615 	{ "l1cache-writeback-modified",	0x44, 0x10, true },
    616 	{ "l1DTLB-hit-all",		0x4d, 0x07, true },
    617 	{ "l1DTLB-hit-4Kpage",		0x4d, 0x01, true },
    618 	{ "l1DTLB-hit-2Mpage",		0x4d, 0x02, true },
    619 	{ "l1DTLB-hit-1Gpage",		0x4d, 0x04, true },
    620 	{ "l1DTLB-miss-all",		0x45, 0x07, true },
    621 	{ "l1DTLB-miss-4Kpage",		0x45, 0x01, true },
    622 	{ "l1DTLB-miss-2Mpage",		0x45, 0x02, true },
    623 	{ "l1DTLB-miss-1Gpage",		0x45, 0x04, true },
    624 	{ "l2DTLB-miss-all",		0x46, 0x03, true },
    625 	{ "l2DTLB-miss-4Kpage",		0x46, 0x01, true },
    626 	{ "l2DTLB-miss-2Mpage",		0x46, 0x02, true },
    627 	/* l2DTLB-miss-1Gpage: reserved on some revisions, so disabled */
    628 	{ "l1ITLB-miss",		0x84, 0x00, true },
    629 	{ "l2ITLB-miss-all",		0x85, 0x03, true },
    630 	{ "l2ITLB-miss-4Kpage",		0x85, 0x01, true },
    631 	{ "l2ITLB-miss-2Mpage",		0x85, 0x02, true },
    632 	{ "mem-misalign-ref",		0x47, 0x00, true },
    633 	{ "ins-fetch",			0x80, 0x00, true },
    634 	{ "ins-fetch-miss",		0x81, 0x00, true },
    635 	{ "ins-refill-l2",		0x82, 0x00, true },
    636 	{ "ins-refill-sys",		0x83, 0x00, true },
    637 	{ "ins-fetch-stall",		0x87, 0x00, true },
    638 	{ "ins-retired",		0xc0, 0x00, true },
    639 	{ "ins-empty",			0xd0, 0x00, true },
    640 	{ "ops-retired",		0xc1, 0x00, true },
    641 	{ "branch-retired",		0xc2, 0x00, true },
    642 	{ "branch-miss-retired",	0xc3, 0x00, true },
    643 	{ "branch-taken-retired",	0xc4, 0x00, true },
    644 	{ "branch-taken-miss-retired",	0xc5, 0x00, true },
    645 	{ "branch-far-retired",		0xc6, 0x00, true },
    646 	{ "branch-resync-retired",	0xc7, 0x00, true },
    647 	{ "branch-near-retired",	0xc8, 0x00, true },
    648 	{ "branch-near-miss-retired",	0xc9, 0x00, true },
    649 	{ "branch-indirect-miss-retired", 0xca, 0x00, true },
    650 	{ "int-hw",			0xcf, 0x00, true },
    651 	{ "int-cycles-masked",		0xcd, 0x00, true },
    652 	{ "int-cycles-masked-pending",	0xce, 0x00, true },
    653 	{ "fpu-exceptions",		0xdb, 0x00, true },
    654 	{ "break-match0",		0xdc, 0x00, true },
    655 	{ "break-match1",		0xdd, 0x00, true },
    656 	{ "break-match2",		0xde, 0x00, true },
    657 	{ "break-match3",		0xdf, 0x00, true },
    658 };
    659 
    660 static struct event_table amd_f10h = {
    661 	.tablename = "AMD Family 10h",
    662 	.names = amd_f10h_names,
    663 	.nevents = sizeof(amd_f10h_names) /
    664 	    sizeof(struct name_to_event),
    665 	.next = NULL
    666 };
    667 
    668 /*
    669  * AMD Family 15h
    670  */
    671 static struct name_to_event amd_f15h_names[] = {
    672 	{ "FpPipeAssignment",		0x000, 0x77, true },
    673 	{ "FpSchedulerEmpty",		0x001, 0x00, true },
    674 	{ "FpRetSseAvxOps",		0x003, 0xff, true },
    675 	{ "FpNumMovElim",		0x004, 0x0f, true },
    676 	{ "FpRetiredSerOps",		0x005, 0x0f, true },
    677 	{ "LsSegRegLoads",		0x020, 0x7f, true },
    678 	{ "LsPipeRestartSelfMod",	0x021, 0x00, true },
    679 	{ "LsPipeRestartVarious",	0x022, 0x1f, true },
    680 	{ "LsLoadQueueStoreQFull",	0x023, 0x03, true },
    681 	{ "LsLockedOps",		0x024, 0x00, true },
    682 	{ "LsRetClflushInstr",		0x026, 0x00, true },
    683 	{ "LsRetCpuidInstr",		0x027, 0x00, true },
    684 	{ "LsDispatch",			0x029, 0x07, true },
    685 	{ "LsCanStoreToLoadFwOps",	0x02a, 0x03, true },
    686 	{ "LsSmisReceived",		0x02b, 0x00, true },
    687 	{ "LsExecClflushInstr",		0x030, 0x00, true },
    688 	{ "LsMisalignStore",		0x032, 0x00, true },
    689 	{ "LsFpLoadBufStall",		0x034, 0x00, true },
    690 	{ "LsStlf",			0x035, 0x00, true },
    691 	{ "DcCacheAccess",		0x040, 0x00, true },
    692 	{ "DcCacheMiss",		0x041, 0x00, true },
    693 	{ "DcCacheFillL2Sys",		0x042, 0x1f, true },
    694 	{ "DcCacheFillSys",		0x043, 0x00, true },
    695 	{ "DcUnifiedTlbHit",		0x045, 0x77, true },
    696 	{ "DcUnifiedTlbMiss",		0x046, 0x77, true },
    697 	{ "DcMisalignAccess",		0x047, 0x00, true },
    698 	{ "DcPrefetchInstrDisp",	0x04b, 0x07, true },
    699 	{ "DcIneffSwPrefetch",		0x052, 0x09, true },
    700 	{ "CuCmdVictimBuf",		0x060, 0x98, true },
    701 	{ "CuCmdMaskedOps",		0x061, 0x65, true },
    702 	{ "CuCmdReadBlkOps",		0x062, 0x77, true },
    703 	{ "CuCmdChgDirtyOps",		0x063, 0x08, true },
    704 	{ "CuDramSysReq",		0x064, 0x00, true },
    705 	{ "CuMemReqByType",		0x065, 0x83, true },
    706 	{ "CuDataCachePrefetch",	0x067, 0x03, true },
    707 	{ "CuMabReq",			0x068, 0xff, true },
    708 	{ "CuMabWaitCyc",		0x069, 0xff, true },
    709 	{ "CuSysRespCacheFill",		0x06c, 0x3f, true },
    710 	{ "CuOctwordsWritten",		0x06d, 0x01, true },
    711 	{ "CuCacheXInv",		0x075, 0x0f, true },
    712 	{ "CuCpuClkNotHalted",		0x076, 0x00, true },
    713 	{ "CuL2Req",			0x07d, 0x5f, true },
    714 	{ "CuL2Miss",			0x07e, 0x17, true },
    715 	{ "CuL2FillWb",			0x07f, 0x07, true },
    716 	{ "CuPageSplintering",		0x165, 0x07, true },
    717 	{ "CuL2PrefetchTrigEv",		0x16c, 0x03, true },
    718 	{ "CuXabAllocStall",		0x177, 0x03, true },
    719 	{ "CuFreeXabEntries",		0x17f, 0x01, true },
    720 	{ "IcCacheFetch",		0x080, 0x00, true },
    721 	{ "IcCacheMiss",		0x081, 0x00, true },
    722 	{ "IcCacheFillL2",		0x082, 0x00, true },
    723 	{ "IcCacheFillSys",		0x083, 0x00, true },
    724 	{ "IcL1TlbMissL2Hit",		0x084, 0x00, true },
    725 	{ "IcL1TlbMissL2Miss",		0x085, 0x07, true },
    726 	{ "IcPipeRestartInstrStrProbe",	0x086, 0x00, true },
    727 	{ "IcFetchStall",		0x087, 0x00, true },
    728 	{ "IcRetStackHits",		0x088, 0x00, true },
    729 	{ "IcRetStackOver",		0x089, 0x00, true },
    730 	{ "IcCacheVictims",		0x08b, 0x00, true },
    731 	{ "IcCacheLinesInv",		0x08c, 0x0f, true },
    732 	{ "IcTlbReload",		0x099, 0x00, true },
    733 	{ "IcTlbReloadAbort",		0x09a, 0x00, true },
    734 	{ "IcUopsDispatched",		0x186, 0x01, true },
    735 	{ "ExRetInstr",			0x0c0, 0x00, true },
    736 	{ "ExRetCops",			0x0c1, 0x00, true },
    737 	{ "ExRetBrn",			0x0c2, 0x00, true },
    738 	{ "ExRetBrnMisp",		0x0c3, 0x00, true },
    739 	{ "ExRetBrnTkn",		0x0c4, 0x00, true },
    740 	{ "ExRetBrnTknMisp",		0x0c5, 0x00, true },
    741 	{ "ExRetBrnFar",		0x0c6, 0x00, true },
    742 	{ "ExRetBrnResync",		0x0c7, 0x00, true },
    743 	{ "ExRetNearRet",		0x0c8, 0x00, true },
    744 	{ "ExRetNearRetMispred",	0x0c9, 0x00, true },
    745 	{ "ExRetBrnIndMisp",		0x0ca, 0x00, true },
    746 	{ "ExRetMmxFpInstr@X87",	0x0cb, 0x01, true },
    747 	{ "ExRetMmxFpInstr@Mmx",	0x0cb, 0x02, true },
    748 	{ "ExRetMmxFpInstr@Sse",	0x0cb, 0x04, true },
    749 	{ "ExIntMaskedCyc",		0x0cd, 0x00, true },
    750 	{ "ExIntMaskedCycIntPend",	0x0ce, 0x00, true },
    751 	{ "ExIntTaken",			0x0cf, 0x00, true },
    752 	{ "ExDecEmpty",			0x0d0, 0x00, true },
    753 	{ "ExDispStall",		0x0d1, 0x00, true },
    754 	{ "ExUseqStallSer",		0x0d2, 0x00, true },
    755 	{ "ExDispStallInstrRetQFull",	0x0d5, 0x00, true },
    756 	{ "ExDispStallIntSchedQFull",	0x0d6, 0x00, true },
    757 	{ "ExDispStallFpSchedQFull",	0x0d7, 0x00, true },
    758 	{ "ExDispStallLdqFull",		0x0d8, 0x00, true },
    759 	{ "ExUseqStallAllQuiet",	0x0d9, 0x00, true },
    760 	{ "ExFpuEx",			0x0db, 0x1f, true },
    761 	{ "ExBpDr0",			0x0dc, 0x8f, true },
    762 	{ "ExBpDr1",			0x0dd, 0x8f, true },
    763 	{ "ExBpDr2",			0x0de, 0x8f, true },
    764 	{ "ExBpDr3",			0x0df, 0x8f, true },
    765 	{ "ExRetx87FpOps",		0x1c0, 0x07, true },
    766 	{ "ExTaggedIbsOps",		0x1cf, 0x07, true },
    767 	{ "ExRetFusBrInstr",		0x1d0, 0x00, true },
    768 	{ "ExDispStallStqFull",		0x1d8, 0x00, true },
    769 	{ "ExCycNoDispIntPrfTok",	0x1dd, 0x00, true },
    770 	{ "ExCycNoDispfpPrfTok",	0x1de, 0x00, true },
    771 	{ "ExFpDispContention",		0x1df, 0x0f, true },
    772 };
    773 
    774 static struct event_table amd_f15h = {
    775 	.tablename = "AMD Family 15h",
    776 	.names = amd_f15h_names,
    777 	.nevents = sizeof(amd_f15h_names) /
    778 	    sizeof(struct name_to_event),
    779 	.next = NULL
    780 };
    781 
    782 /*
    783  * AMD Family 17h
    784  */
    785 static struct name_to_event amd_f17h_names[] = {
    786 	{ "FpRetx87FpOps",		0x02, __BITS(2,0), true },
    787 	{ "FpRetSseAvxOps",		0x03, __BITS(7,0), true },
    788 	{ "FpRetiredSerOps",		0x05, __BITS(3,0), true },
    789 	{ "LsL1DTlbMiss",		0x45, __BITS(7,0), true },
    790 	{ "LsTableWalker",		0x46, __BITS(3,0), true },
    791 	{ "LsMisalAccesses",		0x47, 0x00, true },
    792 	{ "LsInefSwPref",		0x52, __BITS(1,0), true },
    793 	{ "LsNotHaltedCyc",		0x76, 0x00, true },
    794 	{ "IcFw32",			0x80, 0x00, true },
    795 	{ "IcFw32Miss",			0x81, 0x00, true },
    796 	{ "IcCacheFillL2",		0x82, 0x00, true },
    797 	{ "IcCacheFillSys",		0x83, 0x00, true },
    798 	{ "IcFetchStall",		0x87, __BITS(2,0), true },
    799 	{ "IcCacheInval",		0x8c, __BITS(1,0), true },
    800 	{ "BpL1TlbMissL2Hit",		0x84, 0x00, true },
    801 	{ "BpL1TlbMissL2Miss",		0x85, 0x00, true },
    802 	{ "BpSnpReSync",		0x86, 0x00, true },
    803 	{ "BpL1BTBCorrect",		0x8a, 0x00, true },
    804 	{ "BpL2BTBCorrect",		0x8b, 0x00, true },
    805 	{ "BpTlbRel",			0x99, 0x00, true },
    806 	{ "ExRetInstr",			0xc0, 0x00, true },
    807 	{ "ExRetCops",			0xc1, 0x00, true },
    808 	{ "ExRetBrn",			0xc2, 0x00, true },
    809 	{ "ExRetBrnMisp",		0xc3, 0x00, true },
    810 	{ "ExRetBrnTkn",		0xc4, 0x00, true },
    811 	{ "ExRetBrnTknMisp",		0xc5, 0x00, true },
    812 	{ "ExRetBrnFar",		0xc6, 0x00, true },
    813 	{ "ExRetBrnResync",		0xc7, 0x00, true },
    814 	{ "ExRetBrnIndMisp",		0xca, 0x00, true },
    815 	{ "ExRetNearRet",		0xc8, 0x00, true },
    816 	{ "ExRetNearRetMispred",	0xc9, 0x00, true },
    817 	{ "ExRetMmxFpInstr@X87",	0xcb, __BIT(0), true },
    818 	{ "ExRetMmxFpInstr@Mmx",	0xcb, __BIT(1), true },
    819 	{ "ExRetMmxFpInstr@Sse",	0xcb, __BIT(2), true },
    820 	{ "ExRetCond",			0xd1, 0x00, true },
    821 	{ "ExRetCondMisp",		0xd2, 0x00, true },
    822 	{ "ExDivBusy",			0xd3, 0x00, true },
    823 	{ "ExDivCount",			0xd4, 0x00, true },
    824 };
    825 
    826 static struct event_table amd_f17h = {
    827 	.tablename = "AMD Family 17h",
    828 	.names = amd_f17h_names,
    829 	.nevents = sizeof(amd_f17h_names) /
    830 	    sizeof(struct name_to_event),
    831 	.next = NULL
    832 };
    833 
    834 static struct event_table *
    835 init_amd_generic(void)
    836 {
    837 	unsigned int eax, ebx, ecx, edx;
    838 
    839 	eax = 0x01;
    840 	ebx = 0;
    841 	ecx = 0;
    842 	edx = 0;
    843 	x86_cpuid(&eax, &ebx, &ecx, &edx);
    844 
    845 	switch (CPUID_TO_FAMILY(eax)) {
    846 	case 0x10:
    847 		return &amd_f10h;
    848 	case 0x15:
    849 		return &amd_f15h;
    850 	case 0x17:
    851 		return &amd_f17h;
    852 	}
    853 
    854 	return NULL;
    855 }
    856 
    857 /* ------------------------------------------------------------------------- */
    858 
    859 int
    860 tprof_event_init(uint32_t ident)
    861 {
    862 
    863 	switch (ident) {
    864 	case TPROF_IDENT_NONE:
    865 		return -1;
    866 	case TPROF_IDENT_INTEL_GENERIC:
    867 		cpuevents = init_intel_generic();
    868 		break;
    869 	case TPROF_IDENT_AMD_GENERIC:
    870 		cpuevents = init_amd_generic();
    871 		break;
    872 	}
    873 	return (cpuevents == NULL) ? -1 : 0;
    874 }
    875 
    876 static void
    877 recursive_event_list(struct event_table *table)
    878 {
    879 	size_t i;
    880 
    881 	printf("%s:\n", table->tablename);
    882 	for (i = 0; i < table->nevents; i++) {
    883 		if (!table->names[i].enabled)
    884 			continue;
    885 		printf("\t%s\n", table->names[i].name);
    886 	}
    887 
    888 	if (table->next != NULL)
    889 		recursive_event_list(table->next);
    890 }
    891 
    892 void
    893 tprof_event_list(void)
    894 {
    895 
    896 	recursive_event_list(cpuevents);
    897 }
    898 
    899 static void
    900 recursive_event_lookup(struct event_table *table, const char *name,
    901     struct tprof_param *param)
    902 {
    903 	size_t i;
    904 
    905 	for (i = 0; i < table->nevents; i++) {
    906 		if (!table->names[i].enabled)
    907 			continue;
    908 		if (!strcmp(table->names[i].name, name)) {
    909 			param->p_event = table->names[i].event;
    910 			param->p_unit = table->names[i].unit;
    911 			return;
    912 		}
    913 	}
    914 
    915 	if (table->next != NULL)
    916 		recursive_event_lookup(table->next, name, param);
    917 	else
    918 		errx(EXIT_FAILURE, "event '%s' unknown", name);
    919 }
    920 
    921 void
    922 tprof_event_lookup(const char *name, struct tprof_param *param)
    923 {
    924 
    925 	recursive_event_lookup(cpuevents, name, param);
    926 }
    927