hc
2023-05-26 a23f51ed7a39e452c1037343a84d7db1ca2c5bd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*************************************************************************/ /*!
@File           physmem.c
@Title          Physmem
@Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
@Description    Common entry point for creation of RAM backed PMR's
@License        Dual MIT/GPLv2
 
The contents of this file are subject to the MIT license as set out below.
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
Alternatively, the contents of this file may be used under the terms of
the GNU General Public License Version 2 ("GPL") in which case the provisions
of GPL are applicable instead of those above.
 
If you wish to allow use of your version of this file only under the terms of
GPL, and not to allow others to use your version of this file under the terms
of the MIT license, indicate your decision by deleting the provisions above
and replace them with the notice and other provisions required by GPL as set
out in the file called "GPL-COPYING" included in this distribution. If you do
not delete the provisions above, a recipient may use your version of this file
under the terms of either the MIT license or GPL.
 
This License is also included in this distribution in the file called
"MIT-COPYING".
 
EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ /***************************************************************************/
#include "img_types.h"
#include "pvrsrv_error.h"
#include "pvrsrv_memallocflags.h"
#include "device.h"
#include "physmem.h"
#include "pvrsrv.h"
#include "osfunc.h"
#include "pdump_physmem.h"
#include "pdump_km.h"
#include "rgx_heaps.h"
 
#if defined(DEBUG)
IMG_UINT32 gPMRAllocFail = 0;
#endif /* defined(DEBUG) */
 
PVRSRV_ERROR DevPhysMemAlloc(PVRSRV_DEVICE_NODE    *psDevNode,
                           IMG_UINT32 ui32MemSize,
                           const IMG_UINT8 u8Value,
                           IMG_BOOL bInitPage,
#if defined(PDUMP)
                           const IMG_CHAR *pszDevSpace,
                           const IMG_CHAR *pszSymbolicAddress,
                           IMG_HANDLE *phHandlePtr,
#endif
                           IMG_HANDLE hMemHandle,
                           IMG_DEV_PHYADDR *psDevPhysAddr)
{
   void    *pvCpuVAddr;
   PVRSRV_ERROR eError;
#if defined(PDUMP)
    IMG_CHAR szFilenameOut[PDUMP_PARAM_MAX_FILE_NAME];
    PDUMP_FILEOFFSET_T uiOffsetOut;
#endif
   PG_HANDLE *psMemHandle;
   IMG_UINT32 ui32PageSize;
 
   psMemHandle = hMemHandle;
   ui32PageSize = OSGetPageSize();
 
   /*Allocate the page */
   eError = psDevNode->pfnDevPxAlloc(psDevNode,
                                       TRUNCATE_64BITS_TO_SIZE_T(ui32MemSize),
                                       psMemHandle,
                                       psDevPhysAddr);
   if(PVRSRV_OK != eError)
   {
       PVR_DPF((PVR_DBG_ERROR,"Unable to allocate the pages"));
       return eError;
   }
 
#if defined(PDUMP)
   eError = PDumpMalloc(pszDevSpace,
                               pszSymbolicAddress,
                               ui32MemSize,
                               ui32PageSize,
                               IMG_FALSE,
                               0,
                               IMG_FALSE,
                               phHandlePtr);
   if(PVRSRV_OK != eError)
   {
       PDUMPCOMMENT("Allocating pages failed");
       *phHandlePtr = NULL;
   }
#endif
 
   if(bInitPage)
   {
       /*Map the page to the CPU VA space */
       eError = psDevNode->pfnDevPxMap(psDevNode,
                                       psMemHandle,
                                       ui32MemSize,
                                       psDevPhysAddr,
                                       &pvCpuVAddr);
       if(PVRSRV_OK != eError)
       {
           PVR_DPF((PVR_DBG_ERROR,"Unable to map the allocated page"));
           psDevNode->pfnDevPxFree(psDevNode, psMemHandle);
           return eError;
       }
 
       /*Fill the memory with given content */
       /*NOTE: Wrong for the LMA + ARM64 combination, but this is unlikely */
       OSCachedMemSet(pvCpuVAddr, u8Value, ui32MemSize);
 
       /*Map the page to the CPU VA space */
       eError = psDevNode->pfnDevPxClean(psDevNode,
                                         psMemHandle,
                                         0,
                                         ui32MemSize);
       if(PVRSRV_OK != eError)
       {
           PVR_DPF((PVR_DBG_ERROR,"Unable to clean the allocated page"));
           psDevNode->pfnDevPxUnMap(psDevNode, psMemHandle, pvCpuVAddr);
           psDevNode->pfnDevPxFree(psDevNode, psMemHandle);
           return eError;
       }
 
#if defined(PDUMP)
       /*P-Dumping of the page contents can be done in two ways
        * 1. Store the single byte init value to the .prm file
        *       and load the same value to the entire dummy page buffer
        *       This method requires lot of LDB's inserted into the out2.txt
        *
        * 2. Store the entire contents of the buffer to the .prm file
        *    and load them back.
        *    This only needs a single LDB instruction in the .prm file
        *    and chosen this method
        *    size of .prm file might go up but that's not huge at least
        *       for this allocation
        */
       /*Write the buffer contents to the prm file */
       eError = PDumpWriteBuffer(pvCpuVAddr,
                                   ui32MemSize,
                                   PDUMP_FLAGS_CONTINUOUS,
                                   szFilenameOut,
                                   sizeof(szFilenameOut),
                                   &uiOffsetOut);
       if(PVRSRV_OK == eError)
       {
           /* Load the buffer back to the allocated memory when playing the pdump */
           eError = PDumpPMRLDB(pszDevSpace,
                                       pszSymbolicAddress,
                                       0,
                                       ui32MemSize,
                                       szFilenameOut,
                                       uiOffsetOut,
                                       PDUMP_FLAGS_CONTINUOUS);
           if(PVRSRV_OK != eError)
           {
               PDUMP_ERROR(eError, "Failed to write LDB statement to script file");
               PVR_DPF((PVR_DBG_ERROR, "Failed to write LDB statement to script file, error %d", eError));
           }
 
       }
       else if (eError != PVRSRV_ERROR_PDUMP_NOT_ALLOWED)
       {
           PDUMP_ERROR(eError, "Failed to write device allocation to parameter file");
           PVR_DPF((PVR_DBG_ERROR, "Failed to write device allocation to parameter file, error %d", eError));
       }
       else
       {
           /* else Write to parameter file prevented under the flags and
            * current state of the driver so skip write to script and error IF.
            */
           eError = PVRSRV_OK;
       }
#endif
 
       /*UnMap the page */
       psDevNode->pfnDevPxUnMap(psDevNode,
                                       psMemHandle,
                                       pvCpuVAddr);
   }
 
   return PVRSRV_OK;
 
}
 
void DevPhysMemFree(PVRSRV_DEVICE_NODE *psDevNode,
#if defined(PDUMP)
                           IMG_HANDLE hPDUMPMemHandle,
#endif
                           IMG_HANDLE    hMemHandle)
{
   PG_HANDLE *psMemHandle;
 
   psMemHandle = hMemHandle;
   psDevNode->pfnDevPxFree(psDevNode, psMemHandle);
#if defined(PDUMP)
   if(NULL != hPDUMPMemHandle)
   {
       PDumpFree(hPDUMPMemHandle);
   }
#endif
 
}
 
PVRSRV_ERROR
PhysmemNewRamBackedPMR(CONNECTION_DATA * psConnection,
                       PVRSRV_DEVICE_NODE *psDevNode,
                       IMG_DEVMEM_SIZE_T uiSize,
                       PMR_SIZE_T uiChunkSize,
                       IMG_UINT32 ui32NumPhysChunks,
                       IMG_UINT32 ui32NumVirtChunks,
                       IMG_UINT32 *pui32MappingTable,
                       IMG_UINT32 uiLog2PageSize,
                       PVRSRV_MEMALLOCFLAGS_T uiFlags,
                       IMG_UINT32 uiAnnotationLength,
                       const IMG_CHAR *pszAnnotation,
                       PMR **ppsPMRPtr)
{
   PVRSRV_DEVICE_PHYS_HEAP ePhysHeapIdx;
   PFN_SYS_DEV_CHECK_MEM_ALLOC_SIZE pfnCheckMemAllocSize =
       psDevNode->psDevConfig->pfnCheckMemAllocSize;
 
   PVR_UNREFERENCED_PARAMETER(psConnection);
   PVR_UNREFERENCED_PARAMETER(uiAnnotationLength);
 
   /* We don't currently support sparse memory with non OS page sized heaps */
   if (ui32NumVirtChunks > 1 && (uiLog2PageSize != OSGetPageShift()))
   {
       PVR_DPF((PVR_DBG_ERROR,
               "Requested page size for sparse 2^%u is not OS page size.",
               uiLog2PageSize));
       return PVRSRV_ERROR_INVALID_PARAMS;
   }
 
   /* Protect against ridiculous page sizes */
   if (uiLog2PageSize > RGX_HEAP_2MB_PAGE_SHIFT)
   {
       PVR_DPF((PVR_DBG_ERROR, "Page size is too big: 2^%u.", uiLog2PageSize));
       return PVRSRV_ERROR_INVALID_PARAMS;
   }
 
   /* Lookup the requested physheap index to use for this PMR allocation */
   if (PVRSRV_CHECK_FW_LOCAL(uiFlags))
   {
       ePhysHeapIdx = PVRSRV_DEVICE_PHYS_HEAP_FW_LOCAL;
   }
   else if (PVRSRV_CHECK_CPU_LOCAL(uiFlags))
   {
       ePhysHeapIdx = PVRSRV_DEVICE_PHYS_HEAP_CPU_LOCAL;
   }
   else
   {
       ePhysHeapIdx = PVRSRV_DEVICE_PHYS_HEAP_GPU_LOCAL;
   }
 
   /* Fail if requesting coherency on one side but uncached on the other */
   if ( (PVRSRV_CHECK_CPU_CACHE_COHERENT(uiFlags) &&
            (PVRSRV_CHECK_GPU_UNCACHED(uiFlags) || PVRSRV_CHECK_GPU_WRITE_COMBINE(uiFlags))) )
   {
       PVR_DPF((PVR_DBG_ERROR, "Request for CPU coherency but specifying GPU uncached "
               "Please use GPU cached flags for coherency."));
       return PVRSRV_ERROR_UNSUPPORTED_CACHE_MODE;
   }
 
   if ( (PVRSRV_CHECK_GPU_CACHE_COHERENT(uiFlags) &&
            (PVRSRV_CHECK_CPU_UNCACHED(uiFlags) || PVRSRV_CHECK_CPU_WRITE_COMBINE(uiFlags))) )
   {
       PVR_DPF((PVR_DBG_ERROR, "Request for GPU coherency but specifying CPU uncached "
               "Please use CPU cached flags for coherency."));
       return PVRSRV_ERROR_UNSUPPORTED_CACHE_MODE;
   }
 
   /* Apply memory budgeting policy */
   if (pfnCheckMemAllocSize)
   {
       IMG_UINT64 uiMemSize = (IMG_UINT64)uiChunkSize * ui32NumPhysChunks;
       PVRSRV_ERROR eError;
 
       eError = pfnCheckMemAllocSize(psDevNode->psDevConfig->hSysData, uiMemSize);
       if (eError != PVRSRV_OK)
       {
           return eError;
       }
   }
 
#if defined(DEBUG)
   if (gPMRAllocFail > 0)
   {
       static IMG_UINT32 ui32AllocCount = 1;
 
       if (ui32AllocCount < gPMRAllocFail)
       {
           ui32AllocCount++;
       }
       else
       {
           PVR_DPF((PVR_DBG_ERROR, "%s failed on %d allocation.",
                    __func__, ui32AllocCount));
           return PVRSRV_ERROR_OUT_OF_MEMORY;
       }
   }
#endif /* defined(DEBUG) */
 
   return psDevNode->pfnCreateRamBackedPMR[ePhysHeapIdx](psDevNode,
                                           uiSize,
                                           uiChunkSize,
                                           ui32NumPhysChunks,
                                           ui32NumVirtChunks,
                                           pui32MappingTable,
                                           uiLog2PageSize,
                                           uiFlags,
                                           pszAnnotation,
                                           ppsPMRPtr);
}
 
PVRSRV_ERROR
PhysmemNewRamBackedLockedPMR(CONNECTION_DATA * psConnection,
                           PVRSRV_DEVICE_NODE *psDevNode,
                           IMG_DEVMEM_SIZE_T uiSize,
                           PMR_SIZE_T uiChunkSize,
                           IMG_UINT32 ui32NumPhysChunks,
                           IMG_UINT32 ui32NumVirtChunks,
                           IMG_UINT32 *pui32MappingTable,
                           IMG_UINT32 uiLog2PageSize,
                           PVRSRV_MEMALLOCFLAGS_T uiFlags,
                           IMG_UINT32 uiAnnotationLength,
                           const IMG_CHAR *pszAnnotation,
                           PMR **ppsPMRPtr)
{
 
   PVRSRV_ERROR eError;
   eError = PhysmemNewRamBackedPMR(psConnection,
                                   psDevNode,
                                   uiSize,
                                   uiChunkSize,
                                   ui32NumPhysChunks,
                                   ui32NumVirtChunks,
                                   pui32MappingTable,
                                   uiLog2PageSize,
                                   uiFlags,
                                   uiAnnotationLength,
                                   pszAnnotation,
                                   ppsPMRPtr);
 
   if (eError == PVRSRV_OK)
   {
       eError = PMRLockSysPhysAddresses(*ppsPMRPtr);
   }
 
   return eError;
}