1 /*
2 * Copyright (C) 2017-2019 Lyude Paul
3 * Copyright (C) 2017-2019 Alyssa Rosenzweig
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #ifndef __PAN_DECODE_H__
27 #define __PAN_DECODE_H__
28
29 #include "genxml/gen_macros.h"
30 #include "util/rb_tree.h"
31 #include "util/simple_mtx.h"
32 #include "util/u_dynarray.h"
33
34 #include "wrap.h"
35
36 struct pandecode_context {
37 int id; /* only used for the filename */
38 FILE *dump_stream;
39 unsigned indent;
40 struct rb_tree mmap_tree;
41 struct util_dynarray ro_mappings;
42 int dump_frame_count;
43 simple_mtx_t lock;
44
45 /* On CSF context, set to true if the root CS ring buffer
46 * is managed in userspace. The blob does that, and mesa might use
47 * usermode queues too at some point.
48 */
49 bool usermode_queue;
50 };
51
52 void pandecode_dump_file_open(struct pandecode_context *ctx);
53
54 struct pandecode_mapped_memory {
55 struct rb_node node;
56 size_t length;
57 void *addr;
58 uint64_t gpu_va;
59 bool ro;
60 char name[32];
61 };
62
63 char *pointer_as_memory_reference(struct pandecode_context *ctx, uint64_t ptr);
64
65 struct pandecode_mapped_memory *
66 pandecode_find_mapped_gpu_mem_containing(struct pandecode_context *ctx,
67 uint64_t addr);
68
69 void pandecode_map_read_write(struct pandecode_context *ctx);
70
71 void pandecode_dump_mappings(struct pandecode_context *ctx);
72
73 static inline void *
__pandecode_fetch_gpu_mem(struct pandecode_context * ctx,uint64_t gpu_va,size_t size,int line,const char * filename)74 __pandecode_fetch_gpu_mem(struct pandecode_context *ctx, uint64_t gpu_va,
75 size_t size, int line, const char *filename)
76 {
77 const struct pandecode_mapped_memory *mem =
78 pandecode_find_mapped_gpu_mem_containing(ctx, gpu_va);
79
80 if (!mem) {
81 fprintf(stderr, "Access to unknown memory %" PRIx64 " in %s:%d\n", gpu_va,
82 filename, line);
83 assert(0);
84 }
85
86 assert(size + (gpu_va - mem->gpu_va) <= mem->length);
87
88 return mem->addr + gpu_va - mem->gpu_va;
89 }
90
91 #define pandecode_fetch_gpu_mem(ctx, gpu_va, size) \
92 __pandecode_fetch_gpu_mem(ctx, gpu_va, size, __LINE__, __FILE__)
93
94 /* Returns a validated pointer to mapped GPU memory with the given pointer type,
95 * size automatically determined from the pointer type
96 */
97 #define PANDECODE_PTR(ctx, gpu_va, type) \
98 ((type *)(__pandecode_fetch_gpu_mem(ctx, gpu_va, sizeof(type), __LINE__, \
99 __FILE__)))
100
101 /* Usage: <variable type> PANDECODE_PTR_VAR(name, gpu_va) */
102 #define PANDECODE_PTR_VAR(ctx, name, gpu_va) \
103 name = __pandecode_fetch_gpu_mem(ctx, gpu_va, sizeof(*name), __LINE__, \
104 __FILE__)
105
106 void pandecode_validate_buffer(struct pandecode_context *ctx, mali_ptr addr,
107 size_t sz);
108
109 /* Forward declare for all supported gens to permit thunking */
110 void pandecode_jc_v4(struct pandecode_context *ctx, mali_ptr jc_gpu_va,
111 unsigned gpu_id);
112 void pandecode_jc_v5(struct pandecode_context *ctx, mali_ptr jc_gpu_va,
113 unsigned gpu_id);
114 void pandecode_jc_v6(struct pandecode_context *ctx, mali_ptr jc_gpu_va,
115 unsigned gpu_id);
116 void pandecode_jc_v7(struct pandecode_context *ctx, mali_ptr jc_gpu_va,
117 unsigned gpu_id);
118 void pandecode_jc_v9(struct pandecode_context *ctx, mali_ptr jc_gpu_va,
119 unsigned gpu_id);
120
121 void pandecode_abort_on_fault_v4(struct pandecode_context *ctx,
122 mali_ptr jc_gpu_va);
123 void pandecode_abort_on_fault_v5(struct pandecode_context *ctx,
124 mali_ptr jc_gpu_va);
125 void pandecode_abort_on_fault_v6(struct pandecode_context *ctx,
126 mali_ptr jc_gpu_va);
127 void pandecode_abort_on_fault_v7(struct pandecode_context *ctx,
128 mali_ptr jc_gpu_va);
129 void pandecode_abort_on_fault_v9(struct pandecode_context *ctx,
130 mali_ptr jc_gpu_va);
131
132 void pandecode_cs_v10(struct pandecode_context *ctx, mali_ptr queue,
133 uint32_t size, unsigned gpu_id, uint32_t *regs);
134
135 /* Logging infrastructure */
136 static void
pandecode_make_indent(struct pandecode_context * ctx)137 pandecode_make_indent(struct pandecode_context *ctx)
138 {
139 for (unsigned i = 0; i < ctx->indent; ++i)
140 fprintf(ctx->dump_stream, " ");
141 }
142
143 static inline void PRINTFLIKE(2, 3)
pandecode_log(struct pandecode_context * ctx,const char * format,...)144 pandecode_log(struct pandecode_context *ctx, const char *format, ...)
145 {
146 va_list ap;
147
148 pandecode_make_indent(ctx);
149 va_start(ap, format);
150 vfprintf(ctx->dump_stream, format, ap);
151 va_end(ap);
152 }
153
154 static inline void
pandecode_log_cont(struct pandecode_context * ctx,const char * format,...)155 pandecode_log_cont(struct pandecode_context *ctx, const char *format, ...)
156 {
157 va_list ap;
158
159 va_start(ap, format);
160 vfprintf(ctx->dump_stream, format, ap);
161 va_end(ap);
162 }
163
164 /* Convenience methods */
165 #define DUMP_UNPACKED(ctx, T, var, ...) \
166 { \
167 pandecode_log(ctx, __VA_ARGS__); \
168 pan_print(ctx->dump_stream, T, var, (ctx->indent + 1) * 2); \
169 }
170
171 #define DUMP_CL(ctx, T, cl, ...) \
172 { \
173 pan_unpack(cl, T, temp); \
174 DUMP_UNPACKED(ctx, T, temp, __VA_ARGS__); \
175 }
176
177 #define DUMP_SECTION(ctx, A, S, cl, ...) \
178 { \
179 pan_section_unpack(cl, A, S, temp); \
180 pandecode_log(ctx, __VA_ARGS__); \
181 pan_section_print(ctx->dump_stream, A, S, temp, (ctx->indent + 1) * 2); \
182 }
183
184 #define MAP_ADDR(ctx, T, addr, cl) \
185 const uint8_t *cl = pandecode_fetch_gpu_mem(ctx, addr, pan_size(T));
186
187 #define DUMP_ADDR(ctx, T, addr, ...) \
188 { \
189 MAP_ADDR(ctx, T, addr, cl) \
190 DUMP_CL(ctx, T, cl, __VA_ARGS__); \
191 }
192
193 void pandecode_shader_disassemble(struct pandecode_context *ctx,
194 mali_ptr shader_ptr, unsigned gpu_id);
195
196 #ifdef PAN_ARCH
197
198 /* Information about the framebuffer passed back for additional analysis */
199 struct pandecode_fbd {
200 unsigned rt_count;
201 bool has_extra;
202 };
203
204 struct pandecode_fbd GENX(pandecode_fbd)(struct pandecode_context *ctx,
205 uint64_t gpu_va, bool is_fragment,
206 unsigned gpu_id);
207
208 #if PAN_ARCH >= 9
209 void GENX(pandecode_dcd)(struct pandecode_context *ctx,
210 const struct MALI_DRAW *p, unsigned unused,
211 unsigned gpu_id);
212 #else
213 void GENX(pandecode_dcd)(struct pandecode_context *ctx,
214 const struct MALI_DRAW *p, enum mali_job_type job_type,
215 unsigned gpu_id);
216 #endif
217
218 #if PAN_ARCH <= 5
219 void GENX(pandecode_texture)(struct pandecode_context *ctx, mali_ptr u,
220 unsigned tex);
221 #else
222 void GENX(pandecode_texture)(struct pandecode_context *ctx, const void *cl,
223 unsigned tex);
224 #endif
225
226 #if PAN_ARCH >= 5
227 mali_ptr GENX(pandecode_blend)(struct pandecode_context *ctx, void *descs,
228 int rt_no, mali_ptr frag_shader);
229 #endif
230
231 #if PAN_ARCH >= 6
232 void GENX(pandecode_tiler)(struct pandecode_context *ctx, mali_ptr gpu_va,
233 unsigned gpu_id);
234 #endif
235
236 #if PAN_ARCH >= 9
237 void GENX(pandecode_shader_environment)(struct pandecode_context *ctx,
238 const struct MALI_SHADER_ENVIRONMENT *p,
239 unsigned gpu_id);
240
241 void GENX(pandecode_resource_tables)(struct pandecode_context *ctx,
242 mali_ptr addr, const char *label);
243
244 void GENX(pandecode_fau)(struct pandecode_context *ctx, mali_ptr addr,
245 unsigned count, const char *name);
246
247 mali_ptr GENX(pandecode_shader)(struct pandecode_context *ctx, mali_ptr addr,
248 const char *label, unsigned gpu_id);
249
250 void GENX(pandecode_blend_descs)(struct pandecode_context *ctx, mali_ptr blend,
251 unsigned count, mali_ptr frag_shader,
252 unsigned gpu_id);
253
254 void GENX(pandecode_depth_stencil)(struct pandecode_context *ctx,
255 mali_ptr addr);
256 #endif
257
258 #endif
259
260 #endif /* __MMAP_TRACE_H__ */
261