Debian Codegraph @

media_softlet/agnostic/common/renderhal/surface_state_heap_mgr.cpp

raw
1 /*
2 * Copyright (c) 2024, 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 surface_state_heap_mgr.cpp
24 //! \brief Render Engine state heap manager for VP and CM
25 //! \details Platform/OS Independent Render Engine state heap management interfaces
26 //!
27 #include "surface_state_heap_mgr.h"
28 #include "vp_utils.h"
29
30 SurfaceStateHeapManager::SurfaceStateHeapManager(PMOS_INTERFACE pOsInterface) : m_osInterface(pOsInterface)
31 {
32 }
33
34 SurfaceStateHeapManager::~SurfaceStateHeapManager()
35 {
36 DestroyHeap();
37 }
38
39 MOS_STATUS SurfaceStateHeapManager::CreateHeap(size_t surfStateSize)
40 {
41 uint8_t *pMem;
42 uint32_t uiSize;
43 uint32_t uiOffset;
44 MOS_ALLOC_GFXRES_PARAMS AllocParams;
45 MOS_LOCK_PARAMS LockFlags;
46 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
47
48 uiSize = sizeof(SURFACE_STATES_HEAP_OBJ);
49 uiSize += MAX_SURFACE_STATES *
50 sizeof(SURFACE_STATES_OBJ);
51
52 // Allocate memory for render surface state heap
53 pMem = (uint8_t *)MOS_AllocAndZeroMemory(uiSize);
54 MHW_CHK_NULL_RETURN(pMem);
55
56 m_surfStateHeap = (SURFACE_STATES_HEAP_OBJ *)pMem;
57
58 m_surfStateHeap->pSurfStateObj =
59 (SURFACE_STATES_OBJ *)(pMem + sizeof(SURFACE_STATES_HEAP_OBJ));
60
61 // Appending sync data after all heap instances
62 m_surfStateHeap->uiOffsetSync = surfStateSize * MAX_SURFACE_STATES;
63
64 // Allocate GPU memory
65 uiSize = surfStateSize * MAX_SURFACE_STATES + SYNC_SIZE;
66
67 m_surfStateHeap->uiStateHeapSize = uiSize;
68 m_surfStateHeap->uiInstanceSize = surfStateSize;
69
70 MOS_ZeroMemory(&AllocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
71
72 AllocParams.Type = MOS_GFXRES_BUFFER;
73 AllocParams.TileType = MOS_TILE_LINEAR;
74 AllocParams.Format = Format_Buffer;
75 AllocParams.dwBytes = uiSize;
76 AllocParams.pBufName = "VphalSurfaceState";
77 AllocParams.ResUsageType = MOS_HW_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER;
78 AllocParams.dwMemType = MOS_MEMPOOL_VIDEOMEMORY;
79
80 MHW_CHK_STATUS_RETURN(m_osInterface->pfnAllocateResource(
81 m_osInterface,
82 &AllocParams,
83 &m_surfStateHeap->osResource));
84
85 // Lock the driver resource
86 MOS_ZeroMemory(&LockFlags, sizeof(MOS_LOCK_PARAMS));
87
88 LockFlags.NoOverWrite = 1;
89
90 m_surfStateHeap->pLockedOsResourceMem =
91 (uint8_t *)m_osInterface->pfnLockResource(
92 m_osInterface,
93 &m_surfStateHeap->osResource,
94 &LockFlags);
95 MHW_CHK_NULL_RETURN(m_surfStateHeap->pLockedOsResourceMem);
96
97 // Initialize VeboxHeap controls that depend on mapping
98 m_surfStateHeap->pSync =
99 (uint32_t *)(m_surfStateHeap->pLockedOsResourceMem +
100 m_surfStateHeap->uiOffsetSync);
101
102 return eStatus;
103 }
104
105 MOS_STATUS SurfaceStateHeapManager::DestroyHeap()
106 {
107 if (m_surfStateHeap)
108 {
109 if (!Mos_ResourceIsNull(&m_surfStateHeap->osResource))
110 {
111 if (m_surfStateHeap->pLockedOsResourceMem)
112 {
113 m_osInterface->pfnUnlockResource(
114 m_osInterface,
115 &m_surfStateHeap->osResource);
116 m_surfStateHeap->pLockedOsResourceMem = nullptr;
117 }
118
119 m_osInterface->pfnFreeResource(
120 m_osInterface,
121 &m_surfStateHeap->osResource);
122 }
123
124 MOS_FreeMemory(m_surfStateHeap);
125 m_surfStateHeap = nullptr;
126 }
127 return MOS_STATUS_SUCCESS;
128 }
129
130 void SurfaceStateHeapManager::RefreshSync()
131 {
132 SURFACE_STATES_OBJ *pCurInstance;
133 SURFACE_STATES_HEAP_OBJ *pSurfStateHeap;
134 uint32_t dwCurrentTag;
135 int32_t i;
136 int32_t iInstanceInUse;
137 MOS_NULL_RENDERING_FLAGS NullRenderingFlags;
138
139 MHW_FUNCTION_ENTER;
140 if (m_surfStateHeap == nullptr ||
141 m_osInterface == nullptr)
142 {
143 MHW_ASSERTMESSAGE("RefreshSync failed due to m_veboxHeap or m_osInterface is invalid ");
144 return;
145 }
146 iInstanceInUse = 0;
147
148 // Vebox Heap will always be locked by driver
149 pSurfStateHeap = m_surfStateHeap;
150
151 // Most recent tag
152 if (m_osInterface->bEnableKmdMediaFrameTracking)
153 {
154 dwCurrentTag = m_osInterface->pfnGetGpuStatusSyncTag(m_osInterface, MOS_GPU_CONTEXT_COMPUTE);
155 }
156 else
157 {
158 dwCurrentTag = pSurfStateHeap->pSync[0];
159 }
160 if (dwCurrentTag < 1)
161 {
162 return;
163 }
164 pSurfStateHeap->dwSyncTag = dwCurrentTag - 1;
165
166 NullRenderingFlags = m_osInterface->pfnGetNullHWRenderFlags(
167 m_osInterface);
168
169 // Refresh VeboxHeap states
170 pCurInstance = pSurfStateHeap->pSurfStateObj;
171 for (i = MAX_SURFACE_STATES; i > 0; i--, pCurInstance++)
172 {
173 if (!pCurInstance->bBusy)
174 continue;
175
176 // The condition below is valid when sync tag wraps from 2^32-1 to 0
177 if (((int32_t)(dwCurrentTag - pCurInstance->dwSyncTag) >= 0) ||
178 NullRenderingFlags.VPGobal)
179 {
180 pCurInstance->bBusy = false;
181 }
182 else
183 {
184 iInstanceInUse++;
185 }
186 }
187
188 // Save number of instance in use
189 m_surfHeapInUse = iInstanceInUse;
190 }
191
192 MOS_STATUS SurfaceStateHeapManager::AssignSurfaceState(uint32_t surfaceStateEntryIndex, uint32_t &offset, uint8_t *&curSurfaceStatePtr, PMOS_RESOURCE &stateHeap, int32_t &surfaceStateIndex)
193 {
194 VP_FUNC_CALL();
195
196 uint32_t dwWaitMs, dwWaitTag;
197 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
198
199 SURFACE_STATES_OBJ *pSurfStateCurObj;
200 SURFACE_STATES_HEAP_OBJ *pSurfStateHeap;
201
202 MHW_FUNCTION_ENTER;
203 MHW_CHK_NULL_RETURN(m_surfStateHeap);
204 MHW_CHK_NULL_RETURN(m_osInterface);
205
206 pSurfStateHeap = m_surfStateHeap;
207 pSurfStateCurObj = &m_surfStateHeap->pSurfStateObj[m_surfStateHeap->uiNextState];
208
209 // Refresh sync tag for all heap instance
210 RefreshSync();
211
212 // Check validity of current vebox heap instance
213 // The code below is unlikely to be executed - unless all Vebox states are in use
214 // If this ever happens, please consider increasing the number of media states
215 MHW_CHK_NULL_RETURN(pSurfStateCurObj);
216 if (pSurfStateCurObj->bBusy)
217 {
218 // Get current vebox instance sync tag
219 dwWaitTag = pSurfStateCurObj->dwSyncTag;
220
221 // Wait for Batch Buffer complete event OR timeout
222 for (dwWaitMs = MHW_TIMEOUT_MS_DEFAULT; dwWaitMs > 0; dwWaitMs--)
223 {
224 uint32_t dwCurrentTag;
225
226 MHW_CHK_STATUS_RETURN(m_osInterface->pfnWaitForBBCompleteNotifyEvent(
227 m_osInterface,
228 MOS_GPU_CONTEXT_COMPUTE,
229 MHW_EVENT_TIMEOUT_MS));
230
231 if (m_osInterface->bEnableKmdMediaFrameTracking)
232 {
233 dwCurrentTag = m_osInterface->pfnGetGpuStatusSyncTag(m_osInterface, MOS_GPU_CONTEXT_COMPUTE);
234 }
235 else
236 {
237 dwCurrentTag = pSurfStateHeap->pSync[0];
238 }
239 // Mark current instance status as availabe. Wait if this sync tag came back from GPU
240 if ((int32_t)(dwCurrentTag - dwWaitTag) >= 0)
241 {
242 pSurfStateCurObj->bBusy = false;
243 break;
244 }
245 }
246
247 // Timeout
248 if (dwWaitMs == 0)
249 {
250 MHW_ASSERTMESSAGE("Timeout on waiting for free Vebox Heap.");
251 eStatus = MOS_STATUS_UNKNOWN;
252 return eStatus;
253 }
254 }
255
256 // Prepare syncTag for GPU write back
257 if (m_osInterface->bEnableKmdMediaFrameTracking)
258 {
259 pSurfStateCurObj->dwSyncTag = m_osInterface->pfnGetGpuStatusTag(m_osInterface, MOS_GPU_CONTEXT_COMPUTE);
260 }
261 else
262 {
263 pSurfStateCurObj->dwSyncTag = pSurfStateHeap->dwNextTag;
264 }
265
266 // Assign current state and increase next state
267 pSurfStateHeap->uiCurState = pSurfStateHeap->uiNextState;
268 pSurfStateHeap->uiNextState = (pSurfStateHeap->uiNextState + 1) % MAX_SURFACE_STATES;
269
270 //Clean the memory of current veboxheap to avoid the history states
271 offset = pSurfStateHeap->uiCurState * pSurfStateHeap->uiInstanceSize;
272 MOS_ZeroMemory(pSurfStateHeap->pLockedOsResourceMem + offset, pSurfStateHeap->uiInstanceSize);
273
274 curSurfaceStatePtr = pSurfStateHeap->pLockedOsResourceMem + offset;
275 stateHeap = &pSurfStateHeap->osResource;
276 surfaceStateIndex = pSurfStateHeap->uiCurState;
277
278 m_usedStates.insert(std::make_pair(surfaceStateIndex, surfaceStateEntryIndex));
279
280 return eStatus;
281 }
282
283 MOS_STATUS SurfaceStateHeapManager::GetSurfaceStateSize(uint32_t& size)
284 {
285 MHW_CHK_NULL_RETURN(m_surfStateHeap);
286 size = m_surfStateHeap->uiInstanceSize;
287 return MOS_STATUS_SUCCESS;
288 }
289
290 MOS_STATUS SurfaceStateHeapManager::GetSurfaceStateBasePtr(uint8_t*& ptr)
291 {
292 MHW_CHK_NULL_RETURN(m_surfStateHeap);
293 ptr = m_surfStateHeap->pLockedOsResourceMem;
294 return MOS_STATUS_SUCCESS;
295 }