misc:mediatek:gpu:gpu_rgx: Fix GPU MMU cleanup timeout

Cherry pick changes from DDK 1.13
1. fix sync value overrun issue in cleanup thread for free
 GPU mmu mapping
2. set correct timeout value

Bug: 171901091
Change-Id: Ida7de1d3a3fef94ee40d6bfc3f71d9859febfcdb
diff --git a/drivers/misc/mediatek/gpu/gpu_rgx/m1.11_5516664/mmu_common.c b/drivers/misc/mediatek/gpu/gpu_rgx/m1.11_5516664/mmu_common.c
index 2f3709c..a14b4f2 100644
--- a/drivers/misc/mediatek/gpu/gpu_rgx/m1.11_5516664/mmu_common.c
+++ b/drivers/misc/mediatek/gpu/gpu_rgx/m1.11_5516664/mmu_common.c
@@ -372,27 +372,45 @@
 	if (psCleanup->psSync == NULL)
 	{
 		/* Kick to invalidate the MMU caches and get sync info */
-		psDevNode->pfnMMUCacheInvalidateKick(psDevNode,
+		eError = psDevNode->pfnMMUCacheInvalidateKick(psDevNode,
 		                                     &psCleanup->uiRequiredSyncVal,
 		                                     IMG_TRUE);
+		if (eError != PVRSRV_OK)
+		{
+			OSLockRelease(psMMUCtxCleanupData->hCleanupLock);
+			return PVRSRV_ERROR_RETRY;
+		}
 		psCleanup->psSync = psDevNode->psMMUCacheSyncPrim;
 	}
 
 	uiSyncCurrent = OSReadDeviceMem32(psCleanup->psSync->pui32LinAddr);
 	uiSyncReq = psCleanup->uiRequiredSyncVal;
 
-	/* Either the invalidate has been executed ... */
-	bFreeNow = (uiSyncCurrent >= uiSyncReq) ? IMG_TRUE :
-			/* ... with the counter wrapped around ... */
-			(uiSyncReq - uiSyncCurrent) > 0xEFFFFFFFUL ? IMG_TRUE :
-					/* ... or are we still waiting for the invalidate? */
-					IMG_FALSE;
+	/* Has the invalidate executed */
+	bFreeNow = (uiSyncCurrent >= uiSyncReq) ?
+			/* ... with the counter wrapped around ...
+			 * There can't be 3*1024*1024 transactions completed, so consider wrapped */
+			(((uiSyncCurrent - uiSyncReq) > 0xF0000000UL)? IMG_FALSE : IMG_TRUE):
+			/* There can't be 3*1024*1024 transactions pending, so consider wrapped */
+			(((uiSyncReq - uiSyncCurrent) > 0xF0000000UL)? IMG_TRUE : IMG_FALSE);
 
 #if defined(NO_HARDWARE)
 	/* In NOHW the syncs will never be updated so just free the tables */
 	bFreeNow = IMG_TRUE;
 #endif
 
+	if (!bFreeNow)
+	{
+		/* If the time left for the completion of invalidate operation is
+		 * within 500ms of time-out, consider the operation as timed out */
+		if ((psCleanup->sCleanupThreadFn.ui32TimeEnd - psCleanup->sCleanupThreadFn.ui32TimeStart - 500) <=
+		    (OSClockms() - psCleanup->sCleanupThreadFn.ui32TimeStart))
+		{
+			/* Consider the operation is timed out */
+			bFreeNow = IMG_TRUE;
+		}
+	}
+
 	if (bFreeNow)
 	{
 		_FreeMMUMapping(psDevNode, &psCleanup->sMMUMappingHead);
diff --git a/drivers/misc/mediatek/gpu/gpu_rgx/m1.11_5516664/pvrsrv_cleanup.h b/drivers/misc/mediatek/gpu/gpu_rgx/m1.11_5516664/pvrsrv_cleanup.h
index 99ddcea..d476b8f 100644
--- a/drivers/misc/mediatek/gpu/gpu_rgx/m1.11_5516664/pvrsrv_cleanup.h
+++ b/drivers/misc/mediatek/gpu/gpu_rgx/m1.11_5516664/pvrsrv_cleanup.h
@@ -81,7 +81,7 @@
 /* Like for CLEANUP_THREAD_RETRY_COUNT_DEFAULT but call will wait for
  * a specified amount of time rather than number of retries.
  */
-#define CLEANUP_THREAD_RETRY_TIMEOUT_MS_DEFAULT 2000u /* 2s */
+#define CLEANUP_THREAD_RETRY_TIMEOUT_MS_DEFAULT 20000u /* 20s */
 
 /* Use to set retry count on a cleanup item.
  * _item - pointer to the PVRSRV_CLEANUP_THREAD_WORK