xref: /aosp_15_r20/external/intel-media-driver/media_driver/agnostic/common/cm/cm_hal_dump.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2017-2021, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file      cm_hal_dump.cpp
24 //! \brief     Functions related to dump generated hw commands/curbe data
25 //!
26 
27 #include "cm_hal.h"
28 #include "renderhal_platform_interface.h"
29 
30 #if (MDF_COMMAND_BUFFER_DUMP || MDF_CURBE_DATA_DUMP || MDF_SURFACE_STATE_DUMP)
31 #if defined(ANDROID) || defined(LINUX)
32 #define PlatformSNPrintf snprintf
33 #define PLATFORM_DIR_SEPERATOR   "/"
34 #else
35 #define PlatformSNPrintf sprintf_s
36 #define PLATFORM_DIR_SEPERATOR   "\\"
37 #endif
38 
39 #define SIZE_OF_DWORD_PLUS_ONE                  (2*sizeof(uint32_t) +1)
40 //!
41 //! \brief    Dump Hex Dword to dest buffer
42 //! \param    [in] destBuf
43 //!           dest buffer
44 //! \param    [in] buflen
45 //!           length of buffer
46 //! \param    [in] srcBuf
47 //!           pointer to surface of buffer
48 //! \param    [in] uNum
49 //!           number of dword to dump
50 //! \return   number of bytes written
51 //!
HalCm_CopyHexDwordLine(char * destBuf,size_t buflen,uint32_t * srcBuf,uint32_t uNum)52 static uint32_t HalCm_CopyHexDwordLine(char  *destBuf, size_t buflen, uint32_t *srcBuf, uint32_t uNum)
53 {
54     uint32_t bytesWritten = 0;
55     for (uint32_t i = 0; i < uNum; ++i) {
56         bytesWritten += PlatformSNPrintf(destBuf + bytesWritten, buflen - bytesWritten, "%08x ", srcBuf[i]);
57     }
58     bytesWritten += PlatformSNPrintf(destBuf + bytesWritten, buflen - bytesWritten, "\n");
59 
60     return bytesWritten;
61 }
62 
63 //!
64 //! \brief    Get dump file name and counter
65 //! \param    [in] fileNamePrefix[]
66 //!           dump file name
67 //! \param    [in] timeStampFlag
68 //!           flag to control if need time stamp
69 //! \param    [in] counter
70 //!           dump file counter
71 //! \param    [in] outputDir
72 //!           pointer to output dir
73 //! \param    [in] outputFile
74 //!           pointer output file prefix
75 //! \return   number of bytes written
76 //!
GetFileNameAndCounter(char fileNamePrefix[],bool timeStampFlag,int32_t counter,const char * outputDir,const char * outputFile,CM_HAL_STATE * halState)77 int32_t GetFileNameAndCounter(char fileNamePrefix[],
78                               bool timeStampFlag,
79                               int32_t counter,
80                               const char *outputDir,
81                               const char *outputFile,
82                               CM_HAL_STATE *halState)
83 {
84     GetLogFileLocation(outputDir, fileNamePrefix, halState->osInterface->pOsContext);
85     PlatformSNPrintf(fileNamePrefix + strlen(fileNamePrefix),
86                      MOS_MAX_HLT_FILENAME_LEN - strlen(fileNamePrefix), PLATFORM_DIR_SEPERATOR);
87 
88     if (timeStampFlag)
89     {
90         SYSTEMTIME systime;
91         GetLocalTime(&systime);
92         PlatformSNPrintf(fileNamePrefix + strlen(fileNamePrefix),
93                         MOS_MAX_HLT_FILENAME_LEN - strlen(fileNamePrefix), "%s_%d_%d_%d_%d_%d_%d_%d.txt",
94                         outputFile, systime.wMonth, systime.wDay, systime.wHour,
95                         systime.wMinute, systime.wSecond, systime.wMilliseconds, counter);
96     }
97     else
98     {
99         //check if command buffer or surface state counter, and get it
100         if (!strcmp(outputFile,"Command_Buffer"))
101         {
102             counter = GetCommandBufferDumpCounter(
103                 __MEDIA_USER_FEATURE_VALUE_MDF_CMD_DUMP_COUNTER_ID,
104                 halState->osInterface->pOsContext);
105         }
106         else if (!strcmp(outputFile,"Surface_State_Dump"))
107         {
108             counter = GetSurfaceStateDumpCounter(
109                 __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_STATE_DUMP_COUNTER_ID,
110                 halState->osInterface->pOsContext);
111         }
112         else if (!strcmp(outputFile, "Interface_Descriptor_Data_Dump"))
113         {
114             counter = GetInterfaceDescriptorDataDumpCounter(
115                 __MEDIA_USER_FEATURE_VALUE_MDF_INTERFACE_DESCRIPTOR_DATA_COUNTER_ID,
116                 halState->osInterface->pOsContext);
117         }
118 
119         PlatformSNPrintf(fileNamePrefix + strlen(fileNamePrefix),
120                          MOS_MAX_HLT_FILENAME_LEN - strlen(fileNamePrefix), "%s_%d.txt", outputFile, counter);
121     }
122     return counter;
123 
124 }
125 
126 #endif
127 
128 #if MDF_COMMAND_BUFFER_DUMP
129 
130 #define HALCM_COMMAND_BUFFER_OUTPUT_DIR         "HALCM_Command_Buffer_Dumps"
131 #define HALCM_COMMAND_BUFFER_OUTPUT_FILE        "Command_Buffer"
132 
133 //!
134 //! \brief    Read Register key to check if dump flag enabled
135 //! \param    [in] state
136 //!           Pointer to cm hal state
137 //! \return   CM_SUCCESS if success, else fail reason
138 //!
HalCm_InitDumpCommandBuffer(PCM_HAL_STATE state)139 int32_t HalCm_InitDumpCommandBuffer(PCM_HAL_STATE state)
140 {
141     char                            fileName[MOS_MAX_HLT_FILENAME_LEN];
142     MOS_STATUS                      eStatus;
143     MOS_USER_FEATURE_VALUE_DATA     userFeatureValueData;
144     int32_t                         hr = CM_FAILURE;
145 
146     MOS_OS_ASSERT(state);
147 
148     MOS_ZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
149 
150     // Check if command buffer dump was enabled in user feature settings.
151     GetLogFileLocation(HALCM_COMMAND_BUFFER_OUTPUT_DIR, fileName,
152                        state->osInterface->pOsContext);
153 
154     eStatus = MOS_UserFeature_ReadValue_ID(
155         nullptr,
156         __MEDIA_USER_FEATURE_VALUE_MDF_CMD_DUMP_ENABLE_ID,
157         &userFeatureValueData,
158         state->osInterface->pOsContext);
159     if (eStatus != MOS_STATUS_SUCCESS)
160     {
161         MOS_OS_NORMALMESSAGE("Unable to read command buffer user feature key. Status = %d", eStatus);
162         goto finish;
163     }
164     if (userFeatureValueData.bData)
165     {
166         eStatus = MosUtilities::MosCreateDirectory(fileName);
167         if (eStatus != MOS_STATUS_SUCCESS)
168         {
169             MOS_OS_NORMALMESSAGE("Failed to create output directory. Status = %d", eStatus);
170             goto finish;
171         }
172         // Setup member function and variable.
173         state->dumpCommandBuffer = userFeatureValueData.bData ? true : false;
174         if (userFeatureValueData.bData == 17)
175         {
176             state->enableCMDDumpTimeStamp = true;
177         }
178         else
179         {
180             state->enableCMDDumpTimeStamp = false;
181         }
182     }
183     hr = CM_SUCCESS;
184 finish:
185     return hr;
186 }
187 
188 //!
189 //! \brief    Dump command buffer to file
190 //! \param    [in] state
191 //!           pointer to cm hal state
192 //! \param    [in] cmdBuffer
193 //!           pointer to command buffer
194 //! \param    [in] offsetSurfaceState
195 //!           offset to surface state
196 //! \param    [in] sizeOfSurfaceState
197 //!           size of surface state
198 //! \return   int32_t
199 //!           CM_SUCCESS if success, else fail reason
200 //!
HalCm_DumpCommadBuffer(PCM_HAL_STATE state,PMOS_COMMAND_BUFFER cmdBuffer,int offsetSurfaceState,size_t sizeOfSurfaceState)201 int32_t HalCm_DumpCommadBuffer(PCM_HAL_STATE state, PMOS_COMMAND_BUFFER cmdBuffer, int offsetSurfaceState, size_t sizeOfSurfaceState)
202 {
203 
204     int32_t        commandBufferNumber = 0;
205     int32_t         hr = CM_FAILURE;
206     MOS_STATUS      eStatus = MOS_STATUS_UNKNOWN;
207     char            *outputBuffer = nullptr;
208     // Each hex value should have 9 chars.
209     uint32_t        bytesWritten = 0;
210     uint32_t        numberOfDwords = 0;
211     uint32_t        sizeToAllocate = 0;
212     char            fileName[MOS_MAX_HLT_FILENAME_LEN];
213     uint32_t        offset = 0;
214 
215     PMOS_INTERFACE osInterface = state->osInterface;
216     PRENDERHAL_STATE_HEAP stateHeap   = state->renderHal->pStateHeap;
217 
218     MOS_OS_ASSERT(state);
219     MOS_OS_ASSERT(cmdBuffer);
220 
221    //Check if use timestamp in cmd buffer dump file
222     commandBufferNumber = GetFileNameAndCounter(fileName, state->enableCMDDumpTimeStamp,
223                                                 commandBufferNumber,
224                                                 HALCM_COMMAND_BUFFER_OUTPUT_DIR,
225                                                 HALCM_COMMAND_BUFFER_OUTPUT_FILE, state);
226 
227     //get the command buffer header size
228     offset = GetCommandBufferHeaderDWords(osInterface);
229 
230     numberOfDwords = cmdBuffer->iOffset / sizeof(uint32_t) - offset;
231     sizeToAllocate = numberOfDwords * (SIZE_OF_DWORD_PLUS_ONE)+2 +   //length of command buffer line
232         stateHeap->iCurrentSurfaceState *
233         (SIZE_OF_DWORD_PLUS_ONE *
234         state->renderHal->pRenderHalPltInterface->GetSurfaceStateCmdSize() / sizeof(uint32_t) + 2); //length of surface state lines
235                                                                                                                           // Alloc output buffer.
236     outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
237     if (!outputBuffer) {
238         MOS_OS_NORMALMESSAGE("Failed to allocate memory for command buffer dump");
239         return MOS_STATUS_NO_SPACE;
240     }
241 
242     // write command buffer dwords.
243     bytesWritten += HalCm_CopyHexDwordLine(outputBuffer, sizeToAllocate - bytesWritten,
244                                           (uint32_t *)cmdBuffer->pCmdBase + offset, numberOfDwords);
245     MOS_OS_CHK_STATUS(MosUtilities::MosWriteFileFromPtr((const char *)fileName, outputBuffer, bytesWritten));
246     commandBufferNumber++;
247 
248     //Record command buffer dump counter
249     if (!state->enableCMDDumpTimeStamp)
250     {
251         RecordCommandBufferDumpCounter(commandBufferNumber,
252                                        __MEDIA_USER_FEATURE_VALUE_MDF_CMD_DUMP_COUNTER_ID,
253                                        state->osInterface->pOsContext);
254     }
255     hr = CM_SUCCESS;
256 finish:
257     // Free the memory.
258     if (outputBuffer)
259     {
260         MOS_FreeMemAndSetNull(outputBuffer);
261     }
262     return hr;
263 }
264 
265 #endif
266 
267 #if MDF_CURBE_DATA_DUMP
268 
269 #define HALCM_CURBE_DATA_OUTPUT_DIR         "HALCM_Curbe_Data_Dumps"
270 #define HALCM_CURBE_DATA_OUTPUT_FILE        "Curbe_Data"
271 
272 //!
273 //! \brief    Read Register key to check if Curbe Data dump flag enabled
274 //! \param    [in] state
275 //!           Pointer to cm hal state
276 //! \return   CM_SUCCESS if success, else fail reason
277 //!
HalCm_InitDumpCurbeData(PCM_HAL_STATE state)278 int32_t HalCm_InitDumpCurbeData(PCM_HAL_STATE state)
279 {
280     MOS_USER_FEATURE_VALUE_DATA userFeatureValueData;
281     char                        fileName[MOS_MAX_HLT_FILENAME_LEN];
282     MOS_STATUS                  eStatus;
283     int32_t                     hr = CM_FAILURE;
284 
285     MOS_OS_ASSERT(state);
286 
287     MOS_ZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
288 
289     // Check if curbe data dump was enabled in user feature settings.
290     eStatus = MOS_UserFeature_ReadValue_ID(
291         nullptr,
292         __MEDIA_USER_FEATURE_VALUE_MDF_CURBE_DUMP_ENABLE_ID,
293         &userFeatureValueData,
294         state->osInterface->pOsContext);
295     if (eStatus != MOS_STATUS_SUCCESS)
296     {
297         MOS_OS_NORMALMESSAGE("Unable to read curbe data dump user feature key. Status = %d", eStatus);
298         goto finish;
299     }
300     if (userFeatureValueData.bData)
301     {
302         GetLogFileLocation(HALCM_CURBE_DATA_OUTPUT_DIR, fileName,
303                            state->osInterface->pOsContext);
304         eStatus = MosUtilities::MosCreateDirectory(fileName);
305         if (eStatus != MOS_STATUS_SUCCESS)
306         {
307             MOS_OS_NORMALMESSAGE("Failed to create curbe data output directory. Status = %d", eStatus);
308             goto finish;
309         }
310         // Setup member function and variable.
311         state->dumpCurbeData = userFeatureValueData.bData ? true : false;
312     }
313     hr = CM_SUCCESS;
314 finish:
315     return hr;
316 }
317 
318 //!
319 //! \brief    Dump Curbe Data to file
320 //! \param    [in] state
321 //!           pointer to cm hal state
322 //! \return   int32_t
323 //!           CM_SUCCESS if success, else fail reason
324 //!
HalCm_DumpCurbeData(PCM_HAL_STATE state)325 int32_t HalCm_DumpCurbeData(PCM_HAL_STATE state)
326 {
327     static uint32_t curbeDataNumber = 0;
328     int32_t         hr = CM_FAILURE;
329     MOS_STATUS      eStatus = MOS_STATUS_UNKNOWN;
330     char            *outputBuffer = nullptr;
331     uint32_t        bytesWritten = 0;
332     char            fileName[MOS_MAX_HLT_FILENAME_LEN];
333     uint32_t        numberOfDwords = 0;
334     uint32_t        sizeToAllocate = 0;
335     PMOS_INTERFACE osInterface = state->osInterface;
336     uint32_t        *curbeData = nullptr;
337     PRENDERHAL_STATE_HEAP stateHeap = state->renderHal->pStateHeap;
338 
339     MOS_OS_ASSERT(state);
340 
341     // Set the file name.
342     GetLogFileLocation(HALCM_CURBE_DATA_OUTPUT_DIR, fileName,
343                        state->osInterface->pOsContext);
344 
345     PlatformSNPrintf(fileName + strlen(fileName), MOS_MAX_HLT_FILENAME_LEN - strlen(fileName),
346                      PLATFORM_DIR_SEPERATOR);
347     PlatformSNPrintf(fileName + strlen(fileName), MOS_MAX_HLT_FILENAME_LEN - strlen(fileName),
348                      "%s_%d.txt", HALCM_CURBE_DATA_OUTPUT_FILE, (int)curbeDataNumber);
349 
350     // write curbe data dwords.
351     if (state->dshEnabled)
352     {
353         PRENDERHAL_MEDIA_STATE_LEGACY pCurMediaStateLegacy = (PRENDERHAL_MEDIA_STATE_LEGACY)stateHeap->pCurMediaState;
354         numberOfDwords = pCurMediaStateLegacy->pDynamicState->Curbe.dwSize / sizeof(uint32_t);
355         sizeToAllocate = numberOfDwords*SIZE_OF_DWORD_PLUS_ONE+2;
356         outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
357         curbeData = (uint32_t *)MOS_AllocAndZeroMemory(pCurMediaStateLegacy->pDynamicState->Curbe.dwSize);
358         pCurMediaStateLegacy->pDynamicState->memoryBlock.ReadData(curbeData,
359             pCurMediaStateLegacy->pDynamicState->Curbe.dwOffset,
360             pCurMediaStateLegacy->pDynamicState->Curbe.dwSize);
361 
362         bytesWritten += HalCm_CopyHexDwordLine(outputBuffer,
363                           sizeToAllocate - bytesWritten,
364                           curbeData,
365                           numberOfDwords);
366     }
367     else
368     {
369         numberOfDwords = stateHeap->pCurMediaState->iCurbeOffset / sizeof(uint32_t);
370         sizeToAllocate = numberOfDwords*SIZE_OF_DWORD_PLUS_ONE+2;
371         outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
372         bytesWritten += HalCm_CopyHexDwordLine(outputBuffer,
373                           sizeToAllocate - bytesWritten,
374                           (uint32_t*)(stateHeap->pGshBuffer + stateHeap->pCurMediaState->dwOffset + stateHeap->dwOffsetCurbe),
375                           numberOfDwords);
376     }
377 
378     MOS_OS_CHK_STATUS(MosUtilities::MosWriteFileFromPtr((const char *)fileName, outputBuffer, bytesWritten));
379 
380     curbeDataNumber++;
381 
382     hr = CM_SUCCESS;
383 
384 finish:
385     // Free the memory.
386     if (outputBuffer)
387     {
388         MOS_FreeMemAndSetNull(outputBuffer);
389     }
390 
391     if (curbeData)
392     {
393         MOS_FreeMemAndSetNull(curbeData);
394     }
395 
396     return hr;
397 
398 }
399 
400 #endif
401 
402 #if MDF_SURFACE_CONTENT_DUMP
403 
404 //!
405 //! \brief    Read Register key to check if Surface content flag enabled
406 //! \param    [in] state
407 //!           Pointer to cm hal state
408 //! \return   CM_SUCCESS if success, else fail reason
409 //!
HalCm_InitSurfaceDump(PCM_HAL_STATE state)410 int32_t HalCm_InitSurfaceDump(PCM_HAL_STATE state)
411 {
412     MOS_USER_FEATURE_VALUE_DATA userFeatureValueData;
413     MOS_STATUS                  eStatus;
414     int32_t                     hr = CM_FAILURE;
415 
416     MOS_OS_ASSERT(state);
417 
418     MOS_ZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
419 
420     // Check if surface content dump was enabled in user feature settings.
421     eStatus = MOS_UserFeature_ReadValue_ID(
422         nullptr,
423         __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_DUMP_ENABLE_ID,
424         &userFeatureValueData,
425         state->osInterface->pOsContext);
426     if (eStatus != MOS_STATUS_SUCCESS)
427     {
428         MOS_OS_NORMALMESSAGE("Unable to read surface content dump user feature key. Status = %d", eStatus);
429         goto finish;
430     }
431     if (userFeatureValueData.bData)
432     {
433         // Setup member function and variable.
434         state->dumpSurfaceContent = userFeatureValueData.bData ? true : false;
435     }
436     hr = CM_SUCCESS;
437 finish:
438     return hr;
439 }
440 
441 #endif
442 
443 
444 #if MDF_SURFACE_STATE_DUMP
445 #define HALCM_SURFACE_STATE_OUTPUT_DIR         "HALCM_Surface_State_Dumps"
446 #define HALCM_SURFACE_STATE_OUTPUT_FILE        "Surface_State_Dump"
447 
HalCm_InitDumpSurfaceState(PCM_HAL_STATE state)448 int32_t HalCm_InitDumpSurfaceState(PCM_HAL_STATE state)
449 {
450     MOS_USER_FEATURE_VALUE_DATA userFeatureValueData;
451     char                        fileName[MOS_MAX_HLT_FILENAME_LEN];
452     MOS_STATUS                  eStatus;
453     int32_t                     hr = CM_FAILURE;
454 
455     MOS_OS_ASSERT(state);
456 
457     MOS_ZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
458 
459     // Check if command buffer dump was enabled in user feature settings.
460     GetLogFileLocation(HALCM_SURFACE_STATE_OUTPUT_DIR, fileName,
461                        state->osInterface->pOsContext);
462 
463     eStatus = MOS_UserFeature_ReadValue_ID(
464         nullptr,
465         __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_STATE_DUMP_ENABLE_ID,
466         &userFeatureValueData,
467         state->osInterface->pOsContext);
468     if (eStatus != MOS_STATUS_SUCCESS)
469     {
470         MOS_OS_NORMALMESSAGE("Unable to read surface state user feature key. Status = %d", eStatus);
471         goto finish;
472     }
473     if (userFeatureValueData.bData)
474     {
475         eStatus = MosUtilities::MosCreateDirectory(fileName);
476         if (eStatus != MOS_STATUS_SUCCESS)
477         {
478             MOS_OS_NORMALMESSAGE("Failed to create output directory. Status = %d", eStatus);
479             goto finish;
480         }
481         // Setup member function and variable.
482         state->dumpSurfaceState = userFeatureValueData.bData ? true : false;
483 
484         if (userFeatureValueData.bData == 17)
485         {
486             state->enableSurfaceStateDumpTimeStamp = true;
487         }
488         else
489         {
490             state->enableSurfaceStateDumpTimeStamp = false;
491         }
492     }
493     hr = CM_SUCCESS;
494 finish:
495     return hr;
496 }
497 
498 //!
499 //! \brief    Dump surface state to file
500 //! \param    [in] state
501 //!           pointer to cm hal state
502 //! \param    [in] offsetSurfaceState
503 //!           offset to surface state
504 //! \param    [in] sizeOfSurfaceState
505 //!           size of surface state
506 //! \return   int32_t
507 //!           CM_SUCCESS if success, else fail reason
508 //!
HalCm_DumpSurfaceState(PCM_HAL_STATE state,int offsetSurfaceState,size_t sizeOfSurfaceState)509 int32_t HalCm_DumpSurfaceState(PCM_HAL_STATE state,  int offsetSurfaceState, size_t sizeOfSurfaceState)
510 {
511     int32_t         hr = CM_FAILURE;
512     MOS_STATUS      eStatus = MOS_STATUS_UNKNOWN;
513 
514     PMOS_INTERFACE osInterface = state->osInterface;
515     PRENDERHAL_STATE_HEAP stateHeap = state->renderHal->pStateHeap;
516 
517     char            filename[MOS_MAX_HLT_FILENAME_LEN];
518     int32_t         surfacestatedumpNumber = 0;
519     uint32_t        surfacebytesWritten = 0;
520     char            *surfaceoutputBuffer = nullptr;
521     uint32_t        surfacesizeToAllocate = 0;
522 
523    //Check if use timestamp in cmd buffer dump file
524     surfacestatedumpNumber = GetFileNameAndCounter(filename,
525                                                    state->enableSurfaceStateDumpTimeStamp,
526                                                    surfacestatedumpNumber,
527                                                    HALCM_SURFACE_STATE_OUTPUT_DIR,
528                                                    HALCM_SURFACE_STATE_OUTPUT_FILE,
529                                                    state);
530 
531     //calculate surface state dump allocation size
532     surfacesizeToAllocate = stateHeap->iCurrentSurfaceState *
533         (SIZE_OF_DWORD_PLUS_ONE *
534         state->renderHal->pRenderHalPltInterface->GetSurfaceStateCmdSize() / sizeof(uint32_t) + 2);
535 
536     //allocate surface output buffer
537     surfaceoutputBuffer = (char *)MOS_AllocAndZeroMemory(surfacesizeToAllocate);
538     if (!surfaceoutputBuffer) {
539         MOS_OS_NORMALMESSAGE("Failed to allocate memory for surface state dump");
540         return MOS_STATUS_NO_SPACE;
541     }
542 
543     //write all surface states
544     for (int32_t index = 0; index < stateHeap->iCurrentSurfaceState; ++index) {
545         PRENDERHAL_SURFACE_STATE_ENTRY entry = stateHeap->pSurfaceEntry + index;
546         void *surfaceState = (char*)entry->pSurfaceState;
547         //the address of surface states are 32bit or uint32_t aligned.
548         surfacebytesWritten += HalCm_CopyHexDwordLine(surfaceoutputBuffer + surfacebytesWritten,
549                                surfacesizeToAllocate - surfacebytesWritten, (uint32_t*)surfaceState,
550                                sizeOfSurfaceState / sizeof(uint32_t));
551     }
552 
553     //Write to file
554     MOS_OS_CHK_STATUS(MosUtilities::MosWriteFileFromPtr((const char *)filename, surfaceoutputBuffer,
555                       surfacebytesWritten));
556 
557     surfacestatedumpNumber++;
558 
559     if (!state->enableSurfaceStateDumpTimeStamp)
560     {
561         RecordSurfaceStateDumpCounter(
562             surfacestatedumpNumber,
563             __MEDIA_USER_FEATURE_VALUE_MDF_SURFACE_STATE_DUMP_COUNTER_ID,
564             state->osInterface->pOsContext);
565     }
566 
567     hr = CM_SUCCESS;
568 
569 finish:
570     // Free the memory.
571     if (surfaceoutputBuffer)
572     {
573         MOS_FreeMemAndSetNull(surfaceoutputBuffer);
574     }
575 
576     return hr;
577 
578 }
579 #endif
580 
581 #if MDF_INTERFACE_DESCRIPTOR_DATA_DUMP
582 #define HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_DIR         "HALCM_Interface_Descriptor_Data_Dumps"
583 #define HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_FILE        "Interface_Descriptor_Data_Dump"
584 
HalCm_InitDumpInterfaceDescriporData(PCM_HAL_STATE state)585 int32_t HalCm_InitDumpInterfaceDescriporData(PCM_HAL_STATE state)
586 {
587     MOS_USER_FEATURE_VALUE_DATA userFeatureValueData;
588     char                        fileName[MOS_MAX_HLT_FILENAME_LEN];
589     MOS_STATUS                  eStatus;
590     int32_t                     hr = CM_FAILURE;
591 
592     MOS_OS_ASSERT(state);
593 
594     MOS_ZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
595 
596     // Check if command buffer dump was enabled in user feature settings.
597     GetLogFileLocation(HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_DIR, fileName,
598                        state->osInterface->pOsContext);
599 
600     eStatus = MOS_UserFeature_ReadValue_ID(
601         nullptr,
602         __MEDIA_USER_FEATURE_VALUE_MDF_INTERFACE_DESCRIPTOR_DATA_DUMP_ID,
603         &userFeatureValueData,
604         state->osInterface->pOsContext);
605     if (eStatus != MOS_STATUS_SUCCESS)
606     {
607         MOS_OS_NORMALMESSAGE("Unable to read interface descriptor data dump user feature key. Status = %d", eStatus);
608         goto finish;
609     }
610     if (userFeatureValueData.bData)
611     {
612         eStatus = MosUtilities::MosCreateDirectory(fileName);
613         if (eStatus != MOS_STATUS_SUCCESS)
614         {
615             MOS_OS_NORMALMESSAGE("Failed to create output directory. Status = %d", eStatus);
616             goto finish;
617         }
618         // Setup member function and variable.
619         state->dumpIDData = userFeatureValueData.bData ? true : false;
620         if (userFeatureValueData.bData == 17)
621         {
622             state->enableIDDumpTimeStamp = true;
623         }
624         else
625         {
626             state->enableIDDumpTimeStamp = false;
627         }
628 
629     }
630     hr = CM_SUCCESS;
631 finish:
632     return hr;
633 }
634 
635 //!
636 //! \brief    Dump interface descriptor data to file
637 //! \param    [in] state
638 //!           pointer to cm hal state
639 //! \return   int32_t
640 //!           CM_SUCCESS if success, else fail reason
641 //!
HalCm_DumpInterfaceDescriptorData(PCM_HAL_STATE state)642 int32_t HalCm_DumpInterfaceDescriptorData(PCM_HAL_STATE state)
643 {
644     uint32_t        IDDNumber = 0;
645     int32_t         hr = CM_FAILURE;
646     MOS_STATUS      eStatus = MOS_STATUS_UNKNOWN;
647     char            *outputBuffer = nullptr;
648     uint32_t        bytesWritten = 0;
649     char            fileName[MOS_MAX_HLT_FILENAME_LEN];
650     uint32_t        numberOfDwords = 0;
651     uint32_t        sizeToAllocate = 0;
652     PMOS_INTERFACE osInterface = state->osInterface;
653     uint32_t        *InterfaceDescriptorData = nullptr;
654     PRENDERHAL_STATE_HEAP stateHeap = state->renderHal->pStateHeap;
655 
656     MOS_OS_ASSERT(state);
657 
658     // Set the file name.
659     GetLogFileLocation(HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_DIR, fileName,
660                        state->osInterface->pOsContext);
661 
662     //Check if use timestamp in cmd buffer dump file
663     IDDNumber = GetFileNameAndCounter(fileName, state->enableIDDumpTimeStamp,
664                                       IDDNumber, HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_DIR,
665                                       HALCM_INTERFACE_DESCRIPTOR_DATA_OUTPUT_FILE,
666                                       state);
667 
668     // write interface descriptor data dwords.
669     if (state->dshEnabled)
670     {
671         PRENDERHAL_MEDIA_STATE_LEGACY pCurMediaStateLegacy = (PRENDERHAL_MEDIA_STATE_LEGACY)stateHeap->pCurMediaState;
672         numberOfDwords = pCurMediaStateLegacy->pDynamicState->MediaID.dwSize / sizeof(uint32_t);
673         sizeToAllocate = numberOfDwords * SIZE_OF_DWORD_PLUS_ONE + 2;
674         outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
675         InterfaceDescriptorData = (uint32_t *)MOS_AllocAndZeroMemory(pCurMediaStateLegacy->pDynamicState->MediaID.dwSize);
676         pCurMediaStateLegacy->pDynamicState->memoryBlock.ReadData(InterfaceDescriptorData,
677             pCurMediaStateLegacy->pDynamicState->MediaID.dwOffset,
678             pCurMediaStateLegacy->pDynamicState->MediaID.dwSize);
679 
680         bytesWritten += HalCm_CopyHexDwordLine(outputBuffer,
681             sizeToAllocate - bytesWritten,
682             InterfaceDescriptorData,
683             numberOfDwords);
684     }
685     else
686     {
687         numberOfDwords = stateHeap->dwSizeMediaID / sizeof(uint32_t);
688         sizeToAllocate = numberOfDwords * SIZE_OF_DWORD_PLUS_ONE + 2;
689         outputBuffer = (char *)MOS_AllocAndZeroMemory(sizeToAllocate);
690         bytesWritten += HalCm_CopyHexDwordLine(outputBuffer,
691             sizeToAllocate - bytesWritten,
692             (uint32_t*)(stateHeap->pGshBuffer + stateHeap->pCurMediaState->dwOffset + stateHeap->dwOffsetMediaID),
693             numberOfDwords);
694     }
695 
696     MOS_OS_CHK_STATUS(MosUtilities::MosWriteFileFromPtr((const char *)fileName, outputBuffer, bytesWritten));
697 
698     IDDNumber++;
699     if (!state->enableIDDumpTimeStamp)
700     {
701         RecordInterfaceDescriptorDataDumpCounter(
702             IDDNumber,
703             __MEDIA_USER_FEATURE_VALUE_MDF_INTERFACE_DESCRIPTOR_DATA_COUNTER_ID,
704             state->osInterface->pOsContext);
705     }
706 
707     hr = CM_SUCCESS;
708 
709 finish:
710     // Free the memory.
711     if (outputBuffer)
712     {
713         MOS_FreeMemAndSetNull(outputBuffer);
714     }
715 
716     if (InterfaceDescriptorData)
717     {
718         MOS_FreeMemAndSetNull(InterfaceDescriptorData);
719     }
720 
721     return hr;
722 
723 }
724 #endif
725