summaryrefslogtreecommitdiff
path: root/intel_batchbuffer_dump.c
blob: 99c2c1cfc0d3c4071aa19aa876a2fcad9c3ea41f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>

#include "intel_driver.h"
#include "intel_batchbuffer_dump.h"

#define BUFFER_FAIL(_count, _len, _name) do {			\
    fprintf(gout, "Buffer size too small in %s (%d < %d)\n",	\
	    (_name), (_count), (_len));				\
    (*failures)++;						\
    return count;						\
} while (0)

static FILE *gout;

static void
instr_out(unsigned int *data, unsigned int offset, unsigned int index, char *fmt, ...)
{
    va_list va;

    fprintf(gout, "0x%08x: 0x%08x:%s ", offset + index * 4, data[index],
	    index == 0 ? "" : "  ");
    va_start(va, fmt);
    vfprintf(gout, fmt, va);
    va_end(va);
}


static int
dump_mi(unsigned int *data, unsigned int offset, int count, unsigned int device, int *failures)
{
    unsigned int opcode;
    int length, i;

    struct {
	unsigned int opcode;
	int mask_length;
	int min_len;
	int max_len;
	char *name;
    } mi_commands[] = {
	{ 0x00, 0, 1, 1, "MI_NOOP" },
	{ 0x04, 0, 1, 1, "MI_FLUSH" },
	{ 0x0a, 0, 1, 1, "MI_BATCH_BUFFER_END" },
	{ 0x26, 0x3f, 4, 5, "MI_FLUSH_DW" },
    };

    opcode = ((data[0] & MASK_MI_OPCODE) >> SHIFT_MI_OPCODE);

    for (i = 0; i < sizeof(mi_commands) / sizeof(mi_commands[0]); i++) {
        if (opcode == mi_commands[i].opcode) {
            int index;

            length = 1;
	    instr_out(data, offset, 0, "%s\n", mi_commands[i].name);

	    if (mi_commands[i].max_len > 1) {
                length = (data[0] & mi_commands[i].mask_length) + 2;

                if (length < mi_commands[i].min_len ||
                    length > mi_commands[i].max_len) {
		    fprintf(gout, "Bad length (%d) in %s, [%d, %d]\n",
			    length, mi_commands[i].name,
			    mi_commands[i].min_len,
			    mi_commands[i].max_len);
		}
	    }

            for (index = 1; index < length; index++) {
                if (index >= count)
		    BUFFER_FAIL(count, length, mi_commands[i].name);

		instr_out(data, offset, index, "dword %d\n", index);
	    }

	    return length;
	}
    }

    instr_out(data, offset, 0, "UNKNOWN MI COMMAND\n");
    (*failures)++;
    return 1;
}

static int
dump_gfxpipe_3d(unsigned int *data, unsigned int offset, int count, unsigned int device, int *failures)
{
    instr_out(data, offset, 0, "UNKNOWN 3D COMMAND\n");
    (*failures)++;

    return 1;
}

static void
dump_avc_bsd_img_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    int img_struct = ((data[3] >> 8) & 0x3);

    instr_out(data, offset, 1, "frame size: %d\n", (data[1] & 0xffff));
    instr_out(data, offset, 2, "width: %d, height: %d\n", (data[2] & 0xff), (data[2] >> 16) & 0xff);
    instr_out(data, offset, 3, 
              "second_chroma_qp_offset: %d,"
              "chroma_qp_offset: %d,"
              "QM present flag: %d," 
              "image struct: %s,"
              "img_dec_fs_idc: %d,"
              "\n",
              (data[3] >> 24) & 0x1f,
              (data[3] >> 16) & 0x1f,
              (data[3] >> 10) & 0x1,
              (img_struct == 0) ? "frame" : (img_struct == 2) ? "invalid" : (img_struct == 1) ? "top field" : "bottom field",
              data[3] & 0xff);
    instr_out(data, offset, 4,
              "residual off: 0x%x,"
              "16MV: %d,"
              "chroma fmt: %d,"
              "CABAC: %d,"
              "non-ref: %d,"
              "constrained intra: %d,"
              "direct8x8: %d,"
              "trans8x8: %d,"
              "MB only: %d,"
              "MBAFF: %d,"
              "\n",
              (data[4] >> 24) & 0xff,
              (data[4] >> 12) & 0x1,
              (data[4] >> 10) & 0x3,
              (data[4] >> 7) & 0x1,
              (data[4] >> 6) & 0x1,
              (data[4] >> 5) & 0x1,
              (data[4] >> 4) & 0x1,
              (data[4] >> 3) & 0x1,
              (data[4] >> 2) & 0x1,
              (data[4] >> 1) & 0x1);
    instr_out(data, offset, 5, "AVC-IT Command Header\n");
}

static void
dump_avc_bsd_qm_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    unsigned int length = ((data[0] & MASK_GFXPIPE_LENGTH) >> SHIFT_GFXPIPE_LENGTH) + 2;
    int i;

    instr_out(data, offset, 1, "user default: %02x, QM list present: %02x\n", 
              (data[1] >> 8) & 0xff, data[1] & 0xff);

    for (i = 2; i < length; i++) {
        instr_out(data, offset, i, "dword %d\n", i);
    }
}

static void
dump_avc_bsd_slice_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{

}

static void
dump_avc_bsd_buf_base_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    int i;

    instr_out(data, offset, 1, "BSD row store base address\n");
    instr_out(data, offset, 2, "MPR row store base address\n");
    instr_out(data, offset, 3, "AVC-IT command buffer base address\n");
    instr_out(data, offset, 4, "AVC-IT data buffer: 0x%08x, write offset: 0x%x\n", 
              data[4] & 0xFFFFF000, data[4] & 0xFC0);
    instr_out(data, offset, 5, "ILDB data buffer\n");

    for (i = 6; i < 38; i++) {
        instr_out(data, offset, i, "Direct MV read base address for reference frame %d\n", i - 6);
    }

    instr_out(data, offset, 38, "direct mv wr0 top\n");
    instr_out(data, offset, 39, "direct mv wr0 bottom\n");

    for (i = 40; i < 74; i++) {
        instr_out(data, offset, i, "POC List %d\n", i - 40);
    }
}

static void
dump_bsd_ind_obj_base_addr(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "AVC indirect object base address\n");
    instr_out(data, offset, 2, "AVC Indirect Object Access Upper Bound\n");
}

static void 
dump_ironlake_avc_bsd_object(unsigned int *data, unsigned int offset, int *failures)
{
    int slice_type = data[3] & 0xf;
    int i, is_phantom = ((data[1] & 0x3fffff) == 0);

    if (!is_phantom) {
        instr_out(data, offset, 1, "Encrypted: %d, bitsteam length: %d\n", data[1] >> 31, data[1] & 0x3fffff);
        instr_out(data, offset, 2, "Indirect Data Start Address: %d\n", data[2] & 0x1fffffff);
        instr_out(data, offset, 3, "%s Slice\n", slice_type == 0 ? "P" : slice_type == 1 ? "B" : "I");
        instr_out(data, offset, 4, 
                  "Num_Ref_Idx_L1: %d,"
                  "Num_Ref_Idx_L0: %d,"
                  "Log2WeightDenomChroma: %d,"
                  "Log2WeightDenomLuma: %d"
                  "\n",
                  (data[4] >> 24) & 0x3f,
                  (data[4] >> 16) & 0x3f,
                  (data[4] >> 8) & 0x3,
                  (data[4] >> 0) & 0x3);
        instr_out(data, offset, 5,
                  "WeightedPredIdc: %d,"
                  "DirectPredType: %d,"
                  "DisableDeblockingFilter: %d,"
                  "CabacInitIdc: %d,"
                  "SliceQp: %d,"
                  "SliceBetaOffsetDiv2: %d,"
                  "SliceAlphaC0OffsetDiv2: %d"
                  "\n",
                  (data[5] >> 30) & 0x3,
                  (data[5] >> 29) & 0x1,
                  (data[5] >> 27) & 0x3,
                  (data[5] >> 24) & 0x3,
                  (data[5] >> 16) & 0x3f,
                  (data[5] >> 8) & 0xf,
                  (data[5] >> 0) & 0xf);
        instr_out(data, offset, 6,
                  "Slice_MB_Start_Vert_Pos: %d,"
                  "Slice_MB_Start_Hor_Pos: %d,"
                  "Slice_Start_Mb_Num: %d"
                  "\n",
                  (data[6] >> 24) & 0xff,
                  (data[6] >> 16) & 0xff,
                  (data[6] >> 0) & 0x7fff);
        instr_out(data, offset, 7,
                  "Fix_Prev_Mb_Skipped: %d,"
                  "First_MB_Bit_Offset: %d"
                  "\n",
                  (data[7] >> 7) & 0x1,
                  (data[7] >> 0) & 0x7);

        for (i = 8; i < 16; i++)
            instr_out(data, offset, i, "dword %d\n", i);
    } else {
        instr_out(data, offset, 1, "phantom slice\n");

        for (i = 2; i < 6; i++)
            instr_out(data, offset, i, "dword %d\n", i);

        instr_out(data, offset, 6,
                  "Slice_Start_Mb_Num: %d"
                  "\n",
                  (data[6] >> 0) & 0x7fff);

        for (i = 7; i < 16; i++)
            instr_out(data, offset, i, "dword %d\n", i);

    }
}

static void 
dump_g4x_avc_bsd_object(unsigned int *data, unsigned int offset, int *failures)
{

}

static void 
dump_avc_bsd_object(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    if (IS_IRONLAKE(device))
        dump_ironlake_avc_bsd_object(data, offset, failures);
    else
        dump_g4x_avc_bsd_object(data, offset, failures);
}

static int
dump_bsd_avc(unsigned int *data, unsigned int offset, int count, unsigned int device, int *failures)
{
    unsigned int subopcode;
    int length, i;

    struct {
	unsigned int subopcode;
	int min_len;
	int max_len;
	char *name;
        void (*detail)(unsigned int *data, unsigned int offset, unsigned int device, int  *failures);
    } avc_commands[] = {
        { 0x00, 0x06, 0x06, "AVC_BSD_IMG_STATE", dump_avc_bsd_img_state },
        { 0x01, 0x02, 0x3a, "AVC_BSD_QM_STATE", dump_avc_bsd_qm_state },
        { 0x02, 0x02, 0xd2, "AVC_BSD_SLICE_STATE", NULL },
        { 0x03, 0x4a, 0x4a, "AVC_BSD_BUF_BASE_STATE", dump_avc_bsd_buf_base_state },
        { 0x04, 0x03, 0x03, "BSD_IND_OBJ_BASE_ADDR", dump_bsd_ind_obj_base_addr },
        { 0x08, 0x08, 0x10, "AVC_BSD_OBJECT", dump_avc_bsd_object },
    };

    subopcode = ((data[0] & MASK_GFXPIPE_SUBOPCODE) >> SHIFT_GFXPIPE_SUBOPCODE);

    for (i = 0; i < sizeof(avc_commands) / sizeof(avc_commands[0]); i++) {
        if (subopcode == avc_commands[i].subopcode) {
            unsigned int index;

            length = (data[0] & MASK_GFXPIPE_LENGTH) >> SHIFT_GFXPIPE_LENGTH;
            length += 2;
            instr_out(data, offset, 0, "%s\n", avc_commands[i].name);

            if (length < avc_commands[i].min_len || 
                length > avc_commands[i].max_len) {
                fprintf(gout, "Bad length(%d) in %s [%d, %d]\n", 
                        length, avc_commands[i].name,
                        avc_commands[i].min_len,
                        avc_commands[i].max_len);
            }

            if (length - 1 >= count)
                BUFFER_FAIL(count, length, avc_commands[i].name);

            if (avc_commands[i].detail)
                avc_commands[i].detail(data, offset, device, failures);
            else {
                for (index = 1; index < length; index++)
                    instr_out(data, offset, index, "dword %d\n", index);
            }

	    return length;
	}
    }

    instr_out(data, offset, 0, "UNKNOWN AVC COMMAND\n");
    (*failures)++;
    return 1;
}

static int
dump_gfxpipe_bsd(unsigned int *data, unsigned int offset, int count, unsigned int device, int *failures)
{
    int length;

    switch ((data[0] & MASK_GFXPIPE_OPCODE) >> SHIFT_GFXPIPE_OPCODE) {
    case OPCODE_BSD_AVC:
        length = dump_bsd_avc(data, offset, count, device, failures);
        break;

    default:
        length = 1;
        (*failures)++;
        instr_out(data, offset, 0, "UNKNOWN BSD OPCODE\n");
        break;
    }

    return length;
}

static void
dump_mfx_mode_select(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, 
              "decoder mode: %d(%s),"
              "post deblocking output enable %d,"
              "pre deblocking output enable %d,"
              "codec select: %d(%s),"
              "standard select: %d(%s)"
              "\n",
              (data[1] >> 16) & 0x1, ((data[1] >> 16) & 0x1) ? "IT" : "VLD",
              (data[1] >> 9) & 0x1,
              (data[1] >> 8) & 0x1,
              (data[1] >> 4) & 0x1, ((data[1] >> 4) & 0x1) ? "Encode" : "Decode",
              (data[1] >> 0) & 0x3, ((data[1] >> 0) & 0x3) == 0 ? "MPEG2" :
              ((data[1] >> 0) & 0x3) == 1 ? "VC1" :
              ((data[1] >> 0) & 0x3) == 2 ? "AVC" : "Reserved");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
}

static void
dump_mfx_surface_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
    instr_out(data, offset, 4, "dword 04\n");
    instr_out(data, offset, 5, "dword 05\n");
}

static void
dump_mfx_pipe_buf_addr_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
    instr_out(data, offset, 4, "dword 04\n");
    instr_out(data, offset, 5, "dword 05\n");
    instr_out(data, offset, 6, "dword 06\n");
    instr_out(data, offset, 7, "dword 07\n");
    instr_out(data, offset, 8, "dword 08\n");
    instr_out(data, offset, 9, "dword 09\n");
    instr_out(data, offset, 10, "dword 10\n");
    instr_out(data, offset, 11, "dword 11\n");
    instr_out(data, offset, 12, "dword 12\n");
    instr_out(data, offset, 13, "dword 13\n");
    instr_out(data, offset, 14, "dword 14\n");
    instr_out(data, offset, 15, "dword 15\n");
    instr_out(data, offset, 16, "dword 16\n");
    instr_out(data, offset, 17, "dword 17\n");
    instr_out(data, offset, 18, "dword 18\n");
    instr_out(data, offset, 19, "dword 19\n");
    instr_out(data, offset, 20, "dword 20\n");
    instr_out(data, offset, 21, "dword 21\n");
    instr_out(data, offset, 22, "dword 22\n");
    instr_out(data, offset, 24, "dword 23\n");
}

static void
dump_mfx_ind_obj_base_addr_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
    instr_out(data, offset, 4, "dword 04\n");
    instr_out(data, offset, 5, "dword 05\n");
    instr_out(data, offset, 6, "dword 06\n");
    instr_out(data, offset, 7, "dword 07\n");
    instr_out(data, offset, 8, "dword 08\n");
    instr_out(data, offset, 9, "dword 09\n");
    instr_out(data, offset, 10, "dword 10\n");
}

static void
dump_mfx_bsp_buf_base_addr_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
}

static void
dump_mfx_aes_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
    instr_out(data, offset, 4, "dword 04\n");
    instr_out(data, offset, 5, "dword 05\n");
    instr_out(data, offset, 6, "dword 06\n");
}

static void
dump_mfx_state_pointer(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
}

static int
dump_mfx_common(unsigned int *data, unsigned int offset, int count, unsigned int device, int *failures)
{
    unsigned int subopcode;
    int length, i;

    struct {
	unsigned int subopcode;
	int min_len;
	int max_len;
	char *name;
        void (*detail)(unsigned int *data, unsigned int offset, unsigned int device, int  *failures);
    } mfx_common_commands[] = {
        { SUBOPCODE_MFX(0, 0), 0x04, 0x04, "MFX_PIPE_MODE_SELECT", dump_mfx_mode_select },
        { SUBOPCODE_MFX(0, 1), 0x06, 0x06, "MFX_SURFACE_STATE", dump_mfx_surface_state },
        { SUBOPCODE_MFX(0, 2), 0x18, 0x18, "MFX_PIPE_BUF_ADDR_STATE", dump_mfx_pipe_buf_addr_state },
        { SUBOPCODE_MFX(0, 3), 0x0b, 0x0b, "MFX_IND_OBJ_BASE_ADDR_STATE", dump_mfx_ind_obj_base_addr_state },
        { SUBOPCODE_MFX(0, 4), 0x04, 0x04, "MFX_BSP_BUF_BASE_ADDR_STATE", dump_mfx_bsp_buf_base_addr_state },
        { SUBOPCODE_MFX(0, 5), 0x07, 0x07, "MFX_AES_STATE", dump_mfx_aes_state },
        { SUBOPCODE_MFX(0, 6), 0x00, 0x00, "MFX_STATE_POINTER", dump_mfx_state_pointer },
    };

    subopcode = ((data[0] & MASK_GFXPIPE_SUBOPCODE) >> SHIFT_GFXPIPE_SUBOPCODE);

    for (i = 0; i < ARRAY_ELEMS(mfx_common_commands); i++) {
        if (subopcode == mfx_common_commands[i].subopcode) {
            unsigned int index;

            length = (data[0] & MASK_GFXPIPE_LENGTH) >> SHIFT_GFXPIPE_LENGTH;
            length += 2;
            instr_out(data, offset, 0, "%s\n", mfx_common_commands[i].name);

            if (length < mfx_common_commands[i].min_len || 
                length > mfx_common_commands[i].max_len) {
                fprintf(gout, "Bad length(%d) in %s [%d, %d]\n", 
                        length, mfx_common_commands[i].name,
                        mfx_common_commands[i].min_len,
                        mfx_common_commands[i].max_len);
            }

            if (length - 1 >= count)
                BUFFER_FAIL(count, length, mfx_common_commands[i].name);

            if (mfx_common_commands[i].detail)
                mfx_common_commands[i].detail(data, offset, device, failures);
            else {
                for (index = 1; index < length; index++)
                    instr_out(data, offset, index, "dword %d\n", index);
            }

	    return length;
	}
    }

    instr_out(data, offset, 0, "UNKNOWN MFX COMMON COMMAND\n");
    (*failures)++;
    return 1;
}

static void
dump_mfx_avc_img_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
    instr_out(data, offset, 4, "dword 04\n");
    instr_out(data, offset, 5, "dword 05\n");
    instr_out(data, offset, 6, "dword 06\n");
    instr_out(data, offset, 7, "dword 07\n");
    instr_out(data, offset, 8, "dword 08\n");
    instr_out(data, offset, 9, "dword 09\n");
    instr_out(data, offset, 10, "dword 10\n");
    instr_out(data, offset, 11, "dword 11\n");
    instr_out(data, offset, 12, "dword 12\n");
}

static void
dump_mfx_avc_qm_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    unsigned int length = ((data[0] & MASK_GFXPIPE_LENGTH) >> SHIFT_GFXPIPE_LENGTH) + 2;
    int i;

    instr_out(data, offset, 1, "user default: %02x, QM list present: %02x\n", 
              (data[1] >> 8) & 0xff, data[1] & 0xff);

    for (i = 2; i < length; i++) {
        instr_out(data, offset, i, "dword %d\n", i);
    }
}

static void
dump_mfx_avc_directmode_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    int i;

    for (i = 1; i < 33; i++) {
        instr_out(data, offset, i, "Direct MV Buffer Base Address for Picture %d\n", i - 1);
    }

    for (i = 33; i < 35; i++) {
        instr_out(data, offset, i, "Direct MV Buffer Base Address for Current Decoding Frame/Field\n");
    }

    for (i = 35; i < 69; i++) {
        instr_out(data, offset, i, "POC List\n");
    }
}

static void
dump_mfx_avc_slice_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
    instr_out(data, offset, 4, "dword 04\n");
    instr_out(data, offset, 5, "dword 05\n");
    instr_out(data, offset, 6, "dword 06\n");
    instr_out(data, offset, 7, "dword 07\n");
    instr_out(data, offset, 8, "dword 08\n");
    instr_out(data, offset, 9, "dword 09\n");
}

static void
dump_mfx_avc_ref_idx_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    instr_out(data, offset, 1, "dword 01\n");
    instr_out(data, offset, 2, "dword 02\n");
    instr_out(data, offset, 3, "dword 03\n");
    instr_out(data, offset, 4, "dword 04\n");
    instr_out(data, offset, 5, "dword 05\n");
    instr_out(data, offset, 6, "dword 06\n");
    instr_out(data, offset, 7, "dword 07\n");
    instr_out(data, offset, 8, "dword 08\n");
    instr_out(data, offset, 9, "dword 09\n");
}

static void
dump_mfx_avc_weightoffset_state(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    int i;

    instr_out(data, offset, 1, 
              "Weight and Offset L%d table\n",
              (data[1] >> 0) & 0x1);

    for (i = 2; i < 31; i++) {
        instr_out(data, offset, i, "dword %d\n", i);
    }
}

static void
dump_mfd_bsd_object(unsigned int *data, unsigned int offset, unsigned int device, int *failures)
{
    int is_phantom_slice = ((data[1] & 0x3fffff) == 0);

    if (is_phantom_slice) {
        instr_out(data, offset, 1, "phantom slice\n");
        instr_out(data, offset, 2, "dword 02\n");
        instr_out(data, offset, 3, "dword 03\n");
        instr_out(data, offset, 4, "dword 04\n");
        instr_out(data, offset, 5, "dword 05\n");
    } else {
        instr_out(data, offset, 1, "Indirect BSD Data Length: %d\n", data[1] & 0x3fffff);
        instr_out(data, offset, 2, "Indirect BSD Data Start Address: 0x%08x\n", data[2] & 0x1fffffff);
        instr_out(data, offset, 3, "dword 03\n");
        instr_out(data, offset, 4,
                  "First_MB_Byte_Offset of Slice Data from Slice Header: 0x%08x,"
                  "slice header skip mode: %d"
                  "\n",
                  (data[4] >> 16),
                  (data[4] >> 6) & 0x1);
        instr_out(data, offset, 5, "dword 05\n");
    }
}

static int
dump_mfx_avc(unsigned int *data, unsigned int offset, int count, unsigned int device, int *failures)
{
    unsigned int subopcode;
    int length, i;

    struct {
	unsigned int subopcode;
	int min_len;
	int max_len;
	char *name;
        void (*detail)(unsigned int *data, unsigned int offset, unsigned int device, int  *failures);
    } mfx_avc_commands[] = {
        { SUBOPCODE_MFX(0, 0), 0x0d, 0x0d, "MFX_AVC_IMG_STATE", dump_mfx_avc_img_state },
        { SUBOPCODE_MFX(0, 1), 0x02, 0x3a, "MFX_AVC_QM_STATE", dump_mfx_avc_qm_state },
        { SUBOPCODE_MFX(0, 2), 0x45, 0x45, "MFX_AVC_DIRECTMODE_STATE", dump_mfx_avc_directmode_state },
        { SUBOPCODE_MFX(0, 3), 0x0b, 0x0b, "MFX_AVC_SLICE_STATE", dump_mfx_avc_slice_state },
        { SUBOPCODE_MFX(0, 4), 0x0a, 0x0a, "MFX_AVC_REF_IDX_STATE", dump_mfx_avc_ref_idx_state },
        { SUBOPCODE_MFX(0, 5), 0x32, 0x32, "MFX_AVC_WEIGHTOFFSET_STATE", dump_mfx_avc_weightoffset_state },
        { SUBOPCODE_MFX(1, 8), 0x06, 0x06, "MFD_AVC_BSD_OBJECT", dump_mfd_bsd_object },
    };

    subopcode = ((data[0] & MASK_GFXPIPE_SUBOPCODE) >> SHIFT_GFXPIPE_SUBOPCODE);

    for (i = 0; i < ARRAY_ELEMS(mfx_avc_commands); i++) {
        if (subopcode == mfx_avc_commands[i].subopcode) {
            unsigned int index;

            length = (data[0] & MASK_GFXPIPE_LENGTH) >> SHIFT_GFXPIPE_LENGTH;
            length += 2;
            instr_out(data, offset, 0, "%s\n", mfx_avc_commands[i].name);

            if (length < mfx_avc_commands[i].min_len || 
                length > mfx_avc_commands[i].max_len) {
                fprintf(gout, "Bad length(%d) in %s [%d, %d]\n", 
                        length, mfx_avc_commands[i].name,
                        mfx_avc_commands[i].min_len,
                        mfx_avc_commands[i].max_len);
            }

            if (length - 1 >= count)
                BUFFER_FAIL(count, length, mfx_avc_commands[i].name);

            if (mfx_avc_commands[i].detail)
                mfx_avc_commands[i].detail(data, offset, device, failures);
            else {
                for (index = 1; index < length; index++)
                    instr_out(data, offset, index, "dword %d\n", index);
            }

	    return length;
	}
    }

    instr_out(data, offset, 0, "UNKNOWN MFX AVC COMMAND\n");
    (*failures)++;
    return 1;
}

static int
dump_gfxpipe_mfx(unsigned int *data, unsigned int offset, int count, unsigned int device, int *failures)
{
    int length;

    switch ((data[0] & MASK_GFXPIPE_OPCODE) >> SHIFT_GFXPIPE_OPCODE) {
    case OPCODE_MFX_COMMON:
        length = dump_mfx_common(data, offset, count, device, failures);
        break;

    case OPCODE_MFX_AVC:
        length = dump_mfx_avc(data, offset, count, device, failures);
        break;

    default:
        length = 1;
        (*failures)++;
        instr_out(data, offset, 0, "UNKNOWN MFX OPCODE\n");
        break;
    }

    return length;
}

static int
dump_gfxpipe(unsigned int *data, unsigned int offset, int count, unsigned int device, int *failures)
{
    int length;

    switch ((data[0] & MASK_GFXPIPE_SUBTYPE) >> SHIFT_GFXPIPE_SUBTYPE) {
    case GFXPIPE_3D:
        length = dump_gfxpipe_3d(data, offset, count, device, failures);
        break;

    case GFXPIPE_BSD:
        if (IS_GEN6(device))
            length = dump_gfxpipe_mfx(data, offset, count, device, failures);
        else
            length = dump_gfxpipe_bsd(data, offset, count, device, failures);

        break;

    default:
        length = 1;
        (*failures)++;
        instr_out(data, offset, 0, "UNKNOWN GFXPIPE COMMAND\n");
        break;
    }

    return length;
}

int intel_batchbuffer_dump(unsigned int *data, unsigned int offset, int count, unsigned int device)
{
    int index = 0;
    int failures = 0;

    gout = fopen("/tmp/bsd_command_dump.txt", "w+");

    while (index < count) {
	switch ((data[index] & MASK_CMD_TYPE) >> SHIFT_CMD_TYPE) {
	case CMD_TYPE_MI:
	    index += dump_mi(data + index, offset + index * 4,
                             count - index, device, &failures);
	    break;

	case CMD_TYPE_GFXPIPE:
            index += dump_gfxpipe(data + index, offset + index * 4,
                                  count - index, device, &failures);
	    break;

	default:
	    instr_out(data, offset, index, "UNKNOWN COMMAND\n");
	    failures++;
	    index++;
	    break;
	}

	fflush(gout);
    }

    fclose(gout);

    return failures;
}