RK3368 GPU version Rogue M 1.28
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / rogue_m / services / server / common / physheap.c
1 /*************************************************************************/ /*!
2 @File           physheap.c
3 @Title          Physical heap management
4 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
5 @Description    Management functions for the physical heap(s). A heap contains
6                 all the information required by services when using memory from
7                 that heap (such as CPU <> Device physical address translation).
8                 A system must register one heap but can have more then one which
9                 is why a heap must register with a (system) unique ID.
10 @License        Dual MIT/GPLv2
11
12 The contents of this file are subject to the MIT license as set out below.
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in
22 all copies or substantial portions of the Software.
23
24 Alternatively, the contents of this file may be used under the terms of
25 the GNU General Public License Version 2 ("GPL") in which case the provisions
26 of GPL are applicable instead of those above.
27
28 If you wish to allow use of your version of this file only under the terms of
29 GPL, and not to allow others to use your version of this file under the terms
30 of the MIT license, indicate your decision by deleting the provisions above
31 and replace them with the notice and other provisions required by GPL as set
32 out in the file called "GPL-COPYING" included in this distribution. If you do
33 not delete the provisions above, a recipient may use your version of this file
34 under the terms of either the MIT license or GPL.
35
36 This License is also included in this distribution in the file called
37 "MIT-COPYING".
38
39 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
40 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
41 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
42 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
43 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
44 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
45 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 */ /***************************************************************************/
47 #include "img_types.h"
48 #include "physheap.h"
49 #include "allocmem.h"
50 #include "pvr_debug.h"
51 #include "osfunc.h"
52
53 struct _PHYS_HEAP_
54 {
55         /*! ID of this physcial memory heap */
56         IMG_UINT32                                      ui32PhysHeapID;
57         /*! The type of this heap */
58         PHYS_HEAP_TYPE                  eType;
59
60         /*! Start address of the physcial memory heap (LMA only) */
61         IMG_CPU_PHYADDR                         sStartAddr;
62         /*! Size of the physcial memory heap (LMA only) */
63         IMG_UINT64                                      uiSize;
64
65         /*! PDump name of this physcial memory heap */
66         IMG_CHAR                                        *pszPDumpMemspaceName;
67         /*! Private data for the translate routines */
68         IMG_HANDLE                                      hPrivData;
69         /*! Function callbacks */
70         PHYS_HEAP_FUNCTIONS                     *psMemFuncs;
71
72
73         /*! Refcount */
74         IMG_UINT32                                      ui32RefCount;
75         /*! Pointer to next physcial heap */
76         struct _PHYS_HEAP_              *psNext;
77 };
78
79 PHYS_HEAP *g_psPhysHeapList;
80
81 #if defined(REFCOUNT_DEBUG)
82 #define PHYSHEAP_REFCOUNT_PRINT(fmt, ...)       \
83         PVRSRVDebugPrintf(PVR_DBG_WARNING,      \
84                           __FILE__,             \
85                           __LINE__,             \
86                           fmt,                  \
87                           __VA_ARGS__)
88 #else
89 #define PHYSHEAP_REFCOUNT_PRINT(fmt, ...)
90 #endif
91
92
93 PVRSRV_ERROR PhysHeapRegister(PHYS_HEAP_CONFIG *psConfig,
94                                                           PHYS_HEAP **ppsPhysHeap)
95 {
96         PHYS_HEAP *psNew;
97         PHYS_HEAP *psTmp;
98
99         PVR_DPF_ENTERED;
100
101         if (psConfig->eType == PHYS_HEAP_TYPE_UNKNOWN)
102         {
103                 return PVRSRV_ERROR_INVALID_PARAMS;
104         }
105
106         /* Check this heap ID isn't already in use */
107         psTmp = g_psPhysHeapList;
108         while (psTmp)
109         {
110                 if (psTmp->ui32PhysHeapID == psConfig->ui32PhysHeapID)
111                 {
112                         return PVRSRV_ERROR_PHYSHEAP_ID_IN_USE;
113                 }
114                 psTmp = psTmp->psNext;
115         }
116
117         psNew = OSAllocMem(sizeof(PHYS_HEAP));
118         if (psNew == IMG_NULL)
119         {
120                 return PVRSRV_ERROR_OUT_OF_MEMORY;
121         }
122
123         psNew->ui32PhysHeapID = psConfig->ui32PhysHeapID;
124         psNew->eType = psConfig->eType;
125         psNew->sStartAddr = psConfig->sStartAddr;
126         psNew->uiSize = psConfig->uiSize;
127         psNew->psMemFuncs = psConfig->psMemFuncs;
128         psNew->hPrivData = psConfig->hPrivData;
129         psNew->ui32RefCount = 0;
130         psNew->pszPDumpMemspaceName = psConfig->pszPDumpMemspaceName;
131
132         psNew->psNext = g_psPhysHeapList;
133         g_psPhysHeapList = psNew;
134
135         *ppsPhysHeap = psNew;
136
137         PVR_DPF_RETURN_RC1(PVRSRV_OK, *ppsPhysHeap);
138 }
139
140 IMG_VOID PhysHeapUnregister(PHYS_HEAP *psPhysHeap)
141 {
142         PVR_DPF_ENTERED1(psPhysHeap);
143
144         PVR_ASSERT(psPhysHeap->ui32RefCount == 0);
145
146         if (g_psPhysHeapList == psPhysHeap)
147         {
148                 g_psPhysHeapList = psPhysHeap->psNext;
149         }
150         else
151         {
152                 PHYS_HEAP *psTmp = g_psPhysHeapList;
153
154                 while(psTmp->psNext != psPhysHeap)
155                 {
156                         psTmp = psTmp->psNext;
157                 }
158                 psTmp->psNext = psPhysHeap->psNext;
159         }
160
161         OSFreeMem(psPhysHeap);
162
163         PVR_DPF_RETURN;
164 }
165
166 PVRSRV_ERROR PhysHeapAcquire(IMG_UINT32 ui32PhysHeapID,
167                                                          PHYS_HEAP **ppsPhysHeap)
168 {
169         PHYS_HEAP *psTmp = g_psPhysHeapList;
170         PVRSRV_ERROR eError = PVRSRV_OK;
171
172         PVR_DPF_ENTERED1(ui32PhysHeapID);
173
174         while (psTmp)
175         {
176                 if (psTmp->ui32PhysHeapID == ui32PhysHeapID)
177                 {
178                         break;
179                 }
180                 psTmp = psTmp->psNext;
181         }
182         
183         if (psTmp == IMG_NULL)
184         {
185                 eError = PVRSRV_ERROR_PHYSHEAP_ID_INVALID;
186         }
187         else
188         {
189                 psTmp->ui32RefCount++;
190                 PHYSHEAP_REFCOUNT_PRINT("%s: Heap %p, refcount = %d", __FUNCTION__, psTmp, psTmp->ui32RefCount);
191         }
192
193         *ppsPhysHeap = psTmp;
194         PVR_DPF_RETURN_RC1(eError, *ppsPhysHeap);
195 }
196
197 IMG_VOID PhysHeapRelease(PHYS_HEAP *psPhysHeap)
198 {
199         PVR_DPF_ENTERED1(psPhysHeap);
200
201         psPhysHeap->ui32RefCount--;
202         PHYSHEAP_REFCOUNT_PRINT("%s: Heap %p, refcount = %d", __FUNCTION__, psPhysHeap, psPhysHeap->ui32RefCount);
203
204         PVR_DPF_RETURN;
205 }
206
207 PHYS_HEAP_TYPE PhysHeapGetType(PHYS_HEAP *psPhysHeap)
208 {
209         return psPhysHeap->eType;
210 }
211
212 PVRSRV_ERROR PhysHeapGetAddress(PHYS_HEAP *psPhysHeap,
213                                                                 IMG_CPU_PHYADDR *psCpuPAddr)
214 {
215         if (psPhysHeap->eType == PHYS_HEAP_TYPE_LMA)
216         {
217                 *psCpuPAddr = psPhysHeap->sStartAddr;
218                 return PVRSRV_OK;
219         }
220
221         return PVRSRV_ERROR_INVALID_PARAMS;
222 }
223
224 PVRSRV_ERROR PhysHeapGetSize(PHYS_HEAP *psPhysHeap,
225                                                            IMG_UINT64 *puiSize)
226 {
227         if (psPhysHeap->eType == PHYS_HEAP_TYPE_LMA)
228         {
229                 *puiSize = psPhysHeap->uiSize;
230                 return PVRSRV_OK;
231         }
232
233         return PVRSRV_ERROR_INVALID_PARAMS;
234 }
235
236 IMG_VOID PhysHeapCpuPAddrToDevPAddr(PHYS_HEAP *psPhysHeap,
237                                                                         IMG_UINT32 ui32NumOfAddr,
238                                                                         IMG_DEV_PHYADDR *psDevPAddr,
239                                                                         IMG_CPU_PHYADDR *psCpuPAddr)
240 {
241         psPhysHeap->psMemFuncs->pfnCpuPAddrToDevPAddr(psPhysHeap->hPrivData,
242                                                                                                  ui32NumOfAddr,
243                                                                                                  psDevPAddr,
244                                                                                                  psCpuPAddr);
245 }
246
247 IMG_VOID PhysHeapDevPAddrToCpuPAddr(PHYS_HEAP *psPhysHeap,
248                                                                         IMG_UINT32 ui32NumOfAddr,
249                                                                         IMG_CPU_PHYADDR *psCpuPAddr,
250                                                                         IMG_DEV_PHYADDR *psDevPAddr)
251 {
252         psPhysHeap->psMemFuncs->pfnDevPAddrToCpuPAddr(psPhysHeap->hPrivData,
253                                                                                                  ui32NumOfAddr,
254                                                                                                  psCpuPAddr,
255                                                                                                  psDevPAddr);
256 }
257
258 IMG_CHAR *PhysHeapPDumpMemspaceName(PHYS_HEAP *psPhysHeap)
259 {
260         return psPhysHeap->pszPDumpMemspaceName;
261 }
262
263 PVRSRV_ERROR PhysHeapInit(IMG_VOID)
264 {
265         g_psPhysHeapList = IMG_NULL;
266
267         return PVRSRV_OK;
268 }
269
270 PVRSRV_ERROR PhysHeapDeinit(IMG_VOID)
271 {
272         PVR_ASSERT(g_psPhysHeapList == IMG_NULL);
273
274         return PVRSRV_OK;
275 }