1 /*
2 * Copyright (c) 2019, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 //!
23 //! \file     mos_gpucontext_specific.h
24 //! \brief    Container class for the linux/Android specfic gpu context
25 //!
26 
27 #ifndef __GPU_CONTEXT_SPECIFIC_NEXT_H__
28 #define __GPU_CONTEXT_SPECIFIC_NEXT_H__
29 
30 #include "mos_gpucontext_next.h"
31 #include "mos_graphicsresource_specific_next.h"
32 
33 //!
34 //! \class  GpuContextSpecific
35 //! \brief  Linux/Android specific gpu context
36 //!
37 class GpuContextSpecificNext : public GpuContextNext
38 {
39 public:
40     //!
41     //! \brief  Constructor
42     //!
43     GpuContextSpecificNext(
44         const MOS_GPU_NODE gpuNode,
45         CmdBufMgrNext      *cmdBufMgr,
46         GpuContextNext    *reusedContext);
47 
48     //!
49     //! \brief  Destructor
50     //!
51     ~GpuContextSpecificNext();
52 
53     //!
54     //! \brief    Initialize gpu context
55     //! \details  Linux specific initialize for gpu context
56     //! \param    [in] osContext
57     //!           MOS device context pointer
58     //! \param    [in] streamState
59     //!           Os stream state
60     //! \param    [in] createOption
61     //!           Create option of creating GPU context
62     //! \return   MOS_STATUS
63     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
64     //!
65     MOS_STATUS Init(OsContextNext *osContext,
66                     MOS_STREAM_HANDLE streamState,
67                     PMOS_GPUCTX_CREATOPTIONS createOption);
68 
69     void Clear(void);
70 
71     //!
72     //! \brief    Register graphics resource
73     //! \details  Set the Allocation Index in OS resource structure
74     //! \param    [out] osContext
75     //!           Os context pointer
76     //! \param    [in] writeFlag
77     //!           Write Flag
78     //! \return   MOS_STATUS
79     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
80     //!
81     MOS_STATUS RegisterResource(
82         PMOS_RESOURCE osResource,
83         bool          writeFlag);
84 
85     MOS_STATUS GetCommandBuffer(
86         PMOS_COMMAND_BUFFER comamndBuffer,
87         uint32_t            flags);
88 
VerifyCommandBufferSize(const uint32_t requestedSize)89     MOS_STATUS VerifyCommandBufferSize(const uint32_t requestedSize)
90     {
91         return (m_commandBufferSize < requestedSize) ? MOS_STATUS_UNKNOWN:MOS_STATUS_SUCCESS;
92     }
93 
94     MOS_STATUS GetIndirectState(
95         uint32_t &offset,
96         uint32_t &size);
97 
98     MOS_STATUS GetIndirectStatePointer(
99         uint8_t **indirectState);
100 
101     MOS_STATUS SetIndirectStateSize(const uint32_t size);
102 
103     //!
104     //! \brief    Set patch entry
105     //! \details  Sets the patch entry in patch list
106     //! \param    [in] streamState
107     //!           OS steam state
108     //! \param    [in] params
109     //!           patch entry params
110     //! \return   MOS_STATUS
111     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
112     //!
113     MOS_STATUS SetPatchEntry(
114         MOS_STREAM_HANDLE streamState,
115         PMOS_PATCH_ENTRY_PARAMS params);
116 
117     void ReturnCommandBuffer(
118         PMOS_COMMAND_BUFFER cmdBuffer,
119         uint32_t            flags);
120 
121     //!
122     //! \brief    reset command buffer space
123     //! \details  resets the command buffer space
124     //! \return   MOS_STATUS
125     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
126     //!
127     MOS_STATUS ResetCommandBuffer();
128 
129     //!
130     //! \brief    Verifys the patch list to be used for rendering GPU commands is large enough
131     //! \param    [in] requestedSize
132     //!           Patch list size to be verified
133     //! \return   MOS_STATUS
134     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
135     //!
VerifyPatchListSize(const uint32_t requestedSize)136     MOS_STATUS VerifyPatchListSize(const uint32_t requestedSize)
137     {
138         return (requestedSize > m_maxPatchLocationsize) ? MOS_STATUS_UNKNOWN : MOS_STATUS_SUCCESS;
139     }
140 
141     MOS_STATUS SubmitCommandBuffer(
142         MOS_STREAM_HANDLE   streamState,
143         PMOS_COMMAND_BUFFER cmdBuffer,
144         bool                nullRendering);
145 
146     MOS_STATUS ResizeCommandBufferAndPatchList(
147         uint32_t requestedCommandBufferSize,
148         uint32_t requestedPatchListSize,
149         uint32_t flags);
150 
151     //!
152     //! \brief    Resizes the buffer to be used for rendering GPU commands
153     //! \param    [in] requestedSize
154     //!           Requested size
155     //! \return   MOS_STATUS
156     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
157     //!
158     MOS_STATUS ResizeCommandBuffer(uint32_t requestedSize);
159 
160     //!
161     //! \brief    Get GPU status tag
162     //! \details  Gets the status tag
163     //! \return   uint32_t
164     //!           Returns the tag
165     //!
GetGpuStatusTag()166     uint32_t   GetGpuStatusTag() { return m_GPUStatusTag; }
167 
168     //!
169     //! \brief    Increment GPU status tag
170     //!
171     void       IncrementGpuStatusTag();
172 
173     void       ResetGpuContextStatus();
174 
175     //!
176     //! \brief    Allocate gpu status buffer for gpu sync
177     //! \return   MOS_STATUS
178     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
179     //!
180     MOS_STATUS AllocateGPUStatusBuf();
181 
182 #if MOS_COMMAND_RESINFO_DUMP_SUPPORTED
PushCmdResPtr(const void * p)183     void                PushCmdResPtr(const void *p) { m_cmdResPtrs.push_back(p); }
ClearCmdResPtrs()184     void                ClearCmdResPtrs() { m_cmdResPtrs.clear(); }
GetCmdResPtrs()185     const std::vector<const void *> &GetCmdResPtrs() const { return m_cmdResPtrs; }
186 #endif // MOS_COMMAND_RESINFO_DUMP_SUPPORTED
187 protected:
188     //!
189     //! \brief    Map resources with aux plane to aux table
190     //! \return   MOS_STATUS
191     //!           Return MOS_STATUS_SUCCESS if successful, otherwise failed
192     //!
193     MOS_STATUS MapResourcesToAuxTable(mos_linux_bo *cmd_bo);
194 
195     MOS_VDBOX_NODE_IND GetVdboxNodeId(
196         PMOS_COMMAND_BUFFER cmdBuffer);
197 
198     uint32_t GetVcsExecFlag(
199         PMOS_COMMAND_BUFFER cmdBuffer,
200         MOS_GPU_NODE gpuNode);
201 
202     //!
203     //! \brief    Submit command buffer for single pipe in scalability mode
204     //! \return   int32_t
205     //!           Return 0 if successful, otherwise error code
206     //!
207     int32_t SubmitPipeCommands(MOS_COMMAND_BUFFER *cmdBuffer,
208                                MOS_LINUX_BO *cmdBo,
209                                PMOS_CONTEXT osContext,
210                                uint32_t execFlag,
211                                int32_t dr4);
212 
213     //!
214     //! \brief    Set the flags of engin quering according to create options
215     //! \return   void
216     //!
217     void SetEngineQueryFlags(
218         PMOS_GPUCTX_CREATOPTIONS option,
219         __u64 &caps);
220 
221 #if (_DEBUG || _RELEASE_INTERNAL)
222     MOS_LINUX_BO* GetNopCommandBuffer(
223         MOS_STREAM_HANDLE streamState);
224 #endif // _DEBUG || _RELEASE_INTERNAL
225 
226 private:
227     //! \brief    internal command buffer pool per gpu context
228     std::vector<CommandBufferNext *> m_cmdBufPool;
229 
230     //! \brief    internal command buffer pool per gpu context
231     PMOS_MUTEX m_cmdBufPoolMutex = nullptr;
232 
233     //! \brief    next fetch index of m_cmdBufPool
234     uint32_t m_nextFetchIndex = 0;
235 
236     //! \brief    initialized comamnd buffer size
237     uint32_t m_commandBufferSize = 0;
238 
239     //! \brief    Flag to indicate current command buffer flused or not, if not
240     //!           re-use it
241     volatile bool m_cmdBufFlushed;
242 
243     //! \brief    internal back up for in-use command buffer
244     PMOS_COMMAND_BUFFER m_commandBuffer = nullptr;
245 
246     //! \brief    secondary command buffers for scalability
247     std::map<uint32_t, PMOS_COMMAND_BUFFER> m_secondaryCmdBufs;
248 
249     //! \brief    Allcoation List related struct
250     ALLOCATION_LIST *m_allocationList = nullptr;
251     uint32_t         m_numAllocations = 0;  //!< number of registered allocation list
252     uint32_t         m_maxNumAllocations = 0;  //!< max number of allocation list
253 
254     //! \brief    Pathc List related struct
255     PATCHLOCATIONLIST *m_patchLocationList = nullptr;
256     uint32_t           m_currentNumPatchLocations = 0; //!< number of registered patch list
257     uint32_t           m_maxPatchLocationsize; //!< max number of patch list
258 
259    //! \brief    Resource registrations
260     uint32_t      m_resCount = 0;  //!< number of resources registered
261     PMOS_RESOURCE m_attachedResources = nullptr;  //!< Pointer to resources list
262     bool         *m_writeModeList     = nullptr;  //!< Write mode
263 
264     //! \brief    GPU Status tag
265     uint32_t m_GPUStatusTag = 0;
266 
267     //! \brief    Os context
268     OsContextNext *m_osContext = nullptr;
269 
270     MOS_GPUCTX_CREATOPTIONS_ENHANCED *m_createOptionEnhanced = nullptr;
271     MOS_LINUX_CONTEXT*  m_i915Context[MAX_ENGINE_INSTANCE_NUM+1];
272     uint32_t     m_i915ExecFlag = 0;
273 #if MOS_COMMAND_RESINFO_DUMP_SUPPORTED
274     std::vector<const void *> m_cmdResPtrs; //!< Command OS resource pointers registered by pfnRegisterResource
275 #endif // MOS_COMMAND_RESINFO_DUMP_SUPPORTED
276 };
277 #endif  // __GPU_CONTEXT_SPECIFIC_NEXT_H__
278