tprof_armv7.c revision 1.1.4.2 1 1.1.4.2 christos /* $NetBSD: tprof_armv7.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 ARM ARM.
55 1.1.4.2 christos */
56 1.1.4.2 christos static struct pmu_event pmu_armv7_common_events[] = {
57 1.1.4.2 christos { 0x00, "SW_INCR" },
58 1.1.4.2 christos { 0x01, "L1I_CACHE_REFILL" },
59 1.1.4.2 christos { 0x02, "L1I_TLB_REFILL" },
60 1.1.4.2 christos { 0x03, "L1D_CACHE_REFILL" },
61 1.1.4.2 christos { 0x04, "L1D_CACHE" },
62 1.1.4.2 christos { 0x05, "L1D_TLB_REFILL" },
63 1.1.4.2 christos { 0x06, "LD_RETIRED" },
64 1.1.4.2 christos { 0x07, "ST_RETIRED" },
65 1.1.4.2 christos { 0x08, "INST_RETIRED" },
66 1.1.4.2 christos { 0x09, "EXC_TAKEN" },
67 1.1.4.2 christos { 0x0a, "EXC_RETURN" },
68 1.1.4.2 christos { 0x0b, "CID_WRITE_RETIRED" },
69 1.1.4.2 christos { 0x0c, "PC_WRITE_RETIRED" },
70 1.1.4.2 christos { 0x0d, "BR_IMMED_RETIRED" },
71 1.1.4.2 christos { 0x0e, "BR_RETURN_RETIRED" },
72 1.1.4.2 christos { 0x0f, "UNALIGNED_LDST_RETIRED" },
73 1.1.4.2 christos { 0x10, "BR_MIS_PRED" },
74 1.1.4.2 christos { 0x11, "CPU_CYCLES" },
75 1.1.4.2 christos { 0x12, "BR_PRED" },
76 1.1.4.2 christos { 0x13, "MEM_ACCESS" },
77 1.1.4.2 christos { 0x14, "L1I_CACHE" },
78 1.1.4.2 christos { 0x15, "L1D_CACHE_WB" },
79 1.1.4.2 christos { 0x16, "L2D_CACHE" },
80 1.1.4.2 christos { 0x17, "L2D_CACHE_REFILL" },
81 1.1.4.2 christos { 0x18, "L2D_CACHE_WB" },
82 1.1.4.2 christos { 0x19, "BUS_ACCESS" },
83 1.1.4.2 christos { 0x1a, "MEMORY_ERROR" },
84 1.1.4.2 christos { 0x1b, "INST_SPEC" },
85 1.1.4.2 christos { 0x1c, "TTBR_WRITE_RETIRED" },
86 1.1.4.2 christos { 0x1d, "BUS_CYCLES" },
87 1.1.4.2 christos };
88 1.1.4.2 christos
89 1.1.4.2 christos static struct pmu_event_table pmu_armv7 = {
90 1.1.4.2 christos .name = "ARMv7 common architectural and microarchitectural events",
91 1.1.4.2 christos .events = pmu_armv7_common_events,
92 1.1.4.2 christos .nevents = __arraycount(pmu_armv7_common_events),
93 1.1.4.2 christos .next = NULL
94 1.1.4.2 christos };
95 1.1.4.2 christos
96 1.1.4.2 christos static struct pmu_event_table *events = NULL;
97 1.1.4.2 christos
98 1.1.4.2 christos int
99 1.1.4.2 christos tprof_event_init(uint32_t ident)
100 1.1.4.2 christos {
101 1.1.4.2 christos if (ident != TPROF_IDENT_ARMV7_GENERIC)
102 1.1.4.2 christos return -1;
103 1.1.4.2 christos
104 1.1.4.2 christos events = &pmu_armv7;
105 1.1.4.2 christos
106 1.1.4.2 christos return 0;
107 1.1.4.2 christos }
108 1.1.4.2 christos
109 1.1.4.2 christos static void
110 1.1.4.2 christos tprof_event_list_table(struct pmu_event_table *tbl)
111 1.1.4.2 christos {
112 1.1.4.2 christos u_int n;
113 1.1.4.2 christos
114 1.1.4.2 christos printf("%s:\n", tbl->name);
115 1.1.4.2 christos for (n = 0; n < tbl->nevents; n++) {
116 1.1.4.2 christos printf("\t%s\n", tbl->events[n].name);
117 1.1.4.2 christos }
118 1.1.4.2 christos
119 1.1.4.2 christos if (tbl->next)
120 1.1.4.2 christos tprof_event_list_table(tbl->next);
121 1.1.4.2 christos }
122 1.1.4.2 christos
123 1.1.4.2 christos void
124 1.1.4.2 christos tprof_event_list(void)
125 1.1.4.2 christos {
126 1.1.4.2 christos tprof_event_list_table(events);
127 1.1.4.2 christos }
128 1.1.4.2 christos
129 1.1.4.2 christos static void
130 1.1.4.2 christos tprof_event_lookup_table(const char *name, struct tprof_param *param,
131 1.1.4.2 christos struct pmu_event_table *tbl)
132 1.1.4.2 christos {
133 1.1.4.2 christos u_int n;
134 1.1.4.2 christos
135 1.1.4.2 christos for (n = 0; n < tbl->nevents; n++) {
136 1.1.4.2 christos if (strcmp(tbl->events[n].name, name) == 0) {
137 1.1.4.2 christos param->p_event = tbl->events[n].event;
138 1.1.4.2 christos param->p_unit = 0;
139 1.1.4.2 christos return;
140 1.1.4.2 christos }
141 1.1.4.2 christos }
142 1.1.4.2 christos
143 1.1.4.2 christos if (tbl->next)
144 1.1.4.2 christos tprof_event_lookup_table(name, param, tbl->next);
145 1.1.4.2 christos else
146 1.1.4.2 christos errx(EXIT_FAILURE, "event '%s' unknown", name);
147 1.1.4.2 christos }
148 1.1.4.2 christos
149 1.1.4.2 christos void
150 1.1.4.2 christos tprof_event_lookup(const char *name, struct tprof_param *param)
151 1.1.4.2 christos {
152 1.1.4.2 christos tprof_event_lookup_table(name, param, events);
153 1.1.4.2 christos }
154