1*091e7586Sjmcneill /* $NetBSD: tprof_armv7.c,v 1.1 2018/07/15 23:50:53 jmcneill Exp $ */
2*091e7586Sjmcneill
3*091e7586Sjmcneill /*-
4*091e7586Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5*091e7586Sjmcneill * All rights reserved.
6*091e7586Sjmcneill *
7*091e7586Sjmcneill * Redistribution and use in source and binary forms, with or without
8*091e7586Sjmcneill * modification, are permitted provided that the following conditions
9*091e7586Sjmcneill * are met:
10*091e7586Sjmcneill * 1. Redistributions of source code must retain the above copyright
11*091e7586Sjmcneill * notice, this list of conditions and the following disclaimer.
12*091e7586Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13*091e7586Sjmcneill * notice, this list of conditions and the following disclaimer in the
14*091e7586Sjmcneill * documentation and/or other materials provided with the distribution.
15*091e7586Sjmcneill *
16*091e7586Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*091e7586Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*091e7586Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*091e7586Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*091e7586Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*091e7586Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22*091e7586Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*091e7586Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*091e7586Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*091e7586Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*091e7586Sjmcneill * SUCH DAMAGE.
27*091e7586Sjmcneill */
28*091e7586Sjmcneill
29*091e7586Sjmcneill #include <sys/cdefs.h>
30*091e7586Sjmcneill #include <sys/types.h>
31*091e7586Sjmcneill
32*091e7586Sjmcneill #include <err.h>
33*091e7586Sjmcneill #include <stdio.h>
34*091e7586Sjmcneill #include <stdint.h>
35*091e7586Sjmcneill #include <stdlib.h>
36*091e7586Sjmcneill #include <string.h>
37*091e7586Sjmcneill
38*091e7586Sjmcneill #include <dev/tprof/tprof_ioctl.h>
39*091e7586Sjmcneill #include "../tprof.h"
40*091e7586Sjmcneill
41*091e7586Sjmcneill struct pmu_event {
42*091e7586Sjmcneill uint16_t event;
43*091e7586Sjmcneill const char *name;
44*091e7586Sjmcneill };
45*091e7586Sjmcneill
46*091e7586Sjmcneill struct pmu_event_table {
47*091e7586Sjmcneill const char *name;
48*091e7586Sjmcneill struct pmu_event *events;
49*091e7586Sjmcneill u_int nevents;
50*091e7586Sjmcneill struct pmu_event_table *next;
51*091e7586Sjmcneill };
52*091e7586Sjmcneill
53*091e7586Sjmcneill /*
54*091e7586Sjmcneill * Common event numbers, from ARM ARM.
55*091e7586Sjmcneill */
56*091e7586Sjmcneill static struct pmu_event pmu_armv7_common_events[] = {
57*091e7586Sjmcneill { 0x00, "SW_INCR" },
58*091e7586Sjmcneill { 0x01, "L1I_CACHE_REFILL" },
59*091e7586Sjmcneill { 0x02, "L1I_TLB_REFILL" },
60*091e7586Sjmcneill { 0x03, "L1D_CACHE_REFILL" },
61*091e7586Sjmcneill { 0x04, "L1D_CACHE" },
62*091e7586Sjmcneill { 0x05, "L1D_TLB_REFILL" },
63*091e7586Sjmcneill { 0x06, "LD_RETIRED" },
64*091e7586Sjmcneill { 0x07, "ST_RETIRED" },
65*091e7586Sjmcneill { 0x08, "INST_RETIRED" },
66*091e7586Sjmcneill { 0x09, "EXC_TAKEN" },
67*091e7586Sjmcneill { 0x0a, "EXC_RETURN" },
68*091e7586Sjmcneill { 0x0b, "CID_WRITE_RETIRED" },
69*091e7586Sjmcneill { 0x0c, "PC_WRITE_RETIRED" },
70*091e7586Sjmcneill { 0x0d, "BR_IMMED_RETIRED" },
71*091e7586Sjmcneill { 0x0e, "BR_RETURN_RETIRED" },
72*091e7586Sjmcneill { 0x0f, "UNALIGNED_LDST_RETIRED" },
73*091e7586Sjmcneill { 0x10, "BR_MIS_PRED" },
74*091e7586Sjmcneill { 0x11, "CPU_CYCLES" },
75*091e7586Sjmcneill { 0x12, "BR_PRED" },
76*091e7586Sjmcneill { 0x13, "MEM_ACCESS" },
77*091e7586Sjmcneill { 0x14, "L1I_CACHE" },
78*091e7586Sjmcneill { 0x15, "L1D_CACHE_WB" },
79*091e7586Sjmcneill { 0x16, "L2D_CACHE" },
80*091e7586Sjmcneill { 0x17, "L2D_CACHE_REFILL" },
81*091e7586Sjmcneill { 0x18, "L2D_CACHE_WB" },
82*091e7586Sjmcneill { 0x19, "BUS_ACCESS" },
83*091e7586Sjmcneill { 0x1a, "MEMORY_ERROR" },
84*091e7586Sjmcneill { 0x1b, "INST_SPEC" },
85*091e7586Sjmcneill { 0x1c, "TTBR_WRITE_RETIRED" },
86*091e7586Sjmcneill { 0x1d, "BUS_CYCLES" },
87*091e7586Sjmcneill };
88*091e7586Sjmcneill
89*091e7586Sjmcneill static struct pmu_event_table pmu_armv7 = {
90*091e7586Sjmcneill .name = "ARMv7 common architectural and microarchitectural events",
91*091e7586Sjmcneill .events = pmu_armv7_common_events,
92*091e7586Sjmcneill .nevents = __arraycount(pmu_armv7_common_events),
93*091e7586Sjmcneill .next = NULL
94*091e7586Sjmcneill };
95*091e7586Sjmcneill
96*091e7586Sjmcneill static struct pmu_event_table *events = NULL;
97*091e7586Sjmcneill
98*091e7586Sjmcneill int
tprof_event_init(uint32_t ident)99*091e7586Sjmcneill tprof_event_init(uint32_t ident)
100*091e7586Sjmcneill {
101*091e7586Sjmcneill if (ident != TPROF_IDENT_ARMV7_GENERIC)
102*091e7586Sjmcneill return -1;
103*091e7586Sjmcneill
104*091e7586Sjmcneill events = &pmu_armv7;
105*091e7586Sjmcneill
106*091e7586Sjmcneill return 0;
107*091e7586Sjmcneill }
108*091e7586Sjmcneill
109*091e7586Sjmcneill static void
tprof_event_list_table(struct pmu_event_table * tbl)110*091e7586Sjmcneill tprof_event_list_table(struct pmu_event_table *tbl)
111*091e7586Sjmcneill {
112*091e7586Sjmcneill u_int n;
113*091e7586Sjmcneill
114*091e7586Sjmcneill printf("%s:\n", tbl->name);
115*091e7586Sjmcneill for (n = 0; n < tbl->nevents; n++) {
116*091e7586Sjmcneill printf("\t%s\n", tbl->events[n].name);
117*091e7586Sjmcneill }
118*091e7586Sjmcneill
119*091e7586Sjmcneill if (tbl->next)
120*091e7586Sjmcneill tprof_event_list_table(tbl->next);
121*091e7586Sjmcneill }
122*091e7586Sjmcneill
123*091e7586Sjmcneill void
tprof_event_list(void)124*091e7586Sjmcneill tprof_event_list(void)
125*091e7586Sjmcneill {
126*091e7586Sjmcneill tprof_event_list_table(events);
127*091e7586Sjmcneill }
128*091e7586Sjmcneill
129*091e7586Sjmcneill static void
tprof_event_lookup_table(const char * name,struct tprof_param * param,struct pmu_event_table * tbl)130*091e7586Sjmcneill tprof_event_lookup_table(const char *name, struct tprof_param *param,
131*091e7586Sjmcneill struct pmu_event_table *tbl)
132*091e7586Sjmcneill {
133*091e7586Sjmcneill u_int n;
134*091e7586Sjmcneill
135*091e7586Sjmcneill for (n = 0; n < tbl->nevents; n++) {
136*091e7586Sjmcneill if (strcmp(tbl->events[n].name, name) == 0) {
137*091e7586Sjmcneill param->p_event = tbl->events[n].event;
138*091e7586Sjmcneill param->p_unit = 0;
139*091e7586Sjmcneill return;
140*091e7586Sjmcneill }
141*091e7586Sjmcneill }
142*091e7586Sjmcneill
143*091e7586Sjmcneill if (tbl->next)
144*091e7586Sjmcneill tprof_event_lookup_table(name, param, tbl->next);
145*091e7586Sjmcneill else
146*091e7586Sjmcneill errx(EXIT_FAILURE, "event '%s' unknown", name);
147*091e7586Sjmcneill }
148*091e7586Sjmcneill
149*091e7586Sjmcneill void
tprof_event_lookup(const char * name,struct tprof_param * param)150*091e7586Sjmcneill tprof_event_lookup(const char *name, struct tprof_param *param)
151*091e7586Sjmcneill {
152*091e7586Sjmcneill tprof_event_lookup_table(name, param, events);
153*091e7586Sjmcneill }
154