summaryrefslogtreecommitdiff
path: root/drivers/mxc/amd-gpu/common/gsl_yamato.c
blob: d74c9efe2f36c88358acdea5344dd10318dde167 (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
/* Copyright (c) 2002,2007-2010, Code Aurora Forum. 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"
#ifdef _LINUX
#include <linux/sched.h>
#endif

#ifdef GSL_BLD_YAMATO

#include "gsl_ringbuffer.h"
#include "gsl_drawctxt.h"

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

static int
kgsl_yamato_gmeminit(gsl_device_t *device)
{
    rb_edram_info_u rb_edram_info = {0};
    unsigned int    gmem_size;
    unsigned int    edram_value = 0;

    // make sure edram range is aligned to size
    KOS_ASSERT((device->gmemspace.gpu_base  & (device->gmemspace.sizebytes - 1)) == 0);

    // get edram_size value equivalent
    gmem_size = (device->gmemspace.sizebytes >> 14);
    while (gmem_size >>= 1)
    {
       edram_value++;
    }

    rb_edram_info.f.edram_size         = edram_value;
    rb_edram_info.f.edram_mapping_mode = 0;                                     // EDRAM_MAP_UPPER
    rb_edram_info.f.edram_range        = (device->gmemspace.gpu_base >> 14);    // must be aligned to size

    device->ftbl.device_regwrite(device, mmRB_EDRAM_INFO, (unsigned int)rb_edram_info.val);

    return (GSL_SUCCESS);
}

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

static int
kgsl_yamato_gmemclose(gsl_device_t *device)
{
    device->ftbl.device_regwrite(device, mmRB_EDRAM_INFO, 0x00000000);

    return (GSL_SUCCESS);
}

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

void
kgsl_yamato_rbbmintrcallback(gsl_intrid_t id, void *cookie)
{
    gsl_device_t  *device = (gsl_device_t *) cookie;

    switch(id)
    {
        // error condition interrupt
        case GSL_INTR_YDX_RBBM_READ_ERROR:

            device->ftbl.device_destroy(device);
            break;

        // non-error condition interrupt
        case GSL_INTR_YDX_RBBM_DISPLAY_UPDATE:
        case GSL_INTR_YDX_RBBM_GUI_IDLE:

            kos_event_signal(device->intr.evnt[id]);
            break;

        default:

            break;
    }
}

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

void
kgsl_yamato_cpintrcallback(gsl_intrid_t id, void *cookie)
{
    gsl_device_t  *device = (gsl_device_t *) cookie;

    switch(id)
    {
        case GSL_INTR_YDX_CP_RING_BUFFER:
#ifndef _LINUX		
              kos_event_signal(device->timestamp_event);
#else
			  wake_up_interruptible_all(&(device->timestamp_waitq));
#endif
            break;
        default:
            break;
    }
}
//----------------------------------------------------------------------------

void
kgsl_yamato_sqintrcallback(gsl_intrid_t id, void *cookie)
{
    (void) cookie;      // unreferenced formal parameter
	/*gsl_device_t  *device = (gsl_device_t *) cookie;*/

    switch(id)
    {
        // error condition interrupt
        case GSL_INTR_YDX_SQ_PS_WATCHDOG:
        case GSL_INTR_YDX_SQ_VS_WATCHDOG:

			// todo: take appropriate action

			break;

		default:

			break;
    }
}

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

#ifdef _DEBUG

static int
kgsl_yamato_bist(gsl_device_t *device)
{
    int           status = GSL_FAILURE;
    unsigned int  link[2];

    if (!(device->flags & GSL_FLAGS_STARTED))
    {
        return (GSL_FAILURE);
    }

    status = kgsl_ringbuffer_bist(&device->ringbuffer);
    if (status != GSL_SUCCESS)
    {
        return (status);
    }

    // interrupt bist
    link[0] = pm4_type3_packet(PM4_INTERRUPT, 1);
    link[1] = CP_INT_CNTL__RB_INT_MASK;
	kgsl_ringbuffer_issuecmds(device, 1, &link[0], 2, GSL_CALLER_PROCESSID_GET());

    status = kgsl_mmu_bist(&device->mmu);
    if (status != GSL_SUCCESS)
    {
        return (status);
    }

    return (status);
}
#endif

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

int
kgsl_yamato_isr(gsl_device_t *device)
{
    unsigned int            status;
#ifdef _DEBUG
    mh_mmu_page_fault_u     page_fault        = {0};
    mh_axi_error_u          axi_error         = {0};
    mh_clnt_axi_id_reuse_u  clnt_axi_id_reuse = {0};
    rbbm_read_error_u       read_error        = {0};
#endif // DEBUG

    // determine if yamato is interrupting, and if so, which block
    device->ftbl.device_regread(device, mmMASTER_INT_SIGNAL, &status);

    if (status & MASTER_INT_SIGNAL__MH_INT_STAT)
    {
#ifdef _DEBUG
        // obtain mh error information
        device->ftbl.device_regread(device, mmMH_MMU_PAGE_FAULT, (unsigned int *)&page_fault);
        device->ftbl.device_regread(device, mmMH_AXI_ERROR, (unsigned int *)&axi_error);
        device->ftbl.device_regread(device, mmMH_CLNT_AXI_ID_REUSE, (unsigned int *)&clnt_axi_id_reuse);
#endif // DEBUG

        kgsl_intr_decode(device, GSL_INTR_BLOCK_YDX_MH);
    }

    if (status & MASTER_INT_SIGNAL__CP_INT_STAT)
    {
        kgsl_intr_decode(device, GSL_INTR_BLOCK_YDX_CP);
    }

    if (status & MASTER_INT_SIGNAL__RBBM_INT_STAT)
    {
#ifdef _DEBUG
        // obtain rbbm error information
        device->ftbl.device_regread(device, mmRBBM_READ_ERROR, (unsigned int *)&read_error);
#endif // DEBUG

        kgsl_intr_decode(device, GSL_INTR_BLOCK_YDX_RBBM);
    }

    if (status & MASTER_INT_SIGNAL__SQ_INT_STAT)
    {
        kgsl_intr_decode(device, GSL_INTR_BLOCK_YDX_SQ);
    }

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_tlbinvalidate(gsl_device_t *device, unsigned int reg_invalidate, unsigned int pid)
{
	unsigned int		 link[2];
    mh_mmu_invalidate_u  mh_mmu_invalidate = {0};

    mh_mmu_invalidate.f.invalidate_all = 1;
    mh_mmu_invalidate.f.invalidate_tc  = 1;

	// if possible, invalidate via command stream, otherwise via direct register writes
	if (device->flags & GSL_FLAGS_STARTED)
	{
		link[0] = pm4_type0_packet(reg_invalidate, 1);
		link[1] = mh_mmu_invalidate.val;

		kgsl_ringbuffer_issuecmds(device, 1, &link[0], 2, pid);
	}
	else
	{

        device->ftbl.device_regwrite(device, reg_invalidate, mh_mmu_invalidate.val);
    }

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_setpagetable(gsl_device_t *device, unsigned int reg_ptbase, gpuaddr_t ptbase, unsigned int pid)
{
	unsigned int  link[25];

    // if there is an active draw context, set via command stream,
	if (device->flags & GSL_FLAGS_STARTED)
    {
        // wait for graphics pipe to be idle
        link[0] = pm4_type3_packet(PM4_WAIT_FOR_IDLE, 1);
        link[1] = 0x00000000;

        // set page table base
        link[2] = pm4_type0_packet(reg_ptbase, 1);
		link[3] = ptbase;

        // HW workaround: to resolve MMU page fault interrupts caused by the VGT. It prevents 
		// the CP PFP from filling the VGT DMA request fifo too early, thereby ensuring that 
		// the VGT will not fetch vertex/bin data until after the page table base register 
		// has been updated.
		//
		// Two null DRAW_INDX_BIN packets are inserted right after the page table base update, 
		// followed by a wait for idle. The null packets will fill up the VGT DMA request 
		// fifo and prevent any further vertex/bin updates from occurring until the wait 
		// has finished.
		link[4]  = pm4_type3_packet(PM4_SET_CONSTANT, 2);
		link[5]  = (0x4 << 16) | (mmPA_SU_SC_MODE_CNTL - 0x2000);
		link[6]  = 0;          // disable faceness generation
		link[7]  = pm4_type3_packet(PM4_SET_BIN_BASE_OFFSET, 1);
		link[8]  = device->mmu.dummyspace.gpuaddr;
		link[9]  = pm4_type3_packet(PM4_DRAW_INDX_BIN, 6);
		link[10] = 0;          // viz query info
		link[11] = 0x0003C004; // draw indicator
		link[12] = 0;          // bin base
		link[13] = 3;          // bin size
		link[14] = device->mmu.dummyspace.gpuaddr; // dma base
		link[15] = 6;          // dma size
		link[16] = pm4_type3_packet(PM4_DRAW_INDX_BIN, 6);
		link[17] = 0;          // viz query info
		link[18] = 0x0003C004; // draw indicator
		link[19] = 0;          // bin base
		link[20] = 3;          // bin size
		link[21] = device->mmu.dummyspace.gpuaddr; // dma base
		link[22] = 6;          // dma size
		link[23] = pm4_type3_packet(PM4_WAIT_FOR_IDLE, 1);
		link[24] = 0x00000000;

		kgsl_ringbuffer_issuecmds(device, 1, &link[0], 25, pid);
	}
	else
	{
		device->ftbl.device_idle(device, GSL_TIMEOUT_DEFAULT);
		device->ftbl.device_regwrite(device, reg_ptbase, ptbase);
    }

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_init(gsl_device_t *device)
{
    int  status = GSL_FAILURE;

    device->flags |= GSL_FLAGS_INITIALIZED;

    kgsl_hal_setpowerstate(device->id, GSL_PWRFLAGS_POWER_ON, 100);

    //We need to make sure all blocks are powered up and clocked before
    //issuing a soft reset.  The overrides will be turned off (set to 0)
    //later in kgsl_yamato_start.
    device->ftbl.device_regwrite(device, mmRBBM_PM_OVERRIDE1, 0xfffffffe);
    device->ftbl.device_regwrite(device, mmRBBM_PM_OVERRIDE2, 0xffffffff);

    // soft reset
    device->ftbl.device_regwrite(device, mmRBBM_SOFT_RESET, 0xFFFFFFFF);
    kos_sleep(50);
    device->ftbl.device_regwrite(device, mmRBBM_SOFT_RESET, 0x00000000);

    // RBBM control
    device->ftbl.device_regwrite(device, mmRBBM_CNTL, 0x00004442);

    // setup MH arbiter
    device->ftbl.device_regwrite(device, mmMH_ARBITER_CONFIG, *(unsigned int *) &gsl_cfg_yamato_mharb);

    // SQ_*_PROGRAM
    device->ftbl.device_regwrite(device, mmSQ_VS_PROGRAM, 0x00000000);
    device->ftbl.device_regwrite(device, mmSQ_PS_PROGRAM, 0x00000000);

    // init interrupt
    status = kgsl_intr_init(device);
    if (status != GSL_SUCCESS)
    {
        device->ftbl.device_stop(device);
        return (status);
    }

    // init mmu
    status = kgsl_mmu_init(device);
    if (status != GSL_SUCCESS)
    {
        device->ftbl.device_stop(device);
        return (status);
    }

    return(status);
}

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

int
kgsl_yamato_close(gsl_device_t *device)
{
    if (device->refcnt == 0)
    {
        // shutdown mmu
        kgsl_mmu_close(device);

        // shutdown interrupt
        kgsl_intr_close(device);

        kgsl_hal_setpowerstate(device->id, GSL_PWRFLAGS_POWER_OFF, 0);

        device->flags &= ~GSL_FLAGS_INITIALIZED;
    }

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_destroy(gsl_device_t *device)
{
    int           i;
    unsigned int  pid;

#ifdef _DEBUG
    // for now, signal catastrophic failure in a brute force way
    KOS_ASSERT(0);
#endif // _DEBUG

    // todo: - hard reset core?

    kgsl_drawctxt_destroyall(device);

    for (i = 0; i < GSL_CALLER_PROCESS_MAX; i++)
    {
        pid = device->callerprocess[i];
        if (pid)
        {
            device->ftbl.device_stop(device);
            kgsl_driver_destroy(pid);

            // todo: terminate client process?
        }
    }

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_start(gsl_device_t *device, gsl_flags_t flags)
{
    int           status = GSL_FAILURE;
    unsigned int  pm1, pm2;

    KGSL_DEBUG(GSL_DBGFLAGS_PM4, KGSL_DEBUG_DUMPFBSTART(device));

    (void) flags;      // unreferenced formal parameter

    kgsl_hal_setpowerstate(device->id, GSL_PWRFLAGS_CLK_ON, 100);

    // default power management override when running in safe mode
    pm1 = (device->flags & GSL_FLAGS_SAFEMODE) ? 0xFFFFFFFE : 0x00000000;
    pm2 = (device->flags & GSL_FLAGS_SAFEMODE) ? 0x000000FF : 0x00000000;
    device->ftbl.device_regwrite(device, mmRBBM_PM_OVERRIDE1, pm1);
    device->ftbl.device_regwrite(device, mmRBBM_PM_OVERRIDE2, pm2);

    // enable rbbm interrupts
    kgsl_intr_attach(&device->intr, GSL_INTR_YDX_RBBM_READ_ERROR, kgsl_yamato_rbbmintrcallback, (void *) device);
    kgsl_intr_attach(&device->intr, GSL_INTR_YDX_RBBM_DISPLAY_UPDATE, kgsl_yamato_rbbmintrcallback, (void *) device);
    kgsl_intr_attach(&device->intr, GSL_INTR_YDX_RBBM_GUI_IDLE, kgsl_yamato_rbbmintrcallback, (void *) device);
    kgsl_intr_enable(&device->intr, GSL_INTR_YDX_RBBM_READ_ERROR);
    kgsl_intr_enable(&device->intr, GSL_INTR_YDX_RBBM_DISPLAY_UPDATE);
#if defined GSL_RB_TIMESTAMP_INTERUPT	
	kgsl_intr_attach(&device->intr, GSL_INTR_YDX_CP_RING_BUFFER, kgsl_yamato_cpintrcallback, (void *) device);    
    kgsl_intr_enable(&device->intr, GSL_INTR_YDX_CP_RING_BUFFER);
#endif

  //kgsl_intr_enable(&device->intr, GSL_INTR_YDX_RBBM_GUI_IDLE);

    // enable sq interrupts
    kgsl_intr_attach(&device->intr, GSL_INTR_YDX_SQ_PS_WATCHDOG, kgsl_yamato_sqintrcallback, (void *) device);
    kgsl_intr_attach(&device->intr, GSL_INTR_YDX_SQ_VS_WATCHDOG, kgsl_yamato_sqintrcallback, (void *) device);
  //kgsl_intr_enable(&device->intr, GSL_INTR_YDX_SQ_PS_WATCHDOG);
  //kgsl_intr_enable(&device->intr, GSL_INTR_YDX_SQ_VS_WATCHDOG);

    // init gmem
    kgsl_yamato_gmeminit(device);

    // init ring buffer
    status = kgsl_ringbuffer_init(device);
    if (status != GSL_SUCCESS)
    {
        device->ftbl.device_stop(device);
        return (status);
    }

    // init draw context
    status = kgsl_drawctxt_init(device);
    if (status != GSL_SUCCESS)
    {
        device->ftbl.device_stop(device);
        return (status);
    }

    device->flags |= GSL_FLAGS_STARTED;

    KGSL_DEBUG(GSL_DBGFLAGS_BIST, kgsl_yamato_bist(device));

    return (status);
}

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

int
kgsl_yamato_stop(gsl_device_t *device)
{
    // disable rbbm interrupts
    kgsl_intr_detach(&device->intr, GSL_INTR_YDX_RBBM_READ_ERROR);
    kgsl_intr_detach(&device->intr, GSL_INTR_YDX_RBBM_DISPLAY_UPDATE);
    kgsl_intr_detach(&device->intr, GSL_INTR_YDX_RBBM_GUI_IDLE);
#if defined GSL_RB_TIMESTAMP_INTERUPT	
    kgsl_intr_detach(&device->intr, GSL_INTR_YDX_CP_RING_BUFFER);
#endif

    // disable sq interrupts
    kgsl_intr_detach(&device->intr, GSL_INTR_YDX_SQ_PS_WATCHDOG);
    kgsl_intr_detach(&device->intr, GSL_INTR_YDX_SQ_VS_WATCHDOG);

    kgsl_drawctxt_close(device);

    // shutdown ringbuffer
    kgsl_ringbuffer_close(&device->ringbuffer);

    // shutdown gmem
    kgsl_yamato_gmemclose(device);

    if(device->refcnt == 0)
    {
        kgsl_hal_setpowerstate(device->id, GSL_PWRFLAGS_CLK_OFF, 0);
    }

    device->flags &= ~GSL_FLAGS_STARTED;

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_getproperty(gsl_device_t *device, gsl_property_type_t type, void *value, unsigned int sizebytes)
{
    int  status = GSL_FAILURE;

#ifndef _DEBUG
    (void) sizebytes;       // unreferenced formal parameter
#endif

    if (type == GSL_PROP_DEVICE_INFO)
    {
        gsl_devinfo_t  *devinfo = (gsl_devinfo_t *) value;

        KOS_ASSERT(sizebytes == sizeof(gsl_devinfo_t));

        devinfo->device_id         = device->id;
        devinfo->chip_id           = (gsl_chipid_t)device->chip_id;
        devinfo->mmu_enabled       = kgsl_mmu_isenabled(&device->mmu);
        devinfo->gmem_hostbaseaddr = device->gmemspace.mmio_virt_base;
        devinfo->gmem_gpubaseaddr  = device->gmemspace.gpu_base;
        devinfo->gmem_sizebytes    = device->gmemspace.sizebytes;

        status = GSL_SUCCESS;
    }

    return (status);
}

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

int
kgsl_yamato_setproperty(gsl_device_t *device, gsl_property_type_t type, void *value, unsigned int sizebytes)
{
    int  status = GSL_FAILURE;

#ifndef _DEBUG
    (void) sizebytes;           // unreferenced formal parameter
#endif

    if (type == GSL_PROP_DEVICE_POWER)
    {
        gsl_powerprop_t  *power = (gsl_powerprop_t *) value;

        KOS_ASSERT(sizebytes == sizeof(gsl_powerprop_t));

        if (!(device->flags & GSL_FLAGS_SAFEMODE))
        {
            if (power->flags & GSL_PWRFLAGS_OVERRIDE_ON)
            {
                device->ftbl.device_regwrite(device, mmRBBM_PM_OVERRIDE1, 0xfffffffe);
                device->ftbl.device_regwrite(device, mmRBBM_PM_OVERRIDE2, 0xffffffff);
            }
            else if (power->flags & GSL_PWRFLAGS_OVERRIDE_OFF)
            {
                device->ftbl.device_regwrite(device, mmRBBM_PM_OVERRIDE1, 0x00000000);
                device->ftbl.device_regwrite(device, mmRBBM_PM_OVERRIDE2, 0x00000000);
            }
            else
            {
                kgsl_hal_setpowerstate(device->id, power->flags, power->value);
            }
        }

        status = GSL_SUCCESS;
    }
    else if (type == GSL_PROP_DEVICE_DMI)
    {
        gsl_dmiprop_t  *dmi = (gsl_dmiprop_t *) value;

        KOS_ASSERT(sizebytes == sizeof(gsl_dmiprop_t));

        //
        //  In order to enable DMI, it must not already be enabled.
        //
        switch (dmi->flags)
        {
            case GSL_DMIFLAGS_ENABLE_SINGLE:
            case GSL_DMIFLAGS_ENABLE_DOUBLE:
                if (!gsl_driver.dmi_state)
                {
                    gsl_driver.dmi_state = OS_TRUE;
                    gsl_driver.dmi_mode  = dmi->flags;
                    gsl_driver.dmi_frame = -1;
                    status = GSL_SUCCESS;
                }
                break;
            case GSL_DMIFLAGS_DISABLE:
                //
                //  To disable, we must be enabled.
                //
                if (gsl_driver.dmi_state)
                {
                    gsl_driver.dmi_state = OS_FALSE;
                    gsl_driver.dmi_mode  = -1;
                    gsl_driver.dmi_frame = -2;
                    status = GSL_SUCCESS;
                }
                break;
            case GSL_DMIFLAGS_NEXT_BUFFER:
                //
                //  Going to the next buffer is dependent upon what mod we are in with respect to single, double, or triple buffering.
                //  DMI must also be enabled.
                //
                if (gsl_driver.dmi_state)
                {
                    unsigned int    cmdbuf[10];
                    unsigned int    *cmds = &cmdbuf[0];
                    int             size;

                    if (gsl_driver.dmi_frame == -1)
                    {
                        size = 8;

                        *cmds++ =  pm4_type0_packet(mmRBBM_DSPLY, 1);
                        switch (gsl_driver.dmi_mode)
                        {
                            case GSL_DMIFLAGS_ENABLE_SINGLE:
                                gsl_driver.dmi_max_frame = 1;
                                *cmds++ = 0x041000410;
                                break;
                            case GSL_DMIFLAGS_ENABLE_DOUBLE:
                                gsl_driver.dmi_max_frame = 2;
                                *cmds++ = 0x041000510;
                                break;
                            case GSL_DMIFLAGS_ENABLE_TRIPLE:
                                gsl_driver.dmi_max_frame = 3;
                                *cmds++ = 0x041000610;
                                break;
                        }
                    }
                    else
                    {
                        size = 6;
                    }


                    //
                    //  Wait for 3D core to be idle and wait for vsync
                    //
                    *cmds++ =  pm4_type0_packet(mmWAIT_UNTIL, 1);
                    *cmds++ = 0x00008000;           //  3d idle
                    // *cmds++ = 0x00008008;         //  3d idle & vsync

                    //
                    //  Update the render latest register.
                    //
                    *cmds++ =  pm4_type0_packet(mmRBBM_RENDER_LATEST, 1);
                    switch (gsl_driver.dmi_frame)
                    {
                        case 0:
                            //
                            //  Render frame 0
                            //
                            *cmds++ = 0;
                            //
                            //  Wait for our max frame # indicator to be de-asserted
                            //
                            *cmds++ =  pm4_type0_packet(mmWAIT_UNTIL, 1);
                            *cmds++ = 0x00000008 << gsl_driver.dmi_max_frame;
                            gsl_driver.dmi_frame = 1;
                            break;
                        case -1:
                        case 1:
                            //
                            //  Render frame 1
                            //
                            *cmds++ = 1;
                            *cmds++ =  pm4_type0_packet(mmWAIT_UNTIL, 1);
                            *cmds++ = 0x00000010;   //  Wait for frame 0 to be deasserted
                            gsl_driver.dmi_frame = 2;
                            break;
                        case 2:
                            //
                            //  Render frame 2
                            //
                            *cmds++ = 2;
                            *cmds++ =  pm4_type0_packet(mmWAIT_UNTIL, 1);
                            *cmds++ = 0x00000020;   //  Wait for frame 1 to be deasserted
                            gsl_driver.dmi_frame = 0;
                            break;
                    }
                    
                    // issue the commands
                    kgsl_ringbuffer_issuecmds(device, 1, &cmdbuf[0], size, GSL_CALLER_PROCESSID_GET());

                    gsl_driver.dmi_frame %= gsl_driver.dmi_max_frame;
                    status = GSL_SUCCESS;
                }
                break;
            default:
                status = GSL_FAILURE;
                break;
        }
    }

    return (status);
}

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

int
kgsl_yamato_idle(gsl_device_t *device, unsigned int timeout)
{
    int               status  = GSL_FAILURE;
    gsl_ringbuffer_t  *rb     = &device->ringbuffer;
    rbbm_status_u     rbbm_status;

    (void) timeout;      // unreferenced formal parameter

    KGSL_DEBUG(GSL_DBGFLAGS_DUMPX, KGSL_DEBUG_DUMPX(BB_DUMP_REGPOLL, device->id, mmRBBM_STATUS, 0x80000000, "kgsl_yamato_idle"));

    // first, wait until the CP has consumed all the commands in the ring buffer
    if (rb->flags & GSL_FLAGS_STARTED)
    {
        do
        {
            GSL_RB_GET_READPTR(rb, &rb->rptr);

        } while (rb->rptr != rb->wptr);
    }

    // now, wait for the GPU to finish its operations
    for ( ; ; )
    {
        device->ftbl.device_regread(device, mmRBBM_STATUS, (unsigned int *)&rbbm_status);

        if (!(rbbm_status.val & 0x80000000))
        {
            status = GSL_SUCCESS;
            break;
        }

    }

    return (status);
}

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

int
kgsl_yamato_regread(gsl_device_t *device, unsigned int offsetwords, unsigned int *value)
{
    KGSL_DEBUG(GSL_DBGFLAGS_DUMPX,
            {
                if (!(gsl_driver.flags_debug & GSL_DBGFLAGS_DUMPX_WITHOUT_IFH))
                {
                    if(offsetwords == mmCP_RB_RPTR || offsetwords == mmCP_RB_WPTR)
                    {
                        *value = device->ringbuffer.wptr;
                        return (GSL_SUCCESS);
                    }
                }
            });

    GSL_HAL_REG_READ(device->id, (unsigned int) device->regspace.mmio_virt_base, offsetwords, value);

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_regwrite(gsl_device_t *device, unsigned int offsetwords, unsigned int value)
{
    KGSL_DEBUG(GSL_DBGFLAGS_PM4, KGSL_DEBUG_DUMPREGWRITE(offsetwords, value));

    GSL_HAL_REG_WRITE(device->id, (unsigned int) device->regspace.mmio_virt_base, offsetwords, value);

    // idle device when running in safe mode
    if (device->flags & GSL_FLAGS_SAFEMODE)
    {
        device->ftbl.device_idle(device, GSL_TIMEOUT_DEFAULT);
    }

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_waitirq(gsl_device_t *device, gsl_intrid_t intr_id, unsigned int *count, unsigned int timeout)
{
    int  status = GSL_FAILURE_NOTSUPPORTED;

    if (intr_id == GSL_INTR_YDX_CP_IB1_INT || intr_id == GSL_INTR_YDX_CP_IB2_INT ||
        intr_id == GSL_INTR_YDX_CP_SW_INT  || intr_id == GSL_INTR_YDX_RBBM_DISPLAY_UPDATE)
    {
        if (kgsl_intr_isenabled(&device->intr, intr_id) == GSL_SUCCESS)
        {
            // wait until intr completion event is received
            if (kos_event_wait(device->intr.evnt[intr_id], timeout) == OS_SUCCESS)
            {
                *count = 1;
                status = GSL_SUCCESS;
            }
            else
            {
                status = GSL_FAILURE_TIMEOUT;
            }
        }
    }

    return (status);
}

int
kgsl_yamato_waittimestamp(gsl_device_t *device, gsl_timestamp_t timestamp, unsigned int timeout)
{
#if defined GSL_RB_TIMESTAMP_INTERUPT
#ifndef _LINUX
	return kos_event_wait( device->timestamp_event, timeout );
#else
	int status = wait_event_interruptible_timeout(device->timestamp_waitq,
	                                              kgsl_cmdstream_check_timestamp(device->id, timestamp),
												  msecs_to_jiffies(timeout));
	if (status > 0)
		return GSL_SUCCESS;
	else
		return GSL_FAILURE;
#endif
#else
	return (GSL_SUCCESS);
#endif
}
//----------------------------------------------------------------------------

int
kgsl_yamato_runpending(gsl_device_t *device)
{
    (void) device;

    return (GSL_SUCCESS);
}

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

int
kgsl_yamato_getfunctable(gsl_functable_t *ftbl)
{
    ftbl->device_init           = kgsl_yamato_init;
    ftbl->device_close          = kgsl_yamato_close;
    ftbl->device_destroy        = kgsl_yamato_destroy;
    ftbl->device_start          = kgsl_yamato_start;
    ftbl->device_stop           = kgsl_yamato_stop;
    ftbl->device_getproperty    = kgsl_yamato_getproperty;
    ftbl->device_setproperty    = kgsl_yamato_setproperty;
    ftbl->device_idle           = kgsl_yamato_idle;
	ftbl->device_waittimestamp  = kgsl_yamato_waittimestamp;
    ftbl->device_regread        = kgsl_yamato_regread;
    ftbl->device_regwrite       = kgsl_yamato_regwrite;
    ftbl->device_waitirq        = kgsl_yamato_waitirq;
    ftbl->device_runpending     = kgsl_yamato_runpending;
    ftbl->intr_isr              = kgsl_yamato_isr;
    ftbl->mmu_tlbinvalidate     = kgsl_yamato_tlbinvalidate;
    ftbl->mmu_setpagetable      = kgsl_yamato_setpagetable;
    ftbl->cmdstream_issueibcmds = kgsl_ringbuffer_issueibcmds;
    ftbl->context_create        = kgsl_drawctxt_create;
    ftbl->context_destroy       = kgsl_drawctxt_destroy;

    return (GSL_SUCCESS);
}

#endif