summaryrefslogtreecommitdiff
path: root/drivers/mxc/amd-gpu/common/gsl_device.c
blob: 6fc51eb1bdc283e5fc04cbe1567f1e283e9ff6c3 (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
/* 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"
#ifdef _LINUX
#include <linux/sched.h>
#endif

//////////////////////////////////////////////////////////////////////////////
//  inline functions
//////////////////////////////////////////////////////////////////////////////
OSINLINE void
kgsl_device_getfunctable(gsl_deviceid_t device_id, gsl_functable_t *ftbl)
{
    switch (device_id)
    {
#ifdef GSL_BLD_YAMATO
    case GSL_DEVICE_YAMATO:
        kgsl_yamato_getfunctable(ftbl);
        break;
#endif // GSL_BLD_YAMATO
#ifdef GSL_BLD_G12
    case GSL_DEVICE_G12:
        kgsl_g12_getfunctable(ftbl);
        break;
#endif // GSL_BLD_G12
    default:
        break;
    }
}


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

int
kgsl_device_init(gsl_device_t *device, gsl_deviceid_t device_id)
{
    int              status = GSL_SUCCESS;
    gsl_devconfig_t  config;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_init(gsl_device_t *device=0x%08x, gsl_deviceid_t device_id=%D )\n", device, device_id );

    if (device->flags & GSL_FLAGS_INITIALIZED)
    {
        kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_init. Return value %B\n", GSL_SUCCESS );
        return (GSL_SUCCESS);
    }

    kos_memset(device, 0, sizeof(gsl_device_t));

    // if device configuration is present
    if (kgsl_hal_getdevconfig(device_id, &config) == GSL_SUCCESS)
    {
        kgsl_device_getfunctable(device_id, &device->ftbl);

        kos_memcpy(&device->regspace,  &config.regspace,  sizeof(gsl_memregion_t));
#ifdef GSL_BLD_YAMATO
        kos_memcpy(&device->gmemspace, &config.gmemspace, sizeof(gsl_memregion_t));
#endif // GSL_BLD_YAMATO

        device->refcnt        = 0;
        device->id            = device_id;

#ifndef GSL_NO_MMU
        device->mmu.config    = config.mmu_config;
        device->mmu.mpu_base  = config.mpu_base;
        device->mmu.mpu_range = config.mpu_range;
        device->mmu.va_base   = config.va_base;
        device->mmu.va_range  = config.va_range;
#endif

        if (device->ftbl.device_init)
        {
            status = device->ftbl.device_init(device);
        }
        else
        {
            status = GSL_FAILURE_NOTINITIALIZED;
        }

        // allocate memory store
        status = kgsl_sharedmem_alloc0(device->id, GSL_MEMFLAGS_ALIGNPAGE | GSL_MEMFLAGS_CONPHYS, sizeof(gsl_devmemstore_t), &device->memstore);

        KGSL_DEBUG(GSL_DBGFLAGS_DUMPX,
        {
            // dumpx needs this to be in EMEM0 aperture
            kgsl_sharedmem_free0(&device->memstore, GSL_CALLER_PROCESSID_GET());
            status = kgsl_sharedmem_alloc0(device->id, GSL_MEMFLAGS_ALIGNPAGE, sizeof(gsl_devmemstore_t), &device->memstore);
        });

        if (status != GSL_SUCCESS)
        {
            kgsl_device_stop(device->id);
            return (status);
        }
        kgsl_sharedmem_set0(&device->memstore, 0, 0, device->memstore.size);

        // init memqueue
        device->memqueue.head = NULL;
        device->memqueue.tail = NULL;

        // init cmdstream
        status = kgsl_cmdstream_init(device);
        if (status != GSL_SUCCESS)
        {
            kgsl_device_stop(device->id);
            return (status);
        }

#ifndef _LINUX		
        // Create timestamp event
        device->timestamp_event = kos_event_create(0);
        if( !device->timestamp_event )
        {
            kgsl_device_stop(device->id);
            return (status);
        }
#else
		// Create timestamp wait queue
		init_waitqueue_head(&device->timestamp_waitq);
#endif	

        //
        //  Read the chip ID after the device has been initialized.
        //
        device->chip_id       = kgsl_hal_getchipid(device->id);
    }


    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_init. Return value %B\n", status );

    return (status);
}

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

int
kgsl_device_close(gsl_device_t *device)
{
    int  status = GSL_FAILURE_NOTINITIALIZED;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_close(gsl_device_t *device=0x%08x )\n", device );

    // close cmdstream
    status = kgsl_cmdstream_close(device);
    if( status != GSL_SUCCESS ) return status;

    if (device->flags & GSL_FLAGS_INITIALIZED)
    {
        if (device->ftbl.device_close)
        {
            status = device->ftbl.device_close(device);
        }
    }

    // DumpX allocates memstore from MMU aperture
    if ((device->refcnt == 0) && device->memstore.hostptr
	&& !(gsl_driver.flags_debug & GSL_DBGFLAGS_DUMPX))
    {
	kgsl_sharedmem_free0(&device->memstore, GSL_CALLER_PROCESSID_GET());
    }

#ifndef _LINUX	
    // destroy timestamp event
    if(device->timestamp_event)
    {
        kos_event_signal(device->timestamp_event);  // wake up waiting threads before destroying the structure
        kos_event_destroy( device->timestamp_event );
        device->timestamp_event = 0;
    }
#else
    wake_up_interruptible_all(&(device->timestamp_waitq));
#endif	

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_close. Return value %B\n", status );
    
    return (status);
}

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

int
kgsl_device_destroy(gsl_device_t *device)
{
    int  status = GSL_FAILURE_NOTINITIALIZED;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_destroy(gsl_device_t *device=0x%08x )\n", device );

    if (device->flags & GSL_FLAGS_INITIALIZED)
    {
        if (device->ftbl.device_destroy)
        {
            status = device->ftbl.device_destroy(device);
        }
    }

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_destroy. Return value %B\n", status );

    return (status);
}

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

int
kgsl_device_attachcallback(gsl_device_t *device, unsigned int pid)
{
    int  status = GSL_SUCCESS;
    int  pindex;

#ifndef GSL_NO_MMU

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "--> int kgsl_device_attachcallback(gsl_device_t *device=0x%08x, unsigned int pid=0x%08x)\n", device, pid );

    if (device->flags & GSL_FLAGS_INITIALIZED)
    {
        if (kgsl_driver_getcallerprocessindex(pid, &pindex) == GSL_SUCCESS)
        {
            device->callerprocess[pindex] = pid;

            status = kgsl_mmu_attachcallback(&device->mmu, pid);
        }
    }

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_attachcallback. Return value: %B\n", status );

#else
    (void)pid;
    (void)device;
#endif

    return (status);
}

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

int
kgsl_device_detachcallback(gsl_device_t *device, unsigned int pid)
{
    int  status = GSL_SUCCESS;
    int  pindex;

#ifndef GSL_NO_MMU

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "--> int kgsl_device_detachcallback(gsl_device_t *device=0x%08x, unsigned int pid=0x%08x)\n", device, pid );

    if (device->flags & GSL_FLAGS_INITIALIZED)
    {
        if (kgsl_driver_getcallerprocessindex(pid, &pindex) == GSL_SUCCESS)
        {
            status |= kgsl_mmu_detachcallback(&device->mmu, pid);

            device->callerprocess[pindex] = 0;
        }
    }

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_detachcallback. Return value: %B\n", status );

#else
    (void)pid;
    (void)device;
#endif

    return (status);
}

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

KGSL_API int
kgsl_device_getproperty(gsl_deviceid_t device_id, gsl_property_type_t type, void *value, unsigned int sizebytes)
{
    int           status  = GSL_SUCCESS;
    gsl_device_t  *device = &gsl_driver.device[device_id-1];        // device_id is 1 based

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_getproperty(gsl_deviceid_t device_id=%D, gsl_property_type_t type=%d, void *value=0x08x, unsigned int sizebytes=%d)\n", device_id, type, value, sizebytes );

    KOS_ASSERT(value);

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

    switch (type)
    {
    case GSL_PROP_SHMEM:
        {
        gsl_shmemprop_t  *shem = (gsl_shmemprop_t *) value;

        KOS_ASSERT(sizebytes == sizeof(gsl_shmemprop_t));

        shem->numapertures   = gsl_driver.shmem.numapertures;
        shem->aperture_mask  = GSL_APERTURE_MASK;
        shem->aperture_shift = GSL_APERTURE_SHIFT;

        break;
        }

    case GSL_PROP_SHMEM_APERTURES:
        {
        int i;
        gsl_apertureprop_t  *aperture = (gsl_apertureprop_t *) value;

        KOS_ASSERT(sizebytes == (sizeof(gsl_apertureprop_t) * gsl_driver.shmem.numapertures));

        for (i = 0; i < gsl_driver.shmem.numapertures; i++)
        {
            if (gsl_driver.shmem.apertures[i].memarena)
            {
                aperture->gpuaddr  = GSL_APERTURE_GETGPUADDR(gsl_driver.shmem, i);
                aperture->hostaddr = GSL_APERTURE_GETHOSTADDR(gsl_driver.shmem, i);
            }
            else
            {
                aperture->gpuaddr  = 0x0;
                aperture->hostaddr = 0x0;
            }
            aperture++;
        }

        break;
        }

    case GSL_PROP_DEVICE_SHADOW:
        {
        gsl_shadowprop_t  *shadowprop = (gsl_shadowprop_t *) value;

        KOS_ASSERT(sizebytes == sizeof(gsl_shadowprop_t));

        kos_memset(shadowprop, 0, sizeof(gsl_shadowprop_t));

#ifdef  GSL_DEVICE_SHADOW_MEMSTORE_TO_USER
        if (device->memstore.hostptr)
        {
            shadowprop->hostaddr = (unsigned int) device->memstore.hostptr;
            shadowprop->size     = device->memstore.size;
            shadowprop->flags    = GSL_FLAGS_INITIALIZED;
        }
#endif // GSL_DEVICE_SHADOW_MEMSTORE_TO_USER

        break;
        }

    default:
        {
        if (device->ftbl.device_getproperty)
        {
            status = device->ftbl.device_getproperty(device, type, value, sizebytes);
        }

        break;
        }
    }

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_getproperty. Return value %B\n", status );

    return (status);
}

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

KGSL_API int
kgsl_device_setproperty(gsl_deviceid_t device_id, gsl_property_type_t type, void *value, unsigned int sizebytes)
{
    int           status = GSL_SUCCESS;
    gsl_device_t  *device;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_setproperty(gsl_deviceid_t device_id=%D, gsl_property_type_t type=%d, void *value=0x08x, unsigned int sizebytes=%d)\n", device_id, type, value, sizebytes );

    KOS_ASSERT(value);

    GSL_API_MUTEX_LOCK();

    device = &gsl_driver.device[device_id-1];       // device_id is 1 based

    if (device->flags & GSL_FLAGS_INITIALIZED)
    {
        if (device->ftbl.device_setproperty)
        {
            status = device->ftbl.device_setproperty(device, type, value, sizebytes);
        }
    }

    GSL_API_MUTEX_UNLOCK();

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_setproperty. Return value %B\n", status );

    return (status);
}

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

KGSL_API int
kgsl_device_start(gsl_deviceid_t device_id, gsl_flags_t flags)
{
    int           status = GSL_FAILURE_NOTINITIALIZED;
    gsl_device_t  *device;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_start(gsl_deviceid_t device_id=%D, gsl_flags_t flags=%d)\n", device_id, flags );

    GSL_API_MUTEX_LOCK();

    device = &gsl_driver.device[device_id-1];       // device_id is 1 based
    
    kgsl_device_active(device);
    
    if (!(device->flags & GSL_FLAGS_INITIALIZED))
    {
        GSL_API_MUTEX_UNLOCK();

        kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_ERROR, "ERROR: Trying to start uninitialized device.\n" );
        kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_start. Return value %B\n", GSL_FAILURE );
        return (GSL_FAILURE);
    }

    device->refcnt++;

    if (device->flags & GSL_FLAGS_STARTED)
    {
        GSL_API_MUTEX_UNLOCK();
        kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_start. Return value %B\n", GSL_SUCCESS );
        return (GSL_SUCCESS);
    }

    // start device in safe mode
    if (flags & GSL_FLAGS_SAFEMODE)
    {
        kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_INFO, "Running the device in safe mode.\n" );
        device->flags |= GSL_FLAGS_SAFEMODE;
    }

    if (device->ftbl.device_start)
    {
        status = device->ftbl.device_start(device, flags);
    }

    GSL_API_MUTEX_UNLOCK();

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_start. Return value %B\n", status );

    return (status);
}

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

KGSL_API int
kgsl_device_stop(gsl_deviceid_t device_id)
{
    int           status = GSL_FAILURE_NOTINITIALIZED;
    gsl_device_t  *device;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_stop(gsl_deviceid_t device_id=%D)\n", device_id );

    GSL_API_MUTEX_LOCK();

    device = &gsl_driver.device[device_id-1];       // device_id is 1 based

    if (device->flags & GSL_FLAGS_STARTED)
    {
        KOS_ASSERT(device->refcnt);

        device->refcnt--;

        if (device->refcnt == 0)
        {
            if (device->ftbl.device_stop)
            {
                status = device->ftbl.device_stop(device);
            }
        }
        else
        {
            status = GSL_SUCCESS;
        }
    }

    GSL_API_MUTEX_UNLOCK();

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_stop. Return value %B\n", status );

    return (status);
}

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

KGSL_API int
kgsl_device_idle(gsl_deviceid_t device_id, unsigned int timeout)
{
    int           status = GSL_FAILURE_NOTINITIALIZED;
    gsl_device_t  *device;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_idle(gsl_deviceid_t device_id=%D, unsigned int timeout=%d)\n", device_id, timeout );

    GSL_API_MUTEX_LOCK();

    device = &gsl_driver.device[device_id-1];       // device_id is 1 based

    kgsl_device_active(device);
    
    if (device->ftbl.device_idle)
    {
        status = device->ftbl.device_idle(device, timeout);
    }

    GSL_API_MUTEX_UNLOCK();

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_idle. Return value %B\n", status );

    return (status);
}

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

KGSL_API int
kgsl_device_regread(gsl_deviceid_t device_id, unsigned int offsetwords, unsigned int *value)
{
    int           status = GSL_FAILURE_NOTINITIALIZED;
    gsl_device_t  *device;


#ifdef GSL_LOG
    if( offsetwords != mmRBBM_STATUS && offsetwords != mmCP_RB_RPTR ) // Would otherwise flood the log
        kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                        "--> int kgsl_device_regread(gsl_deviceid_t device_id=%D, unsigned int offsetwords=%R, unsigned int *value=0x%08x)\n", device_id, offsetwords, value );
#endif

    GSL_API_MUTEX_LOCK();

    device = &gsl_driver.device[device_id-1];       // device_id is 1 based

    KOS_ASSERT(value);
    KOS_ASSERT(offsetwords < device->regspace.sizebytes);

    if (device->ftbl.device_regread)
    {
        status = device->ftbl.device_regread(device, offsetwords, value);
    }

    GSL_API_MUTEX_UNLOCK();

#ifdef GSL_LOG
    if( offsetwords != mmRBBM_STATUS && offsetwords != mmCP_RB_RPTR )
        kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_regread. Return value %B\n", status );
#endif

    return (status);
}

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

KGSL_API int
kgsl_device_regwrite(gsl_deviceid_t device_id, unsigned int offsetwords, unsigned int value)
{
    int           status = GSL_FAILURE_NOTINITIALIZED;
    gsl_device_t  *device;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_regwrite(gsl_deviceid_t device_id=%D, unsigned int offsetwords=%R, unsigned int value=0x%08x)\n", device_id, offsetwords, value );

    GSL_API_MUTEX_LOCK();

    device = &gsl_driver.device[device_id-1];       // device_id is 1 based

    KOS_ASSERT(offsetwords < device->regspace.sizebytes);

    if (device->ftbl.device_regwrite)
    {
        status = device->ftbl.device_regwrite(device, offsetwords, value);
    }

    GSL_API_MUTEX_UNLOCK();

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_regwrite. Return value %B\n", status );

    return (status);
}

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

KGSL_API int
kgsl_device_waitirq(gsl_deviceid_t device_id, gsl_intrid_t intr_id, unsigned int *count, unsigned int timeout)
{
    int           status = GSL_FAILURE_NOTINITIALIZED;
    gsl_device_t  *device;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_waitirq(gsl_deviceid_t device_id=%D, gsl_intrid_t intr_id=%d, unsigned int *count=0x%08x, unsigned int timout=0x%08x)\n", device_id, intr_id, count, timeout);

    GSL_API_MUTEX_LOCK();

    device = &gsl_driver.device[device_id-1];       // device_id is 1 based

    if (device->ftbl.device_waitirq)
    {
        status = device->ftbl.device_waitirq(device, intr_id, count, timeout);
    }

    GSL_API_MUTEX_UNLOCK();

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_waitirq. Return value %B\n", status );

    return (status);
}

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

int
kgsl_device_runpending(gsl_device_t *device)
{
    int  status = GSL_FAILURE_NOTINITIALIZED;

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_device_runpending(gsl_device_t *device=0x%08x )\n", device );

    if (device->flags & GSL_FLAGS_INITIALIZED)
    {
        if (device->ftbl.device_runpending)
        {
            status = device->ftbl.device_runpending(device);
        }
    }

    // free any pending freeontimestamps
    kgsl_cmdstream_memqueue_drain(device);

    kgsl_log_write( KGSL_LOG_GROUP_DEVICE | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_device_runpending. Return value %B\n", status );

    return (status);
}