staging: emgd: experimental build 2667
[profile/ivi/intel-emgd-kmod.git] / pvr / services4 / srvkm / common / mem.c
1 /**********************************************************************
2  Copyright (c) Imagination Technologies Ltd.
3
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:
10
11  The above copyright notice and this permission notice shall be included in
12  all copies or substantial portions of the Software.
13
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
20  THE SOFTWARE.
21  ******************************************************************************/
22
23 #include "services_headers.h"
24 #include "pvr_bridge_km.h"
25
26
27 static PVRSRV_ERROR
28 FreeSharedSysMemCallBack(IMG_PVOID      pvParam,
29                                                  IMG_UINT32     ui32Param)
30 {
31         PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo = pvParam;
32
33         PVR_UNREFERENCED_PARAMETER(ui32Param);
34
35         OSFreePages(psKernelMemInfo->ui32Flags,
36                                 psKernelMemInfo->ui32AllocSize,
37                                 psKernelMemInfo->pvLinAddrKM,
38                                 psKernelMemInfo->sMemBlk.hOSMemHandle);
39
40         OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
41                           sizeof(PVRSRV_KERNEL_MEM_INFO),
42                           psKernelMemInfo,
43                           IMG_NULL);
44
45
46         return PVRSRV_OK;
47 }
48
49
50 IMG_EXPORT PVRSRV_ERROR
51 PVRSRVAllocSharedSysMemoryKM(PVRSRV_PER_PROCESS_DATA    *psPerProc,
52                                                          IMG_UINT32                                     ui32Flags,
53                                                          IMG_SIZE_T                             ui32Size,
54                                                          PVRSRV_KERNEL_MEM_INFO         **ppsKernelMemInfo)
55 {
56         PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
57
58         if(OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
59                                   sizeof(PVRSRV_KERNEL_MEM_INFO),
60                                   (IMG_VOID **)&psKernelMemInfo, IMG_NULL,
61                                   "Kernel Memory Info") != PVRSRV_OK)
62         {
63                 PVR_DPF((PVR_DBG_ERROR,"PVRSRVAllocSharedSysMemoryKM: Failed to alloc memory for meminfo"));
64                 return PVRSRV_ERROR_OUT_OF_MEMORY;
65         }
66
67         OSMemSet(psKernelMemInfo, 0, sizeof(*psKernelMemInfo));
68
69         ui32Flags &= ~PVRSRV_HAP_MAPTYPE_MASK;
70         ui32Flags |= PVRSRV_HAP_MULTI_PROCESS;
71         psKernelMemInfo->ui32Flags = ui32Flags;
72         psKernelMemInfo->ui32AllocSize = ui32Size;
73
74         if(OSAllocPages(psKernelMemInfo->ui32Flags,
75                                         psKernelMemInfo->ui32AllocSize,
76                                         HOST_PAGESIZE(),
77                                         &psKernelMemInfo->pvLinAddrKM,
78                                         &psKernelMemInfo->sMemBlk.hOSMemHandle)
79                 != PVRSRV_OK)
80         {
81                 PVR_DPF((PVR_DBG_ERROR, "PVRSRVAllocSharedSysMemoryKM: Failed to alloc memory for block"));
82                 OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
83                                   sizeof(PVRSRV_KERNEL_MEM_INFO),
84                                   psKernelMemInfo,
85                                   0);
86                 return PVRSRV_ERROR_OUT_OF_MEMORY;
87         }
88
89
90         psKernelMemInfo->sMemBlk.hResItem =
91                                 ResManRegisterRes(psPerProc->hResManContext,
92                                                                   RESMAN_TYPE_SHARED_MEM_INFO,
93                                                                   psKernelMemInfo,
94                                                                   0,
95                                                                   FreeSharedSysMemCallBack);
96
97         *ppsKernelMemInfo = psKernelMemInfo;
98
99         return PVRSRV_OK;
100 }
101
102
103 IMG_EXPORT PVRSRV_ERROR
104 PVRSRVFreeSharedSysMemoryKM(PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo)
105 {
106         PVRSRV_ERROR eError;
107
108         if(psKernelMemInfo->sMemBlk.hResItem)
109         {
110                 eError = ResManFreeResByPtr(psKernelMemInfo->sMemBlk.hResItem);
111         }
112         else
113         {
114                 eError = FreeSharedSysMemCallBack(psKernelMemInfo, 0);
115         }
116
117         return eError;
118 }
119
120
121 IMG_EXPORT PVRSRV_ERROR
122 PVRSRVDissociateMemFromResmanKM(PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo)
123 {
124         PVRSRV_ERROR eError = PVRSRV_OK;
125
126         if(!psKernelMemInfo)
127         {
128                 return PVRSRV_ERROR_INVALID_PARAMS;
129         }
130
131         if(psKernelMemInfo->sMemBlk.hResItem)
132         {
133                 eError = ResManDissociateRes(psKernelMemInfo->sMemBlk.hResItem, IMG_NULL);
134
135                 if (eError != PVRSRV_OK)
136                 {
137                         PVR_DPF((PVR_DBG_ERROR,"PVRSRVDissociateMemFromResmanKM: ResManDissociateRes failed"));
138                         PVR_DBG_BREAK;
139                         return eError;
140                 }
141
142                 psKernelMemInfo->sMemBlk.hResItem = IMG_NULL;
143         }
144
145         return eError;
146 }
147