summaryrefslogtreecommitdiff
path: root/drivers/mxc/amd-gpu/common/gsl_memmgr.c
blob: 75f250ae59b1edbe88ed5f7240433ec74c9c751f (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
/* 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"

//////////////////////////////////////////////////////////////////////////////
//  defines                    
//////////////////////////////////////////////////////////////////////////////
#define GSL_MEMARENAPRIV_SIGNATURE_MASK         0x0000FFFF
#define GSL_MEMARENAPRIV_APERTUREID_MASK        0xF0000000
#define GSL_MEMARENAPRIV_MMUVIRTUALIZED_MASK    0x0F000000

#define GSL_MEMARENAPRIV_SIGNATURE_SHIFT        0
#define GSL_MEMARENAPRIV_MMUVIRTUALIZED_SHIFT   24
#define GSL_MEMARENAPRIV_APERTUREID_SHIFT       28

#define GSL_MEMARENA_INSTANCE_SIGNATURE         0x0000CAFE

#ifdef GSL_STATS_MEM
#define GSL_MEMARENA_STATS(x)   x
#else
#define GSL_MEMARENA_STATS(x)
#endif // GSL_STATS_MEM


/////////////////////////////////////////////////////////////////////////////
// macros
//////////////////////////////////////////////////////////////////////////////
#define GSL_MEMARENA_LOCK()                 kos_mutex_lock(memarena->mutex)
#define GSL_MEMARENA_UNLOCK()               kos_mutex_unlock(memarena->mutex)

#define GSL_MEMARENA_SET_SIGNATURE          (memarena->priv |= ((GSL_MEMARENA_INSTANCE_SIGNATURE << GSL_MEMARENAPRIV_SIGNATURE_SHIFT) & GSL_MEMARENAPRIV_SIGNATURE_MASK))
#define GSL_MEMARENA_SET_MMU_VIRTUALIZED    (memarena->priv |= ((mmu_virtualized << GSL_MEMARENAPRIV_MMUVIRTUALIZED_SHIFT) & GSL_MEMARENAPRIV_MMUVIRTUALIZED_MASK))
#define GSL_MEMARENA_SET_ID                 (memarena->priv |= ((aperture_id << GSL_MEMARENAPRIV_APERTUREID_SHIFT) & GSL_MEMARENAPRIV_APERTUREID_MASK))

#define GSL_MEMARENA_GET_SIGNATURE          ((memarena->priv & GSL_MEMARENAPRIV_SIGNATURE_MASK)   >> GSL_MEMARENAPRIV_SIGNATURE_SHIFT)
#define GSL_MEMARENA_IS_MMU_VIRTUALIZED     ((memarena->priv & GSL_MEMARENAPRIV_MMUVIRTUALIZED_MASK) >> GSL_MEMARENAPRIV_MMUVIRTUALIZED_SHIFT)
#define GSL_MEMARENA_GET_ID                 ((memarena->priv & GSL_MEMARENAPRIV_APERTUREID_MASK)  >> GSL_MEMARENAPRIV_APERTUREID_SHIFT)


//////////////////////////////////////////////////////////////////////////////
//  validate
//////////////////////////////////////////////////////////////////////////////
#define GSL_MEMARENA_VALIDATE(memarena)                                 \
    KOS_ASSERT(memarena);                                               \
    if (GSL_MEMARENA_GET_SIGNATURE != GSL_MEMARENA_INSTANCE_SIGNATURE)  \
    {                                                                   \
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_ERROR,   \
                        "ERROR: Memarena validation failed.\n" );       \
        return (GSL_FAILURE);                                           \
    }

//////////////////////////////////////////////////////////////////////////////
//  block alignment shift count
//////////////////////////////////////////////////////////////////////////////
OSINLINE unsigned int
gsl_memarena_alignmentshift(gsl_flags_t flags)
{
    int alignshift = ((flags & GSL_MEMFLAGS_ALIGN_MASK) >> GSL_MEMFLAGS_ALIGN_SHIFT);
    if (alignshift == 0)
        alignshift = 5; //  32 bytes is the minimum alignment boundary
    return (alignshift);
}

//////////////////////////////////////////////////////////////////////////////
//  address alignment
//////////////////////////////////////////////////////////////////////////////
OSINLINE unsigned int
gsl_memarena_alignaddr(unsigned int address, int shift)
{
    //
    //  the value of the returned address is guaranteed to be an even multiple
    //  of the block alignment shift specified.
    //
    unsigned int alignedbaseaddr = ((address) >> shift) << shift;
    if (alignedbaseaddr < address)
    {
        alignedbaseaddr += (1 << shift);
    }
    return (alignedbaseaddr);
}


//////////////////////////////////////////////////////////////////////////////
//                         memory management API
//////////////////////////////////////////////////////////////////////////////

OSINLINE memblk_t*
kgsl_memarena_getmemblknode(gsl_memarena_t *memarena)
{
#ifdef GSL_MEMARENA_NODE_POOL_ENABLED
    gsl_nodepool_t  *nodepool    = memarena->nodepool;
    memblk_t        *memblk      = NULL;
    int             allocnewpool = 1;
    int             i;

    if (nodepool)
    {
        // walk through list of existing pools
        for ( ; ; )
        {
            // if there is a pool with a free memblk node
            if (nodepool->priv != (1 << GSL_MEMARENA_NODE_POOL_MAX)-1)
            {
                // get index of the first free memblk node
                for (i = 0; i < GSL_MEMARENA_NODE_POOL_MAX; i++)
                {
                    if (((nodepool->priv >> i) & 0x1) == 0)
                    {
                        break;
                    }
                }

                // mark memblk node as used
                nodepool->priv |= 1 << i;

                memblk                = &nodepool->memblk[i];
                memblk->nodepoolindex = i;
                memblk->blkaddr       = 0;
                memblk->blksize       = 0;

                allocnewpool = 0;

                break;
            }
            else        
            {
                nodepool = nodepool->next;

                if (nodepool == memarena->nodepool)
                {
                    // no free memblk node found
                    break;
                }
            }
        }
    }

    // if no existing pool has a free memblk node
    if (allocnewpool)
    {
        // alloc new pool of memblk nodes
        nodepool = ((gsl_nodepool_t *)kos_malloc(sizeof(gsl_nodepool_t)));
        if (nodepool)
        {
            kos_memset(nodepool, 0, sizeof(gsl_nodepool_t));

            if (memarena->nodepool)
            {
                nodepool->next                 = memarena->nodepool->next;
                nodepool->prev                 = memarena->nodepool;
                memarena->nodepool->next->prev = nodepool;
                memarena->nodepool->next       = nodepool;
            }
            else
            {
                nodepool->next = nodepool;
                nodepool->prev = nodepool;
            }

            // reposition pool head
            memarena->nodepool = nodepool;

            // mark memblk node as used
            nodepool->priv       |= 0x1;

            memblk                = &nodepool->memblk[0];
            memblk->nodepoolindex = 0;
        }
    }

    KOS_ASSERT(memblk);

    return (memblk);
#else
    // unreferenced formal parameter
    (void) memarena;

    return ((memblk_t *)kos_malloc(sizeof(memblk_t)));
#endif // GSL_MEMARENA_NODE_POOL_ENABLED
}

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

OSINLINE void
kgsl_memarena_releasememblknode(gsl_memarena_t *memarena, memblk_t *memblk)
{
#ifdef GSL_MEMARENA_NODE_POOL_ENABLED
    gsl_nodepool_t *nodepool = memarena->nodepool;

    KOS_ASSERT(memblk);
    KOS_ASSERT(nodepool);

    // locate pool to which this memblk node belongs
    while (((unsigned int) memblk) < ((unsigned int) nodepool) || 
           ((unsigned int) memblk) > ((unsigned int) nodepool) + sizeof(gsl_nodepool_t))
    {
        nodepool = nodepool->prev;

        KOS_ASSERT(nodepool != memarena->nodepool);
    }

    // mark memblk node as unused
    nodepool->priv &= ~(1 << memblk->nodepoolindex);

    // free pool when all its memblk nodes are unused
    if (nodepool->priv == 0)
    {
        if (nodepool != nodepool->prev)
        {
            // reposition pool head
            if (nodepool == memarena->nodepool)
            {
                memarena->nodepool = nodepool->prev;
            }

            nodepool->prev->next = nodepool->next;
            nodepool->next->prev = nodepool->prev;
        }
        else
        {
            memarena->nodepool = NULL;
        }

        kos_free((void *)nodepool);
    }
    else
    {
        // leave pool head in last pool a memblk node was released
        memarena->nodepool = nodepool;
    }
#else
    // unreferenced formal parameter
    (void) memarena;

    kos_free((void *)memblk);
#endif // GSL_MEMARENA_NODE_POOL_ENABLED
}

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

gsl_memarena_t*
kgsl_memarena_create(int aperture_id, int mmu_virtualized, unsigned int hostbaseaddr, gpuaddr_t gpubaseaddr, int sizebytes)
{
    static int      count = 0;
    char            name[100], id_str[2];
    int             len;
    gsl_memarena_t  *memarena;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> gsl_memarena_t* kgsl_memarena_create(int aperture_id=%d, gpuaddr_t gpubaseaddr=0x%08x, int sizebytes=%d)\n", aperture_id, gpubaseaddr, sizebytes );

    memarena = (gsl_memarena_t *)kos_malloc(sizeof(gsl_memarena_t));

    if (!memarena) 
    {
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_ERROR,
                        "ERROR: Memarena allocation failed.\n" );
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_ERROR, "<-- kgsl_memarena_create. Return value: 0x%08x\n", NULL );
        return (NULL);
    }

    kos_memset(memarena, 0, sizeof(gsl_memarena_t));

    GSL_MEMARENA_SET_SIGNATURE;
    GSL_MEMARENA_SET_MMU_VIRTUALIZED;
    GSL_MEMARENA_SET_ID;

    // define unique mutex for each memory arena instance
    id_str[0] = (char) (count + '0');
    id_str[1] = '\0';
    kos_strcpy(name, "GSL_memory_arena_");
    len = kos_strlen(name);
    kos_strcpy(&name[len], id_str);

    memarena->mutex = kos_mutex_create(name);

    // set up the memory arena
    memarena->hostbaseaddr = hostbaseaddr;
    memarena->gpubaseaddr  = gpubaseaddr;
    memarena->sizebytes    = sizebytes;

    // allocate a memory block in free list which represents all memory in arena
    memarena->freelist.head          = kgsl_memarena_getmemblknode(memarena);
    memarena->freelist.head->blkaddr = 0;
    memarena->freelist.head->blksize = memarena->sizebytes;
    memarena->freelist.head->next    = memarena->freelist.head;
    memarena->freelist.head->prev    = memarena->freelist.head;
    memarena->freelist.allocrover    = memarena->freelist.head;
    memarena->freelist.freerover     = memarena->freelist.head;

    count++;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_create. Return value: 0x%08x\n", memarena );

    return (memarena);
}

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

int         
kgsl_memarena_destroy(gsl_memarena_t *memarena)
{
    int       status = GSL_SUCCESS;
    memblk_t  *p, *next;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_memarena_destroy(gsl_memarena_t *memarena=0x%08x)\n", memarena );

    GSL_MEMARENA_VALIDATE(memarena);

    GSL_MEMARENA_LOCK();

#ifdef _DEBUG
    // memory leak check
    if (memarena->freelist.head->blksize != memarena->sizebytes)
    {
        if (GSL_MEMARENA_GET_ID == GSL_APERTURE_EMEM)
        {
            // external memory leak detected
            kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_FATAL,
                            "ERROR: External memory leak detected.\n" );
            return (GSL_FAILURE);
        }
    }
#endif // _DEBUG

    p = memarena->freelist.head;
    do
    {
        next = p->next;
        kgsl_memarena_releasememblknode(memarena, p);
        p = next;
    } while (p != memarena->freelist.head);

    GSL_MEMARENA_UNLOCK();

    if (memarena->mutex)
    {
        kos_mutex_free(memarena->mutex);
    }

    kos_free((void *)memarena);

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

    return (status);
}

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

int
kgsl_memarena_isvirtualized(gsl_memarena_t *memarena)
{
    // mmu virtualization enabled
    return (GSL_MEMARENA_IS_MMU_VIRTUALIZED);
}

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

int
kgsl_memarena_checkconsistency(gsl_memarena_t *memarena)
{
    memblk_t *p;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_memarena_checkconsistency(gsl_memarena_t *memarena=0x%08x)\n", memarena );

    // go through list of free blocks and make sure there are no detectable errors

    p = memarena->freelist.head;
    do
    {
        if (p->next->blkaddr != memarena->freelist.head->blkaddr)
        {
            if (p->prev->next->blkaddr  != p->blkaddr || 
                p->next->prev->blkaddr  != p->blkaddr ||
                p->blkaddr + p->blksize >= p->next->blkaddr)
            {
                KOS_ASSERT(0);
                kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_checkconsistency. Return value: %B\n", GSL_FAILURE );
                return (GSL_FAILURE);
            }
        }
        p = p->next;

    } while (p != memarena->freelist.head);

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

    return (GSL_SUCCESS);
}

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

int             
kgsl_memarena_querystats(gsl_memarena_t *memarena, gsl_memarena_stats_t *stats)
{
#ifdef GSL_STATS_MEM
    KOS_ASSERT(stats);
    GSL_MEMARENA_VALIDATE(memarena);

    kos_memcpy(stats, &memarena->stats, sizeof(gsl_memarena_stats_t));
    
    return (GSL_SUCCESS);
#else
    // unreferenced formal parameters
    (void) memarena;
    (void) stats;

    return (GSL_FAILURE_NOTSUPPORTED);
#endif // GSL_STATS_MEM
}

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

int         
kgsl_memarena_checkfreeblock(gsl_memarena_t *memarena, int bytesneeded)
{
    memblk_t  *p;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_memarena_checkfreeblock(gsl_memarena_t *memarena=0x%08x, int bytesneeded=%d)\n", memarena, bytesneeded );

    GSL_MEMARENA_VALIDATE(memarena);

    if (bytesneeded < 1)
    {
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_ERROR, "ERROR: Illegal number of bytes needed.\n" );
        KOS_ASSERT(0);
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_checkfreeblock. Return value: %B\n", GSL_FAILURE );
        return (GSL_FAILURE);
    }

    GSL_MEMARENA_LOCK();

    p = memarena->freelist.head;
    do
    {
        if (p->blksize >= (unsigned int)bytesneeded)
        {
            kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_checkfreeblock. Return value: %B\n", GSL_SUCCESS );
            return (GSL_SUCCESS);
        }

        p = p->next;
    } while (p != memarena->freelist.head);

    GSL_MEMARENA_UNLOCK();

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

    return (GSL_FAILURE);
}

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

int 
kgsl_memarena_alloc(gsl_memarena_t *memarena, gsl_flags_t flags, int size, gsl_memdesc_t *memdesc)
{
    int           result = GSL_FAILURE_OUTOFMEM;
    memblk_t      *ptrfree, *ptrlast, *p;
    unsigned int  blksize;
    unsigned int  baseaddr, alignedbaseaddr, alignfragment;
    int           freeblk, alignmentshift;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_memarena_alloc(gsl_memarena_t *memarena=0x%08x, gsl_flags_t flags=0x%08x, int size=%d, gsl_memdesc_t *memdesc=%M)\n", memarena, flags, size, memdesc );

    GSL_MEMARENA_VALIDATE(memarena);

    if (size <= 0)
    {
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_ERROR, "ERROR: Invalid size for memory allocation.\n" );
        KOS_ASSERT(0);
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_alloc. Return value: %B\n", GSL_FAILURE );
        return (GSL_FAILURE);
    }

    //
    // go through the list of free blocks.  check to find block which can satisfy the alloc request
    //
    // if no block can satisfy the alloc request this implies that the memory is too fragmented
    // and the requestor needs to free up other memory blocks and re-request the allocation
    //
    // if we do find a block that can satisfy the alloc request then reduce the size of free block
    // by blksize and return the address after allocating the memory.  if the free block size becomes
    // 0 then remove this node from the free list
    //
    // there would be no node on the free list if all available memory were to be allocated.  
    // handling an empty list would require executing error checking code in the main branch which 
    // is not desired.  instead, the free list will have at least one node at all times. This node 
    // could have a block size of zero
    //
    // we use a next fit allocation mechanism that uses a roving pointer on a circular free block list. 
    // the pointer is advanced along the chain when searching for a fit. Thus each allocation begins
    // looking where the previous one finished. 
    //

    // when allocating from external memory aperture, round up size of requested block to multiple of page size if needed
    if (GSL_MEMARENA_GET_ID == GSL_APERTURE_EMEM)
    {
        if ((flags & GSL_MEMFLAGS_FORCEPAGESIZE) || GSL_MEMARENA_IS_MMU_VIRTUALIZED)
        {
            if (size & (GSL_PAGESIZE-1))
            {
                size = ((size >> GSL_PAGESIZE_SHIFT) + 1) << GSL_PAGESIZE_SHIFT;
            }
        }
    }

    // determine shift count for alignment requested
    alignmentshift = gsl_memarena_alignmentshift(flags);

    // adjust size of requested block to include alignment
    blksize = (unsigned int)((size + ((1 << alignmentshift) - 1)) >> alignmentshift) << alignmentshift;

    GSL_MEMARENA_LOCK();

    // check consistency, debug only
    KGSL_DEBUG(GSL_DBGFLAGS_MEMMGR, kgsl_memarena_checkconsistency(memarena));

    ptrfree = memarena->freelist.allocrover;
    ptrlast = memarena->freelist.head->prev;
    freeblk = 0;

    do
    { 
        // align base address
        baseaddr = ptrfree->blkaddr + memarena->gpubaseaddr;
        alignedbaseaddr = gsl_memarena_alignaddr(baseaddr, alignmentshift);
       
        alignfragment = alignedbaseaddr - baseaddr;

        if (ptrfree->blksize >= blksize + alignfragment)
        {
            result  = GSL_SUCCESS;
            freeblk = 1;

            memdesc->gpuaddr = alignedbaseaddr;
            memdesc->hostptr = kgsl_memarena_gethostptr(memarena, memdesc->gpuaddr);
            memdesc->size    = blksize;

            if (alignfragment > 0)
            {
                // insert new node to handle newly created (small) fragment
                p = kgsl_memarena_getmemblknode(memarena);
                p->blkaddr = ptrfree->blkaddr;
                p->blksize = alignfragment;

                p->next = ptrfree;
                p->prev = ptrfree->prev;
                ptrfree->prev->next = p;
                ptrfree->prev       = p;

                if (ptrfree == memarena->freelist.head)
                {
                    memarena->freelist.head = p;
                }
            }

            ptrfree->blkaddr += alignfragment + blksize;
            ptrfree->blksize -= alignfragment + blksize;

            memarena->freelist.allocrover = ptrfree;

            if (ptrfree->blksize == 0 && ptrfree != ptrlast)
            {
                ptrfree->prev->next = ptrfree->next;
                ptrfree->next->prev = ptrfree->prev;
                if (ptrfree == memarena->freelist.head)
                {
                    memarena->freelist.head = ptrfree->next;
                }
                if (ptrfree == memarena->freelist.allocrover)
                {
                    memarena->freelist.allocrover = ptrfree->next;
                }
                if (ptrfree == memarena->freelist.freerover)
                {
                    memarena->freelist.freerover = ptrfree->prev;
                }
                p       = ptrfree;
                ptrfree = ptrfree->prev;
                kgsl_memarena_releasememblknode(memarena, p);
            }
        }

        ptrfree = ptrfree->next;

    } while (!freeblk && ptrfree != memarena->freelist.allocrover);

    GSL_MEMARENA_UNLOCK();

    if (result == GSL_SUCCESS)
    {
        GSL_MEMARENA_STATS(
        {   
            int i = 0;
            while (memdesc->size >> (GSL_PAGESIZE_SHIFT + i))
            {
                i++;
            }
            i = i > (GSL_MEMARENA_PAGE_DIST_MAX-1) ? (GSL_MEMARENA_PAGE_DIST_MAX-1) : i;
            memarena->stats.allocs_pagedistribution[i]++;
        });

        GSL_MEMARENA_STATS(memarena->stats.allocs_success++);
    }
    else
    {
        GSL_MEMARENA_STATS(memarena->stats.allocs_fail++);
    }

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_alloc. Return value: %B\n", result );
        
    return (result);
}

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

void
kgsl_memarena_free(gsl_memarena_t *memarena, gsl_memdesc_t *memdesc)
{
    //
    // request to free a malloc'ed block from the memory arena
    // add this block to the free list
    // adding a block to the free list requires the following:
    // going through the list of free blocks to decide where to add this free block (based on address)
    // coalesce free blocks
    //
    memblk_t      *ptrfree, *ptrend, *p;
    int           mallocfreeblk, clockwise;
    unsigned int  addrtofree;
    
    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> void kgsl_memarena_free(gsl_memarena_t *memarena=0x%08x, gsl_memdesc_t *memdesc=%M)\n", memarena, memdesc );

    KOS_ASSERT(memarena);
    if (GSL_MEMARENA_GET_SIGNATURE != GSL_MEMARENA_INSTANCE_SIGNATURE)
    {
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_free.\n" );
        return;
    }

    // check size of malloc'ed block 
    if (memdesc->size <= 0)
    {
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_ERROR, "ERROR: Illegal size for the memdesc.\n" );
        KOS_ASSERT(0);

        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_free.\n" );
        return;
    }

    // check address range
    KOS_ASSERT( memarena->gpubaseaddr <= memdesc->gpuaddr);
    KOS_ASSERT((memarena->gpubaseaddr + memarena->sizebytes) >= memdesc->gpuaddr + memdesc->size);

    GSL_MEMARENA_LOCK();

    // check consistency of memory map, debug only
    KGSL_DEBUG(GSL_DBGFLAGS_MEMMGR, kgsl_memarena_checkconsistency(memarena));

    addrtofree    = memdesc->gpuaddr - memarena->gpubaseaddr;
    mallocfreeblk = 1;

    if (addrtofree < memarena->freelist.head->blkaddr)
    {
        // add node to head of free list

        if (addrtofree + memdesc->size == memarena->freelist.head->blkaddr)
        {
            memarena->freelist.head->blkaddr  = addrtofree;
            memarena->freelist.head->blksize += memdesc->size;

            mallocfreeblk = 0;
        }

        ptrfree = memarena->freelist.head->prev;
    }
    else if (addrtofree >= memarena->freelist.head->prev->blkaddr)
    {
        // add node to tail of free list

        ptrfree = memarena->freelist.head->prev;

        if (ptrfree->blkaddr + ptrfree->blksize == addrtofree)
        {
            ptrfree->blksize += memdesc->size;

            mallocfreeblk = 0;
        }
    }
    else
    {
        // determine range of free list nodes to traverse and orientation in which to traverse them
        // keep this code segment unrolled for performance reasons!
        if (addrtofree > memarena->freelist.freerover->blkaddr)
        {
            if (addrtofree - memarena->freelist.freerover->blkaddr < memarena->freelist.head->prev->blkaddr - addrtofree)
            {
                ptrfree   = memarena->freelist.freerover;       // rover
                ptrend    = memarena->freelist.head->prev;      // tail
                clockwise = 1;
            }
            else
            {
                ptrfree   = memarena->freelist.head->prev->prev; // tail
                ptrend    = memarena->freelist.freerover->prev;  // rover
                clockwise = 0;
            }
        }
        else
        {
            if (addrtofree - memarena->freelist.head->blkaddr < memarena->freelist.freerover->blkaddr - addrtofree)
            {
                ptrfree   = memarena->freelist.head;            // head
                ptrend    = memarena->freelist.freerover;       // rover
                clockwise = 1;
            }
            else
            {
                ptrfree   = memarena->freelist.freerover->prev; // rover
                ptrend    = memarena->freelist.head->prev;      // head
                clockwise = 0;
            }
        }

        // traverse the nodes
        do
        {
            if ((addrtofree >= ptrfree->blkaddr + ptrfree->blksize) && 
                (addrtofree + memdesc->size <= ptrfree->next->blkaddr))
            {
                if (addrtofree == ptrfree->blkaddr + ptrfree->blksize)
                {
                    memblk_t  *next;

                    ptrfree->blksize += memdesc->size;
                    next              = ptrfree->next;

                    if (ptrfree->blkaddr + ptrfree->blksize == next->blkaddr)
                    {
                        ptrfree->blksize += next->blksize;
                        ptrfree->next     = next->next;
                        next->next->prev  = ptrfree;

                        if (next == memarena->freelist.allocrover)
                        {
                            memarena->freelist.allocrover = ptrfree;
                        }

                        kgsl_memarena_releasememblknode(memarena, next);
                    }

                    mallocfreeblk = 0;
                }
                else if (addrtofree + memdesc->size == ptrfree->next->blkaddr)
                {
                    ptrfree->next->blkaddr  = addrtofree;
                    ptrfree->next->blksize += memdesc->size;

                    mallocfreeblk  = 0;
                }

                break;
            }

            if (clockwise)
            {
                ptrfree = ptrfree->next;
            }
            else
            {
                ptrfree = ptrfree->prev;
            }

        } while (ptrfree != ptrend);
    }

    // this free block could not be coalesced, so create a new free block
    // and add it to the free list in the memory arena
    if (mallocfreeblk)
    {
        p = kgsl_memarena_getmemblknode(memarena);
        p->blkaddr = addrtofree;
        p->blksize = memdesc->size;

        p->next             = ptrfree->next;
        p->prev             = ptrfree;
        ptrfree->next->prev = p;
        ptrfree->next       = p;

        if (p->blkaddr < memarena->freelist.head->blkaddr)
        {
            memarena->freelist.head = p;
        }

        memarena->freelist.freerover = p;
    }
    else
    {
        memarena->freelist.freerover = ptrfree;
    }

    GSL_MEMARENA_UNLOCK();

    GSL_MEMARENA_STATS(
    {   
        int i = 0;
        while (memdesc->size >> (GSL_PAGESIZE_SHIFT + i))
        {
            i++;
        }
        i = i > (GSL_MEMARENA_PAGE_DIST_MAX-1) ? (GSL_MEMARENA_PAGE_DIST_MAX-1) : i;
        memarena->stats.frees_pagedistribution[i]++;
    });

    GSL_MEMARENA_STATS(memarena->stats.frees++);

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_free.\n" );
}

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

void *          
kgsl_memarena_gethostptr(gsl_memarena_t *memarena, gpuaddr_t gpuaddr)
{
    //
    // get the host mapped address for a hardware device address
    //

    void  *hostptr = NULL;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> void* kgsl_memarena_gethostptr(gsl_memarena_t *memarena=0x%08x, gpuaddr_t gpuaddr=0x%08x)\n", memarena, gpuaddr );

    KOS_ASSERT(memarena);
    if (GSL_MEMARENA_GET_SIGNATURE != GSL_MEMARENA_INSTANCE_SIGNATURE)
    {
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_gethostptr. Return value: 0x%08x\n", NULL );
        return (NULL);
    }

    // check address range
    KOS_ASSERT(gpuaddr >= memarena->gpubaseaddr);
    KOS_ASSERT(gpuaddr <  memarena->gpubaseaddr + memarena->sizebytes);

    hostptr = (void *)((gpuaddr - memarena->gpubaseaddr) + memarena->hostbaseaddr);

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_gethostptr. Return value: 0x%08x\n", hostptr );

    return (hostptr);
}

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

gpuaddr_t           
kgsl_memarena_getgpuaddr(gsl_memarena_t *memarena, void *hostptr)
{
    //
    // get the hardware device address for a host mapped address
    //

    gpuaddr_t  gpuaddr = 0;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> int kgsl_memarena_getgpuaddr(gsl_memarena_t *memarena=0x%08x, void *hostptr=0x%08x)\n", memarena, hostptr );

    KOS_ASSERT(memarena);
    if (GSL_MEMARENA_GET_SIGNATURE != GSL_MEMARENA_INSTANCE_SIGNATURE)
    {
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_getgpuaddr. Return value: 0x%08x\n", 0 );
        return (0);
    }

    // check address range
    KOS_ASSERT(hostptr >= (void *)memarena->hostbaseaddr);
    KOS_ASSERT(hostptr <  (void *)(memarena->hostbaseaddr + memarena->sizebytes));
    
    gpuaddr = ((unsigned int)hostptr - memarena->hostbaseaddr) + memarena->gpubaseaddr;

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_getgpuaddr. Return value: 0x%08x\n", gpuaddr );

    return (gpuaddr);
}

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

unsigned int    
kgsl_memarena_getlargestfreeblock(gsl_memarena_t *memarena, gsl_flags_t flags)
{
    memblk_t      *ptrfree;
    unsigned int  blocksize, largestblocksize = 0;
    int           alignmentshift; 

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE,
                    "--> unsigned int kgsl_memarena_getlargestfreeblock(gsl_memarena_t *memarena=0x%08x, gsl_flags_t flags=0x%08x)\n", memarena, flags );

    KOS_ASSERT(memarena);
    if (GSL_MEMARENA_GET_SIGNATURE != GSL_MEMARENA_INSTANCE_SIGNATURE) 
    {
        kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_getlargestfreeblock. Return value: %d\n", 0 );
        return (0);
    }

    // determine shift count for alignment requested
    alignmentshift = gsl_memarena_alignmentshift(flags);

    GSL_MEMARENA_LOCK();

    ptrfree = memarena->freelist.head;

    do
    {
        blocksize = ptrfree->blksize - (ptrfree->blkaddr - ((ptrfree->blkaddr >> alignmentshift) << alignmentshift));

        if (blocksize > largestblocksize)
        {
            largestblocksize = blocksize;
        }

        ptrfree = ptrfree->next;

    } while (ptrfree != memarena->freelist.head);

    GSL_MEMARENA_UNLOCK();

    kgsl_log_write( KGSL_LOG_GROUP_MEMORY | KGSL_LOG_LEVEL_TRACE, "<-- kgsl_memarena_getlargestfreeblock. Return value: %d\n", largestblocksize );

    return (largestblocksize);
}