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
finddom(struct rnn * rnn,uint32_t regbase)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
_rnn_init(struct rnn * rnn,int nocolor)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
rnn_new(int nocolor)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
init(struct rnn * rnn,char * file,char * domain)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
rnn_load_file(struct rnn * rnn,char * file,char * domain)95 void rnn_load_file(struct rnn *rnn, char *file, char *domain)
96 {
97 init(rnn, file, domain);
98 }
99
rnn_load(struct rnn * rnn,const char * gpuname)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
rnn_regbase(struct rnn * rnn,const char * name)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
rnn_regname(struct rnn * rnn,uint32_t regbase,int color)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
rnn_reginfo(struct rnn * rnn,uint32_t regbase)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
rnn_enumname(struct rnn * rnn,const char * name,uint32_t val)144 const char *rnn_enumname(struct rnn *rnn, const char *name, uint32_t val)
145 {
146 return rnndec_decode_enum(rnn->vc, name, val);
147 }
148
regelem(struct rnndomain * domain,const char * name)149 static struct rnndelem *regelem(struct rnndomain *domain, const char *name)
150 {
151 int i;
152 for (i = 0; i < domain->subelemsnum; i++) {
153 struct rnndelem *elem = domain->subelems[i];
154 if (!strcmp(elem->name, name))
155 return elem;
156 }
157 return NULL;
158 }
159
160 /* Lookup rnndelem by name: */
rnn_regelem(struct rnn * rnn,const char * name)161 struct rnndelem *rnn_regelem(struct rnn *rnn, const char *name)
162 {
163 struct rnndelem *elem = regelem(rnn->dom[0], name);
164 if (elem)
165 return elem;
166 return regelem(rnn->dom[1], name);
167 }
168
regoff(struct rnndomain * domain,uint32_t offset)169 static struct rnndelem *regoff(struct rnndomain *domain, uint32_t offset)
170 {
171 int i;
172 for (i = 0; i < domain->subelemsnum; i++) {
173 struct rnndelem *elem = domain->subelems[i];
174 if (elem->offset == offset)
175 return elem;
176 }
177 return NULL;
178 }
179
180 /* Lookup rnndelem by offset: */
rnn_regoff(struct rnn * rnn,uint32_t offset)181 struct rnndelem *rnn_regoff(struct rnn *rnn, uint32_t offset)
182 {
183 struct rnndelem *elem = regoff(rnn->dom[0], offset);
184 if (elem)
185 return elem;
186 return regoff(rnn->dom[1], offset);
187 }
188
rnn_decodelem(struct rnn * rnn,struct rnntypeinfo * info,uint32_t regval,union rnndecval * val)189 enum rnnttype rnn_decodelem(struct rnn *rnn, struct rnntypeinfo *info,
190 uint32_t regval, union rnndecval *val)
191 {
192 val->u = regval;
193 switch (info->type) {
194 case RNN_TTYPE_INLINE_ENUM:
195 case RNN_TTYPE_ENUM:
196 case RNN_TTYPE_HEX:
197 case RNN_TTYPE_INT:
198 case RNN_TTYPE_UINT:
199 case RNN_TTYPE_FLOAT:
200 case RNN_TTYPE_BOOLEAN:
201 return info->type;
202 case RNN_TTYPE_FIXED:
203 case RNN_TTYPE_UFIXED:
204 /* TODO */
205 default:
206 return RNN_TTYPE_INVALID;
207 }
208 }
209