text: /*************************************************************************/ /*!
  @File
  @Title          Device specific transfer queue routines
  @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
  @Description    Device specific functions
  @License        Dual MIT/GPLv2
  
  The contents of this file are subject to the MIT license as set out below.
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  Alternatively, the contents of this file may be used under the terms of
  the GNU General Public License Version 2 ("GPL") in which case the provisions
  of GPL are applicable instead of those above.
  
  If you wish to allow use of your version of this file only under the terms of
  GPL, and not to allow others to use your version of this file under the terms
  of the MIT license, indicate your decision by deleting the provisions above
  and replace them with the notice and other provisions required by GPL as set
  out in the file called "GPL-COPYING" included in this distribution. If you do
  not delete the provisions above, a recipient may use your version of this file
  under the terms of either the MIT license or GPL.
  
  This License is also included in this distribution in the file called
  "MIT-COPYING".
  
  EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
  BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */ /**************************************************************************/
  
  #include "pdump_km.h"
  #include "rgxdevice.h"
  #include "rgxccb.h"
  #include "rgxutils.h"
  #include "rgxfwutils.h"
  #include "rgxtransfer.h"
  #include "rgx_tq_shared.h"
  #include "rgxmem.h"
  #include "allocmem.h"
  #include "devicemem.h"
  #include "devicemem_pdump.h"
  #include "osfunc.h"
  #include "pvr_debug.h"
  #include "pvrsrv.h"
  #include "rgx_fwif_resetframework.h"
  #include "rgx_memallocflags.h"
  #include "rgxccb.h"
  
  #include "sync_server.h"
  #include "sync_internal.h"
  
  #if defined(PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC)
  #include "pvr_sync.h"
  #endif /* defined(PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC) */
  
  typedef struct {
  	DEVMEM_MEMDESC				*psFWContextStateMemDesc;
  	RGX_SERVER_COMMON_CONTEXT	*psServerCommonContext;
  	IMG_UINT32					ui32Priority;
  } RGX_SERVER_TQ_3D_DATA;
  
  typedef struct {
  	RGX_SERVER_COMMON_CONTEXT	*psServerCommonContext;
  	IMG_UINT32					ui32Priority;
  } RGX_SERVER_TQ_2D_DATA;
  
  struct _RGX_SERVER_TQ_CONTEXT_ {
  	PVRSRV_DEVICE_NODE			*psDeviceNode;
  	DEVMEM_MEMDESC				*psFWFrameworkMemDesc;
  	IMG_UINT32					ui32Flags;
  #define RGX_SERVER_TQ_CONTEXT_FLAGS_2D		(1<<0)
  #define RGX_SERVER_TQ_CONTEXT_FLAGS_3D		(1<<1)
  	RGX_SERVER_TQ_3D_DATA		s3DData;
  	RGX_SERVER_TQ_2D_DATA		s2DData;
  	PVRSRV_CLIENT_SYNC_PRIM		*psCleanupSync;
  	DLLIST_NODE					sListNode;
  };
  
  /*
  	Static functions used by transfer context code
  */
  
  static PVRSRV_ERROR _Create3DTransferContext(CONNECTION_DATA *psConnection,
  											 PVRSRV_DEVICE_NODE *psDeviceNode,
  											 DEVMEM_MEMDESC *psFWMemContextMemDesc,
  											 IMG_UINT32 ui32Priority,
  											 RGX_COMMON_CONTEXT_INFO *psInfo,
  											 RGX_SERVER_TQ_3D_DATA *ps3DData)
  {
  	PVRSRV_RGXDEV_INFO *psDevInfo = psDeviceNode->pvDevice;
  	PVRSRV_ERROR eError;
  
  	/*
  		Allocate device memory for the firmware GPU context suspend state.
  		Note: the FW reads/writes the state to memory by accessing the GPU register interface.
  	*/
  	PDUMPCOMMENT("Allocate RGX firmware TQ/3D context suspend state");
  
  	eError = DevmemFwAllocate(psDevInfo,
  							sizeof(RGXFWIF_3DCTX_STATE),
  							RGX_FWCOMCTX_ALLOCFLAGS,
  							"FirmwareTQ3DContext",
  							&ps3DData->psFWContextStateMemDesc);
  	if (eError != PVRSRV_OK)
  	{
  		goto fail_contextswitchstate;
  	}
  
  	eError = FWCommonContextAllocate(psConnection,
  									 psDeviceNode,
  									 "TQ_3D",
  									 IMG_NULL,
  									 0,
  									 psFWMemContextMemDesc,
  									 ps3DData->psFWContextStateMemDesc,
  									 RGX_CCB_SIZE_LOG2,
  									 ui32Priority,
  									 psInfo,
  									 &ps3DData->psServerCommonContext);
  	if (eError != PVRSRV_OK)
  	{
  		goto fail_contextalloc;
  	}
  
  
  	PDUMPCOMMENT("Dump 3D context suspend state buffer");
  	DevmemPDumpLoadMem(ps3DData->psFWContextStateMemDesc, 0, sizeof(RGXFWIF_3DCTX_STATE), PDUMP_FLAGS_CONTINUOUS);
  
  	ps3DData->ui32Priority = ui32Priority;
  	return PVRSRV_OK;
  
  fail_contextalloc:
  	DevmemFwFree(ps3DData->psFWContextStateMemDesc);
  fail_contextswitchstate:
  	PVR_ASSERT(eError != PVRSRV_OK);
  	return eError;
  }
  
  static PVRSRV_ERROR _Create2DTransferContext(CONNECTION_DATA *psConnection,
  											 PVRSRV_DEVICE_NODE *psDeviceNode,
  											 DEVMEM_MEMDESC *psFWMemContextMemDesc,
  											 IMG_UINT32 ui32Priority,
  											 RGX_COMMON_CONTEXT_INFO *psInfo,
  											 RGX_SERVER_TQ_2D_DATA *ps2DData)
  {
  	PVRSRV_ERROR eError;
  
  	eError = FWCommonContextAllocate(psConnection,
  									 psDeviceNode,
  									 "TQ_2D",
  									 IMG_NULL,
  									 0,
  									 psFWMemContextMemDesc,
  									 IMG_NULL,
  									 RGX_CCB_SIZE_LOG2,
  									 ui32Priority,
  									 psInfo,
  									 &ps2DData->psServerCommonContext);
  	if (eError != PVRSRV_OK)
  	{
  		goto fail_contextalloc;
  	}
  
  	ps2DData->ui32Priority = ui32Priority;
  	return PVRSRV_OK;
  
  fail_contextalloc:
  	PVR_ASSERT(eError != PVRSRV_OK);
  	return eError;
  }
  
  
  static PVRSRV_ERROR _Destroy2DTransferContext(RGX_SERVER_TQ_2D_DATA *ps2DData,
  											  PVRSRV_DEVICE_NODE *psDeviceNode,
  											  PVRSRV_CLIENT_SYNC_PRIM *psCleanupSync)
  {
  	PVRSRV_ERROR eError;
  
  	/* Check if the FW has finished with this resource ... */
  	eError = RGXFWRequestCommonContextCleanUp(psDeviceNode,
  											  FWCommonContextGetFWAddress(ps2DData->psServerCommonContext),
  											  psCleanupSync,
  											  RGXFWIF_DM_2D);
  	if (eError == PVRSRV_ERROR_RETRY)
  	{
  		return eError;
  	}
  	else if (eError != PVRSRV_OK)
  	{
  		PVR_LOG(("%s: Unexpected error from RGXFWRequestCommonContextCleanUp (%s)",
  				 __FUNCTION__,
  				 PVRSRVGetErrorStringKM(eError)));
  	}
  
  	/* ... it has so we can free it's resources */
  	FWCommonContextFree(ps2DData->psServerCommonContext);
  	return PVRSRV_OK;
  }
  
  static PVRSRV_ERROR _Destroy3DTransferContext(RGX_SERVER_TQ_3D_DATA *ps3DData,
  											  PVRSRV_DEVICE_NODE *psDeviceNode,
  											  PVRSRV_CLIENT_SYNC_PRIM *psCleanupSync)
  {
  	PVRSRV_ERROR eError;
  
  	/* Check if the FW has finished with this resource ... */
  	eError = RGXFWRequestCommonContextCleanUp(psDeviceNode,
  											  FWCommonContextGetFWAddress(ps3DData->psServerCommonContext),
  											  psCleanupSync,
  											  RGXFWIF_DM_3D);
  	if (eError == PVRSRV_ERROR_RETRY)
  	{
  		return eError;
  	}
  	else if (eError != PVRSRV_OK)
  	{
  		PVR_LOG(("%s: Unexpected error from RGXFWRequestCommonContextCleanUp (%s)",
  				 __FUNCTION__,
  				 PVRSRVGetErrorStringKM(eError)));
  	}
  
  	/* ... it has so we can free it's resources */
  	DevmemFwFree(ps3DData->psFWContextStateMemDesc);
  	FWCommonContextFree(ps3DData->psServerCommonContext);
  
  	return PVRSRV_OK;
  }
  
  /*
   * PVRSRVCreateTransferContextKM
   */
  IMG_EXPORT
  PVRSRV_ERROR PVRSRVRGXCreateTransferContextKM(CONNECTION_DATA		*psConnection,
  										   PVRSRV_DEVICE_NODE		*psDeviceNode,
  										   IMG_UINT32				ui32Priority,
  										   IMG_DEV_VIRTADDR			sMCUFenceAddr,
  										   IMG_UINT32				ui32FrameworkCommandSize,
  										   IMG_PBYTE				pabyFrameworkCommand,
  										   IMG_HANDLE				hMemCtxPrivData,
  										   RGX_SERVER_TQ_CONTEXT	**ppsTransferContext)
  {
  	RGX_SERVER_TQ_CONTEXT	*psTransferContext;
  	DEVMEM_MEMDESC			*psFWMemContextMemDesc = RGXGetFWMemDescFromMemoryContextHandle(hMemCtxPrivData);
  	RGX_COMMON_CONTEXT_INFO	sInfo;
  	PVRSRV_ERROR			eError = PVRSRV_OK;
  
  	/* Allocate the server side structure */
  	psTransferContext = OSAllocMem(sizeof(*psTransferContext));
  	if (psTransferContext == IMG_NULL)
  	{
  		return PVRSRV_ERROR_OUT_OF_MEMORY;
  	}
  
  	OSMemSet(psTransferContext, 0, sizeof(*psTransferContext));
  	*ppsTransferContext = psTransferContext;
  
  	psTransferContext->psDeviceNode = psDeviceNode;
  
  	/* Allocate cleanup sync */
  	eError = SyncPrimAlloc(psDeviceNode->hSyncPrimContext,
  						   &psTransferContext->psCleanupSync);
  	if (eError != PVRSRV_OK)
  	{
  		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateTransferContextKM: Failed to allocate cleanup sync (0x%x)",
  				eError));
  		goto fail_syncalloc;
  	}
  
  	/* 
  	 * Create the FW framework buffer
  	 */
  	eError = PVRSRVRGXFrameworkCreateKM(psDeviceNode,
  										&psTransferContext->psFWFrameworkMemDesc,
  										ui32FrameworkCommandSize);
  	if (eError != PVRSRV_OK)
  	{
  		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateTransferContextKM: Failed to allocate firmware GPU framework state (%u)",
  				eError));
  		goto fail_frameworkcreate;
  	}
  
  	/* Copy the Framework client data into the framework buffer */
  	eError = PVRSRVRGXFrameworkCopyCommand(psTransferContext->psFWFrameworkMemDesc,
  										   pabyFrameworkCommand,
  										   ui32FrameworkCommandSize);
  	if (eError != PVRSRV_OK)
  	{
  		PVR_DPF((PVR_DBG_ERROR,"PVRSRVCreateTransferContextKM: Failed to populate the framework buffer (%u)",
  				eError));
  		goto fail_frameworkcopy;
  	}
  
  	sInfo.psFWFrameworkMemDesc = psTransferContext->psFWFrameworkMemDesc;
  	sInfo.psMCUFenceAddr = &sMCUFenceAddr;
  
  	eError = _Create3DTransferContext(psConnection,
  									  psDeviceNode,
  									  psFWMemContextMemDesc,
  									  ui32Priority,
  									  &sInfo,
  									  &psTransferContext->s3DData);
  	if (eError != PVRSRV_OK)
  	{
  		goto fail_3dtransfercontext;
  	}
  	psTransferContext->ui32Flags |= RGX_SERVER_TQ_CONTEXT_FLAGS_3D;
  
  	eError = _Create2DTransferContext(psConnection,
  									  psDeviceNode,
  									  psFWMemContextMemDesc,
  									  ui32Priority,
  									  &sInfo,
  									  &psTransferContext->s2DData);
  	if (eError != PVRSRV_OK)
  	{
  		goto fail_2dtransfercontext;
  	}
  	psTransferContext->ui32Flags |= RGX_SERVER_TQ_CONTEXT_FLAGS_2D;
  
  	{
  		PVRSRV_RGXDEV_INFO			*psDevInfo = psDeviceNode->pvDevice;
  		dllist_add_to_tail(&(psDevInfo->sTransferCtxtListHead), &(psTransferContext->sListNode));
  	}
  
  	return PVRSRV_OK;
  
  fail_2dtransfercontext:
  	_Destroy3DTransferContext(&psTransferContext->s3DData,
  							  psTransferContext->psDeviceNode,
  							  psTransferContext->psCleanupSync);
  fail_3dtransfercontext:
  fail_frameworkcopy:
  	DevmemFwFree(psTransferContext->psFWFrameworkMemDesc);
  fail_frameworkcreate:
  	SyncPrimFree(psTransferContext->psCleanupSync);
  fail_syncalloc:
  	OSFreeMem(psTransferContext);
  	PVR_ASSERT(eError != PVRSRV_OK);
  	return eError;
  }
  
  IMG_EXPORT
  PVRSRV_ERROR PVRSRVRGXDestroyTransferContextKM(RGX_SERVER_TQ_CONTEXT *psTransferContext)
  {
  	PVRSRV_ERROR eError;
  
  	if (psTransferContext->ui32Flags & RGX_SERVER_TQ_CONTEXT_FLAGS_2D)
  	{
  		eError = _Destroy2DTransferContext(&psTransferContext->s2DData,
  										   psTransferContext->psDeviceNode,
  										   psTransferContext->psCleanupSync);
  		if (eError != PVRSRV_OK)
  		{
  			goto fail_destroy2d;
  		}
  		/* We've freed the 2D context, don't try to free it again */
  		psTransferContext->ui32Flags &= ~RGX_SERVER_TQ_CONTEXT_FLAGS_2D;
  	}
  
  	if (psTransferContext->ui32Flags & RGX_SERVER_TQ_CONTEXT_FLAGS_3D)
  	{
  		eError = _Destroy3DTransferContext(&psTransferContext->s3DData,
  										   psTransferContext->psDeviceNode,
  										   psTransferContext->psCleanupSync);
  		if (eError != PVRSRV_OK)
  		{
  			goto fail_destroy3d;
  		}
  		/* We've freed the 3D context, don't try to free it again */
  		psTransferContext->ui32Flags &= ~RGX_SERVER_TQ_CONTEXT_FLAGS_3D;
  	}
  	dllist_remove_node(&(psTransferContext->sListNode));
  	DevmemFwFree(psTransferContext->psFWFrameworkMemDesc);
  	SyncPrimFree(psTransferContext->psCleanupSync);
  
  	OSFreeMem(psTransferContext);
  
  	return PVRSRV_OK;
  
  fail_destroy2d:
  fail_destroy3d:
  	PVR_ASSERT(eError != PVRSRV_OK);
  	return eError;
  }
  
  /*
   * PVRSRVSubmitTQ3DKickKM
   */
  IMG_EXPORT
  PVRSRV_ERROR PVRSRVRGXSubmitTransferKM(RGX_SERVER_TQ_CONTEXT	*psTransferContext,
  									   IMG_UINT32				ui32PrepareCount,
  									   IMG_UINT32				*paui32ClientFenceCount,
  									   PRGXFWIF_UFO_ADDR		**papauiClientFenceUFOAddress,
  									   IMG_UINT32				**papaui32ClientFenceValue,
  									   IMG_UINT32				*paui32ClientUpdateCount,
  									   PRGXFWIF_UFO_ADDR		**papauiClientUpdateUFOAddress,
  									   IMG_UINT32				**papaui32ClientUpdateValue,
  									   IMG_UINT32				*paui32ServerSyncCount,
  									   IMG_UINT32				**papaui32ServerSyncFlags,
  									   SERVER_SYNC_PRIMITIVE	***papapsServerSyncs,
  									   IMG_UINT32				ui32NumFenceFDs,
  									   IMG_INT32				*paui32FenceFDs,
  									   IMG_UINT32				*paui32FWCommandSize,
  									   IMG_UINT8				**papaui8FWCommand,
  									   IMG_UINT32				*pui32TQPrepareFlags)
  {
  	PVRSRV_DEVICE_NODE *psDeviceNode = psTransferContext->psDeviceNode;
  #if defined(WDDM)
  	RGX_CCB_CMD_HELPER_DATA as3DCmdHelper[TQ_MAX_PREPARES_PER_SUBMIT];
  	RGX_CCB_CMD_HELPER_DATA as2DCmdHelper[TQ_MAX_PREPARES_PER_SUBMIT];
  #endif
  	RGX_CCB_CMD_HELPER_DATA *pas3DCmdHelper;
  	RGX_CCB_CMD_HELPER_DATA *pas2DCmdHelper;
  	IMG_UINT32 ui323DCmdCount = 0;
  	IMG_UINT32 ui322DCmdCount = 0;
  	IMG_BOOL bKick2D = IMG_FALSE;
  	IMG_BOOL bKick3D = IMG_FALSE;
  	IMG_BOOL bPDumpContinuous = IMG_FALSE;
  	IMG_UINT32 i;
  	IMG_UINT32 ui32IntClientFenceCount = 0;
  	PRGXFWIF_UFO_ADDR *pauiIntFenceUFOAddress = IMG_NULL;
  	IMG_UINT32 *paui32IntFenceValue = IMG_NULL;
  	IMG_UINT32 ui32IntClientUpdateCount = 0;
  	PRGXFWIF_UFO_ADDR *pauiIntUpdateUFOAddress = IMG_NULL;
  	IMG_UINT32 *paui32IntUpdateValue = IMG_NULL;
  	PVRSRV_ERROR eError;
  	PVRSRV_ERROR eError2;
  
  #if defined(PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC)
  	IMG_BOOL bSyncsMerged = IMG_FALSE;
  #endif
  
  	if (ui32PrepareCount == 0)
  	{
  		return PVRSRV_ERROR_INVALID_PARAMS;
  	}
  
  	if (ui32NumFenceFDs != 0)
  	{
  #if defined(PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC)
  		/* Fence FD's are only valid in the 3D case with no batching */
  		if ((ui32PrepareCount !=1) && (!TQ_PREP_FLAGS_COMMAND_IS(pui32TQPrepareFlags[0], 3D)))
  		{
  			return PVRSRV_ERROR_INVALID_PARAMS;
  		}
  
  #else
  		/* We only support Fence FD's if built with PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC */
  		return PVRSRV_ERROR_INVALID_PARAMS;
  #endif
  	}
  #if defined(WDDM)
  	pas3DCmdHelper = &as3DCmdHelper;
  	pas2DCmdHelper = &as2DCmdHelper;
  #else
  	/* We can't allocate the required amount of stack space on all consumer architectures */
  	pas3DCmdHelper = OSAllocMem(sizeof(*pas3DCmdHelper) * ui32PrepareCount);
  	if (pas3DCmdHelper == IMG_NULL)
  	{
  		eError = PVRSRV_ERROR_OUT_OF_MEMORY;
  		goto fail_alloc3dhelper;
  	}
  	pas2DCmdHelper = OSAllocMem(sizeof(*pas2DCmdHelper) * ui32PrepareCount);
  	if (pas2DCmdHelper == IMG_NULL)
  	{
  		eError = PVRSRV_ERROR_OUT_OF_MEMORY;
  		goto fail_alloc2dhelper;
  	}
  #endif
  	/*
  		Ensure we do the right thing for server syncs which cross call bounderies
  	*/
  	for (i=0;i<ui32PrepareCount;i++)
  	{
  		IMG_BOOL bHaveStartPrepare = pui32TQPrepareFlags[i] & TQ_PREP_FLAGS_START;
  		IMG_BOOL bHaveEndPrepare = IMG_FALSE;
  
  		if (bHaveStartPrepare)
  		{
  			IMG_UINT32 k;
  			/*
  				We've at the start of a transfer operation (which might be made
  				up of multiple HW operations) so check if we also have then
  				end of the transfer operation in the batch
  			*/
  			for (k=i;k<ui32PrepareCount;k++)
  			{
  				if (pui32TQPrepareFlags[k] & TQ_PREP_FLAGS_END)
  				{
  					bHaveEndPrepare = IMG_TRUE;
  					break;
  				}
  			}
  
  			if (!bHaveEndPrepare)
  			{
  				/*
  					We don't have the complete command passed in this call
  					so drop the update request. When we get called again with
  					the last HW command in this transfer operation we'll do
  					the update at that point.
  				*/
  				for (k=0;k<paui32ServerSyncCount[i];k++)
  				{
  					papaui32ServerSyncFlags[i][k] &= ~PVRSRV_CLIENT_SYNC_PRIM_OP_UPDATE;
  				}
  			}
  		}
  	}
  
  
  	/*
  		Init the command helper commands for all the prepares
  	*/
  	for (i=0;i<ui32PrepareCount;i++)
  	{
  		RGX_CLIENT_CCB *psClientCCB;
  		RGX_SERVER_COMMON_CONTEXT *psServerCommonCtx;
  		IMG_CHAR *pszCommandName;
  		RGX_CCB_CMD_HELPER_DATA *psCmdHelper;
  		RGXFWIF_CCB_CMD_TYPE eType;
  
  		if (TQ_PREP_FLAGS_COMMAND_IS(pui32TQPrepareFlags[i], 3D))
  		{
  			psServerCommonCtx = psTransferContext->s3DData.psServerCommonContext;
  			psClientCCB = FWCommonContextGetClientCCB(psServerCommonCtx);
  			pszCommandName = "TQ-3D";
  			psCmdHelper = &pas3DCmdHelper[ui323DCmdCount++];
  			eType = RGXFWIF_CCB_CMD_TYPE_TQ_3D;
  		}
  		else if (TQ_PREP_FLAGS_COMMAND_IS(pui32TQPrepareFlags[i], 2D))
  		{
  			psServerCommonCtx = psTransferContext->s2DData.psServerCommonContext;
  			psClientCCB = FWCommonContextGetClientCCB(psServerCommonCtx);
  			pszCommandName = "TQ-2D";
  			psCmdHelper = &pas2DCmdHelper[ui322DCmdCount++];
  			eType = RGXFWIF_CCB_CMD_TYPE_TQ_2D;
  		}
  		else
  		{
  			eError = PVRSRV_ERROR_INVALID_PARAMS;
  			goto fail_cmdtype;
  		}
  
  		if (i == 0)
  		{
  			bPDumpContinuous = ((pui32TQPrepareFlags[i] & TQ_PREP_FLAGS_PDUMPCONTINUOUS) == TQ_PREP_FLAGS_PDUMPCONTINUOUS);
  			PDUMPCOMMENTWITHFLAGS((bPDumpContinuous) ? PDUMP_FLAGS_CONTINUOUS : 0,
  					"%s Command Server Submit on FWCtx %08x", pszCommandName, FWCommonContextGetFWAddress(psServerCommonCtx).ui32Addr);
  		}
  		else
  		{
  			IMG_BOOL bNewPDumpContinuous = ((pui32TQPrepareFlags[i] & TQ_PREP_FLAGS_PDUMPCONTINUOUS) == TQ_PREP_FLAGS_PDUMPCONTINUOUS);
  
  			if (bNewPDumpContinuous != bPDumpContinuous)
  			{
  				eError = PVRSRV_ERROR_INVALID_PARAMS;
  				PVR_DPF((PVR_DBG_ERROR, "%s: Mixing of continuous and non-continuous command in a batch is not permitted", __FUNCTION__));
  				goto fail_pdumpcheck;
  			}
  		}
  
  #if defined(PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC)
  		if (ui32NumFenceFDs)
  		{
  			IMG_UINT32 ui32NumFenceSyncs;
  			PRGXFWIF_UFO_ADDR *puiFenceFWAddrs;
  			IMG_UINT32 *pui32FenceValues;
  			IMG_UINT32 ui32NumUpdateSyncs;
  			PRGXFWIF_UFO_ADDR *puiUpdateFWAddrs;
  			IMG_UINT32 *pui32UpdateValues;
  
  			/*
  				FIXME:
  				We can't be taking the server sync operations here as we
  				have no way to undo them should the acquire fail.
  				If client/local syncs where used here would that remove the
  				issue?
  			*/
  			eError = PVRFDSyncQueryFencesKM(ui32NumFenceFDs,
  											paui32FenceFDs,
  											IMG_TRUE,
  											&ui32NumFenceSyncs,
  											&puiFenceFWAddrs,
  											&pui32FenceValues,
  											&ui32NumUpdateSyncs,
  											&puiUpdateFWAddrs,
  											&pui32UpdateValues);
  			if (eError != PVRSRV_OK)
  			{
  				goto fail_fdsync;
  			}
  
  			/*
  				Merge the Android syncs and the client syncs together
  			*/
  			ui32IntClientFenceCount = paui32ClientFenceCount[i] + ui32NumFenceSyncs;
  			pauiIntFenceUFOAddress = OSAllocMem(sizeof(*pauiIntFenceUFOAddress)* ui32IntClientFenceCount);
  			if (pauiIntFenceUFOAddress == IMG_NULL)
  			{
  				/* Free memory created by PVRFDSyncQueryFencesKM */
  				OSFreeMem(puiFenceFWAddrs);
  				OSFreeMem(pui32FenceValues);
  				OSFreeMem(puiUpdateFWAddrs);
  				OSFreeMem(pui32UpdateValues);
  
  				goto fail_fenceUFOarray;
  			}	
  			paui32IntFenceValue = OSAllocMem(sizeof(*paui32IntFenceValue)* ui32IntClientFenceCount);
  			if (paui32IntFenceValue == IMG_NULL)
  			{
  				/* Free memory created by PVRFDSyncQueryFencesKM */
  				OSFreeMem(puiFenceFWAddrs);
  				OSFreeMem(pui32FenceValues);
  				OSFreeMem(puiUpdateFWAddrs);
  				OSFreeMem(pui32UpdateValues);
  
  				OSFreeMem(pauiIntFenceUFOAddress);
  				goto fail_fencevaluearray;
  			}
  			ui32IntClientUpdateCount = paui32ClientUpdateCount[i] + ui32NumUpdateSyncs;
  			pauiIntUpdateUFOAddress = OSAllocMem(sizeof(*pauiIntUpdateUFOAddress)* ui32IntClientUpdateCount);
  			if (pauiIntUpdateUFOAddress == IMG_NULL)
  			{
  				/* Free memory created by PVRFDSyncQueryFencesKM */
  				OSFreeMem(puiFenceFWAddrs);
  				OSFreeMem(pui32FenceValues);
  				OSFreeMem(puiUpdateFWAddrs);
  				OSFreeMem(pui32UpdateValues);
  
  				OSFreeMem(pauiIntFenceUFOAddress);
  				OSFreeMem(paui32IntFenceValue);
  				goto fail_updateUFOarray;
  			}
  			paui32IntUpdateValue = OSAllocMem(sizeof(*paui32IntUpdateValue)* ui32IntClientUpdateCount);
  			if (paui32IntUpdateValue == IMG_NULL)
  			{
  				/* Free memory created by PVRFDSyncQueryFencesKM */
  				OSFreeMem(puiFenceFWAddrs);
  				OSFreeMem(pui32FenceValues);
  				OSFreeMem(puiUpdateFWAddrs);
  				OSFreeMem(pui32UpdateValues);
  
  				OSFreeMem(pauiIntFenceUFOAddress);
  				OSFreeMem(paui32IntFenceValue);
  				OSFreeMem(pauiIntUpdateUFOAddress);
  				goto fail_updatevaluearray;
  			}
  
  			SYNC_MERGE_CLIENT_FENCES(ui32IntClientFenceCount, pauiIntFenceUFOAddress, paui32IntFenceValue,
  									 ui32NumFenceSyncs, puiFenceFWAddrs, pui32FenceValues,
  									 paui32ClientFenceCount[i], papauiClientFenceUFOAddress[i], papaui32ClientFenceValue[i]);
  
  			SYNC_MERGE_CLIENT_UPDATES(ui32IntClientUpdateCount, pauiIntUpdateUFOAddress, paui32IntUpdateValue,
  									 ui32NumUpdateSyncs, puiUpdateFWAddrs, pui32UpdateValues,
  									 paui32ClientUpdateCount[i], papauiClientUpdateUFOAddress[i], papaui32ClientUpdateValue[i]);
  
  			if (ui32NumFenceSyncs || ui32NumUpdateSyncs)
  			{
  				PDUMPCOMMENT("(TQ) Android native fences in use: %u fence syncs, %u update syncs",
  							 ui32NumFenceSyncs, ui32NumUpdateSyncs);
  			}		
  
  			/*
  				Free the data created by PVRFDSyncQueryFencesKM as it has now
  				been merged into *IntClient*
  			*/
  			OSFreeMem(puiFenceFWAddrs);
  			OSFreeMem(pui32FenceValues);
  			OSFreeMem(puiUpdateFWAddrs);
  			OSFreeMem(pui32UpdateValues);
  			bSyncsMerged = IMG_TRUE;
  		}
  		else
  #endif /* PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC */
  		{
  			/* No client sync merging so just copy across the pointers */
  			ui32IntClientFenceCount = paui32ClientFenceCount[i];
  			pauiIntFenceUFOAddress = papauiClientFenceUFOAddress[i];
  			paui32IntFenceValue = papaui32ClientFenceValue[i];
  			ui32IntClientUpdateCount = paui32ClientUpdateCount[i];
  			pauiIntUpdateUFOAddress = papauiClientUpdateUFOAddress[i];
  			paui32IntUpdateValue = papaui32ClientUpdateValue[i];
  		}
  
  
  		/*
  			Create the command helper data for this command
  		*/
  		eError = RGXCmdHelperInitCmdCCB(psClientCCB,
  										ui32IntClientFenceCount,
  										pauiIntFenceUFOAddress,
  										paui32IntFenceValue,
  										ui32IntClientUpdateCount,
  										pauiIntUpdateUFOAddress,
  										paui32IntUpdateValue,
  										paui32ServerSyncCount[i],
  										papaui32ServerSyncFlags[i],
  										papapsServerSyncs[i],
  										paui32FWCommandSize[i],
  										papaui8FWCommand[i],
  										eType,
  										bPDumpContinuous,
  										pszCommandName,
  										psCmdHelper);
  		if (eError != PVRSRV_OK)
  		{
  			goto fail_initcmd;
  		}
  	}
  
  	/*
  		Acquire space for all the commands in one go
  	*/
  	if (ui323DCmdCount)
  	{
  		
  		eError = RGXCmdHelperAcquireCmdCCB(ui323DCmdCount,
  										   &pas3DCmdHelper[0],
  										   &bKick3D);
  		if (eError != PVRSRV_OK)
  		{
  			if (bKick3D)
  			{
  				ui323DCmdCount = 0;
  				ui322DCmdCount = 0;
  			}
  			else
  			{
  				goto fail_3dcmdacquire;
  			}
  		}
  	}
  
  	if (ui322DCmdCount)
  	{
  		eError = RGXCmdHelperAcquireCmdCCB(ui322DCmdCount,
  										   &pas2DCmdHelper[0],
  										   &bKick2D);
  	
  		if (eError != PVRSRV_OK)
  		{
  			if (bKick2D || bKick3D)
  			{
  				ui323DCmdCount = 0;
  				ui322DCmdCount = 0;
  			}
  			else
  			{
  				goto fail_2dcmdacquire;
  			}
  		}
  	}
  
  	/*
  		We should acquire the kernel CCB(s) space here as the schedule could fail
  		and we would have to roll back all the syncs
  	*/
  
  	/*
  		Only do the command helper release (which takes the server sync
  		operations if the acquire succeeded
  	*/
  	if (ui323DCmdCount)
  	{
  		RGXCmdHelperReleaseCmdCCB(ui323DCmdCount,
  								  &pas3DCmdHelper[0],
  								  "TQ_3D",
  								  FWCommonContextGetFWAddress(psTransferContext->s3DData.psServerCommonContext).ui32Addr);
  		
  	}
  
  	if (ui322DCmdCount)
  	{
  		RGXCmdHelperReleaseCmdCCB(ui322DCmdCount,
  								  &pas2DCmdHelper[0],
  								  "TQ_2D",
  								  FWCommonContextGetFWAddress(psTransferContext->s2DData.psServerCommonContext).ui32Addr);
  	}
  
  	/*
  		Even if we failed to acquire the client CCB space we might still need
  		to kick the HW to process a padding packet to release space for us next
  		time round
  	*/
  	if (bKick3D)
  	{
  		RGXFWIF_KCCB_CMD s3DKCCBCmd;
  
  		/* Construct the kernel 3D CCB command. */
  		s3DKCCBCmd.eCmdType = RGXFWIF_KCCB_CMD_KICK;
  		s3DKCCBCmd.uCmdData.sCmdKickData.psContext = FWCommonContextGetFWAddress(psTransferContext->s3DData.psServerCommonContext);
  		s3DKCCBCmd.uCmdData.sCmdKickData.ui32CWoffUpdate = RGXGetHostWriteOffsetCCB(FWCommonContextGetClientCCB(psTransferContext->s3DData.psServerCommonContext));
  		s3DKCCBCmd.uCmdData.sCmdKickData.ui32NumCleanupCtl = 0;
  
  		LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
  		{
  			eError2 = RGXScheduleCommand(psDeviceNode->pvDevice,
  										RGXFWIF_DM_3D,
  										&s3DKCCBCmd,
  										sizeof(s3DKCCBCmd),
  										bPDumpContinuous);
  			if (eError2 != PVRSRV_ERROR_RETRY)
  			{
  				break;
  			}
  			OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
  		} END_LOOP_UNTIL_TIMEOUT();
  	}
  
  	if (bKick2D)
  	{
  		RGXFWIF_KCCB_CMD s2DKCCBCmd;
  
  		/* Construct the kernel 3D CCB command. */
  		s2DKCCBCmd.eCmdType = RGXFWIF_KCCB_CMD_KICK;
  		s2DKCCBCmd.uCmdData.sCmdKickData.psContext = FWCommonContextGetFWAddress(psTransferContext->s2DData.psServerCommonContext);
  		s2DKCCBCmd.uCmdData.sCmdKickData.ui32CWoffUpdate = RGXGetHostWriteOffsetCCB(FWCommonContextGetClientCCB(psTransferContext->s2DData.psServerCommonContext));
  		s2DKCCBCmd.uCmdData.sCmdKickData.ui32NumCleanupCtl = 0;
  
  		LOOP_UNTIL_TIMEOUT(MAX_HW_TIME_US)
  		{
  			eError2 = RGXScheduleCommand(psDeviceNode->pvDevice,
  										RGXFWIF_DM_2D,
  										&s2DKCCBCmd,
  										sizeof(s2DKCCBCmd),
  										bPDumpContinuous);
  			if (eError2 != PVRSRV_ERROR_RETRY)
  			{
  				break;
  			}
  			OSWaitus(MAX_HW_TIME_US/WAIT_TRY_COUNT);
  		} END_LOOP_UNTIL_TIMEOUT();
  	}
  
  	/*
  	 * Now check eError (which may have returned an error from our earlier calls
  	 * to RGXCmdHelperAcquireCmdCCB) - we needed to process any flush command first
  	 * so we check it now...
  	 */
  	if (eError != PVRSRV_OK )
  	{
  		goto fail_2dcmdacquire;
  	}
  
  #if defined(PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC)
  	/*
  		Free the merged sync memory if required
  	*/
  	if (bSyncsMerged)
  	{
  		OSFreeMem(pauiIntFenceUFOAddress);
  		OSFreeMem(paui32IntFenceValue);
  		OSFreeMem(pauiIntUpdateUFOAddress);
  		OSFreeMem(paui32IntUpdateValue);
  	}
  #if defined(NO_HARDWARE)
      for (i = 0; i < ui32NumFenceFDs; i++) 
      {    
  		eError = PVRFDSyncNoHwUpdateFenceKM(paui32FenceFDs[i]);
          if (eError != PVRSRV_OK)
          {    
              PVR_DPF((PVR_DBG_ERROR, "%s: Failed nohw update on fence fd=%d (%s)",
                       __func__, paui32FenceFDs[i], PVRSRVGetErrorStringKM(eError)));
          }    
      }    
  #endif
  #endif
  
  #if !defined(WDDM)
  	OSFreeMem(pas2DCmdHelper);
  	OSFreeMem(pas3DCmdHelper);
  #endif
  
  	return PVRSRV_OK;
  
  /*
  	No resources are created in this function so there is nothing to free
  	unless we had to merge syncs.
  	If we fail after the client CCB acquire there is still nothing to do
  	as only the client CCB release will modify the client CCB
  */
  fail_2dcmdacquire:
  fail_3dcmdacquire:
  fail_initcmd:
  #if defined(PVR_ANDROID_NATIVE_WINDOW_HAS_SYNC)
  	if (bSyncsMerged)
  	{
  		OSFreeMem(pauiIntFenceUFOAddress);
  		OSFreeMem(paui32IntFenceValue);
  		OSFreeMem(pauiIntUpdateUFOAddress);
  		OSFreeMem(paui32IntUpdateValue);
  	}
  fail_updatevaluearray:
  fail_updateUFOarray:
  fail_fencevaluearray:
  fail_fenceUFOarray:
  fail_fdsync:
  #endif
  fail_pdumpcheck:
  fail_cmdtype:
  	PVR_ASSERT(eError != PVRSRV_OK);
  #if !defined(WDDM)
  	OSFreeMem(pas2DCmdHelper);
  fail_alloc2dhelper:
  	OSFreeMem(pas3DCmdHelper);
  fail_alloc3dhelper:
  #endif
  	return eError;
  }
  
  PVRSRV_ERROR PVRSRVRGXSetTransferContextPriorityKM(CONNECTION_DATA *psConnection,
  												   RGX_SERVER_TQ_CONTEXT *psTransferContext,
  												   IMG_UINT32 ui32Priority)
  {
  	PVRSRV_ERROR eError;
  
  	if (psTransferContext->s2DData.ui32Priority != ui32Priority)
  	{
  		eError = ContextSetPriority(psTransferContext->s2DData.psServerCommonContext,
  									psConnection,
  									psTransferContext->psDeviceNode->pvDevice,
  									ui32Priority,
  									RGXFWIF_DM_2D);
  		if (eError != PVRSRV_OK)
  		{
  			PVR_DPF((PVR_DBG_ERROR, "%s: Failed to set the priority of the 2D part of the transfercontext", __FUNCTION__));
  			goto fail_2dcontext;
  		}
  		psTransferContext->s2DData.ui32Priority = ui32Priority;
  	}
  
  	if (psTransferContext->s3DData.ui32Priority != ui32Priority)
  	{
  		eError = ContextSetPriority(psTransferContext->s3DData.psServerCommonContext,
  									psConnection,
  									psTransferContext->psDeviceNode->pvDevice,
  									ui32Priority,
  									RGXFWIF_DM_3D);
  		if (eError != PVRSRV_OK)
  		{
  			PVR_DPF((PVR_DBG_ERROR, "%s: Failed to set the priority of the 3D part of the transfercontext", __FUNCTION__));
  			goto fail_3dcontext;
  		}
  		psTransferContext->s3DData.ui32Priority = ui32Priority;
  	}
  	return PVRSRV_OK;
  
  fail_3dcontext:
  fail_2dcontext:
  	PVR_ASSERT(eError != PVRSRV_OK);
  	return eError;
  }
  
  static IMG_BOOL CheckForStalledTransferCtxtCommand(PDLLIST_NODE psNode, IMG_PVOID pvCallbackData)
  {
  	RGX_SERVER_TQ_CONTEXT 		*psCurrentServerTransferCtx = IMG_CONTAINER_OF(psNode, RGX_SERVER_TQ_CONTEXT, sListNode);
  	RGX_SERVER_TQ_2D_DATA		*psTransferCtx2DData = &(psCurrentServerTransferCtx->s2DData);
  	RGX_SERVER_COMMON_CONTEXT	*psCurrentServerTQ2DCommonCtx = psTransferCtx2DData->psServerCommonContext;
  	RGX_SERVER_TQ_3D_DATA		*psTransferCtx3DData = &(psCurrentServerTransferCtx->s3DData);
  	RGX_SERVER_COMMON_CONTEXT	*psCurrentServerTQ3DCommonCtx = psTransferCtx3DData->psServerCommonContext;
  
  
  	DumpStalledFWCommonContext(psCurrentServerTQ2DCommonCtx);
  	DumpStalledFWCommonContext(psCurrentServerTQ3DCommonCtx);
  
  	return IMG_TRUE;
  }
  IMG_VOID CheckForStalledTransferCtxt(PVRSRV_RGXDEV_INFO *psDevInfo)
  {
  	dllist_foreach_node(&(psDevInfo->sTransferCtxtListHead), CheckForStalledTransferCtxtCommand, IMG_NULL);
  }
  
  /**************************************************************************//**
   End of file (rgxtransfer.c)
  ******************************************************************************/
  