gpu: pvr: V2: Find and fix all incorrect sync counter completion checks
[sgx.git] / pvr / mem.c
1 /**********************************************************************
2  *
3  * Copyright(c) 2008 Imagination Technologies Ltd. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful but, except
10  * as otherwise stated in writing, without any warranty; without even the
11  * implied warranty of merchantability or fitness for a particular purpose.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * Imagination Technologies Ltd. <gpl-support@imgtec.com>
23  * Home Park Estate, Kings Langley, Herts, WD4 8LZ, UK
24  *
25  ******************************************************************************/
26
27 #include "services_headers.h"
28 #include "sgxapi_km.h"
29 #include "pvr_bridge_km.h"
30
31 static enum PVRSRV_ERROR FreeSharedSysMemCallBack(void *pvParam, u32 ui32Param)
32 {
33         struct PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo = pvParam;
34
35         PVR_UNREFERENCED_PARAMETER(ui32Param);
36
37         OSFreePages(psKernelMemInfo->ui32Flags,
38                     psKernelMemInfo->ui32AllocSize,
39                     psKernelMemInfo->pvLinAddrKM,
40                     psKernelMemInfo->sMemBlk.hOSMemHandle);
41
42         OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
43                   sizeof(struct PVRSRV_KERNEL_MEM_INFO), psKernelMemInfo, NULL);
44
45         return PVRSRV_OK;
46 }
47
48 enum PVRSRV_ERROR PVRSRVAllocSharedSysMemoryKM(
49                         struct PVRSRV_PER_PROCESS_DATA *psPerProc,
50                         u32 ui32Flags, u32 ui32Size,
51                         struct PVRSRV_KERNEL_MEM_INFO **ppsKernelMemInfo)
52 {
53         struct PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo;
54
55         if (OSAllocMem(PVRSRV_OS_PAGEABLE_HEAP,
56                        sizeof(struct PVRSRV_KERNEL_MEM_INFO),
57                        (void **) &psKernelMemInfo, NULL) != PVRSRV_OK) {
58                 PVR_DPF(PVR_DBG_ERROR, "PVRSRVAllocSharedSysMemoryKM: "
59                                         "Failed to alloc memory for meminfo");
60                 return PVRSRV_ERROR_OUT_OF_MEMORY;
61         }
62
63         OSMemSet(psKernelMemInfo, 0, sizeof(*psKernelMemInfo));
64
65         ui32Flags &= ~PVRSRV_HAP_MAPTYPE_MASK;
66         ui32Flags |= PVRSRV_HAP_MULTI_PROCESS;
67         psKernelMemInfo->ui32Flags = ui32Flags;
68         psKernelMemInfo->ui32AllocSize = ui32Size;
69
70         if (OSAllocPages(psKernelMemInfo->ui32Flags,
71                          psKernelMemInfo->ui32AllocSize, HOST_PAGESIZE(),
72                          &psKernelMemInfo->pvLinAddrKM,
73                          &psKernelMemInfo->sMemBlk.hOSMemHandle) != PVRSRV_OK) {
74                 PVR_DPF(PVR_DBG_ERROR, "PVRSRVAllocSharedSysMemoryKM: "
75                                         "Failed to alloc memory for block");
76                 OSFreeMem(PVRSRV_OS_PAGEABLE_HEAP,
77                           sizeof(struct PVRSRV_KERNEL_MEM_INFO),
78                           psKernelMemInfo, NULL);
79                 return PVRSRV_ERROR_OUT_OF_MEMORY;
80         }
81
82         psKernelMemInfo->sMemBlk.hResItem = ResManRegisterRes(
83                                         psPerProc->hResManContext,
84                                         RESMAN_TYPE_SHARED_MEM_INFO,
85                                         psKernelMemInfo, 0,
86                                         FreeSharedSysMemCallBack);
87
88         *ppsKernelMemInfo = psKernelMemInfo;
89
90         return PVRSRV_OK;
91 }
92
93 enum PVRSRV_ERROR PVRSRVFreeSharedSysMemoryKM(
94                         struct PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo)
95 {
96         enum PVRSRV_ERROR eError = PVRSRV_OK;
97
98         if (psKernelMemInfo->sMemBlk.hResItem)
99                 ResManFreeResByPtr(psKernelMemInfo->sMemBlk.hResItem);
100         else
101                 eError = FreeSharedSysMemCallBack(psKernelMemInfo, 0);
102
103         return eError;
104 }
105
106 enum PVRSRV_ERROR PVRSRVDissociateMemFromResmanKM(
107                                 struct PVRSRV_KERNEL_MEM_INFO *psKernelMemInfo)
108 {
109         enum PVRSRV_ERROR eError = PVRSRV_OK;
110
111         if (!psKernelMemInfo)
112                 return PVRSRV_ERROR_INVALID_PARAMS;
113
114         if (psKernelMemInfo->sMemBlk.hResItem) {
115                 eError = ResManDissociateRes(psKernelMemInfo->sMemBlk.hResItem,
116                                              NULL);
117
118                 if (eError != PVRSRV_OK) {
119                         PVR_DPF(PVR_DBG_ERROR,
120                                         "PVRSRVDissociateMemFromResmanKM: "
121                                         "ResManDissociateRes failed");
122                         PVR_DBG_BREAK;
123                         return eError;
124                 }
125
126                 psKernelMemInfo->sMemBlk.hResItem = NULL;
127         }
128
129         return eError;
130 }