gpu: pvr: pdumpfs: add stream_frames debugfs entry
[sgx.git] / pvr / ra.c
index e7253bf..bcd96b8 100644 (file)
--- a/pvr/ra.c
+++ b/pvr/ra.c
@@ -1,26 +1,26 @@
 /**********************************************************************
  *
  * Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
- * 
+ *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms and conditions of the GNU General Public License,
  * version 2, as published by the Free Software Foundation.
- * 
- * This program is distributed in the hope it will be useful but, except 
- * as otherwise stated in writing, without any warranty; without even the 
- * implied warranty of merchantability or fitness for a particular purpose. 
+ *
+ * This program is distributed in the hope it will be useful but, except
+ * as otherwise stated in writing, 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 St - Fifth Floor, Boston, MA 02110-1301 USA.
- * 
+ *
  * The full GNU General Public License is included in this distribution in
  * the file called "COPYING".
  *
  * Contact Information:
  * Imagination Technologies Ltd. <gpl-support@imgtec.com>
- * Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK 
+ * Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK
  *
  ******************************************************************************/
 
 #include "buffer_manager.h"
 #include "osfunc.h"
 
-#ifdef __linux__
 #include <linux/kernel.h>
 #include "proc.h"
-#endif
 
-#ifdef USE_BM_FREESPACE_CHECK
-#include <stdio.h>
-#endif
 
-#define MINIMUM_HASH_SIZE (64)
+#define MINIMUM_HASH_SIZE      64
 
-struct _BT_ {
+struct BT {
        enum bt_type {
                btt_span,
                btt_free,
                btt_live
        } type;
 
-       IMG_UINTPTR_T base;
-       IMG_SIZE_T uSize;
+       u32 base;
+       size_t uSize;
 
-       struct _BT_ *pNextSegment;
-       struct _BT_ *pPrevSegment;
+       struct BT *pNextSegment;
+       struct BT *pPrevSegment;
 
-       struct _BT_ *pNextFree;
-       struct _BT_ *pPrevFree;
+       struct BT *pNextFree;
+       struct BT *pPrevFree;
 
-       BM_MAPPING *psMapping;
+       struct BM_MAPPING *psMapping;
 };
-typedef struct _BT_ BT;
-
-struct _RA_ARENA_ {
+struct BT;
 
+struct RA_ARENA {
        char *name;
-
-       IMG_UINT32 uQuantum;
-
-        IMG_BOOL(*pImportAlloc) (void *,
-                                 IMG_SIZE_T uSize,
-                                 IMG_SIZE_T * pActualSize,
-                                 BM_MAPPING ** ppsMapping,
-                                 IMG_UINT32 uFlags, IMG_UINTPTR_T * pBase);
-       void (*pImportFree) (void *, IMG_UINTPTR_T, BM_MAPPING * psMapping);
-       void (*pBackingStoreFree) (void *, IMG_UINT32, IMG_UINT32, IMG_HANDLE);
-
+       u32 uQuantum;
+       IMG_BOOL(*pImportAlloc)(void *, size_t uSize, size_t *pActualSize,
+                               struct BM_MAPPING **ppsMapping, u32 uFlags,
+                               u32 *pBase);
+       void (*pImportFree)(void *, u32, struct BM_MAPPING *psMapping);
+       void (*pBackingStoreFree)(void *, u32, u32, void *);
        void *pImportHandle;
-
-#define FREE_TABLE_LIMIT 32
-
-       BT *aHeadFree[FREE_TABLE_LIMIT];
-
-       BT *pHeadSegment;
-       BT *pTailSegment;
-
-       HASH_TABLE *pSegmentHash;
-
+#define FREE_TABLE_LIMIT       32
+       struct BT *aHeadFree[FREE_TABLE_LIMIT];
+       struct BT *pHeadSegment;
+       struct BT *pTailSegment;
+       struct HASH_TABLE *pSegmentHash;
 #ifdef RA_STATS
-       RA_STATISTICS sStatistics;
+       struct RA_STATISTICS sStatistics;
 #endif
-
-#if defined(CONFIG_PROC_FS) && defined(DEBUG)
+#if defined(CONFIG_PROC_FS) && defined(CONFIG_PVR_DEBUG_EXTRA)
 #define PROC_NAME_SIZE         32
        char szProcInfoName[PROC_NAME_SIZE];
        char szProcSegsName[PROC_NAME_SIZE];
+       u32 ui32PID;
 #endif
 };
 
-void RA_Dump(RA_ARENA * pArena);
-
-#if defined(CONFIG_PROC_FS) && defined(DEBUG)
-static int
-RA_DumpSegs(char *page, char **start, off_t off, int count, int *eof,
-           void *data);
+#if defined(CONFIG_PROC_FS) && defined(CONFIG_PVR_DEBUG_EXTRA)
+static int RA_DumpSegs(char *page, char **start, off_t off, int count, int *eof,
+                      void *data);
 static int RA_DumpInfo(char *page, char **start, off_t off, int count, int *eof,
                       void *data);
 #endif
 
-#ifdef USE_BM_FREESPACE_CHECK
-void CheckBMFreespace(void);
+#if defined(CONFIG_PROC_FS) && defined(CONFIG_PVR_DEBUG_EXTRA)
+static char *ReplaceSpaces(char *const pS)
+{
+       char *pT;
+
+       for (pT = pS; *pT != 0; pT++)
+               if (*pT == ' ' || *pT == '\t')
+                       *pT = '_';
+
+       return pS;
+}
 #endif
 
-static IMG_BOOL
-_RequestAllocFail(void *_h,
-                 IMG_SIZE_T _uSize,
-                 IMG_SIZE_T * _pActualSize,
-                 BM_MAPPING ** _ppsMapping,
-                 IMG_UINT32 _uFlags, IMG_UINTPTR_T * _pBase)
+static IMG_BOOL _RequestAllocFail(void *_h, size_t _uSize, size_t *_pActualSize,
+                                 struct BM_MAPPING **_ppsMapping,
+                                 u32 _uFlags, u32 *_pBase)
 {
        PVR_UNREFERENCED_PARAMETER(_h);
        PVR_UNREFERENCED_PARAMETER(_uSize);
@@ -128,9 +115,9 @@ _RequestAllocFail(void *_h,
        return IMG_FALSE;
 }
 
-static IMG_UINT32 pvr_log2(IMG_SIZE_T n)
+static u32 pvr_log2(size_t n)
 {
-       IMG_UINT32 l = 0;
+       u32 l = 0;
        n >>= 1;
        while (n > 0) {
                n >>= 1;
@@ -139,65 +126,94 @@ static IMG_UINT32 pvr_log2(IMG_SIZE_T n)
        return l;
 }
 
-static void
-_SegmentListInsertAfter(RA_ARENA * pArena, BT * pInsertionPoint, BT * pBT)
+static enum PVRSRV_ERROR _SegmentListInsertAfter(struct RA_ARENA *pArena,
+                                                struct BT *pInsertionPoint,
+                                                struct BT *pBT)
 {
-       PVR_ASSERT(pArena != IMG_NULL);
-       PVR_ASSERT(pInsertionPoint != IMG_NULL);
+       PVR_ASSERT(pArena != NULL);
+       PVR_ASSERT(pInsertionPoint != NULL);
+
+       if ((pInsertionPoint == NULL) || (pArena == NULL)) {
+               PVR_DPF(PVR_DBG_ERROR,
+                        "_SegmentListInsertAfter: invalid parameters");
+               return PVRSRV_ERROR_INVALID_PARAMS;
+       }
 
        pBT->pNextSegment = pInsertionPoint->pNextSegment;
        pBT->pPrevSegment = pInsertionPoint;
-       if (pInsertionPoint->pNextSegment == IMG_NULL)
+       if (pInsertionPoint->pNextSegment == NULL)
                pArena->pTailSegment = pBT;
        else
                pInsertionPoint->pNextSegment->pPrevSegment = pBT;
        pInsertionPoint->pNextSegment = pBT;
+
+       return PVRSRV_OK;
 }
 
-static void _SegmentListInsert(RA_ARENA * pArena, BT * pBT)
+static enum PVRSRV_ERROR _SegmentListInsert(struct RA_ARENA *pArena,
+                                           struct BT *pBT)
 {
+       enum PVRSRV_ERROR eError = PVRSRV_OK;
 
-       if (pArena->pHeadSegment == IMG_NULL) {
+       if (pArena->pHeadSegment == NULL) {
                pArena->pHeadSegment = pArena->pTailSegment = pBT;
-               pBT->pNextSegment = pBT->pPrevSegment = IMG_NULL;
+               pBT->pNextSegment = pBT->pPrevSegment = NULL;
        } else {
-               BT *pBTScan;
-               pBTScan = pArena->pHeadSegment;
-               while (pBTScan->pNextSegment != IMG_NULL
-                      && pBT->base >= pBTScan->pNextSegment->base)
-                       pBTScan = pBTScan->pNextSegment;
-               _SegmentListInsertAfter(pArena, pBTScan, pBT);
+               struct BT *pBTScan;
+               if (pBT->base < pArena->pHeadSegment->base) {
+                       pBT->pNextSegment = pArena->pHeadSegment;
+                       pArena->pHeadSegment->pPrevSegment = pBT;
+                       pArena->pHeadSegment = pBT;
+                       pBT->pPrevSegment = NULL;
+               } else {
+                       pBTScan = pArena->pHeadSegment;
+
+                       while ((pBTScan->pNextSegment != NULL) &&
+                              (pBT->base >= pBTScan->pNextSegment->base))
+                               pBTScan = pBTScan->pNextSegment;
+
+                       eError = _SegmentListInsertAfter(pArena, pBTScan, pBT);
+                       if (eError != PVRSRV_OK)
+                               return eError;
+               }
        }
+       return eError;
 }
 
-static void _SegmentListRemove(RA_ARENA * pArena, BT * pBT)
+static void _SegmentListRemove(struct RA_ARENA *pArena, struct BT *pBT)
 {
-       if (pBT->pPrevSegment == IMG_NULL)
+       if (pBT->pPrevSegment == NULL)
                pArena->pHeadSegment = pBT->pNextSegment;
        else
                pBT->pPrevSegment->pNextSegment = pBT->pNextSegment;
 
-       if (pBT->pNextSegment == IMG_NULL)
+       if (pBT->pNextSegment == NULL)
                pArena->pTailSegment = pBT->pPrevSegment;
        else
                pBT->pNextSegment->pPrevSegment = pBT->pPrevSegment;
 }
 
-static BT *_SegmentSplit(RA_ARENA * pArena, BT * pBT, IMG_SIZE_T uSize)
+static struct BT *_SegmentSplit(struct RA_ARENA *pArena, struct BT *pBT,
+                               size_t uSize)
 {
-       BT *pNeighbour;
+       struct BT *pNeighbour;
 
-       PVR_ASSERT(pArena != IMG_NULL);
+       PVR_ASSERT(pArena != NULL);
 
-       if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
-                      sizeof(BT),
-                      (IMG_VOID **) & pNeighbour, IMG_NULL) != PVRSRV_OK) {
-               return IMG_NULL;
+       if (pArena == NULL) {
+               PVR_DPF(PVR_DBG_ERROR,
+                        "_SegmentSplit: invalid parameter - pArena");
+               return NULL;
        }
 
+       if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
+                      sizeof(struct BT),
+                      (void **) &pNeighbour, NULL) != PVRSRV_OK)
+               return NULL;
+
        pNeighbour->pPrevSegment = pBT;
        pNeighbour->pNextSegment = pBT->pNextSegment;
-       if (pBT->pNextSegment == IMG_NULL)
+       if (pBT->pNextSegment == NULL)
                pArena->pTailSegment = pNeighbour;
        else
                pBT->pNextSegment->pPrevSegment = pNeighbour;
@@ -211,57 +227,55 @@ static BT *_SegmentSplit(RA_ARENA * pArena, BT * pBT, IMG_SIZE_T uSize)
        return pNeighbour;
 }
 
-static void _FreeListInsert(RA_ARENA * pArena, BT * pBT)
+static void _FreeListInsert(struct RA_ARENA *pArena, struct BT *pBT)
 {
-       IMG_UINT32 uIndex;
+       u32 uIndex;
        uIndex = pvr_log2(pBT->uSize);
        pBT->type = btt_free;
        pBT->pNextFree = pArena->aHeadFree[uIndex];
-       pBT->pPrevFree = IMG_NULL;
-       if (pArena->aHeadFree[uIndex] != IMG_NULL)
+       pBT->pPrevFree = NULL;
+       if (pArena->aHeadFree[uIndex] != NULL)
                pArena->aHeadFree[uIndex]->pPrevFree = pBT;
        pArena->aHeadFree[uIndex] = pBT;
 }
 
-static void _FreeListRemove(RA_ARENA * pArena, BT * pBT)
+static void _FreeListRemove(struct RA_ARENA *pArena, struct BT *pBT)
 {
-       IMG_UINT32 uIndex;
+       u32 uIndex;
        uIndex = pvr_log2(pBT->uSize);
-       if (pBT->pNextFree != IMG_NULL)
+       if (pBT->pNextFree != NULL)
                pBT->pNextFree->pPrevFree = pBT->pPrevFree;
-       if (pBT->pPrevFree == IMG_NULL)
+       if (pBT->pPrevFree == NULL)
                pArena->aHeadFree[uIndex] = pBT->pNextFree;
        else
                pBT->pPrevFree->pNextFree = pBT->pNextFree;
 }
 
-static BT *_BuildSpanMarker(IMG_UINTPTR_T base, IMG_SIZE_T uSize)
+static struct BT *_BuildSpanMarker(u32 base, size_t uSize)
 {
-       BT *pBT;
+       struct BT *pBT;
 
        if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
-                      sizeof(BT),
-                      (IMG_VOID **) & pBT, IMG_NULL) != PVRSRV_OK) {
-               return IMG_NULL;
-       }
+                      sizeof(struct BT),
+                      (void **) &pBT, NULL) != PVRSRV_OK)
+               return NULL;
 
        pBT->type = btt_span;
        pBT->base = base;
        pBT->uSize = uSize;
-       pBT->psMapping = IMG_NULL;
+       pBT->psMapping = NULL;
 
        return pBT;
 }
 
-static BT *_BuildBT(IMG_UINTPTR_T base, IMG_SIZE_T uSize)
+static struct BT *_BuildBT(u32 base, size_t uSize)
 {
-       BT *pBT;
+       struct BT *pBT;
 
        if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
-                      sizeof(BT),
-                      (IMG_VOID **) & pBT, IMG_NULL) != PVRSRV_OK) {
-               return IMG_NULL;
-       }
+                      sizeof(struct BT),
+                      (void **) &pBT, NULL) != PVRSRV_OK)
+               return NULL;
 
        pBT->type = btt_free;
        pBT->base = base;
@@ -270,14 +284,24 @@ static BT *_BuildBT(IMG_UINTPTR_T base, IMG_SIZE_T uSize)
        return pBT;
 }
 
-static BT *_InsertResource(RA_ARENA * pArena, IMG_UINTPTR_T base,
-                          IMG_SIZE_T uSize)
+static struct BT *_InsertResource(struct RA_ARENA *pArena, u32 base,
+                          size_t uSize)
 {
-       BT *pBT;
-       PVR_ASSERT(pArena != IMG_NULL);
+       struct BT *pBT;
+       PVR_ASSERT(pArena != NULL);
+       if (pArena == NULL) {
+               PVR_DPF(PVR_DBG_ERROR,
+                        "_InsertResource: invalid parameter - pArena");
+               return NULL;
+       }
+
        pBT = _BuildBT(base, uSize);
-       if (pBT != IMG_NULL) {
-               _SegmentListInsert(pArena, pBT);
+       if (pBT != NULL) {
+               if (_SegmentListInsert(pArena, pBT) != PVRSRV_OK) {
+                       PVR_DPF(PVR_DBG_ERROR,
+                       "_InsertResource: call to _SegmentListInsert failed");
+                       return NULL;
+               }
                _FreeListInsert(pArena, pBT);
 #ifdef RA_STATS
                pArena->sStatistics.uTotalResourceCount += uSize;
@@ -288,59 +312,79 @@ static BT *_InsertResource(RA_ARENA * pArena, IMG_UINTPTR_T base,
        return pBT;
 }
 
-static BT *_InsertResourceSpan(RA_ARENA * pArena, IMG_UINTPTR_T base,
-                              IMG_SIZE_T uSize)
+static struct BT *_InsertResourceSpan(struct RA_ARENA *pArena, u32 base,
+                              size_t uSize)
 {
-       BT *pSpanStart;
-       BT *pSpanEnd;
-       BT *pBT;
-
-       PVR_ASSERT(pArena != IMG_NULL);
+       enum PVRSRV_ERROR eError;
+       struct BT *pSpanStart;
+       struct BT *pSpanEnd;
+       struct BT *pBT;
+
+       PVR_ASSERT(pArena != NULL);
+       if (pArena == NULL) {
+               PVR_DPF(PVR_DBG_ERROR,
+                        "_InsertResourceSpan: invalid parameter - pArena");
+               return NULL;
+       }
 
-       PVR_DPF((PVR_DBG_MESSAGE,
+       PVR_DPF(PVR_DBG_MESSAGE,
                 "RA_InsertResourceSpan: arena='%s', base=0x%x, size=0x%x",
-                pArena->name, base, uSize));
+                pArena->name, base, uSize);
 
        pSpanStart = _BuildSpanMarker(base, uSize);
-       if (pSpanStart == IMG_NULL) {
+       if (pSpanStart == NULL)
                goto fail_start;
-       }
        pSpanEnd = _BuildSpanMarker(base + uSize, 0);
-       if (pSpanEnd == IMG_NULL) {
+       if (pSpanEnd == NULL)
                goto fail_end;
-       }
 
        pBT = _BuildBT(base, uSize);
-       if (pBT == IMG_NULL) {
+       if (pBT == NULL)
                goto fail_bt;
-       }
 
-       _SegmentListInsert(pArena, pSpanStart);
-       _SegmentListInsertAfter(pArena, pSpanStart, pBT);
+       eError = _SegmentListInsert(pArena, pSpanStart);
+       if (eError != PVRSRV_OK)
+               goto fail_SegListInsert;
+
+       eError = _SegmentListInsertAfter(pArena, pSpanStart, pBT);
+       if (eError != PVRSRV_OK)
+               goto fail_SegListInsert;
+
        _FreeListInsert(pArena, pBT);
-       _SegmentListInsertAfter(pArena, pBT, pSpanEnd);
+
+       eError = _SegmentListInsertAfter(pArena, pBT, pSpanEnd);
+       if (eError != PVRSRV_OK)
+               goto fail_SegListInsert;
+
 #ifdef RA_STATS
        pArena->sStatistics.uTotalResourceCount += uSize;
 #endif
        return pBT;
 
+fail_SegListInsert:
+       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT), pBT, NULL);
 fail_bt:
-       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pSpanEnd, IMG_NULL);
+       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT), pSpanEnd, NULL);
 fail_end:
-       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pSpanStart, IMG_NULL);
+       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT), pSpanStart, NULL);
 fail_start:
-       return IMG_NULL;
+       return NULL;
 }
 
-static void _FreeBT(RA_ARENA * pArena, BT * pBT, IMG_BOOL bFreeBackingStore)
+static void _FreeBT(struct RA_ARENA *pArena, struct BT *pBT,
+                   IMG_BOOL bFreeBackingStore)
 {
-       BT *pNeighbour;
-       IMG_UINTPTR_T uOrigBase;
-       IMG_SIZE_T uOrigSize;
+       struct BT *pNeighbour;
+       u32 uOrigBase;
+       size_t uOrigSize;
 
-       PVR_ASSERT(pArena != IMG_NULL);
-       PVR_ASSERT(pBT != IMG_NULL);
+       PVR_ASSERT(pArena != NULL);
+       PVR_ASSERT(pBT != NULL);
 
+       if ((pArena == NULL) || (pBT == NULL)) {
+               PVR_DPF(PVR_DBG_ERROR, "_FreeBT: invalid parameter");
+               return;
+       }
 #ifdef RA_STATS
        pArena->sStatistics.uLiveSegmentCount--;
        pArena->sStatistics.uFreeSegmentCount++;
@@ -351,64 +395,57 @@ static void _FreeBT(RA_ARENA * pArena, BT * pBT, IMG_BOOL bFreeBackingStore)
        uOrigSize = pBT->uSize;
 
        pNeighbour = pBT->pPrevSegment;
-       if (pNeighbour != IMG_NULL
-           && pNeighbour->type == btt_free
-           && pNeighbour->base + pNeighbour->uSize == pBT->base) {
+       if (pNeighbour != NULL && pNeighbour->type == btt_free &&
+           pNeighbour->base + pNeighbour->uSize == pBT->base) {
                _FreeListRemove(pArena, pNeighbour);
                _SegmentListRemove(pArena, pNeighbour);
                pBT->base = pNeighbour->base;
                pBT->uSize += pNeighbour->uSize;
-               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pNeighbour,
-                         IMG_NULL);
+               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT),
+                         pNeighbour, NULL);
 #ifdef RA_STATS
                pArena->sStatistics.uFreeSegmentCount--;
 #endif
        }
 
        pNeighbour = pBT->pNextSegment;
-       if (pNeighbour != IMG_NULL
-           && pNeighbour->type == btt_free
-           && pBT->base + pBT->uSize == pNeighbour->base) {
+       if (pNeighbour != NULL && pNeighbour->type == btt_free &&
+           pBT->base + pBT->uSize == pNeighbour->base) {
                _FreeListRemove(pArena, pNeighbour);
                _SegmentListRemove(pArena, pNeighbour);
                pBT->uSize += pNeighbour->uSize;
-               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pNeighbour,
-                         IMG_NULL);
+               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT),
+                         pNeighbour, NULL);
 #ifdef RA_STATS
                pArena->sStatistics.uFreeSegmentCount--;
 #endif
        }
 
-       if (pArena->pBackingStoreFree != IMG_NULL && bFreeBackingStore) {
-               IMG_UINTPTR_T uRoundedStart, uRoundedEnd;
+       if (pArena->pBackingStoreFree != NULL && bFreeBackingStore) {
+               u32 uRoundedStart, uRoundedEnd;
 
-               uRoundedStart =
-                   (uOrigBase / pArena->uQuantum) * pArena->uQuantum;
+               uRoundedStart = (uOrigBase / pArena->uQuantum) *
+                                                       pArena->uQuantum;
 
-               if (uRoundedStart < pBT->base) {
+               if (uRoundedStart < pBT->base)
                        uRoundedStart += pArena->uQuantum;
-               }
 
-               uRoundedEnd =
-                   ((uOrigBase + uOrigSize + pArena->uQuantum -
+               uRoundedEnd = ((uOrigBase + uOrigSize + pArena->uQuantum -
                      1) / pArena->uQuantum) * pArena->uQuantum;
 
-               if (uRoundedEnd > (pBT->base + pBT->uSize)) {
+               if (uRoundedEnd > (pBT->base + pBT->uSize))
                        uRoundedEnd -= pArena->uQuantum;
-               }
 
-               if (uRoundedStart < uRoundedEnd) {
+               if (uRoundedStart < uRoundedEnd)
                        pArena->pBackingStoreFree(pArena->pImportHandle,
                                                  uRoundedStart, uRoundedEnd,
-                                                 (IMG_HANDLE) 0);
-               }
+                                                 (void *) 0);
        }
 
-       if (pBT->pNextSegment != IMG_NULL && pBT->pNextSegment->type == btt_span
-           && pBT->pPrevSegment != IMG_NULL
-           && pBT->pPrevSegment->type == btt_span) {
-               BT *next = pBT->pNextSegment;
-               BT *prev = pBT->pPrevSegment;
+       if (pBT->pNextSegment != NULL && pBT->pNextSegment->type == btt_span &&
+           pBT->pPrevSegment != NULL && pBT->pPrevSegment->type == btt_span) {
+               struct BT *next = pBT->pNextSegment;
+               struct BT *prev = pBT->pPrevSegment;
                _SegmentListRemove(pArena, next);
                _SegmentListRemove(pArena, prev);
                _SegmentListRemove(pArena, pBT);
@@ -421,220 +458,175 @@ static void _FreeBT(RA_ARENA * pArena, BT * pBT, IMG_BOOL bFreeBackingStore)
                pArena->sStatistics.uFreeResourceCount -= pBT->uSize;
                pArena->sStatistics.uTotalResourceCount -= pBT->uSize;
 #endif
-               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), next, IMG_NULL);
-               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), prev, IMG_NULL);
-               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pBT, IMG_NULL);
+               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT), next,
+                         NULL);
+               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT), prev,
+                         NULL);
+               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT), pBT,
+                         NULL);
        } else
                _FreeListInsert(pArena, pBT);
 }
 
-static IMG_BOOL
-_AttemptAllocAligned(RA_ARENA * pArena,
-                    IMG_SIZE_T uSize,
-                    BM_MAPPING ** ppsMapping,
-                    IMG_UINT32 uFlags,
-                    IMG_UINT32 uAlignment,
-                    IMG_UINT32 uAlignmentOffset, IMG_UINTPTR_T * base)
+static int alloc_from_bt(struct RA_ARENA *arena, struct BT *bt, u32 start,
+                        size_t size, u32 align,
+                        struct BM_MAPPING **new_mapping, u32 *new_base)
 {
-       IMG_UINT32 uIndex;
-       PVR_ASSERT(pArena != IMG_NULL);
+       _FreeListRemove(arena, bt);
+       PVR_ASSERT(bt->type == btt_free);
+#ifdef RA_STATS
+       arena->sStatistics.uLiveSegmentCount++;
+       arena->sStatistics.uFreeSegmentCount--;
+       arena->sStatistics.uFreeResourceCount -= bt->uSize;
+#endif
+       if (start > bt->base) {
+               struct BT *next_bt;
 
-       PVR_UNREFERENCED_PARAMETER(uFlags);
+               next_bt = _SegmentSplit(arena, bt, start - bt->base);
 
-       if (uAlignment > 1)
-               uAlignmentOffset %= uAlignment;
+               if (!next_bt) {
+                       PVR_DPF(PVR_DBG_ERROR, "_AttemptAllocAligned: "
+                                       "Front split failed");
 
-       uIndex = pvr_log2(uSize);
-
-#if 0
+                       _FreeListInsert(arena, bt);
+                       return -1;
+               }
 
-       if (1u << uIndex < uSize)
-               uIndex++;
+               _FreeListInsert(arena, bt);
+#ifdef RA_STATS
+               arena->sStatistics.uFreeSegmentCount++;
+               arena->sStatistics.uFreeResourceCount += bt->uSize;
 #endif
+               bt = next_bt;
+       }
 
-       while (uIndex < FREE_TABLE_LIMIT
-              && pArena->aHeadFree[uIndex] == IMG_NULL)
-               uIndex++;
+       if (bt->uSize > size) {
+               struct BT *next_bt;
+               next_bt = _SegmentSplit(arena, bt, size);
 
-       while (uIndex < FREE_TABLE_LIMIT) {
-               if (pArena->aHeadFree[uIndex] != IMG_NULL) {
-
-                       BT *pBT;
-
-                       pBT = pArena->aHeadFree[uIndex];
-                       while (pBT != IMG_NULL) {
-                               IMG_UINTPTR_T aligned_base;
-
-                               if (uAlignment > 1)
-                                       aligned_base =
-                                           (pBT->base + uAlignmentOffset +
-                                            uAlignment -
-                                            1) / uAlignment * uAlignment -
-                                           uAlignmentOffset;
-                               else
-                                       aligned_base = pBT->base;
-                               PVR_DPF((PVR_DBG_MESSAGE,
-                                        "RA_AttemptAllocAligned: pBT-base=0x%x "
-                                        "pBT-size=0x%x alignedbase=0x%x size=0x%x",
-                                        pBT->base, pBT->uSize, aligned_base,
-                                        uSize));
-
-                               if (pBT->base + pBT->uSize >=
-                                   aligned_base + uSize) {
-                                       if (!pBT->psMapping
-                                           || pBT->psMapping->ui32Flags ==
-                                           uFlags) {
-                                               _FreeListRemove(pArena, pBT);
-
-                                               PVR_ASSERT(pBT->type ==
-                                                          btt_free);
+               if (!next_bt) {
+                       PVR_DPF(PVR_DBG_ERROR, "_AttemptAllocAligned: "
+                                       "Back split failed");
+
+                       _FreeListInsert(arena, bt);
+                       return -1;
+               }
 
+               _FreeListInsert(arena, next_bt);
 #ifdef RA_STATS
-                                               pArena->sStatistics.
-                                                   uLiveSegmentCount++;
-                                               pArena->sStatistics.
-                                                   uFreeSegmentCount--;
-                                               pArena->sStatistics.
-                                                   uFreeResourceCount -=
-                                                   pBT->uSize;
+               arena->sStatistics.uFreeSegmentCount++;
+               arena->sStatistics.uFreeResourceCount += next_bt->uSize;
 #endif
+       }
 
-                                               if (aligned_base > pBT->base) {
-                                                       BT *pNeighbour;
+       bt->type = btt_live;
 
-                                                       pNeighbour =
-                                                           _SegmentSplit
-                                                           (pArena, pBT,
-                                                            aligned_base -
-                                                            pBT->base);
+       if (!HASH_Insert(arena->pSegmentHash, bt->base, (u32)bt)) {
+               _FreeBT(arena, bt, IMG_FALSE);
+               return -1;
+       }
 
-                                                       if (pNeighbour ==
-                                                           IMG_NULL) {
-                                                               PVR_DPF((PVR_DBG_ERROR, "_AttemptAllocAligned: Front split failed"));
+       if (new_mapping)
+               *new_mapping = bt->psMapping;
 
-                                                               _FreeListInsert
-                                                                   (pArena,
-                                                                    pBT);
-                                                               return
-                                                                   IMG_FALSE;
-                                                       }
+       *new_base = bt->base;
 
-                                                       _FreeListInsert(pArena,
-                                                                       pBT);
-#ifdef RA_STATS
-                                                       pArena->sStatistics.
-                                                           uFreeSegmentCount++;
-                                                       pArena->sStatistics.
-                                                           uFreeResourceCount
-                                                           += pBT->uSize;
-#endif
-                                                       pBT = pNeighbour;
-                                               }
-
-                                               if (pBT->uSize > uSize) {
-                                                       BT *pNeighbour;
-                                                       pNeighbour =
-                                                           _SegmentSplit
-                                                           (pArena, pBT,
-                                                            uSize);
-
-                                                       if (pNeighbour ==
-                                                           IMG_NULL) {
-                                                               PVR_DPF((PVR_DBG_ERROR, "_AttemptAllocAligned: Back split failed"));
-
-                                                               _FreeListInsert
-                                                                   (pArena,
-                                                                    pBT);
-                                                               return
-                                                                   IMG_FALSE;
-                                                       }
-
-                                                       _FreeListInsert(pArena,
-                                                                       pNeighbour);
-#ifdef RA_STATS
-                                                       pArena->sStatistics.
-                                                           uFreeSegmentCount++;
-                                                       pArena->sStatistics.
-                                                           uFreeResourceCount
-                                                           +=
-                                                           pNeighbour->uSize;
-#endif
-                                               }
-
-                                               pBT->type = btt_live;
-
-                                               if (!HASH_Insert
-                                                   (pArena->pSegmentHash,
-                                                    pBT->base,
-                                                    (IMG_UINTPTR_T) pBT)) {
-                                                       _FreeBT(pArena, pBT,
-                                                               IMG_FALSE);
-                                                       return IMG_FALSE;
-                                               }
-
-                                               if (ppsMapping != IMG_NULL)
-                                                       *ppsMapping =
-                                                           pBT->psMapping;
-
-                                               *base = pBT->base;
-
-                                               return IMG_TRUE;
-                                       } else {
-                                               PVR_DPF((PVR_DBG_MESSAGE,
-                                                        "AttemptAllocAligned: mismatch in flags. Import has %x, request was %x",
-                                                        pBT->psMapping->
-                                                        ui32Flags, uFlags));
-
-                                       }
-                               }
-                               pBT = pBT->pNextFree;
+       return 0;
+}
+
+static IMG_BOOL _AttemptAllocAligned(struct RA_ARENA *pArena, size_t uSize,
+                    struct BM_MAPPING **ppsMapping, u32 uFlags, u32 uAlignment,
+                    u32 *base)
+{
+       u32 uIndex;
+       PVR_ASSERT(pArena != NULL);
+       if (pArena == NULL) {
+               PVR_DPF(PVR_DBG_ERROR,
+                        "_AttemptAllocAligned: invalid parameter - pArena");
+               return IMG_FALSE;
+       }
+
+       uIndex = pvr_log2(uSize);
+
+       while (uIndex < FREE_TABLE_LIMIT && pArena->aHeadFree[uIndex] == NULL)
+               uIndex++;
+
+       for (; uIndex < FREE_TABLE_LIMIT; uIndex++) {
+               struct BT *pBT;
+
+               pBT = pArena->aHeadFree[uIndex];
+               if (!pBT)
+                       continue;
+
+               for (; pBT != NULL; pBT = pBT->pNextFree) {
+                       u32 aligned_base;
+
+                       if (uAlignment > 1)
+                               aligned_base = (pBT->base + uAlignment -
+                                         1) / uAlignment * uAlignment;
+                       else
+                               aligned_base = pBT->base;
+                       PVR_DPF(PVR_DBG_MESSAGE,
+                          "RA_AttemptAllocAligned: pBT-base=0x%x "
+                          "pBT-size=0x%x alignedbase=0x%x size=0x%x",
+                          pBT->base, pBT->uSize, aligned_base, uSize);
+
+                       if (pBT->base + pBT->uSize < aligned_base + uSize)
+                               continue;
+
+                       if (pBT->psMapping && pBT->psMapping->ui32Flags !=
+                           uFlags) {
+                               PVR_DPF(PVR_DBG_MESSAGE,
+                                       "AttemptAllocAligned: mismatch in "
+                                       "flags. Import has %x, request was %x",
+                                        pBT->psMapping->ui32Flags, uFlags);
+                               continue;
                        }
 
+                       if (alloc_from_bt(pArena, pBT, aligned_base, uSize,
+                                         uFlags, ppsMapping, base) < 0)
+                               return IMG_FALSE;
+
+                       return IMG_TRUE;
                }
-               uIndex++;
        }
 
        return IMG_FALSE;
 }
 
-RA_ARENA *RA_Create(IMG_CHAR * name,
-                   IMG_UINTPTR_T base,
-                   IMG_SIZE_T uSize,
-                   BM_MAPPING * psMapping,
-                   IMG_SIZE_T uQuantum,
-                   IMG_BOOL(*alloc) (IMG_VOID *, IMG_SIZE_T uSize,
-                                     IMG_SIZE_T * pActualSize,
-                                     BM_MAPPING ** ppsMapping,
-                                     IMG_UINT32 _flags, IMG_UINTPTR_T * pBase),
-                   IMG_VOID(*free) (IMG_VOID *, IMG_UINTPTR_T,
-                                    BM_MAPPING * psMapping),
-                   IMG_VOID(*backingstore_free) (IMG_VOID *, IMG_UINT32,
-                                                 IMG_UINT32, IMG_HANDLE),
-                   IMG_VOID * pImportHandle)
+struct RA_ARENA *RA_Create(char *name, u32 base, size_t uSize,
+                          struct BM_MAPPING *psMapping, size_t uQuantum,
+                          IMG_BOOL(*imp_alloc) (void *, size_t uSize,
+                                                size_t *pActualSize,
+                                                struct BM_MAPPING **ppsMapping,
+                                                u32 _flags, u32 *pBase),
+                          void (*imp_free) (void *, u32, struct BM_MAPPING *),
+                          void(*backingstore_free) (void *, u32, u32, void *),
+                          void *pImportHandle)
 {
-       RA_ARENA *pArena;
-       BT *pBT;
+       struct RA_ARENA *pArena;
+       struct BT *pBT;
        int i;
 
-       PVR_DPF((PVR_DBG_MESSAGE,
-                "RA_Create: name='%s', base=0x%x, uSize=0x%x, alloc=0x%x, free=0x%x",
-                name, base, uSize, alloc, free));
+       PVR_DPF(PVR_DBG_MESSAGE, "RA_Create: "
+                "name='%s', base=0x%x, uSize=0x%x, alloc=0x%x, free=0x%x",
+                name, base, uSize, imp_alloc, imp_free);
 
        if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
                       sizeof(*pArena),
-                      (IMG_VOID **) & pArena, IMG_NULL) != PVRSRV_OK) {
+                      (void **) &pArena, NULL) != PVRSRV_OK)
                goto arena_fail;
-       }
 
        pArena->name = name;
-       pArena->pImportAlloc = alloc != IMG_NULL ? alloc : _RequestAllocFail;
-       pArena->pImportFree = free;
+       pArena->pImportAlloc =
+           (imp_alloc != NULL) ? imp_alloc : _RequestAllocFail;
+       pArena->pImportFree = imp_free;
        pArena->pBackingStoreFree = backingstore_free;
        pArena->pImportHandle = pImportHandle;
        for (i = 0; i < FREE_TABLE_LIMIT; i++)
-               pArena->aHeadFree[i] = IMG_NULL;
-       pArena->pHeadSegment = IMG_NULL;
-       pArena->pTailSegment = IMG_NULL;
+               pArena->aHeadFree[i] = NULL;
+       pArena->pHeadSegment = NULL;
+       pArena->pTailSegment = NULL;
        pArena->uQuantum = uQuantum;
 
 #ifdef RA_STATS
@@ -649,25 +641,59 @@ RA_ARENA *RA_Create(IMG_CHAR * name,
        pArena->sStatistics.uExportCount = 0;
 #endif
 
-#if defined(CONFIG_PROC_FS) && defined(DEBUG)
+#if defined(CONFIG_PROC_FS) && defined(CONFIG_PVR_DEBUG_EXTRA)
        if (strcmp(pArena->name, "") != 0) {
-               sprintf(pArena->szProcInfoName, "ra_info_%s", pArena->name);
-               CreateProcEntry(pArena->szProcInfoName, RA_DumpInfo, 0, pArena);
-               sprintf(pArena->szProcSegsName, "ra_segs_%s", pArena->name);
-               CreateProcEntry(pArena->szProcSegsName, RA_DumpSegs, 0, pArena);
+               int ret;
+
+               if (PVRSRVGetInitServerState(PVRSRV_INIT_SERVER_SUCCESSFUL))
+                       pArena->ui32PID = OSGetCurrentProcessIDKM();
+               else
+                       pArena->ui32PID = 0;
+
+               ret = snprintf(pArena->szProcInfoName,
+                            sizeof(pArena->szProcInfoName), "ra_info_%s",
+                            pArena->name);
+               if ((ret > 0) && (ret < sizeof(pArena->szProcInfoName))) {
+                       ReplaceSpaces(pArena->szProcInfoName);
+                       ret = CreatePerProcessProcEntry(pArena->ui32PID,
+                                                       pArena->szProcInfoName,
+                                                       RA_DumpInfo, pArena);
+               } else
+                       pArena->szProcInfoName[0] = 0;
+
+               if (ret) {
+                       pArena->szProcInfoName[0] = 0;
+                       pr_err("%s: couldn't create ra_info proc entry for "
+                              "arena %s", __func__, pArena->name);
+               }
+
+               ret = snprintf(pArena->szProcSegsName,
+                            sizeof(pArena->szProcSegsName), "ra_segs_%s",
+                            pArena->name);
+               if ((ret > 0) && (ret < sizeof(pArena->szProcSegsName))) {
+                       ReplaceSpaces(pArena->szProcSegsName);
+                       ret = CreatePerProcessProcEntry(pArena->ui32PID,
+                                                       pArena->szProcSegsName,
+                                                       RA_DumpSegs, pArena);
+               } else
+                       ret = -1;
+
+               if (ret) {
+                       pArena->szProcSegsName[0] = 0;
+                       pr_err("%s: couldn't create ra_segs proc entry for "
+                              "arena %s", __func__, pArena->name);
+               }
        }
 #endif
 
        pArena->pSegmentHash = HASH_Create(MINIMUM_HASH_SIZE);
-       if (pArena->pSegmentHash == IMG_NULL) {
+       if (pArena->pSegmentHash == NULL)
                goto hash_fail;
-       }
        if (uSize > 0) {
                uSize = (uSize + uQuantum - 1) / uQuantum * uQuantum;
                pBT = _InsertResource(pArena, base, uSize);
-               if (pBT == IMG_NULL) {
+               if (pBT == NULL)
                        goto insert_fail;
-               }
                pBT->psMapping = psMapping;
 
        }
@@ -676,111 +702,136 @@ RA_ARENA *RA_Create(IMG_CHAR * name,
 insert_fail:
        HASH_Delete(pArena->pSegmentHash);
 hash_fail:
-       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, 0, pArena, IMG_NULL);
+       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct RA_ARENA), pArena,
+                 NULL);
 arena_fail:
-       return IMG_NULL;
+       return NULL;
 }
 
-void RA_Delete(RA_ARENA * pArena)
+void RA_Delete(struct RA_ARENA *pArena)
 {
-       IMG_UINT32 uIndex;
+       u32 uIndex;
 
-       PVR_ASSERT(pArena != IMG_NULL);
-       PVR_DPF((PVR_DBG_MESSAGE, "RA_Delete: name='%s'", pArena->name));
+       PVR_ASSERT(pArena != NULL);
+
+       if (pArena == NULL) {
+               PVR_DPF(PVR_DBG_ERROR,
+                        "RA_Delete: invalid parameter - pArena");
+               return;
+       }
+
+       PVR_DPF(PVR_DBG_MESSAGE, "RA_Delete: name='%s'", pArena->name);
 
        for (uIndex = 0; uIndex < FREE_TABLE_LIMIT; uIndex++)
-               pArena->aHeadFree[uIndex] = IMG_NULL;
+               pArena->aHeadFree[uIndex] = NULL;
 
-       while (pArena->pHeadSegment != IMG_NULL) {
-               BT *pBT = pArena->pHeadSegment;
+       while (pArena->pHeadSegment != NULL) {
+               struct BT *pBT = pArena->pHeadSegment;
                PVR_ASSERT(pBT->type == btt_free);
                _SegmentListRemove(pArena, pBT);
-               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(BT), pBT, IMG_NULL);
+               OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct BT), pBT,
+                         NULL);
 #ifdef RA_STATS
                pArena->sStatistics.uSpanCount--;
 #endif
        }
-#if defined(CONFIG_PROC_FS) && defined(DEBUG)
-       RemoveProcEntry(pArena->szProcInfoName);
-       RemoveProcEntry(pArena->szProcSegsName);
+#if defined(CONFIG_PROC_FS) && defined(CONFIG_PVR_DEBUG_EXTRA)
+       {
+               if (pArena->szProcInfoName[0] != 0)
+                       RemovePerProcessProcEntry(pArena->ui32PID,
+                                                 pArena->szProcInfoName);
+
+               if (pArena->szProcSegsName[0] != 0)
+                       RemovePerProcessProcEntry(pArena->ui32PID,
+                                                 pArena->szProcSegsName);
+       }
 #endif
        HASH_Delete(pArena->pSegmentHash);
-       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, 0, pArena, IMG_NULL);
+       OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP, sizeof(struct RA_ARENA), pArena,
+                 NULL);
 }
 
-IMG_BOOL RA_Add(RA_ARENA * pArena, IMG_UINTPTR_T base, IMG_SIZE_T uSize)
+IMG_BOOL RA_TestDelete(struct RA_ARENA *pArena)
 {
-       PVR_ASSERT(pArena != IMG_NULL);
+       PVR_ASSERT(pArena != NULL);
 
-       PVR_DPF((PVR_DBG_MESSAGE,
-                "RA_Add: name='%s', base=0x%x, size=0x%x", pArena->name, base,
-                uSize));
+       if (pArena != NULL)
+               while (pArena->pHeadSegment != NULL) {
+                       struct BT *pBT = pArena->pHeadSegment;
+                       if (pBT->type != btt_free)
+                               return IMG_FALSE;
+               }
 
-       uSize =
-           (uSize + pArena->uQuantum -
-            1) / pArena->uQuantum * pArena->uQuantum;
-       return ((IMG_BOOL) (_InsertResource(pArena, base, uSize) != IMG_NULL));
+       return IMG_TRUE;
 }
 
-IMG_BOOL
-RA_Alloc(RA_ARENA * pArena,
-        IMG_SIZE_T uRequestSize,
-        IMG_SIZE_T * pActualSize,
-        BM_MAPPING ** ppsMapping,
-        IMG_UINT32 uFlags,
-        IMG_UINT32 uAlignment,
-        IMG_UINT32 uAlignmentOffset, IMG_UINTPTR_T * base)
+IMG_BOOL RA_Add(struct RA_ARENA *pArena, u32 base, size_t uSize)
 {
-       IMG_BOOL bResult = IMG_FALSE;
-       IMG_SIZE_T uSize = uRequestSize;
+       PVR_ASSERT(pArena != NULL);
+
+       if (pArena == NULL) {
+               PVR_DPF(PVR_DBG_ERROR, "RA_Add: invalid parameter - pArena");
+               return IMG_FALSE;
+       }
 
-       PVR_ASSERT(pArena != IMG_NULL);
+       PVR_DPF(PVR_DBG_MESSAGE,
+                "RA_Add: name='%s', base=0x%x, size=0x%x", pArena->name, base,
+                uSize);
 
-#ifdef USE_BM_FREESPACE_CHECK
-       CheckBMFreespace();
-#endif
+       uSize = (uSize + pArena->uQuantum - 1) /
+               pArena->uQuantum * pArena->uQuantum;
+       return (IMG_BOOL)(_InsertResource(pArena, base, uSize) != NULL);
+}
 
-       if (pActualSize != IMG_NULL)
-               *pActualSize = uSize;
+IMG_BOOL RA_Alloc(struct RA_ARENA *pArena, size_t uRequestSize,
+                 struct BM_MAPPING **ppsMapping, u32 uFlags, u32 uAlignment,
+                 u32 *base)
+{
+       IMG_BOOL bResult;
+       size_t uSize = uRequestSize;
+
+       PVR_ASSERT(pArena != NULL);
+
+       if (pArena == NULL) {
+               PVR_DPF(PVR_DBG_ERROR,
+                        "RA_Alloc: invalid parameter - pArena");
+               return IMG_FALSE;
+       }
 
-       PVR_DPF((PVR_DBG_MESSAGE,
-                "RA_Alloc: arena='%s', size=0x%x(0x%x), alignment=0x%x, offset=0x%x",
-                pArena->name, uSize, uRequestSize, uAlignment,
-                uAlignmentOffset));
+       PVR_DPF(PVR_DBG_MESSAGE, "RA_Alloc: "
+                "arena='%s', size=0x%x(0x%x), alignment=0x%x",
+                pArena->name, uSize, uRequestSize, uAlignment);
 
        bResult = _AttemptAllocAligned(pArena, uSize, ppsMapping, uFlags,
-                                      uAlignment, uAlignmentOffset, base);
+                                      uAlignment, base);
        if (!bResult) {
-               BM_MAPPING *psImportMapping;
-               IMG_UINTPTR_T import_base;
-               IMG_SIZE_T uImportSize = uSize;
+               struct BM_MAPPING *psImportMapping;
+               u32 import_base;
+               size_t uImportSize = uSize;
 
-               if (uAlignment > pArena->uQuantum) {
+               if (uAlignment > pArena->uQuantum)
                        uImportSize += (uAlignment - 1);
-               }
 
                uImportSize =
-                   ((uImportSize + pArena->uQuantum -
-                     1) / pArena->uQuantum) * pArena->uQuantum;
+                   ((uImportSize + pArena->uQuantum - 1) /
+                    pArena->uQuantum) * pArena->uQuantum;
 
                bResult =
                    pArena->pImportAlloc(pArena->pImportHandle, uImportSize,
                                         &uImportSize, &psImportMapping, uFlags,
                                         &import_base);
                if (bResult) {
-                       BT *pBT;
-                       pBT =
-                           _InsertResourceSpan(pArena, import_base,
+                       struct BT *pBT;
+                       pBT = _InsertResourceSpan(pArena, import_base,
                                                uImportSize);
 
-                       if (pBT == IMG_NULL) {
-
+                       if (pBT == NULL) {
                                pArena->pImportFree(pArena->pImportHandle,
                                                    import_base,
                                                    psImportMapping);
-                               PVR_DPF((PVR_DBG_MESSAGE,
-                                        "RA_Alloc: name='%s', size=0x%x failed!",
-                                        pArena->name, uSize));
+                               PVR_DPF(PVR_DBG_MESSAGE, "RA_Alloc: "
+                                        "name='%s', size=0x%x failed!",
+                                        pArena->name, uSize);
 
                                return IMG_FALSE;
                        }
@@ -791,15 +842,13 @@ RA_Alloc(RA_ARENA * pArena,
                        pArena->sStatistics.uImportCount++;
                        pArena->sStatistics.uSpanCount++;
 #endif
-                       bResult =
-                           _AttemptAllocAligned(pArena, uSize, ppsMapping,
-                                                uFlags, uAlignment,
-                                                uAlignmentOffset, base);
-                       if (!bResult) {
-                               PVR_DPF((PVR_DBG_MESSAGE,
-                                        "RA_Alloc: name='%s' uAlignment failed!",
-                                        pArena->name));
-                       }
+                       bResult = _AttemptAllocAligned(pArena, uSize,
+                                                ppsMapping, uFlags, uAlignment,
+                                                base);
+                       if (!bResult)
+                               PVR_DPF(PVR_DBG_MESSAGE, "RA_Alloc: "
+                                        "name='%s' uAlignment failed!",
+                                        pArena->name);
                }
        }
 #ifdef RA_STATS
@@ -807,28 +856,29 @@ RA_Alloc(RA_ARENA * pArena,
                pArena->sStatistics.uCumulativeAllocs++;
 #endif
 
-       PVR_DPF((PVR_DBG_MESSAGE,
+       PVR_DPF(PVR_DBG_MESSAGE,
                 "RA_Alloc: name='%s', size=0x%x, *base=0x%x = %d",
-                pArena->name, uSize, *base, bResult));
+                pArena->name, uSize, *base, bResult);
 
        return bResult;
 }
 
-void RA_Free(RA_ARENA * pArena, IMG_UINTPTR_T base, IMG_BOOL bFreeBackingStore)
+void RA_Free(struct RA_ARENA *pArena, u32 base, IMG_BOOL bFreeBackingStore)
 {
-       BT *pBT;
+       struct BT *pBT;
 
-       PVR_ASSERT(pArena != IMG_NULL);
+       PVR_ASSERT(pArena != NULL);
 
-#ifdef USE_BM_FREESPACE_CHECK
-       CheckBMFreespace();
-#endif
+       if (pArena == NULL) {
+               PVR_DPF(PVR_DBG_ERROR, "RA_Free: invalid parameter - pArena");
+               return;
+       }
 
-       PVR_DPF((PVR_DBG_MESSAGE,
-                "RA_Free: name='%s', base=0x%x", pArena->name, base));
+       PVR_DPF(PVR_DBG_MESSAGE,
+                "RA_Free: name='%s', base=0x%x", pArena->name, base);
 
-       pBT = (BT *) HASH_Remove(pArena->pSegmentHash, base);
-       PVR_ASSERT(pBT != IMG_NULL);
+       pBT = (struct BT *)HASH_Remove(pArena->pSegmentHash, base);
+       PVR_ASSERT(pBT != NULL);
 
        if (pBT) {
                PVR_ASSERT(pBT->base == base);
@@ -837,55 +887,27 @@ void RA_Free(RA_ARENA * pArena, IMG_UINTPTR_T base, IMG_BOOL bFreeBackingStore)
                pArena->sStatistics.uCumulativeFrees++;
 #endif
 
-#ifdef USE_BM_FREESPACE_CHECK
-               {
-                       unsigned char *p;
-                       unsigned char *endp;
-
-                       p = (unsigned char *)pBT->base +
-                           SysGetDevicePhysOffset();
-                       endp = (unsigned char *)((IMG_UINT32) (p + pBT->uSize));
-                       while ((IMG_UINT32) p & 3) {
-                               *p++ = 0xAA;
-                       }
-                       while (p <
-                              (unsigned char *)((IMG_UINT32) endp &
-                                                0xfffffffc)) {
-                               *(IMG_UINT32 *) p = 0xAAAAAAAA;
-                               p += sizeof(IMG_UINT32);
-                       }
-                       while (p < endp) {
-                               *p++ = 0xAA;
-                       }
-                       PVR_DPF((PVR_DBG_MESSAGE,
-                                "BM_FREESPACE_CHECK: RA_Free Cleared %08X to %08X (size=0x%x)",
-                                (unsigned char *)pBT->base +
-                                SysGetDevicePhysOffset(), endp - 1,
-                                pBT->uSize));
-               }
-#endif
                _FreeBT(pArena, pBT, bFreeBackingStore);
        }
 }
 
-IMG_BOOL RA_GetNextLiveSegment(IMG_HANDLE hArena,
-                              RA_SEGMENT_DETAILS * psSegDetails)
+IMG_BOOL RA_GetNextLiveSegment(void *hArena,
+                              struct RA_SEGMENT_DETAILS *psSegDetails)
 {
-       BT *pBT;
+       struct BT *pBT;
 
        if (psSegDetails->hSegment) {
-               pBT = (BT *) psSegDetails->hSegment;
+               pBT = (struct BT *)psSegDetails->hSegment;
        } else {
-               RA_ARENA *pArena = (RA_ARENA *) hArena;
-
+               struct RA_ARENA *pArena = (struct RA_ARENA *)hArena;
                pBT = pArena->pHeadSegment;
        }
 
-       while (pBT != IMG_NULL) {
+       while (pBT != NULL) {
                if (pBT->type == btt_live) {
                        psSegDetails->uiSize = pBT->uSize;
                        psSegDetails->sCpuPhyAddr.uiAddr = pBT->base;
-                       psSegDetails->hSegment = (IMG_HANDLE) pBT->pNextSegment;
+                       psSegDetails->hSegment = (void *) pBT->pNextSegment;
 
                        return IMG_TRUE;
                }
@@ -895,57 +917,13 @@ IMG_BOOL RA_GetNextLiveSegment(IMG_HANDLE hArena,
 
        psSegDetails->uiSize = 0;
        psSegDetails->sCpuPhyAddr.uiAddr = 0;
-       psSegDetails->hSegment = (IMG_HANDLE) - 1;
+       psSegDetails->hSegment = (void *) -1;
 
        return IMG_FALSE;
 }
 
-#ifdef USE_BM_FREESPACE_CHECK
-RA_ARENA *pJFSavedArena = IMG_NULL;
-
-void CheckBMFreespace(void)
-{
-       BT *pBT;
-       unsigned char *p;
-       unsigned char *endp;
-
-       if (pJFSavedArena != IMG_NULL) {
-               for (pBT = pJFSavedArena->pHeadSegment; pBT != IMG_NULL;
-                    pBT = pBT->pNextSegment) {
-                       if (pBT->type == btt_free) {
-                               p = (unsigned char *)pBT->base +
-                                   SysGetDevicePhysOffset();
-                               endp =
-                                   (unsigned char
-                                    *)((IMG_UINT32) (p +
-                                                     pBT->uSize) & 0xfffffffc);
-
-                               while ((IMG_UINT32) p & 3) {
-                                       if (*p++ != 0xAA) {
-                                               fprintf(stderr,
-                                                       "BM_FREESPACE_CHECK: Blank space at %08X has changed to 0x%x\n",
-                                                       p, *(unsigned long *)p);
-                                               for (;;) ;
-                                               break;
-                                       }
-                               }
-                               while (p < endp) {
-                                       if (*(unsigned long *)p != 0xAAAAAAAA) {
-                                               fprintf(stderr,
-                                                       "BM_FREESPACE_CHECK: Blank space at %08X has changed to 0x%x\n",
-                                                       p, *(unsigned long *)p);
-                                               for (;;) ;
-                                               break;
-                                       }
-                                       p += 4;
-                               }
-                       }
-               }
-       }
-}
-#endif
-
-#if (defined(CONFIG_PROC_FS) && defined(DEBUG)) || defined (RA_STATS)
+#if (defined(CONFIG_PROC_FS) && defined(CONFIG_PVR_DEBUG_EXTRA)) || \
+     defined(RA_STATS)
 static char *_BTType(int eType)
 {
        switch (eType) {
@@ -960,104 +938,119 @@ static char *_BTType(int eType)
 }
 #endif
 
-#if defined(CONFIG_PROC_FS) && defined(DEBUG)
-static int
-RA_DumpSegs(char *page, char **start, off_t off, int count, int *eof,
-           void *data)
+#if defined(ENABLE_RA_DUMP)
+void RA_Dump(struct RA_ARENA *pArena)
+{
+       struct BT *pBT;
+       PVR_ASSERT(pArena != NULL);
+       PVR_DPF(PVR_DBG_MESSAGE, "Arena '%s':", pArena->name);
+       PVR_DPF(PVR_DBG_MESSAGE,
+                "  alloc=%08X free=%08X handle=%08X quantum=%d",
+                pArena->pImportAlloc, pArena->pImportFree,
+                pArena->pImportHandle, pArena->uQuantum);
+       PVR_DPF(PVR_DBG_MESSAGE, "  segment Chain:");
+       if (pArena->pHeadSegment != NULL &&
+           pArena->pHeadSegment->pPrevSegment != NULL)
+               PVR_DPF(PVR_DBG_MESSAGE,
+                        "  error: head boundary tag has invalid pPrevSegment");
+       if (pArena->pTailSegment != NULL &&
+           pArena->pTailSegment->pNextSegment != NULL)
+               PVR_DPF(PVR_DBG_MESSAGE,
+                        "  error: tail boundary tag has invalid pNextSegment");
+
+       for (pBT = pArena->pHeadSegment; pBT != NULL; pBT = pBT->pNextSegment)
+               PVR_DPF(PVR_DBG_MESSAGE,
+                        "\tbase=0x%x size=0x%x type=%s ref=%08X",
+                        (u32) pBT->base, pBT->uSize, _BTType(pBT->type),
+                        pBT->pRef);
+
+}
+#endif
+
+#if defined(CONFIG_PROC_FS) && defined(CONFIG_PVR_DEBUG_EXTRA)
+static int RA_DumpSegs(char *page, char **start, off_t off, int count, int *eof,
+                      void *data)
 {
-       BT *pBT = 0;
+       struct BT *pBT = NULL;
        int len = 0;
-       RA_ARENA *pArena = (RA_ARENA *) data;
+       struct RA_ARENA *pArena = (struct RA_ARENA *)data;
 
        if (count < 80) {
                *start = (char *)0;
-               return (0);
+               return 0;
        }
        *eof = 0;
        *start = (char *)1;
-       if (off == 0) {
+       if (off == 0)
                return printAppend(page, count, 0,
-                                  "Arena \"%s\"\nBase         Size Type Ref\n",
-                                  pArena->name);
-       }
+                       "Arena \"%s\"\nBase         Size Type Ref\n",
+                       pArena->name);
        for (pBT = pArena->pHeadSegment; --off && pBT;
-            pBT = pBT->pNextSegment) ;
-       if (pBT) {
+            pBT = pBT->pNextSegment)
+               ;
+       if (pBT)
                len = printAppend(page, count, 0, "%08x %8x %4s %08x\n",
-                                 (unsigned int)pBT->base,
-                                 (unsigned int)pBT->uSize, _BTType(pBT->type),
-                                 (unsigned int)pBT->psMapping);
-       } else {
+                                 (unsigned)pBT->base, (unsigned)pBT->uSize,
+                                 _BTType(pBT->type), (unsigned)pBT->psMapping);
+       else
                *eof = 1;
-       }
-       return (len);
+       return len;
 }
 
-static int
-RA_DumpInfo(char *page, char **start, off_t off, int count, int *eof,
+static int RA_DumpInfo(char *page, char **start, off_t off, int count, int *eof,
            void *data)
 {
        int len = 0;
-       RA_ARENA *pArena = (RA_ARENA *) data;
+       struct RA_ARENA *pArena = (struct RA_ARENA *)data;
 
        if (count < 80) {
                *start = (char *)0;
-               return (0);
+               return 0;
        }
        *eof = 0;
        switch (off) {
        case 0:
-               len =
-                   printAppend(page, count, 0, "quantum\t\t\t%lu\n",
-                               pArena->uQuantum);
+               len = printAppend(page, count, 0, "quantum\t\t\t%u\n",
+                                 pArena->uQuantum);
                break;
        case 1:
-               len =
-                   printAppend(page, count, 0, "import_handle\t\t%08X\n",
-                               (unsigned int)pArena->pImportHandle);
+               len = printAppend(page, count, 0, "import_handle\t\t%08X\n",
+                               (unsigned)pArena->pImportHandle);
                break;
 #ifdef RA_STATS
        case 2:
-               len =
-                   printAppend(page, count, 0, "span count\t\t%lu\n",
+               len = printAppend(page, count, 0, "span count\t\t%u\n",
                                pArena->sStatistics.uSpanCount);
                break;
        case 3:
-               len =
-                   printAppend(page, count, 0, "live segment count\t%lu\n",
+               len = printAppend(page, count, 0, "live segment count\t%u\n",
                                pArena->sStatistics.uLiveSegmentCount);
                break;
        case 4:
-               len =
-                   printAppend(page, count, 0, "free segment count\t%lu\n",
+               len = printAppend(page, count, 0, "free segment count\t%u\n",
                                pArena->sStatistics.uFreeSegmentCount);
                break;
        case 5:
-               len =
-                   printAppend(page, count, 0,
-                               "free resource count\t%lu (0x%x)\n",
+               len = printAppend(page, count, 0,
+                               "free resource count\t%u (0x%x)\n",
                                pArena->sStatistics.uFreeResourceCount,
-                               (unsigned int)pArena->sStatistics.
+                               (unsigned)pArena->sStatistics.
                                uFreeResourceCount);
                break;
        case 6:
-               len =
-                   printAppend(page, count, 0, "total allocs\t\t%lu\n",
+               len = printAppend(page, count, 0, "total allocs\t\t%u\n",
                                pArena->sStatistics.uCumulativeAllocs);
                break;
        case 7:
-               len =
-                   printAppend(page, count, 0, "total frees\t\t%lu\n",
+               len = printAppend(page, count, 0, "total frees\t\t%u\n",
                                pArena->sStatistics.uCumulativeFrees);
                break;
        case 8:
-               len =
-                   printAppend(page, count, 0, "import count\t\t%lu\n",
+               len = printAppend(page, count, 0, "import count\t\t%u\n",
                                pArena->sStatistics.uImportCount);
                break;
        case 9:
-               len =
-                   printAppend(page, count, 0, "export count\t\t%lu\n",
+               len = printAppend(page, count, 0, "export count\t\t%u\n",
                                pArena->sStatistics.uExportCount);
                break;
 #endif
@@ -1066,77 +1059,68 @@ RA_DumpInfo(char *page, char **start, off_t off, int count, int *eof,
                *eof = 1;
        }
        *start = (char *)1;
-       return (len);
+       return len;
 }
 #endif
 
 #ifdef RA_STATS
-PVRSRV_ERROR RA_GetStats(RA_ARENA * pArena,
-                        IMG_CHAR ** ppszStr, IMG_UINT32 * pui32StrLen)
+enum PVRSRV_ERROR RA_GetStats(struct RA_ARENA *pArena, char **ppszStr,
+                             u32 *pui32StrLen)
 {
-       IMG_CHAR *pszStr = *ppszStr;
-       IMG_UINT32 ui32StrLen = *pui32StrLen;
-       IMG_INT32 i32Count;
-       BT *pBT;
+       char *pszStr = *ppszStr;
+       u32 ui32StrLen = *pui32StrLen;
+       s32 i32Count;
+       struct BT *pBT;
 
        CHECK_SPACE(ui32StrLen);
        i32Count = OSSNPrintf(pszStr, 100, "\nArena '%s':\n", pArena->name);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
-       i32Count =
-           OSSNPrintf(pszStr, 100,
+       i32Count = OSSNPrintf(pszStr, 100,
                       "  allocCB=%08X freeCB=%08X handle=%08X quantum=%d\n",
                       pArena->pImportAlloc, pArena->pImportFree,
                       pArena->pImportHandle, pArena->uQuantum);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
-       i32Count =
-           OSSNPrintf(pszStr, 100, "span count\t\t%lu\n",
+       i32Count = OSSNPrintf(pszStr, 100, "span count\t\t%lu\n",
                       pArena->sStatistics.uSpanCount);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
-       i32Count =
-           OSSNPrintf(pszStr, 100, "live segment count\t%lu\n",
+       i32Count = OSSNPrintf(pszStr, 100, "live segment count\t%lu\n",
                       pArena->sStatistics.uLiveSegmentCount);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
-       i32Count =
-           OSSNPrintf(pszStr, 100, "free segment count\t%lu\n",
+       i32Count = OSSNPrintf(pszStr, 100, "free segment count\t%lu\n",
                       pArena->sStatistics.uFreeSegmentCount);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
        i32Count = OSSNPrintf(pszStr, 100, "free resource count\t%lu (0x%x)\n",
                              pArena->sStatistics.uFreeResourceCount,
-                             (unsigned int)pArena->sStatistics.
-                             uFreeResourceCount);
+                             (unsigned)pArena->sStatistics.uFreeResourceCount);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
-       i32Count =
-           OSSNPrintf(pszStr, 100, "total allocs\t\t%lu\n",
+       i32Count = OSSNPrintf(pszStr, 100, "total allocs\t\t%lu\n",
                       pArena->sStatistics.uCumulativeAllocs);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
-       i32Count =
-           OSSNPrintf(pszStr, 100, "total frees\t\t%lu\n",
+       i32Count = OSSNPrintf(pszStr, 100, "total frees\t\t%lu\n",
                       pArena->sStatistics.uCumulativeFrees);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
-       i32Count =
-           OSSNPrintf(pszStr, 100, "import count\t\t%lu\n",
+       i32Count = OSSNPrintf(pszStr, 100, "import count\t\t%lu\n",
                       pArena->sStatistics.uImportCount);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
        CHECK_SPACE(ui32StrLen);
-       i32Count =
-           OSSNPrintf(pszStr, 100, "export count\t\t%lu\n",
+       i32Count = OSSNPrintf(pszStr, 100, "export count\t\t%lu\n",
                       pArena->sStatistics.uExportCount);
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
@@ -1144,32 +1128,29 @@ PVRSRV_ERROR RA_GetStats(RA_ARENA * pArena,
        i32Count = OSSNPrintf(pszStr, 100, "  segment Chain:\n");
        UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
 
-       if (pArena->pHeadSegment != IMG_NULL &&
-           pArena->pHeadSegment->pPrevSegment != IMG_NULL) {
+       if (pArena->pHeadSegment != NULL &&
+           pArena->pHeadSegment->pPrevSegment != NULL) {
                CHECK_SPACE(ui32StrLen);
-               i32Count =
-                   OSSNPrintf(pszStr, 100,
-                              "  error: head boundary tag has invalid pPrevSegment\n");
+               i32Count = OSSNPrintf(pszStr, 100,
+                      "  error: head boundary tag has invalid pPrevSegment\n");
                UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
        }
 
-       if (pArena->pTailSegment != IMG_NULL &&
-           pArena->pTailSegment->pNextSegment != IMG_NULL) {
+       if (pArena->pTailSegment != NULL &&
+           pArena->pTailSegment->pNextSegment != NULL) {
                CHECK_SPACE(ui32StrLen);
-               i32Count =
-                   OSSNPrintf(pszStr, 100,
-                              "  error: tail boundary tag has invalid pNextSegment\n");
+               i32Count = OSSNPrintf(pszStr, 100,
+                      "  error: tail boundary tag has invalid pNextSegment\n");
                UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
        }
 
-       for (pBT = pArena->pHeadSegment; pBT != IMG_NULL;
+       for (pBT = pArena->pHeadSegment; pBT != NULL;
             pBT = pBT->pNextSegment) {
                CHECK_SPACE(ui32StrLen);
-               i32Count =
-                   OSSNPrintf(pszStr, 100,
+               i32Count = OSSNPrintf(pszStr, 100,
                               "\tbase=0x%x size=0x%x type=%s ref=%08X\n",
-                              (unsigned long)pBT->base, pBT->uSize,
-                              _BTType(pBT->type), pBT->psMapping);
+                              (u32) pBT->base, pBT->uSize, _BTType(pBT->type),
+                              pBT->psMapping);
                UPDATE_SPACE(pszStr, i32Count, ui32StrLen);
        }