gpu: pvr: fix memory context refcount problem leading to leaked handle
[sgx.git] / pvr / buffer_manager.h
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 #ifndef _BUFFER_MANAGER_H_
28 #define _BUFFER_MANAGER_H_
29
30 #include "img_types.h"
31 #include "ra.h"
32 #include "perproc.h"
33
34
35 struct BM_HEAP;
36
37 struct BM_MAPPING {
38         enum {
39                 hm_wrapped = 1,
40                 hm_wrapped_scatter,
41                 hm_wrapped_virtaddr,
42                 hm_wrapped_scatter_virtaddr,
43                 hm_env,
44                 hm_contiguous
45         } eCpuMemoryOrigin;
46
47         struct BM_HEAP *pBMHeap;
48         struct RA_ARENA *pArena;
49
50         void *CpuVAddr;
51         struct IMG_CPU_PHYADDR CpuPAddr;
52         struct IMG_DEV_VIRTADDR DevVAddr;
53         struct IMG_SYS_PHYADDR *psSysAddr;
54         size_t uSize;
55         void *hOSMemHandle;
56         u32 ui32Flags;
57 };
58
59 struct BM_BUF {
60         void **CpuVAddr;
61         void *hOSMemHandle;
62         struct IMG_CPU_PHYADDR CpuPAddr;
63         struct IMG_DEV_VIRTADDR DevVAddr;
64
65         struct BM_MAPPING *pMapping;
66         u32 ui32RefCount;
67 };
68
69 struct BM_HEAP {
70         u32 ui32Attribs;
71         struct BM_CONTEXT *pBMContext;
72         struct RA_ARENA *pImportArena;
73         struct RA_ARENA *pLocalDevMemArena;
74         struct RA_ARENA *pVMArena;
75         struct DEV_ARENA_DESCRIPTOR sDevArena;
76         struct MMU_HEAP *pMMUHeap;
77
78         struct BM_HEAP *psNext;
79 };
80
81 struct BM_CONTEXT {
82         struct MMU_CONTEXT *psMMUContext;
83         struct BM_HEAP *psBMHeap;
84         struct BM_HEAP *psBMSharedHeap;
85         struct PVRSRV_DEVICE_NODE *psDeviceNode;
86         struct HASH_TABLE *pBufferHash;
87         void *hResItem;
88         u32 ui32RefCount;
89         int open_count;
90         struct BM_CONTEXT *psNext;
91 };
92
93 #define BP_POOL_MASK     0x7
94
95 #define BP_CONTIGUOUS                   (1 << 3)
96 #define BP_PARAMBUFFER                  (1 << 4)
97
98 #define BM_MAX_DEVMEM_ARENAS  2
99
100 void *BM_CreateContext(struct PVRSRV_DEVICE_NODE *psDeviceNode,
101                 struct IMG_DEV_PHYADDR *psPDDevPAddr,
102                 struct PVRSRV_PER_PROCESS_DATA *psPerProc,
103                 IMG_BOOL *pbCreated);
104
105 void BM_DestroyContext(void *hBMContext);
106
107 static inline void pvr_get_ctx(struct BM_CONTEXT *ctx)
108 {
109         WARN_ON(!ctx->ui32RefCount);
110         ctx->ui32RefCount++;
111 }
112
113 static inline bool pvr_put_ctx(struct BM_CONTEXT *ctx)
114 {
115         BUG_ON(!ctx->ui32RefCount);
116         ctx->ui32RefCount--;
117         if (!ctx->ui32RefCount) {
118                 BM_DestroyContext(ctx);
119
120                 return true;
121         }
122
123         return false;
124 }
125
126 void *BM_CreateHeap(void *hBMContext,
127                     struct DEVICE_MEMORY_HEAP_INFO *psDevMemHeapInfo);
128 void BM_DestroyHeap(void *hDevMemHeap);
129 IMG_BOOL BM_Reinitialise(struct PVRSRV_DEVICE_NODE *psDeviceNode);
130
131 IMG_BOOL BM_Alloc(void *hDevMemHeap,
132          struct IMG_DEV_VIRTADDR *psDevVAddr,
133          size_t uSize, u32 *pui32Flags, u32 uDevVAddrAlignment, void **phBuf);
134
135 IMG_BOOL BM_Wrap(void *hDevMemHeap,
136         u32 ui32Size,
137         u32 ui32Offset,
138         IMG_BOOL bPhysContig,
139         struct IMG_SYS_PHYADDR *psSysAddr,
140         void *pvCPUVAddr, u32 *pui32Flags, void **phBuf);
141
142 void BM_Free(void *hBuf, u32 ui32Flags);
143 void *BM_HandleToCpuVaddr(void *hBuf);
144 struct IMG_DEV_VIRTADDR BM_HandleToDevVaddr(void *hBuf);
145
146 struct IMG_SYS_PHYADDR BM_HandleToSysPaddr(void *hBuf);
147
148 void *BM_HandleToOSMemHandle(void *hBuf);
149
150 IMG_BOOL BM_ContiguousStatistics(u32 uFlags, u32 *pTotalBytes,
151                 u32 *pAvailableBytes);
152
153 enum PVRSRV_ERROR BM_GetPhysPageAddr(struct PVRSRV_KERNEL_MEM_INFO *psMemInfo,
154                 struct IMG_DEV_VIRTADDR sDevVPageAddr,
155                 struct IMG_DEV_PHYADDR *psDevPAddr);
156
157 enum PVRSRV_ERROR BM_GetHeapInfo(void *hDevMemHeap,
158                             struct PVRSRV_HEAP_INFO *psHeapInfo);
159
160 struct MMU_CONTEXT *BM_GetMMUContext(void *hDevMemHeap);
161
162 struct MMU_CONTEXT *BM_GetMMUContextFromMemContext(void *hDevMemContext);
163
164 void *BM_GetMMUHeap(void *hDevMemHeap);
165
166 struct PVRSRV_DEVICE_NODE *BM_GetDeviceNode(void *hDevMemContext);
167
168 void *BM_GetMappingHandle(struct PVRSRV_KERNEL_MEM_INFO *psMemInfo);
169
170 struct BM_CONTEXT *bm_find_context(struct BM_CONTEXT *head_context,
171                                     u32 page_dir);
172 #endif