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