1 /*
2 * Copyright (c) 2009-2020, 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.h
24 //! \brief    MOS interface definition
25 //!
26 //! Device level: OsDeviceContext in device/Libva context.
27 //!               The global level of entire media driver instance in user space.
28 //!               There could be multiple devices in a single process.
29 //! Stream level: OsStreamState in Hal instances (Codec pipeline, VP pipeline, CM device, CP session, etc)
30 //!               Each Hal instance can have an OsStreamState to indicate that stream's state specific to OS.
31 //!               Each Device can have multiple streams.
32 //!               All OsStreamStates must be binded with a valid OsDeviceContext to indicate the inclusion relationship
33 //!               between device and stream in DDI
34 //!
35 //! MOS interface provide following OS services: (OS services are abstracted and diff OS behavior is tranparent to MOS customers)
36 //! 1) Workload scheduling (GPU context, cmdbuffer, sync, virtual engine, etc)
37 //! 2) Resource managment (Graphic resource, external resource)
38 //! 3) Utilities (Abstraction of generalized system call)
39 //! 4) Performance interface
40 //! 5) Debug interface
41 //!
42 //! Caller: DDI, Media interface, HAL, MHW
43 //! Any interface func returning MOS_STATUS_UNKNOWN mean Device level is go into unstable situation.
44 //! Caller needs to make sure exiting properly.
45 
46 
47 #ifndef __MOS_INTERFACE_H__
48 #define __MOS_INTERFACE_H__
49 
50 #include "mos_defs.h"
51 #include "mos_os.h"
52 
53 class GpuContextSpecificNext;
54 struct _MOS_VIRTUALENGINE_SET_PARAMS;
55 struct _MOS_VIRTUALENGINE_INIT_PARAMS;
56 typedef struct _MOS_VIRTUALENGINE_SET_PARAMS  MOS_VIRTUALENGINE_SET_PARAMS, *PMOS_VIRTUALENGINE_SET_PARAMS;
57 typedef struct _MOS_VIRTUALENGINE_INIT_PARAMS MOS_VIRTUALENGINE_INIT_PARAMS, *PMOS_VIRTUALENGINE_INIT_PARAMS;
58 typedef struct _MOS_CMD_BUF_ATTRI_VE MOS_CMD_BUF_ATTRI_VE, *PMOS_CMD_BUF_ATTRI_VE;
59 class MosInterface
60 {
61 protected:
62     //!
63     //! \brief   Destructor
64     //! \details There is no members in Mos Interface, it's pure interface.
65     //!          Never call the Destructor of Mos interface
66     //!
67     ~MosInterface() = default;
68 
69     //!
70     //! \brief   Constructor
71     //! \details There is no members in Mos Interface, it's pure interface.
72     //!          Never call the Constructor of Mos interface
73     //!
74     MosInterface() = default;
75 
76 public:
77     //!
78     //! \brief    Init Os Utilities
79     //! \details  Include Utilities, user settings key, mem ninja etc
80     //! \details  Must be first called MOS interface before CreateOsDeviceContext
81     //! \details  Caller: DDI only.
82     //!
83     //! \param    [in] ddiDeviceContext
84     //!           Pointer of device context in DDI to init Os Device Context
85     //!
86     //! \return   MOS_STATUS
87     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
88     //!
89     static MOS_STATUS InitOsUtilities(DDI_DEVICE_CONTEXT ddiDeviceContext);
90 
91     //!
92     //! \brief    Close Os Utilities
93     //! \details  Include Utilities, user settings key, mem ninja etc
94     //! \details  Must be last called MOS interface after DestroyOsDeviceContext
95     //! \details  Caller: DDI only.
96     //!
97     //! \param    [in] mosCtx
98     //!           Pointer of device context in DDI for reg ops
99     //!
100     //! \return   MOS_STATUS
101     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
102     //!
103     static MOS_STATUS CloseOsUtilities(PMOS_CONTEXT mosCtx);
104 
105     //!
106     //! \brief    Create Os Device Context
107     //! \details  Create the Os Device Context in device level.
108     //! \details  Caller: DDI only.
109     //! \details  The Os Device Context is a singleton in the device, DDI must make sure call this only once.
110     //!           If the creation failed, DDI must yield to continue the initialization of device.
111     //!
112     //! \param    [in] ddiDeviceContext
113     //!           Pointer of device context in DDI to init Os Device Context
114     //! \param    [out] deviceContext
115     //!           Handle of Os Device Context to create. If creation failed, it is INVALID_HANLE.
116     //!           OsDeviceContext is a device level singleton which stores the states, info specific to OS.
117     //!           It contain sub modules of MOS to transfer OS specific services to OS agnositic abstractions.
118     //!
119     //! \return   MOS_STATUS
120     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
121     //!
122     static MOS_STATUS CreateOsDeviceContext(DDI_DEVICE_CONTEXT ddiDeviceContext, MOS_DEVICE_HANDLE *deviceContext);
123 
124     //!
125     //! \brief    Destroy Os Device Context
126     //! \details  Destroy the Os Device Context in device level
127     //! \details  Caller: DDI only.
128     //!
129     //! \param    [in] deviceContext
130     //!           Handle of Os Device Context to Destroy
131     //!
132     //! \return   MOS_STATUS
133     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
134     //!
135     static MOS_STATUS DestroyOsDeviceContext(MOS_DEVICE_HANDLE deviceContext);
136 
137     //!
138     //! \brief    Create Os Stream State
139     //! \details  Create the Os Stream State in stream level.
140     //! \details  Caller: DDI
141     //! \details  In DDI, one stream (Hal instance) can only create one Os Stream State corresponding to it.
142     //!           Os Stream state directly created by DDI is not corresponding any streams (Hal instances)
143     //!
144     //! \param    [out] streamState
145     //!           Handle of Os Stream State to create. If creation failed, it is INVALID_HANLE.
146     //!           OsStreamState is a stream level state which stores the flags, info specific to OS specfic to that stream.
147     //!           It is be binded with a valid OsDeviceContext to indicate the inclusion relationship between device and stream.
148     //! \param    [in] deviceContext
149     //!           Device context to init streamState
150     //! \param    [in] osInterface
151     //!           Os interface to store streamState
152     //! \param    [in] component
153     //!           Indicate which component the stream state to create belongs to
154     //! \param    [in] extraParams
155     //!           Additional parameters needed to init streamstate
156     //!
157     //! \return   MOS_STATUS
158     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
159     //!
160     static MOS_STATUS CreateOsStreamState(
161         MOS_STREAM_HANDLE       *streamState,
162         MOS_DEVICE_HANDLE       deviceContext,
163         MOS_INTERFACE_HANDLE    osInterface,
164         MOS_COMPONENT           component,
165         EXTRA_PARAMS            extraParams = nullptr);
166 
167     //!
168     //! \brief    Destroy Os Stream State
169     //! \details  Destroy the Os Stream State in stream level
170     //! \details  Caller: DDI
171     //!
172     //! \param    [in] streamState
173     //!           Handle of Os Stream State to Destroy
174     //!
175     //! \return   MOS_STATUS
176     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
177     //!
178     static MOS_STATUS DestroyOsStreamState(
179         MOS_STREAM_HANDLE streamState);
180 
181     //!
182     //! \brief    Get OS runtime interface version
183     //! \details  [System info Interface] Get OS runtime interface version
184     //! \details  Caller: DDI only
185     //! \details  Only DDI can derive diff behavior due to OS runtime interface version
186     //!
187     //! \param    [in] deviceContext
188     //!           Handle of Os Device Context
189     //!
190     //! \return   uint32_t
191     //!           Read-only OS runtime interface version, it's meaning diff from OS and API
192     //!
193     static uint32_t GetInterfaceVersion(MOS_DEVICE_HANDLE deviceContext);
194 
195     //!
196     //! \brief    Get Platform
197     //! \details  [System info Interface] Get Get Platform information
198     //! \details  Caller: DDI & HAL & MHW
199     //! \details  This func is called in DDI only to generate hal instance stand for specific platform.
200     //!           This func can be used in HAL & MHW to get platfrom detailed info to judge the path of different behavior.
201     //!
202     //! \param    [in] streamState
203     //!           Handle of Os Stream State
204     //!
205     //! \return   PLATFORM
206     //!           Gfx driver shared enum of platform got. Read-only.
207     //!
208     static PLATFORM *GetPlatform(MOS_STREAM_HANDLE streamState);
209 
210     //!
211     //! \brief    Get SkuTable
212     //! \details  [System info Interface] Get Sku Table
213     //! \details  Caller: DDI & HAL & MHW
214     //! \details  This func is called to differentiate the behavior according to SKU table.
215     //!
216     //! \param    [in] streamState
217     //!           Handle of Os Stream State
218     //!
219     //! \return   MEDIA_FEATURE_TABLE*
220     //!           Read-only SKU table got, nullptr if failed to get
221     //!
222     static MEDIA_FEATURE_TABLE *GetSkuTable(MOS_STREAM_HANDLE streamState);
223 
224     //!
225     //! \brief    Get WaTable
226     //! \details  [System info Interface] Get WA Table
227     //! \details  Caller: DDI & HAL & MHW
228     //! \details  This func is called to differentiate the behavior according to WA table.
229     //!
230     //! \param    [in] streamState
231     //!           Handle of Os Stream State
232     //!
233     //! \return   MEDIA_WA_TABLE*
234     //!           Read-only WA table got, nullptr if failed to get
235     //!
236     static MEDIA_WA_TABLE *GetWaTable(MOS_STREAM_HANDLE streamState);
237 
238     //!
239     //! \brief    Get Gt System Info
240     //! \details  [System info Interface] Get Gt System Info
241     //! \details  Caller: HAL & MHW
242     //! \details  This func is called to differentiate the behavior according to Gt System Info.
243     //!
244     //! \param    [in] streamState
245     //!           Handle of Os Stream State
246     //!
247     //! \return   MEDIA_SYSTEM_INFO*
248     //!           Read-only GT system info got, nullptr if failed to get
249     //!
250     static MEDIA_SYSTEM_INFO *GetGtSystemInfo(MOS_STREAM_HANDLE streamState);
251 
252     //!
253     //! \brief    Get Media Engine Info
254     //! \details  [System info Interface] Get Media Engine Info
255     //! \details  Caller: HAL & MHW
256     //! \details  This func is called to differentiate the behavior according to Media Engine Info.
257     //!
258     //! \param    [in] streamState
259     //!           Handle of Os Stream State
260     //! \param    [in] info
261     //!           MEDIA_SYS_INFO
262     //!
263     //! \return   MOS_STATUS
264     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
265     //!
266     static MOS_STATUS GetMediaEngineInfo(MOS_STREAM_HANDLE streamState, MEDIA_ENGINE_INFO &info);
267 
268     //!
269     //! \brief    Get Adapter Info
270     //! \details  [System info Interface] Get Adapter Info
271     //! \details  Caller: DDI & HAL
272     //! \details  This func is called to differentiate the behavior according to Adapter Info.
273     //!
274     //! \param    [in] streamState
275     //!           Handle of Os Stream State
276     //!
277     //! \return   ADAPTER_INFO*
278     //!           Read-only Adapter Info got, nullptr if failed to get
279     //!
280     static ADAPTER_INFO *GetAdapterInfo(MOS_STREAM_HANDLE streamState);
281 
282     //!
283     //! \brief    Get current gmmclientcontext
284     //! \details  Get current gmmclientcontext
285     //!
286     //! \param    [in] streamState
287     //!           Handle of Os Stream State
288     //!
289     //! \return   GMM_CLIENT_CONTEXT
290     //!           Current gmmclientcontext
291     //!
292     static GMM_CLIENT_CONTEXT *GetGmmClientContext(
293         MOS_STREAM_HANDLE streamState);
294 
295     //!
296     //! \brief    Get current Gpu context priority
297     //! \details  Get current Gpu context priority
298     //!
299     //! \param    [in] streamState
300     //!           Handle of Os Stream State
301     //!
302     //!           [out]
303     //!           Current Gpu context priority
304     //!
305     static void GetGpuPriority(MOS_STREAM_HANDLE streamState, int32_t* priority);
306 
307     //!
308     //! \brief    Set current Gpu context priority
309     //! \details  Set current Gpu context priority
310     //!
311     //! \param    [in] streamState
312     //!           Handle of Os Stream State
313     //!           [in] priority
314     //!           priority to set for gpu context
315     //!
316     static void SetGpuPriority(MOS_STREAM_HANDLE streamState, int32_t priority);
317 
318     //!
319     //! \brief    Get AuxTable base address
320     //!
321     //! \param    [in] streamState
322     //!           Handle of Os Stream State
323     //! \return   uint64_t
324     //!           64bit base address value of AuxTable
325     //!
326     static uint64_t GetAuxTableBaseAddr(
327         MOS_STREAM_HANDLE streamState);
328 
329     //!
330     //! \brief    Create Gpu Context
331     //! \details  [GPU Context Interface] Create Gpu Context to submit cmdbuffers
332     //! \details  Caller: HAL (Media Context) only
333     //! \details  This func is called when a stream (Hal instance) needs a SW queue to submit cmd buffers programmed with GPU cmds.
334     //! \details  This queue contain options to indicate the properties of virtual GPU engine to execute these cmds.
335     //! \details  Caller can use Usage & option & GPU_CONTEXT_HANDLE to track and re-use the GPU contexts.
336     //!
337     //! \param    [in] streamState
338     //!           Handle of Os Stream State
339     //! \param    [in] createOption
340     //!           Properties of Gpu context to create. They stand for the request from HAL on the Gpu context.
341     //!           The request include engine type, pipe count, restrictions, etc.
342     //! \param    [out] gpuContext
343     //!           Handle of gpu Context created. If creation failed, it is INVALID_HANLE
344     //!           GPU context stands for a SW queue in user space to submit cmd buffers FIFO.
345     //!
346     //! \return   MOS_STATUS
347     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
348     //!
349     static MOS_STATUS CreateGpuContext(
350         MOS_STREAM_HANDLE streamState,
351         GpuContextCreateOption &createOption,
352         GPU_CONTEXT_HANDLE &gpuContext);
353 
354     //!
355     //! \brief    Destroy Gpu Context
356     //! \details  [GPU Context Interface] Destroy Gpu Context to submit cmdbuffers
357     //! \details  Caller: HAL (Media Context) only
358     //! \details  This func is called when a stream (Hal instance) never needs this SW queue to submit cmd buffers
359     //! \details  This func is called only in the destruction stage of Hal instance.
360     //!           Never should be SetGpuContext called to set destroied Gpu Context.
361     //!
362     //! \param    [in] streamState
363     //!           Handle of Os Stream State
364     //! \param    [in] gpuContext
365     //!           Handle of gpu Context to destroy.
366     //!
367     //! \return   MOS_STATUS
368     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
369     //!
370     static MOS_STATUS DestroyGpuContext(
371         MOS_STREAM_HANDLE streamState,
372         GPU_CONTEXT_HANDLE gpuContext);
373 
374     //!
375     //! \brief    Set Gpu Context
376     //! \details  [GPU Context Interface] Set current Gpu Context to submit cmd buffers for the stream(Hal instance)
377     //! \details  Caller: HAL (Media Context) only
378     //! \details  This func is called when a stream (Hal instance) needs an existing GPU context to submit cmd buffers.
379     //! \details  Current GPU context is the major state of Os Stream State.
380     //! \details  Before getting a cmd buffer to program GPU cmds, a valid GPU context must be setted into the stream.
381     //! \details  Calling sequence is like: SetGpuContext -> GetCommandBuffer -> AddCommand ...
382     //!           -> ReturnCommandBuffer -> SubmitCommandBuffer -> SetGpuContext ...
383     //! \details  If Current GPU context is never set, all command buffer / resource interfaces cannot be used.
384     //!           (They will return MOS_STATUS_INVALID_GPU_CONTEXT)
385     //!
386     //! \param    [in] streamState
387     //!           Handle of Os Stream State
388     //! \param    [in] gpuContext
389     //!           Current handle of gpu Context to set.
390     //!
391     //! \return   MOS_STATUS
392     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
393     //!
394     static MOS_STATUS SetGpuContext(
395         MOS_STREAM_HANDLE streamState,
396         GPU_CONTEXT_HANDLE gpuContext);
397 
398     //!
399     //! \brief    Get GPU context pointer
400     //! \details  Get GPU context pointer
401     //! \param    [in] streamState
402     //!           Handle of Os Stream State
403     //! \param    GPU_CONTEXT_HANDLE gpuContextHandle
404     //!           [in] GPU Context Handle
405     //! \return   void *
406     //!           a pointer to a gpu context
407     //!
408     static void *GetGpuContextbyHandle(
409         MOS_STREAM_HANDLE  streamState,
410         GPU_CONTEXT_HANDLE gpuContextHandle);
411 
412     //! \brief    Sets the object capture flags for Linux OCA dump
413     //! \details  Sets the object capture flags for Linux OCA dump
414     //!
415     //! \param    PMOS_RESOURCE osResource
416     //!           [in] osResource
417     //! \return   MOS_STATUS
418     //!           Return MOS_STATUS_SUCCESS if success else failure reason
419     //!
420     static MOS_STATUS SetObjectCapture(
421         PMOS_RESOURCE osResource);
422 
423     //!
424     //! \brief   Get GpuContext
425     //! \details MOS internal toolset func to get GPU context instance
426     //!
427     //! \param    [in] streamState
428     //!           Handle of Os Stream State
429     //! \param    [in] gpuContext
430     //!           MOS GPU Context handle
431     //!
432     //! \return   GpuContextSpecificNext
433     //!           GPU Context instance got by GPU context handle, nullptr if get failed
434     //!
435     static GpuContextSpecificNext *GetGpuContext(MOS_STREAM_HANDLE streamState, GPU_CONTEXT_HANDLE handle);
436 
437     //!
438     //! \brief    Add Command
439     //! \details  [Cmd Buffer Interface] Add gpu commands into cmd buffer
440     //! \details  Caller: MHW only
441     //! \details  It is not device stated function and can be used in both APO MHW and NON-APO MOS.
442     //! \details  This func is called when a stream (Hal instance) adds gpu cmds into cmd buffer.
443     //! \details  Before getting a cmd buffer to program GPU cmds, a valid GPU context must be setted into the stream.
444     //! \details  Calling sequence is like: SetGpuContext -> GetCommandBuffer -> AddCommand ...
445     //!           -> ReturnCommandBuffer -> SubmitCommandBuffer -> SetGpuContext ...
446     //! \details  If Current GPU context is never set, all command buffer / resource interfaces cannot be used.
447     //!           (They will return MOS_STATUS_INVALID_GPU_CONTEXT)
448     //!
449     //! \param    [in] cmdBuffer
450     //!           Handle of cmd buffer to add cmd. cmd buffer handle can be get by calling GetCommandBuffer.
451     //! \param    [in] cmd
452     //!           Pointer to the memory to indicate cmd, caller must make sure it's valid.
453     //! \param    [in] cmdSize
454     //!           Size of cmd to program.
455     //!
456     //! \return   MOS_STATUS
457     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
458     //!
459     static MOS_STATUS AddCommand(
460         COMMAND_BUFFER_HANDLE cmdBuffer,
461         const void *cmd,
462         uint32_t   cmdSize);
463 
464 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
465     //!
466     //! \brief    Dump Indirect state in Command Buffer
467     //!
468     //! \param    [in] streamState
469     //!           Handle of Os Stream State
470     //! \param    [in] cmdBuffer
471     //!           Handle of cmd buffer to add cmd. cmd buffer handle can be get by calling GetCommandBuffer.
472     //! \param    [in] gpuNode
473     //!           Gpu node.
474     //! \param    [in] filePathPrefix
475     //!           The prefix for indirect state dump file.
476     //!
477     //! \return   MOS_STATUS
478     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
479     //!
480     static MOS_STATUS DumpIndirectState(
481         MOS_STREAM_HANDLE     streamState,
482         COMMAND_BUFFER_HANDLE cmdBuffer,
483         MOS_GPU_NODE          gpuNode,
484         const char            *filePathPrefix);
485 
486     //!
487     //! \brief    Dump Command Buffer
488     //! \details  [Cmd Buffer Interface] Dump an existing cmd buffer
489     //! \details  Caller: HAL only
490     //! \details  This func is called when a stream (Hal instance) needs to dump cmd buffer.
491     //! \details  Only after ReturnCommandBuffer can Command Buffer being dumped
492     //! \details  If Current GPU context is never set, all command buffer / resource interfaces cannot be used.
493     //!           (They will return MOS_STATUS_INVALID_GPU_CONTEXT)
494     //!
495     //! \param    [in] streamState
496     //!           Handle of Os Stream State
497     //! \param    [in] cmdBuffer
498     //!           Handle of cmd buffer to add cmd. cmd buffer handle can be get by calling GetCommandBuffer.
499     //!
500     //! \return   MOS_STATUS
501     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
502     //!
503     static MOS_STATUS DumpCommandBuffer(
504         MOS_STREAM_HANDLE streamState,
505         COMMAND_BUFFER_HANDLE cmdBuffer);
506 #endif  // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
507 
508     //!
509     //! \brief    Get Command Buffer
510     //! \details  [Cmd Buffer Interface] Get current cmd buffer to program based on streamState
511     //! \details  Caller: HAL only
512     //! \details  This func is called when a stream (Hal instance) needs to get a cmd buffer corresponding to current GPU context in os stream state.
513     //! \details  Before getting a cmd buffer to program GPU cmds, a valid GPU context must be setted into the stream.
514     //! \details  Calling sequence is like: SetGpuContext -> GetCommandBuffer -> AddCommand ...
515     //!           -> ReturnCommandBuffer -> SubmitCommandBuffer -> SetGpuContext ...
516     //! \details  If Current GPU context is never set, all command buffer / resource interfaces cannot be used.
517     //!           (They will return MOS_STATUS_INVALID_GPU_CONTEXT)
518     //!
519     //! \param    [in] streamState
520     //!           Handle of Os Stream State
521     //! \param    [out] cmdBuffer
522     //!           Handle of cmd buffer to get. If get failed, it is INVALID_HANLE.
523     //! \param    [in] pipeIdx
524     //!           Pipe index to indicate which pipe's cmdbuffer to get.
525     //!           In frame split, pipe index is indicated to get secondary cmd buffers. They are cmd buffers split to different engines.
526     //!
527     //! \return   MOS_STATUS
528     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
529     //!
530     static MOS_STATUS GetCommandBuffer(
531         MOS_STREAM_HANDLE streamState,
532         COMMAND_BUFFER_HANDLE &cmdBuffer,
533         uint32_t pipeIdx = 0);
534 
535     //!
536     //! \brief    Return Command Buffer
537     //! \details  [Cmd Buffer Interface] Return current cmd buffer to the MOS
538     //! \details  Caller: HAL only
539     //! \details  This func is called when a stream (Hal instance) finished add cmds into a cmd buffer.
540     //! \details  ReturnCommandBuffer must be called before submit cmd buffer. MOS will do necessary operations in this interface.
541     //! \details  Calling sequence is like: SetGpuContext -> GetCommandBuffer -> AddCommand ...
542     //!           -> ReturnCommandBuffer -> SubmitCommandBuffer -> SetGpuContext ...
543     //! \details  If Current GPU context is never set, all command buffer / resource interfaces cannot be used.
544     //!           (They will return MOS_STATUS_INVALID_GPU_CONTEXT)
545     //!
546     //! \param    [in] streamState
547     //!           Handle of Os Stream State
548     //! \param    [in] cmdBuffer
549     //!           Handle of cmd buffer to return.
550     //! \param    [in] pipeIdx
551     //!           Pipe index to indicate which pipe's cmdbuffer to get.
552     //!           In frame split, pipe index is indicated to get secondary cmd buffers. They are cmd buffers split to different engines.
553     //!
554     //! \return   MOS_STATUS
555     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
556     //!
557     static MOS_STATUS ReturnCommandBuffer(
558         MOS_STREAM_HANDLE streamState,
559         COMMAND_BUFFER_HANDLE cmdBuffer,
560         uint32_t pipeIdx = 0);
561 
562     //!
563     //! \brief    Submit Command Buffer
564     //! \details  [Cmd Buffer Interface] Submit current cmd buffer to current GPU context queue.
565     //! \details  Caller: HAL only
566     //! \details  When a stream (Hal instance) call this interface, cmd buffer is enqueued into current GPU context in streamState.
567     //! \details  OS runtime and KMD will schedule the workload. HW cmds in the cmd buffer will be executed in HW engines.
568     //! \details  Calling sequence is like: SetGpuContext -> GetCommandBuffer -> AddCommand ...
569     //!           -> ReturnCommandBuffer -> SubmitCommandBuffer -> SetGpuContext ...
570     //! \details  If Current GPU context is never set, all command buffer / resource interfaces cannot be used.
571     //!           (They will return MOS_STATUS_INVALID_GPU_CONTEXT)
572     //! \details  Cmd buffer execution in GPU context is async with Hal programming. Return of this interface does not guarantee finish executing actual cmds.
573     //!
574     //! \param    [in] streamState
575     //!           Handle of Os Stream State
576     //! \param    [in] cmdBuffer
577     //!           Handle of cmd buffer to Submit.
578     //!           If there is frame split case (more than 1 pipe) in current GPU context queue, this is primary cmd buffer handle.
579     //! \param    [in] nullRendering
580     //!           Flag to indicate if not actually submit workload into HW.
581     //!
582     //! \return   MOS_STATUS
583     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
584     //!
585     static MOS_STATUS SubmitCommandBuffer(
586         MOS_STREAM_HANDLE streamState,
587         COMMAND_BUFFER_HANDLE cmdBuffer,
588         bool nullRendering = false);
589 
590     //!
591     //! \brief    Reset Command Buffer
592     //! \details  [Cmd Buffer Interface] Reset cmd buffer to the initialized state.
593     //! \details  Caller: HAL only
594     //! \details  ResetCommandBuffer can be called after a stream (Hal instance) call GetCommandBuffer.
595     //! \details  OS runtime and KMD will schedule the workload. HW cmds in the cmd buffer will be executed in HW engines.
596     //! \details  Calling sequence is like: SetGpuContext -> GetCommandBuffer (-> ResetCommandBuffer) -> AddCommand ...
597     //! \details  If Current GPU context is never set, all command buffer / resource interfaces cannot be used.
598     //!           (They will return MOS_STATUS_INVALID_GPU_CONTEXT)
599     //! \details  Cmd buffer reset means stream starts to program a new set of cmds into a cmd buffer got.
600     //!           This interface must not be called when the cmd buffer already programed cmds and not submitted unless the stream needs to drop these cmds.
601     //!
602     //! \param    [in] streamState
603     //!           Handle of Os Stream State
604     //! \param    [in, out] cmdBuffer
605     //!           Handle of cmd buffer to reset.
606     //!           If there is frame split case (more than 1 pipe) in current GPU context queue, this is primary cmd buffer handle.
607     //!
608     //! \return   MOS_STATUS
609     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
610     //!
611     static MOS_STATUS ResetCommandBuffer(
612         MOS_STREAM_HANDLE streamState,
613         COMMAND_BUFFER_HANDLE cmdBuffer);
614 
615     //!
616     //! \brief    Verify Command Buffer Size
617     //! \details  [Cmd Buffer Interface] Check if cmd buffer size is larger than the requested size
618     //! \details  Caller: HAL only
619     //!
620     //! \param    [in] streamState
621     //!           Handle of Os Stream State
622     //! \param    [in, out] cmdBuffer
623     //!           Handle of cmd buffer to verify size
624     //! \param    [in] requestedSize
625     //!           Requested size
626     //! \param    [in] pipeIdx
627     //!           Pipe index to indicate which pipe's cmdbuffer to verify.
628     //!           In frame split, pipe index is indicated to get secondary cmd buffers. They are cmd buffers split to different engines.
629     //!
630     //! \return   MOS_STATUS
631     //!           Return MOS_STATUS_SUCCESS if successful, MOS_STATUS_UNKNOWN if size does not meet the requirment, otherwise failed
632     //!
633     static MOS_STATUS VerifyCommandBufferSize(
634         MOS_STREAM_HANDLE streamState,
635         COMMAND_BUFFER_HANDLE cmdBuffer,
636         uint32_t requestedSize,
637         uint32_t pipeIdx = 0);
638 
639     //!
640     //! \brief    Resize Command Buffer and Patch List
641     //! \details  [Cmd Buffer Interface] Resize the cmd buffer to contain more cmds. Resize the patch list to have more resource.s
642     //! \details  Caller: HAL only
643     //! \details  ResizeCommandBuffer can be called at any time if providing valid a cmd buffer.
644     //!           When cmds number to be added is increased, this interface needs to be called.
645     //!           MOS will make sure the existing cmds copied to the resized cmd buffer.
646     //!           Patch list contain the entries to patch cmds. When cmds number to be added is increased, this interface needs to be called.
647     //! \details  Recommand to call this interface only once for a specific cmd buffer with a conservative requestedSize.
648     //!
649     //! \param    [in] streamState
650     //!           Handle of Os Stream State
651     //! \param    [in, out] cmdBuffer
652     //!           Handle of cmd buffer to resize.
653     //! \param    [in] requestedSize
654     //!           Requested size. If the size already larger than the requirement, no operations is done to cmd buffer.
655     //! \param    [in] requestedPatchListSize
656     //!           Requested patch list size. If the size already larger than the requirement, no operations is done to cmd buffer.
657     //! \param    [in] pipeIdx
658     //!           Pipe index to indicate which pipe's cmdbuffer to resize.
659     //!           In frame split, pipe index is indicated to get secondary cmd buffers. They are cmd buffers split to different engines.
660     //!
661     //! \return   MOS_STATUS
662     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
663     //!
664     static MOS_STATUS ResizeCommandBufferAndPatchList(
665         MOS_STREAM_HANDLE streamState,
666         COMMAND_BUFFER_HANDLE cmdBuffer,
667         uint32_t requestedSize,
668         uint32_t requestedPatchListSize,
669         uint32_t pipeIdx = 0);
670 
671     //!
672     //! \brief    Set Patch Entry
673     //! \details  [Cmd Buffer Interface] Set a patch entry in cmd buffer.
674     //! \details  Caller: MHW only
675     //! \details  This interface is called only when adding a resource into a cmd.
676     //!           The entries in cmd buffer indicate the gfx address to be patched.
677     //!
678     //! \param    [in] streamState
679     //!           Handle of Os Stream State
680     //! \param    [in] params
681     //!           Pointer to the patch entry parameters.
682     //!
683     //! \return   MOS_STATUS
684     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
685     //!
686     static MOS_STATUS SetPatchEntry(
687         MOS_STREAM_HANDLE streamState,
688         PMOS_PATCH_ENTRY_PARAMS params);
689 
690     //!
691     //! \brief    Get Indirect State
692     //! \details  [Cmd Buffer Interface] Get the indirect state in cmd buffer.
693     //! \details  Caller: MHW only
694     //! \details  This interface is called when preparing indirect state data in cmd buffer.
695     //!           Indirect state is a reserved region in cmd buffer which contains the data needed by execute Media kernel.
696     //!
697     //! \param    [in] streamState
698     //!           Handle of Os Stream State
699     //! \param    [out] indirectState
700     //!           Pointer to pointer to indirectState data. MHW can use this ptr to set data.
701     //! \param    [out] offset
702     //!           Offset of indirect state in the cmd buffer.
703     //! \param    [out] size
704     //!           Size of indirect state in the cmd buffer.
705     //!
706     //! \return   MOS_STATUS
707     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
708     //!
709     static MOS_STATUS GetIndirectState(
710         MOS_STREAM_HANDLE streamState,
711         uint8_t **indirectState,
712         uint32_t &offset,
713         uint32_t &size);
714 
715     //!
716     //! \brief    Setup indirect state
717     //! \details  [Cmd Buffer Interface] Setup the indirect state region in cmd buffer.
718     //! \details  Caller: MHW only
719     //! \details  This interface is called to reserve the region of indirect state data in cmd buffer.
720     //! \details  Indirect state is a reserved region in cmd buffer which contains the data needed by execute Media kernel.
721     //!           The region is at the end of cmd buffer, size is only needed. Between each SubmitCommandBuffer, this interface should only be call once.
722     //!
723     //! \param    [in] streamState
724     //!           Handle of Os Stream State
725     //! \param    [in] size
726     //!           Size of indirect state in the cmd buffer.
727     //!
728     //! \return   MOS_STATUS
729     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
730     //!
731     static MOS_STATUS SetupIndirectState(
732         MOS_STREAM_HANDLE streamState,
733         uint32_t size);
734 
735     //!
736     //! \brief    Setup commandlist and command pool
737     //! \details  Set the commandlist and commandPool used in this stream.
738     //!
739     //! \param    [in] streamState
740     //!           Handle of Os Stream State
741     //! \param    [in] cmdList
742     //!           pointer to the command list.
743     //! \param    [in] cmdBufMgr
744     //!           pointer to the command buffer manager.
745     //!
746     //! \return   MOS_STATUS
747     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
748     //!
749     static MOS_STATUS SetupCurrentCmdListAndPool(
750         MOS_STREAM_HANDLE   streamState,
751         CommandList         *cmdList,
752         CmdBufMgrNext       *cmdBufMgr);
753 
754     //!
755     //! \brief    Setup commandlist and command pool from os interface
756     //! \details  Set the commandlist and commandPool used in this stream from os interface.
757     //!
758     //! \param    [in] pMosInterface
759     //!           pointer to the mos interface
760     //! \param    [out] streamStateDst
761     //!           Handle of Os Stream State.
762     //!
763     //! \return   MOS_STATUS
764     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
765     //!
766     static MOS_STATUS SetupCurrentCmdListAndPoolFromOsInterface(
767         PMOS_INTERFACE      pMosInterface,
768         MOS_STREAM_HANDLE   streamState);
769 
770     //!
771     //! \brief    Is Device Async or not
772     //! \details  Is Device Async or not.
773     //!
774     //! \param    [in] streamStateDst
775     //!           Handle of Os Stream State.
776     //!
777     //! \return   bool
778     //!           Return true if is async, otherwise false
779     //!
780     static bool IsAsyncDevice(
781         MOS_STREAM_HANDLE streamState);
782 
783     //!
784     //! \brief    Setup VE Attribute Buffer
785     //! \details  [Cmd Buffer Interface] Setup VE Attribute Buffer into cmd buffer.
786     //! \details  Caller: MHW only
787     //! \details  This interface is called to setup into cmd buffer.
788     //!
789     //! \param    [in] streamState
790     //!           Handle of Os Stream State
791     //! \param    [out] cmdBuffer
792     //!           Cmd buffer to setup VE attribute.
793     //!
794     //! \return   MOS_STATUS
795     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
796     //!
797     static MOS_STATUS SetupAttributeVeBuffer(
798         MOS_STREAM_HANDLE     streamState,
799         COMMAND_BUFFER_HANDLE cmdBuffer);
800 
801     //!
802     //! \brief    Get VE Attribute Buffer
803     //! \details  [Cmd Buffer Interface] Get VE Attribute Buffer from cmd buffer.
804     //! \details  Caller: HAL only
805     //! \details  This interface is called to get VE attribute buffer from cmd buffer if it contains one.
806     //!           If there is no VE attribute buffer returned, it means the cmd buffer has no such buffer
807     //!           in current MOS module. It is not error state if it is nullptr.
808     //!
809     //! \param    [out] cmdBuffer
810     //!           Cmd buffer to setup VE attribute.
811     //!
812     //! \return   MOS_CMD_BUF_ATTRI_VE*
813     //!           Return pointer of VE attribute buffer, nullptr if current cmdBuffer didn't contain attribute.
814     //!
815     static MOS_CMD_BUF_ATTRI_VE *GetAttributeVeBuffer(
816         COMMAND_BUFFER_HANDLE cmdBuffer);
817 
818     //!
819     //! \brief    Get Cache Policy Memory Object
820     //! \details  [Resource Interface] Get Cache Policy Memory Object in GMM corresponding to the resource usage
821     //!           Caller: HAL & MHW
822     //!
823     //! \param    [in] mosUsage
824     //!           Resource usage as index to the memory object table
825     //!           If prociding unknown usage, default state will be returned
826     //!
827     //! \return   MEMORY_OBJECT_CONTROL_STATE
828     //!           The cache policy memory object got from MOS interface
829     //!
830     static GMM_RESOURCE_USAGE_TYPE GetGmmResourceUsageType(
831         MOS_HW_RESOURCE_DEF mosUsage);
832 
833     //!
834     //! \brief    Get Cache Policy Memory Object
835     //! \details  [Resource Interface] Get Cache Policy Memory Object in GMM corresponding to the resource usage
836     //!           Caller: HAL & MHW
837     //!
838     //! \param    [in] gmmClientContext
839     //!           Handle of gmmClientContext
840     //! \param    [in] mosUsage
841     //!           Resource usage as index to the memory object table
842     //!           If prociding unknown usage, default state will be returned
843     //!
844     //! \return   MEMORY_OBJECT_CONTROL_STATE
845     //!           The cache policy memory object got from MOS interface
846     //!
847     static MEMORY_OBJECT_CONTROL_STATE GetCachePolicyMemoryObject(
848         GMM_CLIENT_CONTEXT *gmmClientContext,
849         MOS_HW_RESOURCE_DEF mosUsage);
850 
851     //!
852     //! \brief    Get Cache Policy Memory Object
853     //! \details  [Resource Interface] Get Cache Policy Memory Object in GMM corresponding to the resource usage
854     //!           Caller: MOS
855     //!
856     //! \param    [in] gmmClientContext
857     //!           Handle of gmmClientContext
858     //! \param    [in] gmmUsage
859     //!           Resource usage value defined in gmm
860     //!           If prociding unknown usage, default state will be returned
861     //!
862     //! \return   MEMORY_OBJECT_CONTROL_STATE
863     //!           The cache policy memory object got from MOS interface
864     //!
865     static MEMORY_OBJECT_CONTROL_STATE GetGmmCachePolicyMemoryObject(
866         GMM_CLIENT_CONTEXT      *gmmClientContext,
867         GMM_RESOURCE_USAGE_TYPE gmmUsage);
868 
869     //!
870     //! \brief    Get Cache Policy L1 Config
871     //! \details  [Resource Interface] Get L1 Cache Config in GMM corresponding to the resource usage
872     //!           Caller: HAL & MHW
873     //!
874     //! \param    [in] streamState
875     //!           Handle of Os Stream State
876     //! \param    [in] mosUsage
877     //!           Resource usage as index to the memory object table
878     //!           If prociding unknown usage, default state will be returned
879     //!
880     //! \return   uint8_t
881     //!           The L1_Cache_Config got from MOS interface
882     //!
883     static uint8_t GetCachePolicyL1Config(
884         MOS_STREAM_HANDLE streamState,
885         MOS_HW_RESOURCE_DEF mosUsage);
886 
887     //!
888     //! \brief    Get Reserved info from resource
889     //! \details
890     //!
891     //! \param    [in] resource
892     //!           Handle of resource
893     //! \param    [out] val
894     //!           result of info.
895     //!
896     //! \return   MOS_STATUS
897     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
898     //!
899     static MOS_STATUS GetReservedFromResource(MOS_RESOURCE_HANDLE resource, uint32_t &val);
900 
901     //!
902     //! \brief    Get Reserved info from Stream
903     //! \details
904     //!
905     //! \param    [in] stream
906     //!           Handle of stream
907     //! \param    [out] val
908     //!           result of info.
909     //!
910     //! \return   MOS_STATUS
911     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
912     //!
913     static MOS_STATUS GetReservedFromStream(MOS_STREAM_HANDLE stream, uint32_t &val);
914 
915     //!
916     //! \brief    Get Reserved info from Device
917     //! \details
918     //!
919     //! \param    [in] osDeivceContext
920     //!           Handle of device
921     //! \param    [out] val
922     //!           result of info.
923     //!
924     //! \return   MOS_STATUS
925     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
926     //!
927     static MOS_STATUS GetReservedFromDevice(MOS_DEVICE_HANDLE device, uint32_t &val);
928 
929     //!
930     //! \brief    Get preStreamParameters(mos context) info from streamState
931     //! \details
932     //!
933     //! \param    [in] stream
934     //!           Handle of stream
935     //! \param    [out] perStreamParameters
936     //!           pointer of mos conxtex.
937     //!
938     //! \return   MOS_STATUS
939     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
940     //!
941     static MOS_STATUS GetperStreamParameters(MOS_STREAM_HANDLE stream, void **perStreamParameters);
942 
943     //!
944     //! \brief    Convert Resource From Ddi
945     //! \details  [Resource Interface] Convert Resource structure From OS/API specific to MOS reource.
946     //! \details  Caller: DDI only
947     //! \details  It is not device stated function and can be used in both APO DDI and NON-APO MOS.
948     //! \details  MOS resoure is the structure inside MOS module. DDI specific resource depends on OS/API verison.
949     //!           DDI call this to convert external resources (not created by hal) to Mos resources so that HAL & MHW can use them.
950     //!
951     //! \param    [in] osResource
952     //!           OS/API specific resource structure to convert.
953     //! \param    [out] resource
954     //!           Handle of Mos resource convert.
955     //! \param    UINT firstArraySlice
956     //!           [in] resource special info
957     //! \param    UINT mipSlice
958     //!           [in] resource special info
959     //!
960     //! \return   MOS_STATUS
961     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
962     //!
963     static MOS_STATUS ConvertResourceFromDdi(
964         OsSpecificRes osResource,
965         MOS_RESOURCE_HANDLE &resource,
966         uint32_t firstArraySlice,
967         uint32_t mipSlice);
968 
969     //!
970     //! \brief    Create Os Specific Resource Info
971     //! \details  [Resource Interface] Create OS/API specific resource info structures.
972     //! \details  Caller: DDI only
973     //! \details  Os Specific resource info must be created before uing in DDI or converting to MOS resource.
974     //!           This interface doesn't allocate Os Specific Resource. It only create the decorated structure of that resource.
975     //!
976     //! \param    [in, out] resource
977     //!           OS/API specific resource structure to initialize.
978     //! \param    [in] isInternal
979     //!           Indicate if the resource is media internal.
980     //!
981     //! \return   MOS_STATUS
982     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
983     //!
984     static MOS_STATUS CreateOsSpecificResourceInfo(OsSpecificRes resource, bool isInternal = false);
985 
986     //!
987     //! \brief    Destroy Os Specific Resource Info
988     //! \details  [Resource Interface] Destroy OS/API specific resource structure.
989     //! \details  Caller: DDI only
990     //! \details  It is not device stated function and can be used in both APO DDI and NON-APO MOS.
991     //! \details  Os Specific resource info must be destroied if the resource is not used anymore.
992     //!
993     //! \param    [in, out] resource
994     //!           OS/API specific resource structure to initialize.
995     //!
996     //! \return   MOS_STATUS
997     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
998     //!
999     static MOS_STATUS DestroySpecificResourceInfo(OsSpecificRes resource);
1000 
1001     //!
1002     //! \brief    Allocate Resource
1003     //! \details  [Resource Interface] Allocate a graphic resource.
1004     //! \details  Caller: HAL only
1005     //! \details  Graphic resource is a buffer contain data used in the HW cmds.
1006     //!           This interface allocates the gfx resource and its internal data structure.
1007     //!           RegisterResource must be called when cmds in cmd buffer programmed are using this resource.
1008     //!
1009     //! \param    [in] streamState
1010     //!           Handle of Os Stream State
1011     //! \param    [in] params
1012     //!           Pointer to the parameters for allocating resource
1013     //! \param    [out] resource
1014     //!           MOS Resource handle of the allocated resource.
1015     //!
1016     //! \return   MOS_STATUS
1017     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1018     //!
1019     static MOS_STATUS AllocateResource(
1020         MOS_STREAM_HANDLE           streamState,
1021         PMOS_ALLOC_GFXRES_PARAMS    params,    // user provided va
1022         MOS_RESOURCE_HANDLE         &resource
1023 #if MOS_MESSAGES_ENABLED
1024         ,
1025         const char                  *functionName,
1026         const char                  *filename,
1027         int32_t                     line
1028 #endif
1029     );
1030 
1031     //!
1032     //! \brief    Convert HAL free flags to OS free flags
1033     //!
1034     //! \param    [in] halFreeFlag
1035     //!           bit definition in MOS_GFXRES_FREE_FLAGS
1036     //!
1037     //! \return   uint32_t
1038     //!           OS resource deallc flags
1039     //!
1040     static uint32_t ConvertHalFreeFlagsToOsFreeFlags(
1041         uint32_t halFreeFlag
1042     );
1043 
1044     //!
1045     //! \brief    Free Resource
1046     //! \details  [Resource Interface] Free a graphic resource.
1047     //! \details  Caller: HAL only
1048     //! \details  Graphic resource is a buffer contain data used in the HW cmds.
1049     //!           This interface frees the gfx resource and its internal data structure.
1050     //!           This interface must be called when the resource is not used anymore.
1051     //!
1052     //! \param    [in] streamState
1053     //!           Handle of Os Stream State
1054     //! \param    [in] resource
1055     //!           MOS Resource handle of the allocated resource.
1056     //! \param    [in] flag
1057     //!           User defined free flag of the resource.
1058     //!
1059     //! \return   MOS_STATUS
1060     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1061     //!
1062     static MOS_STATUS FreeResource(
1063         MOS_STREAM_HANDLE       streamState,
1064         MOS_RESOURCE_HANDLE     resource,
1065         uint32_t                flag
1066 #if MOS_MESSAGES_ENABLED
1067         ,
1068         const char              *functionName,
1069         const char              *filename,
1070         int32_t                 line
1071 #endif  // MOS_MESSAGES_ENABLED
1072     );
1073 
1074     //!
1075     //! \brief    Get Resource Info
1076     //! \details  [Resource Interface] Get the info of a graphic resource.
1077     //! \details  Caller: HAL only
1078     //! \details  This interface gets the read-only detailed info of a graphic resource.
1079     //!           Any modification of details provided by this interface will not impact the actual resource.
1080     //!
1081     //! \param    [in] streamState
1082     //!           Handle of Os Stream State
1083     //! \param    [in] resource
1084     //!           MOS Resource handle of the allocated resource.
1085     //! \param    [out] details
1086     //!           Resource detailed info got.
1087     //!
1088     //! \return   MOS_STATUS
1089     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1090     //!
1091     static MOS_STATUS GetResourceInfo(
1092         MOS_STREAM_HANDLE streamState,
1093         MOS_RESOURCE_HANDLE resource,
1094         MosResourceInfo &details);
1095 
1096     //!
1097     //! \brief    Lock Resource
1098     //! \details  [Resource Interface] Lock the gfx resource for CPU to access
1099     //! \details  Caller: HAL only
1100     //! \details  A sys memory ptr will be provided by this interface if executed successfully.
1101     //! \details  The sys memory is mapped to the gfx memory inside MOS module.
1102     //! \details  This interface is usually for driver to read/write data into a resource directly (without program HW cmd).
1103     //! \details  Caller must make sure no access out of bound of the locked out data. UnlockResource must be called when finished access the locked data.
1104     //!           A resource already been locked cannot be locked again.
1105     //!           This is a blocking call if the resource is used by the cmdbuffer which already submitted to an existing GPU context.
1106     //!           Unless SkipResourceSync is called. This interface will make sure the sync of Lock.
1107     //! \details  If the resource is compressed, gfx memory decompression will be triggered.
1108     //!
1109     //! \param    [in] streamState
1110     //!           Handle of Os Stream State
1111     //! \param    [in] resource
1112     //!           MOS Resource handle of the resource to lock.
1113     //! \param    [in] flags
1114     //!           Control flags of locking resource.
1115     //!
1116     //! \return   void *
1117     //!           Locked memory data pointer, nullptr if lock failed.
1118     //!
1119     static void *LockMosResource(
1120         MOS_STREAM_HANDLE streamState,
1121         MOS_RESOURCE_HANDLE resource,
1122         PMOS_LOCK_PARAMS flags);
1123 
1124     //!
1125     //! \brief    Unlock Resource
1126     //! \details  [Resource Interface] Unlock the gfx resource which is locked out.
1127     //! \details  Caller: HAL only
1128     //! \details  UnlockResource must be called when finished access the locked data of the resource.
1129     //!           A resource already been unlocked cannot be unlocked again.
1130     //! \details  Unlock resource will not trigger compressing or changing the layout of the resource.
1131     //!
1132     //! \param    [in] streamState
1133     //!           Handle of Os Stream State
1134     //! \param    [in] resource
1135     //!           MOS Resource handle of the allocated resource.
1136     //!
1137     //! \return   MOS_STATUS
1138     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1139     //!
1140     static MOS_STATUS UnlockMosResource(
1141         MOS_STREAM_HANDLE streamState,
1142         MOS_RESOURCE_HANDLE resource);
1143 
1144     //!
1145     //! \brief    Update resource usage type
1146     //! \details  update the resource usage for cache policy
1147     //! \param    PMOS_RESOURCE pOsResource
1148     //!           [in/out] Pointer to OS Resource
1149     //! \param    MOS_HW_RESOURCE_DEF resUsageType
1150     //!           [in] MOS resosuce usage type
1151     //! \return   MOS_STATUS
1152     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1153     //!
1154     static MOS_STATUS UpdateResourceUsageType(
1155         PMOS_RESOURCE           pOsResource,
1156         MOS_HW_RESOURCE_DEF     resUsageType);
1157 
1158     //!
1159     //! \brief    Register Resource
1160     //! \details  [Resource Interface] Register the resource to current streamState.
1161     //! \details  Caller: MHW only
1162     //! \details  Register resource to inform MOS that the resource is read/written by current cmd buffer being programmed
1163     //!           and this cmd buffer will be submitted into current GPU context in streamState.
1164     //! \details  RegisterResource must be called when cmds in cmd buffer programmed are using this resource.
1165     //! \details  This interface is to make the residency of the resource and handle resource sync harzad between GPU contexts.
1166     //! \details  Calling sequence is like:  SetGpuContext -> RegisterResource... -> SubmitCommandBuffer ->
1167     //!           SetGpuContext(another) -> RegisterResource(another or same resource)... -> SubmitCommandBuffer
1168     //! \details  If Register same resource to different GPU context when calling SetGpuContext, sync harzad will be handled.
1169     //!           RegisterResource for the same resource can be called repeatedly. MOS will make sure no duplicated residency making and sync.
1170     //!
1171     //! \param    [in] streamState
1172     //!           Handle of Os Stream State
1173     //! \param    [out] resource
1174     //!           MOS Resource handle of the allocated resource.
1175     //! \param    [in] write
1176     //!           Indicate if the resource is written by HW or just read.
1177     //!
1178     //! \return   MOS_STATUS
1179     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1180     //!
1181     static MOS_STATUS RegisterResource(
1182         MOS_STREAM_HANDLE streamState,
1183         MOS_RESOURCE_HANDLE resource,
1184         bool write);
1185 
1186     //!
1187     //! \brief    Get Resource Gfx Address
1188     //! \details  [Resource Interface] Get the graphic virtual address of the resource.
1189     //! \details  Caller: MHW only
1190     //! \details  Only use this interface to add resource's address directly into cmd field.
1191     //!           If MHW needs patch the address in the cmd field, GetResourceAllocationIndex should be called.
1192     //!
1193     //! \param    [in] streamState
1194     //!           Handle of Os Stream State
1195     //! \param    [in] resource
1196     //!           MOS Resource handle of the allocated resource.
1197     //!
1198     //! \return   uint64_t
1199     //!           64bit virtual graphic address got. 0x00000000 if execution failed.
1200     //!
1201     static uint64_t GetResourceGfxAddress(
1202         MOS_STREAM_HANDLE streamState,
1203         MOS_RESOURCE_HANDLE resource);
1204 
1205     //!
1206     //! \brief    Get Resource Allocation Handle
1207     //! \details  [Resource Interface] Get the allocation handle of a graphic resource.
1208     //! \details  Caller: MHW Only
1209     //! \details  This interface gets the read-only detailed info of a graphic resource.
1210     //!           Any modification of details provided by this interface will not impact the actual resource.
1211     //!
1212     //! \param    [in] resource
1213     //!           MOS Resource handle of the allocated resource.
1214     //!
1215     //! \return   uint32_t
1216     //!           Allocation Handle. 0 if execution failed.
1217     //!
1218     static uint32_t GetResourceAllocationHandle(
1219         MOS_RESOURCE_HANDLE resource);
1220 
1221     //!
1222     //! \brief    Get Resource Allocation index
1223     //! \details  [Resource Interface] Get the allocation index of the resource.
1224     //! \details  Caller: MHW only
1225     //! \details  Allocation index is used when calling SetPatchEntry to add resource into cmd.
1226     //!           If MHW needs patch the address in the cmd field, GetResourceAllocationIndex should be called.
1227     //!
1228     //! \param    [in] streamState
1229     //!           Handle of Os Stream State
1230     //! \param    [in] resource
1231     //!           MOS Resource handle of the allocated resource.
1232     //!
1233     //! \return   uint32_t
1234     //!           Allocation index got. 0 if execution failed.
1235     //!
1236     static uint32_t GetResourceAllocationIndex(
1237         MOS_STREAM_HANDLE streamState,
1238         MOS_RESOURCE_HANDLE resource);
1239 
1240     //!
1241     //! \brief    Skip Resource Sync
1242     //! \details  [Resource Interface] Skip the sync handling of the resource
1243     //! \details  Caller: HAL only
1244     //! \details  It is not device stated function and can be used in both APO HAL and NON-APO MOS.
1245     //! \details  Indicate the resource provided needn't to be synced.
1246     //!           The resource skipping sync can be accessed by different cmd buffers on different GPU contexts at the same time.
1247     //! \details  RegisterResource and LockResource will not handling the sync of the resources between different GPU cotnexts.
1248     //! \details  Usually the resource skipping sync is for the case like:
1249     //!           Different cmd buffers at the same time access the non-overlapped region of the resource
1250     //!
1251     //! \param    [in] streamState
1252     //!           Handle of Os Stream State
1253     //! \param    [in] params
1254     //!           Pointer to the parameters for allocating resource
1255     //! \param    [out] resource
1256     //!           MOS Resource handle of the allocated resource.
1257     //!
1258     //! \return   MOS_STATUS
1259     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1260     //!
1261     static MOS_STATUS SkipResourceSync(
1262         MOS_RESOURCE_HANDLE resource);
1263 
1264     //!
1265     //! \brief    Sync on resource
1266     //! \details  [Resource Interface] Explicit sync on resource
1267     //! \details  Caller: HAL only
1268     //! \details  Resource is shared by different cmd buffers on different GPU contexts.
1269     //!           Adding sync object into requestor GPU context queue to resolve the hazard if necessary.
1270     //!           This func is called by hal to declare the resource to consider the sync explicitly.
1271     //!           It is a strong sync request for the resource.
1272     //!
1273     //! \param    [in] streamState
1274     //!           Handle of Os Stream State
1275     //! \param    [in] resource
1276     //!           MOS Resource handle for the resource contain hazard of sync
1277     //! \param    [in] writeOperation
1278     //!           Indicate the current programming is to write resource or not
1279     //! \param    [in] requsetorGpuContext
1280     //!           GpuContext which programming the resource. Recommand not setting it and use current GPU context.
1281     //!
1282     //! \return   MOS_STATUS
1283     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1284     //!
1285     static MOS_STATUS SyncOnResource(
1286         MOS_STREAM_HANDLE streamState,
1287         MOS_RESOURCE_HANDLE resource,
1288         bool writeOperation,
1289         GPU_CONTEXT_HANDLE requsetorGpuContext = MOS_GPU_CONTEXT_INVALID_HANDLE);
1290 
1291     //!
1292     //! \brief    Resource Sync call back between Media and 3D for resource Sync
1293     //! \details  [Resource Interface] Sync Call Back based on resource
1294     //! \details  Caller: DDI only
1295     //! \details  Resource is shared by different cmd buffers on different GPU contexts.
1296     //!           Adding sync object into requestor GPU context queue to resolve the hazard if necessary.
1297     //!           If there is a hazard, one cmd buffer in requestor GPU context queue will wait for the other cmd buffer in busy GPU context.
1298     //!
1299     //! \param    [in] resource
1300     //!           OS specific resource handle for the resource contain hazard of sync
1301     //! \param    [in] deviceContext
1302     //!           Handle of Os Device Context
1303     //! \param    [in] index
1304     //!           Sub-resource index
1305     //! \param    [in] hazardType
1306     //!           Type of hazard: RAW, WAR, WAR
1307     //! \param    [in] busyCtx
1308     //!           GPU Context handle of the queue being waiting for.
1309     //! \param    [in] requestorCtx
1310     //!           GPU Context handle of current GPU which requesting to use the resoure and find the hazard to wait the busy context.
1311     //! \param    [in] osRequestorHandle
1312     //!           OS runtime handle of requestor context
1313     //!
1314     //! \return   MOS_STATUS
1315     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1316     //!
1317     static MOS_STATUS ResourceSyncCallback(
1318         OsSpecificRes          resource,
1319         MOS_DEVICE_HANDLE      deviceContext,
1320         uint32_t               index,
1321         SYNC_HAZARD            hazardType,
1322         GPU_CONTEXT_HANDLE     busyCtx,
1323         GPU_CONTEXT_HANDLE     requestorCtx,
1324         OS_HANDLE              osRequestorHandle);
1325 
1326     //!
1327     //! \brief    Lock Sync Callback between Media and 3D
1328     //! \details  [Resource Interface] Lock Sync Call Back
1329     //! \details  Caller: DDI only
1330     //! \details  Resource is used in a cmd buffer on an existing GPU context.
1331     //!           Before Locking the resource, make sure the resource finished used by all GPU contexts which are using this resource.
1332     //!           If there is a hazard, CPU side will wait for the cmd buffer in busy GPU context.
1333     //!
1334     //! \param    [in] resource
1335     //!           OS specific resource handle for the resource contain hazard of sync
1336     //! \param    [in] deviceContext
1337     //!           Handle of Os Device Context
1338     //! \param    [in] index
1339     //!           Sub-resource index
1340     //! \param    [in] hazardType
1341     //!           Type of hazard: RAW, WAR, WAR
1342     //! \param    [in] busyCtx
1343     //!           GPU Context handle of the queue being waiting for.
1344     //! \param    [in] doNotWait
1345     //!           Indicate this is blocking call or not. When set to true, possibly return MOS_STATUS_STILL_DRAWING
1346     //!
1347     //! \return   MOS_STATUS
1348     //!           Return MOS_STATUS_SUCCESS if successful, MOS_STATUS_STILL_DRAWING if doNotWait
1349     //!           is set to true and resoure is still being used in HW, otherwise failed
1350     //!
1351     static MOS_STATUS LockSyncCallback(
1352         OsSpecificRes           resource,
1353         MOS_DEVICE_HANDLE       deviceContext,
1354         uint32_t                index,
1355         SYNC_HAZARD             hazardType,
1356         GPU_CONTEXT_HANDLE      busyCtx,
1357         bool                    doNotWait);
1358 
1359     //!
1360     //! \brief    Wait For cmd Completion
1361     //! \details  [GPU Context Interface] Waiting for the completion of cmd in provided GPU context
1362     //! \details  Caller: HAL only
1363     //!
1364     //! \param    [in] streamState
1365     //!           Handle of Os Stream State
1366     //! \param    [in] gpuCtx
1367     //!           GpuContext handle of the gpu context to wait cmd completion
1368     //!
1369     //! \return   MOS_STATUS
1370     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1371     //!
1372     static MOS_STATUS WaitForCmdCompletion(
1373         MOS_STREAM_HANDLE streamState,
1374         GPU_CONTEXT_HANDLE gpuCtx);
1375 
1376     //!
1377     //! \brief    Trim Residency
1378     //!
1379     //! \param    [in] device
1380     //!           MOS device handle
1381     //! \param    [in] periodicTrim
1382     //!           Indicate if the trim is periodic
1383     //! \param    [in] restartPeriodicTrim
1384     //!           Indicate if restarting periodic trim
1385     //! \param    [in] numBytesToTrim
1386     //!           Number bytes to trim
1387     //! \param    [in] trimToMinimum
1388     //!           Indicate if trim to minimum
1389     //! \param    [in] trimOnlyMediaResources
1390     //!           Indicate if only trim media resources.
1391     //!
1392     //! \return   MOS_STATUS
1393     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1394     //!
1395     static MOS_STATUS TrimResidency(
1396         MOS_DEVICE_HANDLE device,
1397         bool periodicTrim,
1398         bool restartPeriodicTrim,
1399         uint64_t &numBytesToTrim,
1400         bool trimToMinimum,
1401         bool trimOnlyMediaResources);
1402 
1403     //!
1404     //! \brief    Update Residency
1405     //!
1406     //! \param    [in] device
1407     //!           MOS device handle
1408     //! \param    [in] resInfo
1409     //!           Os specific resource info
1410     //! \param    [in] index
1411     //!           Resource index
1412     //!
1413     //! \return   MOS_STATUS
1414     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1415     //!
1416     static MOS_STATUS UpdateResidency(
1417         MOS_DEVICE_HANDLE device,
1418         OsSpecificRes     resInfo,
1419         uint32_t          index);
1420 
1421 
1422     // Memory compression interfaces
1423 
1424     //!
1425     //! \brief    Decompress resource
1426     //!
1427     //! \param    [in] streamState
1428     //!           Handle of Os Stream State
1429     //! \param    [in] resource
1430     //!           MOS Resource handle of the resource to decompress.
1431     //! \return   MOS_STATUS
1432     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1433     //!
1434     static MOS_STATUS DecompResource(
1435         MOS_STREAM_HANDLE streamState,
1436         MOS_RESOURCE_HANDLE resource);
1437 
1438     //!
1439     //! \brief  Set Memory Compression Mode
1440     //!
1441     //! \param    [in] streamState
1442     //!           Handle of Os Stream State
1443     //! \param    [in, out] resource
1444     //!           MOS Resource handle
1445     //! \param    [in] resMmcMode
1446     //!           MMC mode
1447     //!
1448     //! \return   MOS_STATUS
1449     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1450     //!
1451     static MOS_STATUS SetMemoryCompressionMode(
1452         MOS_STREAM_HANDLE streamState,
1453         MOS_RESOURCE_HANDLE resource,
1454         MOS_MEMCOMP_STATE resMmcMode);
1455 
1456     //!
1457     //! \brief  Get Memory Compression Mode
1458     //!
1459     //! \param    [in] streamState
1460     //!           Handle of Os Stream State
1461     //! \param    [in] resource
1462     //!           MOS Resource handle
1463     //! \param    [out] resMmcMode
1464     //!           MMC mode
1465     //!
1466     //! \return   MOS_STATUS
1467     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1468     //!
1469     static MOS_STATUS GetMemoryCompressionMode(
1470         MOS_STREAM_HANDLE streamState,
1471         MOS_RESOURCE_HANDLE resource,
1472         MOS_MEMCOMP_STATE &resMmcMode);
1473 
1474     //!
1475     //! \brief  Set Memory Compression Hint
1476     //!
1477     //! \param    [in] streamState
1478     //!           Handle of Os Stream State
1479     //! \param    [in, out] resource
1480     //!           MOS Resource handle
1481     //! \param    [in] hintOn
1482     //!           Flag to set hint on or off
1483     //!
1484     //! \return   MOS_STATUS
1485     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1486     //!
1487     static MOS_STATUS SetMemoryCompressionHint(
1488         MOS_STREAM_HANDLE streamState,
1489         MOS_RESOURCE_HANDLE resource,
1490         bool                hintOn);
1491 
1492     //!
1493     //! \brief  Get Memory Compression Format
1494     //!
1495     //! \param    [in] streamState
1496     //!           Handle of Os Stream State
1497     //! \param    [in, out] resource
1498     //!           MOS Resource handle
1499     //! \param    [out] resMmcFormat
1500     //!           MMC format got
1501     //!
1502     //! \return   MOS_STATUS
1503     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1504     //!
1505     static MOS_STATUS GetMemoryCompressionFormat(
1506         MOS_STREAM_HANDLE streamState,
1507         MOS_RESOURCE_HANDLE resource,
1508         uint32_t *resMmcFormat);
1509 
1510     //!
1511     //! \brief    Double buffer copy resource
1512     //!
1513     //! \param    [in] streamState
1514     //!           Handle of Os Stream State
1515     //! \param    [in] inputResource
1516     //!           Input resource to copy.
1517     //! \param    [out] outputResource
1518     //!           Output resource.
1519     //! \param    [in] outputCompressed
1520     //!           Insdicate if output resource is compressed.
1521     //! \return   MOS_STATUS
1522     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1523     //!
1524     static MOS_STATUS DoubleBufferCopyResource(
1525         MOS_STREAM_HANDLE   streamState,
1526         MOS_RESOURCE_HANDLE inputResource,
1527         MOS_RESOURCE_HANDLE outputResource,
1528         bool                outputCompressed);
1529 
1530     //!
1531     //! \brief    Copy Resource to Another Buffer
1532     //! \details  Decompress and Copy Resource to Another 2D Buffer
1533     //!
1534     //! \param    [in] streamState
1535     //!           Handle of Os Stream State
1536     //! \param    inputResource
1537     //!           [in] Input Resource object
1538     //! \param    outputResource
1539     //!           [out] output Resource object
1540     //! \param    [in] copyWidth
1541     //!           The 2D surface Width
1542     //! \param    [in] copyHeight
1543     //!           The 2D surface height
1544     //! \param    [in] copyInputOffset
1545     //!           The offset of copied surface from
1546     //! \param    [in] copyOutputOffset
1547     //!           The offset of copied to
1548     //! \param    [in] outputCompressed
1549     //!           True means apply compression on output surface, else output uncompressed surface
1550     //! \return   MOS_STATUS
1551     //!           MOS_STATUS_SUCCESS if successful
1552     //!
1553     static MOS_STATUS MediaCopyResource2D(
1554         MOS_STREAM_HANDLE   streamState,
1555         MOS_RESOURCE_HANDLE inputResource,
1556         MOS_RESOURCE_HANDLE outputResource,
1557         uint32_t            copyWidth,
1558         uint32_t            copyHeight,
1559         uint32_t            copyInputOffset,
1560         uint32_t            copyOutputOffset,
1561         uint32_t            bpp,
1562         bool                outputCompressed);
1563 
1564     // GPU Status interfaces
1565     //!
1566     //! \brief   Get Gpu Status Tag
1567     //!
1568     //! \param    [in] streamState
1569     //!           Handle of Os Stream State
1570     //! \param    [in] gpuContext
1571     //!           MOS GPU Context handle
1572     //!
1573     //! \return   uint32_t
1574     //!           Tag got from GPU Context indicated, 0 if failed to get the tag
1575     //!
1576     static uint32_t GetGpuStatusTag(
1577         MOS_STREAM_HANDLE streamState,
1578         GPU_CONTEXT_HANDLE gpuContext);
1579 
1580     //!
1581     //! \brief   Increment Gpu Status Tag
1582     //!
1583     //! \param    [in] streamState
1584     //!           Handle of Os Stream State
1585     //! \param    [in] gpuContext
1586     //!           MOS GPU Context handle
1587     //!
1588     //! \return   MOS_STATUS
1589     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1590     //!
1591     static MOS_STATUS IncrementGpuStatusTag(
1592         MOS_STREAM_HANDLE streamState,
1593         GPU_CONTEXT_HANDLE gpuContext);
1594 
1595     //!
1596     //! \brief   Get Gpu Status Sync Tag
1597     //!
1598     //! \param    [in] streamState
1599     //!           Handle of Os Stream State
1600     //! \param    [in] gpuContext
1601     //!           MOS GPU Context handle
1602     //!
1603     //! \return   uint64_t
1604     //!           HW tag got from GPU context, 0 if get failed
1605     //!
1606     static uint64_t GetGpuStatusSyncTag(
1607         MOS_STREAM_HANDLE streamState,
1608         GPU_CONTEXT_HANDLE gpuContext);
1609 
1610     //!
1611     //! \brief   Get Gpu Status Buffer Resource
1612     //!
1613     //! \param    [in] streamState
1614     //!           Handle of Os Stream State
1615     //! \param    [out] resource
1616     //!           MOS resource handle of GPU status buffer got from current GPU context
1617     //! \param    [in] gpuContext
1618     //!           MOS GPU Context handle
1619     //!
1620     //! \return   MOS_STATUS
1621     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1622     //!
1623 
1624     static MOS_STATUS GetGpuStatusBufferResource(
1625         MOS_STREAM_HANDLE streamState,
1626         MOS_RESOURCE_HANDLE &resource,
1627         GPU_CONTEXT_HANDLE gpuContext);
1628 
1629     //!
1630     //! \brief   Get CP Interface
1631     //!
1632     //! \param    [in] streamState
1633     //!           Handle of Os Stream State
1634     //!
1635     //! \return   MosCpInterface
1636     //!           CP Interface got from stream State, nullptr if get failed
1637     //!
1638     static MosCpInterface *GetCpInterface(MOS_STREAM_HANDLE streamState);
1639 
1640     //!
1641     //! \brief   Get OCA Interface
1642     //!
1643     //! \param    [in] streamState
1644     //!           Handle of Os Stream State
1645     //!
1646     //! \return   MosOcaInterface
1647     //!           OCA Interface got from stream State, nullptr if get failed
1648     //!
1649     static MosOcaInterface *GetOcaInterface(MOS_STREAM_HANDLE streamState);
1650 
1651     //!
1652     //! \brief    Maps the specified executable module into the address space of
1653     //!           the calling process.
1654     //! \param    PMOS_INTERFACE pOsInterface
1655     //!           [in] A handle to OS interface.  This can be nullptr which allows a caller to
1656     //!           always get library from specified library path (function will never check
1657     //!           driver store) which is useful if there's a constant static path of a library
1658     //! \param    const PCCHAR lpLibFileName
1659     //!           [in] String containing resource name to load.  Absolute path is given here
1660     //!           if pOsInterface is nullptr, else only lib path is given, and driver will check store for path
1661     //! \param    PHMODULE phModule
1662     //!           [out] Handle to library given back to the caller
1663     //! \return   MOS_STATUS
1664     //!           Returns one of the MOS_STATUS error codes if failed,
1665     //!           else MOS_STATUS_SUCCESS
1666     //!
1667     static MOS_STATUS MosLoadLibrary(
1668         MOS_STREAM_HANDLE           streamState,
1669         PCCHAR                      pFileName,
1670         PHMODULE                    phModule);
1671 
1672     //!
1673     //! \brief    Free the loaded dynamic-link library
1674     //! \details  Free the loaded dynamic-link library
1675     //! \param    [in] hLibModule
1676     //!           A handle to the loaded DLL module
1677     //! \return   int32_t
1678     //!           true if success else false
1679     //!
1680     static MOS_STATUS MosFreeLibrary(HMODULE hLibModule);
1681 
1682     //! \brief    Get Virtual Engine State
1683     //! \details  [Virtual Engine Interface] Get Virtual Engine State from streamState
1684     //! \details  Caller: Hal (Scalability) only
1685     //! \details  This func is called when a stream (Hal instance) need to get a VE state
1686     //! \details  corresponding to current GPU context.
1687     //!
1688     //! \param    [in] streamState
1689     //!           Handle of Os Stream State
1690     //!
1691     //! \return   MOS_VE_HANDLE
1692     //!           Handle of MOS virtual engine state, Invalid handle if get failed
1693     //!
1694     static MOS_VE_HANDLE GetVirtualEngineState(
1695         MOS_STREAM_HANDLE  streamState);
1696 
1697     //!
1698     //! \brief    Set Virtual Engine State
1699     //! \details  [Virtual Engine Interface] Set Virtual Engine State of provided streamState
1700     //! \details  Caller: Hal (Scalability) only
1701     //! \details  This func is called when a stream (Hal instance) need to set an existing VE state
1702     //! \details  into provided stream.
1703     //!
1704     //! \param    [in] streamState
1705     //!           Handle of Os Stream State
1706     //! \param    [in] veState
1707     //!           Handle of Virtual Engine State to set
1708     //!
1709     //! \return   MOS_STATUS
1710     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1711     //!
1712     static MOS_STATUS SetVirtualEngineState(
1713         MOS_STREAM_HANDLE streamState,
1714         MOS_VE_HANDLE veState);
1715 
1716     //!
1717     //! \brief    Create Virtual Engine State
1718     //! \details  [Virtual Engine Interface] Create Virtual Engine State of provided streamState
1719     //! \details  Caller: Hal (Scalability) only
1720     //! \details  This func is called when a stream (Hal instance) need to create a VE state
1721     //! \details  into provided stream.
1722     //!
1723     //! \param    [in] streamState
1724     //!           Handle of Os Stream State
1725     //! \param    [in] veInitParms
1726     //!           Pointer of parameters to init ve staet
1727     //! \param    [out] veState
1728     //!           Reference of the handle of Virtual Engine State to created
1729     //!
1730     //! \return   MOS_STATUS
1731     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1732     //!
1733     static MOS_STATUS CreateVirtualEngineState(
1734         MOS_STREAM_HANDLE streamState,
1735         PMOS_VIRTUALENGINE_INIT_PARAMS veInitParms,
1736         MOS_VE_HANDLE    &veState);
1737 
1738     //!
1739     //! \brief    Destroy Virtual Engine State
1740     //! \details  [Virtual Engine Interface] Destroy Virtual Engine State of provided streamState
1741     //! \details  Caller: Hal (Scalability) only
1742     //! \details  This func is called when a stream (Hal instance) need to destroy a VE state
1743     //! \details  into provided stream.
1744     //!
1745     //! \param    [in] streamState
1746     //!           Handle of Os Stream State
1747     //! \param    [out] veState
1748     //!           Reference of the handle of Virtual Engine State to created
1749     //!
1750     //! \return   MOS_STATUS
1751     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1752     //!
1753     static MOS_STATUS DestroyVirtualEngineState(
1754         MOS_STREAM_HANDLE streamState);
1755 
1756     //!
1757     //! \brief    Set hint parameters
1758     //!
1759     //! \details  [Virtual Engine Interface] Set hint parameters into Virtual Engine State in provided stream
1760     //! \details  Caller: Hal (Scalability) only
1761     //! \details  Set hint parameters for virtual engine state
1762     //!
1763     //! \param    [in] streamState
1764     //!           Handle of Os Stream State
1765     //! \param    [in] veParams
1766     //!           pointer to VE parameter data structure to set
1767     //! \return   MOS_STATUS
1768     //!           MOS_STATUS_SUCCESS if success, else fail reason
1769     //!
1770     static MOS_STATUS SetVeHintParams(
1771         MOS_STREAM_HANDLE streamState,
1772         PMOS_VIRTUALENGINE_SET_PARAMS veParams);
1773 
1774     //!
1775     //! \brief    Get hint parameters
1776     //!
1777     //! \details  [Virtual Engine Interface] Get hint parameters from Virtual Engine State in provided stream
1778     //! \details  Caller: Hal (Scalability) only
1779     //! \details  Get hint parameters from virtual engine state
1780     //!
1781     //! \param    [in] streamState
1782     //!           Handle of Os Stream State
1783     //! \param    [in] scalableMode
1784     //!           flag to indicate if scalability mode
1785     //! \param    [out] hintParams
1786     //!           pointer to VE hint parameter address
1787     //! \return   MOS_STATUS
1788     //!           MOS_STATUS_SUCCESS if success, else fail reason
1789     //!
1790     static MOS_STATUS GetVeHintParams(
1791         MOS_STREAM_HANDLE streamState,
1792         bool scalableMode,
1793         PMOS_VIRTUALENGINE_HINT_PARAMS *hintParams);
1794 
1795     //!
1796     //! \brief    Set Virtual Engine Submission Type
1797     //!
1798     //! \details  [Virtual Engine Interface] Set submission type for the provided cmd buffer
1799     //! \details  Caller: Hal (Scalability) only
1800     //! \details  Set submission type as per cmd buffer hint parameter. Must be set before submission.
1801     //!           Submission type is to set cmd buffer (primary or secondary) property to indicate
1802     //!           which pipe it belongs. See MOS_SUBMISSION_TYPE.
1803     //!
1804     //! \param    [in] streamState
1805     //!           Handle of Os Stream State
1806     //! \param    [out] cmdBuf
1807     //!           Handle of cmd buffer to set submission type
1808     //! \param    [in] type
1809     //!           Submission type to set
1810     //! \return   MOS_STATUS
1811     //!           MOS_STATUS_SUCCESS if success, else fail reason
1812     //!
1813     static MOS_STATUS SetVeSubmissionType(
1814         MOS_STREAM_HANDLE     streamState,
1815         COMMAND_BUFFER_HANDLE cmdBuf,
1816         MOS_SUBMISSION_TYPE   type);
1817 
1818     //!
1819     //! \brief    Get Adapter BDF
1820     //! \details  [System info Interface] Get Adapter BDF
1821     //! \details  Caller: DDI & HAL
1822     //! \details  This func is called to differentiate the behavior according to Adapter BDF.
1823     //!
1824     //! \param    [in] mosCtx
1825     //!           Pointer of Mos context
1826     //! \param    [out] adapterBDF
1827     //!           Adapter BDF info
1828     //!
1829     //! \return   MOS_STATUS
1830     //!           MOS_STATUS_SUCCESS if success, else fail reason
1831     //!
1832     static MOS_STATUS GetAdapterBDF(PMOS_CONTEXT mosCtx, ADAPTER_BDF *adapterBDF);
1833 
1834 #if _DEBUG || _RELEASE_INTERNAL
1835     //!
1836     //! \brief    Get engine count
1837     //!
1838     //! \details  [Virtual Engine Interface] Get engine count from Virtual Engine State in provided stream
1839     //! \details  Caller: Hal (Scalability) only
1840     //! \details  Get engine count from virtual engine state
1841     //!
1842     //! \param    [in] streamState
1843     //!           Handle of Os Stream State
1844     //! \return   uint8_t
1845     //!           Engine count
1846     //!
1847     static uint8_t GetVeEngineCount(
1848         MOS_STREAM_HANDLE streamState);
1849 
1850     //!
1851     //! \brief    Get Engine Logic Id
1852     //! \details  [Virtual Engine Interface] Get engine Logic Id from Virtual Engine State in provided stream
1853     //! \details  Caller: Hal (Scalability) only
1854     //! \details  Get engine Logic Id from virtual engine state
1855     //!
1856     //! \param    [in] streamState
1857     //!           Handle of Os Stream State
1858     //! \param    [in] instanceIdx
1859     //!           Engine instance index
1860     //! \return   uint8_t
1861     //!
1862     static uint8_t GetEngineLogicId(
1863         MOS_STREAM_HANDLE streamState,
1864         uint32_t instanceIdx);
1865 
1866 #endif // _DEBUG || _RELEASE_INTERNAL
1867 
1868     //!
1869     //! \brief    Sets the perf tag
1870     //! \details  Sets the perf tag
1871     //! \param    [in] streamState
1872     //!           Handle of Os Stream State
1873     //! \param    uint32_t perfTag
1874     //!           [in] Perf tag
1875     //! \return   void
1876     //!
1877     static void SetPerfTag(
1878         MOS_STREAM_HANDLE streamState,
1879         uint32_t       perfTag);
1880 
1881     //!
1882     //! \brief    Gets the perf tag
1883     //! \details  Gets the perf tag
1884     //! \param    [in] streamState
1885     //!           Handle of Os Stream State
1886     //! \return   uint32_t
1887     //!           Return perf tag
1888     //!
1889     static uint32_t GetPerfTag(
1890         MOS_STREAM_HANDLE streamState);
1891 
1892     //!
1893     //! \brief    Check if Perf Tag is already set
1894     //! \details  Check if Perf Tag is already set
1895     //! \param    [in] streamState
1896     //!           Handle of Os Stream State
1897     //! \return   int32_t
1898     //!
1899     static int32_t IsPerfTagSet(
1900         MOS_STREAM_HANDLE streamState);
1901 
1902     //!
1903     //! \brief    Increase performance data frame ID
1904     //! \details  Increase performance data frame ID
1905     //! \param    [in] streamState
1906     //!           Handle of Os Stream State
1907     //! \return   void
1908     //!
1909     static void IncPerfFrameID(
1910         MOS_STREAM_HANDLE streamState);
1911 
1912     //!
1913     //! \brief    Set Hybrid Kernel ID
1914     //! \details  Set Hybrid Kernel ID
1915     //! \param    [in] streamState
1916     //!           Handle of Os Stream State
1917     //! \param    uint32_t kernelID
1918     //!           [in] Hybrid Decoder kernel ID
1919     //! \return   void
1920     //!
1921     static void SetPerfHybridKernelID(
1922         MOS_STREAM_HANDLE streamState,
1923         uint32_t          kernelID);
1924 
1925     //!
1926     //! \brief    Reset performance data buffer ID
1927     //! \details  Reset performance data buffer ID
1928     //! \param    [in] streamState
1929     //!           Handle of Os Stream State
1930     //! \return   void
1931     //!
1932     static void ResetPerfBufferID(
1933         MOS_STREAM_HANDLE streamState);
1934 
1935     //!
1936     //! \brief    Increase performance data buffer ID
1937     //! \details  Increase performance data buffer ID
1938     //! \param    [in] streamState
1939     //!           Handle of Os Stream State
1940     //! \return   VOID
1941     //!
1942     static void IncPerfBufferID(
1943         MOS_STREAM_HANDLE streamState);
1944 
1945     //!
1946     //! \brief    Determines if the GPU Hung
1947     //! \param    [in] streamState
1948     //!           Handle of Os Stream State
1949     //! \return   int32_t
1950     //!           Return if the GPU Hung
1951     //!
1952     static int32_t IsGPUHung(
1953         MOS_STREAM_HANDLE streamState);
1954 
1955     //!
1956     //! \brief    Get SetMarker enabled flag
1957     //! \details  Get SetMarker enabled flag from streamState
1958     //! \param    [in] streamState
1959     //!           Handle of Os Stream State
1960     //! \return   bool
1961     //!           SetMarker enabled flag
1962     //!
1963     static bool IsSetMarkerEnabled(
1964         MOS_STREAM_HANDLE streamState);
1965 
1966     //!
1967     //! \brief    Get SetMarker resource address
1968     //! \details  Get SetMarker resource address from streamState
1969     //! \param    [in] streamState
1970     //!           Handle of Os Stream State
1971     //! \return   PMOS_RESOURCE
1972     //!           SetMarker resource address
1973     //!
1974     static PMOS_RESOURCE GetMarkerResource(
1975         MOS_STREAM_HANDLE streamState);
1976 
1977     //!
1978     //! \brief    Get plane offset inside surface
1979     //! \details  Returns the offset
1980     //! \param    MOS_PLANE_OFFSET planeOffset
1981     //!           [in] Reference to MOS_PLANE_OFFSET structure
1982     //! \return   int - offset of the plane
1983     //!
1984     static int GetPlaneSurfaceOffset(
1985         const MOS_PLANE_OFFSET &planeOffset);
1986 
1987 private:
1988     //!
1989     //! \brief    Init per stream parameters
1990     //! \details  Init per stream parameters
1991     //!
1992     //! \param    [in] streamState
1993     //!           Handle of Os Stream State
1994     //! \param    [in] extraParams
1995     //!           Additional parameters needed to init streamstate
1996     //!
1997     //! \return   MOS_STATUS
1998     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
1999     //!
2000     static MOS_STATUS InitStreamParameters(
2001         MOS_STREAM_HANDLE   streamState,
2002         EXTRA_PARAMS        extraParams = nullptr);
2003 
2004     //!
2005     //! \brief    Compose Cmd buffer header
2006     //! \details  Compose Cmd buffer header if it contains header
2007     //!
2008     //! \param    [in] streamState
2009     //!           Handle of Os Stream State
2010     //! \param    [out] cmdBuffer
2011     //!           Cmd buffer to compose header.
2012     //!
2013     //! \return   MOS_STATUS
2014     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
2015     //!
2016     static MOS_STATUS ComposeCommandBufferHeader(
2017         MOS_STREAM_HANDLE streamState,
2018         COMMAND_BUFFER_HANDLE cmdBuffer);
2019 
2020 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
2021     //! \brief    Unified dump command buffer initialization
2022     //! \details  check if dump command buffer was enabled and create the output directory
2023     //! \param    [in/out] streamState
2024     //!           Os stream state to init cmd buffer dump
2025     //! \return   MOS_STATUS
2026     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
2027     //!
2028     static MOS_STATUS DumpCommandBufferInit(
2029         MOS_STREAM_HANDLE streamState);
2030 #endif  // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
2031 
2032 #if (_DEBUG || _RELEASE_INTERNAL)
2033 
2034     enum OS_API_FAIL_TYPE
2035     {
2036         OS_API_FAIL_TYPE_NONE    = 0,
2037         OS_FAIL_ALLOC_GFX_RES    = 1,
2038         OS_FAIL_REGISTER_GFX_RES = 1 << 1,
2039         OS_API_FAIL_TYPE_MAX     = OS_FAIL_ALLOC_GFX_RES | OS_FAIL_REGISTER_GFX_RES,
2040     };
2041 
2042     enum OS_API_FAIL_SIMULATE_MODE
2043     {
2044         OS_API_FAIL_SIMULATE_MODE_DEFAULT  = 0,
2045         OS_API_FAIL_SIMULATE_MODE_RANDOM   = 1,
2046         OS_API_FAIL_SIMULATE_MODE_TRAVERSE = 1 << 1,
2047         OS_API_FAIL_SIMULATE_MODE_MAX      = OS_API_FAIL_SIMULATE_MODE_RANDOM | OS_API_FAIL_SIMULATE_MODE_TRAVERSE,
2048     };
2049 
2050     #define MIN_OS_API_FAIL_FREQ (1)      //max memory allcation fail rate 100%
2051     #define MAX_OS_API_FAIL_FREQ (10000)  //min memory allcation fail rate 1/10000
2052 
2053     #define MosOsApiFailSimulationEnabled(OsApiType)                  \
2054         (m_mosOsApiFailSimulateType == OsApiType &&                   \
2055          m_mosOsApiFailSimulateMode &  OS_API_FAIL_SIMULATE_MODE_MAX)
2056 
2057     //!
2058     //! \brief    Init OS API fail simulate flags
2059     //! \details  Init OS API fail simulate flags according user feature value
2060     //! \param    [in] mosCtx
2061     //!           os device ctx handle
2062     //! \return   void
2063     //!
2064     static void MosInitOsApiFailSimulateFlag(MOS_CONTEXT_HANDLE mosCtx);
2065 
2066     //!
2067     //! \brief    Deinit OS API fail simulate flags
2068     //! \details  Reset OS API fail simulate flags
2069     //! \param    none
2070     //! \return   void
2071     //!
2072     static void MosDeinitOsApiFailSimulateFlag();
2073 
2074     static bool MosSimulateOsApiFail(
2075         OS_API_FAIL_TYPE type,
2076         const char *functionName,
2077         const char *filename,
2078         int32_t     line);
2079 
2080     static uint32_t m_mosOsApiFailSimulateType;
2081     static uint32_t m_mosOsApiFailSimulateMode;
2082     static uint32_t m_mosOsApiFailSimulateFreq;
2083     static uint32_t m_mosOsApiFailSimulateHint;
2084     static uint32_t m_mosOsApiFailSimulateCounter;
2085 #endif
2086 };
2087 
2088 #endif  // __MOS_INTERFACE_H__
2089