1*0b7c38e1Sjmcneill /* $NetBSD: tprof_armv8.c,v 1.1 2018/07/15 16:25:31 jmcneill Exp $ */
2*0b7c38e1Sjmcneill
3*0b7c38e1Sjmcneill /*-
4*0b7c38e1Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5*0b7c38e1Sjmcneill * All rights reserved.
6*0b7c38e1Sjmcneill *
7*0b7c38e1Sjmcneill * Redistribution and use in source and binary forms, with or without
8*0b7c38e1Sjmcneill * modification, are permitted provided that the following conditions
9*0b7c38e1Sjmcneill * are met:
10*0b7c38e1Sjmcneill * 1. Redistributions of source code must retain the above copyright
11*0b7c38e1Sjmcneill * notice, this list of conditions and the following disclaimer.
12*0b7c38e1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13*0b7c38e1Sjmcneill * notice, this list of conditions and the following disclaimer in the
14*0b7c38e1Sjmcneill * documentation and/or other materials provided with the distribution.
15*0b7c38e1Sjmcneill *
16*0b7c38e1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*0b7c38e1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*0b7c38e1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*0b7c38e1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*0b7c38e1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*0b7c38e1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*0b7c38e1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*0b7c38e1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*0b7c38e1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*0b7c38e1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*0b7c38e1Sjmcneill * SUCH DAMAGE.
27*0b7c38e1Sjmcneill */
28*0b7c38e1Sjmcneill
29*0b7c38e1Sjmcneill #include <sys/cdefs.h>
30*0b7c38e1Sjmcneill #include <sys/types.h>
31*0b7c38e1Sjmcneill
32*0b7c38e1Sjmcneill #include <err.h>
33*0b7c38e1Sjmcneill #include <stdio.h>
34*0b7c38e1Sjmcneill #include <stdint.h>
35*0b7c38e1Sjmcneill #include <stdlib.h>
36*0b7c38e1Sjmcneill #include <string.h>
37*0b7c38e1Sjmcneill
38*0b7c38e1Sjmcneill #include <dev/tprof/tprof_ioctl.h>
39*0b7c38e1Sjmcneill #include "../tprof.h"
40*0b7c38e1Sjmcneill
41*0b7c38e1Sjmcneill struct pmu_event {
42*0b7c38e1Sjmcneill uint16_t event;
43*0b7c38e1Sjmcneill const char *name;
44*0b7c38e1Sjmcneill };
45*0b7c38e1Sjmcneill
46*0b7c38e1Sjmcneill struct pmu_event_table {
47*0b7c38e1Sjmcneill const char *name;
48*0b7c38e1Sjmcneill struct pmu_event *events;
49*0b7c38e1Sjmcneill u_int nevents;
50*0b7c38e1Sjmcneill struct pmu_event_table *next;
51*0b7c38e1Sjmcneill };
52*0b7c38e1Sjmcneill
53*0b7c38e1Sjmcneill /*
54*0b7c38e1Sjmcneill * Common event numbers, from ARMv8 ARM.
55*0b7c38e1Sjmcneill */
56*0b7c38e1Sjmcneill static struct pmu_event pmu_armv8_common_events[] = {
57*0b7c38e1Sjmcneill { 0x0000, "SW_INCR" },
58*0b7c38e1Sjmcneill { 0x0001, "L1I_CACHE_REFILL" },
59*0b7c38e1Sjmcneill { 0x0002, "L1I_TLB_REFILL" },
60*0b7c38e1Sjmcneill { 0x0003, "L1D_CACHE_REFILL" },
61*0b7c38e1Sjmcneill { 0x0004, "L1D_CACHE" },
62*0b7c38e1Sjmcneill { 0x0005, "L1D_TLB_REFILL" },
63*0b7c38e1Sjmcneill { 0x0006, "LD_RETIRED" },
64*0b7c38e1Sjmcneill { 0x0007, "ST_RETIRED" },
65*0b7c38e1Sjmcneill { 0x0008, "INST_RETIRED" },
66*0b7c38e1Sjmcneill { 0x0009, "EXC_TAKEN" },
67*0b7c38e1Sjmcneill { 0x000a, "EXC_RETURN" },
68*0b7c38e1Sjmcneill { 0x000b, "CID_WRITE_RETIRED" },
69*0b7c38e1Sjmcneill { 0x000c, "PC_WRITE_RETIRED" },
70*0b7c38e1Sjmcneill { 0x000d, "BR_IMMED_RETIRED" },
71*0b7c38e1Sjmcneill { 0x000e, "BR_RETURN_RETIRED" },
72*0b7c38e1Sjmcneill { 0x000f, "UNALIGNED_LDST_RETIRED" },
73*0b7c38e1Sjmcneill { 0x0010, "BR_MIS_PRED" },
74*0b7c38e1Sjmcneill { 0x0011, "CPU_CYCLES" },
75*0b7c38e1Sjmcneill { 0x0012, "BR_PRED" },
76*0b7c38e1Sjmcneill { 0x0013, "MEM_ACCESS" },
77*0b7c38e1Sjmcneill { 0x0014, "L1I_CACHE" },
78*0b7c38e1Sjmcneill { 0x0015, "L1D_CACHE_WB" },
79*0b7c38e1Sjmcneill { 0x0016, "L2D_CACHE" },
80*0b7c38e1Sjmcneill { 0x0017, "L2D_CACHE_REFILL" },
81*0b7c38e1Sjmcneill { 0x0018, "L2D_CACHE_WB" },
82*0b7c38e1Sjmcneill { 0x0019, "BUS_ACCESS" },
83*0b7c38e1Sjmcneill { 0x001a, "MEMORY_ERROR" },
84*0b7c38e1Sjmcneill { 0x001b, "INST_SPEC" },
85*0b7c38e1Sjmcneill { 0x001c, "TTBR_WRITE_RETIRED" },
86*0b7c38e1Sjmcneill { 0x001d, "BUS_CYCLES" },
87*0b7c38e1Sjmcneill { 0x001e, "CHAIN" },
88*0b7c38e1Sjmcneill { 0x001f, "L1D_CACHE_ALLOCATE" },
89*0b7c38e1Sjmcneill { 0x0020, "L2D_CACHE_ALLOCATE" },
90*0b7c38e1Sjmcneill { 0x0021, "BR_RETIRED" },
91*0b7c38e1Sjmcneill { 0x0022, "BR_MIS_PRED_RETIRED" },
92*0b7c38e1Sjmcneill { 0x0023, "STALL_FRONTEND" },
93*0b7c38e1Sjmcneill { 0x0024, "STALL_BACKEND" },
94*0b7c38e1Sjmcneill { 0x0025, "L1D_TLB" },
95*0b7c38e1Sjmcneill { 0x0026, "L1I_TLB" },
96*0b7c38e1Sjmcneill { 0x0027, "L2I_CACHE" },
97*0b7c38e1Sjmcneill { 0x0028, "L2I_CACHE_REFILL" },
98*0b7c38e1Sjmcneill { 0x0029, "L3D_CACHE_ALLOCATE" },
99*0b7c38e1Sjmcneill { 0x002a, "L3D_CACHE_REFILL" },
100*0b7c38e1Sjmcneill { 0x002b, "L3D_CACHE" },
101*0b7c38e1Sjmcneill { 0x002c, "L3D_CACHE_WB" },
102*0b7c38e1Sjmcneill { 0x002d, "L2D_TLB_REFILL" },
103*0b7c38e1Sjmcneill { 0x002e, "L2I_TLB_REFILL" },
104*0b7c38e1Sjmcneill { 0x002f, "L2D_TLB" },
105*0b7c38e1Sjmcneill { 0x0030, "L2I_TLB" },
106*0b7c38e1Sjmcneill { 0x0031, "REMOTE_ACCESS" },
107*0b7c38e1Sjmcneill { 0x0032, "LL_CACHE" },
108*0b7c38e1Sjmcneill { 0x0033, "LL_CACHE_MISS" },
109*0b7c38e1Sjmcneill { 0x0034, "DTLB_WALK" },
110*0b7c38e1Sjmcneill { 0x0035, "ITLB_WALK" },
111*0b7c38e1Sjmcneill { 0x0036, "LL_CACHE_RD" },
112*0b7c38e1Sjmcneill { 0x0037, "LL_CACHE_MISS_RD" },
113*0b7c38e1Sjmcneill { 0x0038, "REMOTE_ACCESS_RD" },
114*0b7c38e1Sjmcneill };
115*0b7c38e1Sjmcneill
116*0b7c38e1Sjmcneill static struct pmu_event_table pmu_armv8 = {
117*0b7c38e1Sjmcneill .name = "ARMv8 common architectural and microarchitectural events",
118*0b7c38e1Sjmcneill .events = pmu_armv8_common_events,
119*0b7c38e1Sjmcneill .nevents = __arraycount(pmu_armv8_common_events),
120*0b7c38e1Sjmcneill .next = NULL
121*0b7c38e1Sjmcneill };
122*0b7c38e1Sjmcneill
123*0b7c38e1Sjmcneill static struct pmu_event_table *events = NULL;
124*0b7c38e1Sjmcneill
125*0b7c38e1Sjmcneill int
tprof_event_init(uint32_t ident)126*0b7c38e1Sjmcneill tprof_event_init(uint32_t ident)
127*0b7c38e1Sjmcneill {
128*0b7c38e1Sjmcneill if (ident != TPROF_IDENT_ARMV8_GENERIC)
129*0b7c38e1Sjmcneill return -1;
130*0b7c38e1Sjmcneill
131*0b7c38e1Sjmcneill events = &pmu_armv8;
132*0b7c38e1Sjmcneill
133*0b7c38e1Sjmcneill return 0;
134*0b7c38e1Sjmcneill }
135*0b7c38e1Sjmcneill
136*0b7c38e1Sjmcneill static void
tprof_event_list_table(struct pmu_event_table * tbl)137*0b7c38e1Sjmcneill tprof_event_list_table(struct pmu_event_table *tbl)
138*0b7c38e1Sjmcneill {
139*0b7c38e1Sjmcneill u_int n;
140*0b7c38e1Sjmcneill
141*0b7c38e1Sjmcneill printf("%s:\n", tbl->name);
142*0b7c38e1Sjmcneill for (n = 0; n < tbl->nevents; n++) {
143*0b7c38e1Sjmcneill printf("\t%s\n", tbl->events[n].name);
144*0b7c38e1Sjmcneill }
145*0b7c38e1Sjmcneill
146*0b7c38e1Sjmcneill if (tbl->next)
147*0b7c38e1Sjmcneill tprof_event_list_table(tbl->next);
148*0b7c38e1Sjmcneill }
149*0b7c38e1Sjmcneill
150*0b7c38e1Sjmcneill void
tprof_event_list(void)151*0b7c38e1Sjmcneill tprof_event_list(void)
152*0b7c38e1Sjmcneill {
153*0b7c38e1Sjmcneill tprof_event_list_table(events);
154*0b7c38e1Sjmcneill }
155*0b7c38e1Sjmcneill
156*0b7c38e1Sjmcneill static void
tprof_event_lookup_table(const char * name,struct tprof_param * param,struct pmu_event_table * tbl)157*0b7c38e1Sjmcneill tprof_event_lookup_table(const char *name, struct tprof_param *param,
158*0b7c38e1Sjmcneill struct pmu_event_table *tbl)
159*0b7c38e1Sjmcneill {
160*0b7c38e1Sjmcneill u_int n;
161*0b7c38e1Sjmcneill
162*0b7c38e1Sjmcneill for (n = 0; n < tbl->nevents; n++) {
163*0b7c38e1Sjmcneill if (strcmp(tbl->events[n].name, name) == 0) {
164*0b7c38e1Sjmcneill param->p_event = tbl->events[n].event;
165*0b7c38e1Sjmcneill param->p_unit = 0;
166*0b7c38e1Sjmcneill return;
167*0b7c38e1Sjmcneill }
168*0b7c38e1Sjmcneill }
169*0b7c38e1Sjmcneill
170*0b7c38e1Sjmcneill if (tbl->next)
171*0b7c38e1Sjmcneill tprof_event_lookup_table(name, param, tbl->next);
172*0b7c38e1Sjmcneill else
173*0b7c38e1Sjmcneill errx(EXIT_FAILURE, "event '%s' unknown", name);
174*0b7c38e1Sjmcneill }
175*0b7c38e1Sjmcneill
176*0b7c38e1Sjmcneill void
tprof_event_lookup(const char * name,struct tprof_param * param)177*0b7c38e1Sjmcneill tprof_event_lookup(const char *name, struct tprof_param *param)
178*0b7c38e1Sjmcneill {
179*0b7c38e1Sjmcneill tprof_event_lookup_table(name, param, events);
180*0b7c38e1Sjmcneill }
181