ae5fe82683457e720cda77316f8a1d26d2844213
[mesa.git] / src / freedreno / decode / rnnutil.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include <err.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <assert.h>
35
36 #include "rnnutil.h"
37
38 static struct rnndomain *finddom(struct rnn *rnn, uint32_t regbase)
39 {
40 if (rnndec_checkaddr(rnn->vc, rnn->dom[0], regbase, 0))
41 return rnn->dom[0];
42 return rnn->dom[1];
43 }
44
45 void _rnn_init(struct rnn *rnn, int nocolor)
46 {
47 rnn_init();
48
49 rnn->db = rnn_newdb();
50 rnn->vc_nocolor = rnndec_newcontext(rnn->db);
51 rnn->vc_nocolor->colors = &envy_null_colors;
52 if (nocolor) {
53 rnn->vc = rnn->vc_nocolor;
54 } else {
55 rnn->vc = rnndec_newcontext(rnn->db);
56 rnn->vc->colors = &envy_def_colors;
57 }
58 }
59
60 struct rnn *rnn_new(int nocolor)
61 {
62 struct rnn *rnn = calloc(sizeof(*rnn), 1);
63
64 if (!rnn)
65 return NULL;
66
67 _rnn_init(rnn, nocolor);
68
69 return rnn;
70 }
71
72 static void init(struct rnn *rnn, char *file, char *domain)
73 {
74 /* prepare rnn stuff for lookup */
75 rnn_parsefile(rnn->db, file);
76 rnn_prepdb(rnn->db);
77 rnn->dom[0] = rnn_finddomain(rnn->db, domain);
78 if ((strcmp(domain, "A2XX") == 0) || (strcmp(domain, "A3XX") == 0)) {
79 rnn->dom[1] = rnn_finddomain(rnn->db, "AXXX");
80 } else {
81 rnn->dom[1] = rnn->dom[0];
82 }
83 if (!rnn->dom[0] && rnn->dom[1]) {
84 fprintf(stderr, "Could not find domain %s in %s\n", domain, file);
85 }
86 rnn->variant = domain;
87
88 rnndec_varadd(rnn->vc, "chip", domain);
89 if (rnn->vc != rnn->vc_nocolor)
90 rnndec_varadd(rnn->vc_nocolor, "chip", domain);
91 if (rnn->db->estatus)
92 errx(rnn->db->estatus, "failed to parse register database");
93 }
94
95 void rnn_load_file(struct rnn *rnn, char *file, char *domain)
96 {
97 init(rnn, file, domain);
98 }
99
100 void rnn_load(struct rnn *rnn, const char *gpuname)
101 {
102 if (strstr(gpuname, "a2")) {
103 init(rnn, "adreno/a2xx.xml", "A2XX");
104 } else if (strstr(gpuname, "a3")) {
105 init(rnn, "adreno/a3xx.xml", "A3XX");
106 } else if (strstr(gpuname, "a4")) {
107 init(rnn, "adreno/a4xx.xml", "A4XX");
108 } else if (strstr(gpuname, "a5")) {
109 init(rnn, "adreno/a5xx.xml", "A5XX");
110 } else if (strstr(gpuname, "a6")) {
111 init(rnn, "adreno/a6xx.xml", "A6XX");
112 }
113 }
114
115 uint32_t rnn_regbase(struct rnn *rnn, const char *name)
116 {
117 uint32_t regbase = rnndec_decodereg(rnn->vc_nocolor, rnn->dom[0], name);
118 if (!regbase)
119 regbase = rnndec_decodereg(rnn->vc_nocolor, rnn->dom[1], name);
120 return regbase;
121 }
122
123 const char *rnn_regname(struct rnn *rnn, uint32_t regbase, int color)
124 {
125 static char buf[128];
126 struct rnndecaddrinfo *info;
127
128 info = rnndec_decodeaddr(color ? rnn->vc : rnn->vc_nocolor,
129 finddom(rnn, regbase), regbase, 0);
130 if (info) {
131 strcpy(buf, info->name);
132 free(info->name);
133 free(info);
134 return buf;
135 }
136 return NULL;
137 }
138
139 struct rnndecaddrinfo *rnn_reginfo(struct rnn *rnn, uint32_t regbase)
140 {
141 return rnndec_decodeaddr(rnn->vc, finddom(rnn, regbase), regbase, 0);
142 }
143
144 const char *rnn_enumname(struct rnn *rnn, const char *name, uint32_t val)
145 {
146 struct rnndeccontext *ctx = rnn->vc;
147 struct rnnenum *en = rnn_findenum(ctx->db, name);
148 if (en) {
149 int i;
150 for (i = 0; i < en->valsnum; i++) {
151 struct rnnvalue *eval = en->vals[i];
152 if (eval->valvalid && eval->value == val &&
153 rnndec_varmatch(ctx, &eval->varinfo)) {
154 return en->vals[i]->name;
155 }
156 }
157 }
158 return NULL;
159 }
160
161 static struct rnndelem *regelem(struct rnndomain *domain, const char *name)
162 {
163 int i;
164 for (i = 0; i < domain->subelemsnum; i++) {
165 struct rnndelem *elem = domain->subelems[i];
166 if (!strcmp(elem->name, name))
167 return elem;
168 }
169 return NULL;
170 }
171
172 /* Lookup rnndelem by name: */
173 struct rnndelem *rnn_regelem(struct rnn *rnn, const char *name)
174 {
175 struct rnndelem *elem = regelem(rnn->dom[0], name);
176 if (elem)
177 return elem;
178 return regelem(rnn->dom[1], name);
179 }
180
181 static struct rnndelem *regoff(struct rnndomain *domain, uint32_t offset)
182 {
183 int i;
184 for (i = 0; i < domain->subelemsnum; i++) {
185 struct rnndelem *elem = domain->subelems[i];
186 if (elem->offset == offset)
187 return elem;
188 }
189 return NULL;
190 }
191
192 /* Lookup rnndelem by offset: */
193 struct rnndelem *rnn_regoff(struct rnn *rnn, uint32_t offset)
194 {
195 struct rnndelem *elem = regoff(rnn->dom[0], offset);
196 if (elem)
197 return elem;
198 return regoff(rnn->dom[1], offset);
199 }
200
201 enum rnnttype rnn_decodelem(struct rnn *rnn, struct rnntypeinfo *info,
202 uint32_t regval, union rnndecval *val)
203 {
204 val->u = regval;
205 switch (info->type) {
206 case RNN_TTYPE_INLINE_ENUM:
207 case RNN_TTYPE_ENUM:
208 case RNN_TTYPE_HEX:
209 case RNN_TTYPE_INT:
210 case RNN_TTYPE_UINT:
211 case RNN_TTYPE_FLOAT:
212 case RNN_TTYPE_BOOLEAN:
213 return info->type;
214 case RNN_TTYPE_FIXED:
215 case RNN_TTYPE_UFIXED:
216 /* TODO */
217 default:
218 return RNN_TTYPE_INVALID;
219 }
220 }