📄 buffer_manager.c
字号:
/*!
******************************************************************************
@file : buffer_manager.c
@brief
@Author Marcus Shawcroft
@date 14 May 2003
<b>Copyright 2003 by Imagination Technologies Limited.</b>
All rights reserved. No part of this software, either
material or conceptual may be copied or distributed,
transmitted, transcribed, stored in a retrieval system
or translated into any human or computer language in any
form by any means, electronic, mechanical, manual or
other-wise, or disclosed to third parties without the
express written permission of Imagination Technologies
Limited, Unit 8, HomePark Industrial Estate,
King's Langley, Hertfordshire, WD4 8LZ, U.K.
<b>Description:</b>
Manages buffers mapped into two virtual memory spaces,
cpu and device.
<b>Platform:</b>
ALL
******************************************************************************/
#include "services_headers.h"
#include "buffer_manager.h"
#include "hash.h"
#include "ra.h"
#include "dual_buffer.h"
#include "handle.h"
#include "env.h"
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_Initialise
PURPOSE: Initialise the buffer manager. This function must be called
before any other buffer manager function.
PARAMETERS: In: registers - cpu virtual address for the device registers.
RETURNS: IMG_TRUE - Success
IMG_FALSE - Failed
</function>
-----------------------------------------------------------------------------*/
IMG_BOOL
BM_Initialise ( IMG_CPU_VIRTADDR registers,
IMG_CPU_VIRTADDR slaveports,
IMG_UINT32 ui32CoreConfig
)
{
SYS_DATA *psSysData;
if(SysAcquireData(&psSysData) != PVRSRV_OK)
return IMG_FALSE;
PVR_DPF ((PVR_DBG_MESSAGE, "BM_Initialise (registers=0x%x)", registers));
psSysData->bm_hs = HDL_SpaceCreate (256, 256);
if (psSysData->bm_hs == IMG_NULL)
goto failed_hdl_space_create;
if (!DB_Initialise (registers, slaveports, ui32CoreConfig, &psSysData->db_state))
goto failed_db_initialise;
return IMG_TRUE;
failed_db_initialise:
HDL_SpaceDelete (psSysData->bm_hs);
failed_hdl_space_create:
return IMG_FALSE;
}
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_Finalise
PURPOSE: Finalise the buffer manager. All allocated buffers must be
free'd before calling this function.
PARAMETERS: None
RETURNS: None
</function>
-----------------------------------------------------------------------------*/
void
BM_Finalise (void)
{
BM_HANDLE h;
SYS_DATA *psSysData;
if(SysAcquireData(&psSysData) != PVRSRV_OK)
{
PVR_ASSERT (0);
return;
}
PVR_DPF ((PVR_DBG_MESSAGE, "BM_Finalise ()"));
if (HDL_SpaceFirstHandle (psSysData->bm_hs, &h.h))
{
do
{
BM_Free (h);
} while (HDL_SpaceNextHandle (psSysData->bm_hs, &h.h));
}
DB_Finalise (psSysData->db_state);
HDL_SpaceDelete (psSysData->bm_hs);
}
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_Alloc
PURPOSE: Allocate a buffer mapped into both cpu and device virtual
memory maps.
PARAMETERS: In: uSize - require size in bytes of the buffer.
In: uFlags - bit mask of buffer property flags.
In: uDevVAddrAlignment - required alignment in bytes, or 0.
Out: phBuf - receives buffer handle
RETURNS: IMG_TRUE - Success
IMG_FALSE - Failure
</function>
-----------------------------------------------------------------------------*/
IMG_BOOL
BM_Alloc (IMG_SIZE_T uSize,
IMG_UINT32 uFlags,
IMG_UINT32 uDevVAddrAlignment,
BM_HANDLE *phBuf)
{
BM_BUF *pBuf;
SYS_DATA *psSysData;
if (SysAcquireData(&psSysData) != PVRSRV_OK)
return IMG_FALSE;
PVR_DPF ((PVR_DBG_MESSAGE,
"BM_Alloc (uSize=0x%lx, uFlags=%d, uDevVAddrAlignment=0x%x)",
uSize, uFlags, uDevVAddrAlignment));
if(uDevVAddrAlignment == 0)
uDevVAddrAlignment = 1;
if (!DB_AllocBuf (psSysData->db_state, uSize, uFlags, uDevVAddrAlignment, &pBuf))
return IMG_FALSE;
if (!HDL_SpaceAlloc (psSysData->bm_hs, pBuf, &(phBuf->h)))
DB_FreeBuf (psSysData->db_state, &pBuf);
PVR_DPF ((PVR_DBG_MESSAGE,
"BM_Alloc (uSize=0x%lx, uFlags=%d)=%d",
uSize, uFlags, phBuf->h));
return IMG_TRUE;
}
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_Wrap
PURPOSE: Create a buffer which wraps user provided system physical
memory.
The wrapped memory must be page aligned. BM_Wrap will
roundup the size to a multiple of cpu pages.
PARAMETERS: In: base - system physical base address of memory to wrap.
In: uSize - size of memory to wrap.
In: uDevVAddrAlignment - required device virtual address
alignment in bytes, or 0.
Out: phBuf - receives the buffer handle.
RETURNS: IMG_TRUE - Success.
IMG_FALSE - Failed
</function>
-----------------------------------------------------------------------------*/
IMG_BOOL
BM_Wrap (IMG_SYS_PHYADDR base,
IMG_SIZE_T uSize,
IMG_UINT32 uFlags,
IMG_UINT32 uDevVAddrAlignment,
BM_HANDLE *phBuf)
{
BM_BUF *pBuf;
SYS_DATA *psSysData;
if(SysAcquireData (&psSysData) != PVRSRV_OK)
return IMG_FALSE;
PVR_DPF ((PVR_DBG_MESSAGE,
"BM_Wrap (base=0x%lx, uSize=0x%lx, uFlags=%d, alignment=0x%x)",
base.uiAddr, uSize, uFlags, uDevVAddrAlignment));
if (!DB_WrapBuf (psSysData->db_state, base, uSize, uFlags, uDevVAddrAlignment, &pBuf))
return IMG_FALSE;
if (!HDL_SpaceAlloc (psSysData->bm_hs, pBuf, &(phBuf->h)))
DB_FreeBuf (psSysData->db_state, &pBuf);
PVR_DPF ((PVR_DBG_MESSAGE,
"BM_Wrap (base=0x%lx, uSize=0x%lx, uFlags=%d) =%d",
base, uSize, uFlags, phBuf->h));
return IMG_TRUE;
}
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_Free
PURPOSE: Free a buffer previously allocated via BM_Alloc.
PARAMETERS: In: hBuf - buffer handle.
RETURNS: None.
</function>
-----------------------------------------------------------------------------*/
void
BM_Free (BM_HANDLE hBuf)
{
BM_BUF *pBuf;
SYS_DATA *psSysData;
if(SysAcquireData (&psSysData) != PVRSRV_OK)
return;
PVR_DPF ((PVR_DBG_MESSAGE, "BM_Free (h=%d)...", hBuf.h));
pBuf = HDL_SpaceLookup (psSysData->bm_hs, hBuf.h);
if (pBuf!=IMG_NULL)
{
DB_FreeBuf (psSysData->db_state, &pBuf);
HDL_SpaceFree (psSysData->bm_hs, hBuf.h);
}
PVR_DPF ((PVR_DBG_MESSAGE, "...BM_Free (h=%d)", hBuf.h));
}
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_HandleToCpuVaddr
PURPOSE: Retreive the cpu virtual address associated with a buffer.
PARAMETERS: In: hBuf - buffer handle.
RETURNS: buffers cpu virtual address.
</function>
-----------------------------------------------------------------------------*/
IMG_CPU_VIRTADDR
BM_HandleToCpuVaddr (BM_HANDLE hBuf)
{
BM_BUF *pBuf;
SYS_DATA *psSysData;
if(SysAcquireData (&psSysData) != PVRSRV_OK)
return 0;
PVR_DPF ((PVR_DBG_MESSAGE,
"BM_HandleToCpuVaddr (h=%d)", hBuf.h));
pBuf = HDL_SpaceLookup (psSysData->bm_hs, hBuf.h);
PVR_ASSERT (pBuf != IMG_NULL);
return pBuf->CpuVAddr;
}
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_HandleToDevVaddr
PURPOSE: Retreive the device virtual address associated with a buffer.
PARAMETERS: In: hBuf - buffer handle.
RETURNS: buffers device virtual address.
</function>
-----------------------------------------------------------------------------*/
IMG_DEV_VIRTADDR
BM_HandleToDevVaddr (BM_HANDLE hBuf)
{
BM_BUF *pBuf;
SYS_DATA *psSysData;
if (SysAcquireData (&psSysData) != PVRSRV_OK)
{
PVR_ASSERT (0);
}
/* PVR_DPF ((PVR_DBG_MESSAGE, "BM_HandleToDevVaddr (h=%d)", hBuf.h));
*/
pBuf = HDL_SpaceLookup (psSysData->bm_hs, hBuf.h);
PVR_ASSERT (pBuf != IMG_NULL);
return pBuf->DevVAddr;
}
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_HandleToSysPaddr
PURPOSE: Retreive the system physical address associated with a buffer.
PARAMETERS: In: hBuf - buffer handle.
RETURNS: buffers device virtual address.
</function>
-----------------------------------------------------------------------------*/
IMG_SYS_PHYADDR
BM_HandleToSysPaddr (BM_HANDLE hBuf)
{
BM_BUF *pBuf;
SYS_DATA *psSysData;
if (SysAcquireData (&psSysData) != PVRSRV_OK)
{
PVR_ASSERT (0);
}
/* PVR_DPF ((PVR_DBG_MESSAGE, "BM_HandleToDevVaddr (h=%d)", hBuf.h));
*/
pBuf = HDL_SpaceLookup (psSysData->bm_hs, hBuf.h);
PVR_ASSERT (pBuf != IMG_NULL);
return ENV_CpuPAddrToSysPAddr (pBuf->CpuPAddr);
}
/*----------------------------------------------------------------------------
<function>
FUNCTION: BM_ContiguousStatistics
PURPOSE: Retreive the total size of the contiguous memory source and
the number of available_bytes. Neither of these statistics
reflect the effects of fragmentation within the contiguous
arena. The buffer manager must be initialised by calling
BM_Initialise() before calling this function.
PARAMETERS: Out: pTotalBytes - receives the total number of bytes in
the contiguous memory source.
Out: pAvailableBytes = receives the number of available
bytes in the contiguous memory source.
RETURNS: IMG_TRUE - Success
IMG_FALSE - Failure
</function>
-----------------------------------------------------------------------------*/
IMG_BOOL
BM_ContiguousStatistics (IMG_UINT32 uFlags,
IMG_UINT32 *pTotalBytes,
IMG_UINT32 *pAvailableBytes)
{
RA_STATISTICS *pStats;
SYS_DATA *psSysData;
IMG_BOOL bResult;
if (SysAcquireData (&psSysData) != PVRSRV_OK)
return IMG_FALSE;
bResult = DB_ContiguousStatistics (psSysData->db_state, uFlags, &pStats);
*pTotalBytes = pStats->uTotalResourceCount;
*pAvailableBytes = pStats->uFreeResourceCount;
return bResult;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -