Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / intel / intel_decode.c
1 /* -*- c-basic-offset: 4 -*- */
2 /*
3 * Copyright © 2007 Intel Corporation
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
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 *
24 * Authors:
25 * Eric Anholt <eric@anholt.net>
26 *
27 */
28
29 /** @file intel_decode.c
30 * This file contains code to print out batchbuffer contents in a
31 * human-readable format.
32 *
33 * The current version only supports i915 packets, and only pretty-prints a
34 * subset of them. The intention is for it to make just a best attempt to
35 * decode, but never crash in the process.
36 */
37
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <inttypes.h>
41
42 #include "intel_decode.h"
43 #include "intel_chipset.h"
44
45 #define BUFFER_FAIL(_count, _len, _name) do { \
46 fprintf(out, "Buffer size too small in %s (%d < %d)\n", \
47 (_name), (_count), (_len)); \
48 (*failures)++; \
49 return count; \
50 } while (0)
51
52 static FILE *out;
53 static uint32_t saved_s2 = 0, saved_s4 = 0;
54 static char saved_s2_set = 0, saved_s4_set = 0;
55
56 static float
57 int_as_float(uint32_t intval)
58 {
59 union intfloat {
60 uint32_t i;
61 float f;
62 } uval;
63
64 uval.i = intval;
65 return uval.f;
66 }
67
68 static void
69 instr_out(uint32_t *data, uint32_t hw_offset, unsigned int index,
70 char *fmt, ...)
71 {
72 va_list va;
73
74 fprintf(out, "0x%08x: 0x%08x:%s ", hw_offset + index * 4, data[index],
75 index == 0 ? "" : " ");
76 va_start(va, fmt);
77 vfprintf(out, fmt, va);
78 va_end(va);
79 }
80
81
82 static int
83 decode_mi(uint32_t *data, int count, uint32_t hw_offset, int *failures)
84 {
85 unsigned int opcode;
86
87 struct {
88 uint32_t opcode;
89 int min_len;
90 int max_len;
91 char *name;
92 } opcodes_mi[] = {
93 { 0x08, 1, 1, "MI_ARB_ON_OFF" },
94 { 0x0a, 1, 1, "MI_BATCH_BUFFER_END" },
95 { 0x31, 2, 2, "MI_BATCH_BUFFER_START" },
96 { 0x14, 3, 3, "MI_DISPLAY_BUFFER_INFO" },
97 { 0x04, 1, 1, "MI_FLUSH" },
98 { 0x22, 3, 3, "MI_LOAD_REGISTER_IMM" },
99 { 0x13, 2, 2, "MI_LOAD_SCAN_LINES_EXCL" },
100 { 0x12, 2, 2, "MI_LOAD_SCAN_LINES_INCL" },
101 { 0x00, 1, 1, "MI_NOOP" },
102 { 0x11, 2, 2, "MI_OVERLAY_FLIP" },
103 { 0x07, 1, 1, "MI_REPORT_HEAD" },
104 { 0x18, 2, 2, "MI_SET_CONTEXT" },
105 { 0x20, 3, 4, "MI_STORE_DATA_IMM" },
106 { 0x21, 3, 4, "MI_STORE_DATA_INDEX" },
107 { 0x24, 3, 3, "MI_STORE_REGISTER_MEM" },
108 { 0x02, 1, 1, "MI_USER_INTERRUPT" },
109 { 0x03, 1, 1, "MI_WAIT_FOR_EVENT" },
110 };
111
112
113 for (opcode = 0; opcode < sizeof(opcodes_mi) / sizeof(opcodes_mi[0]);
114 opcode++) {
115 if ((data[0] & 0x1f800000) >> 23 == opcodes_mi[opcode].opcode) {
116 unsigned int len = 1, i;
117
118 instr_out(data, hw_offset, 0, "%s\n", opcodes_mi[opcode].name);
119 if (opcodes_mi[opcode].max_len > 1) {
120 len = (data[0] & 0x000000ff) + 2;
121 if (len < opcodes_mi[opcode].min_len ||
122 len > opcodes_mi[opcode].max_len)
123 {
124 fprintf(out, "Bad length in %s\n",
125 opcodes_mi[opcode].name);
126 }
127 }
128
129 for (i = 1; i < len; i++) {
130 if (i >= count)
131 BUFFER_FAIL(count, len, opcodes_mi[opcode].name);
132 instr_out(data, hw_offset, i, "dword %d\n", i);
133 }
134
135 return len;
136 }
137 }
138
139 instr_out(data, hw_offset, 0, "MI UNKNOWN\n");
140 (*failures)++;
141 return 1;
142 }
143
144 static int
145 decode_2d(uint32_t *data, int count, uint32_t hw_offset, int *failures)
146 {
147 unsigned int opcode, len;
148 char *format = NULL;
149
150 struct {
151 uint32_t opcode;
152 int min_len;
153 int max_len;
154 char *name;
155 } opcodes_2d[] = {
156 { 0x40, 5, 5, "COLOR_BLT" },
157 { 0x43, 6, 6, "SRC_COPY_BLT" },
158 { 0x01, 8, 8, "XY_SETUP_BLT" },
159 { 0x11, 9, 9, "XY_SETUP_MONO_PATTERN_SL_BLT" },
160 { 0x03, 3, 3, "XY_SETUP_CLIP_BLT" },
161 { 0x24, 2, 2, "XY_PIXEL_BLT" },
162 { 0x25, 3, 3, "XY_SCANLINES_BLT" },
163 { 0x26, 4, 4, "Y_TEXT_BLT" },
164 { 0x31, 5, 134, "XY_TEXT_IMMEDIATE_BLT" },
165 { 0x50, 6, 6, "XY_COLOR_BLT" },
166 { 0x51, 6, 6, "XY_PAT_BLT" },
167 { 0x76, 8, 8, "XY_PAT_CHROMA_BLT" },
168 { 0x72, 7, 135, "XY_PAT_BLT_IMMEDIATE" },
169 { 0x77, 9, 137, "XY_PAT_CHROMA_BLT_IMMEDIATE" },
170 { 0x52, 9, 9, "XY_MONO_PAT_BLT" },
171 { 0x59, 7, 7, "XY_MONO_PAT_FIXED_BLT" },
172 { 0x53, 8, 8, "XY_SRC_COPY_BLT" },
173 { 0x54, 8, 8, "XY_MONO_SRC_COPY_BLT" },
174 { 0x71, 9, 137, "XY_MONO_SRC_COPY_IMMEDIATE_BLT" },
175 { 0x55, 9, 9, "XY_FULL_BLT" },
176 { 0x55, 9, 137, "XY_FULL_IMMEDIATE_PATTERN_BLT" },
177 { 0x56, 9, 9, "XY_FULL_MONO_SRC_BLT" },
178 { 0x75, 10, 138, "XY_FULL_MONO_SRC_IMMEDIATE_PATTERN_BLT" },
179 { 0x57, 12, 12, "XY_FULL_MONO_PATTERN_BLT" },
180 { 0x58, 12, 12, "XY_FULL_MONO_PATTERN_MONO_SRC_BLT" },
181 };
182
183 switch ((data[0] & 0x1fc00000) >> 22) {
184 case 0x50:
185 instr_out(data, hw_offset, 0,
186 "XY_COLOR_BLT (rgb %sabled, alpha %sabled, dst tile %d)\n",
187 (data[0] & (1 << 20)) ? "en" : "dis",
188 (data[0] & (1 << 21)) ? "en" : "dis",
189 (data[0] >> 11) & 1);
190
191 len = (data[0] & 0x000000ff) + 2;
192 if (len != 6)
193 fprintf(out, "Bad count in XY_COLOR_BLT\n");
194 if (count < 6)
195 BUFFER_FAIL(count, len, "XY_COLOR_BLT");
196
197 switch ((data[1] >> 24) & 0x3) {
198 case 0:
199 format="8";
200 break;
201 case 1:
202 format="565";
203 break;
204 case 2:
205 format="1555";
206 break;
207 case 3:
208 format="8888";
209 break;
210 }
211
212 instr_out(data, hw_offset, 1, "format %s, pitch %d, "
213 "clipping %sabled\n", format,
214 (short)(data[1] & 0xffff),
215 data[1] & (1 << 30) ? "en" : "dis");
216 instr_out(data, hw_offset, 2, "(%d,%d)\n",
217 data[2] & 0xffff, data[2] >> 16);
218 instr_out(data, hw_offset, 3, "(%d,%d)\n",
219 data[3] & 0xffff, data[3] >> 16);
220 instr_out(data, hw_offset, 4, "offset 0x%08x\n", data[4]);
221 instr_out(data, hw_offset, 5, "color\n");
222 return len;
223 case 0x53:
224 instr_out(data, hw_offset, 0,
225 "XY_SRC_COPY_BLT (rgb %sabled, alpha %sabled, "
226 "src tile %d, dst tile %d)\n",
227 (data[0] & (1 << 20)) ? "en" : "dis",
228 (data[0] & (1 << 21)) ? "en" : "dis",
229 (data[0] >> 15) & 1,
230 (data[0] >> 11) & 1);
231
232 len = (data[0] & 0x000000ff) + 2;
233 if (len != 8)
234 fprintf(out, "Bad count in XY_SRC_COPY_BLT\n");
235 if (count < 8)
236 BUFFER_FAIL(count, len, "XY_SRC_COPY_BLT");
237
238 switch ((data[1] >> 24) & 0x3) {
239 case 0:
240 format="8";
241 break;
242 case 1:
243 format="565";
244 break;
245 case 2:
246 format="1555";
247 break;
248 case 3:
249 format="8888";
250 break;
251 }
252
253 instr_out(data, hw_offset, 1, "format %s, dst pitch %d, "
254 "clipping %sabled\n", format,
255 (short)(data[1] & 0xffff),
256 data[1] & (1 << 30) ? "en" : "dis");
257 instr_out(data, hw_offset, 2, "dst (%d,%d)\n",
258 data[2] & 0xffff, data[2] >> 16);
259 instr_out(data, hw_offset, 3, "dst (%d,%d)\n",
260 data[3] & 0xffff, data[3] >> 16);
261 instr_out(data, hw_offset, 4, "dst offset 0x%08x\n", data[4]);
262 instr_out(data, hw_offset, 5, "src (%d,%d)\n",
263 data[5] & 0xffff, data[5] >> 16);
264 instr_out(data, hw_offset, 6, "src pitch %d\n",
265 (short)(data[6] & 0xffff));
266 instr_out(data, hw_offset, 7, "src offset 0x%08x\n", data[7]);
267 return len;
268 }
269
270 for (opcode = 0; opcode < sizeof(opcodes_2d) / sizeof(opcodes_2d[0]);
271 opcode++) {
272 if ((data[0] & 0x1fc00000) >> 22 == opcodes_2d[opcode].opcode) {
273 unsigned int i;
274
275 len = 1;
276 instr_out(data, hw_offset, 0, "%s\n", opcodes_2d[opcode].name);
277 if (opcodes_2d[opcode].max_len > 1) {
278 len = (data[0] & 0x000000ff) + 2;
279 if (len < opcodes_2d[opcode].min_len ||
280 len > opcodes_2d[opcode].max_len)
281 {
282 fprintf(out, "Bad count in %s\n", opcodes_2d[opcode].name);
283 }
284 }
285
286 for (i = 1; i < len; i++) {
287 if (i >= count)
288 BUFFER_FAIL(count, len, opcodes_2d[opcode].name);
289 instr_out(data, hw_offset, i, "dword %d\n", i);
290 }
291
292 return len;
293 }
294 }
295
296 instr_out(data, hw_offset, 0, "2D UNKNOWN\n");
297 (*failures)++;
298 return 1;
299 }
300
301 static int
302 decode_3d_1c(uint32_t *data, int count, uint32_t hw_offset, int *failures)
303 {
304 switch ((data[0] & 0x00f80000) >> 19) {
305 case 0x11:
306 instr_out(data, hw_offset, 0, "3DSTATE_DEPTH_SUBRECTANGLE_DISALBE\n");
307 return 1;
308 case 0x10:
309 instr_out(data, hw_offset, 0, "3DSTATE_SCISSOR_ENABLE\n");
310 return 1;
311 }
312
313 instr_out(data, hw_offset, 0, "3D UNKNOWN\n");
314 (*failures)++;
315 return 1;
316 }
317
318 static int
319 decode_3d_1d(uint32_t *data, int count, uint32_t hw_offset, int *failures)
320 {
321 unsigned int len, i, c, opcode, word, map, sampler, instr;
322
323 struct {
324 uint32_t opcode;
325 int min_len;
326 int max_len;
327 char *name;
328 } opcodes_3d_1d[] = {
329 { 0x8e, 3, 3, "3DSTATE_BUFFER_INFO" },
330 { 0x86, 4, 4, "3DSTATE_CHROMA_KEY" },
331 { 0x9c, 1, 1, "3DSTATE_CLEAR_PARAMETERS" },
332 { 0x88, 2, 2, "3DSTATE_CONSTANT_BLEND_COLOR" },
333 { 0x99, 2, 2, "3DSTATE_DEFAULT_DIFFUSE" },
334 { 0x9a, 2, 2, "3DSTATE_DEFAULT_SPECULAR" },
335 { 0x98, 2, 2, "3DSTATE_DEFAULT_Z" },
336 { 0x97, 2, 2, "3DSTATE_DEPTH_OFFSET_SCALE" },
337 { 0x85, 2, 2, "3DSTATE_DEST_BUFFER_VARIABLES" },
338 { 0x80, 5, 5, "3DSTATE_DRAWING_RECTANGLE" },
339 { 0x8e, 3, 3, "3DSTATE_BUFFER_INFO" },
340 { 0x9d, 65, 65, "3DSTATE_FILTER_COEFFICIENTS_4X4" },
341 { 0x9e, 4, 4, "3DSTATE_MONO_FILTER" },
342 { 0x89, 4, 4, "3DSTATE_FOG_MODE" },
343 { 0x8f, 2, 16, "3DSTATE_MAP_PALLETE_LOAD_32" },
344 { 0x81, 3, 3, "3DSTATE_SCISSOR_RECTANGLE" },
345 { 0x83, 2, 2, "3DSTATE_SPAN_STIPPLE" },
346 };
347
348 switch ((data[0] & 0x00ff0000) >> 16) {
349 case 0x07:
350 /* This instruction is unusual. A 0 length means just 1 DWORD instead of
351 * 2. The 0 length is specified in one place to be unsupported, but
352 * stated to be required in another, and 0 length LOAD_INDIRECTs appear
353 * to cause no harm at least.
354 */
355 instr_out(data, hw_offset, 0, "3DSTATE_LOAD_INDIRECT\n");
356 len = (data[0] & 0x000000ff) + 1;
357 i = 1;
358 if (data[0] & (0x01 << 8)) {
359 if (i + 2 >= count)
360 BUFFER_FAIL(count, len, "3DSTATE_LOAD_INDIRECT");
361 instr_out(data, hw_offset, i++, "SIS.0\n");
362 instr_out(data, hw_offset, i++, "SIS.1\n");
363 }
364 if (data[0] & (0x02 << 8)) {
365 if (i + 1 >= count)
366 BUFFER_FAIL(count, len, "3DSTATE_LOAD_INDIRECT");
367 instr_out(data, hw_offset, i++, "DIS.0\n");
368 }
369 if (data[0] & (0x04 << 8)) {
370 if (i + 2 >= count)
371 BUFFER_FAIL(count, len, "3DSTATE_LOAD_INDIRECT");
372 instr_out(data, hw_offset, i++, "SSB.0\n");
373 instr_out(data, hw_offset, i++, "SSB.1\n");
374 }
375 if (data[0] & (0x08 << 8)) {
376 if (i + 2 >= count)
377 BUFFER_FAIL(count, len, "3DSTATE_LOAD_INDIRECT");
378 instr_out(data, hw_offset, i++, "MSB.0\n");
379 instr_out(data, hw_offset, i++, "MSB.1\n");
380 }
381 if (data[0] & (0x10 << 8)) {
382 if (i + 2 >= count)
383 BUFFER_FAIL(count, len, "3DSTATE_LOAD_INDIRECT");
384 instr_out(data, hw_offset, i++, "PSP.0\n");
385 instr_out(data, hw_offset, i++, "PSP.1\n");
386 }
387 if (data[0] & (0x20 << 8)) {
388 if (i + 2 >= count)
389 BUFFER_FAIL(count, len, "3DSTATE_LOAD_INDIRECT");
390 instr_out(data, hw_offset, i++, "PSC.0\n");
391 instr_out(data, hw_offset, i++, "PSC.1\n");
392 }
393 if (len != i) {
394 fprintf(out, "Bad count in 3DSTATE_LOAD_INDIRECT\n");
395 (*failures)++;
396 return len;
397 }
398 return len;
399 case 0x04:
400 instr_out(data, hw_offset, 0, "3DSTATE_LOAD_STATE_IMMEDIATE_1\n");
401 len = (data[0] & 0x0000000f) + 2;
402 i = 1;
403 for (word = 0; word <= 7; word++) {
404 if (data[0] & (1 << (4 + word))) {
405 if (i >= count)
406 BUFFER_FAIL(count, len, "3DSTATE_LOAD_STATE_IMMEDIATE_1");
407
408 /* save vertex state for decode */
409 if (word == 2) {
410 saved_s2_set = 1;
411 saved_s2 = data[i];
412 }
413 if (word == 4) {
414 saved_s4_set = 1;
415 saved_s4 = data[i];
416 }
417
418 instr_out(data, hw_offset, i++, "S%d\n", word);
419 }
420 }
421 if (len != i) {
422 fprintf(out, "Bad count in 3DSTATE_LOAD_INDIRECT\n");
423 (*failures)++;
424 }
425 return len;
426 case 0x00:
427 instr_out(data, hw_offset, 0, "3DSTATE_MAP_STATE\n");
428 len = (data[0] & 0x0000003f) + 2;
429
430 i = 1;
431 for (map = 0; map <= 15; map++) {
432 if (data[1] & (1 << map)) {
433 if (i + 3 >= count)
434 BUFFER_FAIL(count, len, "3DSTATE_MAP_STATE");
435 instr_out(data, hw_offset, i++, "map %d MS2\n", map);
436 instr_out(data, hw_offset, i++, "map %d MS3\n", map);
437 instr_out(data, hw_offset, i++, "map %d MS4\n", map);
438 }
439 }
440 if (len != i) {
441 fprintf(out, "Bad count in 3DSTATE_MAP_STATE\n");
442 (*failures)++;
443 return len;
444 }
445 return len;
446 case 0x06:
447 instr_out(data, hw_offset, 0, "3DSTATE_PIXEL_SHADER_CONSTANTS\n");
448 len = (data[0] & 0x000000ff) + 2;
449
450 i = 1;
451 for (c = 0; c <= 31; c++) {
452 if (data[1] & (1 << c)) {
453 if (i + 4 >= count)
454 BUFFER_FAIL(count, len, "3DSTATE_PIXEL_SHADER_CONSTANTS");
455 instr_out(data, hw_offset, i, "C%d.X = %f\n",
456 c, int_as_float(data[i]));
457 i++;
458 instr_out(data, hw_offset, i, "C%d.Y = %f\n",
459 c, int_as_float(data[i]));
460 i++;
461 instr_out(data, hw_offset, i, "C%d.Z = %f\n",
462 c, int_as_float(data[i]));
463 i++;
464 instr_out(data, hw_offset, i, "C%d.W = %f\n",
465 c, int_as_float(data[i]));
466 i++;
467 }
468 }
469 if (len != i) {
470 fprintf(out, "Bad count in 3DSTATE_MAP_STATE\n");
471 (*failures)++;
472 }
473 return len;
474 case 0x05:
475 instr_out(data, hw_offset, 0, "3DSTATE_PIXEL_SHADER_PROGRAM\n");
476 len = (data[0] & 0x000000ff) + 2;
477 if ((len - 1) % 3 != 0 || len > 370) {
478 fprintf(out, "Bad count in 3DSTATE_PIXEL_SHADER_PROGRAM\n");
479 (*failures)++;
480 }
481 i = 1;
482 for (instr = 0; instr < (len - 1) / 3; instr++) {
483 if (i + 3 >= count)
484 BUFFER_FAIL(count, len, "3DSTATE_MAP_STATE");
485 instr_out(data, hw_offset, i++, "PS%03x\n", instr);
486 instr_out(data, hw_offset, i++, "PS%03x\n", instr);
487 instr_out(data, hw_offset, i++, "PS%03x\n", instr);
488 }
489 return len;
490 case 0x01:
491 instr_out(data, hw_offset, 0, "3DSTATE_SAMPLER_STATE\n");
492 len = (data[0] & 0x0000003f) + 2;
493 i = 1;
494 for (sampler = 0; sampler <= 15; sampler++) {
495 if (data[1] & (1 << sampler)) {
496 if (i + 3 >= count)
497 BUFFER_FAIL(count, len, "3DSTATE_SAMPLER_STATE");
498 instr_out(data, hw_offset, i++, "sampler %d SS2\n",
499 sampler);
500 instr_out(data, hw_offset, i++, "sampler %d SS3\n",
501 sampler);
502 instr_out(data, hw_offset, i++, "sampler %d SS4\n",
503 sampler);
504 }
505 }
506 if (len != i) {
507 fprintf(out, "Bad count in 3DSTATE_SAMPLER_STATE\n");
508 (*failures)++;
509 }
510 return len;
511 }
512
513 for (opcode = 0; opcode < sizeof(opcodes_3d_1d) / sizeof(opcodes_3d_1d[0]);
514 opcode++)
515 {
516 if (((data[0] & 0x00ff0000) >> 16) == opcodes_3d_1d[opcode].opcode) {
517 len = 1;
518
519 instr_out(data, hw_offset, 0, "%s\n", opcodes_3d_1d[opcode].name);
520 if (opcodes_3d_1d[opcode].max_len > 1) {
521 len = (data[0] & 0x0000ffff) + 2;
522 if (len < opcodes_3d_1d[opcode].min_len ||
523 len > opcodes_3d_1d[opcode].max_len)
524 {
525 fprintf(out, "Bad count in %s\n",
526 opcodes_3d_1d[opcode].name);
527 (*failures)++;
528 }
529 }
530
531 for (i = 1; i < len; i++) {
532 if (i >= count)
533 BUFFER_FAIL(count, len, opcodes_3d_1d[opcode].name);
534 instr_out(data, hw_offset, i, "dword %d\n", i);
535 }
536
537 return len;
538 }
539 }
540
541 instr_out(data, hw_offset, 0, "3D UNKNOWN\n");
542 (*failures)++;
543 return 1;
544 }
545
546 static int
547 decode_3d_primitive(uint32_t *data, int count, uint32_t hw_offset,
548 int *failures)
549 {
550 char immediate = (data[0] & (1 << 23)) == 0;
551 unsigned int len, i;
552 char *primtype;
553
554 switch ((data[0] >> 18) & 0xf) {
555 case 0x0: primtype = "TRILIST"; break;
556 case 0x1: primtype = "TRISTRIP"; break;
557 case 0x2: primtype = "TRISTRIP_REVERSE"; break;
558 case 0x3: primtype = "TRIFAN"; break;
559 case 0x4: primtype = "POLYGON"; break;
560 case 0x5: primtype = "LINELIST"; break;
561 case 0x6: primtype = "LINESTRIP"; break;
562 case 0x7: primtype = "RECTLIST"; break;
563 case 0x8: primtype = "POINTLIST"; break;
564 case 0x9: primtype = "DIB"; break;
565 case 0xa: primtype = "CLEAR_RECT"; break;
566 default: primtype = "unknown"; break;
567 }
568
569 /* XXX: 3DPRIM_DIB not supported */
570 if (immediate) {
571 len = (data[0] & 0x0003ffff) + 2;
572 instr_out(data, hw_offset, 0, "3DPRIMITIVE inline %s\n", primtype);
573 if (count < len)
574 BUFFER_FAIL(count, len, "3DPRIMITIVE inline");
575 if (!saved_s2_set || !saved_s4_set) {
576 fprintf(out, "unknown vertex format\n");
577 for (i = 1; i < len; i++) {
578 instr_out(data, hw_offset, i,
579 " vertex data (%f float)\n",
580 int_as_float(data[i]));
581 }
582 } else {
583 unsigned int vertex = 0;
584 for (i = 1; i < len;) {
585 unsigned int tc;
586
587 #define VERTEX_OUT(fmt, ...) do { \
588 if (i < len) \
589 instr_out(data, hw_offset, i, " V%d."fmt"\n", vertex, __VA_ARGS__); \
590 else \
591 fprintf(out, " missing data in V%d\n", vertex); \
592 i++; \
593 } while (0)
594
595 VERTEX_OUT("X = %f", int_as_float(data[i]));
596 VERTEX_OUT("Y = %f", int_as_float(data[i]));
597 switch (saved_s4 >> 6 & 0x7) {
598 case 0x1:
599 VERTEX_OUT("Z = %f", int_as_float(data[i]));
600 break;
601 case 0x2:
602 VERTEX_OUT("Z = %f", int_as_float(data[i]));
603 VERTEX_OUT("W = %f", int_as_float(data[i]));
604 break;
605 case 0x3:
606 break;
607 case 0x4:
608 VERTEX_OUT("W = %f", int_as_float(data[i]));
609 break;
610 default:
611 fprintf(out, "bad S4 position mask\n");
612 }
613
614 if (saved_s4 & (1 << 10)) {
615 VERTEX_OUT("color = (A=0x%02x, R=0x%02x, G=0x%02x, "
616 "B=0x%02x)",
617 data[i] >> 24,
618 (data[i] >> 16) & 0xff,
619 (data[i] >> 8) & 0xff,
620 data[i] & 0xff);
621 }
622 if (saved_s4 & (1 << 11)) {
623 VERTEX_OUT("spec = (A=0x%02x, R=0x%02x, G=0x%02x, "
624 "B=0x%02x)",
625 data[i] >> 24,
626 (data[i] >> 16) & 0xff,
627 (data[i] >> 8) & 0xff,
628 data[i] & 0xff);
629 }
630 if (saved_s4 & (1 << 12))
631 VERTEX_OUT("width = 0x%08x)", data[i]);
632
633 for (tc = 0; tc <= 7; tc++) {
634 switch ((saved_s2 >> (tc * 4)) & 0xf) {
635 case 0x0:
636 VERTEX_OUT("T%d.X = %f", tc, int_as_float(data[i]));
637 VERTEX_OUT("T%d.Y = %f", tc, int_as_float(data[i]));
638 break;
639 case 0x1:
640 VERTEX_OUT("T%d.X = %f", tc, int_as_float(data[i]));
641 VERTEX_OUT("T%d.Y = %f", tc, int_as_float(data[i]));
642 VERTEX_OUT("T%d.Z = %f", tc, int_as_float(data[i]));
643 break;
644 case 0x2:
645 VERTEX_OUT("T%d.X = %f", tc, int_as_float(data[i]));
646 VERTEX_OUT("T%d.Y = %f", tc, int_as_float(data[i]));
647 VERTEX_OUT("T%d.Z = %f", tc, int_as_float(data[i]));
648 VERTEX_OUT("T%d.W = %f", tc, int_as_float(data[i]));
649 break;
650 case 0x3:
651 VERTEX_OUT("T%d.X = %f", tc, int_as_float(data[i]));
652 break;
653 case 0x4:
654 VERTEX_OUT("T%d.XY = 0x%08x half-float", tc, data[i]);
655 break;
656 case 0x5:
657 VERTEX_OUT("T%d.XY = 0x%08x half-float", tc, data[i]);
658 VERTEX_OUT("T%d.ZW = 0x%08x half-float", tc, data[i]);
659 break;
660 case 0xf:
661 break;
662 default:
663 fprintf(out, "bad S2.T%d format\n", tc);
664 }
665 }
666 vertex++;
667 }
668 }
669 } else {
670 /* indirect vertices */
671 len = data[0] & 0x0000ffff; /* index count */
672 if (data[0] & (1 << 17)) {
673 /* random vertex access */
674 if (count < (len + 1) / 2 + 1) {
675 BUFFER_FAIL(count, (len + 1) / 2 + 1,
676 "3DPRIMITIVE random indirect");
677 }
678 instr_out(data, hw_offset, 0,
679 "3DPRIMITIVE random indirect %s (%d)\n", primtype, len);
680 if (len == 0) {
681 /* vertex indices continue until 0xffff is found */
682 for (i = 1; i < count; i++) {
683 if ((data[i] & 0xffff) == 0xffff) {
684 instr_out(data, hw_offset, i,
685 " indices: (terminator)\n");
686 return i;
687 } else if ((data[i] >> 16) == 0xffff) {
688 instr_out(data, hw_offset, i,
689 " indices: 0x%04x, "
690 "(terminator)\n",
691 data[i] & 0xffff);
692 return i;
693 } else {
694 instr_out(data, hw_offset, i,
695 " indices: 0x%04x, 0x%04x\n",
696 data[i] & 0xffff, data[i] >> 16);
697 }
698 }
699 fprintf(out,
700 "3DPRIMITIVE: no terminator found in index buffer\n");
701 (*failures)++;
702 return count;
703 } else {
704 /* fixed size vertex index buffer */
705 for (i = 0; i < len; i += 2) {
706 if (i * 2 == len - 1) {
707 instr_out(data, hw_offset, i,
708 " indices: 0x%04x\n",
709 data[i] & 0xffff);
710 } else {
711 instr_out(data, hw_offset, i,
712 " indices: 0x%04x, 0x%04x\n",
713 data[i] & 0xffff, data[i] >> 16);
714 }
715 }
716 }
717 return (len + 1) / 2 + 1;
718 } else {
719 /* sequential vertex access */
720 if (count < 2)
721 BUFFER_FAIL(count, 2, "3DPRIMITIVE seq indirect");
722 instr_out(data, hw_offset, 0,
723 "3DPRIMITIVE sequential indirect %s, %d starting from "
724 "%d\n", primtype, len, data[1] & 0xffff);
725 instr_out(data, hw_offset, 1, " start\n");
726 return 2;
727 }
728 }
729
730 return len;
731 }
732
733 static int
734 decode_3d(uint32_t *data, int count, uint32_t hw_offset, int *failures)
735 {
736 unsigned int opcode;
737
738 struct {
739 uint32_t opcode;
740 int min_len;
741 int max_len;
742 char *name;
743 } opcodes_3d[] = {
744 { 0x06, 1, 1, "3DSTATE_ANTI_ALIASING" },
745 { 0x08, 1, 1, "3DSTATE_BACKFACE_STENCIL_OPS" },
746 { 0x09, 1, 1, "3DSTATE_BACKFACE_STENCIL_MASKS" },
747 { 0x16, 1, 1, "3DSTATE_COORD_SET_BINDINGS" },
748 { 0x15, 1, 1, "3DSTATE_FOG_COLOR" },
749 { 0x0b, 1, 1, "3DSTATE_INDEPENDENT_ALPHA_BLEND" },
750 { 0x0d, 1, 1, "3DSTATE_MODES_4" },
751 { 0x0c, 1, 1, "3DSTATE_MODES_5" },
752 { 0x07, 1, 1, "3DSTATE_RASTERIZATION_RULES" },
753 };
754
755 switch ((data[0] & 0x1f000000) >> 24) {
756 case 0x1f:
757 return decode_3d_primitive(data, count, hw_offset, failures);
758 case 0x1d:
759 return decode_3d_1d(data, count, hw_offset, failures);
760 case 0x1c:
761 return decode_3d_1c(data, count, hw_offset, failures);
762 }
763
764 for (opcode = 0; opcode < sizeof(opcodes_3d) / sizeof(opcodes_3d[0]);
765 opcode++) {
766 if ((data[0] & 0x1f000000) >> 24 == opcodes_3d[opcode].opcode) {
767 unsigned int len = 1, i;
768
769 instr_out(data, hw_offset, 0, "%s\n", opcodes_3d[opcode].name);
770 if (opcodes_3d[opcode].max_len > 1) {
771 len = (data[0] & 0xff) + 2;
772 if (len < opcodes_3d[opcode].min_len ||
773 len > opcodes_3d[opcode].max_len)
774 {
775 fprintf(out, "Bad count in %s\n", opcodes_3d[opcode].name);
776 }
777 }
778
779 for (i = 1; i < len; i++) {
780 if (i >= count)
781 BUFFER_FAIL(count, len, opcodes_3d[opcode].name);
782 instr_out(data, hw_offset, i, "dword %d\n", i);
783 }
784 return len;
785 }
786 }
787
788 instr_out(data, hw_offset, 0, "3D UNKNOWN\n");
789 (*failures)++;
790 return 1;
791 }
792
793 static const char *
794 get_965_surfacetype(unsigned int surfacetype)
795 {
796 switch (surfacetype) {
797 case 0: return "1D";
798 case 1: return "2D";
799 case 2: return "3D";
800 case 3: return "CUBE";
801 case 4: return "BUFFER";
802 case 7: return "NULL";
803 default: return "unknown";
804 }
805 }
806
807 static const char *
808 get_965_depthformat(unsigned int depthformat)
809 {
810 switch (depthformat) {
811 case 0: return "s8_z24float";
812 case 1: return "z32float";
813 case 2: return "z24s8";
814 case 5: return "z16";
815 default: return "unknown";
816 }
817 }
818
819 static int
820 decode_3d_965(uint32_t *data, int count, uint32_t hw_offset, int *failures)
821 {
822 unsigned int opcode, len;
823
824 struct {
825 uint32_t opcode;
826 int min_len;
827 int max_len;
828 char *name;
829 } opcodes_3d[] = {
830 { 0x6000, 3, 3, "URB_FENCE" },
831 { 0x6001, 2, 2, "CS_URB_STATE" },
832 { 0x6002, 2, 2, "CONSTANT_BUFFER" },
833 { 0x6101, 6, 6, "STATE_BASE_ADDRESS" },
834 { 0x6102, 2, 2 , "STATE_SIP" },
835 { 0x6104, 1, 1, "3DSTATE_PIPELINE_SELECT" },
836 { 0x680b, 1, 1, "3DSTATE_VF_STATISTICS" },
837 { 0x6904, 1, 1, "3DSTATE_PIPELINE_SELECT" },
838 { 0x7800, 7, 7, "3DSTATE_PIPELINED_POINTERS" },
839 { 0x7801, 6, 6, "3DSTATE_BINDING_TABLE_POINTERS" },
840 { 0x780b, 1, 1, "3DSTATE_VF_STATISTICS" },
841 { 0x7808, 5, 257, "3DSTATE_VERTEX_BUFFERS" },
842 { 0x7809, 3, 256, "3DSTATE_VERTEX_ELEMENTS" },
843 /* 0x7808: 3DSTATE_VERTEX_BUFFERS */
844 /* 0x7809: 3DSTATE_VERTEX_ELEMENTS */
845 { 0x7900, 4, 4, "3DSTATE_DRAWING_RECTANGLE" },
846 { 0x7901, 5, 5, "3DSTATE_CONSTANT_COLOR" },
847 { 0x7905, 5, 7, "3DSTATE_DEPTH_BUFFER" },
848 { 0x7906, 2, 2, "3DSTATE_POLY_STIPPLE_OFFSET" },
849 { 0x7907, 33, 33, "3DSTATE_POLY_STIPPLE_PATTERN" },
850 { 0x7908, 3, 3, "3DSTATE_LINE_STIPPLE" },
851 { 0x7909, 2, 2, "3DSTATE_GLOBAL_DEPTH_OFFSET_CLAMP" },
852 { 0x790a, 3, 3, "3DSTATE_AA_LINE_PARAMETERS" },
853 { 0x7b00, 6, 6, "3DPRIMITIVE" },
854 };
855
856 len = (data[0] & 0x0000ffff) + 2;
857
858 switch ((data[0] & 0xffff0000) >> 16) {
859 case 0x6101:
860 if (len != 6)
861 fprintf(out, "Bad count in STATE_BASE_ADDRESS\n");
862 if (count < 6)
863 BUFFER_FAIL(count, len, "STATE_BASE_ADDRESS");
864
865 instr_out(data, hw_offset, 0,
866 "STATE_BASE_ADDRESS\n");
867
868 if (data[1] & 1) {
869 instr_out(data, hw_offset, 1, "General state at 0x%08x\n",
870 data[1] & ~1);
871 } else
872 instr_out(data, hw_offset, 1, "General state not updated\n");
873
874 if (data[2] & 1) {
875 instr_out(data, hw_offset, 2, "Surface state at 0x%08x\n",
876 data[2] & ~1);
877 } else
878 instr_out(data, hw_offset, 2, "Surface state not updated\n");
879
880 if (data[3] & 1) {
881 instr_out(data, hw_offset, 3, "Indirect state at 0x%08x\n",
882 data[3] & ~1);
883 } else
884 instr_out(data, hw_offset, 3, "Indirect state not updated\n");
885
886 if (data[4] & 1) {
887 instr_out(data, hw_offset, 4, "General state upper bound 0x%08x\n",
888 data[4] & ~1);
889 } else
890 instr_out(data, hw_offset, 4, "General state not updated\n");
891
892 if (data[5] & 1) {
893 instr_out(data, hw_offset, 5, "Indirect state upper bound 0x%08x\n",
894 data[5] & ~1);
895 } else
896 instr_out(data, hw_offset, 5, "Indirect state not updated\n");
897
898 return len;
899 case 0x7800:
900 if (len != 7)
901 fprintf(out, "Bad count in 3DSTATE_PIPELINED_POINTERS\n");
902 if (count < 7)
903 BUFFER_FAIL(count, len, "3DSTATE_PIPELINED_POINTERS");
904
905 instr_out(data, hw_offset, 0,
906 "3DSTATE_PIPELINED_POINTERS\n");
907 instr_out(data, hw_offset, 1, "VS state\n");
908 instr_out(data, hw_offset, 2, "GS state\n");
909 instr_out(data, hw_offset, 3, "Clip state\n");
910 instr_out(data, hw_offset, 4, "SF state\n");
911 instr_out(data, hw_offset, 5, "WM state\n");
912 instr_out(data, hw_offset, 6, "CC state\n");
913 return len;
914 case 0x7801:
915 if (len != 6)
916 fprintf(out, "Bad count in 3DSTATE_BINDING_TABLE_POINTERS\n");
917 if (count < 6)
918 BUFFER_FAIL(count, len, "3DSTATE_BINDING_TABLE_POINTERS");
919
920 instr_out(data, hw_offset, 0,
921 "3DSTATE_BINDING_TABLE_POINTERS\n");
922 instr_out(data, hw_offset, 1, "VS binding table\n");
923 instr_out(data, hw_offset, 2, "GS binding table\n");
924 instr_out(data, hw_offset, 3, "Clip binding table\n");
925 instr_out(data, hw_offset, 4, "SF binding table\n");
926 instr_out(data, hw_offset, 5, "WM binding table\n");
927
928 return len;
929
930 case 0x7900:
931 if (len != 4)
932 fprintf(out, "Bad count in 3DSTATE_DRAWING_RECTANGLE\n");
933 if (count < 4)
934 BUFFER_FAIL(count, len, "3DSTATE_DRAWING_RECTANGLE");
935
936 instr_out(data, hw_offset, 0,
937 "3DSTATE_DRAWING_RECTANGLE\n");
938 instr_out(data, hw_offset, 1, "top left: %d,%d\n",
939 data[1] & 0xffff,
940 (data[1] >> 16) & 0xffff);
941 instr_out(data, hw_offset, 2, "bottom right: %d,%d\n",
942 data[2] & 0xffff,
943 (data[2] >> 16) & 0xffff);
944 instr_out(data, hw_offset, 3, "origin: %d,%d\n",
945 (int)data[3] & 0xffff,
946 ((int)data[3] >> 16) & 0xffff);
947
948 return len;
949
950 case 0x7905:
951 if (len != 5)
952 fprintf(out, "Bad count in 3DSTATE_DEPTH_BUFFER\n");
953 if (count < 5)
954 BUFFER_FAIL(count, len, "3DSTATE_DEPTH_BUFFER");
955
956 instr_out(data, hw_offset, 0,
957 "3DSTATE_DEPTH_BUFFER\n");
958 instr_out(data, hw_offset, 1, "%s, %s, pitch = %d bytes, %stiled\n",
959 get_965_surfacetype(data[1] >> 29),
960 get_965_depthformat((data[1] >> 18) & 0x7),
961 (data[1] & 0x0001ffff) + 1,
962 data[1] & (1 << 27) ? "" : "not ");
963 instr_out(data, hw_offset, 2, "depth offset\n");
964 instr_out(data, hw_offset, 3, "%dx%d\n",
965 ((data[3] & 0x0007ffc0) >> 6) + 1,
966 ((data[3] & 0xfff80000) >> 19) + 1);
967 instr_out(data, hw_offset, 4, "volume depth\n");
968
969 return len;
970 }
971
972 for (opcode = 0; opcode < sizeof(opcodes_3d) / sizeof(opcodes_3d[0]);
973 opcode++) {
974 if ((data[0] & 0xffff0000) >> 16 == opcodes_3d[opcode].opcode) {
975 unsigned int i;
976 len = 1;
977
978 instr_out(data, hw_offset, 0, "%s\n", opcodes_3d[opcode].name);
979 if (opcodes_3d[opcode].max_len > 1) {
980 len = (data[0] & 0xff) + 2;
981 if (len < opcodes_3d[opcode].min_len ||
982 len > opcodes_3d[opcode].max_len)
983 {
984 fprintf(out, "Bad count in %s\n", opcodes_3d[opcode].name);
985 }
986 }
987
988 for (i = 1; i < len; i++) {
989 if (i >= count)
990 BUFFER_FAIL(count, len, opcodes_3d[opcode].name);
991 instr_out(data, hw_offset, i, "dword %d\n", i);
992 }
993 return len;
994 }
995 }
996
997 instr_out(data, hw_offset, 0, "3D UNKNOWN\n");
998 (*failures)++;
999 return 1;
1000 }
1001
1002 /**
1003 * Decodes an i830-i915 batch buffer, writing the output to stdout.
1004 *
1005 * \param data batch buffer contents
1006 * \param count number of DWORDs to decode in the batch buffer
1007 * \param hw_offset hardware address for the buffer
1008 */
1009 int
1010 intel_decode(uint32_t *data, int count, uint32_t hw_offset, uint32_t devid)
1011 {
1012 int index = 0;
1013 int failures = 0;
1014
1015 out = stderr;
1016
1017 while (index < count) {
1018 switch ((data[index] & 0xe0000000) >> 29) {
1019 case 0x0:
1020 index += decode_mi(data + index, count - index,
1021 hw_offset + index * 4, &failures);
1022 break;
1023 case 0x2:
1024 index += decode_2d(data + index, count - index,
1025 hw_offset + index * 4, &failures);
1026 break;
1027 case 0x3:
1028 if (IS_965(devid)) {
1029 index += decode_3d_965(data + index, count - index,
1030 hw_offset + index * 4, &failures);
1031 } else {
1032 index += decode_3d(data + index, count - index,
1033 hw_offset + index * 4, &failures);
1034 }
1035 break;
1036 default:
1037 instr_out(data, hw_offset, index, "UNKNOWN\n");
1038 failures++;
1039 index++;
1040 break;
1041 }
1042 fflush(out);
1043 }
1044
1045 return failures;
1046 }
1047
1048 void intel_decode_context_reset(void)
1049 {
1050 saved_s2_set = 0;
1051 saved_s4_set = 1;
1052 }
1053