1 /*
2 * Copyright (c) 2009-2019, 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      mos_os.c
24 //! \brief
25 //!
26 //!
27 //! \file     mos_os.c
28 //! \brief    Unified OS Formats
29 //! \details  Unified OS Formats
30 //!
31 
32 #include "mos_os.h"
33 #include "mos_util_debug.h"
34 #include "mos_util_user_interface.h"
35 #include "mos_interface.h"
36 
37 uint32_t g_apoMosEnabled = 0;
38 
39 PerfUtility* g_perfutility = PerfUtility::getInstance();
40 
AutoPerfUtility(std::string tag,std::string comp,std::string level)41 AutoPerfUtility::AutoPerfUtility(std::string tag, std::string comp, std::string level)
42 {
43     if (PERFUTILITY_IS_ENABLED(comp, level))
44     {
45         g_perfutility->startTick(tag);
46         autotag = tag;
47         bEnable = true;
48     }
49 }
50 
~AutoPerfUtility()51 AutoPerfUtility::~AutoPerfUtility()
52 {
53     if (bEnable)
54     {
55         g_perfutility->stopTick(autotag);
56     }
57 }
58 
59 #if MOS_MEDIASOLO_SUPPORTED
60 void *   _MOS_INTERFACE::pvSoloContext = nullptr;
61 uint32_t _MOS_INTERFACE::soloRefCnt = 0;
62 #endif  // MOS_MEDIASOLO_SUPPORTED
63 
64 //! \brief    Unified OS add command to command buffer
65 //! \details  Offset returned is dword aligned but size requested can be byte aligned
66 //! \param    PMOS_COMMAND_BUFFER pCmdBuffer
67 //!           [in/out] Pointer to Command Buffer
68 //! \param    void  *pCmd
69 //!           [in] Command Pointer
70 //! \param    uint32_t dwCmdSize
71 //!           [in] Size of command in bytes
72 //! \return   MOS_STATUS
73 //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
74 //!
Mos_AddCommand(PMOS_COMMAND_BUFFER pCmdBuffer,const void * pCmd,uint32_t dwCmdSize)75 MOS_STATUS Mos_AddCommand(
76     PMOS_COMMAND_BUFFER     pCmdBuffer,
77     const void              *pCmd,
78     uint32_t                dwCmdSize)
79 {
80     uint32_t dwCmdSizeDwAligned = 0;
81 
82     if (g_apoMosEnabled)
83     {
84         return MosInterface::AddCommand(pCmdBuffer, pCmd, dwCmdSize);
85     }
86 
87     //---------------------------------------------
88     MOS_OS_CHK_NULL_RETURN(pCmdBuffer);
89     MOS_OS_CHK_NULL_RETURN(pCmd);
90     //---------------------------------------------
91 
92     if (dwCmdSize == 0)
93     {
94         MOS_OS_ASSERTMESSAGE("Incorrect command size to add to command buffer.");
95         return MOS_STATUS_INVALID_PARAMETER;
96     }
97 
98     dwCmdSizeDwAligned = MOS_ALIGN_CEIL(dwCmdSize, sizeof(uint32_t));
99 
100     pCmdBuffer->iOffset    += dwCmdSizeDwAligned;
101     pCmdBuffer->iRemaining -= dwCmdSizeDwAligned;
102 
103     if (pCmdBuffer->iRemaining < 0)
104     {
105         pCmdBuffer->iOffset    -= dwCmdSizeDwAligned;
106         pCmdBuffer->iRemaining += dwCmdSizeDwAligned;
107         MOS_OS_ASSERTMESSAGE("Unable to add command: remaining space = %d, command size = %d.",
108                             pCmdBuffer->iRemaining, dwCmdSizeDwAligned);
109         return MOS_STATUS_UNKNOWN;
110     }
111 
112     MOS_OS_VERBOSEMESSAGE("The command was successfully added: remaining space = %d, buffer size = %d.",
113                         pCmdBuffer->iRemaining, pCmdBuffer->iOffset + pCmdBuffer->iRemaining);
114 
115     MOS_SecureMemcpy(pCmdBuffer->pCmdPtr, dwCmdSize, pCmd, dwCmdSize);
116     pCmdBuffer->pCmdPtr += (dwCmdSizeDwAligned / sizeof(uint32_t));
117 
118     return MOS_STATUS_SUCCESS;
119 }
120 
121 //!
122 //! \brief    Unified OS fill Resource
123 //! \details  Locks the surface and fills the resource with data
124 //! \param    PMOS_INTERFACE pOsInterface
125 //!           [in] Pointer to OS Interface
126 //! \param    PMOS_RESOURCE pOsResource
127 //!           [in] Pointer to OS Resource
128 //! \param    uint32_t dwSize
129 //!           [in] Size of the Buffer
130 //! \param    uint8_t iValue
131 //!           [in] Value to be filled
132 //! \return   MOS_STATUS
133 //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
134 //!
Mos_OsFillResource(PMOS_INTERFACE pOsInterface,PMOS_RESOURCE pOsResource,uint32_t dwSize,uint8_t iValue)135 MOS_STATUS Mos_OsFillResource(
136     PMOS_INTERFACE    pOsInterface,
137     PMOS_RESOURCE     pOsResource,
138     uint32_t          dwSize,
139     uint8_t           iValue)
140 {
141     MOS_OS_CHK_NULL_RETURN(pOsInterface);
142     MOS_OS_CHK_NULL_RETURN(pOsResource);
143     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
144 
145     uint8_t *       pByte = nullptr;
146     MOS_LOCK_PARAMS LockFlags;
147 
148     // Lock the surface for writing
149     MOS_ZeroMemory(&LockFlags, sizeof(MOS_LOCK_PARAMS));
150 
151     LockFlags.WriteOnly = 1;
152     pByte = (uint8_t*)pOsInterface->pfnLockResource(
153                         pOsInterface,
154                         pOsResource,
155                         &LockFlags);
156 
157     MOS_OS_CHK_NULL(pByte);
158 
159     MOS_FillMemory(pByte, dwSize, iValue);
160 
161     // Unlock the surface
162     MOS_OS_CHK_STATUS(pOsInterface->pfnUnlockResource(pOsInterface, pOsResource));
163 
164 finish:
165     return eStatus;
166 }
167 
168 //!
169 //! \brief    Unified OS Resources sync
170 //! \details  Syncs Resource
171 //! \param    PMOS_INTERFACE pOsInterface
172 //!           [in] Pointer to OS Interface
173 //! \param    PMOS_RESOURCE pOsResource
174 //!           [in] Pointer to OS Resource
175 //! \return   MOS_STATUS
176 //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
177 //!
Mos_OsWaitOnResource(PMOS_INTERFACE pOsInterface,PMOS_RESOURCE pOsResource)178 MOS_STATUS Mos_OsWaitOnResource(
179     PMOS_INTERFACE        pOsInterface,
180     PMOS_RESOURCE         pOsResource)
181 {
182     MOS_STATUS      eStatus;
183     MOS_LOCK_PARAMS LockFlags;
184 
185     //--------------------------
186     MOS_OS_CHK_NULL_RETURN(pOsInterface);
187     MOS_OS_CHK_NULL_RETURN(pOsResource);
188     MOS_OS_CHK_NULL_RETURN(pOsInterface->pOsContext);
189     //--------------------------
190 
191     eStatus = MOS_STATUS_SUCCESS;
192 
193     MOS_ZeroMemory(&LockFlags, sizeof(MOS_LOCK_PARAMS));
194 
195     LockFlags.WriteOnly = 1;
196 
197     MOS_OS_CHK_NULL(pOsInterface->pfnLockResource(
198                 pOsInterface,
199                 pOsResource,
200                 &LockFlags));
201 
202     MOS_OS_CHK_STATUS(pOsInterface->pfnUnlockResource(pOsInterface, pOsResource));
203 
204 finish:
205     return eStatus;
206 }
207 
208 //! \brief    Unified OS Get bits per pixel
209 //! \details  Gets Bits Per Pixel
210 //! \param    PMOS_INTERFACE pOsInterface
211 //!           [in] Pointer to OS Interface
212 //! \param    PMOS_RESOURCE pOsResource
213 //!           [in] Pointer to OS Resource
214 //! \param    uint32_t *piBpp
215 //!           [out] Bits per pixel
216 //! \return   MOS_STATUS
217 //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
218 //!
Mos_OsGetBitsPerPixel(PMOS_INTERFACE pOsInterface,MOS_FORMAT Format,uint32_t * piBpp)219 MOS_STATUS Mos_OsGetBitsPerPixel(
220     PMOS_INTERFACE            pOsInterface,
221     MOS_FORMAT                Format,
222     uint32_t                  *piBpp)
223 {
224     MOS_STATUS eStatus;
225     MOS_UNUSED(pOsInterface);
226 
227     eStatus = MOS_STATUS_SUCCESS;
228 
229     switch(Format)
230     {
231     case Format_A8R8G8B8:
232     case Format_X8R8G8B8:
233     case Format_A8B8G8R8:
234     case Format_X8B8G8R8:
235     case Format_AYUV:
236     case Format_R32U:
237     case Format_R32S:
238     case Format_R32F:
239     case Format_R10G10B10A2:
240     case Format_B10G10R10A2:
241         *piBpp = 32;
242         break;
243 
244     case Format_P010:
245     case Format_P016:
246     case Format_R8G8B8:
247         *piBpp = 24;
248         break;
249 
250     case Format_R5G6B5:
251     case Format_A8P8:
252     case Format_A8L8:
253     case Format_R16U:
254         *piBpp = 16;
255         break;
256 
257     case Format_YUY2:
258     case Format_YUYV:
259     case Format_YVYU:
260     case Format_UYVY:
261     case Format_VYUY:
262         *piBpp = 16;
263         break;
264 
265     case Format_AI44:
266     case Format_IA44:
267     case Format_A8:
268     case Format_P8:
269     case Format_L8:
270     case Format_A4L4:
271         *piBpp = 8;
272         break;
273 
274     case Format_IMC1:
275     case Format_IMC3:
276     case Format_IMC2:
277     case Format_IMC4:
278     case Format_NV12:
279     case Format_YV12:
280     case Format_I420:
281     case Format_IYUV:
282         *piBpp = 12;
283         break;
284 
285     case Format_400P:
286         *piBpp = 8;
287         break;
288 
289     case Format_411P:
290         *piBpp = 12;
291         break;
292 
293     case Format_422H:
294     case Format_422V:
295         *piBpp = 16;
296         break;
297 
298     case Format_444P:
299     case Format_RGBP:
300     case Format_BGRP:
301         *piBpp = 24;
302         break;
303 
304     case Format_YVU9:
305         *piBpp = 9;
306         break;
307 
308     case Format_Buffer:
309     case Format_RAW:
310     case Format_R8U:
311        *piBpp = 8;
312         break;
313 
314     default:
315         MOS_OS_ASSERTMESSAGE("Format '%d' not supported.", Format);
316         *piBpp = 0;
317         eStatus = MOS_STATUS_UNKNOWN;
318         goto finish;
319     }
320 
321 finish:
322     return eStatus;
323 }
324 
325 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
326 //!
327 //! \brief    Get Platform Name
328 //! \details  This function retrieve the platform name
329 //! \param    PMOS_RESOURCE pOsResource
330 //!           [in] Pointer to OS Resource
331 //! \param    buffer
332 //!           [out] Buffer to contain platform name
333 //! \return   MOS_STATUS
334 //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
335 //!
Mos_GetPlatformName(PMOS_INTERFACE pOsInterface,char * buffer)336 MOS_STATUS Mos_GetPlatformName(
337     PMOS_INTERFACE pOsInterface,
338     char           *buffer)
339 {
340     PLATFORM    platform;
341     MOS_STATUS  eStatus = MOS_STATUS_UNKNOWN;
342 
343     MOS_OS_ASSERT(pOsInterface);
344     MOS_OS_CHK_NULL(buffer);
345 
346     MOS_ZeroMemory(&platform, sizeof(platform));
347 
348     pOsInterface->pfnGetPlatform(pOsInterface, &platform);
349 
350     switch (platform.eProductFamily)
351     {
352         case IGFX_BROADWELL:
353             MOS_SecureStrcpy(buffer, MOS_COMMAND_BUFFER_PLATFORM_LEN, "BDW");
354             break;
355         case IGFX_SKYLAKE:
356             MOS_SecureStrcpy(buffer, MOS_COMMAND_BUFFER_PLATFORM_LEN, "SKL");
357             break;
358         case IGFX_BROXTON:
359             MOS_SecureStrcpy(buffer, MOS_COMMAND_BUFFER_PLATFORM_LEN, "BXT");
360             break;
361         case IGFX_CANNONLAKE:
362             MOS_SecureStrcpy(buffer, MOS_COMMAND_BUFFER_PLATFORM_LEN, "CNL");
363             break;
364         default:
365             MOS_SecureStrcpy(buffer, MOS_COMMAND_BUFFER_PLATFORM_LEN, "N/A");
366     }
367 
368     eStatus = MOS_STATUS_SUCCESS;
369 
370 finish:
371     return eStatus;
372 }
373 
374 //!
375 //! \brief    Dump command buffers
376 //! \details  This function dumps the command buffer just before rendering it to the GPU.
377 //!           The output is saved to %TEMP%\Command_buffer_dumps.
378 //!           A GPU engine prefix is printed for each command buffer, to let the user know which command streamer is being used.
379 //! \param    PMOS_RESOURCE pOsResource
380 //!           [in] Pointer to OS Resource
381 //! \param    PMOS_COMMAND_BUFFER pCmdBuffer
382 //!           [in] Pointer to command buffer structure
383 //! \return   MOS_STATUS
384 //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
385 //!
Mos_DumpCommandBuffer(PMOS_INTERFACE pOsInterface,PMOS_COMMAND_BUFFER pCmdBuffer)386 MOS_STATUS Mos_DumpCommandBuffer(
387     PMOS_INTERFACE      pOsInterface,
388     PMOS_COMMAND_BUFFER pCmdBuffer)
389 {
390     static uint32_t dwCommandBufferNumber   = 0;
391     MOS_STATUS      eStatus                 = MOS_STATUS_UNKNOWN;
392     char            *pOutputBuffer          = nullptr;
393     // Each hex value should have 9 chars.
394     uint32_t        SIZE_OF_ONE_WORD        = 9;
395     uint32_t        dwBytesWritten          = 0;
396     uint32_t        dwNumberOfDwords        = 0;
397     uint32_t        dwSizeToAllocate        = 0;
398     char            sFileName[MOS_MAX_HLT_FILENAME_LEN];
399     // Maximum length of engine name is 6
400     char            sEngName[6];
401     char            *psFileNameAfterPrefix  = nullptr;
402     size_t          nSizeFileNamePrefix     = 0;
403 
404     MOS_OS_CHK_NULL_RETURN(pOsInterface);
405     MOS_OS_CHK_NULL_RETURN(pCmdBuffer);
406 
407     if (g_apoMosEnabled)
408     {
409         return MosInterface::DumpCommandBuffer(pOsInterface->osStreamState, pCmdBuffer);
410     }
411 
412     // Set the name of the engine that is going to be used.
413     MOS_GPU_CONTEXT sGpuContext = pOsInterface->pfnGetGpuContext(pOsInterface);
414     switch (sGpuContext)
415     {
416         case MOS_GPU_CONTEXT_VIDEO:
417         case MOS_GPU_CONTEXT_VIDEO2:
418         case MOS_GPU_CONTEXT_VIDEO3:
419         case MOS_GPU_CONTEXT_VIDEO4:
420         case MOS_GPU_CONTEXT_VDBOX2_VIDEO:
421         case MOS_GPU_CONTEXT_VDBOX2_VIDEO2:
422         case MOS_GPU_CONTEXT_VDBOX2_VIDEO3:
423         case MOS_GPU_CONTEXT_VIDEO5:
424         case MOS_GPU_CONTEXT_VIDEO6:
425         case MOS_GPU_CONTEXT_VIDEO7:
426             MOS_SecureStrcpy(sEngName, sizeof(sEngName), MOS_COMMAND_BUFFER_VIDEO_ENGINE);
427             break;
428         case MOS_GPU_CONTEXT_RTE:
429             MOS_SecureStrcpy(sEngName, sizeof(sEngName), MOS_COMMAND_BUFFER_RTE_ENGINE);
430             break;
431         case MOS_GPU_CONTEXT_RENDER:
432         case MOS_GPU_CONTEXT_RENDER2:
433         case MOS_GPU_CONTEXT_RENDER3:
434         case MOS_GPU_CONTEXT_RENDER4:
435         case MOS_GPU_CONTEXT_RENDER_RA:
436         case MOS_GPU_CONTEXT_COMPUTE:
437         case MOS_GPU_CONTEXT_COMPUTE_RA:
438         case MOS_GPU_CONTEXT_CM_COMPUTE:
439             MOS_SecureStrcpy(sEngName, sizeof(sEngName), MOS_COMMAND_BUFFER_RENDER_ENGINE);
440             break;
441         case MOS_GPU_CONTEXT_VEBOX:
442             MOS_SecureStrcpy(sEngName, sizeof(sEngName), MOS_COMMAND_BUFFER_VEBOX_ENGINE);
443             break;
444         default:
445             MOS_OS_ASSERTMESSAGE("Unsupported GPU context.");
446             goto finish;
447     }
448 
449     dwNumberOfDwords = pCmdBuffer->iOffset / sizeof(uint32_t);
450 
451     dwSizeToAllocate =
452         dwNumberOfDwords * (SIZE_OF_ONE_WORD + 1) // Add 1 byte for the space following each Dword.
453         +        3       *  SIZE_OF_ONE_WORD;     // For engine and platform names.
454 
455     // Alloc output buffer.
456     pOutputBuffer = (char *)MOS_AllocAndZeroMemory(dwSizeToAllocate);
457     MOS_OS_CHK_NULL(pOutputBuffer);
458 
459     dwBytesWritten = MOS_SecureStringPrint(
460                          pOutputBuffer,
461                          SIZE_OF_ONE_WORD * 3,
462                          SIZE_OF_ONE_WORD * 3,
463                          "Eng=%s Plat=%s ",
464                          sEngName, pOsInterface->sPlatformName);
465 
466     if (pOsInterface->bDumpCommandBufferToFile)
467     {
468         // Set the file name.
469         memcpy(sFileName, pOsInterface->sDirName, MOS_MAX_HLT_FILENAME_LEN);
470         nSizeFileNamePrefix = strnlen(sFileName, sizeof(sFileName));
471         MOS_SecureStringPrint(
472             sFileName + nSizeFileNamePrefix,
473             sizeof(sFileName) - nSizeFileNamePrefix,
474             sizeof(sFileName) - nSizeFileNamePrefix,
475             "%c%s%c%s_%d.txt",
476             MOS_DIR_SEPERATOR, MOS_COMMAND_BUFFER_OUT_DIR,
477             MOS_DIR_SEPERATOR, MOS_COMMAND_BUFFER_OUT_FILE, dwCommandBufferNumber);
478 
479         // Write the output buffer to file.
480         MOS_OS_CHK_STATUS(MOS_WriteFileFromPtr((const char *)sFileName, pOutputBuffer, dwBytesWritten));
481     }
482 
483     if (pOsInterface->bDumpCommandBufferAsMessages)
484     {
485         MOS_OS_NORMALMESSAGE(pOutputBuffer);
486     }
487 
488     MOS_ZeroMemory(pOutputBuffer, dwBytesWritten);
489     dwBytesWritten = 0;
490 
491     // Fill in the output buffer with the command buffer dwords.
492     for (uint32_t dwIndex = 0; dwIndex < dwNumberOfDwords; dwIndex++)
493     {
494         dwBytesWritten += MOS_SecureStringPrint(
495             pOutputBuffer + dwBytesWritten,
496             SIZE_OF_ONE_WORD + 1,
497             SIZE_OF_ONE_WORD + 1,
498             "%.8x ",
499             pCmdBuffer->pCmdBase[dwIndex]);
500 
501         if (dwBytesWritten % (SIZE_OF_ONE_WORD + 1) == 0)
502         {
503             if (pOsInterface->bDumpCommandBufferToFile)
504             {
505                 MOS_OS_CHK_STATUS(MOS_AppendFileFromPtr((const char *)sFileName, pOutputBuffer, dwBytesWritten));
506             }
507             if (pOsInterface->bDumpCommandBufferAsMessages)
508             {
509                 MOS_OS_NORMALMESSAGE(pOutputBuffer);
510             }
511 
512             MOS_ZeroMemory(pOutputBuffer, dwBytesWritten);
513             dwBytesWritten = 0;
514         }
515     }
516 
517     if (pOsInterface->bDumpCommandBufferToFile)
518     {
519         MOS_OS_CHK_STATUS(MOS_AppendFileFromPtr((const char *)sFileName, pOutputBuffer, dwBytesWritten));
520     }
521 
522     if (pOsInterface->bDumpCommandBufferAsMessages)
523     {
524         MOS_OS_NORMALMESSAGE(pOutputBuffer);
525     }
526 
527     dwCommandBufferNumber++;
528 
529     eStatus = MOS_STATUS_SUCCESS;
530 
531 finish:
532     // Free the memory.
533     if (pOutputBuffer)
534     {
535         MOS_FreeMemAndSetNull(pOutputBuffer);
536     }
537 
538     return eStatus;
539 }
540 
541 //! \brief    Unified dump command buffer initialization
542 //! \details  check if dump command buffer was enabled and create the output directory
543 //! \param    PMOS_INTERFACE pOsInterface
544 //!           [in/out] Pointer to OS Interface
545 //! \return   MOS_STATUS
546 //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
547 //!
Mos_DumpCommandBufferInit(PMOS_INTERFACE pOsInterface)548 MOS_STATUS Mos_DumpCommandBufferInit(
549     PMOS_INTERFACE pOsInterface)
550 {
551     char                                sFileName[MOS_MAX_HLT_FILENAME_LEN];
552     MOS_STATUS                          eStatus = MOS_STATUS_UNKNOWN;
553     MOS_USER_FEATURE_VALUE_DATA         UserFeatureData;
554     char                                *psFileNameAfterPrefix = nullptr;
555     size_t                              nSizeFileNamePrefix = 0;
556 
557     MOS_OS_CHK_NULL_RETURN(pOsInterface);
558 
559     // Setup member function and variable.
560     pOsInterface->pfnDumpCommandBuffer  = Mos_DumpCommandBuffer;
561     // Check if command buffer dump was enabled in user feature.
562     MOS_UserFeature_ReadValue_ID(
563         nullptr,
564         __MEDIA_USER_FEATURE_VALUE_DUMP_COMMAND_BUFFER_ENABLE_ID,
565         &UserFeatureData);
566     pOsInterface->bDumpCommandBuffer = (UserFeatureData.i32Data != 0);
567     pOsInterface->bDumpCommandBufferToFile = ((UserFeatureData.i32Data & 1) != 0);
568     pOsInterface->bDumpCommandBufferAsMessages = ((UserFeatureData.i32Data & 2) != 0);
569 
570     if (pOsInterface->bDumpCommandBufferToFile)
571     {
572         // Create output directory.
573         eStatus = MOS_LogFileNamePrefix(pOsInterface->sDirName);
574         if (eStatus != MOS_STATUS_SUCCESS)
575         {
576             MOS_OS_NORMALMESSAGE("Failed to create log file prefix. Status = %d", eStatus);
577             goto finish;
578         }
579 
580         memcpy(sFileName, pOsInterface->sDirName, MOS_MAX_HLT_FILENAME_LEN);
581         nSizeFileNamePrefix = strnlen(sFileName, sizeof(sFileName));
582         MOS_SecureStringPrint(
583             sFileName + nSizeFileNamePrefix,
584             sizeof(sFileName) - nSizeFileNamePrefix,
585             sizeof(sFileName) - nSizeFileNamePrefix,
586             "%c%s",
587             MOS_DIR_SEPERATOR, MOS_COMMAND_BUFFER_OUT_DIR);
588 
589         eStatus = MOS_CreateDirectory(sFileName);
590         if (eStatus != MOS_STATUS_SUCCESS)
591         {
592             MOS_OS_NORMALMESSAGE("Failed to create output directory. Status = %d", eStatus);
593             goto finish;
594         }
595     }
596 
597     Mos_GetPlatformName(pOsInterface, pOsInterface->sPlatformName);
598 
599     eStatus = MOS_STATUS_SUCCESS;
600 
601 finish:
602     return eStatus;
603 }
604 #endif // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
605 
606 #if MOS_COMMAND_RESINFO_DUMP_SUPPORTED
607 
608 std::shared_ptr<GpuCmdResInfoDump> GpuCmdResInfoDump::m_instance = nullptr;
609 
GetInstance()610 const GpuCmdResInfoDump *GpuCmdResInfoDump::GetInstance()
611 {
612     if (m_instance == nullptr)
613     {
614         m_instance = std::make_shared<GpuCmdResInfoDump>();
615     }
616     return m_instance.get();
617 }
618 
GpuCmdResInfoDump()619 GpuCmdResInfoDump::GpuCmdResInfoDump()
620 {
621     MOS_USER_FEATURE_VALUE_DATA userFeatureData;
622     MOS_ZeroMemory(&userFeatureData, sizeof(userFeatureData));
623     MOS_UserFeature_ReadValue_ID(
624         nullptr,
625         __MEDIA_USER_FEATURE_VALUE_DUMP_COMMAND_INFO_ENABLE_ID,
626         &userFeatureData);
627     m_dumpEnabled = userFeatureData.bData;
628 
629     if (!m_dumpEnabled)
630     {
631         return;
632     }
633 
634     char path[MOS_MAX_PATH_LENGTH + 1];
635     MOS_ZeroMemory(path, sizeof(path));
636     MOS_ZeroMemory(&userFeatureData, sizeof(userFeatureData));
637     userFeatureData.StringData.pStringData = path;
638     MOS_UserFeature_ReadValue_ID(
639         nullptr,
640         __MEDIA_USER_FEATURE_VALUE_DUMP_COMMAND_INFO_PATH_ID,
641         &userFeatureData);
642     if (userFeatureData.StringData.uSize > MOS_MAX_PATH_LENGTH)
643     {
644         userFeatureData.StringData.uSize = 0;
645     }
646     if (userFeatureData.StringData.uSize > 0)
647     {
648         userFeatureData.StringData.pStringData[userFeatureData.StringData.uSize] = '\0';
649         userFeatureData.StringData.uSize++;
650     }
651 
652     auto tmpPath = std::string(path);
653     if (tmpPath.back() != '/' && tmpPath.back() != '\\')
654     {
655         tmpPath += '/';
656     }
657     m_path = tmpPath + "gpuCmdResInfo_" + std::to_string(MOS_GetPid()) + ".txt";
658 }
659 
Dump(PMOS_INTERFACE pOsInterface)660 void GpuCmdResInfoDump::Dump(PMOS_INTERFACE pOsInterface) const
661 {
662     if (!m_dumpEnabled)
663     {
664         return;
665     }
666 
667     using std::endl;
668 
669     std::ofstream outputFile;
670     outputFile.open(m_path, std::ios_base::app);
671     MOS_OS_ASSERT(outputFile.is_open());
672 
673     auto &cmdResInfoPtrs = GetCmdResPtrs(pOsInterface);
674 
675     outputFile << "--PerfTag: " << std::to_string(pOsInterface->pfnGetPerfTag(pOsInterface)) << " --Cmd Num: "
676         << cmdResInfoPtrs.size() << " --Dump Count: " << ++m_cnt << endl;
677 
678     outputFile << "********************************CMD Paket Begin********************************" << endl;
679     for (auto e : cmdResInfoPtrs)
680     {
681         Dump(e, outputFile);
682     }
683     outputFile << "********************************CMD Paket End**********************************" << endl << endl;
684 
685     outputFile.close();
686 }
687 
GetResType(MOS_GFXRES_TYPE resType)688 const char *GpuCmdResInfoDump::GetResType(MOS_GFXRES_TYPE resType) const
689 {
690     switch (resType)
691     {
692     case MOS_GFXRES_INVALID:
693         return "MOS_GFXRES_INVALID";
694     case MOS_GFXRES_BUFFER:
695         return "MOS_GFXRES_BUFFER";
696     case MOS_GFXRES_2D:
697         return "MOS_GFXRES_2D";
698     case MOS_GFXRES_VOLUME:
699         return "MOS_GFXRES_VOLUME";
700     default:
701         return "";
702     }
703 }
704 
GetTileType(MOS_TILE_TYPE tileType)705 const char *GpuCmdResInfoDump::GetTileType(MOS_TILE_TYPE tileType) const
706 {
707     switch (tileType)
708     {
709     case MOS_TILE_X:
710         return "MOS_TILE_X";
711     case MOS_TILE_Y:
712         return "MOS_TILE_Y";
713     case MOS_TILE_YF:
714         return "MOS_TILE_YF";
715     case MOS_TILE_YS:
716         return "MOS_TILE_YS";
717     case MOS_TILE_LINEAR:
718         return "MOS_TILE_LINEAR";
719     case MOS_TILE_INVALID:
720         return "MOS_TILE_INVALID";
721     default:
722         return "";
723     }
724 }
725 #endif // MOS_COMMAND_RESINFO_DUMP_SUPPORTED
726 
727 //! \brief    Unified OS Initializes OS Interface
728 //! \details  OS Interface initilization
729 //! \param    PMOS_INTERFACE pOsInterface
730 //!           [in/out] Pointer to OS Interface
731 //! \param    PMOS_CONTEXT pOsDriverContext
732 //!           [in] Pointer to Driver context
733 //! \return   MOS_STATUS
734 //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
735 //!
Mos_InitInterface(PMOS_INTERFACE pOsInterface,PMOS_CONTEXT pOsDriverContext,MOS_COMPONENT component)736 MOS_STATUS Mos_InitInterface(
737     PMOS_INTERFACE pOsInterface,
738     PMOS_CONTEXT   pOsDriverContext,
739     MOS_COMPONENT  component)
740 {
741     MOS_OS_CHK_NULL_RETURN(pOsInterface);
742 #if !EMUL
743     MOS_OS_CHK_NULL_RETURN(pOsDriverContext);
744 #endif
745     MOS_STATUS                  eStatus = MOS_STATUS_UNKNOWN;
746     PMOS_USER_FEATURE_INTERFACE pOsUserFeatureInterface = nullptr;
747     MOS_USER_FEATURE_VALUE_WRITE_DATA UserFeatureWriteData = __NULL_USER_FEATURE_VALUE_WRITE_DATA__;
748 
749     pOsUserFeatureInterface = &pOsInterface->UserFeatureInterface;
750     MOS_OS_CHK_NULL_RETURN(pOsUserFeatureInterface);
751 
752     // Setup Member functions
753     pOsInterface->pfnFillResource       = Mos_OsFillResource;
754     pOsInterface->pfnWaitOnResource     = Mos_OsWaitOnResource;
755     pOsInterface->pfnGetBitsPerPixel    = Mos_OsGetBitsPerPixel;
756     pOsInterface->Component             = component;
757     pOsInterface->modulizedMosEnabled   = true;
758     pOsInterface->osContextPtr          = nullptr;
759     pOsInterface->veDefaultEnable       = true;
760 
761     pOsInterface->streamIndex = 0;
762     pOsInterface->bSimIsActive = 0;
763 
764     eStatus = Mos_Specific_InitInterface(pOsInterface, pOsDriverContext);
765     if (eStatus != MOS_STATUS_SUCCESS)
766     {
767         MOS_OS_ASSERTMESSAGE("Mos_Specific_InitInterface FAILED, errno = 0x%x", eStatus);
768         return eStatus;
769     }
770 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
771     Mos_DumpCommandBufferInit(pOsInterface);
772 #endif // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
773 
774     // Report if pre-si environment is in use
775     UserFeatureWriteData.Value.i32Data  = pOsInterface->bSimIsActive;
776     UserFeatureWriteData.ValueID        = __MEDIA_USER_FEATURE_VALUE_SIM_IN_USE_ID;
777     MOS_UserFeature_WriteValues_ID(
778         nullptr,
779         &UserFeatureWriteData,
780         1);
781 
782     // Apo wrapper
783     if (g_apoMosEnabled)
784     {
785         pOsInterface->osStreamState->component                = pOsInterface->Component;
786         pOsInterface->osStreamState->currentGpuContextHandle  = pOsInterface->CurrentGpuContextHandle;
787         pOsInterface->osStreamState->GpuResetCount            = pOsInterface->dwGPUResetCount;
788         pOsInterface->osStreamState->mediaReset               = pOsInterface->bMediaReset;
789         pOsInterface->osStreamState->nullHwAccelerationEnable = pOsInterface->NullHWAccelerationEnable;
790         pOsInterface->osStreamState->osCpInterface            = pOsInterface->osCpInterface;
791         pOsInterface->osStreamState->osDeviceContext          = (OsDeviceContext *)pOsInterface->pOsContext->m_osDeviceContext;
792         pOsInterface->osStreamState->simIsActive              = pOsInterface->bSimIsActive;
793         pOsInterface->osStreamState->virtualEngineInterface   = nullptr; // Will be updated by HAL on demand
794 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
795         pOsInterface->osStreamState->dumpCommandBuffer        = pOsInterface->bDumpCommandBuffer;
796         pOsInterface->osStreamState->dumpCommandBufferAsMessages = pOsInterface->bDumpCommandBufferAsMessages;
797         pOsInterface->osStreamState->dumpCommandBufferToFile  = pOsInterface->bDumpCommandBufferToFile;
798 #endif  // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
799 
800 #if _DEBUG || _RELEASE_INTERNAL
801         pOsInterface->osStreamState->enableDbgOvrdInVirtualEngine = pOsInterface->bEnableDbgOvrdInVE;
802         pOsInterface->osStreamState->eForceVdbox = pOsInterface->eForceVdbox;
803         pOsInterface->osStreamState->eForceVebox = pOsInterface->eForceVebox;
804 #endif  // _DEBUG || _RELEASE_INTERNAL
805 
806         pOsInterface->osStreamState->ctxBasedScheduling       = pOsInterface->ctxBasedScheduling;
807         pOsInterface->osStreamState->perStreamParameters      = pOsInterface->pOsContext;
808     }
809 
810     return eStatus;
811 }
812 
813 #if !EMUL
Mos_CachePolicyGetMemoryObject(MOS_HW_RESOURCE_DEF MosUsage,GMM_CLIENT_CONTEXT * pGmmClientContext)814 MEMORY_OBJECT_CONTROL_STATE Mos_CachePolicyGetMemoryObject(
815     MOS_HW_RESOURCE_DEF MosUsage,
816     GMM_CLIENT_CONTEXT  *pGmmClientContext)
817 {
818     GMM_RESOURCE_USAGE_TYPE GmmResourceUsage[MOS_HW_RESOURCE_DEF_MAX] =
819     {
820         //
821         // CODEC USAGES
822         //
823         GMM_RESOURCE_USAGE_BEGIN_CODEC,
824         GMM_RESOURCE_USAGE_PRE_DEBLOCKING_CODEC,
825         GMM_RESOURCE_USAGE_PRE_DEBLOCKING_CODEC_PARTIALENCSURFACE,
826         GMM_RESOURCE_USAGE_POST_DEBLOCKING_CODEC,
827         GMM_RESOURCE_USAGE_ORIGINAL_UNCOMPRESSED_PICTURE_ENCODE,
828         GMM_RESOURCE_USAGE_ORIGINAL_UNCOMPRESSED_PICTURE_DECODE,
829         GMM_RESOURCE_USAGE_STREAMOUT_DATA_CODEC,
830         GMM_RESOURCE_USAGE_INTRA_ROWSTORE_SCRATCH_BUFFER_CODEC,
831         GMM_RESOURCE_USAGE_DEBLOCKINGFILTER_ROWSTORE_SCRATCH_BUFFER_CODEC,
832         GMM_RESOURCE_USAGE_REFERENCE_PICTURE_CODEC,
833         GMM_RESOURCE_USAGE_MACROBLOCK_STATUS_BUFFER_CODEC,
834         GMM_RESOURCE_USAGE_MFX_INDIRECT_BITSTREAM_OBJECT_DECODE,
835         GMM_RESOURCE_USAGE_MFX_INDIRECT_MV_OBJECT_CODEC,
836         GMM_RESOURCE_USAGE_MFD_INDIRECT_IT_COEF_OBJECT_DECODE,
837         GMM_RESOURCE_USAGE_MFC_INDIRECT_PAKBASE_OBJECT_CODEC,
838         GMM_RESOURCE_USAGE_BSDMPC_ROWSTORE_SCRATCH_BUFFER_CODEC,
839         GMM_RESOURCE_USAGE_MPR_ROWSTORE_SCRATCH_BUFFER_CODEC,
840         GMM_RESOURCE_USAGE_BITPLANE_READ_CODEC,
841         GMM_RESOURCE_USAGE_DIRECTMV_BUFFER_CODEC,
842         GMM_RESOURCE_USAGE_SURFACE_CURR_ENCODE,
843         GMM_RESOURCE_USAGE_SURFACE_REF_ENCODE,
844         GMM_RESOURCE_USAGE_SURFACE_MV_DATA_ENCODE,
845         GMM_RESOURCE_USAGE_SURFACE_HME_DOWNSAMPLED_ENCODE,
846         GMM_RESOURCE_USAGE_SURFACE_HME_DOWNSAMPLED_ENCODE_FF,
847         GMM_RESOURCE_USAGE_SURFACE_HME_DOWNSAMPLED_ENCODE_DST,
848         GMM_RESOURCE_USAGE_SURFACE_ME_DISTORTION_ENCODE,
849         GMM_RESOURCE_USAGE_SURFACE_BRC_ME_DISTORTION_ENCODE,
850         GMM_RESOURCE_USAGE_PAK_OBJECT_ENCODE,
851         GMM_RESOURCE_USAGE_SURFACE_FLATNESS_CHECK_ENCODE,
852         GMM_RESOURCE_USAGE_SURFACE_MBENC_CURBE_ENCODE,
853         GMM_RESOURCE_USAGE_SURFACE_MAD_ENCODE,
854         GMM_RESOURCE_USAGE_VP8_BLOCK_MODE_COST_ENCODE,
855         GMM_RESOURCE_USAGE_VP8_MB_MODE_COST_ENCODE,
856         GMM_RESOURCE_USAGE_VP8_MBENC_OUTPUT_ENCODE,
857         GMM_RESOURCE_USAGE_VP8_HISTOGRAM_ENCODE,
858         GMM_RESOURCE_USAGE_VP8_L3_LLC_ENCODE,
859         GMM_RESOURCE_USAGE_MFX_STANDALONE_DEBLOCKING_CODEC,
860         GMM_RESOURCE_USAGE_HCP_MD_CODEC,
861         GMM_RESOURCE_USAGE_HCP_SAO_CODEC,
862         GMM_RESOURCE_USAGE_HCP_MV_CODEC,
863         GMM_RESOURCE_USAGE_HCP_STATUS_ERROR_CODEC,
864         GMM_RESOURCE_USAGE_HCP_LCU_ILDB_STREAMOUT_CODEC,
865         GMM_RESOURCE_USAGE_VP9_PROBABILITY_BUFFER_CODEC,
866         GMM_RESOURCE_USAGE_VP9_SEGMENT_ID_BUFFER_CODEC,
867         GMM_RESOURCE_USAGE_VP9_HVD_ROWSTORE_BUFFER_CODEC,
868         GMM_RESOURCE_USAGE_MBDISABLE_SKIPMAP_CODEC,
869         GMM_RESOURCE_USAGE_VDENC_ROW_STORE_BUFFER_CODEC,
870         GMM_RESOURCE_USAGE_VDENC_STREAMIN_CODEC,
871         GMM_RESOURCE_USAGE_SURFACE_MB_QP_CODEC,
872         GMM_RESOURCE_USAGE_MACROBLOCK_ILDB_STREAM_OUT_BUFFER_CODEC,
873         GMM_RESOURCE_USAGE_SSE_SRC_PIXEL_ROW_STORE_BUFFER_CODEC,
874         GMM_RESOURCE_USAGE_SLICE_STATE_STREAM_OUT_BUFFER_CODEC,
875         GMM_RESOURCE_USAGE_CABAC_SYNTAX_STREAM_OUT_BUFFER_CODEC,
876         GMM_RESOURCE_USAGE_PRED_COL_STORE_BUFFER_CODEC,
877         GMM_RESOURCE_USAGE_SURFACE_PAK_IMAGESTATE_ENCODE,
878         GMM_RESOURCE_USAGE_SURFACE_MBENC_BRC_ENCODE,
879         GMM_RESOURCE_USAGE_SURFACE_MB_BRC_CONST_ENCODE,
880         GMM_RESOURCE_USAGE_SURFACE_BRC_MB_QP_ENCODE,
881         GMM_RESOURCE_USAGE_SURFACE_BRC_ROI_ENCODE,
882         GMM_RESOURCE_USAGE_SURFACE_SLICE_MAP_ENCODE,
883         GMM_RESOURCE_USAGE_SURFACE_WP_DOWNSAMPLED_ENCODE,
884         GMM_RESOURCE_USAGE_SURFACE_VDENC_IMAGESTATE_ENCODE,
885         GMM_RESOURCE_USAGE_SURFACE_UNCACHED,
886         GMM_RESOURCE_USAGE_SURFACE_ELLC_ONLY,
887         GMM_RESOURCE_USAGE_SURFACE_ELLC_LLC_ONLY,
888         GMM_RESOURCE_USAGE_SURFACE_ELLC_LLC_L3,
889         GMM_RESOURCE_USAGE_SURFACE_BRC_HISTORY_ENCODE,
890         GMM_RESOURCE_USAGE_SURFACE_SOFTWARE_SCOREBOARD_ENCODE,
891         GMM_RESOURCE_USAGE_SURFACE_ME_MV_DATA_ENCODE,
892         GMM_RESOURCE_USAGE_SURFACE_MV_DISTORTION_ENCODE,
893         GMM_RESOURCE_USAGE_SURFACE_4XME_DISTORTION_ENCODE,
894         GMM_RESOURCE_USAGE_SURFACE_INTRA_DISTORTION_ENCODE,
895         GMM_RESOURCE_USAGE_MB_STATS_ENCODE,
896         GMM_RESOURCE_USAGE_SURFACE_PAK_STATS_ENCODE,
897         GMM_RESOURCE_USAGE_SURFACE_PIC_STATE_READ_ENCODE,
898         GMM_RESOURCE_USAGE_SURFACE_PIC_STATE_WRITE_ENCODE,
899         GMM_RESOURCE_USAGE_SURFACE_COMBINED_ENC_ENCODE,
900         GMM_RESOURCE_USAGE_SURFACE_BRC_CONSTANT_DATA_ENCODE,
901         GMM_RESOURCE_USAGE_SURFACE_INTERMEDIATE_CU_RECORD_SURFACE_ENCODE,
902         GMM_RESOURCE_USAGE_SURFACE_SCRATCH_ENCODE,
903         GMM_RESOURCE_USAGE_SURFACE_LCU_LEVEL_DATA_ENCODE,
904         GMM_RESOURCE_USAGE_SURFACE_ENC_HISTORY_INPUT_ENCODE,
905         GMM_RESOURCE_USAGE_SURFACE_ENC_HISTORY_OUTPUT_ENCODE,
906         GMM_RESOURCE_USAGE_SURFACE_DEBUG_ENCODE,
907         GMM_RESOURCE_USAGE_SURFACE_ENC_CONSTANT_TABLE_ENCODE,
908         GMM_RESOURCE_USAGE_SURFACE_ENC_CU_RECORD_ENCODE,
909         GMM_RESOURCE_USAGE_SURFACE_ENC_MV_TEMPORAL_BUFFER_ENCODE,
910         GMM_RESOURCE_USAGE_SURFACE_ENC_CU_PACKET_FOR_PAK_ENCODE,
911         GMM_RESOURCE_USAGE_SURFACE_ENC_BCOMBINED1_ENCODE,
912         GMM_RESOURCE_USAGE_SURFACE_ENC_BCOMBINED2_ENCODE,
913         GMM_RESOURCE_USAGE_FRAME_STATS_STREAMOUT_DATA_CODEC,
914         GMM_RESOURCE_USAGE_DEBLOCKINGFILTER_ROWSTORE_TILE_LINE_BUFFER_CODEC,
915         GMM_RESOURCE_USAGE_DEBLOCKINGFILTER_ROWSTORE_TILE_COLUMN_BUFFER_CODEC,
916         GMM_RESOURCE_USAGE_HCP_MD_TILE_LINE_CODEC,
917         GMM_RESOURCE_USAGE_HCP_MD_TILE_COLUMN_CODEC,
918         GMM_RESOURCE_USAGE_HCP_SAO_TILE_LINE_CODEC,
919         GMM_RESOURCE_USAGE_HCP_SAO_TILE_COLUMN_CODEC,
920         GMM_RESOURCE_USAGE_VP9_PROBABILITY_COUNTER_BUFFER_CODEC,
921         GMM_RESOURCE_USAGE_HUC_VIRTUAL_ADDR_REGION_BUFFER_CODEC,
922         GMM_RESOURCE_USAGE_SIZE_STREAMOUT_CODEC,
923         GMM_RESOURCE_USAGE_COMPRESSED_HEADER_BUFFER_CODEC,
924         GMM_RESOURCE_USAGE_PROBABILITY_DELTA_BUFFER_CODEC,
925         GMM_RESOURCE_USAGE_TILE_RECORD_BUFFER_CODEC,
926         GMM_RESOURCE_USAGE_TILE_SIZE_STAS_BUFFER_CODEC,
927         GMM_RESOURCE_USAGE_END_CODEC,
928 
929         //
930         // CM USAGES
931         //
932         CM_RESOURCE_USAGE_SurfaceState,
933         CM_RESOURCE_USAGE_StateHeap,
934         CM_RESOURCE_USAGE_NO_L3_SurfaceState,
935         CM_RESOURCE_USAGE_NO_LLC_ELLC_SurfaceState,
936         CM_RESOURCE_USAGE_NO_LLC_SurfaceState,
937         CM_RESOURCE_USAGE_NO_ELLC_SurfaceState,
938         CM_RESOURCE_USAGE_NO_LLC_L3_SurfaceState,
939         CM_RESOURCE_USAGE_NO_ELLC_L3_SurfaceState,
940         CM_RESOURCE_USAGE_NO_CACHE_SurfaceState,
941         CM_RESOURCE_USAGE_L1_Enabled_SurfaceState,
942 
943         //
944         // MP USAGES
945         //
946         MP_RESOURCE_USAGE_BEGIN,
947         MP_RESOURCE_USAGE_DEFAULT,
948         MP_RESOURCE_USAGE_DEFAULT_FF,
949         MP_RESOURCE_USAGE_DEFAULT_RCS,
950         MP_RESOURCE_USAGE_SurfaceState,
951         MP_RESOURCE_USAGE_SurfaceState_FF,
952         MP_RESOURCE_USAGE_SurfaceState_RCS,
953         MP_RESOURCE_USAGE_AGE3_SurfaceState,
954         MP_RESOURCE_USAGE_EDRAM_SurfaceState,
955         MP_RESOURCE_USAGE_EDRAM_AGE3_SurfaceState,
956         MP_RESOURCE_USAGE_No_L3_SurfaceState,
957         MP_RESOURCE_USAGE_No_LLC_L3_SurfaceState,
958         MP_RESOURCE_USAGE_No_LLC_L3_AGE_SurfaceState,
959         MP_RESOURCE_USAGE_No_LLC_eLLC_L3_AGE_SurfaceState,
960         MP_RESOURCE_USAGE_PartialEnc_No_LLC_L3_AGE_SurfaceState,
961         MP_RESOURCE_USAGE_END,
962 
963         // MHW - SFC
964         MHW_RESOURCE_USAGE_Sfc_CurrentOutputSurface,                                //!< SFC output surface
965         MHW_RESOURCE_USAGE_Sfc_CurrentOutputSurface_PartialEncSurface,              //!< SFC output surface for partial secure surfaces
966         MHW_RESOURCE_USAGE_Sfc_AvsLineBufferSurface,                                //!< SFC AVS Line buffer Surface
967         MHW_RESOURCE_USAGE_Sfc_IefLineBufferSurface,                                //!< SFC IEF Line buffer Surface
968 
969     };
970 
971     MOS_OS_ASSERT(pGmmClientContext);
972 
973     GMM_RESOURCE_USAGE_TYPE usage = GmmResourceUsage[MosUsage];
974     if (pGmmClientContext->GetCachePolicyElement(usage).Initialized)
975     {
976         return pGmmClientContext->CachePolicyGetMemoryObject(nullptr, usage);
977     }
978     else
979     {
980         return pGmmClientContext->GetCachePolicyUsage()[GMM_RESOURCE_USAGE_UNKNOWN].MemoryObjectOverride;
981     }
982 }
983 #endif
984 
985 #ifndef SKIP_VE_DEFINE
Mos_CheckVirtualEngineSupported(PMOS_INTERFACE osInterface,bool isDecode,bool veDefaultEnable)986 MOS_STATUS Mos_CheckVirtualEngineSupported(
987     PMOS_INTERFACE      osInterface,
988     bool                isDecode,
989     bool                veDefaultEnable)
990 {
991     MOS_STATUS                  eStatus = MOS_STATUS_SUCCESS;
992     PLATFORM                    platform;
993     MOS_USER_FEATURE_VALUE_DATA userFeatureData;
994 
995     MOS_OS_CHK_NULL_RETURN(osInterface);
996     MOS_ZeroMemory(&platform, sizeof(PLATFORM));
997 
998     osInterface->pfnGetPlatform(osInterface, &platform);
999 
1000     if (isDecode)
1001     {
1002         //UMD Decode Virtual Engine Override
1003         // 0: disable. can set to 1 only when KMD VE is enabled.
1004         // Default value is 1 if not set this key
1005         memset(&userFeatureData, 0, sizeof(userFeatureData));
1006         eStatus = MOS_UserFeature_ReadValue_ID(
1007             nullptr,
1008             __MEDIA_USER_FEATURE_VALUE_ENABLE_DECODE_VIRTUAL_ENGINE_ID,
1009             &userFeatureData);
1010         osInterface->bSupportVirtualEngine = userFeatureData.u32Data ? true : false;
1011 
1012         // force bSupportVirtualEngine to false when virtual engine not enabled by default
1013         if ((!veDefaultEnable || !osInterface->veDefaultEnable) &&
1014             (eStatus == MOS_STATUS_USER_FEATURE_KEY_OPEN_FAILED))
1015         {
1016             osInterface->bSupportVirtualEngine = false;
1017         }
1018 
1019         auto skuTable = osInterface->pfnGetSkuTable(osInterface);
1020         MOS_OS_CHK_NULL_RETURN(skuTable);
1021         if (osInterface->bSupportVirtualEngine && MEDIA_IS_SKU(skuTable, FtrContextBasedScheduling))
1022         {
1023             osInterface->ctxBasedScheduling = true;
1024         }
1025         else
1026         {
1027             osInterface->ctxBasedScheduling = false;
1028         }
1029 
1030         if(osInterface->pfnCheckVirtualEngineSupported)
1031         {
1032             osInterface->pfnCheckVirtualEngineSupported(osInterface);
1033         }
1034 
1035         osInterface->multiNodeScaling = osInterface->ctxBasedScheduling && MEDIA_IS_SKU(skuTable, FtrVcs2) ? true : false;
1036 
1037 #if (_DEBUG || _RELEASE_INTERNAL)
1038         MOS_USER_FEATURE_VALUE_WRITE_DATA  userFeatureWriteData = __NULL_USER_FEATURE_VALUE_WRITE_DATA__;
1039         userFeatureWriteData.Value.i32Data = osInterface->ctxBasedScheduling ? true : false;
1040         userFeatureWriteData.ValueID = __MEDIA_USER_FEATURE_VALUE_ENABLE_DECODE_VE_CTXSCHEDULING_ID;
1041         MOS_UserFeature_WriteValues_ID(nullptr, &userFeatureWriteData, 1);
1042 #endif
1043     }
1044     else
1045     {
1046         //UMD Encode Virtual Engine Override
1047         memset(&userFeatureData, 0, sizeof(userFeatureData));
1048         eStatus = MOS_UserFeature_ReadValue_ID(
1049             nullptr,
1050             __MEDIA_USER_FEATURE_VALUE_ENABLE_ENCODE_VIRTUAL_ENGINE_ID,
1051             &userFeatureData);
1052         osInterface->bSupportVirtualEngine = userFeatureData.u32Data ? true : false;
1053 
1054         // force bSupportVirtualEngine to false when virtual engine not enabled by default
1055         if (!osInterface->veDefaultEnable && (eStatus == MOS_STATUS_USER_FEATURE_KEY_READ_FAILED || eStatus == MOS_STATUS_USER_FEATURE_KEY_OPEN_FAILED))
1056         {
1057             osInterface->bSupportVirtualEngine = false;
1058         }
1059 
1060         auto skuTable = osInterface->pfnGetSkuTable(osInterface);
1061         MOS_OS_CHK_NULL_RETURN(skuTable);
1062         if (osInterface->bSupportVirtualEngine && MEDIA_IS_SKU(skuTable, FtrContextBasedScheduling))
1063         {
1064             osInterface->ctxBasedScheduling = true;
1065         }
1066         else
1067         {
1068             osInterface->ctxBasedScheduling = false;
1069         }
1070         osInterface->multiNodeScaling = osInterface->ctxBasedScheduling && MEDIA_IS_SKU(skuTable, FtrVcs2) ? true : false;
1071     }
1072 
1073     if (g_apoMosEnabled)
1074     {
1075         // Update ctx based scheduling flag also in APO MOS stream state
1076         MOS_OS_CHK_NULL_RETURN(osInterface->osStreamState);
1077         osInterface->osStreamState->ctxBasedScheduling = osInterface->ctxBasedScheduling;
1078     }
1079     MOS_OS_VERBOSEMESSAGE("Virtual Engine Context based SCheduling enabled:%d.\n", osInterface->ctxBasedScheduling);
1080     MOS_OS_VERBOSEMESSAGE("Virtual Engine Multi-node Scaling enabled:%d.\n", osInterface->multiNodeScaling);
1081 
1082     return eStatus;
1083 }
1084 #endif // !SKIP_VE_DEFINE
1085 
1086 
1087 
1088