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