1 /*************************************************************************/ /*!
3 @Title Resource Handle Manager - IDR Back-end
4 @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description Provide IDR based resource handle management back-end
6 @License Dual MIT/GPLv2
8 The contents of this file are subject to the MIT license as set out below.
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
20 Alternatively, the contents of this file may be used under the terms of
21 the GNU General Public License Version 2 ("GPL") in which case the provisions
22 of GPL are applicable instead of those above.
24 If you wish to allow use of your version of this file only under the terms of
25 GPL, and not to allow others to use your version of this file under the terms
26 of the MIT license, indicate your decision by deleting the provisions above
27 and replace them with the notice and other provisions required by GPL as set
28 out in the file called "GPL-COPYING" included in this distribution. If you do
29 not delete the provisions above, a recipient may use your version of this file
30 under the terms of either the MIT license or GPL.
32 This License is also included in this distribution in the file called
35 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
36 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
37 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
38 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
39 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
40 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
41 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 */ /***************************************************************************/
44 #include <linux/version.h>
45 #include <linux/kernel.h>
46 #include <linux/err.h>
47 #include <linux/gfp.h>
48 #include <linux/idr.h>
50 #include "handle_impl.h"
53 #include "pvr_debug.h"
55 #define ID_VALUE_MIN 1
56 #define ID_VALUE_MAX INT_MAX
58 #define ID_TO_HANDLE(i) ((IMG_HANDLE)(uintptr_t)(i))
59 #define HANDLE_TO_ID(h) ((IMG_INT)(uintptr_t)(h))
61 struct _HANDLE_IMPL_BASE_
65 IMG_UINT32 ui32MaxHandleValue;
67 IMG_UINT32 ui32TotalHandCount;
70 typedef struct _HANDLE_ITER_DATA_WRAPPER_
72 PFN_HANDLE_ITER pfnHandleIter;
73 void *pvHandleIterData;
74 } HANDLE_ITER_DATA_WRAPPER;
77 static int HandleIterFuncWrapper(int id, void *data, void *iter_data)
79 HANDLE_ITER_DATA_WRAPPER *psIterData = (HANDLE_ITER_DATA_WRAPPER *)iter_data;
81 PVR_UNREFERENCED_PARAMETER(data);
83 return (int)psIterData->pfnHandleIter(ID_TO_HANDLE(id), psIterData->pvHandleIterData);
87 ******************************************************************************
89 @Function AcquireHandle
91 @Description Acquire a new handle
93 @Input psBase - Pointer to handle base structure
94 phHandle - Points to a handle pointer
95 pvData - Pointer to resource to be associated with the handle
97 @Output phHandle - Points to a handle pointer
99 @Return Error code or PVRSRV_OK
101 ******************************************************************************/
102 static PVRSRV_ERROR AcquireHandle(HANDLE_IMPL_BASE *psBase,
103 IMG_HANDLE *phHandle,
109 PVR_ASSERT(psBase != NULL);
110 PVR_ASSERT(phHandle != NULL);
111 PVR_ASSERT(pvData != NULL);
113 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0))
114 idr_preload(GFP_KERNEL);
115 id = idr_alloc(&psBase->sIdr, pvData, ID_VALUE_MIN, psBase->ui32MaxHandleValue + 1, 0);
122 if (idr_pre_get(&psBase->sIdr, GFP_KERNEL) == 0)
124 return PVRSRV_ERROR_OUT_OF_MEMORY;
127 result = idr_get_new_above(&psBase->sIdr, pvData, ID_VALUE_MIN, &id);
128 } while (result == -EAGAIN);
130 if ((IMG_UINT32)id > psBase->ui32MaxHandleValue)
132 idr_remove(&psBase->sIdr, id);
139 if (result == -ENOSPC)
141 PVR_DPF((PVR_DBG_ERROR, "%s: Limit of %u handles reached",
142 __FUNCTION__, psBase->ui32MaxHandleValue));
144 return PVRSRV_ERROR_UNABLE_TO_ADD_HANDLE;
147 return PVRSRV_ERROR_OUT_OF_MEMORY;
150 psBase->ui32TotalHandCount++;
152 *phHandle = ID_TO_HANDLE(id);
158 ******************************************************************************
160 @Function ReleaseHandle
162 @Description Release a handle that is no longer needed.
164 @Input psBase - Pointer to handle base structure
165 hHandle - Handle to release
166 ppvData - Points to a void data pointer
168 @Output ppvData - Points to a void data pointer
170 @Return PVRSRV_OK or PVRSRV_ERROR
172 ******************************************************************************/
173 static PVRSRV_ERROR ReleaseHandle(HANDLE_IMPL_BASE *psBase,
177 int id = HANDLE_TO_ID(hHandle);
182 /* Get the data associated with the handle. If we get back NULL then
183 it's an invalid handle */
185 pvData = idr_find(&psBase->sIdr, id);
188 idr_remove(&psBase->sIdr, id);
189 psBase->ui32TotalHandCount--;
194 PVR_DPF((PVR_DBG_ERROR, "%s: Handle out of range (%u > %u)",
195 __FUNCTION__, id, psBase->ui32TotalHandCount));
196 return PVRSRV_ERROR_HANDLE_INDEX_OUT_OF_RANGE;
208 ******************************************************************************
210 @Function GetHandleData
212 @Description Get the data associated with the given handle
214 @Input psBase - Pointer to handle base structure
215 hHandle - Handle from which data should be retrieved
216 ppvData - Points to a void data pointer
218 @Output ppvData - Points to a void data pointer
220 @Return Error code or PVRSRV_OK
222 ******************************************************************************/
223 static PVRSRV_ERROR GetHandleData(HANDLE_IMPL_BASE *psBase,
227 int id = HANDLE_TO_ID(hHandle);
233 pvData = idr_find(&psBase->sIdr, id);
242 return PVRSRV_ERROR_HANDLE_INDEX_OUT_OF_RANGE;
247 ******************************************************************************
249 @Function SetHandleData
251 @Description Set the data associated with the given handle
253 @Input psBase - Pointer to handle base structure
254 hHandle - Handle for which data should be changed
255 pvData - Pointer to new data to be associated with the handle
257 @Return Error code or PVRSRV_OK
259 ******************************************************************************/
260 static PVRSRV_ERROR SetHandleData(HANDLE_IMPL_BASE *psBase,
264 int id = HANDLE_TO_ID(hHandle);
269 pvOldData = idr_replace(&psBase->sIdr, pvData, id);
270 if (IS_ERR(pvOldData))
272 if (PTR_ERR(pvOldData) == -ENOENT)
274 return PVRSRV_ERROR_HANDLE_NOT_ALLOCATED;
278 return PVRSRV_ERROR_HANDLE_INDEX_OUT_OF_RANGE;
285 static PVRSRV_ERROR IterateOverHandles(HANDLE_IMPL_BASE *psBase, PFN_HANDLE_ITER pfnHandleIter, void *pvHandleIterData)
287 HANDLE_ITER_DATA_WRAPPER sIterData;
290 PVR_ASSERT(pfnHandleIter);
292 sIterData.pfnHandleIter = pfnHandleIter;
293 sIterData.pvHandleIterData = pvHandleIterData;
295 return (PVRSRV_ERROR)idr_for_each(&psBase->sIdr, HandleIterFuncWrapper, &sIterData);
299 ******************************************************************************
301 @Function EnableHandlePurging
303 @Description Enable purging for a given handle base
305 @Input psBase - pointer to handle base structure
307 @Return Error code or PVRSRV_OK
309 ******************************************************************************/
310 static PVRSRV_ERROR EnableHandlePurging(HANDLE_IMPL_BASE *psBase)
312 PVR_UNREFERENCED_PARAMETER(psBase);
319 ******************************************************************************
321 @Function PurgeHandles
323 @Description Purge handles for a given handle base
325 @Input psBase - Pointer to handle base structure
327 @Return Error code or PVRSRV_OK
329 ******************************************************************************/
330 static PVRSRV_ERROR PurgeHandles(HANDLE_IMPL_BASE *psBase)
332 PVR_UNREFERENCED_PARAMETER(psBase);
339 ******************************************************************************
341 @Function CreateHandleBase
343 @Description Create a handle base structure
345 @Input ppsBase - pointer to handle base structure pointer
347 @Output ppsBase - points to handle base structure pointer
349 @Return Error code or PVRSRV_OK
351 ******************************************************************************/
352 static PVRSRV_ERROR CreateHandleBase(HANDLE_IMPL_BASE **ppsBase)
354 HANDLE_IMPL_BASE *psBase;
358 psBase = OSAllocZMem(sizeof(*psBase));
361 PVR_DPF((PVR_DBG_ERROR, "%s: Couldn't allocate generic handle base", __FUNCTION__));
363 return PVRSRV_ERROR_OUT_OF_MEMORY;
366 idr_init(&psBase->sIdr);
368 psBase->ui32MaxHandleValue = ID_VALUE_MAX;
369 psBase->ui32TotalHandCount = 0;
377 ******************************************************************************
379 @Function DestroyHandleBase
381 @Description Destroy a handle base structure
383 @Input psBase - pointer to handle base structure
385 @Return Error code or PVRSRV_OK
387 ******************************************************************************/
388 static PVRSRV_ERROR DestroyHandleBase(HANDLE_IMPL_BASE *psBase)
392 #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0))
393 idr_remove_all(&psBase->sIdr);
396 /* Finally destroy the idr */
397 idr_destroy(&psBase->sIdr);
405 static const HANDLE_IMPL_FUNCTAB g_sHandleFuncTab =
407 .pfnAcquireHandle = AcquireHandle,
408 .pfnReleaseHandle = ReleaseHandle,
409 .pfnGetHandleData = GetHandleData,
410 .pfnSetHandleData = SetHandleData,
411 .pfnIterateOverHandles = IterateOverHandles,
412 .pfnEnableHandlePurging = EnableHandlePurging,
413 .pfnPurgeHandles = PurgeHandles,
414 .pfnCreateHandleBase = CreateHandleBase,
415 .pfnDestroyHandleBase = DestroyHandleBase
418 PVRSRV_ERROR PVRSRVHandleGetFuncTable(HANDLE_IMPL_FUNCTAB const **ppsFuncs)
420 static IMG_BOOL bAcquired = IMG_FALSE;
424 PVR_DPF((PVR_DBG_ERROR, "%s: Function table already acquired",
426 return PVRSRV_ERROR_RESOURCE_UNAVAILABLE;
429 if (ppsFuncs == NULL)
431 return PVRSRV_ERROR_INVALID_PARAMS;
434 *ppsFuncs = &g_sHandleFuncTab;
436 bAcquired = IMG_TRUE;