1 /*
2 * Copyright (c) 2009-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     mos_interface.cpp
24 //! \brief    MOS interface implementation
25 //!
26 
27 #include "mos_interface.h"
28 #include "mos_context_specific_next.h"
29 #include "mos_gpucontext_specific_next.h"
30 #include "mos_os_specific_next.h"
31 #include "media_libva_common.h"
32 #include "mos_auxtable_mgr.h"
33 #include "mos_os_virtualengine_singlepipe_specific_next.h"
34 #include "mos_os_virtualengine_scalability_specific_next.h"
35 #include "mos_graphicsresource_specific_next.h"
36 #include "mos_bufmgr_priv.h"
37 
38 #if (_DEBUG || _RELEASE_INTERNAL)
39 #include <stdlib.h>   //for simulate random OS API failure
40 #include <time.h>     //for simulate random OS API failure
41 #endif
42 
43 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
44 #include <fstream>
45 #include <sstream>
46 #include <iomanip>
47 #endif
48 
InitOsUtilities(DDI_DEVICE_CONTEXT ddiDeviceContext)49 MOS_STATUS MosInterface::InitOsUtilities(DDI_DEVICE_CONTEXT ddiDeviceContext)
50 {
51     MOS_UNUSED(ddiDeviceContext);
52     MosUtilities::MosUtilitiesInit(nullptr);
53 
54     // MOS_OS_FUNCTION_ENTER need mos utilities init
55     MOS_OS_FUNCTION_ENTER;
56 
57 #if (_DEBUG || _RELEASE_INTERNAL)
58     //Init MOS OS API fail simulate flags
59     MosInitOsApiFailSimulateFlag(ddiDeviceContext);
60 #endif
61 
62     //Read user feature key here for Per Utility Tool Enabling
63     if (!g_perfutility->bPerfUtilityKey)
64     {
65         MOS_USER_FEATURE_VALUE_DATA UserFeatureData;
66         MosUtilities::MosZeroMemory(&UserFeatureData, sizeof(UserFeatureData));
67         MosUtilities::MosUserFeatureReadValueID(
68             NULL,
69             __MEDIA_USER_FEATURE_VALUE_PERF_UTILITY_TOOL_ENABLE_ID,
70             &UserFeatureData,
71             (MOS_CONTEXT_HANDLE) nullptr);
72         g_perfutility->dwPerfUtilityIsEnabled = UserFeatureData.i32Data;
73 
74         char                        sFilePath[MOS_MAX_PERF_FILENAME_LEN + 1] = "";
75         MOS_USER_FEATURE_VALUE_DATA perfFilePath;
76         MOS_STATUS                  eStatus_Perf = MOS_STATUS_SUCCESS;
77 
78         MosUtilities::MosZeroMemory(&perfFilePath, sizeof(perfFilePath));
79         perfFilePath.StringData.pStringData = sFilePath;
80         eStatus_Perf                        = MosUtilities::MosUserFeatureReadValueID(
81             nullptr,
82             __MEDIA_USER_FEATURE_VALUE_PERF_OUTPUT_DIRECTORY_ID,
83             &perfFilePath,
84             (MOS_CONTEXT_HANDLE) nullptr);
85         if (eStatus_Perf == MOS_STATUS_SUCCESS)
86         {
87             g_perfutility->setupFilePath(sFilePath);
88         }
89         else
90         {
91             g_perfutility->setupFilePath();
92         }
93 
94         g_perfutility->bPerfUtilityKey = true;
95     }
96 
97     return MOS_STATUS_SUCCESS;
98 }
99 
CloseOsUtilities(PMOS_CONTEXT mosCtx)100 MOS_STATUS MosInterface::CloseOsUtilities(PMOS_CONTEXT mosCtx)
101 {
102     MOS_OS_FUNCTION_ENTER;
103     // Close MOS utlities
104     MosUtilities::MosUtilitiesClose(nullptr);
105 
106 #if (_DEBUG || _RELEASE_INTERNAL)
107     //reset MOS init OS API simulate flags
108     MosDeinitOsApiFailSimulateFlag();
109 #endif
110 
111     return MOS_STATUS_SUCCESS;
112 }
113 
CreateOsDeviceContext(DDI_DEVICE_CONTEXT ddiDeviceContext,MOS_DEVICE_HANDLE * deviceContext)114 MOS_STATUS MosInterface::CreateOsDeviceContext(DDI_DEVICE_CONTEXT ddiDeviceContext, MOS_DEVICE_HANDLE *deviceContext)
115 {
116     MOS_OS_FUNCTION_ENTER;
117 
118     MOS_OS_CHK_NULL_RETURN(deviceContext);
119     MOS_OS_CHK_NULL_RETURN(ddiDeviceContext);
120 
121     *deviceContext = MOS_New(OsContextSpecificNext);
122 
123     MOS_OS_CHK_NULL_RETURN(*deviceContext);
124 
125     MOS_OS_CHK_STATUS_RETURN((*deviceContext)->Init((PMOS_CONTEXT)ddiDeviceContext));
126 
127     return MOS_STATUS_SUCCESS;
128 }
129 
DestroyOsDeviceContext(MOS_DEVICE_HANDLE deviceContext)130 MOS_STATUS MosInterface::DestroyOsDeviceContext(MOS_DEVICE_HANDLE deviceContext)
131 {
132     MOS_OS_FUNCTION_ENTER;
133 
134     if (deviceContext)
135     {
136         deviceContext->CleanUp();
137         MOS_Delete(deviceContext);
138         deviceContext = nullptr;
139     }
140 
141     return MOS_STATUS_SUCCESS;
142 }
143 
CreateOsStreamState(MOS_STREAM_HANDLE * streamState,MOS_DEVICE_HANDLE deviceContext,MOS_INTERFACE_HANDLE osInterface,MOS_COMPONENT component,EXTRA_PARAMS extraParams)144 MOS_STATUS MosInterface::CreateOsStreamState(
145     MOS_STREAM_HANDLE    *streamState,
146     MOS_DEVICE_HANDLE    deviceContext,
147     MOS_INTERFACE_HANDLE osInterface,
148     MOS_COMPONENT        component,
149     EXTRA_PARAMS         extraParams)
150 {
151     MOS_USER_FEATURE_VALUE_DATA userFeatureData     = {};
152     MOS_STATUS                  eStatusUserFeature  = MOS_STATUS_SUCCESS;
153 
154     MOS_OS_FUNCTION_ENTER;
155     MOS_OS_CHK_NULL_RETURN(streamState);
156     MOS_OS_CHK_NULL_RETURN(deviceContext);
157 
158     *streamState = MOS_New(MosStreamState);
159     MOS_OS_CHK_NULL_RETURN(*streamState);
160 
161     (*streamState)->osDeviceContext         = deviceContext;
162     (*streamState)->component               = component;
163     (*streamState)->currentGpuContextHandle = 0;
164 
165     (*streamState)->simIsActive             = false;
166     (*streamState)->mediaReset              = false;
167 
168     (*streamState)->usesPatchList           = true;
169     (*streamState)->usesGfxAddress          = !(*streamState)->usesPatchList;
170 
171     MosUtilities::MosZeroMemory(&userFeatureData, sizeof(userFeatureData));
172 #if (_DEBUG || _RELEASE_INTERNAL)
173     MosUtilities::MosUserFeatureReadValueID(
174         nullptr,
175         __MEDIA_USER_FEATURE_VALUE_SIM_ENABLE_ID,
176         &userFeatureData,
177         (MOS_CONTEXT_HANDLE) nullptr);
178     (*streamState)->simIsActive = (int32_t)userFeatureData.i32Data;
179 
180     // Null HW Driver
181     // 0: Disabled
182     MosUtilities::MosZeroMemory(&userFeatureData, sizeof(userFeatureData));
183     MosUtilities::MosUserFeatureReadValueID(
184         nullptr,
185         __MEDIA_USER_FEATURE_VALUE_NULL_HW_ACCELERATION_ENABLE_ID,
186         &userFeatureData,
187         (MOS_CONTEXT_HANDLE) nullptr);
188     (*streamState)->nullHwAccelerationEnable.Value = userFeatureData.u32Data;
189 #endif
190 
191     // SupportVirtualEngine flag is set by Hals
192     (*streamState)->supportVirtualEngine    = false;
193     (*streamState)->useHwSemaForResSyncInVe = false;
194     (*streamState)->virtualEngineInterface  = nullptr;
195     (*streamState)->veEnable                = false;
196     (*streamState)->phasedSubmission        = true;
197 
198     auto skuTable = GetSkuTable(*streamState);
199     MOS_OS_CHK_NULL_RETURN(skuTable);
200     if (MEDIA_IS_SKU(skuTable, FtrGucSubmission))
201     {
202         (*streamState)->bGucSubmission = true;
203     }
204 
205 #if (_DEBUG || _RELEASE_INTERNAL)
206     // read the "Force VDBOX" user feature key
207     // 0: not force
208     MosUtilities::MosZeroMemory(&userFeatureData, sizeof(userFeatureData));
209     MosUtilities::MosUserFeatureReadValueID(
210         nullptr,
211         __MEDIA_USER_FEATURE_VALUE_FORCE_VDBOX_ID,
212         &userFeatureData,
213         (MOS_CONTEXT_HANDLE) nullptr);
214     (*streamState)->eForceVdbox = userFeatureData.u32Data;
215 
216     //Read Scalable/Legacy Decode mode on Gen11+
217     //1:by default for scalable decode mode
218     //0:for legacy decode mode
219     MosUtilities::MosZeroMemory(&userFeatureData, sizeof(userFeatureData));
220     eStatusUserFeature = MosUtilities::MosUserFeatureReadValueID(
221         nullptr,
222         __MEDIA_USER_FEATURE_VALUE_ENABLE_HCP_SCALABILITY_DECODE_ID,
223         &userFeatureData,
224         (MOS_CONTEXT_HANDLE) nullptr);
225     (*streamState)->hcpDecScalabilityMode = userFeatureData.u32Data ? MOS_SCALABILITY_ENABLE_MODE_DEFAULT : MOS_SCALABILITY_ENABLE_MODE_FALSE;
226     if((*streamState)->hcpDecScalabilityMode
227         && (eStatusUserFeature == MOS_STATUS_SUCCESS))
228     {
229         //user's value to enable scalability
230         (*streamState)->hcpDecScalabilityMode = MOS_SCALABILITY_ENABLE_MODE_USER_FORCE;
231     }
232 
233     (*streamState)->frameSplit = false;
234     MosUtilities::MosZeroMemory(&userFeatureData, sizeof(userFeatureData));
235     MosUtilities::MosUserFeatureReadValueID(
236         NULL,
237         __MEDIA_USER_FEATURE_VALUE_ENABLE_LINUX_FRAME_SPLIT_ID,
238         &userFeatureData,
239         (MOS_CONTEXT_HANDLE) nullptr);
240     (*streamState)->frameSplit = (uint32_t)userFeatureData.i32Data;
241 
242     MOS_ZeroMemory(&userFeatureData, sizeof(userFeatureData));
243     MOS_UserFeature_ReadValue_ID(
244         NULL,
245         __MEDIA_USER_FEATURE_VALUE_ENABLE_GUC_SUBMISSION_ID,
246         &userFeatureData,
247         (MOS_CONTEXT_HANDLE) nullptr);
248     (*streamState)->bGucSubmission = (*streamState)->bGucSubmission && ((uint32_t)userFeatureData.i32Data);
249 
250     //KMD Virtual Engine DebugOverride
251     // 0: not Override
252     MosUtilities::MosZeroMemory(&userFeatureData, sizeof(userFeatureData));
253     MosUtilities::MosUserFeatureReadValueID(
254         nullptr,
255         __MEDIA_USER_FEATURE_VALUE_ENABLE_VE_DEBUG_OVERRIDE_ID,
256         &userFeatureData,
257         (MOS_CONTEXT_HANDLE) nullptr);
258     (*streamState)->enableDbgOvrdInVirtualEngine = userFeatureData.u32Data ? true : false;
259 #endif
260 
261     if (component == COMPONENT_VPCommon ||
262         component == COMPONENT_VPreP    ||
263         component == COMPONENT_LibVA)
264     {
265         // UMD Vebox Virtual Engine Scalability Mode
266         // 0: disable. can set to 1 only when KMD VE is enabled.
267         MosUtilities::MosZeroMemory(&userFeatureData, sizeof(userFeatureData));
268         eStatusUserFeature = MosUtilities::MosUserFeatureReadValueID(
269             nullptr,
270             __MEDIA_USER_FEATURE_VALUE_ENABLE_VEBOX_SCALABILITY_MODE_ID,
271             &userFeatureData,
272             (MOS_CONTEXT_HANDLE) nullptr);
273         (*streamState)->veboxScalabilityMode = userFeatureData.u32Data ? MOS_SCALABILITY_ENABLE_MODE_DEFAULT : MOS_SCALABILITY_ENABLE_MODE_FALSE;
274 
275 #if (_DEBUG || _RELEASE_INTERNAL)
276         if((*streamState)->veboxScalabilityMode
277             && (eStatusUserFeature == MOS_STATUS_SUCCESS))
278         {
279             //user's value to enable scalability
280             (*streamState)->veboxScalabilityMode = MOS_SCALABILITY_ENABLE_MODE_USER_FORCE;
281             (*streamState)->enableDbgOvrdInVirtualEngine = true;
282 
283             if ((*streamState)->eForceVebox == MOS_FORCE_VEBOX_NONE)
284             {
285                 (*streamState)->eForceVebox = MOS_FORCE_VEBOX_1_2;
286             }
287         }
288         else if ((!(*streamState)->veboxScalabilityMode)
289             && (eStatusUserFeature == MOS_STATUS_SUCCESS))
290         {
291             (*streamState)->enableDbgOvrdInVirtualEngine = true;
292             (*streamState)->eForceVebox        = MOS_FORCE_VEBOX_NONE;
293         }
294 
295         // read the "Force VEBOX" user feature key
296         // 0: not force
297         MosUtilities::MosZeroMemory(&userFeatureData, sizeof(userFeatureData));
298         MosUtilities::MosUserFeatureReadValueID(
299             nullptr,
300             __MEDIA_USER_FEATURE_VALUE_FORCE_VEBOX_ID,
301             &userFeatureData,
302             (MOS_CONTEXT_HANDLE) nullptr);
303         (*streamState)->eForceVebox = (MOS_FORCE_VEBOX)userFeatureData.u32Data;
304 #endif
305     }
306 
307     MOS_USER_FEATURE_VALUE_WRITE_DATA userFeatureWriteData = __NULL_USER_FEATURE_VALUE_WRITE_DATA__;
308     // Report if pre-si environment is in use
309     userFeatureWriteData.Value.i32Data = (*streamState)->simIsActive;
310     userFeatureWriteData.ValueID       = __MEDIA_USER_FEATURE_VALUE_SIM_IN_USE_ID;
311     MosUtilities::MosUserFeatureWriteValuesID(
312         nullptr,
313         &userFeatureWriteData,
314         1,
315         (MOS_CONTEXT_HANDLE)nullptr);
316 
317 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
318     DumpCommandBufferInit(*streamState);
319 #endif  // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
320 
321     MOS_OS_CHK_STATUS_RETURN(MosInterface::InitStreamParameters(*streamState, extraParams));
322 
323     return MOS_STATUS_SUCCESS;
324 }
325 
DestroyOsStreamState(MOS_STREAM_HANDLE streamState)326 MOS_STATUS MosInterface::DestroyOsStreamState(
327     MOS_STREAM_HANDLE streamState)
328 {
329     MOS_OS_FUNCTION_ENTER;
330 
331     MOS_OS_CHK_NULL_RETURN(streamState);
332 
333     MOS_Delete(streamState);
334     streamState = nullptr;
335 
336     return MOS_STATUS_SUCCESS;
337 }
338 
InitStreamParameters(MOS_STREAM_HANDLE streamState,EXTRA_PARAMS extraParams)339 MOS_STATUS MosInterface::InitStreamParameters(
340     MOS_STREAM_HANDLE   streamState,
341     EXTRA_PARAMS        extraParams)
342 {
343     MOS_STATUS                  eStatus             = MOS_STATUS_SUCCESS;
344     PMOS_CONTEXT                context             = nullptr;
345     uint32_t                    resetCount          = 0;
346     int32_t                     ret                 = 0;
347     MOS_BUFMGR                  *bufMgr             = nullptr;
348     int32_t                     fd                  = -1;
349     OsContextSpecificNext       *osDeviceContext    = nullptr;
350     MOS_USER_FEATURE_VALUE_DATA userFeatureData     = {};
351 
352     MOS_OS_FUNCTION_ENTER;
353 
354     MOS_OS_CHK_NULL_RETURN(streamState);
355     MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
356     MOS_OS_CHK_NULL_RETURN(extraParams);
357 
358     osDeviceContext = (OsContextSpecificNext *)streamState->osDeviceContext;
359     fd              = osDeviceContext->GetFd();
360     if (0 >= fd)
361     {
362         MOS_OS_ASSERTMESSAGE("Invalid fd");
363         return MOS_STATUS_INVALID_HANDLE;
364     }
365 
366     bufMgr = osDeviceContext->GetBufMgr();
367     MOS_OS_CHK_NULL_RETURN(bufMgr);
368 
369     context = (PMOS_OS_CONTEXT)MOS_AllocAndZeroMemory(sizeof(MOS_OS_CONTEXT));
370     MOS_OS_CHK_NULL_RETURN(context);
371 
372     context->m_apoMosEnabled    = true;
373     context->m_osDeviceContext  = streamState->osDeviceContext;
374     context->bSimIsActive       = streamState->simIsActive;
375 
376     streamState->perStreamParameters = (OS_PER_STREAM_PARAMETERS)context;
377 
378     context->pGmmClientContext  = streamState->osDeviceContext->GetGmmClientContext();;
379 
380     context->bufmgr             = bufMgr;
381     context->m_gpuContextMgr    = osDeviceContext->GetGpuContextMgr();
382     context->m_cmdBufMgr        = osDeviceContext->GetCmdBufferMgr();
383     context->fd                 = fd;
384     context->pPerfData          = ((PMOS_CONTEXT)extraParams)->pPerfData;
385 
386     context->m_auxTableMgr      = osDeviceContext->GetAuxTableMgr();
387 
388     mos_bufmgr_gem_enable_reuse(bufMgr);
389 
390     context->SkuTable           = *osDeviceContext->GetSkuTable();
391     context->WaTable            = *osDeviceContext->GetWaTable();
392     context->gtSystemInfo       = *osDeviceContext->GetGtSysInfo();
393     context->platform           = *osDeviceContext->GetPlatformInfo();
394 
395     context->bUse64BitRelocs    = true;
396     context->bUseSwSwizzling    = context->bSimIsActive || MEDIA_IS_SKU(&context->SkuTable, FtrUseSwSwizzling);
397     context->bTileYFlag         = MEDIA_IS_SKU(&context->SkuTable, FtrTileY);
398 
399     if (MEDIA_IS_SKU(&context->SkuTable, FtrContextBasedScheduling))
400     {
401         MOS_TraceEventExt(EVENT_GPU_CONTEXT_CREATE, EVENT_TYPE_START,
402                           &eStatus, sizeof(eStatus), nullptr, 0);
403         context->intel_context = mos_gem_context_create_ext(context->bufmgr, 0);
404         MOS_OS_CHK_NULL_RETURN(context->intel_context);
405         context->intel_context->vm = mos_gem_vm_create(context->bufmgr);
406         MOS_OS_CHK_NULL_RETURN(context->intel_context->vm);
407         MOS_TraceEventExt(EVENT_GPU_CONTEXT_CREATE, EVENT_TYPE_END,
408                           &context->intel_context, sizeof(void *),
409                           &eStatus, sizeof(eStatus));
410     }
411     else  //use legacy context create ioctl for pre-gen11 platforms
412     {
413         MOS_OS_ASSERTMESSAGE("Do not support the legacy context creation.\n");
414         MOS_FreeMemAndSetNull(context->pPerfData);
415         MOS_FreeMemAndSetNull(context);
416         streamState->perStreamParameters = nullptr;
417         return MOS_STATUS_UNIMPLEMENTED;
418     }
419     context->intel_context->pOsContext = context;
420     ret                                = mos_get_reset_stats(context->intel_context, &resetCount, nullptr, nullptr);
421     if (ret)
422     {
423         MOS_OS_NORMALMESSAGE("mos_get_reset_stats return error(%d)\n", ret);
424         resetCount = 0;
425     }
426     streamState->ctxPriority      = 0;
427     streamState->gpuResetCount    = resetCount;
428     streamState->gpuActiveBatch   = 0;
429     streamState->gpuPendingBatch  = 0;
430 
431     context->bIsAtomSOC           = false;
432     context->bFreeContext         = true;
433 #ifndef ANDROID
434     {
435         drm_i915_getparam_t gp;
436         int32_t             ret   = -1;
437         int32_t             value = 0;
438 
439         //KMD support VCS2?
440         gp.value = &value;
441         gp.param = I915_PARAM_HAS_BSD2;
442 
443         ret = drmIoctl(context->fd, DRM_IOCTL_I915_GETPARAM, &gp);
444         if (ret == 0 && value != 0)
445         {
446             context->bKMDHasVCS2 = true;
447         }
448         else
449         {
450             context->bKMDHasVCS2 = false;
451         }
452     }
453 #endif
454 
455     // read "Linux PerformanceTag Enable" user feature key
456     MosUtilities::MosUserFeatureReadValueID(
457         nullptr,
458         __MEDIA_USER_FEATURE_VALUE_LINUX_PERFORMANCETAG_ENABLE_ID,
459         &userFeatureData,
460         (MOS_CONTEXT_HANDLE)nullptr);
461     context->uEnablePerfTag = userFeatureData.u32Data;
462 
463     return MOS_STATUS_SUCCESS;
464 }
465 
GetInterfaceVersion(MOS_DEVICE_HANDLE deviceContext)466 uint32_t MosInterface::GetInterfaceVersion(MOS_DEVICE_HANDLE deviceContext)
467 {
468     MOS_OS_FUNCTION_ENTER;
469 
470     // No interface version to get in Linux
471 
472     return 0;
473 }
474 
GetPlatform(MOS_STREAM_HANDLE streamState)475 PLATFORM *MosInterface::GetPlatform(MOS_STREAM_HANDLE streamState)
476 {
477     MOS_OS_FUNCTION_ENTER;
478 
479     if (streamState && streamState->osDeviceContext)
480     {
481         return streamState->osDeviceContext->GetPlatformInfo();
482     }
483 
484     return nullptr;
485 }
486 
GetSkuTable(MOS_STREAM_HANDLE streamState)487 MEDIA_FEATURE_TABLE *MosInterface::GetSkuTable(MOS_STREAM_HANDLE streamState)
488 {
489     MOS_OS_FUNCTION_ENTER;
490 
491     if (streamState && streamState->osDeviceContext)
492     {
493         return streamState->osDeviceContext->GetSkuTable();
494     }
495 
496     return nullptr;
497 }
498 
GetWaTable(MOS_STREAM_HANDLE streamState)499 MEDIA_WA_TABLE *MosInterface::GetWaTable(MOS_STREAM_HANDLE streamState)
500 {
501     MOS_OS_FUNCTION_ENTER;
502 
503     if (streamState && streamState->osDeviceContext)
504     {
505         return streamState->osDeviceContext->GetWaTable();
506     }
507 
508     return nullptr;
509 }
510 
GetGtSystemInfo(MOS_STREAM_HANDLE streamState)511 MEDIA_SYSTEM_INFO *MosInterface::GetGtSystemInfo(MOS_STREAM_HANDLE streamState)
512 {
513     MOS_OS_FUNCTION_ENTER;
514 
515     if (streamState && streamState->osDeviceContext)
516     {
517         return streamState->osDeviceContext->GetGtSysInfo();
518     }
519 
520     return nullptr;
521 }
522 
GetMediaEngineInfo(MOS_STREAM_HANDLE streamState,MEDIA_ENGINE_INFO & info)523 MOS_STATUS MosInterface::GetMediaEngineInfo(MOS_STREAM_HANDLE streamState, MEDIA_ENGINE_INFO &info)
524 {
525     MOS_OS_FUNCTION_ENTER;
526 
527     auto systemInfo = MosInterface::GetGtSystemInfo(streamState);
528     MOS_OS_CHK_NULL_RETURN(systemInfo);
529 
530     MosUtilities::MosZeroMemory(&info, sizeof(info));
531     info.VDBoxInfo = systemInfo->VDBoxInfo;
532     info.VEBoxInfo = systemInfo->VEBoxInfo;
533 
534     return MOS_STATUS_SUCCESS;
535 }
536 
GetAdapterInfo(MOS_STREAM_HANDLE streamState)537 ADAPTER_INFO *MosInterface::GetAdapterInfo(MOS_STREAM_HANDLE streamState)
538 {
539     MOS_OS_FUNCTION_ENTER;
540 
541     // No adapter Info in Linux
542 
543     return nullptr;
544 }
545 
CreateGpuContext(MOS_STREAM_HANDLE streamState,GpuContextCreateOption & createOption,GPU_CONTEXT_HANDLE & gpuContextHandle)546 MOS_STATUS MosInterface::CreateGpuContext(
547     MOS_STREAM_HANDLE             streamState,
548     GpuContextCreateOption       &createOption,
549     GPU_CONTEXT_HANDLE           &gpuContextHandle)
550 {
551     MOS_OS_FUNCTION_ENTER;
552 
553     MOS_OS_CHK_NULL_RETURN(streamState);
554     MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
555 
556     auto osDeviceContext = streamState->osDeviceContext;
557 
558     auto gpuContextMgr = osDeviceContext->GetGpuContextMgr();
559     MOS_OS_CHK_NULL_RETURN(gpuContextMgr);
560 
561     auto cmdBufMgr = osDeviceContext->GetCmdBufferMgr();
562     MOS_OS_CHK_NULL_RETURN(cmdBufMgr);
563 
564     auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
565     MOS_OS_CHK_NULL_RETURN(osParameters);
566 
567     if (createOption.gpuNode == MOS_GPU_NODE_3D && createOption.SSEUValue != 0)
568     {
569         struct drm_i915_gem_context_param_sseu sseu;
570         MosUtilities::MosZeroMemory(&sseu, sizeof(sseu));
571         sseu.engine.engine_class    = I915_ENGINE_CLASS_RENDER;
572         sseu.engine.engine_instance = 0;
573 
574         if (mos_get_context_param_sseu(osParameters->intel_context, &sseu))
575         {
576             MOS_OS_ASSERTMESSAGE("Failed to get sseu configuration.");
577             return MOS_STATUS_UNKNOWN;
578         };
579 
580         if (mos_hweight8(sseu.subslice_mask) > createOption.packed.SubSliceCount)
581         {
582             sseu.subslice_mask = mos_switch_off_n_bits(sseu.subslice_mask,
583                 mos_hweight8(sseu.subslice_mask) - createOption.packed.SubSliceCount);
584         }
585 
586         if (mos_set_context_param_sseu(osParameters->intel_context, sseu))
587         {
588             MOS_OS_ASSERTMESSAGE("Failed to set sseu configuration.");
589             return MOS_STATUS_UNKNOWN;
590         };
591     }
592 
593     MOS_GPU_NODE gpuNode = MOS_GPU_NODE_3D;
594     gpuNode = static_cast<MOS_GPU_NODE>(createOption.gpuNode);
595 
596     auto gpuContext = gpuContextMgr->CreateGpuContext(gpuNode, cmdBufMgr);
597     MOS_OS_CHK_NULL_RETURN(gpuContext);
598 
599     auto gpuContextSpecific = static_cast<GpuContextSpecificNext *>(gpuContext);
600     MOS_OS_CHK_NULL_RETURN(gpuContextSpecific);
601 
602     MOS_OS_CHK_STATUS_RETURN(gpuContextSpecific->Init(gpuContextMgr->GetOsContext(), streamState, &createOption));
603 
604     gpuContextHandle = gpuContextSpecific->GetGpuContextHandle();
605 
606     return MOS_STATUS_SUCCESS;
607 }
608 
GetAdapterBDF(PMOS_CONTEXT mosCtx,ADAPTER_BDF * adapterBDF)609 MOS_STATUS MosInterface::GetAdapterBDF(PMOS_CONTEXT mosCtx, ADAPTER_BDF *adapterBDF)
610 {
611     MOS_OS_FUNCTION_ENTER;
612 
613     drmDevicePtr device;
614 
615     MOS_OS_CHK_NULL_RETURN(mosCtx);
616     if (drmGetDevice(mosCtx->fd, &device) == 0)
617     {
618         adapterBDF->Bus      = device->businfo.pci->bus;
619         adapterBDF->Device   = device->businfo.pci->dev;
620         adapterBDF->Function = device->businfo.pci->func;
621         drmFreeDevice(&device);
622     }
623     else
624     {
625         adapterBDF->Data = 0;
626     }
627 
628     return MOS_STATUS_SUCCESS;
629 }
630 
DestroyGpuContext(MOS_STREAM_HANDLE streamState,GPU_CONTEXT_HANDLE gpuContext)631 MOS_STATUS MosInterface::DestroyGpuContext(
632     MOS_STREAM_HANDLE  streamState,
633     GPU_CONTEXT_HANDLE gpuContext)
634 {
635     MOS_OS_FUNCTION_ENTER;
636 
637     MOS_STATUS eStatus;
638 
639     eStatus = MOS_STATUS_SUCCESS;
640 
641     MOS_OS_CHK_NULL_RETURN(streamState);
642     MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
643     MOS_OS_ASSERT(gpuContext != MOS_GPU_CONTEXT_INVALID_HANDLE);
644 
645     auto gpuContextMgr = streamState->osDeviceContext->GetGpuContextMgr();
646     MOS_OS_CHK_NULL_RETURN(gpuContextMgr);
647     auto gpuContextInstance = gpuContextMgr->GetGpuContext(gpuContext);
648     MOS_OS_CHK_NULL_RETURN(gpuContextInstance);
649 
650     gpuContextMgr->DestroyGpuContext(gpuContextInstance);
651 
652     return MOS_STATUS_SUCCESS;
653 }
654 
SetGpuContext(MOS_STREAM_HANDLE streamState,GPU_CONTEXT_HANDLE gpuContext)655 MOS_STATUS MosInterface::SetGpuContext(
656     MOS_STREAM_HANDLE  streamState,
657     GPU_CONTEXT_HANDLE gpuContext)
658 {
659     MOS_OS_FUNCTION_ENTER;
660 
661     auto gpuContextMgr = streamState->osDeviceContext->GetGpuContextMgr();
662     MOS_OS_CHK_NULL_RETURN(gpuContextMgr);
663     auto gpuContextPtr = gpuContextMgr->GetGpuContext(gpuContext);
664     MOS_OS_CHK_NULL_RETURN(gpuContextPtr);
665 
666     streamState->currentGpuContextHandle = gpuContext;
667 
668     return MOS_STATUS_SUCCESS;
669 }
670 
GetGpuContextbyHandle(MOS_STREAM_HANDLE streamState,GPU_CONTEXT_HANDLE gpuContextHandle)671 void *MosInterface::GetGpuContextbyHandle(
672     MOS_STREAM_HANDLE  streamState,
673     GPU_CONTEXT_HANDLE gpuContextHandle)
674 {
675     if (!streamState || !streamState->osDeviceContext)
676     {
677         MOS_OS_ASSERTMESSAGE("Invalid nullptr");
678         return nullptr;
679     }
680 
681     auto gpuContextMgr = streamState->osDeviceContext->GetGpuContextMgr();
682     if (!gpuContextMgr)
683     {
684         MOS_OS_ASSERTMESSAGE("Invalid nullptr");
685         return nullptr;
686     }
687 
688     GpuContextNext *gpuContext = gpuContextMgr->GetGpuContext(gpuContextHandle);
689 
690     if (!gpuContext)
691     {
692         MOS_OS_ASSERTMESSAGE("Invalid nullptr");
693     }
694     return (void *)gpuContext;
695 }
696 
SetObjectCapture(PMOS_RESOURCE osResource)697 MOS_STATUS MosInterface:: SetObjectCapture(
698     PMOS_RESOURCE osResource)
699 {
700     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
701 
702     //---------------------------------------
703     MOS_OS_CHK_NULL_RETURN(osResource);
704     //---------------------------------------
705 
706     mos_bo_set_object_capture(osResource->bo);
707 
708     return eStatus;
709 }
710 
AddCommand(COMMAND_BUFFER_HANDLE cmdBuffer,const void * cmd,uint32_t cmdSize)711 MOS_STATUS MosInterface::AddCommand(
712     COMMAND_BUFFER_HANDLE cmdBuffer,
713     const void *cmd,
714     uint32_t cmdSize)
715 {
716     MOS_OS_FUNCTION_ENTER;
717 
718     uint32_t cmdSizeDwAligned = 0;
719 
720     MOS_OS_CHK_NULL_RETURN(cmdBuffer);
721     MOS_OS_CHK_NULL_RETURN(cmd);
722 
723     if (cmdSize == 0)
724     {
725         MOS_OS_ASSERTMESSAGE("Incorrect command size to add to command buffer.");
726         return MOS_STATUS_INVALID_PARAMETER;
727     }
728 
729     cmdSizeDwAligned = MOS_ALIGN_CEIL(cmdSize, sizeof(uint32_t));
730 
731     cmdBuffer->iOffset += cmdSizeDwAligned;
732     cmdBuffer->iRemaining -= cmdSizeDwAligned;
733 
734     if (cmdBuffer->iRemaining < 0)
735     {
736         cmdBuffer->iOffset -= cmdSizeDwAligned;
737         cmdBuffer->iRemaining += cmdSizeDwAligned;
738         MOS_OS_ASSERTMESSAGE("Unable to add command: remaining space = %d, command size = %d.",
739             cmdBuffer->iRemaining,
740             cmdSizeDwAligned);
741         return MOS_STATUS_UNKNOWN;
742     }
743 
744     MOS_OS_VERBOSEMESSAGE("The command was successfully added: remaining space = %d, buffer size = %d.",
745         cmdBuffer->iRemaining,
746         cmdBuffer->iOffset + cmdBuffer->iRemaining);
747 
748     MosUtilities::MosSecureMemcpy(cmdBuffer->pCmdPtr, cmdSize, cmd, cmdSize);
749     cmdBuffer->pCmdPtr += (cmdSizeDwAligned / sizeof(uint32_t));
750 
751     return MOS_STATUS_SUCCESS;
752 }
753 
754 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
DumpIndirectState(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer,MOS_GPU_NODE gpuNode,const char * filePathPrefix)755 MOS_STATUS MosInterface::DumpIndirectState(
756     MOS_STREAM_HANDLE     streamState,
757     COMMAND_BUFFER_HANDLE cmdBuffer,
758     MOS_GPU_NODE          gpuNode,
759     const char            *filePathPrefix)
760 {
761     MOS_OS_CHK_NULL_RETURN(filePathPrefix);
762 
763     if (MOS_GPU_NODE_COMPUTE == gpuNode || MOS_GPU_NODE_3D == gpuNode)
764     {
765         uint8_t *indirectState = nullptr;
766         uint32_t offset = 0;
767         uint32_t size = 0;
768         MosInterface::GetIndirectState(streamState, &indirectState, offset, size);
769 
770         if (indirectState)
771         {
772             std::stringstream ss;
773             uint32_t dwordCount = size / 4;
774             uint32_t *data = (uint32_t *)indirectState;
775 
776             for (uint32_t i = 0; i < dwordCount; ++i)
777             {
778                 if (0 == i % 4)
779                 {
780                     if (0 != i)
781                     {
782                         ss << std::endl;
783                     }
784                     ss << "#0    #0";
785                 }
786                 ss << "    " << std::hex << std::setw(8) << std::setfill('0') << data[i];
787             }
788 
789             std::stringstream fileName;
790             fileName << filePathPrefix << "_binding_table.txt";
791             std::fstream fs;
792             fs.open(fileName.str(), std::ios_base::out | std::ios_base::app);
793             fs << ss.str();
794             fs.close();
795         }
796         else
797         {
798             MOS_OS_NORMALMESSAGE("nullptr == indirectState");
799         }
800     }
801 
802     return MOS_STATUS_SUCCESS;
803 }
804 
DumpCommandBuffer(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer)805 MOS_STATUS MosInterface::DumpCommandBuffer(
806     MOS_STREAM_HANDLE     streamState,
807     COMMAND_BUFFER_HANDLE cmdBuffer)
808 {
809     MOS_OS_FUNCTION_ENTER;
810 
811     static uint32_t dwCommandBufferNumber = 0;
812     MOS_STATUS      eStatus               = MOS_STATUS_UNKNOWN;
813     char *          pOutputBuffer         = nullptr;
814     // Each hex value should have 9 chars.
815     uint32_t SIZE_OF_ONE_WORD = 9;
816     uint32_t dwBytesWritten   = 0;
817     uint32_t dwNumberOfDwords = 0;
818     uint32_t dwSizeToAllocate = 0;
819     char     sFileName[MOS_MAX_HLT_FILENAME_LEN] = {0};
820     // Maximum length of engine name is 6
821     char sEngName[6];
822     size_t nSizeFileNamePrefix   = 0;
823 
824     MOS_OS_CHK_NULL_RETURN(streamState);
825     MOS_OS_CHK_NULL_RETURN(cmdBuffer);
826 
827     // Set the name of the engine that is going to be used.
828     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
829     MOS_OS_CHK_NULL_RETURN(gpuContext);
830     MOS_GPU_NODE gpuNode = gpuContext->GetContextNode();
831     switch (gpuNode)
832     {
833     case MOS_GPU_NODE_VIDEO:
834     case MOS_GPU_NODE_VIDEO2:
835         MosUtilities::MosSecureStrcpy(sEngName, sizeof(sEngName), MOS_COMMAND_BUFFER_VIDEO_ENGINE);
836         break;
837     case MOS_GPU_NODE_COMPUTE:
838     case MOS_GPU_NODE_BLT:
839         MosUtilities::MosSecureStrcpy(sEngName, sizeof(sEngName), MOS_COMMAND_BUFFER_RENDER_ENGINE);
840         break;
841     case MOS_GPU_NODE_VE:
842         MosUtilities::MosSecureStrcpy(sEngName, sizeof(sEngName), MOS_COMMAND_BUFFER_VEBOX_ENGINE);
843         break;
844     case MOS_GPU_NODE_3D:
845         MosUtilities::MosSecureStrcpy(sEngName, sizeof(sEngName), MOS_COMMAND_BUFFER_RENDER_ENGINE);
846         break;
847     default:
848         MOS_OS_ASSERTMESSAGE("Unsupported GPU context.");
849         return eStatus;
850     }
851 
852     dwNumberOfDwords = cmdBuffer->iOffset / sizeof(uint32_t);
853 
854     dwSizeToAllocate =
855         dwNumberOfDwords * (SIZE_OF_ONE_WORD + 1)  // Add 1 byte for the space following each Dword.
856         + 3 * SIZE_OF_ONE_WORD;                    // For engine and platform names.
857 
858     // Alloc output buffer.
859     pOutputBuffer = (char *)MOS_AllocAndZeroMemory(dwSizeToAllocate);
860     MOS_OS_CHK_NULL_RETURN(pOutputBuffer);
861 
862     dwBytesWritten = MosUtilities::MosSecureStringPrint(
863         pOutputBuffer,
864         SIZE_OF_ONE_WORD * 3,
865         SIZE_OF_ONE_WORD * 3,
866         "Eng=%s ",
867         sEngName);
868 
869     if (streamState->dumpCommandBufferToFile)
870     {
871         MosUtilities::MosSecureMemcpy(sFileName, MOS_MAX_HLT_FILENAME_LEN, streamState->sDirName, MOS_MAX_HLT_FILENAME_LEN);
872 
873         nSizeFileNamePrefix = strnlen(sFileName, sizeof(sFileName));
874         MosUtilities::MosSecureStringPrint(
875             sFileName + nSizeFileNamePrefix,
876             sizeof(sFileName) - nSizeFileNamePrefix,
877             sizeof(sFileName) - nSizeFileNamePrefix,
878             "%c%s%c%s_%d.txt",
879             MOS_DIR_SEPERATOR,
880             MOS_COMMAND_BUFFER_OUT_DIR,
881             MOS_DIR_SEPERATOR,
882             MOS_COMMAND_BUFFER_OUT_FILE,
883             dwCommandBufferNumber);
884 
885         // Write the output buffer to file.
886         MOS_OS_CHK_STATUS_RETURN(MosUtilities::MosWriteFileFromPtr((const char *)sFileName, pOutputBuffer, dwBytesWritten));
887     }
888 
889     if (streamState->dumpCommandBufferAsMessages)
890     {
891         MOS_OS_NORMALMESSAGE(pOutputBuffer);
892     }
893 
894     MosUtilities::MosZeroMemory(pOutputBuffer, dwBytesWritten);
895     dwBytesWritten = 0;
896 
897     // Fill in the output buffer with the command buffer dwords.
898     for (uint32_t dwIndex = 0; dwIndex < dwNumberOfDwords; dwIndex++)
899     {
900         dwBytesWritten += MosUtilities::MosSecureStringPrint(
901             pOutputBuffer + dwBytesWritten,
902             SIZE_OF_ONE_WORD + 1,
903             SIZE_OF_ONE_WORD + 1,
904             "%.8x ",
905             cmdBuffer->pCmdBase[dwIndex]);
906 
907         if (dwBytesWritten % (SIZE_OF_ONE_WORD + 1) == 0)
908         {
909             if (streamState->dumpCommandBufferToFile)
910             {
911                 MOS_OS_CHK_STATUS_RETURN(MosUtilities::MosAppendFileFromPtr((const char *)sFileName, pOutputBuffer, dwBytesWritten));
912             }
913             if (streamState->dumpCommandBufferAsMessages)
914             {
915                 MOS_OS_NORMALMESSAGE(pOutputBuffer);
916             }
917 
918             MosUtilities::MosZeroMemory(pOutputBuffer, dwBytesWritten);
919             dwBytesWritten = 0;
920         }
921     }
922 
923     if (streamState->dumpCommandBufferToFile)
924     {
925         MOS_OS_CHK_STATUS_RETURN(MosUtilities::MosAppendFileFromPtr((const char *)sFileName, pOutputBuffer, dwBytesWritten));
926         MOS_OS_CHK_STATUS_RETURN(DumpIndirectState(streamState, cmdBuffer, gpuNode, sFileName));
927     }
928 
929     if (streamState->dumpCommandBufferAsMessages)
930     {
931         MOS_OS_NORMALMESSAGE(pOutputBuffer);
932     }
933 
934     dwCommandBufferNumber++;
935 
936     eStatus = MOS_STATUS_SUCCESS;
937 
938     return eStatus;
939 
940 }
941 #endif  // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
942 
GetCommandBuffer(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE & cmdBuffer,uint32_t pipeIdx)943 MOS_STATUS MosInterface::GetCommandBuffer(
944     MOS_STREAM_HANDLE      streamState,
945     COMMAND_BUFFER_HANDLE &cmdBuffer,
946     uint32_t               pipeIdx)
947 {
948     MOS_OS_FUNCTION_ENTER;
949 
950     MOS_OS_CHK_NULL_RETURN(streamState);
951 
952     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
953     MOS_OS_CHK_NULL_RETURN(gpuContext);
954 
955     return (gpuContext->GetCommandBuffer(cmdBuffer, pipeIdx));
956 }
957 
ReturnCommandBuffer(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer,uint32_t pipeIdx)958 MOS_STATUS MosInterface::ReturnCommandBuffer(
959     MOS_STREAM_HANDLE     streamState,
960     COMMAND_BUFFER_HANDLE cmdBuffer,
961     uint32_t              pipeIdx)
962 {
963     MOS_OS_FUNCTION_ENTER;
964 
965     MOS_OS_CHK_NULL_RETURN(streamState);
966 
967     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
968     MOS_OS_CHK_NULL_RETURN(gpuContext);
969 
970     (gpuContext->ReturnCommandBuffer(cmdBuffer, pipeIdx));
971 
972     return MOS_STATUS_SUCCESS;
973 }
974 
SubmitCommandBuffer(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer,bool nullRendering)975 MOS_STATUS MosInterface::SubmitCommandBuffer(
976     MOS_STREAM_HANDLE     streamState,
977     COMMAND_BUFFER_HANDLE cmdBuffer,
978     bool                  nullRendering)
979 {
980     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
981 
982     MOS_OS_CHK_NULL_RETURN(streamState);
983 
984     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
985     MOS_OS_CHK_NULL_RETURN(gpuContext);
986 
987     gpuContext->UpdatePriority(streamState->ctxPriority);
988 
989     return (gpuContext->SubmitCommandBuffer(streamState, cmdBuffer, nullRendering));
990 }
991 
ResetCommandBuffer(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer)992 MOS_STATUS MosInterface::ResetCommandBuffer(
993     MOS_STREAM_HANDLE     streamState,
994     COMMAND_BUFFER_HANDLE cmdBuffer)
995 {
996     MOS_OS_FUNCTION_ENTER;
997 
998     MOS_OS_CHK_NULL_RETURN(streamState);
999 
1000     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
1001     MOS_OS_CHK_NULL_RETURN(gpuContext);
1002 
1003     // Reset the explicitly provided cmd buffer, or reset GPU context states
1004     if (cmdBuffer)
1005     {
1006         MOS_OS_CHK_STATUS_RETURN(gpuContext->ResetCommandBuffer());
1007     }
1008     else
1009     {
1010         gpuContext->ResetGpuContextStatus();
1011     }
1012 
1013     return MOS_STATUS_SUCCESS;
1014 }
1015 
VerifyCommandBufferSize(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer,uint32_t requestedSize,uint32_t pipeIdx)1016 MOS_STATUS MosInterface::VerifyCommandBufferSize(
1017     MOS_STREAM_HANDLE     streamState,
1018     COMMAND_BUFFER_HANDLE cmdBuffer,
1019     uint32_t              requestedSize,
1020     uint32_t              pipeIdx)
1021 {
1022     MOS_OS_FUNCTION_ENTER;
1023 
1024     MOS_OS_CHK_NULL_RETURN(streamState);
1025 
1026     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
1027     MOS_OS_CHK_NULL_RETURN(gpuContext);
1028 
1029     return (gpuContext->VerifyCommandBufferSize(requestedSize));
1030 }
1031 
ResizeCommandBufferAndPatchList(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer,uint32_t requestedSize,uint32_t requestedPatchListSize,uint32_t pipeIdx)1032 MOS_STATUS MosInterface::ResizeCommandBufferAndPatchList(
1033     MOS_STREAM_HANDLE     streamState,
1034     COMMAND_BUFFER_HANDLE cmdBuffer,
1035     uint32_t              requestedSize,
1036     uint32_t              requestedPatchListSize,
1037     uint32_t              pipeIdx)
1038 {
1039     MOS_OS_FUNCTION_ENTER;
1040 
1041     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
1042     MOS_OS_CHK_NULL_RETURN(gpuContext);
1043 
1044     return (gpuContext->ResizeCommandBufferAndPatchList(requestedSize, requestedPatchListSize, pipeIdx));
1045 }
1046 
SetPatchEntry(MOS_STREAM_HANDLE streamState,PMOS_PATCH_ENTRY_PARAMS params)1047 MOS_STATUS MosInterface::SetPatchEntry(
1048     MOS_STREAM_HANDLE       streamState,
1049     PMOS_PATCH_ENTRY_PARAMS params)
1050 {
1051     MOS_OS_FUNCTION_ENTER;
1052 
1053     MOS_OS_CHK_NULL_RETURN(params);
1054     MOS_OS_CHK_NULL_RETURN(streamState);
1055 
1056     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
1057     MOS_OS_CHK_NULL_RETURN(gpuContext);
1058 
1059     MOS_OS_CHK_STATUS_RETURN(gpuContext->SetPatchEntry(streamState, params));
1060 
1061     return MOS_STATUS_SUCCESS;
1062 }
1063 
GetIndirectState(MOS_STREAM_HANDLE streamState,uint8_t ** indirectState,uint32_t & offset,uint32_t & size)1064 MOS_STATUS MosInterface::GetIndirectState(
1065     MOS_STREAM_HANDLE streamState,
1066     uint8_t **indirectState,
1067     uint32_t &offset,
1068     uint32_t &size)
1069 {
1070     MOS_OS_FUNCTION_ENTER;
1071 
1072     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
1073     MOS_OS_CHK_NULL_RETURN(gpuContext);
1074 
1075     MOS_OS_CHK_STATUS_RETURN(gpuContext->GetIndirectState(offset, size));
1076     if (indirectState)
1077     {
1078         MOS_OS_CHK_STATUS_RETURN(gpuContext->GetIndirectStatePointer(indirectState));
1079     }
1080 
1081     return MOS_STATUS_SUCCESS;
1082 }
1083 
SetupIndirectState(MOS_STREAM_HANDLE streamState,uint32_t size)1084 MOS_STATUS MosInterface::SetupIndirectState(
1085     MOS_STREAM_HANDLE     streamState,
1086     uint32_t              size)
1087 {
1088     MOS_OS_FUNCTION_ENTER;
1089 
1090     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
1091     MOS_OS_CHK_NULL_RETURN(gpuContext);
1092 
1093     MOS_OS_CHK_STATUS_RETURN(gpuContext->SetIndirectStateSize(size));
1094 
1095     return MOS_STATUS_SUCCESS;
1096 }
1097 
SetupAttributeVeBuffer(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer)1098 MOS_STATUS MosInterface::SetupAttributeVeBuffer(
1099     MOS_STREAM_HANDLE     streamState,
1100     COMMAND_BUFFER_HANDLE cmdBuffer)
1101 {
1102     MOS_OS_FUNCTION_ENTER;
1103 
1104     // no VE attribute buffer to setup
1105 
1106     return MOS_STATUS_SUCCESS;
1107 }
1108 
GetAttributeVeBuffer(COMMAND_BUFFER_HANDLE cmdBuffer)1109 MOS_CMD_BUF_ATTRI_VE *MosInterface::GetAttributeVeBuffer(
1110         COMMAND_BUFFER_HANDLE cmdBuffer)
1111 {
1112     MOS_OS_FUNCTION_ENTER;
1113 
1114     // no VE attribute buffer to get
1115     return nullptr;
1116 }
1117 
1118 static GMM_RESOURCE_USAGE_TYPE GmmResourceUsage[MOS_HW_RESOURCE_DEF_MAX] =
1119 {
1120     //
1121     // CODEC USAGES
1122     //
1123     GMM_RESOURCE_USAGE_BEGIN_CODEC,
1124     GMM_RESOURCE_USAGE_PRE_DEBLOCKING_CODEC,
1125     GMM_RESOURCE_USAGE_PRE_DEBLOCKING_CODEC_PARTIALENCSURFACE,
1126     GMM_RESOURCE_USAGE_POST_DEBLOCKING_CODEC,
1127     GMM_RESOURCE_USAGE_ORIGINAL_UNCOMPRESSED_PICTURE_ENCODE,
1128     GMM_RESOURCE_USAGE_ORIGINAL_UNCOMPRESSED_PICTURE_DECODE,
1129     GMM_RESOURCE_USAGE_STREAMOUT_DATA_CODEC,
1130     GMM_RESOURCE_USAGE_INTRA_ROWSTORE_SCRATCH_BUFFER_CODEC,
1131     GMM_RESOURCE_USAGE_DEBLOCKINGFILTER_ROWSTORE_SCRATCH_BUFFER_CODEC,
1132     GMM_RESOURCE_USAGE_REFERENCE_PICTURE_CODEC,
1133     GMM_RESOURCE_USAGE_MACROBLOCK_STATUS_BUFFER_CODEC,
1134     GMM_RESOURCE_USAGE_MFX_INDIRECT_BITSTREAM_OBJECT_DECODE,
1135     GMM_RESOURCE_USAGE_MFX_INDIRECT_MV_OBJECT_CODEC,
1136     GMM_RESOURCE_USAGE_MFD_INDIRECT_IT_COEF_OBJECT_DECODE,
1137     GMM_RESOURCE_USAGE_MFC_INDIRECT_PAKBASE_OBJECT_CODEC,
1138     GMM_RESOURCE_USAGE_BSDMPC_ROWSTORE_SCRATCH_BUFFER_CODEC,
1139     GMM_RESOURCE_USAGE_MPR_ROWSTORE_SCRATCH_BUFFER_CODEC,
1140     GMM_RESOURCE_USAGE_BITPLANE_READ_CODEC,
1141     GMM_RESOURCE_USAGE_DIRECTMV_BUFFER_CODEC,
1142     GMM_RESOURCE_USAGE_SURFACE_CURR_ENCODE,
1143     GMM_RESOURCE_USAGE_SURFACE_REF_ENCODE,
1144     GMM_RESOURCE_USAGE_SURFACE_MV_DATA_ENCODE,
1145     GMM_RESOURCE_USAGE_SURFACE_HME_DOWNSAMPLED_ENCODE,
1146     GMM_RESOURCE_USAGE_SURFACE_HME_DOWNSAMPLED_ENCODE_FF,
1147     GMM_RESOURCE_USAGE_SURFACE_HME_DOWNSAMPLED_ENCODE_DST,
1148     GMM_RESOURCE_USAGE_SURFACE_ME_DISTORTION_ENCODE,
1149     GMM_RESOURCE_USAGE_SURFACE_BRC_ME_DISTORTION_ENCODE,
1150     GMM_RESOURCE_USAGE_PAK_OBJECT_ENCODE,
1151     GMM_RESOURCE_USAGE_SURFACE_FLATNESS_CHECK_ENCODE,
1152     GMM_RESOURCE_USAGE_SURFACE_MBENC_CURBE_ENCODE,
1153     GMM_RESOURCE_USAGE_SURFACE_MAD_ENCODE,
1154     GMM_RESOURCE_USAGE_VP8_BLOCK_MODE_COST_ENCODE,
1155     GMM_RESOURCE_USAGE_VP8_MB_MODE_COST_ENCODE,
1156     GMM_RESOURCE_USAGE_VP8_MBENC_OUTPUT_ENCODE,
1157     GMM_RESOURCE_USAGE_VP8_HISTOGRAM_ENCODE,
1158     GMM_RESOURCE_USAGE_VP8_L3_LLC_ENCODE,
1159     GMM_RESOURCE_USAGE_MFX_STANDALONE_DEBLOCKING_CODEC,
1160     GMM_RESOURCE_USAGE_HCP_MD_CODEC,
1161     GMM_RESOURCE_USAGE_HCP_SAO_CODEC,
1162     GMM_RESOURCE_USAGE_HCP_MV_CODEC,
1163     GMM_RESOURCE_USAGE_HCP_STATUS_ERROR_CODEC,
1164     GMM_RESOURCE_USAGE_HCP_LCU_ILDB_STREAMOUT_CODEC,
1165     GMM_RESOURCE_USAGE_VP9_PROBABILITY_BUFFER_CODEC,
1166     GMM_RESOURCE_USAGE_VP9_SEGMENT_ID_BUFFER_CODEC,
1167     GMM_RESOURCE_USAGE_VP9_HVD_ROWSTORE_BUFFER_CODEC,
1168     GMM_RESOURCE_USAGE_MBDISABLE_SKIPMAP_CODEC,
1169     GMM_RESOURCE_USAGE_VDENC_ROW_STORE_BUFFER_CODEC,
1170     GMM_RESOURCE_USAGE_VDENC_STREAMIN_CODEC,
1171     GMM_RESOURCE_USAGE_SURFACE_MB_QP_CODEC,
1172     GMM_RESOURCE_USAGE_MACROBLOCK_ILDB_STREAM_OUT_BUFFER_CODEC,
1173     GMM_RESOURCE_USAGE_SSE_SRC_PIXEL_ROW_STORE_BUFFER_CODEC,
1174     GMM_RESOURCE_USAGE_SLICE_STATE_STREAM_OUT_BUFFER_CODEC,
1175     GMM_RESOURCE_USAGE_CABAC_SYNTAX_STREAM_OUT_BUFFER_CODEC,
1176     GMM_RESOURCE_USAGE_PRED_COL_STORE_BUFFER_CODEC,
1177     GMM_RESOURCE_USAGE_SURFACE_PAK_IMAGESTATE_ENCODE,
1178     GMM_RESOURCE_USAGE_SURFACE_MBENC_BRC_ENCODE,
1179     GMM_RESOURCE_USAGE_SURFACE_MB_BRC_CONST_ENCODE,
1180     GMM_RESOURCE_USAGE_SURFACE_BRC_MB_QP_ENCODE,
1181     GMM_RESOURCE_USAGE_SURFACE_BRC_ROI_ENCODE,
1182     GMM_RESOURCE_USAGE_SURFACE_SLICE_MAP_ENCODE,
1183     GMM_RESOURCE_USAGE_SURFACE_WP_DOWNSAMPLED_ENCODE,
1184     GMM_RESOURCE_USAGE_SURFACE_VDENC_IMAGESTATE_ENCODE,
1185     GMM_RESOURCE_USAGE_SURFACE_UNCACHED,
1186     GMM_RESOURCE_USAGE_SURFACE_ELLC_ONLY,
1187     GMM_RESOURCE_USAGE_SURFACE_ELLC_LLC_ONLY,
1188     GMM_RESOURCE_USAGE_SURFACE_ELLC_LLC_L3,
1189     GMM_RESOURCE_USAGE_SURFACE_BRC_HISTORY_ENCODE,
1190     GMM_RESOURCE_USAGE_SURFACE_SOFTWARE_SCOREBOARD_ENCODE,
1191     GMM_RESOURCE_USAGE_SURFACE_ME_MV_DATA_ENCODE,
1192     GMM_RESOURCE_USAGE_SURFACE_MV_DISTORTION_ENCODE,
1193     GMM_RESOURCE_USAGE_SURFACE_4XME_DISTORTION_ENCODE,
1194     GMM_RESOURCE_USAGE_SURFACE_INTRA_DISTORTION_ENCODE,
1195     GMM_RESOURCE_USAGE_MB_STATS_ENCODE,
1196     GMM_RESOURCE_USAGE_SURFACE_PAK_STATS_ENCODE,
1197     GMM_RESOURCE_USAGE_SURFACE_PIC_STATE_READ_ENCODE,
1198     GMM_RESOURCE_USAGE_SURFACE_PIC_STATE_WRITE_ENCODE,
1199     GMM_RESOURCE_USAGE_SURFACE_COMBINED_ENC_ENCODE,
1200     GMM_RESOURCE_USAGE_SURFACE_BRC_CONSTANT_DATA_ENCODE,
1201     GMM_RESOURCE_USAGE_SURFACE_INTERMEDIATE_CU_RECORD_SURFACE_ENCODE,
1202     GMM_RESOURCE_USAGE_SURFACE_SCRATCH_ENCODE,
1203     GMM_RESOURCE_USAGE_SURFACE_LCU_LEVEL_DATA_ENCODE,
1204     GMM_RESOURCE_USAGE_SURFACE_ENC_HISTORY_INPUT_ENCODE,
1205     GMM_RESOURCE_USAGE_SURFACE_ENC_HISTORY_OUTPUT_ENCODE,
1206     GMM_RESOURCE_USAGE_SURFACE_DEBUG_ENCODE,
1207     GMM_RESOURCE_USAGE_SURFACE_ENC_CONSTANT_TABLE_ENCODE,
1208     GMM_RESOURCE_USAGE_SURFACE_ENC_CU_RECORD_ENCODE,
1209     GMM_RESOURCE_USAGE_SURFACE_ENC_MV_TEMPORAL_BUFFER_ENCODE,
1210     GMM_RESOURCE_USAGE_SURFACE_ENC_CU_PACKET_FOR_PAK_ENCODE,
1211     GMM_RESOURCE_USAGE_SURFACE_ENC_BCOMBINED1_ENCODE,
1212     GMM_RESOURCE_USAGE_SURFACE_ENC_BCOMBINED2_ENCODE,
1213     GMM_RESOURCE_USAGE_FRAME_STATS_STREAMOUT_DATA_CODEC,
1214     GMM_RESOURCE_USAGE_DEBLOCKINGFILTER_ROWSTORE_TILE_LINE_BUFFER_CODEC,
1215     GMM_RESOURCE_USAGE_DEBLOCKINGFILTER_ROWSTORE_TILE_COLUMN_BUFFER_CODEC,
1216     GMM_RESOURCE_USAGE_HCP_MD_TILE_LINE_CODEC,
1217     GMM_RESOURCE_USAGE_HCP_MD_TILE_COLUMN_CODEC,
1218     GMM_RESOURCE_USAGE_HCP_SAO_TILE_LINE_CODEC,
1219     GMM_RESOURCE_USAGE_HCP_SAO_TILE_COLUMN_CODEC,
1220     GMM_RESOURCE_USAGE_VP9_PROBABILITY_COUNTER_BUFFER_CODEC,
1221     GMM_RESOURCE_USAGE_HUC_VIRTUAL_ADDR_REGION_BUFFER_CODEC,
1222     GMM_RESOURCE_USAGE_SIZE_STREAMOUT_CODEC,
1223     GMM_RESOURCE_USAGE_COMPRESSED_HEADER_BUFFER_CODEC,
1224     GMM_RESOURCE_USAGE_PROBABILITY_DELTA_BUFFER_CODEC,
1225     GMM_RESOURCE_USAGE_TILE_RECORD_BUFFER_CODEC,
1226     GMM_RESOURCE_USAGE_TILE_SIZE_STAS_BUFFER_CODEC,
1227     GMM_RESOURCE_USAGE_END_CODEC,
1228 
1229     //
1230     // CM USAGES
1231     //
1232     CM_RESOURCE_USAGE_SurfaceState,
1233     CM_RESOURCE_USAGE_StateHeap,
1234     CM_RESOURCE_USAGE_NO_L3_SurfaceState,
1235     CM_RESOURCE_USAGE_NO_LLC_ELLC_SurfaceState,
1236     CM_RESOURCE_USAGE_NO_LLC_SurfaceState,
1237     CM_RESOURCE_USAGE_NO_ELLC_SurfaceState,
1238     CM_RESOURCE_USAGE_NO_LLC_L3_SurfaceState,
1239     CM_RESOURCE_USAGE_NO_ELLC_L3_SurfaceState,
1240     CM_RESOURCE_USAGE_NO_CACHE_SurfaceState,
1241     CM_RESOURCE_USAGE_L1_Enabled_SurfaceState,
1242 
1243     //
1244     // MP USAGES
1245     //
1246     MP_RESOURCE_USAGE_BEGIN,
1247     MP_RESOURCE_USAGE_DEFAULT,
1248     MP_RESOURCE_USAGE_DEFAULT_FF,
1249     MP_RESOURCE_USAGE_DEFAULT_RCS,
1250     MP_RESOURCE_USAGE_SurfaceState,
1251     MP_RESOURCE_USAGE_SurfaceState_FF,
1252     MP_RESOURCE_USAGE_SurfaceState_RCS,
1253     MP_RESOURCE_USAGE_AGE3_SurfaceState,
1254     MP_RESOURCE_USAGE_EDRAM_SurfaceState,
1255     MP_RESOURCE_USAGE_EDRAM_AGE3_SurfaceState,
1256     MP_RESOURCE_USAGE_No_L3_SurfaceState,
1257     MP_RESOURCE_USAGE_No_LLC_L3_SurfaceState,
1258     MP_RESOURCE_USAGE_No_LLC_L3_AGE_SurfaceState,
1259     MP_RESOURCE_USAGE_No_LLC_eLLC_L3_AGE_SurfaceState,
1260     MP_RESOURCE_USAGE_PartialEnc_No_LLC_L3_AGE_SurfaceState,
1261     MP_RESOURCE_USAGE_END,
1262 
1263     // MHW - SFC
1264     MHW_RESOURCE_USAGE_Sfc_CurrentOutputSurface,                    //!< SFC output surface
1265     MHW_RESOURCE_USAGE_Sfc_CurrentOutputSurface_PartialEncSurface,  //!< SFC output surface for partial secure surfaces
1266     MHW_RESOURCE_USAGE_Sfc_AvsLineBufferSurface,                    //!< SFC AVS Line buffer Surface
1267     MHW_RESOURCE_USAGE_Sfc_IefLineBufferSurface,                    //!< SFC IEF Line buffer Surface
1268 
1269     // PAT Media Usages
1270     GMM_RESOURCE_USAGE_MEDIA_BATCH_BUFFERS,
1271     // DECODE
1272     GMM_RESOURCE_USAGE_DECODE_INPUT_BITSTREAM,
1273     GMM_RESOURCE_USAGE_DECODE_INPUT_REFERENCE,
1274     GMM_RESOURCE_USAGE_DECODE_INTERNAL_READ,
1275     GMM_RESOURCE_USAGE_DECODE_INTERNAL_WRITE,
1276     GMM_RESOURCE_USAGE_DECODE_INTERNAL_READ_WRITE_CACHE,
1277     GMM_RESOURCE_USAGE_DECODE_INTERNAL_READ_WRITE_NOCACHE,
1278     GMM_RESOURCE_USAGE_DECODE_OUTPUT_PICTURE,
1279     GMM_RESOURCE_USAGE_DECODE_OUTPUT_STATISTICS_WRITE,
1280     GMM_RESOURCE_USAGE_DECODE_OUTPUT_STATISTICS_READ_WRITE,
1281     // ENCODE
1282     GMM_RESOURCE_USAGE_ENCODE_INPUT_RAW,
1283     GMM_RESOURCE_USAGE_ENCODE_INPUT_RECON,
1284     GMM_RESOURCE_USAGE_ENCODE_INTERNAL_READ,
1285     GMM_RESOURCE_USAGE_ENCODE_INTERNAL_WRITE,
1286     GMM_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE,
1287     GMM_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_NOCACHE,
1288     GMM_RESOURCE_USAGE_ENCODE_EXTERNAL_READ,
1289     GMM_RESOURCE_USAGE_ENCODE_OUTPUT_PICTURE,
1290     GMM_RESOURCE_USAGE_ENCODE_OUTPUT_BITSTREAM,
1291     GMM_RESOURCE_USAGE_ENCODE_OUTPUT_STATISTICS_WRITE,
1292     GMM_RESOURCE_USAGE_ENCODE_OUTPUT_STATISTICS_READ_WRITE,
1293     // VP
1294     GMM_RESOURCE_USAGE_VP_INPUT_PICTURE_FF,
1295     GMM_RESOURCE_USAGE_VP_INPUT_REFERENCE_FF,
1296     GMM_RESOURCE_USAGE_VP_INTERNAL_READ_FF,
1297     GMM_RESOURCE_USAGE_VP_INTERNAL_WRITE_FF,
1298     GMM_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_FF,
1299     GMM_RESOURCE_USAGE_VP_OUTPUT_PICTURE_FF,
1300     GMM_RESOURCE_USAGE_VP_INPUT_PICTURE_RENDER,
1301     GMM_RESOURCE_USAGE_VP_INPUT_REFERENCE_RENDER,
1302     GMM_RESOURCE_USAGE_VP_INTERNAL_READ_RENDER,
1303     GMM_RESOURCE_USAGE_VP_INTERNAL_WRITE_RENDER,
1304     GMM_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER,
1305     GMM_RESOURCE_USAGE_VP_OUTPUT_PICTURE_RENDER,
1306     // CP
1307     GMM_RESOURCE_USAGE_CP_EXTERNAL_READ,
1308     GMM_RESOURCE_USAGE_CP_INTERNAL_WRITE,
1309 };
1310 
GetGmmResourceUsageType(MOS_HW_RESOURCE_DEF resUsage)1311 GMM_RESOURCE_USAGE_TYPE MosInterface::GetGmmResourceUsageType(
1312     MOS_HW_RESOURCE_DEF resUsage)
1313 {
1314     if (resUsage >= (sizeof(GmmResourceUsage) / sizeof(GmmResourceUsage[0])))
1315         return GMM_RESOURCE_USAGE_UNKNOWN;
1316 
1317     return GmmResourceUsage[resUsage];
1318 }
1319 
GetGmmCachePolicyMemoryObject(GMM_CLIENT_CONTEXT * gmmClientContext,GMM_RESOURCE_USAGE_TYPE gmmUsage)1320 MEMORY_OBJECT_CONTROL_STATE MosInterface::GetGmmCachePolicyMemoryObject(
1321     GMM_CLIENT_CONTEXT      *gmmClientContext,
1322     GMM_RESOURCE_USAGE_TYPE gmmUsage)
1323 {
1324     MOS_OS_FUNCTION_ENTER;
1325     if (!gmmClientContext)
1326     {
1327         return {0};
1328     }
1329 
1330     if (gmmClientContext->GetCachePolicyElement(gmmUsage).Initialized)
1331     {
1332         return gmmClientContext->CachePolicyGetMemoryObject(nullptr, gmmUsage);
1333     }
1334     else if (gmmClientContext->GetCachePolicyElement(MP_RESOURCE_USAGE_DEFAULT).Initialized)
1335     {
1336         MOS_OS_NORMALMESSAGE("Cache is not initialized for GMM_RESOURCE_USAGE_TYPE %d, use MP_RESOURCE_USAGE_DEFAULT", gmmUsage);
1337         return gmmClientContext->CachePolicyGetMemoryObject(nullptr, MP_RESOURCE_USAGE_DEFAULT);
1338     }
1339     else
1340     {
1341         MOS_OS_NORMALMESSAGE("Cache is not initialized for GMM_RESOURCE_USAGE_TYPE %d", gmmUsage);
1342         return gmmClientContext->GetCachePolicyUsage()[GMM_RESOURCE_USAGE_UNKNOWN].MemoryObjectOverride;
1343     }
1344 }
1345 
GetCachePolicyMemoryObject(GMM_CLIENT_CONTEXT * gmmClientContext,MOS_HW_RESOURCE_DEF mosUsage)1346 MEMORY_OBJECT_CONTROL_STATE MosInterface::GetCachePolicyMemoryObject(
1347     GMM_CLIENT_CONTEXT *gmmClientContext,
1348     MOS_HW_RESOURCE_DEF mosUsage)
1349 {
1350     MOS_OS_FUNCTION_ENTER;
1351 
1352     GMM_RESOURCE_USAGE_TYPE usage = GmmResourceUsage[mosUsage];
1353     return MosInterface::GetGmmCachePolicyMemoryObject(gmmClientContext, usage);
1354 }
1355 
GetCachePolicyL1Config(MOS_STREAM_HANDLE streamState,MOS_HW_RESOURCE_DEF mosUsage)1356 uint8_t MosInterface::GetCachePolicyL1Config(
1357     MOS_STREAM_HANDLE streamState,
1358     MOS_HW_RESOURCE_DEF mosUsage)
1359 {
1360     MOS_OS_FUNCTION_ENTER;
1361     return 0;
1362 }
1363 
GetReservedFromResource(MOS_RESOURCE_HANDLE resource,uint32_t & val)1364 MOS_STATUS MosInterface::GetReservedFromResource(MOS_RESOURCE_HANDLE resource, uint32_t &val)
1365 {
1366     return MOS_STATUS_UNIMPLEMENTED;
1367 }
1368 
GetReservedFromStream(MOS_STREAM_HANDLE stream,uint32_t & val)1369 MOS_STATUS MosInterface::GetReservedFromStream(MOS_STREAM_HANDLE stream, uint32_t &val)
1370 {
1371     return MOS_STATUS_UNIMPLEMENTED;
1372 }
1373 
GetReservedFromDevice(MOS_DEVICE_HANDLE device,uint32_t & val)1374 MOS_STATUS MosInterface::GetReservedFromDevice(MOS_DEVICE_HANDLE device, uint32_t &val)
1375 {
1376     MOS_OS_CHK_NULL_RETURN(device);
1377     OsContextSpecificNext *osDevice = dynamic_cast<OsContextSpecificNext*>(device);
1378     MOS_OS_CHK_NULL_RETURN(osDevice);
1379     if (osDevice->GetBufMgr()->get_reserved)
1380     {
1381         val = *(osDevice->GetBufMgr()->get_reserved);
1382         return MOS_STATUS_SUCCESS;
1383     }
1384     else
1385     {
1386         return MOS_STATUS_UNIMPLEMENTED;
1387     }
1388 }
1389 
GetperStreamParameters(MOS_STREAM_HANDLE stream,void ** perStreamParameters)1390 MOS_STATUS MosInterface::GetperStreamParameters(MOS_STREAM_HANDLE stream, void **perStreamParameters)
1391 {
1392     MOS_OS_CHK_NULL_RETURN(stream);
1393     *perStreamParameters = (void*)stream->perStreamParameters;
1394     return MOS_STATUS_SUCCESS;
1395 }
1396 
ConvertResourceFromDdi(OsSpecificRes osResource,MOS_RESOURCE_HANDLE & resource,uint32_t firstArraySlice,uint32_t mipSlice)1397 MOS_STATUS MosInterface::ConvertResourceFromDdi(
1398     OsSpecificRes osResource,
1399     MOS_RESOURCE_HANDLE &resource,
1400     uint32_t firstArraySlice,
1401     uint32_t mipSlice)
1402 {
1403     MOS_OS_FUNCTION_ENTER;
1404 
1405     if (firstArraySlice == OS_SPECIFIC_RESOURCE_INVALID || firstArraySlice >= OS_SPECIFIC_RESOURCE_MAX)
1406     {
1407         MOS_OS_ASSERTMESSAGE("Cannot Convert Resource From Ddi, invalid ddi resource type!");
1408         return MOS_STATUS_INVALID_PARAMETER;
1409     }
1410 
1411     MOS_OS_CHK_NULL_RETURN(osResource);
1412     MOS_OS_CHK_NULL_RETURN(resource);
1413 
1414     if (firstArraySlice == OS_SPECIFIC_RESOURCE_SURFACE)
1415     {
1416         DDI_MEDIA_SURFACE *mediaSurface = (DDI_MEDIA_SURFACE *)osResource;
1417 
1418         switch (mediaSurface->format)
1419         {
1420         case Media_Format_NV12:
1421             resource->Format = Format_NV12;
1422             break;
1423         case Media_Format_NV21:
1424             resource->Format = Format_NV21;
1425             break;
1426         case Media_Format_YUY2:
1427             resource->Format = Format_YUY2;
1428             break;
1429         case Media_Format_X8R8G8B8:
1430             resource->Format = Format_X8R8G8B8;
1431             break;
1432         case Media_Format_X8B8G8R8:
1433             resource->Format = Format_X8B8G8R8;
1434             break;
1435         case Media_Format_A8B8G8R8:
1436         case Media_Format_R8G8B8A8:
1437             resource->Format = Format_A8B8G8R8;
1438             break;
1439         case Media_Format_A8R8G8B8:
1440             resource->Format = Format_A8R8G8B8;
1441             break;
1442         case Media_Format_R5G6B5:
1443             resource->Format = Format_R5G6B5;
1444             break;
1445         case Media_Format_R8G8B8:
1446             resource->Format = Format_R8G8B8;
1447             break;
1448         case Media_Format_RGBP:
1449             resource->Format = Format_RGBP;
1450             break;
1451         case Media_Format_BGRP:
1452             resource->Format = Format_BGRP;
1453             break;
1454         case Media_Format_444P:
1455             resource->Format = Format_444P;
1456             break;
1457         case Media_Format_411P:
1458             resource->Format = Format_411P;
1459             break;
1460         case Media_Format_IMC3:
1461             resource->Format = Format_IMC3;
1462             break;
1463         case Media_Format_400P:
1464             resource->Format = Format_400P;
1465             break;
1466         case Media_Format_422H:
1467             resource->Format = Format_422H;
1468             break;
1469         case Media_Format_422V:
1470             resource->Format = Format_422V;
1471             break;
1472         case Media_Format_Buffer:
1473             resource->Format = Format_Any;
1474         case Media_Format_P010:
1475             resource->Format = Format_P010;
1476             break;
1477         case Media_Format_P012:
1478         case Media_Format_P016:
1479             resource->Format = Format_P016;
1480             break;
1481         case Media_Format_Y210:
1482             resource->Format = Format_Y210;
1483             break;
1484 #if VA_CHECK_VERSION(1, 9, 0)
1485         case Media_Format_Y212:
1486 #endif
1487         case Media_Format_Y216:
1488             resource->Format = Format_Y216;
1489             break;
1490         case Media_Format_AYUV:
1491 #if VA_CHECK_VERSION(1, 13, 0)
1492         case Media_Format_XYUV:
1493 #endif
1494             resource->Format = Format_AYUV;
1495             break;
1496         case Media_Format_Y410:
1497             resource->Format = Format_Y410;
1498             break;
1499 #if VA_CHECK_VERSION(1, 9, 0)
1500         case Media_Format_Y412:
1501 #endif
1502         case Media_Format_Y416:
1503             resource->Format = Format_Y416;
1504             break;
1505         case Media_Format_Y8:
1506             resource->Format = Format_Y8;
1507             break;
1508         case Media_Format_Y16S:
1509             resource->Format = Format_Y16S;
1510             break;
1511         case Media_Format_Y16U:
1512             resource->Format = Format_Y16U;
1513             break;
1514         case Media_Format_R10G10B10A2:
1515         case Media_Format_R10G10B10X2:
1516             resource->Format = Format_R10G10B10A2;
1517             break;
1518         case Media_Format_B10G10R10A2:
1519         case Media_Format_B10G10R10X2:
1520             resource->Format = Format_B10G10R10A2;
1521             break;
1522         case Media_Format_UYVY:
1523             resource->Format = Format_UYVY;
1524             break;
1525         case Media_Format_VYUY:
1526             resource->Format = Format_VYUY;
1527             break;
1528         case Media_Format_YVYU:
1529             resource->Format = Format_YVYU;
1530             break;
1531         case Media_Format_A16R16G16B16:
1532             resource->Format = Format_A16R16G16B16;
1533             break;
1534         case Media_Format_A16B16G16R16:
1535             resource->Format = Format_A16B16G16R16;
1536             break;
1537         default:
1538             MOS_OS_ASSERTMESSAGE("MOS: unsupported media format for surface.");
1539             break;
1540         }
1541         resource->iWidth   = mediaSurface->iWidth;
1542         resource->iHeight  = mediaSurface->iHeight;
1543         resource->iPitch   = mediaSurface->iPitch;
1544         resource->iCount   = mediaSurface->iRefCount;
1545         resource->isTiled  = mediaSurface->isTiled;
1546         resource->TileType = LinuxToMosTileType(mediaSurface->TileType);
1547         resource->bo       = mediaSurface->bo;
1548         resource->name     = mediaSurface->name;
1549 
1550         resource->ppCurrentFrameSemaphore   = &mediaSurface->pCurrentFrameSemaphore;
1551         resource->ppReferenceFrameSemaphore = &mediaSurface->pReferenceFrameSemaphore;
1552         resource->bSemInitialized           = false;
1553         resource->bMapped                   = false;
1554 
1555         if (mediaSurface->bMapped == true)
1556         {
1557             resource->pData = mediaSurface->pData;
1558         }
1559         else
1560         {
1561             resource->pData = nullptr;
1562         }
1563         resource->pGmmResInfo  = mediaSurface->pGmmResourceInfo;
1564         resource->dwGfxAddress = 0;
1565     }
1566     else if (firstArraySlice == OS_SPECIFIC_RESOURCE_BUFFER)
1567     {
1568         DDI_MEDIA_BUFFER *mediaBuffer = (DDI_MEDIA_BUFFER *)osResource;
1569         switch (mediaBuffer->format)
1570         {
1571         case Media_Format_Buffer:
1572             resource->Format  = Format_Buffer;
1573             resource->iWidth  = mediaBuffer->iSize;
1574             resource->iHeight = 1;
1575             resource->iPitch  = mediaBuffer->iSize;
1576             break;
1577         case Media_Format_Perf_Buffer:
1578             resource->Format  = Format_Buffer;
1579             resource->iWidth  = mediaBuffer->iSize;
1580             resource->iHeight = 1;
1581             resource->iPitch  = mediaBuffer->iSize;
1582             break;
1583         case Media_Format_2DBuffer:
1584             resource->Format  = Format_Buffer_2D;
1585             resource->iWidth  = mediaBuffer->uiWidth;
1586             resource->iHeight = mediaBuffer->uiHeight;
1587             resource->iPitch  = mediaBuffer->uiPitch;
1588             break;
1589         case Media_Format_CPU:
1590             return MOS_STATUS_SUCCESS;
1591         default:
1592             resource->iWidth  = mediaBuffer->iSize;
1593             resource->iHeight = 1;
1594             resource->iPitch  = mediaBuffer->iSize;
1595             MOS_OS_ASSERTMESSAGE("MOS: unsupported media format for surface.");
1596             break;
1597         }
1598         resource->iCount   = mediaBuffer->iRefCount;
1599         resource->isTiled  = 0;
1600         resource->TileType = LinuxToMosTileType(mediaBuffer->TileType);
1601         resource->bo       = mediaBuffer->bo;
1602         resource->name     = mediaBuffer->name;
1603         resource->bMapped  = false;
1604 
1605         if (mediaBuffer->bMapped == true)
1606         {
1607             resource->pData = mediaBuffer->pData;
1608         }
1609         else
1610         {
1611             resource->pData = nullptr;
1612         }
1613         resource->dwGfxAddress = 0;
1614         resource->pGmmResInfo  = mediaBuffer->pGmmResourceInfo;
1615     }
1616 
1617     resource->bConvertedFromDDIResource     = true;
1618     resource->memObjCtrlState.DwordValue    = 0;
1619     resource->mocsMosResUsageType           = MOS_CODEC_RESOURCE_USAGE_BEGIN_CODEC;
1620 
1621     return MOS_STATUS_SUCCESS;
1622 }
1623 
CreateOsSpecificResourceInfo(OsSpecificRes resource,bool isInternal)1624 MOS_STATUS MosInterface::CreateOsSpecificResourceInfo(OsSpecificRes resource, bool isInternal)
1625 {
1626     MOS_OS_FUNCTION_ENTER;
1627 
1628     // No OsSpecificResourceInfo in Linux
1629 
1630     return MOS_STATUS_SUCCESS;
1631 }
1632 
DestroySpecificResourceInfo(OsSpecificRes resource)1633 MOS_STATUS MosInterface::DestroySpecificResourceInfo(OsSpecificRes resource)
1634 {
1635     MOS_OS_FUNCTION_ENTER;
1636 
1637     // No OsSpecificResourceInfo in Linux
1638 
1639     return MOS_STATUS_SUCCESS;
1640 }
1641 
AllocateResource(MOS_STREAM_HANDLE streamState,PMOS_ALLOC_GFXRES_PARAMS params,MOS_RESOURCE_HANDLE & resource,const char * functionName,const char * filename,int32_t line)1642 MOS_STATUS MosInterface::AllocateResource(
1643     MOS_STREAM_HANDLE        streamState,
1644     PMOS_ALLOC_GFXRES_PARAMS params,
1645     MOS_RESOURCE_HANDLE      &resource
1646 #if MOS_MESSAGES_ENABLED
1647     ,
1648     const char              *functionName,
1649     const char              *filename,
1650     int32_t                 line
1651 #endif
1652 )
1653 {
1654     MOS_STATUS estatus = MOS_STATUS_SUCCESS;
1655     MOS_OS_FUNCTION_ENTER;
1656 
1657     MOS_OS_CHK_NULL_RETURN(resource);
1658     MOS_OS_CHK_NULL_RETURN(streamState);
1659     MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
1660 
1661 #if (_DEBUG || _RELEASE_INTERNAL)
1662     if (MosSimulateOsApiFail(OS_FAIL_ALLOC_GFX_RES, __FUNCTION__, __FILE__, __LINE__))
1663     {
1664         return MOS_STATUS_NO_SPACE;
1665     }
1666 #endif
1667 
1668     resource->bConvertedFromDDIResource = false;
1669     if (!params->bBypassMODImpl)
1670     {
1671         resource->pGfxResourceNext = GraphicsResourceNext::CreateGraphicResource(GraphicsResourceNext::osSpecificResource);
1672         MOS_OS_CHK_NULL_RETURN(resource->pGfxResourceNext);
1673 
1674         GraphicsResourceNext::CreateParams createParams(params);
1675         auto eStatus = resource->pGfxResourceNext->Allocate(streamState->osDeviceContext, createParams);
1676         MOS_OS_CHK_STATUS_MESSAGE_RETURN(eStatus, "Allocate graphic resource failed");
1677 
1678         eStatus = resource->pGfxResourceNext->ConvertToMosResource(resource);
1679         MOS_OS_CHK_STATUS_MESSAGE_RETURN(eStatus, "Convert graphic resource failed");
1680     }
1681     else
1682     {
1683         estatus = GraphicsResourceSpecificNext::AllocateExternalResource(streamState, params, resource);
1684         MOS_OS_CHK_STATUS_MESSAGE_RETURN(estatus, "Allocate external graphic resource failed");
1685     }
1686 
1687     MOS_OS_CHK_NULL_RETURN(resource->pGmmResInfo);
1688     MosUtilities::MosAtomicIncrement(&MosUtilities::m_mosMemAllocCounterGfx);
1689 
1690     MOS_MEMNINJA_GFX_ALLOC_MESSAGE(
1691         resource->pGmmResInfo,
1692         params->pBufName,
1693         streamState->component,
1694         (uint32_t)resource->pGmmResInfo->GetSizeSurface(),
1695         params->dwArraySize,
1696         functionName,
1697         filename,
1698         line);
1699 
1700     return MOS_STATUS_SUCCESS;
1701 }
1702 
1703 //The input bit definition in MOS_GFXRES_FREE_FLAGS
ConvertHalFreeFlagsToOsFreeFlags(uint32_t halFreeFlag)1704 uint32_t MosInterface::ConvertHalFreeFlagsToOsFreeFlags(
1705     uint32_t halFreeFlag)
1706 {
1707     return halFreeFlag;
1708 }
1709 
FreeResource(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,uint32_t flag,const char * functionName,const char * filename,int32_t line)1710 MOS_STATUS MosInterface::FreeResource(
1711     MOS_STREAM_HANDLE   streamState,
1712     MOS_RESOURCE_HANDLE resource,
1713     uint32_t            flag
1714 #if MOS_MESSAGES_ENABLED
1715     ,
1716     const char          *functionName,
1717     const char          *filename,
1718     int32_t             line
1719 #endif  // MOS_MESSAGES_ENABLED
1720 )
1721 {
1722     MOS_OS_FUNCTION_ENTER;
1723 
1724     MOS_OS_CHK_NULL_RETURN(resource);
1725     MOS_OS_CHK_NULL_RETURN(streamState);
1726     MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
1727 
1728     bool osContextValid = streamState->osDeviceContext->GetOsContextValid();
1729 
1730     bool byPassMod = !((!resource->bConvertedFromDDIResource) && (osContextValid == true) && (resource->pGfxResourceNext));
1731 
1732     if (!byPassMod)
1733     {
1734         if (resource && resource->pGfxResourceNext)
1735         {
1736             resource->pGfxResourceNext->Free(streamState->osDeviceContext);
1737         }
1738         else
1739         {
1740             MOS_OS_VERBOSEMESSAGE("Received an empty Graphics Resource, skip free");
1741         }
1742         MOS_Delete(resource->pGfxResourceNext);
1743         resource->pGfxResourceNext = nullptr;
1744 
1745         MosUtilities::MosAtomicDecrement(&MosUtilities::m_mosMemAllocCounterGfx);
1746         MOS_MEMNINJA_GFX_FREE_MESSAGE(resource->pGmmResInfo, functionName, filename, line);
1747         MosUtilities::MosZeroMemory(resource, sizeof(*resource));
1748 
1749         return MOS_STATUS_SUCCESS;
1750     }
1751 
1752     MOS_STATUS status = GraphicsResourceSpecificNext::FreeExternalResource(streamState, resource, flag);
1753 
1754     if (resource->pGmmResInfo != nullptr &&
1755         streamState->perStreamParameters != nullptr)
1756     {
1757         PMOS_CONTEXT perStreamParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
1758         if (perStreamParameters && perStreamParameters->pGmmClientContext)
1759         {
1760             MosUtilities::m_mosMemAllocCounterGfx--;
1761             MOS_MEMNINJA_GFX_FREE_MESSAGE(resource->pGmmResInfo, functionName, filename, line);
1762 
1763             perStreamParameters->pGmmClientContext->DestroyResInfoObject(resource->pGmmResInfo);
1764 
1765             resource->pGmmResInfo = nullptr;
1766         }
1767     }
1768 
1769     return status;
1770 }
1771 
GetResourceInfo(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,MosResourceInfo & details)1772 MOS_STATUS MosInterface::GetResourceInfo(
1773     MOS_STREAM_HANDLE   streamState,
1774     MOS_RESOURCE_HANDLE resource,
1775     MosResourceInfo     &details)  //MOS_SURFACE
1776 {
1777     MOS_OS_FUNCTION_ENTER;
1778 
1779     GMM_RESOURCE_INFO * gmmResourceInfo = nullptr;
1780     GMM_DISPLAY_FRAME   gmmChannel = GMM_DISPLAY_FRAME_MAX;
1781     GMM_REQ_OFFSET_INFO reqInfo[3] = {};
1782     GMM_RESOURCE_FLAG   gmmFlags = {};
1783     MOS_STATUS          eStatus = MOS_STATUS_SUCCESS;
1784 
1785     MOS_UNUSED(streamState);
1786     MOS_OS_CHK_NULL_RETURN(resource);
1787 
1788     // Get Gmm resource info
1789     gmmResourceInfo = (GMM_RESOURCE_INFO*)resource->pGmmResInfo;
1790     MOS_OS_CHK_NULL_RETURN(gmmResourceInfo);
1791 
1792     gmmFlags = gmmResourceInfo->GetResFlags();
1793 
1794     // Get resource information
1795     details.dwWidth         = GFX_ULONG_CAST(gmmResourceInfo->GetBaseWidth());
1796     details.dwHeight        = gmmResourceInfo->GetBaseHeight();
1797     details.dwPitch         = GFX_ULONG_CAST(gmmResourceInfo->GetRenderPitch());
1798     details.dwSize          = GFX_ULONG_CAST(gmmResourceInfo->GetSizeSurface());
1799     details.dwDepth         = MOS_MAX(1, gmmResourceInfo->GetBaseDepth());
1800     details.dwLockPitch     = GFX_ULONG_CAST(gmmResourceInfo->GetRenderPitch());
1801 
1802     details.dwQPitch = gmmResourceInfo->GetQPitch();
1803 
1804     details.bCompressible   = gmmFlags.Gpu.MMC ?
1805         (gmmResourceInfo->GetMmcHint(0) == GMM_MMC_HINT_ON) : false;
1806     details.bIsCompressed   = gmmResourceInfo->IsMediaMemoryCompressed(0);
1807     details.CompressionMode = (MOS_RESOURCE_MMC_MODE)gmmResourceInfo->GetMmcMode(0);
1808 
1809     if (0 == details.dwPitch)
1810     {
1811         MOS_OS_ASSERTMESSAGE("Pitch from GmmResource is 0, unexpected.");
1812         return MOS_STATUS_INVALID_PARAMETER;
1813     }
1814     // check resource's tile type
1815     details.TileModeGMM     = (MOS_TILE_MODE_GMM)gmmResourceInfo->GetTileModeSurfaceState();
1816     details.bGMMTileEnabled = true;
1817     switch (gmmResourceInfo->GetTileType())
1818     {
1819     case GMM_TILED_Y:
1820           if (gmmFlags.Info.TiledYf)
1821           {
1822               details.TileType = MOS_TILE_YF;
1823           }
1824           else if (gmmFlags.Info.TiledYs)
1825           {
1826               details.TileType = MOS_TILE_YS;
1827           }
1828           else
1829           {
1830               details.TileType = MOS_TILE_Y;
1831           }
1832           break;
1833     case GMM_TILED_X:
1834           details.TileType = MOS_TILE_X;
1835           break;
1836     case GMM_NOT_TILED:
1837           details.TileType = MOS_TILE_LINEAR;
1838           break;
1839     default:
1840           details.TileType = MOS_TILE_Y;
1841           break;
1842     }
1843     details.Format   = resource->Format;
1844 
1845     // Get planes
1846     MosUtilities::MosZeroMemory(reqInfo, sizeof(reqInfo));
1847     gmmChannel = GMM_DISPLAY_BASE;
1848     // Get the base offset of the surface (plane Y)
1849     reqInfo[2].ReqRender = true;
1850     reqInfo[2].Plane     = GMM_PLANE_Y;
1851     reqInfo[2].Frame     = gmmChannel;
1852     reqInfo[2].CubeFace  = __GMM_NO_CUBE_MAP;
1853     reqInfo[2].ArrayIndex = 0;
1854     gmmResourceInfo->GetOffset(reqInfo[2]);
1855     details.RenderOffset.YUV.Y.BaseOffset = reqInfo[2].Render.Offset;
1856     details.RenderOffset.YUV.Y.XOffset    = reqInfo[2].Render.XOffset;
1857     details.RenderOffset.YUV.Y.YOffset    = reqInfo[2].Render.YOffset;
1858     details.LockOffset.YUV.Y              = reqInfo[2].Lock.Offset;
1859 
1860     // Get U/UV plane information (plane offset, X/Y offset)
1861     reqInfo[0].ReqRender = true;
1862     reqInfo[0].Plane     = GMM_PLANE_U;
1863     reqInfo[0].Frame     = gmmChannel;
1864     reqInfo[0].CubeFace  = __GMM_NO_CUBE_MAP;
1865     reqInfo[0].ArrayIndex = 0;
1866     gmmResourceInfo->GetOffset(reqInfo[0]);
1867 
1868     details.RenderOffset.YUV.U.BaseOffset = reqInfo[0].Render.Offset;
1869     details.RenderOffset.YUV.U.XOffset    = reqInfo[0].Render.XOffset;
1870     details.RenderOffset.YUV.U.YOffset    = reqInfo[0].Render.YOffset;
1871     details.LockOffset.YUV.U              = reqInfo[0].Lock.Offset;
1872 
1873     // Get V plane information (plane offset, X/Y offset)
1874     reqInfo[1].ReqRender = true;
1875     reqInfo[1].Plane     = GMM_PLANE_V;
1876     reqInfo[1].Frame     = gmmChannel;
1877     reqInfo[1].CubeFace  = __GMM_NO_CUBE_MAP;
1878     reqInfo[1].ArrayIndex = 0;
1879     gmmResourceInfo->GetOffset(reqInfo[1]);
1880 
1881     details.RenderOffset.YUV.V.BaseOffset = reqInfo[1].Render.Offset;
1882     details.RenderOffset.YUV.V.XOffset    = reqInfo[1].Render.XOffset;
1883     details.RenderOffset.YUV.V.YOffset    = reqInfo[1].Render.YOffset;
1884     details.LockOffset.YUV.V              = reqInfo[1].Lock.Offset;
1885 
1886     // Get Y plane information (plane offset, X / Y offset)
1887     details.dwOffset                        = details.RenderOffset.YUV.Y.BaseOffset;
1888     details.YPlaneOffset.iSurfaceOffset     = details.RenderOffset.YUV.Y.BaseOffset;
1889     details.YPlaneOffset.iXOffset           = details.RenderOffset.YUV.Y.XOffset;
1890     details.YPlaneOffset.iYOffset           = details.RenderOffset.YUV.Y.YOffset;
1891     details.YPlaneOffset.iLockSurfaceOffset = details.LockOffset.YUV.Y;
1892 
1893     // Get U/UV plane information (plane offset, X/Y offset)
1894     details.UPlaneOffset.iSurfaceOffset     = details.RenderOffset.YUV.U.BaseOffset;
1895     details.UPlaneOffset.iXOffset           = details.RenderOffset.YUV.U.XOffset;
1896     details.UPlaneOffset.iYOffset           = details.RenderOffset.YUV.U.YOffset;
1897     details.UPlaneOffset.iLockSurfaceOffset = details.LockOffset.YUV.U;
1898 
1899     // Get V plane information (plane offset, X/Y offset)
1900     details.VPlaneOffset.iSurfaceOffset     = details.RenderOffset.YUV.V.BaseOffset;
1901     details.VPlaneOffset.iXOffset           = details.RenderOffset.YUV.V.XOffset;
1902     details.VPlaneOffset.iYOffset           = details.RenderOffset.YUV.V.YOffset;
1903     details.VPlaneOffset.iLockSurfaceOffset = details.LockOffset.YUV.V;
1904 
1905     details.YoffsetForUplane = (details.UPlaneOffset.iSurfaceOffset - details.dwOffset) / details.dwPitch +
1906                               details.UPlaneOffset.iYOffset;
1907     details.YoffsetForVplane = (details.VPlaneOffset.iSurfaceOffset - details.dwOffset) / details.dwPitch +
1908                               details.VPlaneOffset.iYOffset;
1909 
1910     return eStatus;
1911 }
1912 
LockMosResource(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,PMOS_LOCK_PARAMS flags)1913 void *MosInterface::LockMosResource(
1914     MOS_STREAM_HANDLE   streamState,
1915     MOS_RESOURCE_HANDLE resource,
1916     PMOS_LOCK_PARAMS    flags)
1917 {
1918     MOS_OS_FUNCTION_ENTER;
1919 
1920     void *pData    = nullptr;
1921 
1922     if (nullptr == streamState)
1923     {
1924         MOS_OS_ASSERTMESSAGE("input parameter streamState is NULL.");
1925         return nullptr;
1926     }
1927 
1928     if (nullptr == resource)
1929     {
1930         MOS_OS_ASSERTMESSAGE("input parameter resource is NULL.");
1931         return nullptr;
1932     }
1933 
1934     if ((!resource->bConvertedFromDDIResource) && (resource->pGfxResourceNext))
1935     {
1936         if (nullptr == streamState->osDeviceContext)
1937         {
1938             MOS_OS_ASSERTMESSAGE("invalid osDeviceContext, skip lock");
1939             return nullptr;
1940         }
1941 
1942         if (resource->pGfxResourceNext)
1943         {
1944             GraphicsResourceNext::LockParams params(flags);
1945             pData = resource->pGfxResourceNext->Lock(streamState->osDeviceContext, params);
1946         }
1947         else
1948         {
1949             MOS_OS_ASSERTMESSAGE("Received an empty Graphics Resource, skip lock");
1950             return nullptr;
1951         }
1952         return pData;
1953     }
1954 
1955     pData = GraphicsResourceSpecificNext::LockExternalResource(streamState, resource, flags);
1956     return pData;
1957 }
1958 
UnlockMosResource(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource)1959 MOS_STATUS MosInterface::UnlockMosResource(
1960     MOS_STREAM_HANDLE   streamState,
1961     MOS_RESOURCE_HANDLE resource)
1962 {
1963     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
1964 
1965     MOS_OS_FUNCTION_ENTER;
1966 
1967     MOS_OS_CHK_NULL_RETURN(resource);
1968     MOS_OS_CHK_NULL_RETURN(streamState);
1969     MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
1970 
1971     if ((!resource->bConvertedFromDDIResource) && (resource->pGfxResourceNext))
1972     {
1973         if (resource->pGfxResourceNext)
1974         {
1975             eStatus = resource->pGfxResourceNext->Unlock(streamState->osDeviceContext);
1976         }
1977         else
1978         {
1979             MOS_OS_VERBOSEMESSAGE("Received an empty Graphics Resource, skip unlock");
1980         }
1981         return eStatus;
1982     }
1983 
1984     eStatus = GraphicsResourceSpecificNext::UnlockExternalResource(streamState, resource);
1985 
1986     return eStatus;
1987 }
1988 
UpdateResourceUsageType(PMOS_RESOURCE pOsResource,MOS_HW_RESOURCE_DEF resUsageType)1989 MOS_STATUS MosInterface::UpdateResourceUsageType(
1990     PMOS_RESOURCE           pOsResource,
1991     MOS_HW_RESOURCE_DEF     resUsageType)
1992 {
1993     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
1994 
1995     //---------------------------------
1996     MOS_OS_CHK_NULL_RETURN(pOsResource);
1997     MOS_OS_CHK_NULL_RETURN(pOsResource->pGmmResInfo);
1998     //---------------------------------
1999 
2000     pOsResource->pGmmResInfo->OverrideCachePolicyUsage(GetGmmResourceUsageType(resUsageType));
2001 
2002     return eStatus;
2003 }
2004 
RegisterResource(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,bool write)2005 MOS_STATUS MosInterface::RegisterResource(
2006     MOS_STREAM_HANDLE   streamState,
2007     MOS_RESOURCE_HANDLE resource,
2008     bool                write)
2009 {
2010     MOS_OS_FUNCTION_ENTER;
2011 
2012     MOS_OS_CHK_NULL_RETURN(streamState);
2013     MOS_OS_CHK_NULL_RETURN(resource);
2014     MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
2015 
2016 #if (_DEBUG || _RELEASE_INTERNAL)
2017     if (MosSimulateOsApiFail(OS_FAIL_REGISTER_GFX_RES, __FUNCTION__, __FILE__, __LINE__))
2018     {
2019         return MOS_STATUS_NO_SPACE;
2020     }
2021 #endif
2022 
2023     auto gpuContext = MosInterface::GetGpuContext(streamState, streamState->currentGpuContextHandle);
2024     MOS_OS_CHK_NULL_RETURN(gpuContext);
2025 
2026     return (gpuContext->RegisterResource(resource, write));
2027 }
2028 
GetResourceGfxAddress(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource)2029 uint64_t MosInterface::GetResourceGfxAddress(
2030     MOS_STREAM_HANDLE   streamState,
2031     MOS_RESOURCE_HANDLE resource)
2032 {
2033     MOS_OS_FUNCTION_ENTER;
2034 
2035     MOS_OS_CHK_NULL_RETURN(streamState);
2036     MOS_OS_CHK_NULL_RETURN(resource);
2037 
2038     if (!mos_gem_bo_is_softpin(resource->bo))
2039     {
2040         mos_bo_set_softpin(resource->bo);
2041     }
2042     return resource->bo->offset64;
2043 }
2044 
GetResourceAllocationHandle(MOS_RESOURCE_HANDLE resource)2045 uint32_t MosInterface::GetResourceAllocationHandle(
2046     MOS_RESOURCE_HANDLE resource)
2047 {
2048     MOS_OS_FUNCTION_ENTER;
2049 
2050     if (resource && resource->bo)
2051     {
2052         return resource->bo->handle;
2053     }
2054     else
2055     {
2056         return 0;
2057     }
2058 }
2059 
GetResourceAllocationIndex(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource)2060 uint32_t MosInterface::GetResourceAllocationIndex(
2061     MOS_STREAM_HANDLE   streamState,
2062     MOS_RESOURCE_HANDLE resource)
2063 {
2064     MOS_OS_FUNCTION_ENTER;
2065 
2066     return 0;
2067 }
2068 
SkipResourceSync(MOS_RESOURCE_HANDLE resource)2069 MOS_STATUS MosInterface::SkipResourceSync(
2070     MOS_RESOURCE_HANDLE resource)
2071 {
2072     MOS_OS_FUNCTION_ENTER;
2073 
2074     // No resource sync to skip
2075 
2076     return MOS_STATUS_SUCCESS;
2077 }
2078 
SyncOnResource(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,bool writeOperation,GPU_CONTEXT_HANDLE requsetorGpuContext)2079 MOS_STATUS SyncOnResource(
2080     MOS_STREAM_HANDLE streamState,
2081     MOS_RESOURCE_HANDLE resource,
2082     bool writeOperation,
2083     GPU_CONTEXT_HANDLE requsetorGpuContext)
2084 {
2085     MOS_OS_FUNCTION_ENTER;
2086 
2087     // No need to do sync on resource
2088 
2089     return MOS_STATUS_SUCCESS;
2090 }
2091 
ResourceSyncCallback(OsSpecificRes resource,MOS_DEVICE_HANDLE deviceContext,uint32_t index,SYNC_HAZARD hazardType,GPU_CONTEXT_HANDLE busyCtx,GPU_CONTEXT_HANDLE requestorCtx,OS_HANDLE osHandle)2092 MOS_STATUS MosInterface::ResourceSyncCallback(
2093     OsSpecificRes       resource,
2094     MOS_DEVICE_HANDLE   deviceContext,
2095     uint32_t            index,
2096     SYNC_HAZARD         hazardType,
2097     GPU_CONTEXT_HANDLE  busyCtx,
2098     GPU_CONTEXT_HANDLE  requestorCtx,
2099     OS_HANDLE           osHandle)
2100 {
2101     MOS_OS_FUNCTION_ENTER;
2102 
2103     // No need to do resource sync
2104 
2105     return MOS_STATUS_SUCCESS;
2106 }
2107 
LockSyncCallback(OsSpecificRes resource,MOS_DEVICE_HANDLE deviceContext,uint32_t index,SYNC_HAZARD hazardType,GPU_CONTEXT_HANDLE busyCtx,bool doNotWait)2108 MOS_STATUS MosInterface::LockSyncCallback(
2109     OsSpecificRes       resource,
2110     MOS_DEVICE_HANDLE   deviceContext,
2111     uint32_t            index,
2112     SYNC_HAZARD         hazardType,
2113     GPU_CONTEXT_HANDLE  busyCtx,
2114     bool                doNotWait)
2115 {
2116     MOS_OS_FUNCTION_ENTER;
2117 
2118     // No need to do Lock sync
2119 
2120     return MOS_STATUS_SUCCESS;
2121 }
2122 
TrimResidency(MOS_DEVICE_HANDLE device,bool periodicTrim,bool restartPeriodicTrim,uint64_t & numBytesToTrim,bool trimToMinimum,bool trimOnlyMediaResources)2123 MOS_STATUS MosInterface::TrimResidency(
2124     MOS_DEVICE_HANDLE device,
2125     bool      periodicTrim,
2126     bool      restartPeriodicTrim,
2127     uint64_t &numBytesToTrim,
2128     bool      trimToMinimum,
2129     bool      trimOnlyMediaResources)
2130 {
2131     MOS_OS_FUNCTION_ENTER;
2132 
2133     // No residency to trim
2134 
2135     return MOS_STATUS_SUCCESS;
2136 }
2137 
UpdateResidency(MOS_DEVICE_HANDLE device,OsSpecificRes resInfo,uint32_t index)2138 MOS_STATUS UpdateResidency(
2139     MOS_DEVICE_HANDLE device,
2140     OsSpecificRes     resInfo,
2141     uint32_t          index)
2142 {
2143     MOS_OS_FUNCTION_ENTER;
2144 
2145     // No residency to update
2146 
2147     return MOS_STATUS_SUCCESS;
2148 }
2149 
SetMemoryCompressionMode(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,MOS_MEMCOMP_STATE resMmcMode)2150 MOS_STATUS MosInterface::SetMemoryCompressionMode(
2151     MOS_STREAM_HANDLE   streamState,
2152     MOS_RESOURCE_HANDLE resource,
2153     MOS_MEMCOMP_STATE   resMmcMode)
2154 {
2155     MOS_OS_FUNCTION_ENTER;
2156 
2157     PGMM_RESOURCE_INFO pGmmResourceInfo = nullptr;
2158     GMM_RESOURCE_MMC_INFO GmmResMmcMode = GMM_MMC_DISABLED;
2159     MOS_STATUS eStatus = MOS_STATUS_UNKNOWN;
2160 
2161     MOS_OS_CHK_NULL_RETURN(resource);
2162 
2163     // Get Gmm resource info
2164     pGmmResourceInfo = (GMM_RESOURCE_INFO *)resource->pGmmResInfo;
2165     MOS_OS_CHK_NULL_RETURN(pGmmResourceInfo);
2166 
2167     switch (resMmcMode)
2168     {
2169     case MOS_MEMCOMP_HORIZONTAL:
2170         GmmResMmcMode = GMM_MMC_HORIZONTAL;
2171         break;
2172     case MOS_MEMCOMP_VERTICAL:
2173         GmmResMmcMode = GMM_MMC_VERTICAL;
2174         break;
2175     case MOS_MEMCOMP_DISABLED:
2176     default:
2177         GmmResMmcMode = GMM_MMC_DISABLED;
2178         break;
2179     }
2180 
2181     pGmmResourceInfo->SetMmcMode(GmmResMmcMode, 0);
2182 
2183     eStatus = MOS_STATUS_SUCCESS;
2184 
2185     return eStatus;
2186 }
2187 
GetMemoryCompressionMode(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,MOS_MEMCOMP_STATE & resMmcMode)2188 MOS_STATUS MosInterface::GetMemoryCompressionMode(
2189     MOS_STREAM_HANDLE   streamState,
2190     MOS_RESOURCE_HANDLE resource,
2191     MOS_MEMCOMP_STATE  &resMmcMode)
2192 {
2193     MOS_OS_FUNCTION_ENTER;
2194 
2195     PGMM_RESOURCE_INFO gmmResourceInfo = nullptr;
2196     GMM_RESOURCE_FLAG  flags;
2197     MOS_STATUS eStatus = MOS_STATUS_UNKNOWN;
2198 
2199     MOS_OS_CHK_NULL_RETURN(resource);
2200     MosUtilities::MosZeroMemory(&flags, sizeof(GMM_RESOURCE_FLAG));
2201 
2202     // Get Gmm resource info
2203     gmmResourceInfo = (GMM_RESOURCE_INFO *)resource->pGmmResInfo;
2204     MOS_OS_CHK_NULL_RETURN(gmmResourceInfo);
2205 
2206     flags = resource->pGmmResInfo->GetResFlags();
2207 
2208     if (!flags.Gpu.MMC || !flags.Gpu.CCS)
2209     {
2210         resMmcMode = MOS_MEMCOMP_DISABLED;
2211         return MOS_STATUS_SUCCESS;
2212     }
2213 
2214     if (flags.Info.MediaCompressed || flags.Info.RenderCompressed)
2215     {
2216         resMmcMode = flags.Info.RenderCompressed ? MOS_MEMCOMP_RC : MOS_MEMCOMP_MC;
2217     }
2218     else
2219     {
2220         switch (gmmResourceInfo->GetMmcMode(0))
2221         {
2222         case GMM_MMC_HORIZONTAL:
2223             resMmcMode = MOS_MEMCOMP_HORIZONTAL;
2224             break;
2225         case GMM_MMC_VERTICAL:
2226             resMmcMode = MOS_MEMCOMP_VERTICAL;
2227             break;
2228         case GMM_MMC_DISABLED:
2229         default:
2230             resMmcMode = MOS_MEMCOMP_DISABLED;
2231             break;
2232         }
2233     }
2234 
2235     uint32_t          MmcFormat = 0;
2236     GMM_RESOURCE_FORMAT gmmResFmt;
2237     gmmResFmt = gmmResourceInfo->GetResourceFormat();
2238     auto skuTable = GetSkuTable(streamState);
2239     MOS_OS_CHK_NULL_RETURN(MosInterface::GetGmmClientContext(streamState));
2240     MOS_OS_CHK_NULL_RETURN(skuTable);
2241 
2242     if (resMmcMode == MOS_MEMCOMP_MC         &&
2243        (!MEDIA_IS_SKU(skuTable, FtrFlatPhysCCS)))
2244     {
2245         MmcFormat = static_cast<uint32_t>(MosInterface::GetGmmClientContext(streamState)->GetMediaSurfaceStateCompressionFormat(gmmResFmt));
2246         resMmcMode = (MmcFormat != 0) ? resMmcMode : MOS_MEMCOMP_DISABLED;
2247     }
2248 
2249     eStatus = MOS_STATUS_SUCCESS;
2250 
2251     return eStatus;
2252 }
2253 
SetMemoryCompressionHint(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,bool hintOn)2254 MOS_STATUS MosInterface::SetMemoryCompressionHint(
2255     MOS_STREAM_HANDLE   streamState,
2256     MOS_RESOURCE_HANDLE resource,
2257     bool                hintOn)
2258 {
2259     MOS_OS_FUNCTION_ENTER;
2260 
2261     PGMM_RESOURCE_INFO pGmmResourceInfo = nullptr;
2262     uint32_t uiArrayIndex = 0;
2263     MOS_STATUS eStatus = MOS_STATUS_UNKNOWN;
2264     MOS_OS_CHK_NULL_RETURN(resource);
2265 
2266     // Get Gmm resource info
2267     pGmmResourceInfo = (GMM_RESOURCE_INFO *)resource->pGmmResInfo;
2268     MOS_OS_CHK_NULL_RETURN(pGmmResourceInfo);
2269 
2270     pGmmResourceInfo->SetMmcHint(hintOn ? GMM_MMC_HINT_ON : GMM_MMC_HINT_OFF, uiArrayIndex);
2271 
2272     eStatus = MOS_STATUS_SUCCESS;
2273 
2274     return eStatus;
2275 }
2276 
GetMemoryCompressionFormat(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource,uint32_t * resMmcFormat)2277 MOS_STATUS MosInterface::GetMemoryCompressionFormat(
2278     MOS_STREAM_HANDLE   streamState,
2279     MOS_RESOURCE_HANDLE resource,
2280     uint32_t *          resMmcFormat)
2281 {
2282     MOS_OS_FUNCTION_ENTER;
2283 
2284     MOS_STATUS         eStatus = MOS_STATUS_UNKNOWN;
2285     PGMM_RESOURCE_INFO pGmmResourceInfo;
2286 
2287     MOS_OS_CHK_NULL_RETURN(streamState);
2288     MOS_OS_CHK_NULL_RETURN(resource);
2289     MOS_OS_CHK_NULL_RETURN(resMmcFormat);
2290 
2291     pGmmResourceInfo = (GMM_RESOURCE_INFO *)resource->pGmmResInfo;
2292     MOS_OS_CHK_NULL_RETURN(pGmmResourceInfo);
2293     MOS_OS_CHK_NULL_RETURN(MosInterface::GetGmmClientContext(streamState));
2294     // Get compression format from GMM RESOURCE FORMAT
2295     GMM_RESOURCE_FORMAT gmmResFmt;
2296     gmmResFmt = pGmmResourceInfo->GetResourceFormat();
2297     MOS_MEMCOMP_STATE MmcMode   = MOS_MEMCOMP_DISABLED;
2298     uint32_t          MmcFormat = 0;
2299     MosInterface::GetMemoryCompressionMode(streamState, resource, MmcMode);
2300     switch (MmcMode)
2301     {
2302     case MOS_MEMCOMP_MC:
2303          MmcFormat = static_cast<uint32_t>(MosInterface::GetGmmClientContext(streamState)->GetMediaSurfaceStateCompressionFormat(gmmResFmt));
2304          break;
2305     case MOS_MEMCOMP_RC:
2306          MmcFormat = static_cast<uint32_t>(MosInterface::GetGmmClientContext(streamState)->GetSurfaceStateCompressionFormat(gmmResFmt));
2307          break;
2308     default:
2309           MmcFormat = 0;
2310     }
2311 
2312     if (MmcFormat > 0x1F)
2313     {
2314         MOS_OS_ASSERTMESSAGE("Get a incorrect Compression format(%d) from GMM", MmcFormat);
2315     }
2316     else
2317     {
2318         *resMmcFormat = MmcFormat;
2319         MOS_OS_VERBOSEMESSAGE("GMM compression mode %d, compression format %d", MmcMode, MmcFormat);
2320     }
2321 
2322     eStatus = MOS_STATUS_SUCCESS;
2323 
2324     return eStatus;
2325 }
2326 
DoubleBufferCopyResource(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE inputResource,MOS_RESOURCE_HANDLE outputResource,bool outputCompressed)2327 MOS_STATUS MosInterface::DoubleBufferCopyResource(
2328     MOS_STREAM_HANDLE   streamState,
2329     MOS_RESOURCE_HANDLE inputResource,
2330     MOS_RESOURCE_HANDLE outputResource,
2331     bool                outputCompressed)
2332 {
2333     MOS_OS_FUNCTION_ENTER;
2334 
2335     MOS_STATUS status = MOS_STATUS_SUCCESS;
2336     MOS_OS_CHK_NULL_RETURN(inputResource);
2337     MOS_OS_CHK_NULL_RETURN(outputResource);
2338     MOS_OS_CHK_NULL_RETURN(streamState);
2339 
2340     if (inputResource && inputResource->bo && inputResource->pGmmResInfo &&
2341         outputResource && outputResource->bo && outputResource->pGmmResInfo)
2342     {
2343         OsContextNext *osCtx = streamState->osDeviceContext;
2344         MOS_OS_CHK_NULL_RETURN(osCtx);
2345 
2346         MosDecompression *mosDecompression = osCtx->GetMosDecompression();
2347         MOS_OS_CHK_NULL_RETURN(mosDecompression);
2348 
2349         // Double Buffer Copy can support any tile status surface with/without compression
2350         mosDecompression->MediaMemoryCopy(inputResource, outputResource, outputCompressed);
2351     }
2352 
2353     return status;
2354 }
2355 
MediaCopyResource2D(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE inputResource,MOS_RESOURCE_HANDLE outputResource,uint32_t copyWidth,uint32_t copyHeight,uint32_t copyInputOffset,uint32_t copyOutputOffset,uint32_t bpp,bool outputCompressed)2356 MOS_STATUS MosInterface::MediaCopyResource2D(
2357     MOS_STREAM_HANDLE   streamState,
2358     MOS_RESOURCE_HANDLE inputResource,
2359     MOS_RESOURCE_HANDLE outputResource,
2360     uint32_t            copyWidth,
2361     uint32_t            copyHeight,
2362     uint32_t            copyInputOffset,
2363     uint32_t            copyOutputOffset,
2364     uint32_t            bpp,
2365     bool                outputCompressed)
2366 {
2367     MOS_OS_FUNCTION_ENTER;
2368 
2369     MOS_STATUS status = MOS_STATUS_SUCCESS;
2370     MOS_OS_CHK_NULL_RETURN(inputResource);
2371     MOS_OS_CHK_NULL_RETURN(outputResource);
2372     MOS_OS_CHK_NULL_RETURN(streamState);
2373 
2374     if (inputResource && inputResource->bo && inputResource->pGmmResInfo &&
2375         outputResource && outputResource->bo && outputResource->pGmmResInfo)
2376     {
2377         OsContextNext *osCtx = streamState->osDeviceContext;
2378         MOS_OS_CHK_NULL_RETURN(osCtx);
2379 
2380         MosDecompression *mosDecompression = osCtx->GetMosDecompression();
2381         MOS_OS_CHK_NULL_RETURN(mosDecompression);
2382 
2383         // Double Buffer Copy can support any tile status surface with/without compression
2384         mosDecompression->MediaMemoryCopy2D(inputResource, outputResource,
2385             copyWidth, copyHeight, copyInputOffset, copyOutputOffset, bpp, outputCompressed);
2386     }
2387 
2388     return status;
2389 }
2390 
DecompResource(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE resource)2391 MOS_STATUS MosInterface::DecompResource(
2392     MOS_STREAM_HANDLE   streamState,
2393     MOS_RESOURCE_HANDLE resource)
2394 {
2395     MOS_OS_FUNCTION_ENTER;
2396     MOS_STATUS status = MOS_STATUS_SUCCESS;
2397 
2398     MOS_OS_CHK_NULL_RETURN(streamState);
2399     MOS_OS_CHK_NULL_RETURN(resource);
2400     MOS_OS_CHK_NULL_RETURN(resource->bo);
2401     MOS_OS_CHK_NULL_RETURN(resource->pGmmResInfo);
2402 
2403     MOS_LINUX_BO *bo = resource->bo;
2404     if (resource->pGmmResInfo->IsMediaMemoryCompressed(0))
2405     {
2406         OsContextNext *osCtx = streamState->osDeviceContext;
2407         MOS_OS_CHK_NULL_RETURN(osCtx);
2408 
2409         MosDecompression *mosDecompression = osCtx->GetMosDecompression();
2410         MOS_OS_CHK_NULL_RETURN(mosDecompression);
2411 
2412         mosDecompression->MemoryDecompress(resource);
2413     }
2414 
2415     return MOS_STATUS_SUCCESS;
2416 }
2417 
GetGpuStatusTag(MOS_STREAM_HANDLE streamState,GPU_CONTEXT_HANDLE gpuContext)2418 uint32_t MosInterface::GetGpuStatusTag(
2419         MOS_STREAM_HANDLE  streamState,
2420         GPU_CONTEXT_HANDLE gpuContext)
2421 {
2422     MOS_OS_FUNCTION_ENTER;
2423 
2424     if (streamState)
2425     {
2426         auto gpuContextIns = MosInterface::GetGpuContext(streamState, gpuContext);
2427         if (gpuContextIns == nullptr)
2428         {
2429             MOS_OS_ASSERTMESSAGE("Get GPU Status Tag failed.");
2430             return 0;
2431         }
2432 
2433         return gpuContextIns->GetGpuStatusTag();
2434     }
2435     MOS_OS_ASSERTMESSAGE("Get GPU Status Tag failed.");
2436 
2437     return 0;
2438 }
2439 
IncrementGpuStatusTag(MOS_STREAM_HANDLE streamState,GPU_CONTEXT_HANDLE gpuContext)2440 MOS_STATUS MosInterface::IncrementGpuStatusTag(
2441     MOS_STREAM_HANDLE  streamState,
2442     GPU_CONTEXT_HANDLE gpuContext)
2443 {
2444     MOS_OS_FUNCTION_ENTER;
2445 
2446     MOS_OS_CHK_NULL_RETURN(streamState);
2447     auto gpuContextIns = MosInterface::GetGpuContext(streamState, gpuContext);
2448     MOS_OS_CHK_NULL_RETURN(gpuContextIns);
2449 
2450     gpuContextIns->IncrementGpuStatusTag();
2451 
2452     return MOS_STATUS_SUCCESS;
2453 }
2454 
GetGpuStatusSyncTag(MOS_STREAM_HANDLE streamState,GPU_CONTEXT_HANDLE gpuContext)2455 uint64_t MosInterface::GetGpuStatusSyncTag(
2456     MOS_STREAM_HANDLE  streamState,
2457     GPU_CONTEXT_HANDLE gpuContext)
2458 {
2459     MOS_OS_FUNCTION_ENTER;
2460 
2461     // No Gpu Status Sync Tag in Linux
2462 
2463     return 0;
2464 }
2465 
GetGpuStatusBufferResource(MOS_STREAM_HANDLE streamState,MOS_RESOURCE_HANDLE & resource,GPU_CONTEXT_HANDLE gpuContext)2466 MOS_STATUS MosInterface::GetGpuStatusBufferResource(
2467     MOS_STREAM_HANDLE   streamState,
2468     MOS_RESOURCE_HANDLE &resource,
2469     GPU_CONTEXT_HANDLE  gpuContext)
2470 {
2471     MOS_OS_FUNCTION_ENTER;
2472 
2473     MOS_OS_CHK_NULL_RETURN(streamState);
2474 
2475     auto gpuContextIns = MosInterface::GetGpuContext(streamState, gpuContext);
2476     MOS_OS_CHK_NULL_RETURN(gpuContextIns);
2477 
2478     resource = gpuContextIns->GetStatusBufferResource();
2479 
2480     return MOS_STATUS_SUCCESS;
2481 }
2482 
GetGmmClientContext(MOS_STREAM_HANDLE streamState)2483 GMM_CLIENT_CONTEXT *MosInterface::GetGmmClientContext(
2484     MOS_STREAM_HANDLE streamState)
2485 {
2486     MOS_OS_FUNCTION_ENTER;
2487 
2488     if (streamState && streamState->osDeviceContext)
2489     {
2490         return streamState->osDeviceContext->GetGmmClientContext();
2491     }
2492 
2493     return nullptr;
2494 }
2495 
GetGpuPriority(MOS_STREAM_HANDLE streamState,int32_t * pPriority)2496 void MosInterface::GetGpuPriority(MOS_STREAM_HANDLE streamState, int32_t* pPriority)
2497 {
2498     MOS_OS_FUNCTION_ENTER;
2499 
2500     if (streamState == nullptr)
2501     {
2502         MOS_OS_ASSERTMESSAGE("Failed to set the gpu priority");
2503         return;
2504     }
2505 
2506     PMOS_OS_CONTEXT pOsContext = (PMOS_OS_CONTEXT)streamState->perStreamParameters;
2507     if (pOsContext == nullptr)
2508     {
2509         MOS_OS_ASSERTMESSAGE("Failed to set the gpu priority");
2510         return;
2511     }
2512 
2513     uint64_t priority = 0;
2514     mos_get_context_param(pOsContext->intel_context, 0, I915_CONTEXT_PARAM_PRIORITY, &priority);
2515     *pPriority = (int32_t)priority;
2516 }
2517 
SetGpuPriority(MOS_STREAM_HANDLE streamState,int32_t priority)2518 void MosInterface::SetGpuPriority(MOS_STREAM_HANDLE streamState, int32_t priority)
2519 {
2520     MOS_OS_FUNCTION_ENTER;
2521 
2522     if (streamState == nullptr)
2523     {
2524         MOS_OS_ASSERTMESSAGE("Failed to set the gpu priority");
2525         return;
2526     }
2527 
2528     if (streamState->ctxPriority == priority)
2529         return;
2530 
2531     PMOS_OS_CONTEXT pOsContext = (PMOS_OS_CONTEXT)streamState->perStreamParameters;
2532     if (pOsContext == nullptr)
2533     {
2534         MOS_OS_ASSERTMESSAGE("Failed to set the gpu priority");
2535         return;
2536     }
2537 
2538     int32_t ret = mos_set_context_param(pOsContext->intel_context, 0, I915_CONTEXT_PARAM_PRIORITY,(uint64_t)priority);
2539     if (ret != 0)
2540     {
2541         MOS_OS_ASSERTMESSAGE("failed to set the gpu priority, error is %d", ret);
2542     }
2543 
2544     streamState->ctxPriority = priority;
2545 }
2546 
GetAuxTableBaseAddr(MOS_STREAM_HANDLE streamState)2547 uint64_t MosInterface::GetAuxTableBaseAddr(
2548     MOS_STREAM_HANDLE streamState)
2549 {
2550     MOS_OS_FUNCTION_ENTER;
2551 
2552     return 0;
2553 }
2554 
GetCpInterface(MOS_STREAM_HANDLE streamState)2555 MosCpInterface *MosInterface::GetCpInterface(MOS_STREAM_HANDLE streamState)
2556 {
2557     MOS_OS_FUNCTION_ENTER;
2558 
2559     return streamState ? streamState->osCpInterface : nullptr;
2560 }
2561 
GetOcaInterface(MOS_STREAM_HANDLE streamState)2562 MosOcaInterface *MosInterface::GetOcaInterface(MOS_STREAM_HANDLE streamState)
2563 {
2564     MOS_OS_FUNCTION_ENTER;
2565 
2566     return nullptr;
2567 }
2568 
GetVirtualEngineState(MOS_STREAM_HANDLE streamState)2569 MOS_VE_HANDLE MosInterface::GetVirtualEngineState(
2570     MOS_STREAM_HANDLE streamState)
2571 {
2572     MOS_OS_FUNCTION_ENTER;
2573 
2574     return streamState ? streamState->virtualEngineInterface : nullptr;
2575 }
2576 
SetVirtualEngineState(MOS_STREAM_HANDLE streamState,MOS_VE_HANDLE veState)2577 MOS_STATUS MosInterface::SetVirtualEngineState(
2578     MOS_STREAM_HANDLE streamState,
2579     MOS_VE_HANDLE veState)
2580 {
2581     MOS_OS_FUNCTION_ENTER;
2582 
2583     MOS_OS_CHK_NULL_RETURN(streamState);
2584     MOS_OS_CHK_NULL_RETURN(veState);
2585 
2586     streamState->virtualEngineInterface = veState;
2587 
2588     return MOS_STATUS_SUCCESS;
2589 }
2590 
CreateVirtualEngineState(MOS_STREAM_HANDLE streamState,PMOS_VIRTUALENGINE_INIT_PARAMS veInitParms,MOS_VE_HANDLE & veState)2591 MOS_STATUS MosInterface::CreateVirtualEngineState(
2592     MOS_STREAM_HANDLE streamState,
2593     PMOS_VIRTUALENGINE_INIT_PARAMS veInitParms,
2594     MOS_VE_HANDLE& veState)
2595 {
2596     MOS_OS_FUNCTION_ENTER;
2597 
2598     MOS_OS_CHK_NULL_RETURN(streamState);
2599 
2600     if (veInitParms->bScalabilitySupported)
2601     {
2602         streamState->virtualEngineInterface = MOS_New(MosOsVeScalabilitySpecific);
2603     }
2604     else
2605     {
2606         streamState->virtualEngineInterface = MOS_New(MosOsVeSinglePipeSpecific);
2607     }
2608     MOS_OS_CHK_NULL_RETURN(streamState->virtualEngineInterface);
2609 
2610     MOS_OS_CHK_STATUS_RETURN(streamState->virtualEngineInterface->Initialize(streamState, veInitParms));
2611 
2612     veState = streamState->virtualEngineInterface;
2613 
2614     return MOS_STATUS_SUCCESS;
2615 }
2616 
DestroyVirtualEngineState(MOS_STREAM_HANDLE streamState)2617 MOS_STATUS MosInterface::DestroyVirtualEngineState(
2618     MOS_STREAM_HANDLE streamState)
2619 {
2620     MOS_OS_FUNCTION_ENTER;
2621 
2622     MOS_OS_CHK_NULL_RETURN(streamState);
2623     if (streamState->virtualEngineInterface)
2624     {
2625         streamState->virtualEngineInterface->Destroy();
2626         MOS_Delete(streamState->virtualEngineInterface);
2627     }
2628 
2629     return MOS_STATUS_SUCCESS;
2630 }
2631 
SetVeHintParams(MOS_STREAM_HANDLE streamState,PMOS_VIRTUALENGINE_SET_PARAMS veParams)2632 MOS_STATUS MosInterface::SetVeHintParams(
2633     MOS_STREAM_HANDLE             streamState,
2634     PMOS_VIRTUALENGINE_SET_PARAMS veParams)
2635 {
2636     MOS_OS_FUNCTION_ENTER;
2637 
2638     MOS_OS_CHK_NULL_RETURN(streamState);
2639     MOS_OS_CHK_NULL_RETURN(streamState->virtualEngineInterface);
2640 
2641     return streamState->virtualEngineInterface->SetHintParams(veParams);
2642 }
2643 
GetVeHintParams(MOS_STREAM_HANDLE streamState,bool scalableMode,PMOS_VIRTUALENGINE_HINT_PARAMS * hintParams)2644 MOS_STATUS MosInterface::GetVeHintParams(
2645     MOS_STREAM_HANDLE               streamState,
2646     bool                            scalableMode,
2647     PMOS_VIRTUALENGINE_HINT_PARAMS* hintParams)
2648 {
2649     MOS_OS_CHK_NULL_RETURN(streamState);
2650     MOS_OS_CHK_NULL_RETURN(streamState->virtualEngineInterface);
2651     MOS_OS_CHK_NULL_RETURN(hintParams);
2652 
2653     return streamState->virtualEngineInterface->GetHintParams(scalableMode, hintParams);
2654 }
2655 
SetVeSubmissionType(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuf,MOS_SUBMISSION_TYPE type)2656 MOS_STATUS MosInterface::SetVeSubmissionType(
2657     MOS_STREAM_HANDLE     streamState,
2658     COMMAND_BUFFER_HANDLE cmdBuf,
2659     MOS_SUBMISSION_TYPE   type)
2660 {
2661     MOS_OS_CHK_NULL_RETURN(cmdBuf);
2662     MOS_OS_CHK_NULL_RETURN(streamState);
2663     MOS_OS_CHK_NULL_RETURN(streamState->virtualEngineInterface);
2664 
2665     return streamState->virtualEngineInterface->SetSubmissionType(cmdBuf, type);
2666 }
2667 
2668 #if _DEBUG || _RELEASE_INTERNAL
2669 
GetVeEngineCount(MOS_STREAM_HANDLE streamState)2670 uint8_t MosInterface::GetVeEngineCount(
2671     MOS_STREAM_HANDLE streamState)
2672 {
2673     return streamState && streamState->virtualEngineInterface ?
2674         streamState->virtualEngineInterface->GetEngineCount() : 0;
2675 }
2676 
GetEngineLogicId(MOS_STREAM_HANDLE streamState,uint32_t instanceIdx)2677 uint8_t MosInterface::GetEngineLogicId(
2678     MOS_STREAM_HANDLE streamState,
2679     uint32_t instanceIdx)
2680 {
2681     return streamState && streamState->virtualEngineInterface ?
2682         streamState->virtualEngineInterface->GetEngineLogicId(instanceIdx) : 0;
2683 }
2684 
2685 #endif  // _DEBUG || _RELEASE_INTERNAL
ComposeCommandBufferHeader(MOS_STREAM_HANDLE streamState,COMMAND_BUFFER_HANDLE cmdBuffer)2686 MOS_STATUS MosInterface::ComposeCommandBufferHeader(
2687     MOS_STREAM_HANDLE     streamState,
2688     COMMAND_BUFFER_HANDLE cmdBuffer)
2689 {
2690     MOS_OS_FUNCTION_ENTER;
2691 
2692     // No Command buffer header to compose
2693 
2694     return MOS_STATUS_SUCCESS;
2695 }
2696 
MosLoadLibrary(MOS_STREAM_HANDLE streamState,PCCHAR pFileName,PHMODULE ppvModule)2697 MOS_STATUS MosInterface::MosLoadLibrary(
2698     MOS_STREAM_HANDLE           streamState,
2699     PCCHAR                      pFileName,
2700     PHMODULE                    ppvModule)
2701 {
2702     MOS_OS_FUNCTION_ENTER;
2703 
2704     MOS_UNUSED(streamState);
2705     MOS_OS_ASSERT(pFileName);
2706     return MosUtilities::MosLoadLibrary(pFileName, ppvModule);
2707 }
2708 
MosFreeLibrary(HMODULE hLibModule)2709 MOS_STATUS MosInterface::MosFreeLibrary(HMODULE hLibModule)
2710 {
2711     MOS_OS_ASSERT(hLibModule);
2712 
2713     uint32_t ret = MosUtilities::MosFreeLibrary(hLibModule);
2714 
2715     return (ret == true) ? MOS_STATUS_SUCCESS : MOS_STATUS_UNKNOWN;
2716 }
2717 
GetGpuContext(MOS_STREAM_HANDLE streamState,GPU_CONTEXT_HANDLE handle)2718 GpuContextSpecificNext *MosInterface::GetGpuContext(MOS_STREAM_HANDLE streamState, GPU_CONTEXT_HANDLE handle)
2719 {
2720     MOS_OS_FUNCTION_ENTER;
2721 
2722     if (streamState && streamState->osDeviceContext)
2723     {
2724         auto osDeviceContext = streamState->osDeviceContext;
2725 
2726         auto gpuContextMgr = osDeviceContext->GetGpuContextMgr();
2727         if (gpuContextMgr)
2728         {
2729             GpuContextNext *gpuCtx = gpuContextMgr->GetGpuContext(handle);
2730 
2731             return static_cast<GpuContextSpecificNext *>(gpuCtx);
2732         }
2733     }
2734 
2735     MOS_OS_ASSERTMESSAGE("GetGpuContext failed!");
2736     return nullptr;
2737 }
2738 
SetPerfTag(MOS_STREAM_HANDLE streamState,uint32_t perfTag)2739 void MosInterface::SetPerfTag(MOS_STREAM_HANDLE streamState, uint32_t perfTag)
2740 {
2741     MOS_OS_FUNCTION_ENTER;
2742 
2743     uint32_t     componentTag;
2744 
2745     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState);
2746     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState->perStreamParameters);
2747 
2748     auto osParameters   = (PMOS_CONTEXT)streamState->perStreamParameters;
2749     PERF_DATA *perfData = osParameters->pPerfData;
2750     MOS_OS_CHK_NULL_NO_STATUS_RETURN(perfData);
2751 
2752     switch (streamState->component)
2753     {
2754     case COMPONENT_VPreP:
2755     case COMPONENT_VPCommon:
2756         componentTag = PERFTAG_VPREP;
2757         break;
2758 
2759     case COMPONENT_LibVA:
2760         componentTag = PERFTAG_LIBVA;
2761         break;
2762 
2763     case COMPONENT_CM:
2764         componentTag = PERFTAG_CM;
2765         break;
2766 
2767     case COMPONENT_Decode:
2768         componentTag = PERFTAG_DECODE;
2769         break;
2770 
2771     case COMPONENT_Encode:
2772         componentTag = PERFTAG_ENCODE;
2773         break;
2774 
2775     default:
2776         componentTag = 0xF000 & perfData->dmaBufID;
2777         break;
2778     }
2779 
2780     perfData->dmaBufID = componentTag | (perfTag & 0x0fff);
2781 
2782     return;
2783 }
2784 
IsPerfTagSet(MOS_STREAM_HANDLE streamState)2785 int32_t MosInterface::IsPerfTagSet(MOS_STREAM_HANDLE streamState)
2786 {
2787     uint32_t                 componentTag   = 0;
2788     int32_t                  ret            = false;
2789 
2790     MOS_OS_FUNCTION_ENTER;
2791 
2792     if (streamState == nullptr ||
2793         streamState->perStreamParameters == nullptr)
2794     {
2795         MOS_OS_ASSERTMESSAGE("streamState or perStreamParameters invalid nullptr");
2796         return false;
2797     }
2798 
2799     auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
2800 
2801     PERF_DATA *perfData     = osParameters->pPerfData;
2802     if (perfData == nullptr)
2803     {
2804         MOS_OS_ASSERTMESSAGE("perfData invalid nullptr");
2805         return false;
2806     }
2807 
2808     componentTag = 0xF000 & perfData->dmaBufID;
2809 
2810     switch (componentTag)
2811     {
2812     case PERFTAG_ENCODE:
2813     case PERFTAG_DECODE:
2814         ret = true;
2815         break;
2816 
2817     default:
2818         ret = false;
2819         break;
2820     }
2821 
2822     return ret;
2823 }
2824 
IncPerfFrameID(MOS_STREAM_HANDLE streamState)2825 void MosInterface::IncPerfFrameID(MOS_STREAM_HANDLE streamState)
2826 {
2827     MOS_OS_FUNCTION_ENTER;
2828 
2829     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState);
2830     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState->perStreamParameters);
2831 
2832     auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
2833 
2834     MOS_OS_CHK_NULL_NO_STATUS_RETURN(osParameters->pPerfData);
2835 
2836     osParameters->pPerfData->frameID++;
2837 
2838     return;
2839 }
2840 
GetPerfTag(MOS_STREAM_HANDLE streamState)2841 uint32_t MosInterface::GetPerfTag(MOS_STREAM_HANDLE streamState)
2842 {
2843     uint32_t perfTag;
2844 
2845     MOS_OS_FUNCTION_ENTER;
2846 
2847     if (streamState == nullptr ||
2848         streamState->perStreamParameters == nullptr)
2849     {
2850         MOS_OS_ASSERTMESSAGE("streamState or perStreamParameters invalid nullptr");
2851         return 0;
2852     }
2853 
2854     auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
2855 
2856     perfTag = *(uint32_t *)(osParameters->pPerfData);
2857     return perfTag;
2858 }
2859 
SetPerfHybridKernelID(MOS_STREAM_HANDLE streamState,uint32_t kernelID)2860 void MosInterface::SetPerfHybridKernelID(
2861     MOS_STREAM_HANDLE streamState,
2862     uint32_t          kernelID)
2863 {
2864     MOS_OS_FUNCTION_ENTER;
2865 
2866     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState);
2867     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState->perStreamParameters);
2868 
2869     auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
2870     PERF_DATA *perfData     = osParameters->pPerfData;
2871     MOS_OS_CHK_NULL_NO_STATUS_RETURN(perfData);
2872 
2873     perfData->dmaBufID = (perfData->dmaBufID & 0xF0FF) | ((kernelID << 8) & 0x0F00);
2874 
2875     return;
2876 }
2877 
ResetPerfBufferID(MOS_STREAM_HANDLE streamState)2878 void MosInterface::ResetPerfBufferID(
2879     MOS_STREAM_HANDLE streamState)
2880 {
2881     MOS_OS_FUNCTION_ENTER;
2882 
2883     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState);
2884     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState->perStreamParameters);
2885 
2886     auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
2887 
2888     MOS_OS_CHK_NULL_NO_STATUS_RETURN(osParameters->pPerfData);
2889 
2890     osParameters->pPerfData->bufferID = 0;
2891 
2892     return;
2893 }
2894 
IncPerfBufferID(MOS_STREAM_HANDLE streamState)2895 void MosInterface::IncPerfBufferID(
2896     MOS_STREAM_HANDLE streamState)
2897 {
2898     MOS_OS_FUNCTION_ENTER;
2899 
2900     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState);
2901     MOS_OS_CHK_NULL_NO_STATUS_RETURN(streamState->perStreamParameters);
2902 
2903     auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
2904 
2905     MOS_OS_CHK_NULL_NO_STATUS_RETURN(osParameters->pPerfData);
2906 
2907     osParameters->pPerfData->bufferID++;
2908 
2909     return;
2910 }
2911 
IsGPUHung(MOS_STREAM_HANDLE streamState)2912 int32_t MosInterface::IsGPUHung(
2913     MOS_STREAM_HANDLE streamState)
2914 {
2915     uint32_t     resetCount   = 0;
2916     uint32_t     activeBatch  = 0;
2917     uint32_t     pendingBatch = 0;
2918     int32_t      result       = false;
2919     int32_t      ret          = 0;
2920     PMOS_CONTEXT osParameters = nullptr;
2921 
2922     MOS_OS_FUNCTION_ENTER;
2923 
2924     if (!streamState || !streamState->perStreamParameters)
2925     {
2926         MOS_OS_ASSERTMESSAGE("invalid streamstate");
2927         return false;
2928     }
2929 
2930     osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
2931 
2932     ret = mos_get_reset_stats(osParameters->intel_context, &resetCount, &activeBatch, &pendingBatch);
2933     if (ret)
2934     {
2935         MOS_OS_NORMALMESSAGE("mos_get_reset_stats return error(%d)\n", ret);
2936         return false;
2937     }
2938 
2939     if (resetCount      != streamState->gpuResetCount ||
2940         activeBatch     != streamState->gpuActiveBatch ||
2941         pendingBatch    != streamState->gpuPendingBatch)
2942     {
2943         streamState->gpuResetCount    = resetCount;
2944         streamState->gpuActiveBatch   = activeBatch;
2945         streamState->gpuPendingBatch  = pendingBatch;
2946         result                        = true;
2947     }
2948     else
2949     {
2950         result = false;
2951     }
2952     return result;
2953 }
2954 
IsSetMarkerEnabled(MOS_STREAM_HANDLE streamState)2955 bool MosInterface::IsSetMarkerEnabled(
2956     MOS_STREAM_HANDLE streamState)
2957 {
2958     return false;
2959 }
2960 
GetMarkerResource(MOS_STREAM_HANDLE streamState)2961 PMOS_RESOURCE MosInterface::GetMarkerResource(
2962     MOS_STREAM_HANDLE   streamState)
2963 {
2964     return nullptr;
2965 }
2966 
GetPlaneSurfaceOffset(const MOS_PLANE_OFFSET & planeOffset)2967 int MosInterface::GetPlaneSurfaceOffset(const MOS_PLANE_OFFSET &planeOffset)
2968 {
2969     return planeOffset.iSurfaceOffset;
2970 }
2971 
2972 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
DumpCommandBufferInit(MOS_STREAM_HANDLE streamState)2973 MOS_STATUS MosInterface::DumpCommandBufferInit(
2974     MOS_STREAM_HANDLE streamState)
2975 {
2976     char sFileName[MOS_MAX_HLT_FILENAME_LEN] = {0};
2977     MOS_STATUS eStatus = MOS_STATUS_UNKNOWN;
2978     MOS_USER_FEATURE_VALUE_DATA UserFeatureData = {0};
2979     char *psFileNameAfterPrefix = nullptr;
2980     size_t nSizeFileNamePrefix = 0;
2981 
2982     MOS_OS_CHK_NULL_RETURN(streamState);
2983 
2984     // Check if command buffer dump was enabled in user feature.
2985     MOS_UserFeature_ReadValue_ID(
2986         nullptr,
2987         __MEDIA_USER_FEATURE_VALUE_DUMP_COMMAND_BUFFER_ENABLE_ID,
2988         &UserFeatureData,
2989         (MOS_CONTEXT_HANDLE)streamState->perStreamParameters);
2990     streamState->dumpCommandBuffer            = (UserFeatureData.i32Data != 0);
2991     streamState->dumpCommandBufferToFile      = ((UserFeatureData.i32Data & 1) != 0);
2992     streamState->dumpCommandBufferAsMessages  = ((UserFeatureData.i32Data & 2) != 0);
2993 
2994     if (streamState->dumpCommandBufferToFile)
2995     {
2996         // Create output directory.
2997         eStatus = MosUtilDebug::MosLogFileNamePrefix(streamState->sDirName, nullptr);
2998         if (eStatus != MOS_STATUS_SUCCESS)
2999         {
3000             MOS_OS_NORMALMESSAGE("Failed to create log file prefix. Status = %d", eStatus);
3001             return eStatus;
3002         }
3003 
3004         memcpy(sFileName, streamState->sDirName, MOS_MAX_HLT_FILENAME_LEN);
3005         nSizeFileNamePrefix = strnlen(sFileName, sizeof(sFileName));
3006         MOS_SecureStringPrint(
3007             sFileName + nSizeFileNamePrefix,
3008             sizeof(sFileName) - nSizeFileNamePrefix,
3009             sizeof(sFileName) - nSizeFileNamePrefix,
3010             "%c%s",
3011             MOS_DIR_SEPERATOR,
3012             MOS_COMMAND_BUFFER_OUT_DIR);
3013 
3014         eStatus = MosUtilities::MosCreateDirectory(sFileName);
3015         if (eStatus != MOS_STATUS_SUCCESS)
3016         {
3017             MOS_OS_NORMALMESSAGE("Failed to create output directory. Status = %d", eStatus);
3018             return eStatus;
3019         }
3020     }
3021 
3022     eStatus = MOS_STATUS_SUCCESS;
3023 
3024     return eStatus;
3025 }
3026 #endif  // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
3027 
3028 #if (_DEBUG || _RELEASE_INTERNAL)
3029 
3030 uint32_t MosInterface::m_mosOsApiFailSimulateType         = 0;
3031 uint32_t MosInterface::m_mosOsApiFailSimulateMode         = 0;
3032 uint32_t MosInterface::m_mosOsApiFailSimulateFreq         = 0;
3033 uint32_t MosInterface::m_mosOsApiFailSimulateHint         = 0;
3034 uint32_t MosInterface::m_mosOsApiFailSimulateCounter      = 0;
3035 
MosInitOsApiFailSimulateFlag(MOS_CONTEXT_HANDLE mosCtx)3036 void MosInterface::MosInitOsApiFailSimulateFlag(MOS_CONTEXT_HANDLE mosCtx)
3037 {
3038     MOS_USER_FEATURE_VALUE_DATA userFeatureValueData;
3039     MOS_STATUS                  eStatus = MOS_STATUS_SUCCESS;
3040 
3041     //default off for simulate random fail
3042     m_mosOsApiFailSimulateType         = OS_API_FAIL_TYPE_NONE;
3043     m_mosOsApiFailSimulateMode         = OS_API_FAIL_SIMULATE_MODE_DEFAULT;
3044     m_mosOsApiFailSimulateFreq         = 0;
3045     m_mosOsApiFailSimulateHint         = 0;
3046     m_mosOsApiFailSimulateCounter      = 0;
3047 
3048     // Read Config : memory allocation failure simulate mode
3049     MosUtilities::MosZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
3050     MosUtilities::MosUserFeatureReadValueID(
3051         nullptr,
3052         __MEDIA_USER_FEATURE_VALUE_OS_API_FAIL_SIMULATE_TYPE_ID,
3053         &userFeatureValueData,
3054         mosCtx);
3055 
3056     if (userFeatureValueData.u32Data & OS_API_FAIL_TYPE_MAX)
3057     {
3058         m_mosOsApiFailSimulateType = userFeatureValueData.u32Data;
3059         MOS_OS_NORMALMESSAGE("Init MosSimulateOsApiFailSimulateType as %d \n ", m_mosOsApiFailSimulateType);
3060     }
3061     else
3062     {
3063         m_mosOsApiFailSimulateType = OS_API_FAIL_TYPE_NONE;
3064         MOS_OS_NORMALMESSAGE("Invalid OS API Fail Simulate Type from config: %d \n ", userFeatureValueData.u32Data);
3065     }
3066 
3067     // Read Config : memory allocation failure simulate mode
3068     MosUtilities::MosZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
3069     MosUtilities::MosUserFeatureReadValueID(
3070         nullptr,
3071         __MEDIA_USER_FEATURE_VALUE_OS_API_FAIL_SIMULATE_MODE_ID,
3072         &userFeatureValueData,
3073         mosCtx);
3074 
3075     if ((userFeatureValueData.u32Data == OS_API_FAIL_SIMULATE_MODE_DEFAULT) ||
3076         (userFeatureValueData.u32Data == OS_API_FAIL_SIMULATE_MODE_RANDOM) ||
3077         (userFeatureValueData.u32Data == OS_API_FAIL_SIMULATE_MODE_TRAVERSE))
3078     {
3079         m_mosOsApiFailSimulateMode = userFeatureValueData.u32Data;
3080         MOS_OS_NORMALMESSAGE("Init MosSimulateOsApiFailSimulateMode as %d \n ", m_mosOsApiFailSimulateMode);
3081     }
3082     else
3083     {
3084         m_mosOsApiFailSimulateMode = OS_API_FAIL_SIMULATE_MODE_DEFAULT;
3085         MOS_OS_NORMALMESSAGE("Invalid OS API Fail Simulate Mode from config: %d \n ", userFeatureValueData.u32Data);
3086     }
3087 
3088     // Read Config : memory allocation failure simulate frequence
3089     MosUtilities::MosZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
3090     MosUtilities::MosUserFeatureReadValueID(
3091         nullptr,
3092         __MEDIA_USER_FEATURE_VALUE_OS_API_FAIL_SIMULATE_FREQ_ID,
3093         &userFeatureValueData,
3094         mosCtx);
3095 
3096     if ((userFeatureValueData.u32Data >= MIN_OS_API_FAIL_FREQ) &&
3097         (userFeatureValueData.u32Data <= MAX_OS_API_FAIL_FREQ))
3098     {
3099         m_mosOsApiFailSimulateFreq = userFeatureValueData.u32Data;
3100         MOS_OS_NORMALMESSAGE("Init m_MosSimulateRandomOsApiFailFreq as %d \n ", m_mosOsApiFailSimulateFreq);
3101 
3102         if (m_mosOsApiFailSimulateMode == OS_API_FAIL_SIMULATE_MODE_RANDOM)
3103         {
3104             srand((unsigned int)time(nullptr));
3105         }
3106     }
3107     else
3108     {
3109         m_mosOsApiFailSimulateFreq = 0;
3110         MOS_OS_NORMALMESSAGE("Invalid OS API Fail Simulate Freq from config: %d \n ", userFeatureValueData.u32Data);
3111     }
3112 
3113     // Read Config : memory allocation failure simulate counter
3114     MosUtilities::MosZeroMemory(&userFeatureValueData, sizeof(userFeatureValueData));
3115     MosUtilities::MosUserFeatureReadValueID(
3116         nullptr,
3117         __MEDIA_USER_FEATURE_VALUE_OS_API_FAIL_SIMULATE_HINT_ID,
3118         &userFeatureValueData,
3119         mosCtx);
3120 
3121     if (userFeatureValueData.u32Data <= m_mosOsApiFailSimulateFreq)
3122     {
3123         m_mosOsApiFailSimulateHint = userFeatureValueData.u32Data;
3124         MOS_OS_NORMALMESSAGE("Init m_MosOsApiFailSimulateHint as %d \n ", m_mosOsApiFailSimulateHint);
3125     }
3126     else
3127     {
3128         m_mosOsApiFailSimulateHint = m_mosOsApiFailSimulateFreq;
3129         MOS_OS_NORMALMESSAGE("Set m_mosOsApiFailSimulateHint as %d since INVALID CONFIG %d \n ", m_mosOsApiFailSimulateHint, userFeatureValueData.u32Data);
3130     }
3131 }
3132 
MosDeinitOsApiFailSimulateFlag()3133 void MosInterface::MosDeinitOsApiFailSimulateFlag()
3134 {
3135     //default off for simulate fail
3136     m_mosOsApiFailSimulateType    = OS_API_FAIL_TYPE_NONE;
3137     m_mosOsApiFailSimulateMode    = OS_API_FAIL_SIMULATE_MODE_DEFAULT;
3138     m_mosOsApiFailSimulateFreq    = 0;
3139     m_mosOsApiFailSimulateHint    = 0;
3140     m_mosOsApiFailSimulateCounter = 0;
3141 }
3142 
MosSimulateOsApiFail(OS_API_FAIL_TYPE type,const char * functionName,const char * filename,int32_t line)3143 bool MosInterface::MosSimulateOsApiFail(
3144     OS_API_FAIL_TYPE type,
3145     const char *functionName,
3146     const char *filename,
3147     int32_t     line)
3148 {
3149     bool bSimulateOsApiFail = false;
3150 
3151     if (!MosOsApiFailSimulationEnabled(type))
3152     {
3153         return false;
3154     }
3155 
3156     if (m_mosOsApiFailSimulateMode == OS_API_FAIL_SIMULATE_MODE_RANDOM)
3157     {
3158         int32_t Rn = rand();
3159         m_mosOsApiFailSimulateCounter++;
3160         if (Rn % m_mosOsApiFailSimulateFreq == 1)
3161         {
3162             bSimulateOsApiFail = true;
3163             MOS_DEBUGMESSAGE(MOS_MESSAGE_LVL_CRITICAL, MOS_COMPONENT_OS, MOS_SUBCOMP_SELF,
3164                 "Simulated OS API(Type %d) Fail (Rn=%d, SimulateAllocCounter=%d) for: functionName: %s, filename: %s, line: %d\n",
3165                 m_mosOsApiFailSimulateType, Rn, m_mosOsApiFailSimulateCounter, functionName, filename, line);
3166         }
3167         else
3168         {
3169             bSimulateOsApiFail = false;
3170         }
3171     }
3172     else if (m_mosOsApiFailSimulateMode == OS_API_FAIL_SIMULATE_MODE_TRAVERSE)
3173     {
3174         if (m_mosOsApiFailSimulateCounter++ == m_mosOsApiFailSimulateHint)
3175         {
3176             MOS_DEBUGMESSAGE(MOS_MESSAGE_LVL_CRITICAL, MOS_COMPONENT_OS, MOS_SUBCOMP_SELF,
3177                 "Simulated OS API(Type %d) Fail (hint=%d) for: functionName: %s, filename: %s, line: %d\n",
3178                 m_mosOsApiFailSimulateType, m_mosOsApiFailSimulateHint, functionName, filename, line);
3179             bSimulateOsApiFail = true;
3180         }
3181         else
3182         {
3183             bSimulateOsApiFail = false;
3184         }
3185     }
3186     else
3187     {
3188         //MOS_OS_NORMALMESSAGE("Invalid m_mosOsApiFailSimulateMode: %d \n ", m_mosOsApiFailSimulateMode);
3189         bSimulateOsApiFail = false;
3190     }
3191 
3192     return bSimulateOsApiFail;
3193 }
3194 #endif  // #if (_DEBUG || _RELEASE_INTERNAL)
3195 
IsAsyncDevice(MOS_STREAM_HANDLE streamState)3196 bool MosInterface::IsAsyncDevice(MOS_STREAM_HANDLE streamState)
3197 {
3198     return false;
3199 }
3200