1 /**********************************************************************
2 Copyright (c) Imagination Technologies Ltd.
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 ******************************************************************************/
23 #include "services_headers.h"
27 #include "osperproc.h"
29 #define HASH_TAB_INIT_SIZE 32
31 static HASH_TABLE *psHashTab = IMG_NULL;
33 static PVRSRV_ERROR FreePerProcessData(PVRSRV_PER_PROCESS_DATA *psPerProc)
36 IMG_UINTPTR_T uiPerProc;
38 PVR_ASSERT(psPerProc != IMG_NULL);
40 if (psPerProc == IMG_NULL)
42 PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: invalid parameter"));
43 return PVRSRV_ERROR_INVALID_PARAMS;
46 uiPerProc = HASH_Remove(psHashTab, (IMG_UINTPTR_T)psPerProc->ui32PID);
49 PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: Couldn't find process in per-process data hash table"));
51 PVR_ASSERT(psPerProc->ui32PID == 0);
55 PVR_ASSERT((PVRSRV_PER_PROCESS_DATA *)uiPerProc == psPerProc);
56 PVR_ASSERT(((PVRSRV_PER_PROCESS_DATA *)uiPerProc)->ui32PID == psPerProc->ui32PID);
60 if (psPerProc->psHandleBase != IMG_NULL)
62 eError = PVRSRVFreeHandleBase(psPerProc->psHandleBase);
63 if (eError != PVRSRV_OK)
65 PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: Couldn't free handle base for process (%d)", eError));
71 if (psPerProc->hPerProcData != IMG_NULL)
73 eError = PVRSRVReleaseHandle(KERNEL_HANDLE_BASE, psPerProc->hPerProcData, PVRSRV_HANDLE_TYPE_PERPROC_DATA);
75 if (eError != PVRSRV_OK)
77 PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: Couldn't release per-process data handle (%d)", eError));
83 eError = OSPerProcessPrivateDataDeInit(psPerProc->hOsPrivateData);
84 if (eError != PVRSRV_OK)
86 PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: OSPerProcessPrivateDataDeInit failed (%d)", eError));
90 eError = OSFreeMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
93 psPerProc->hBlockAlloc);
95 if (eError != PVRSRV_OK)
97 PVR_DPF((PVR_DBG_ERROR, "FreePerProcessData: Couldn't free per-process data (%d)", eError));
105 PVRSRV_PER_PROCESS_DATA *PVRSRVPerProcessData(IMG_UINT32 ui32PID)
107 PVRSRV_PER_PROCESS_DATA *psPerProc;
109 PVR_ASSERT(psHashTab != IMG_NULL);
112 psPerProc = (PVRSRV_PER_PROCESS_DATA *)HASH_Retrieve(psHashTab, (IMG_UINTPTR_T)ui32PID);
117 PVRSRV_ERROR PVRSRVPerProcessDataConnect(IMG_UINT32 ui32PID)
119 PVRSRV_PER_PROCESS_DATA *psPerProc;
120 IMG_HANDLE hBlockAlloc;
121 PVRSRV_ERROR eError = PVRSRV_OK;
123 PVR_ASSERT(psHashTab != IMG_NULL);
126 psPerProc = (PVRSRV_PER_PROCESS_DATA *)HASH_Retrieve(psHashTab, (IMG_UINTPTR_T)ui32PID);
128 if (psPerProc == IMG_NULL)
131 eError = OSAllocMem(PVRSRV_OS_NON_PAGEABLE_HEAP,
133 (IMG_PVOID *)&psPerProc,
136 if (eError != PVRSRV_OK)
138 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't allocate per-process data (%d)", eError));
141 OSMemSet(psPerProc, 0, sizeof(*psPerProc));
142 psPerProc->hBlockAlloc = hBlockAlloc;
144 if (!HASH_Insert(psHashTab, (IMG_UINTPTR_T)ui32PID, (IMG_UINTPTR_T)psPerProc))
146 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't insert per-process data into hash table"));
147 eError = PVRSRV_ERROR_GENERIC;
151 psPerProc->ui32PID = ui32PID;
152 psPerProc->ui32RefCount = 0;
155 eError = OSPerProcessPrivateDataInit(&psPerProc->hOsPrivateData);
156 if (eError != PVRSRV_OK)
158 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: OSPerProcessPrivateDataInit failed (%d)", eError));
163 eError = PVRSRVAllocHandle(KERNEL_HANDLE_BASE,
164 &psPerProc->hPerProcData,
166 PVRSRV_HANDLE_TYPE_PERPROC_DATA,
167 PVRSRV_HANDLE_ALLOC_FLAG_NONE);
168 if (eError != PVRSRV_OK)
170 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't allocate handle for per-process data (%d)", eError));
175 eError = PVRSRVAllocHandleBase(&psPerProc->psHandleBase);
176 if (eError != PVRSRV_OK)
178 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't allocate handle base for process (%d)", eError));
183 eError = OSPerProcessSetHandleOptions(psPerProc->psHandleBase);
184 if (eError != PVRSRV_OK)
186 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't set handle options (%d)", eError));
191 eError = PVRSRVResManConnect(psPerProc, &psPerProc->hResManContext);
192 if (eError != PVRSRV_OK)
194 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataConnect: Couldn't register with the resource manager"));
199 psPerProc->ui32RefCount++;
200 PVR_DPF((PVR_DBG_MESSAGE,
201 "PVRSRVPerProcessDataConnect: Process 0x%x has ref-count %d",
202 ui32PID, psPerProc->ui32RefCount));
207 (IMG_VOID)FreePerProcessData(psPerProc);
212 IMG_VOID PVRSRVPerProcessDataDisconnect(IMG_UINT32 ui32PID)
215 PVRSRV_PER_PROCESS_DATA *psPerProc;
217 PVR_ASSERT(psHashTab != IMG_NULL);
219 psPerProc = (PVRSRV_PER_PROCESS_DATA *)HASH_Retrieve(psHashTab, (IMG_UINTPTR_T)ui32PID);
220 if (psPerProc == IMG_NULL)
222 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataDealloc: Couldn't locate per-process data for PID %u", ui32PID));
226 psPerProc->ui32RefCount--;
227 if (psPerProc->ui32RefCount == 0)
229 PVR_DPF((PVR_DBG_MESSAGE, "PVRSRVPerProcessDataDisconnect: "
230 "Last close from process 0x%x received", ui32PID));
233 PVRSRVResManDisconnect(psPerProc->hResManContext, IMG_FALSE);
236 eError = FreePerProcessData(psPerProc);
237 if (eError != PVRSRV_OK)
239 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataDisconnect: Error freeing per-process data"));
244 eError = PVRSRVPurgeHandles(KERNEL_HANDLE_BASE);
245 if (eError != PVRSRV_OK)
247 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataDisconnect: Purge of global handle pool failed (%d)", eError));
252 PVRSRV_ERROR PVRSRVPerProcessDataInit(IMG_VOID)
254 PVR_ASSERT(psHashTab == IMG_NULL);
257 psHashTab = HASH_Create(HASH_TAB_INIT_SIZE);
258 if (psHashTab == IMG_NULL)
260 PVR_DPF((PVR_DBG_ERROR, "PVRSRVPerProcessDataInit: Couldn't create per-process data hash table"));
261 return PVRSRV_ERROR_GENERIC;
267 PVRSRV_ERROR PVRSRVPerProcessDataDeInit(IMG_VOID)
270 if (psHashTab != IMG_NULL)
273 HASH_Delete(psHashTab);
274 psHashTab = IMG_NULL;