⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hostfunc.c

📁 Lido PXA270平台开发板的最新BSP,包括源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
IMG_BOOL HostIsResourceLocked (PVRSRV_RES_HANDLE *phResource)
{
	volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)phResource;

	return 	*(volatile IMG_UINT32 *)pui32Access == 0
			?	IMG_FALSE
			:	IMG_TRUE;
}
#endif



/*----------------------------------------------------------------------------
<function>
    FUNCTION   : HostBreakResource
    PURPOSE    : Break an OS dependant resource object
    RETURNS    : phRes - pointer to OS dependent resource structure
</function>
------------------------------------------------------------------------------*/

PVRSRV_ERROR HostBreakResource(IMG_UINT32 ui32ProcessID, PVRSRV_RES_HANDLE *phRes)
{
	volatile IMG_UINT32 *pui32Access = (volatile IMG_UINT32 *)phRes;
	PVRSRV_ERROR eError = PVRSRV_OK;

	if (*pui32Access != ui32ProcessID)
	{
		PVR_DPF((PVR_DBG_MESSAGE,"HostBreakResource: resource is not locked with expected value"));
		eError = PVRSRV_ERROR_GENERIC;
	}
	else
	{
		*pui32Access = 0;
	}

	return(eError);
}


/*!
******************************************************************************

 @Function		HostMapLinToCPUPhys
 
 @Description	A physical bus address corresponding to the given linear address.
               Will crash if an invalid address is passed in.
               (so make sure you lock it)
	
 @Input    pvLinAddr - pointer to locked location

 @Return  physical bus address

******************************************************************************/
IMG_CPU_PHYADDR HostMapLinToCPUPhys (void* pvLinAddr)
{
#if 0
	return virt_to_phys(pvLinAddr);
#else
	IMG_CPU_PHYADDR uDevicePhysAddress;
	
	uDevicePhysAddress.uiAddr = (((IMG_UINT32)pvLinAddr) & 0x1FFFFFFF) - SysGetDevicePhysOffset();

	return uDevicePhysAddress;
#endif
}


/*!
******************************************************************************

 @Function		HostGetCurrentProcessID
 
 @Description	Returns handle for current process
 
 @Return    ID of current process

******************************************************************************/

IMG_UINT32 HostGetCurrentProcessID (void)
{
	HANDLE hOwner = GetOwnerProcess();

	return((IMG_UINT32) hOwner);
}


/*!
******************************************************************************

 @Function	HostMemSet
 
 @Description Function that does the same as the C memset() functions
 
 @Modified *pvDest :	pointer to start of buffer to be set

 @Input    ui8Value:	value to set each byte to

 @Input    ui32Size :	number of bytes to set

 @Return   IMG_VOID

******************************************************************************/
IMG_VOID HostMemSet(IMG_VOID *pvDest, IMG_UINT8 ui8Value, IMG_UINT32 ui32Size)
{
	memset(pvDest, (int) ui8Value, (size_t) ui32Size);
}


/*!
******************************************************************************

 @Function		HostMapPhysToLin
 
 @Description	Maps the physical memory into linear addr range
 
 @Input    BasePAddr : physical cpu address

 @Input    ui32Bytes - bytes to map

 @Input    ui32CacheType - cache type

 @Return    : Linear addr of mapping on success, else NULL

******************************************************************************/
IMG_PVOID HostMapPhysToLin (IMG_CPU_PHYADDR BasePAddr, IMG_UINT32 ui32Bytes, IMG_UINT32 ui32CacheType)
{
	PVOID		pv;
	IMG_UINT32	ui32ReserveSize	= ui32Bytes;
	IMG_UINT32	ui32MapSize		= ui32Bytes;

	/* Allocate memory to map into */

	if(ui32CacheType & EXTRA_CACHETYPE_SHARED)
	{
		/* 
			Map into shared memory area 
			Need to map a minimum of 2mb on Wince to be in shared region 
		*/
		if(ui32ReserveSize < 0x200000)
		{
			ui32ReserveSize = 0x200000;
		}

		/* round to nearest page */
		ui32ReserveSize = (ui32ReserveSize + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
	}

	/* 
		add a guard page to mapping 
	*/
	pv = VirtualAlloc(NULL, ui32ReserveSize+PAGE_SIZE, MEM_RESERVE, PAGE_NOACCESS);

	/* Map in memory */

	if(pv == NULL)
	{
		PVR_DPF((PVR_DBG_ERROR,"HostMapPhysToLin: CreateFileMapping/MapViewOfFile/VirtualAlloc failed"));
	}
	else
	{
		IMG_UINT32 ui32CacheFlags		= PAGE_READWRITE;
		IMG_UINT32 ui32SourceAddress;

		/* Setup cache flags and source address for mapping */

		if ((ui32CacheType & CACHETYPE_UNCACHED) ||
			(ui32CacheType & CACHETYPE_WRITECOMBINED))
		{
			ui32CacheFlags |= PAGE_NOCACHE;
		}

		if(ui32CacheType & EXTRA_CACHETYPE_VIRTUAL)	
		{			
			ui32SourceAddress	= BasePAddr.uiAddr;
		}
		else
		{
			ui32CacheFlags |= PAGE_PHYSICAL;
			ui32SourceAddress	= BasePAddr.uiAddr / 256;
		}

		/* Do the mapping */

		if(VirtualCopy(pv, (LPVOID)ui32SourceAddress, ui32MapSize, ui32CacheFlags) == FALSE)
		{
			PVR_DPF((PVR_DBG_ERROR,"HostMapPhysToLin: VirtualAlloc failed"));
			VirtualFree(pv, 0, MEM_RELEASE);
		}
		else
		{
			PVR_DPF((PVR_DBG_MESSAGE,"HostMapPhysToLin: PhysAddr:%08lx, LinAddr:%08lx, Bytes:%d", BasePAddr.uiAddr, pv, ui32MapSize));
		} 
#ifdef SUPPORT_XSCALE_PLATFORM
		/*********************************************** Set write-coalescing */
		if (ui32CacheType & CACHETYPE_WRITECOMBINED)
		{
#ifdef ADJUSTWRITECOMBINING
			if(g_bWriteCombining)
#endif
			{
				if(!VirtualSetAttributes(pv, ui32MapSize,4, 4, NULL))
				{
					PVR_DPF((PVR_DBG_ERROR,"HostMapPhysToLin: VirtualSetAttributes(1) failed"));
				}
			}
#ifdef ADJUSTWRITECOMBINING

			/* Make a note of this mapping so that we can disable it at a later date */
			AddWCEntry(pv,ui32MapSize);
#endif

		}
#endif
	}

	return pv;
}


/*!
******************************************************************************

 @Function HostUnMapPhysToLin
 
 @Description Unmaps memory that was mapped with HostMapPhysToLin
 
 @Input pvLinAddr
 @Input dwPages

 @Return nothing

******************************************************************************/ 
BOOL HostUnMapPhysToLin(PVOID pvLinAddr, DWORD dwPages)
{
	if(!VirtualFree(pvLinAddr, 0, MEM_RELEASE))
	{
		PVR_DPF((PVR_DBG_ERROR,"HostUnMapPhysToLin: VirtualFree failed"));
		return(FALSE);
	}
#ifdef ADJUSTWRITECOMBINING
	RemoveWCEntry(pvLinAddr);
#endif
	return(TRUE);
}



/*!
******************************************************************************

 @Function HostWaitus
 
 @Description 
 	This function implements a busy wait of specified microseconds
 	This function does NOT release thread quanta
 
 @Input ui32Timeus - (us)

 @Return nothing

******************************************************************************/ 
IMG_VOID  HostWaitus (IMG_UINT32 ui32Timeus)
{
	#define APPROX_LOOPS_PER_MICROSECOND 10
	IMG_UINT32 uiMicroSecond;
	volatile IMG_UINT32 uiLoop;
	for (uiMicroSecond = 0; uiMicroSecond < ui32Timeus; uiMicroSecond++)
	{
		for (uiLoop = 0; uiLoop < APPROX_LOOPS_PER_MICROSECOND; uiLoop++) ;
	}
}

/*!
******************************************************************************

 @Function HostReleaseThreadQuanta
 
 @Description 
	Releases thread quanta
	 
 @Return nothing

******************************************************************************/ 
IMG_VOID  HostReleaseThreadQuanta ()
{
	Sleep(0);
}
/*!
******************************************************************************

 @Function HostClockus
 
 @Description 
 	This function returns the clock in microseconds
 
 @Input void

 @Return - clock (us)

******************************************************************************/ 
IMG_UINT32  HostClockus (IMG_VOID)
{
	return 	(GetTickCount() * 1000);
}

/*!
******************************************************************************

 @Function HostSetupEnvData
 
 @Description allocates space for env specific data
 
 @Input ppvEnvSpecificData

 @Return PVRSRV_ERROR

******************************************************************************/ 
PVRSRV_ERROR HostInitEnvData (IMG_PVOID *ppvEnvSpecificData)
{
	PVRSRV_ERROR	eError;
	HOST_HEAP		*psHeap;
	ENV_DATA		*psEnvData;
	
	/* allocate env specific data */
	psEnvData = HostPageableByteAlloc(sizeof(ENV_DATA));
	if(psEnvData == NULL)
	{
		return PVRSRV_ERROR_GENERIC;
	}

	eError = InitHeapman(&psHeap);
	if (eError != PVRSRV_OK)
	{
		HostPageableByteFree(psEnvData);
		return eError;
	}
	psEnvData->psHeap = psHeap;

	/* copy structure back */
	*ppvEnvSpecificData = psEnvData;

	return eError;
}



/*!
******************************************************************************

 @Function		HostDeInitEnvData
 
 @Description	frees env specific data memory
 
 @Input    pvEnvSpecificData - pointer to private structure

 @Return   PVRSRV_OK on success else PVRSRV_ERROR_OUT_OF_MEMORY 

******************************************************************************/ 
PVRSRV_ERROR HostDeInitEnvData (IMG_PVOID pvEnvSpecificData)
{
	PVRSRV_ERROR	eError;

	eError = DeInitHeapman((HOST_HEAP*)pvEnvSpecificData);

	HostPageableByteFree(pvEnvSpecificData);

	return eError;
}



#ifdef ADJUSTWRITECOMBINING

/*!
******************************************************************************

 @Function HostDisableWriteCombining
 
 @Description Zip through the list and disable write combining for any entries found 

 @Return PVRSRV_ERROR

******************************************************************************/ 
PVRSRV_ERROR HostDisableWriteCombining ()
{
	PWRITE_COMBINED_MEM_TAG  psWCEntry = &g_sWCList;
	g_bWriteCombining=IMG_FALSE;
	while(psWCEntry!=NULL)
	{
		if(psWCEntry->pvAddress)
		{
			if(!VirtualSetAttributes(psWCEntry->pvAddress, psWCEntry->ui32MapSize,0, 4, NULL))
			{
				PVR_DPF((PVR_DBG_ERROR,"HostMapPhysToLin: VirtualSetAttributes failed"));
			}
		}
		psWCEntry=psWCEntry->psNext;
	}
		return	 PVRSRV_OK;

}

/*!
******************************************************************************

 @Function HostEnableWriteCombining
 
 @Description Zip through the list and enable write combining for any entries found 

 @Return PVRSRV_ERROR

******************************************************************************/ 
PVRSRV_ERROR HostEnableWriteCombining ()
{
	PWRITE_COMBINED_MEM_TAG  psWCEntry = &g_sWCList;
	g_bWriteCombining=IMG_TRUE;
	while(psWCEntry!=NULL)
	{
		if(psWCEntry->pvAddress)
		{
			if(!VirtualSetAttributes(psWCEntry->pvAddress, psWCEntry->ui32MapSize,4, 4, NULL))
			{
				PVR_DPF((PVR_DBG_ERROR,"HostMapPhysToLin: VirtualSetAttributes failed"));
			}
		}
		psWCEntry=psWCEntry->psNext;
	}
		return	 PVRSRV_OK;

}

/*!
******************************************************************************

 @Function AddWCEntry
 
 @Description Add a WC entry to the list 

 @Input	pvAddress
 @Input	ui32MapSize
 
 @Return PVRSRV_ERROR

******************************************************************************/ 
static PVRSRV_ERROR AddWCEntry(PVOID pvAddress,IMG_UINT32 ui32MapSize)
{
	PWRITE_COMBINED_MEM_TAG  psWCEntry = &g_sWCList;

	while(psWCEntry->psNext!=NULL)
	{
		psWCEntry=psWCEntry->psNext;
	}

	psWCEntry->psNext=malloc(sizeof(WRITE_COMBINED_MEM_TAG));
	psWCEntry=psWCEntry->psNext;
	psWCEntry->pvAddress=pvAddress;
	psWCEntry->ui32MapSize=ui32MapSize;
	psWCEntry->psNext=NULL;
	return	 PVRSRV_OK;
}

/*!
******************************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -