summaryrefslogtreecommitdiff
path: root/drivers/mxc/amd-gpu/common/gsl_debug_pm4.c
blob: 847df8dbe386658727b922ec6f21a8212291bbc8 (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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
/* Copyright (c) 2008-2010, Advanced Micro Devices. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 */

#include "gsl.h"
#include "gsl_hal.h"

#if defined(_WIN32) && defined (GSL_BLD_YAMATO)

#include <stdio.h>
#include <string.h>
#include <stdarg.h>

//#define PM4_DEBUG_USE_MEMBUF

#ifdef PM4_DEBUG_USE_MEMBUF

#define MEMBUF_SIZE 100000
#define BUFFER_END_MARGIN 1000
char memBuf[MEMBUF_SIZE];
static int writePtr = 0;
static unsigned int lineNumber = 0;
//#define fprintf(A,...); writePtr += sprintf( memBuf+writePtr, __VA_ARGS__ ); sprintf( memBuf+writePtr, "###" ); if( writePtr > MEMBUF_SIZE-BUFFER_END_MARGIN ) { memset(memBuf+writePtr, '#', MEMBUF_SIZE-writePtr); writePtr = 0; }
#define FILE char
#define fopen(X,Y) 0
#define fclose(X)

int printString( FILE *_File, const char * _Format, ...)
{
    int ret;
    va_list ap;
    (void)_File;

    va_start(ap, _Format);
    if( writePtr > 0 && memBuf[writePtr-1] == '\n' )
    {
        // Add line number if last written character was newline
        writePtr += sprintf( memBuf+writePtr, "%d: ", lineNumber++ );
    }
    ret = vsprintf(memBuf+writePtr, _Format, ap);
    writePtr += ret;
    sprintf( memBuf+writePtr, "###" );
    if( writePtr > MEMBUF_SIZE-BUFFER_END_MARGIN ) 
    { 
        memset(memBuf+writePtr, '#', MEMBUF_SIZE-writePtr); 
        writePtr = 0;
    }

    va_end(ap);

    return ret;
}

#else

int printString( FILE *_File, const char * _Format, ...)
{
    int ret;
    va_list ap;
    va_start(ap, _Format);
    ret = vfprintf(_File, _Format, ap);
    va_end(ap);
    fflush(_File);
    return ret;
}

#endif

#ifndef    _WIN32_WCE
#define PM4_DUMPFILE    "pm4dump.txt"
#else
#define PM4_DUMPFILE    "\\Release\\pm4dump.txt"
#endif

//////////////////////////////////////////////////////////////////////////////
// defines
//////////////////////////////////////////////////////////////////////////////
#define EXPAND_OPCODE(opcode) ((opcode << 8) | PM4_PKT_MASK)

#define GetString_uint                       GetString_int
#define GetString_fixed12_4(val, szValue)    GetString_fixed(val, 12, 4, szValue)
#define GetString_signedint15(val, szValue)  GetString_signedint(val, 15, szValue)

// Need a prototype for this function
void WritePM4Packet_Type3(FILE* pFile, unsigned int dwHeader, unsigned int** ppBuffer);

static int indirectionLevel = 0;


//////////////////////////////////////////////////////////////////////////////
// functions
//////////////////////////////////////////////////////////////////////////////

void WriteDWORD(FILE* pFile, unsigned int dwValue)
{
    printString(pFile, "    0x%08x", dwValue);
}

void WriteDWORD2(FILE* pFile, unsigned int dwValue)
{
    printString(pFile, "    0x%08x\n", dwValue);
}

//----------------------------------------------------------------------------

// Generate the GetString_## functions for enumerated types
#define START_ENUMTYPE(__type)                      \
void GetString_##__type(unsigned int val, char* szValue)   \
{                                                   \
    switch(val)                                     \
    {

#define GENERATE_ENUM(__enumname, __val)            \
        case __val:                                 \
            kos_strcpy(szValue, #__enumname);        \
            break;

#define END_ENUMTYPE(__type)                        \
        default:                                    \
            sprintf(szValue, "Unknown: %d", val);   \
            break;                                  \
    }                                               \
}

#include _YAMATO_GENENUM_H

//----------------------------------------------------------------------------

void 
GetString_hex(unsigned int val, char* szValue)
{
    sprintf(szValue, "0x%x", val);
}

//----------------------------------------------------------------------------

void 
GetString_float(unsigned int val, char* szValue)
{
    float fval = *((float*) &val);
    sprintf(szValue, "%.4f", fval);
}

//----------------------------------------------------------------------------

void 
GetString_bool(unsigned int val, char* szValue)
{
    if (val)
    {
        kos_strcpy(szValue, "TRUE");
    }
    else
    {
        kos_strcpy(szValue, "FALSE");
    }
}

//----------------------------------------------------------------------------

void GetString_int(unsigned int val, char* szValue)
{
    sprintf(szValue, "%d", val);
}

//----------------------------------------------------------------------------

void 
GetString_intMinusOne(unsigned int val, char* szValue)
{
    sprintf(szValue, "%d+1", val);
}

//----------------------------------------------------------------------------

void 
GetString_signedint(unsigned int val, unsigned int dwNumBits, char* szValue)
{
    int nValue = val;

    if (val & (1<<(dwNumBits-1)))
    {
        nValue |= 0xffffffff << dwNumBits;
    }

    sprintf(szValue, "%d", nValue);
}

//----------------------------------------------------------------------------

void 
GetString_fixed(unsigned int val, unsigned int dwNumInt, unsigned int dwNumFrac, char* szValue)
{

   (void) dwNumInt;     // unreferenced formal parameter

    if (val>>dwNumFrac == 0)
    {
        // Integer part is 0 - just print out the fractional part
        sprintf(szValue, "%d/%d",
            val&((1<<dwNumFrac)-1),
            1<<dwNumFrac);
    }
    else
    {
        // Print out as a mixed fraction
        sprintf(szValue, "%d %d/%d",
            val>>dwNumFrac,
            val&((1<<dwNumFrac)-1),
            1<<dwNumFrac);
    }
}

//----------------------------------------------------------------------------

void 
GetString_Register(unsigned int dwBaseIndex, unsigned int dwValue, char* pszString)
{
    char szValue[64];
    char szField[128];

    // Empty the string
    pszString[0] = '\0';

    switch(dwBaseIndex)
    {
#define START_REGISTER(__reg)               \
        case mm##__reg:                     \
            {                               \
                reg##__reg reg;             \
                reg.u32All = dwValue;       \
                strcat(pszString, #__reg ", (");

#define GENERATE_FIELD(__name, __type)                                \
                GetString_##__type(reg.bitfields.__name, szValue);    \
                sprintf(szField, #__name " = %s, ", szValue);         \
                strcat(pszString, szField);

#define END_REGISTER(__reg)                           \
                pszString[strlen(pszString)-2]='\0';  \
                strcat(pszString, ")");               \
            }                                         \
            break;

#include _YAMATO_GENREG_H

        default:
            break;
    }
}

//----------------------------------------------------------------------------

void 
GetString_Type3Opcode(unsigned int opcode, char* pszValue)
{
switch(EXPAND_OPCODE(opcode))
    {
#define TYPE3SWITCH(__opcode)                  \
        case PM4_PACKET3_##__opcode:           \
            kos_strcpy(pszValue, #__opcode);    \
            break;

        TYPE3SWITCH(NOP)
        TYPE3SWITCH(IB_PREFETCH_END)
        TYPE3SWITCH(SUBBLK_PREFETCH)

        TYPE3SWITCH(INSTR_PREFETCH)
        TYPE3SWITCH(REG_RMW)
        TYPE3SWITCH(DRAW_INDX)
        TYPE3SWITCH(VIZ_QUERY)
        TYPE3SWITCH(SET_STATE)
        TYPE3SWITCH(WAIT_FOR_IDLE)
        TYPE3SWITCH(IM_LOAD)
        TYPE3SWITCH(IM_LOAD_IMMEDIATE)
        TYPE3SWITCH(SET_CONSTANT)
        TYPE3SWITCH(LOAD_CONSTANT_CONTEXT)
        TYPE3SWITCH(LOAD_ALU_CONSTANT)

        TYPE3SWITCH(DRAW_INDX_BIN)
        TYPE3SWITCH(3D_DRAW_INDX_2_BIN)
        TYPE3SWITCH(3D_DRAW_INDX_2)
        TYPE3SWITCH(INDIRECT_BUFFER_PFD)
        TYPE3SWITCH(INVALIDATE_STATE)
        TYPE3SWITCH(WAIT_REG_MEM)
        TYPE3SWITCH(MEM_WRITE)
        TYPE3SWITCH(REG_TO_MEM)
        TYPE3SWITCH(INDIRECT_BUFFER)

        TYPE3SWITCH(CP_INTERRUPT)
        TYPE3SWITCH(COND_EXEC)
        TYPE3SWITCH(COND_WRITE)
        TYPE3SWITCH(EVENT_WRITE)
        TYPE3SWITCH(INSTR_MATCH)
        TYPE3SWITCH(ME_INIT)
        TYPE3SWITCH(CONST_PREFETCH)
        TYPE3SWITCH(MEM_WRITE_CNTR)

        TYPE3SWITCH(SET_BIN_MASK)
        TYPE3SWITCH(SET_BIN_SELECT)
        TYPE3SWITCH(WAIT_REG_EQ)
        TYPE3SWITCH(WAIT_REG_GTE)
        TYPE3SWITCH(INCR_UPDT_STATE)
        TYPE3SWITCH(INCR_UPDT_CONST)
        TYPE3SWITCH(INCR_UPDT_INSTR)
        TYPE3SWITCH(EVENT_WRITE_SHD)
        TYPE3SWITCH(EVENT_WRITE_CFL)
        TYPE3SWITCH(EVENT_WRITE_ZPD)
        TYPE3SWITCH(WAIT_UNTIL_READ)
        TYPE3SWITCH(WAIT_IB_PFD_COMPLETE)
        TYPE3SWITCH(CONTEXT_UPDATE)

        default:
            sprintf(pszValue, "Unknown: %d", opcode);
            break;
    }
}

//----------------------------------------------------------------------------

void 
WritePM4Packet_Type0(FILE* pFile, unsigned int dwHeader, unsigned int** ppBuffer)
{
    pm4_type0 header = *((pm4_type0*) &dwHeader);
    unsigned int* pBuffer = *ppBuffer;
    unsigned int dwIndex;

    WriteDWORD(pFile, dwHeader);
    printString(pFile, "    // Type-0 packet (BASE_INDEX = 0x%x, ONE_REG_WR = %d, COUNT = %d+1)\n",
        header.base_index, header.one_reg_wr, header.count);   

    // Now go through and write the dwNumDWORDs 
    for (dwIndex = 0; dwIndex < header.count+1; dwIndex++)
    {
        char szRegister[1024];
        unsigned int dwRegIndex;
        unsigned int dwRegValue = *(pBuffer++);

        if (header.one_reg_wr)
        {
            dwRegIndex = header.base_index;
        }
        else
        {
            dwRegIndex = header.base_index + dwIndex;
        }

        WriteDWORD(pFile, dwRegValue);
        // Write register string based on fields
        GetString_Register(dwRegIndex, dwRegValue, szRegister);
        printString(pFile, "    // %s\n", szRegister);

        // Write actual unsigned int
        
    }

    *ppBuffer = pBuffer;
}

//----------------------------------------------------------------------------

void 
WritePM4Packet_Type2(FILE* pFile, unsigned int dwHeader, unsigned int** ppBuffer)
{
    unsigned int* pBuffer = *ppBuffer;

    WriteDWORD(pFile, dwHeader);
    printString(pFile, "    // Type-2 packet\n");
    
    *ppBuffer = pBuffer;
}

//----------------------------------------------------------------------------

void
AnalyzePacketType(FILE *pFile, unsigned int dwHeader, unsigned int**ppBuffer)
{
    switch(dwHeader & PM4_PKT_MASK)
    {
    case PM4_TYPE0_PKT:
        WritePM4Packet_Type0(pFile, dwHeader, ppBuffer);
        break;

    case PM4_TYPE1_PKT:
        break;

    case PM4_TYPE2_PKT:
        WritePM4Packet_Type2(pFile, dwHeader, ppBuffer);
        break;

    case PM4_TYPE3_PKT:
        WritePM4Packet_Type3(pFile, dwHeader, ppBuffer);
        break;
    }
}

void 
WritePM4Packet_Type3(FILE* pFile, unsigned int dwHeader, unsigned int** ppBuffer)
{
    pm4_type3 header = *((pm4_type3*) &dwHeader);
    unsigned int* pBuffer = *ppBuffer;
    unsigned int dwIndex;
    char szOpcode[64];

    if((EXPAND_OPCODE(header.it_opcode) == PM4_PACKET3_INDIRECT_BUFFER) ||
       (EXPAND_OPCODE(header.it_opcode) == PM4_PACKET3_INDIRECT_BUFFER_PFD))
    {
        unsigned int *pIndirectBuffer = (unsigned int *) *(pBuffer++); // ordinal 2 of IB packet is an address
        unsigned int *pIndirectBufferEnd = pIndirectBuffer + *(pBuffer++); // ordinal 3 of IB packet is size
        unsigned int gpuaddr = kgsl_sharedmem_convertaddr((unsigned int) pIndirectBuffer, 1);

        indirectionLevel++;

        WriteDWORD2(pFile, dwHeader);
        WriteDWORD2(pFile, gpuaddr);
        WriteDWORD2(pFile, (unsigned int) (pIndirectBufferEnd-pIndirectBuffer));

        if (indirectionLevel == 1)
        {
            printString(pFile, "Start_IB1, base=0x%x, size=%d\n", gpuaddr, (unsigned int)(pIndirectBufferEnd - pIndirectBuffer));
        }
        else
        {
            printString(pFile, "Start_IB2, base=0x%x, size=%d\n", gpuaddr, (unsigned int)(pIndirectBufferEnd - pIndirectBuffer));
        }

        while(pIndirectBuffer < pIndirectBufferEnd)
        {
            unsigned int _dwHeader = *(pIndirectBuffer++);

            AnalyzePacketType(pFile, _dwHeader, &pIndirectBuffer);
        }

        if (indirectionLevel == 1)
        {
            printString(pFile, "End_IB1\n");
        }
        else
        {
            printString(pFile, "End_IB2\n");
        }

        indirectionLevel--;
    }
    else
    {
        unsigned int registerAddr = 0xffffffff;
        char szRegister[1024];

        GetString_Type3Opcode(header.it_opcode, szOpcode);

        WriteDWORD(pFile, dwHeader);
        printString(pFile, "    // Type-3 packet (PREDICATE = %d, IT_OPCODE = %s, COUNT = %d+1)\n",
            header.predicate, szOpcode, header.count);
        
        // Go through each command
        for (dwIndex = 0; dwIndex < header.count+1; dwIndex++)
        {
            // Check for a register write
            if((EXPAND_OPCODE(header.it_opcode) == PM4_PACKET3_SET_CONSTANT) && (((*pBuffer) >> 16) == 0x4))
                registerAddr = (*pBuffer) & 0xffff;

            // Write unsigned int
            WriteDWORD(pFile, *pBuffer);

            // Starting at Ordinal 2 is actual register values
            if((dwIndex > 0) && (registerAddr != 0xffffffff))
            {
                // Write register string based on address
                GetString_Register(registerAddr + 0x2000, *pBuffer, szRegister);
                printString(pFile, "    // %s\n", szRegister);
                registerAddr++;
            }
            else
            {
                // Write out newline if we aren't augmenting with register fields
                printString(pFile, "\n");
            }

            pBuffer++;
        }
    }
    *ppBuffer = pBuffer;
}

//----------------------------------------------------------------------------

void
Yamato_DumpInitParams(unsigned int dwEDRAMBase, unsigned int dwEDRAMSize)
{
    FILE* pFile = fopen(PM4_DUMPFILE, "a");

    printString(pFile, "InitParams, edrambase=0x%x, edramsize=%d\n",
        dwEDRAMBase, dwEDRAMSize);

    fclose(pFile);
}

//----------------------------------------------------------------------------

void
Yamato_DumpSwapBuffers(unsigned int dwAddress, unsigned int dwWidth,
    unsigned int dwHeight, unsigned int dwPitch, unsigned int dwAlignedHeight, unsigned int dwBitsPerPixel)
{
    // Open file
    FILE* pFile = fopen(PM4_DUMPFILE, "a");

    printString(pFile, "SwapBuffers, address=0x%08x, width=%d, height=%d, pitch=%d, alignedheight=%d, bpp=%d\n",
        dwAddress, dwWidth, dwHeight, dwPitch, dwAlignedHeight, dwBitsPerPixel);

    fclose(pFile);
}

//----------------------------------------------------------------------------

void
Yamato_DumpRegSpace(gsl_device_t *device)
{
    int           regsPerLine = 0x20;
    unsigned int  dwOffset;
    unsigned int  value;

    FILE* pFile = fopen(PM4_DUMPFILE, "a");

    printString(pFile, "Start_RegisterSpace\n");
    
    for (dwOffset = 0; dwOffset < device->regspace.sizebytes; dwOffset += 4)
    {
        if (dwOffset % regsPerLine == 0)
        {
           printString(pFile, "    0x%08x   ", dwOffset);
        }

        GSL_HAL_REG_READ(device->id, (unsigned int) device->regspace.mmio_virt_base, (dwOffset >> 2), &value);

        printString(pFile, " 0x%08x", value);

        if (((dwOffset + 4) % regsPerLine == 0) && ((dwOffset + 4) < device->regspace.sizebytes))
        {
           printString(pFile, "\n");
        }
    }

    printString(pFile, "\nEnd_RegisterSpace\n");

    fclose(pFile);
}

//----------------------------------------------------------------------------

void 
Yamato_DumpAllocateMemory(unsigned int dwSize, unsigned int dwFlags, unsigned int dwAddress,
    unsigned int dwActualSize)
{
    // Open file
    FILE* pFile = fopen(PM4_DUMPFILE, "a");

    printString(pFile, "AllocateMemory, size=%d, flags=0x%x, address=0x%x, actualSize=%d\n",
        dwSize, dwFlags, dwAddress, dwActualSize);

    fclose(pFile);
}

//----------------------------------------------------------------------------

void
Yamato_DumpFreeMemory(unsigned int dwAddress)
{
    // Open file
    FILE* pFile = fopen(PM4_DUMPFILE, "a");

    printString(pFile, "FreeMemory, address=0x%x\n", dwAddress);

    fclose(pFile);
}

//----------------------------------------------------------------------------

void 
Yamato_DumpWriteMemory(unsigned int dwAddress, unsigned int dwSize, void* pData)
{
    // Open file
    FILE* pFile = fopen(PM4_DUMPFILE, "a");
    unsigned int dwNumDWORDs;
    unsigned int dwIndex;
    unsigned int *pDataPtr;

    printString(pFile, "StartWriteMemory, address=0x%x, size=%d\n", dwAddress, dwSize);

    // Now write the data, in dwNumDWORDs
    dwNumDWORDs = dwSize >> 2;

    // If there are spillover bytes into the next dword, increment the amount dumped out here.
    // The reader needs to take care of not overwriting the nonvalid bytes
    if((dwSize % 4) != 0)
        dwNumDWORDs++;

    for (dwIndex = 0, pDataPtr = (unsigned int *)pData; dwIndex < dwNumDWORDs; dwIndex++, pDataPtr++)
    {
        WriteDWORD2(pFile, *pDataPtr);
    }

    printString(pFile, "EndWriteMemory\n");

    fclose(pFile);
}

void 
Yamato_DumpSetMemory(unsigned int dwAddress, unsigned int dwSize, unsigned int pData)
{
    // Open file
    FILE* pFile = fopen(PM4_DUMPFILE, "a");
//    unsigned int* pDataPtr;

    printString(pFile, "SetMemory, address=0x%x, size=%d, value=0x%x\n",
        dwAddress, dwSize, pData);

    fclose(pFile);
}

//----------------------------------------------------------------------------
void 
Yamato_ConvertIBAddr(unsigned int dwHeader, unsigned int *pBuffer, int gpuToHost)
{
    unsigned int hostaddr;
    unsigned int *ibend;
    unsigned int *addr;
    unsigned int *ib    = pBuffer;
    pm4_type3    header = *((pm4_type3*) &dwHeader);

    // convert ib1 base address
    if((EXPAND_OPCODE(header.it_opcode) == PM4_PACKET3_INDIRECT_BUFFER) ||
       (EXPAND_OPCODE(header.it_opcode) == PM4_PACKET3_INDIRECT_BUFFER_PFD))
    {
        if (gpuToHost)
        {
            // from gpu to host
            *ib = kgsl_sharedmem_convertaddr(*ib, 0);

            hostaddr = *ib;
        }
        else
        {
            // from host to gpu
            hostaddr = *ib;
            *ib = kgsl_sharedmem_convertaddr(*ib, 1);
        }

        // walk through ib1 and convert any ib2 base address

        ib    = (unsigned int *) hostaddr;
        ibend = (unsigned int *) (ib + *(++pBuffer));

        while (ib < ibend)
        {
            dwHeader = *(ib);
            header   = *((pm4_type3*) (&dwHeader));

            switch(dwHeader & PM4_PKT_MASK)
            {
            case PM4_TYPE0_PKT:
                ib += header.count + 2;
                break;

            case PM4_TYPE1_PKT:
                break;

            case PM4_TYPE2_PKT:
                ib++;
                break;

            case PM4_TYPE3_PKT:
                if((EXPAND_OPCODE(header.it_opcode) == PM4_PACKET3_INDIRECT_BUFFER) ||
                   (EXPAND_OPCODE(header.it_opcode) == PM4_PACKET3_INDIRECT_BUFFER_PFD))
                {
                    addr = ib + 1;
                    if (gpuToHost)
                    {
                        // from gpu to host
                        *addr = kgsl_sharedmem_convertaddr(*addr, 0);
                    }
                    else
                    {
                        // from host to gpu
                        *addr = kgsl_sharedmem_convertaddr(*addr, 1);
                    }
                }
                ib += header.count + 2;
                break;
            }
        }
    }
}

//----------------------------------------------------------------------------

void
Yamato_DumpPM4(unsigned int* pBuffer, unsigned int sizeDWords)
{
    unsigned int *pBufferEnd = pBuffer + sizeDWords;
    unsigned int *tmp;

    // Open file
    FILE* pFile = fopen(PM4_DUMPFILE, "a");

    printString(pFile, "Start_PM4Buffer\n");//, count=%d\n", sizeDWords);

    // So look at the first unsigned int - should be a header
    while(pBuffer < pBufferEnd)
    {
        unsigned int dwHeader = *(pBuffer++);

        //printString(pFile, "  Start_Packet\n");
        switch(dwHeader & PM4_PKT_MASK)
        {
            case PM4_TYPE0_PKT:
                WritePM4Packet_Type0(pFile, dwHeader, &pBuffer);
                break;

            case PM4_TYPE1_PKT:
                break;

            case PM4_TYPE2_PKT:
                WritePM4Packet_Type2(pFile, dwHeader, &pBuffer);
                break;

            case PM4_TYPE3_PKT:
                indirectionLevel = 0;
                tmp = pBuffer;
                Yamato_ConvertIBAddr(dwHeader, tmp, 1);
                WritePM4Packet_Type3(pFile, dwHeader, &pBuffer);
                Yamato_ConvertIBAddr(dwHeader, tmp, 0);
                break;
        }
        //printString(pFile, "  End_Packet\n");
    }

    printString(pFile, "End_PM4Buffer\n");
    fclose(pFile);
}

//----------------------------------------------------------------------------

void
Yamato_DumpRegisterWrite(unsigned int dwAddress, unsigned int value)
{
    FILE *pFile;

    // Build a Type-0 packet that maps to this register write
    unsigned int pBuffer[100], *pBuf = &pBuffer[1];

    // Don't dump CP_RB_WPTR (switch statement may be necessary here for future additions)
    if(dwAddress == mmCP_RB_WPTR)
        return;

    pFile = fopen(PM4_DUMPFILE, "a");

    pBuffer[0] = dwAddress;
    pBuffer[1] = value;

    printString(pFile, "StartRegisterWrite\n");
    WritePM4Packet_Type0(pFile, pBuffer[0], &pBuf);
    printString(pFile, "EndRegisterWrite\n");

    fclose(pFile);
}

//----------------------------------------------------------------------------

void
Yamato_DumpFbStart(gsl_device_t *device)
{
    FILE *pFile;

    static int firstCall = 0;

    // We only want to call this once
    if(firstCall)
        return;

    pFile = fopen(PM4_DUMPFILE, "a");

    printString(pFile, "FbStart, value=0x%x\n", device->mmu.mpu_base);
    printString(pFile, "FbSize, value=0x%x\n", device->mmu.mpu_range);

    fclose(pFile);

    firstCall = 1;
}

//----------------------------------------------------------------------------

void
Yamato_DumpWindow(unsigned int addr, unsigned int width, unsigned int height)
{
    FILE *pFile;

    pFile = fopen(PM4_DUMPFILE, "a");

    printString(pFile, "DumpWindow, addr=0x%x, width=0x%x, height=0x%x\n", addr, width, height);

    fclose(pFile);
}

//----------------------------------------------------------------------------
#ifdef _DEBUG

#define ADDRESS_STACK_SIZE 256
#define GET_PM4_TYPE3_OPCODE(x) ((*(x) >> 8) & 0xFF)
#define IF_REGISTER_IN_RANGE(reg, base, count)         \
    offset = (reg) - (base);                           \
    if(offset >= 0 && offset <= (count) - 2)
#define GET_CP_CONSTANT_DATA(x) (*((x) + offset + 2))

static const char format2bpp[] =
{
    2,  // COLORX_4_4_4_4 
    2,  // COLORX_1_5_5_5 
    2,  // COLORX_5_6_5 
    1,  // COLORX_8 
    2,  // COLORX_8_8 
    4,  // COLORX_8_8_8_8 
    4,  // COLORX_S8_8_8_8 
    2,  // COLORX_16_FLOAT 
    4,  // COLORX_16_16_FLOAT 
    8,  // COLORX_16_16_16_16_FLOAT 
    4,  // COLORX_32_FLOAT 
    8,  // COLORX_32_32_FLOAT 
    16, // COLORX_32_32_32_32_FLOAT ,
    1,  // COLORX_2_3_3 
    3,  // COLORX_8_8_8
};

static unsigned int kgsl_dumpx_addr_count = 0; //unique command buffer addresses encountered
static int kgsl_dumpx_handle_type3(unsigned int* hostaddr, int count)
{
    // For swap detection we need to find the below declared static values, and detect DI during EDRAM copy
    static unsigned int width = 0, height = 0, format = 0, baseaddr = 0, iscopy = 0;

    static unsigned int addr_stack[ADDRESS_STACK_SIZE];
    static unsigned int size_stack[ADDRESS_STACK_SIZE];
    int swap = 0; // have we encountered a swap during recursion (return value)

    switch(GET_PM4_TYPE3_OPCODE(hostaddr))
    {
        case PM4_INDIRECT_BUFFER_PFD:
        case PM4_INDIRECT_BUFFER:
        {
            // traverse indirect buffers
            unsigned int i;
            unsigned int ibaddr = *(hostaddr+1);
            unsigned int ibsize = *(hostaddr+2);
            
            // is this address already in encountered?
            for(i = 0; i < kgsl_dumpx_addr_count && addr_stack[i] != ibaddr; i++);
            
            if(kgsl_dumpx_addr_count == i)
            {
                // yes it was, store the address so we don't dump this buffer twice
                addr_stack[kgsl_dumpx_addr_count] = ibaddr;
                // just for sanity checking
                size_stack[kgsl_dumpx_addr_count++] = ibsize; 
                KOS_ASSERT(kgsl_dumpx_addr_count < ADDRESS_STACK_SIZE);

                // recursively follow the indirect link and update swap if indirect buffer had resolve
                swap |= kgsl_dumpx_parse_ibs(ibaddr, ibsize); 
            }
            else
            {
                KOS_ASSERT(size_stack[i] == ibsize);
            }
        } 
        break;

        case PM4_SET_CONSTANT:
        if((*(hostaddr+1) >> 16) == 0x4)
        {   
            // parse register writes, and figure out framebuffer configuration

            unsigned int regaddr = (*(hostaddr + 1) & 0xFFFF) + 0x2000; //dword address in register space
            int offset; // used by the macros

            IF_REGISTER_IN_RANGE(mmPA_SC_WINDOW_SCISSOR_BR, regaddr, count)
            {
                // found write to PA_SC_WINDOW_SCISSOR_BR, we use this to detect current
                // width and height of the framebuffer (TODO: find more reliable way of achieving this)
                unsigned int data = GET_CP_CONSTANT_DATA(hostaddr);
                width  = data & 0xFFFF;
                height = data >> 16;
            }
            
            IF_REGISTER_IN_RANGE(mmRB_MODECONTROL, regaddr, count)
            {
                // found write to RB_MODECONTROL, we use this to find out if next DI is resolve
                unsigned int data = GET_CP_CONSTANT_DATA(hostaddr);
                iscopy = (data & RB_MODECONTROL__EDRAM_MODE_MASK) == (EDRAM_COPY << RB_MODECONTROL__EDRAM_MODE__SHIFT);
            }

            IF_REGISTER_IN_RANGE(mmRB_COPY_DEST_BASE, regaddr, count)
            {
                // found write to RB_COPY_DEST_BASE, we use this to find out the framebuffer base address
                unsigned int data = GET_CP_CONSTANT_DATA(hostaddr);
                baseaddr = (data & RB_COPY_DEST_BASE__COPY_DEST_BASE_MASK);
            }

            IF_REGISTER_IN_RANGE(mmRB_COPY_DEST_INFO, regaddr, count)
            {
               // found write to RB_COPY_DEST_INFO, we use this to find out the framebuffer format
                unsigned int data = GET_CP_CONSTANT_DATA(hostaddr);
                format = (data & RB_COPY_DEST_INFO__COPY_DEST_FORMAT_MASK) >> RB_COPY_DEST_INFO__COPY_DEST_FORMAT__SHIFT;
            }             
        }
        break;

        case PM4_DRAW_INDX:
        case PM4_DRAW_INDX_2:
        {
            // DI found
            // check if it is resolve
            if(iscopy && !swap)
            {
                // printf("resolve: %ix%i @ 0x%08x, format = 0x%08x\n", width, height, baseaddr, format);
                KOS_ASSERT(format < 15);

                // yes it was and we need to update color buffer config because this is the first bin
                // dumpx framebuffer base address, and dimensions
                KGSL_DEBUG_DUMPX( BB_DUMP_CBUF_AWH, (unsigned int)baseaddr, width, height, " ");

                // find aligned width
                width = (width + 31) & ~31;

                //dump bytes-per-pixel and aligned width
                KGSL_DEBUG_DUMPX( BB_DUMP_CBUF_FS, format2bpp[format], width, 0, " ");
                swap = 1;
            }
            
        }
        break;

        default:
        break;
    }
    return swap;
}
           
// Traverse IBs and dump them to test vector. Detect swap by inspecting register 
// writes, keeping note of the current state, and dump framebuffer config to test vector 
int kgsl_dumpx_parse_ibs(gpuaddr_t gpuaddr, int sizedwords)
{
    static unsigned int level = 0; //recursion level

    int swap = 0; // have we encountered a swap during recursion (return value)
    unsigned int *hostaddr;
    int dwords_left = sizedwords; //dwords left in the current command buffer

    level++;

    KOS_ASSERT(sizeof(unsigned int *) == sizeof(unsigned int));
    KOS_ASSERT(level <= 2);
    hostaddr = (unsigned int *)kgsl_sharedmem_convertaddr(gpuaddr, 0);    

    // dump the IB to test vector
    KGSL_DEBUG(GSL_DBGFLAGS_DUMPX, KGSL_DEBUG_DUMPX(BB_DUMP_MEMWRITE, gpuaddr, (unsigned int)hostaddr, sizedwords*4, "kgsl_dumpx_write_ibs"));

    while(dwords_left)
    {
        int count = 0; //dword count including packet header

        switch(*hostaddr >> 30)
        {
        case 0x0: // type-0
            count = (*hostaddr >> 16)+2;
            break;
        case 0x1: // type-1
            count = 2;
            break;
        case 0x3: // type-3
            count = ((*hostaddr >> 16) & 0x3fff) + 2;
            swap |= kgsl_dumpx_handle_type3(hostaddr, count);
            break; // type-3
        default:
            KOS_ASSERT(!"unknown packet type");
        }
        
        // jump to next packet
        dwords_left -= count;
        hostaddr += count;
        KOS_ASSERT(dwords_left >= 0 && "PM4 parsing error");
    }

    level--;

    // if this is the starting level of recursion, we are done. clean-up
    if(level == 0) kgsl_dumpx_addr_count = 0;

    return swap;
}
#endif

#endif // WIN32