1 /*
2 * Copyright (c) 2021-2022, 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     media_render_cmd_packet.cpp
24 //! \brief    Defines the interface for media render workload cmd packet
25 //! \details  The media cmd packet is dedicated for command buffer sequenece submit
26 //!
27 
28 #include "media_render_cmd_packet.h"
29 #include "renderhal_platform_interface.h"
30 #include "hal_oca_interface_next.h"
31 #include "mos_os_cp_interface_specific.h"
32 
33 #define COMPUTE_WALKER_THREAD_SPACE_WIDTH 1
34 #define COMPUTE_WALKER_THREAD_SPACE_HEIGHT 1
35 #define COMPUTE_WALKER_THREAD_SPACE_DEPTH 1
36 
RenderCmdPacket(MediaTask * task,PMOS_INTERFACE pOsInterface,RENDERHAL_INTERFACE * renderHal)37 RenderCmdPacket::RenderCmdPacket(MediaTask* task, PMOS_INTERFACE pOsInterface, RENDERHAL_INTERFACE* renderHal) : CmdPacket(task),
38 m_renderHal(renderHal),
39 m_cpInterface(nullptr)
40 {
41     m_osInterface = pOsInterface;
42 }
43 
~RenderCmdPacket()44 RenderCmdPacket::~RenderCmdPacket()
45 {
46     Destroy();
47 }
48 
Init()49 MOS_STATUS RenderCmdPacket::Init()
50 {
51     bool mediaWalkerUsed = false;
52     bool computeWalkerUsed = false;
53 
54     if (nullptr == m_renderHal)
55     {
56         // Could not allocate renderhal in packet class
57         // Because we need to share renderhal for many Packets to reduce memory
58         RENDER_PACKET_ASSERTMESSAGE("renderHal should be passed When doing construction");
59         return MOS_STATUS_INVALID_PARAMETER;
60     }
61     mediaWalkerUsed = m_renderHal->pfnGetMediaWalkerStatus(m_renderHal) ? true : false;
62     computeWalkerUsed = m_renderHal->pRenderHalPltInterface->IsComputeContextInUse(m_renderHal);
63 
64     if (mediaWalkerUsed && !computeWalkerUsed)
65     {
66         m_walkerType = WALKER_TYPE_MEDIA;
67     }
68     else if (computeWalkerUsed)
69     {
70         m_walkerType = WALKER_TYPE_COMPUTE;
71     }
72     else
73     {
74         m_walkerType = WALKER_TYPE_DISABLED;
75     }
76 
77     if (m_renderHal->pRenderHalPltInterface)
78     {
79         m_miItf = m_renderHal->pRenderHalPltInterface->GetMhwMiItf();
80     }
81 
82     return MOS_STATUS_SUCCESS;
83 }
84 
Destroy()85 MOS_STATUS RenderCmdPacket::Destroy()
86 {
87     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
88 
89     return MOS_STATUS_SUCCESS;
90 }
91 
Submit(MOS_COMMAND_BUFFER * commandBuffer,uint8_t packetPhase)92 MOS_STATUS RenderCmdPacket::Submit(MOS_COMMAND_BUFFER *commandBuffer, uint8_t packetPhase)
93 {
94     PMOS_INTERFACE                      pOsInterface = nullptr;
95     MOS_STATUS                          eStatus = MOS_STATUS_SUCCESS;
96     uint32_t                            dwSyncTag = 0;
97     int32_t                             i = 0, iRemaining = 0;
98     MHW_MEDIA_STATE_FLUSH_PARAM         FlushParam = {};
99     bool                                bEnableSLM = false;
100     RENDERHAL_GENERIC_PROLOG_PARAMS     GenericPrologParams = {};
101     MOS_RESOURCE                        GpuStatusBuffer = {};
102     MOS_CONTEXT*                        pOsContext = nullptr;
103     PMHW_MI_MMIOREGISTERS               pMmioRegisters = nullptr;
104 
105     RENDER_PACKET_CHK_NULL_RETURN(m_renderHal);
106     RENDER_PACKET_CHK_NULL_RETURN(m_renderHal->pRenderHalPltInterface);
107     RENDER_PACKET_CHK_NULL_RETURN(m_renderHal->pRenderHalPltInterface->GetMmioRegisters(m_renderHal));
108     RENDER_PACKET_CHK_NULL_RETURN(m_renderHal->pOsInterface);
109     RENDER_PACKET_CHK_NULL_RETURN(m_renderHal->pOsInterface->pOsContext);
110     RENDER_PACKET_CHK_NULL_RETURN(commandBuffer);
111 
112     eStatus = MOS_STATUS_UNKNOWN;
113     pOsInterface = m_renderHal->pOsInterface;
114     iRemaining = 0;
115     FlushParam = g_cRenderHal_InitMediaStateFlushParams;
116     pOsContext = pOsInterface->pOsContext;
117     pMmioRegisters = m_renderHal->pRenderHalPltInterface->GetMmioRegisters(m_renderHal);
118 
119     RENDER_PACKET_CHK_STATUS_RETURN(SetPowerMode(0));
120 
121     m_renderHal->pRenderHalPltInterface->On1stLevelBBStart(m_renderHal, commandBuffer, pOsContext, pOsInterface->CurrentGpuContextHandle, pMmioRegisters);
122 
123     OcaDumpDbgInfo(*commandBuffer, *pOsContext);
124 
125     RENDER_PACKET_CHK_STATUS_RETURN(SetMediaFrameTracking(GenericPrologParams));
126 
127     // Initialize command buffer and insert prolog
128     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnInitCommandBuffer(m_renderHal, commandBuffer, &GenericPrologParams));
129 
130     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddPerfCollectStartCmd(m_renderHal, pOsInterface, commandBuffer));
131 
132     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->StartPredicate(m_renderHal, commandBuffer));
133 
134     // Write timing data for 3P budget
135     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSendTimingData(m_renderHal, commandBuffer, true));
136 
137     bEnableSLM = false;  // Media walker first
138     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetCacheOverrideParams(
139         m_renderHal,
140         &m_renderHal->L3CacheSettings,
141         bEnableSLM));
142 
143     if (m_renderHal->bCmfcCoeffUpdate)
144     {
145         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSendCscCoeffSurface(m_renderHal,
146             commandBuffer,
147             m_renderHal->pCmfcCoeffSurface,
148             m_renderHal->pStateHeap->pKernelAllocation[m_renderHal->iKernelAllocationID].pKernelEntry));
149     }
150 
151     // Flush media states
152     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSendMediaStates(
153         m_renderHal,
154         commandBuffer,
155         (m_walkerType == WALKER_TYPE_MEDIA) ? &m_mediaWalkerParams : nullptr,
156         &m_gpgpuWalkerParams));
157 
158     // Write back GPU Status tag
159     if (!pOsInterface->bEnableKmdMediaFrameTracking)
160     {
161         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSendRcsStatusTag(m_renderHal, commandBuffer));
162     }
163 
164     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->StopPredicate(m_renderHal, commandBuffer));
165 
166     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddPerfCollectEndCmd(m_renderHal, pOsInterface, commandBuffer));
167 
168     // Write timing data for 3P budget
169     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSendTimingData(m_renderHal, commandBuffer, false));
170 
171     MHW_PIPE_CONTROL_PARAMS PipeControlParams;
172 
173     MOS_ZeroMemory(&PipeControlParams, sizeof(PipeControlParams));
174     PipeControlParams.dwFlushMode = MHW_FLUSH_WRITE_CACHE;
175     PipeControlParams.bGenericMediaStateClear = true;
176     PipeControlParams.bIndirectStatePointersDisable = true;
177     PipeControlParams.bDisableCSStall = false;
178 
179     RENDER_PACKET_CHK_NULL_RETURN(pOsInterface->pfnGetSkuTable);
180     auto *skuTable = pOsInterface->pfnGetSkuTable(pOsInterface);
181     if (skuTable && MEDIA_IS_SKU(skuTable, FtrEnablePPCFlush))
182     {
183         // Add PPC fulsh
184         PipeControlParams.bPPCFlush = true;
185     }
186     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddMiPipeControl(m_renderHal, commandBuffer, &PipeControlParams));
187 
188     if (MEDIA_IS_WA(m_renderHal->pWaTable, WaSendDummyVFEafterPipelineSelect))
189     {
190         MHW_VFE_PARAMS VfeStateParams = {};
191         VfeStateParams.dwNumberofURBEntries = 1;
192         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddMediaVfeCmd(m_renderHal, commandBuffer, &VfeStateParams));
193     }
194 
195     // Add media flush command in case HW not cleaning the media state
196     if (MEDIA_IS_WA(m_renderHal->pWaTable, WaMSFWithNoWatermarkTSGHang))
197     {
198         FlushParam.bFlushToGo = true;
199         if (m_walkerType == WALKER_TYPE_MEDIA)
200         {
201             FlushParam.ui8InterfaceDescriptorOffset = m_mediaWalkerParams.InterfaceDescriptorOffset;
202         }
203         else
204         {
205             RENDER_PACKET_ASSERTMESSAGE("ERROR, pWalkerParams is nullptr and cannot get InterfaceDescriptorOffset.");
206         }
207 
208         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddMediaStateFlush(m_renderHal, commandBuffer, &FlushParam));
209     }
210     else if (MEDIA_IS_WA(m_renderHal->pWaTable, WaAddMediaStateFlushCmd))
211     {
212         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddMediaStateFlush(m_renderHal, commandBuffer, &FlushParam));
213     }
214 
215 #if (_DEBUG || _RELEASE_INTERNAL)
216     RENDER_PACKET_CHK_STATUS_RETURN(StallBatchBuffer(commandBuffer));
217 #endif
218 
219     HalOcaInterfaceNext::On1stLevelBBEnd(*commandBuffer, *pOsInterface);
220 
221     if (pBatchBuffer)
222     {
223         // Send Batch Buffer end command (HW/OS dependent)
224         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddMiBatchBufferEnd(m_renderHal, commandBuffer, nullptr));
225     }
226     else if (IsMiBBEndNeeded(pOsInterface))
227     {
228         // Send Batch Buffer end command for 1st level Batch Buffer
229         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddMiBatchBufferEnd(m_renderHal, commandBuffer, nullptr));
230     }
231     else if (m_renderHal->pOsInterface->bNoParsingAssistanceInKmd)
232     {
233         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pRenderHalPltInterface->AddMiBatchBufferEnd(m_renderHal, commandBuffer, nullptr));
234     }
235 
236     // No need return command buffer here, which will be done in CmdTask::Submit.
237     MOS_NULL_RENDERING_FLAGS  NullRenderingFlags;
238 
239     NullRenderingFlags =
240         pOsInterface->pfnGetNullHWRenderFlags(pOsInterface);
241 
242     if ((NullRenderingFlags.VPLgca ||
243         NullRenderingFlags.VPGobal) == false)
244     {
245         dwSyncTag = m_renderHal->pStateHeap->dwNextTag++;
246 
247         // Set media state and batch buffer as busy
248         m_renderHal->pStateHeap->pCurMediaState->bBusy = true;
249         if (pBatchBuffer)
250         {
251             pBatchBuffer->bBusy = true;
252             pBatchBuffer->dwSyncTag = dwSyncTag;
253         }
254     }
255 
256     return MOS_STATUS_SUCCESS;
257 }
258 
RenderEngineSetup()259 MOS_STATUS RenderCmdPacket::RenderEngineSetup()
260 {
261     // pls make sure the context already switched to render/compute engine before submit
262     RENDER_PACKET_CHK_NULL_RETURN(m_renderHal);
263 
264     // Register the resource of GSH
265     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnReset(m_renderHal));
266 
267     // Assign media state
268     m_renderData.mediaState = m_renderHal->pfnAssignMediaState(m_renderHal, RENDERHAL_COMPONENT_PACKET);
269     RENDER_PACKET_CHK_NULL_RETURN(m_renderData.mediaState);
270     RENDER_PACKET_CHK_NULL_RETURN(m_renderHal->pStateHeap);
271 
272     // Allocate and reset SSH instance
273     if ((m_isMultiBindingTables == false) || (m_renderHal->pStateHeap->iCurrentBindingTable >= m_renderHal->StateHeapSettings.iBindingTables) ||
274         (m_renderHal->pStateHeap->iCurrentSurfaceState >= m_renderHal->StateHeapSettings.iSurfaceStates) || m_isMultiKernelOneMediaState)
275     {
276         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnAssignSshInstance(m_renderHal));
277     }
278 
279     // Assign and Reset binding table
280     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnAssignBindingTable(
281         m_renderHal,
282         &m_renderData.bindingTable));
283 
284     // Reset a new Binding index from start
285     m_renderData.bindingTableEntry = 0;
286 
287     // load kernels before packet submit PipeLine to load kernel
288 
289     return MOS_STATUS_SUCCESS;
290 }
291 
292 
InitKernelEntry()293 MOS_STATUS RenderCmdPacket::InitKernelEntry()
294 {
295     if (m_kernelCount == 0)
296     {
297         RENDER_PACKET_NORMALMESSAGE("no kernel set up");
298         return MOS_STATUS_LOAD_LIBRARY_FAILED;
299     }
300 
301     return MOS_STATUS_SUCCESS;
302 }
303 
SetPowerMode(uint32_t KernelID)304 MOS_STATUS RenderCmdPacket::SetPowerMode(uint32_t KernelID)
305 {
306     MOS_STATUS            eStatus                = MOS_STATUS_SUCCESS;
307     uint16_t              wNumRequestedEUSlices  = 1;  // Default to 1 slice
308     uint16_t              wNumRequestedSubSlices = 3;  // Default to 3 subslice
309     uint16_t              wNumRequestedEUs       = 8;  // Default to 8 EUs
310     RENDERHAL_POWEROPTION PowerOption;
311     bool                  bSetRequestedSlices = false;
312     const SseuSetting *   pcSSEUTable         = nullptr;
313     MediaUserSettingSharedPtr   userSettingPtr  = nullptr;
314 
315     RENDER_PACKET_CHK_NULL_RETURN(m_renderHal);
316 
317     if ((m_renderHal->bRequestSingleSlice) || (m_renderHal->bEUSaturationNoSSD))
318     {
319         bSetRequestedSlices = true;
320         // bEUSaturationNoSSD: No slice shutdown, must request 2 slices [CM EU saturation on].
321         // bRequestSingleSlice: Always single slice.
322         wNumRequestedEUSlices = (m_renderHal->bEUSaturationNoSSD) ? 2 : 1;
323     }
324     else
325     {
326         bSetRequestedSlices = false;
327     }
328 
329     if (m_renderHal->sseuTable)
330     {
331         pcSSEUTable = (const SseuSetting *)m_renderHal->sseuTable;
332     }
333     else
334     {
335         RENDER_PACKET_ASSERTMESSAGE("SSEU Table not valid.");
336         return MOS_STATUS_UNKNOWN;
337     }
338 
339     RENDER_PACKET_CHK_NULL_RETURN(pcSSEUTable);
340     pcSSEUTable += KernelID;
341     if (!bSetRequestedSlices)  // If num Slices is already programmed, then don't change it
342     {
343         if (wNumRequestedEUSlices < pcSSEUTable->numSlices)
344         {
345             wNumRequestedEUSlices = pcSSEUTable->numSlices;
346         }
347     }
348 
349     wNumRequestedSubSlices = pcSSEUTable->numSubSlices;
350     wNumRequestedEUs       = pcSSEUTable->numEUs;
351 
352 #if (_DEBUG || _RELEASE_INTERNAL)
353     // User feature key reads
354     userSettingPtr = m_osInterface->pfnGetUserSettingInstance(m_osInterface);
355 
356     uint32_t value = 0;
357     ReadUserSettingForDebug(
358         userSettingPtr,
359         value,
360         __MEDIA_USER_FEATURE_VALUE_SSEU_SETTING_OVERRIDE,
361         MediaUserSetting::Group::Device);
362     if (value != 0xDEADC0DE)
363     {
364         wNumRequestedEUSlices  = value & 0xFF;            // Bits 0-7
365         wNumRequestedSubSlices = (value >> 8) & 0xFF;     // Bits 8-15
366         wNumRequestedEUs       = (value >> 16) & 0xFFFF;  // Bits 16-31
367     }
368 #endif
369 
370     PowerOption.nSlice    = wNumRequestedEUSlices;
371     PowerOption.nSubSlice = wNumRequestedSubSlices;
372     PowerOption.nEU       = wNumRequestedEUs;
373     m_renderHal->pfnSetPowerOptionMode(m_renderHal, &PowerOption);
374 
375     return eStatus;
376 }
377 
SetSurfaceForHwAccess(PMOS_SURFACE surface,PRENDERHAL_SURFACE_NEXT pRenderSurface,PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,bool bWrite)378 uint32_t RenderCmdPacket::SetSurfaceForHwAccess(
379     PMOS_SURFACE                    surface,
380     PRENDERHAL_SURFACE_NEXT         pRenderSurface,
381     PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,
382     bool                            bWrite)
383 {
384     PMOS_INTERFACE                 pOsInterface;
385     PRENDERHAL_SURFACE_STATE_ENTRY pSurfaceEntries[MHW_MAX_SURFACE_PLANES];
386     int32_t                        iSurfaceEntries;
387     int32_t                        i;
388     MOS_STATUS                     eStatus;
389     RENDERHAL_SURFACE_STATE_PARAMS surfaceParams;
390 
391     // Initialize Variables
392     eStatus      = MOS_STATUS_SUCCESS;
393     pOsInterface = m_osInterface;
394 
395     RENDER_PACKET_CHK_NULL_RETURN(pRenderSurface);
396     RENDER_PACKET_CHK_NULL_RETURN(pOsInterface);
397 
398     // Register surfaces for rendering (GfxAddress/Allocation index)
399     // Register resource
400     RENDER_PACKET_CHK_STATUS_RETURN(m_osInterface->pfnRegisterResource(
401         m_osInterface,
402         &surface->OsResource,
403         bWrite,
404         true));
405 
406     if (!pSurfaceParams)
407     {
408         MOS_ZeroMemory(&surfaceParams, sizeof(RENDERHAL_SURFACE_STATE_PARAMS));
409 
410         //set mem object control for cache
411         surfaceParams.MemObjCtl = (m_renderHal->pOsInterface->pfnCachePolicyGetMemoryObject(
412                                        MOS_HW_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER,
413                                        m_renderHal->pOsInterface->pfnGetGmmClientContext(m_renderHal->pOsInterface))).DwordValue;
414 
415         pSurfaceParams = &surfaceParams;
416     }
417 
418     if (pSurfaceParams->bAVS)
419     {
420         pSurfaceParams->Type = m_renderHal->SurfaceTypeAdvanced;
421     }
422     else
423     {
424         pSurfaceParams->Type = m_renderHal->SurfaceTypeDefault;
425     }
426 
427     RENDER_PACKET_CHK_STATUS_RETURN(InitRenderHalSurface(
428         *surface,
429         pRenderSurface));
430 
431     if (bWrite)
432     {
433         pRenderSurface->SurfType = RENDERHAL_SURF_OUT_RENDERTARGET;
434     }
435 
436     // Setup surface states-----------------------------------------------------
437     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetupSurfaceState(
438         m_renderHal,
439         pRenderSurface,
440         pSurfaceParams,
441         &iSurfaceEntries,  // for most cases, surface entry should only take 1 entry, need align with kerenl design
442         pSurfaceEntries,
443         nullptr));
444 
445     if (!m_isLargeSurfaceStateNeeded)
446     {
447         if (m_renderData.bindingTableEntry > 15)
448         {
449             RENDER_PACKET_ASSERTMESSAGE("input surface support up to 16 RSS");
450             m_renderData.bindingTableEntry = 0;
451         }
452     }
453     else
454     {
455         if (m_renderData.bindingTableEntry > 255)
456         {
457             RENDER_PACKET_ASSERTMESSAGE("input surface support up to 256 RSS");
458             m_renderData.bindingTableEntry = 0;
459         }
460     }
461 
462     uint32_t iBTEntry = m_renderData.bindingTableEntry;
463     // Bind surface states------------------------------------------------------
464     for (i = 0; i < iSurfaceEntries; i++, m_renderData.bindingTableEntry++)
465     {
466         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnBindSurfaceState(
467             m_renderHal,
468             m_renderData.bindingTable,
469             m_renderData.bindingTableEntry,
470             pSurfaceEntries[i]));
471 
472         pRenderSurface->Index = m_renderData.bindingTableEntry;
473     }
474 
475     return iBTEntry;
476 }
477 
478 
SetSurfaceForHwAccess(PMOS_SURFACE surface,PRENDERHAL_SURFACE_NEXT pRenderSurface,PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,bool bWrite,std::set<uint32_t> & stateOffsets)479 uint32_t RenderCmdPacket::SetSurfaceForHwAccess(
480     PMOS_SURFACE                    surface,
481     PRENDERHAL_SURFACE_NEXT         pRenderSurface,
482     PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,
483     bool                            bWrite,
484     std::set<uint32_t>             &stateOffsets)
485 {
486     PMOS_INTERFACE                 pOsInterface;
487     PRENDERHAL_SURFACE_STATE_ENTRY pSurfaceEntries[MHW_MAX_SURFACE_PLANES];
488     int32_t                        iSurfaceEntries;
489     int32_t                        i;
490     MOS_STATUS                     eStatus;
491     RENDERHAL_SURFACE_STATE_PARAMS surfaceParams;
492 
493     // Initialize Variables
494     eStatus      = MOS_STATUS_SUCCESS;
495     pOsInterface = m_osInterface;
496 
497     RENDER_PACKET_CHK_NULL_RETURN(pRenderSurface);
498     RENDER_PACKET_CHK_NULL_RETURN(pOsInterface);
499 
500     // Register surfaces for rendering (GfxAddress/Allocation index)
501     // Register resource
502     RENDER_PACKET_CHK_STATUS_RETURN(m_osInterface->pfnRegisterResource(
503         m_osInterface,
504         &surface->OsResource,
505         bWrite,
506         true));
507 
508     if (!pSurfaceParams)
509     {
510         MOS_ZeroMemory(&surfaceParams, sizeof(RENDERHAL_SURFACE_STATE_PARAMS));
511 
512         //set mem object control for cache
513         surfaceParams.MemObjCtl = (m_renderHal->pOsInterface->pfnCachePolicyGetMemoryObject(
514                                        MOS_HW_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER,
515                                        m_renderHal->pOsInterface->pfnGetGmmClientContext(m_renderHal->pOsInterface))).DwordValue;
516 
517         pSurfaceParams = &surfaceParams;
518     }
519 
520     if (pSurfaceParams->bAVS)
521     {
522         pSurfaceParams->Type = m_renderHal->SurfaceTypeAdvanced;
523     }
524     else
525     {
526         pSurfaceParams->Type = m_renderHal->SurfaceTypeDefault;
527     }
528 
529     RENDER_PACKET_CHK_STATUS_RETURN(InitRenderHalSurface(
530         *surface,
531         pRenderSurface));
532 
533     if (bWrite)
534     {
535         pRenderSurface->SurfType = RENDERHAL_SURF_OUT_RENDERTARGET;
536     }
537 
538     // Setup surface states-----------------------------------------------------
539     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetupSurfaceState(
540         m_renderHal,
541         pRenderSurface,
542         pSurfaceParams,
543         &iSurfaceEntries,  // for most cases, surface entry should only take 1 entry, need align with kerenl design
544         pSurfaceEntries,
545         nullptr));
546 
547     if (!m_isLargeSurfaceStateNeeded)
548     {
549         if (m_renderData.bindingTableEntry > 15)
550         {
551             RENDER_PACKET_ASSERTMESSAGE("input surface support up to 16 RSS");
552             m_renderData.bindingTableEntry = 0;
553         }
554     }
555     else
556     {
557         if (m_renderData.bindingTableEntry > 255)
558         {
559             RENDER_PACKET_ASSERTMESSAGE("input surface support up to 256 RSS");
560             m_renderData.bindingTableEntry = 0;
561         }
562     }
563 
564     uint32_t iBTEntry = m_renderData.bindingTableEntry;
565     if (m_renderHal->isBindlessHeapInUse == false)
566     {
567         // Bind surface states------------------------------------------------------
568         for (i = 0; i < iSurfaceEntries; i++, m_renderData.bindingTableEntry++)
569         {
570             RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnBindSurfaceState(
571                 m_renderHal,
572                 m_renderData.bindingTable,
573                 m_renderData.bindingTableEntry,
574                 pSurfaceEntries[i]));
575 
576             pRenderSurface->Index = m_renderData.bindingTableEntry;
577         }
578     }
579     else
580     {
581         for (i = 0; i < iSurfaceEntries; i++)
582         {
583             stateOffsets.insert(pSurfaceEntries[i]->dwSurfStateOffset);
584         }
585     }
586 
587     return iBTEntry;
588 }
589 
SetSurfaceForHwAccess(PMOS_SURFACE surface,PRENDERHAL_SURFACE_NEXT pRenderSurface,PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,uint32_t & bindingIndex,bool bWrite,PRENDERHAL_SURFACE_STATE_ENTRY * surfaceEntries,uint32_t * numOfSurfaceEntries)590 MOS_STATUS RenderCmdPacket::SetSurfaceForHwAccess(
591     PMOS_SURFACE                    surface,
592     PRENDERHAL_SURFACE_NEXT         pRenderSurface,
593     PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,
594     uint32_t                        &bindingIndex,
595     bool                            bWrite,
596     PRENDERHAL_SURFACE_STATE_ENTRY *surfaceEntries,
597     uint32_t *                      numOfSurfaceEntries)
598 {
599     PMOS_INTERFACE                  pOsInterface                              = nullptr;
600     PRENDERHAL_SURFACE_STATE_ENTRY  surfaceEntriesTmp[MHW_MAX_SURFACE_PLANES] = {};
601     PRENDERHAL_SURFACE_STATE_ENTRY *pSurfaceEntries                           = nullptr;
602     int32_t                         iSurfaceEntries                           = 0;
603     int32_t                         i                                         = 0;
604     MOS_STATUS                      eStatus                                   = MOS_STATUS_SUCCESS;
605     RENDERHAL_SURFACE_STATE_PARAMS  surfaceParams                             = {};
606 
607     if (nullptr == surfaceEntries || nullptr == numOfSurfaceEntries)
608     {
609         pSurfaceEntries = surfaceEntriesTmp;
610     }
611     else
612     {
613         pSurfaceEntries = surfaceEntries;
614     }
615 
616     // Initialize Variables
617     eStatus      = MOS_STATUS_SUCCESS;
618     pOsInterface = m_osInterface;
619 
620     RENDER_PACKET_CHK_NULL_RETURN(pRenderSurface);
621     RENDER_PACKET_CHK_NULL_RETURN(pOsInterface);
622 
623     // Register surfaces for rendering (GfxAddress/Allocation index)
624     // Register resource
625     RENDER_PACKET_CHK_STATUS_RETURN(m_osInterface->pfnRegisterResource(
626         m_osInterface,
627         &surface->OsResource,
628         bWrite,
629         true));
630 
631     if (!pSurfaceParams)
632     {
633         MOS_ZeroMemory(&surfaceParams, sizeof(RENDERHAL_SURFACE_STATE_PARAMS));
634 
635         //set mem object control for cache
636         surfaceParams.MemObjCtl = (m_renderHal->pOsInterface->pfnCachePolicyGetMemoryObject(
637                                        MOS_HW_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER,
638                                        m_renderHal->pOsInterface->pfnGetGmmClientContext(m_renderHal->pOsInterface))).DwordValue;
639 
640         pSurfaceParams = &surfaceParams;
641     }
642 
643     if (pSurfaceParams->bAVS)
644     {
645         pSurfaceParams->Type = m_renderHal->SurfaceTypeAdvanced;
646     }
647     else
648     {
649         pSurfaceParams->Type = m_renderHal->SurfaceTypeDefault;
650     }
651 
652     RENDER_PACKET_CHK_STATUS_RETURN(InitRenderHalSurface(
653         *surface,
654         pRenderSurface));
655 
656     if (bWrite)
657     {
658         pRenderSurface->SurfType = RENDERHAL_SURF_OUT_RENDERTARGET;
659     }
660 
661     // Setup surface states-----------------------------------------------------
662     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetupSurfaceState(
663         m_renderHal,
664         pRenderSurface,
665         pSurfaceParams,
666         &iSurfaceEntries,  // for most cases, surface entry should only take 1 entry, need align with kerenl design
667         pSurfaceEntries,
668         nullptr));
669 
670     uint32_t iBTEntry = bindingIndex;
671     // Bind surface states------------------------------------------------------
672     for (i = 0; i < iSurfaceEntries; i++, iBTEntry++)
673     {
674         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnBindSurfaceState(
675             m_renderHal,
676             m_renderData.bindingTable,
677             iBTEntry,
678             pSurfaceEntries[i]));
679 
680         pRenderSurface->Index = iBTEntry;
681     }
682 
683     if (numOfSurfaceEntries)
684     {
685         *numOfSurfaceEntries = iSurfaceEntries;
686     }
687 
688     return eStatus;
689 }
690 
SetSurfaceForHwAccess(PMOS_SURFACE surface,PRENDERHAL_SURFACE_NEXT pRenderSurface,PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,std::set<uint32_t> & bindingIndexes,bool bWrite,std::set<uint32_t> & stateOffsets,uint32_t capcityOfSurfaceEntries,PRENDERHAL_SURFACE_STATE_ENTRY * surfaceEntries,uint32_t * numOfSurfaceEntries)691 MOS_STATUS RenderCmdPacket::SetSurfaceForHwAccess(
692     PMOS_SURFACE                    surface,
693     PRENDERHAL_SURFACE_NEXT         pRenderSurface,
694     PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,
695     std::set<uint32_t>             &bindingIndexes,
696     bool                            bWrite,
697     std::set<uint32_t>             &stateOffsets,
698     uint32_t                        capcityOfSurfaceEntries,
699     PRENDERHAL_SURFACE_STATE_ENTRY *surfaceEntries,
700     uint32_t                       *numOfSurfaceEntries)
701 {
702     PMOS_INTERFACE                  pOsInterface                              = nullptr;
703     PRENDERHAL_SURFACE_STATE_ENTRY  pSurfaceEntries[MHW_MAX_SURFACE_PLANES]   = {};
704     int32_t                         iSurfaceEntries                           = 0;
705     int32_t                         i                                         = 0;
706     MOS_STATUS                      eStatus                                   = MOS_STATUS_SUCCESS;
707     RENDERHAL_SURFACE_STATE_PARAMS  surfaceParams                             = {};
708 
709     // Initialize Variables
710     eStatus      = MOS_STATUS_SUCCESS;
711     pOsInterface = m_osInterface;
712 
713     RENDER_PACKET_CHK_NULL_RETURN(pRenderSurface);
714     RENDER_PACKET_CHK_NULL_RETURN(pOsInterface);
715 
716     // Register surfaces for rendering (GfxAddress/Allocation index)
717     // Register resource
718     RENDER_PACKET_CHK_STATUS_RETURN(m_osInterface->pfnRegisterResource(
719         m_osInterface,
720         &surface->OsResource,
721         bWrite,
722         true));
723 
724     if (!pSurfaceParams)
725     {
726         MOS_ZeroMemory(&surfaceParams, sizeof(RENDERHAL_SURFACE_STATE_PARAMS));
727 
728         //set mem object control for cache
729         surfaceParams.MemObjCtl = (m_renderHal->pOsInterface->pfnCachePolicyGetMemoryObject(
730                                        MOS_HW_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER,
731                                        m_renderHal->pOsInterface->pfnGetGmmClientContext(m_renderHal->pOsInterface)))
732                                       .DwordValue;
733 
734         pSurfaceParams = &surfaceParams;
735     }
736 
737     if (pSurfaceParams->bAVS)
738     {
739         pSurfaceParams->Type = m_renderHal->SurfaceTypeAdvanced;
740     }
741     else
742     {
743         pSurfaceParams->Type = m_renderHal->SurfaceTypeDefault;
744     }
745 
746     RENDER_PACKET_CHK_STATUS_RETURN(InitRenderHalSurface(
747         *surface,
748         pRenderSurface));
749 
750     if (bWrite)
751     {
752         pRenderSurface->SurfType = RENDERHAL_SURF_OUT_RENDERTARGET;
753     }
754 
755     // Setup surface states-----------------------------------------------------
756     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetupSurfaceState(
757         m_renderHal,
758         pRenderSurface,
759         pSurfaceParams,
760         &iSurfaceEntries,  // for most cases, surface entry should only take 1 entry, need align with kerenl design
761         pSurfaceEntries,
762         nullptr));
763 
764     if (iSurfaceEntries > MHW_MAX_SURFACE_PLANES)
765     {
766         RENDER_PACKET_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
767     }
768     if (static_cast<uint32_t>(iSurfaceEntries) <= capcityOfSurfaceEntries && surfaceEntries != nullptr)
769     {
770         for (int32_t i = 0; i < iSurfaceEntries; ++i)
771         {
772             surfaceEntries[i] = pSurfaceEntries[i];
773         }
774     }
775 
776     if (m_renderHal->isBindlessHeapInUse == false)
777     {
778         for (uint32_t const &bindingIndex : bindingIndexes)
779         {
780             uint32_t iBTEntry = bindingIndex;
781             // Bind surface states------------------------------------------------------
782             for (i = 0; i < iSurfaceEntries; i++, iBTEntry++)
783             {
784                 RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnBindSurfaceState(
785                     m_renderHal,
786                     m_renderData.bindingTable,
787                     iBTEntry,
788                     pSurfaceEntries[i]));
789 
790                 pRenderSurface->Index = iBTEntry;
791             }
792         }
793     }
794     else
795     {
796         for (i = 0; i < iSurfaceEntries; i++)
797         {
798             stateOffsets.insert(pSurfaceEntries[i]->dwSurfStateOffset);
799         }
800     }
801 
802     if (numOfSurfaceEntries)
803     {
804         *numOfSurfaceEntries = iSurfaceEntries;
805     }
806 
807     return eStatus;
808 }
809 
810 
SetBufferForHwAccess(PMOS_SURFACE buffer,PRENDERHAL_SURFACE_NEXT pRenderSurface,PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,bool bWrite,std::set<uint32_t> & stateOffsets)811 uint32_t RenderCmdPacket::SetBufferForHwAccess(
812     PMOS_SURFACE                    buffer,
813     PRENDERHAL_SURFACE_NEXT         pRenderSurface,
814     PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,
815     bool                            bWrite,
816     std::set<uint32_t>             &stateOffsets)
817 {
818     RENDERHAL_SURFACE              RenderHalSurface;
819     RENDERHAL_SURFACE_STATE_PARAMS SurfaceParam;
820     PRENDERHAL_SURFACE_STATE_ENTRY pSurfaceEntry;
821 
822     RENDER_PACKET_CHK_NULL_RETURN(m_osInterface);
823     RENDER_PACKET_CHK_NULL_RETURN(m_osInterface->osCpInterface);
824     RENDER_PACKET_CHK_NULL_RETURN(buffer);
825 
826     MOS_ZeroMemory(&RenderHalSurface, sizeof(RenderHalSurface));
827 
828     // Register surfaces for rendering (GfxAddress/Allocation index)
829     // Register resource
830     RENDER_PACKET_CHK_STATUS_RETURN(m_osInterface->pfnRegisterResource(
831         m_osInterface,
832         &buffer->OsResource,
833         bWrite,
834         true));
835 
836     // Setup Buffer surface-----------------------------------------------------
837     if (pSurfaceParams == nullptr)
838     {
839         MOS_ZeroMemory(&SurfaceParam, sizeof(SurfaceParam));
840 
841         auto memObjCtrlState = m_osInterface->pfnGetResourceCachePolicyMemoryObject(m_renderHal->pOsInterface, &buffer->OsResource);
842         SurfaceParam.MemObjCtl = memObjCtrlState.DwordValue;
843 
844         pSurfaceParams = &SurfaceParam;
845     }
846 
847     RENDER_PACKET_CHK_STATUS_RETURN(InitRenderHalSurface(
848         *buffer,
849         &RenderHalSurface));
850 
851     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetupBufferSurfaceState(
852         m_renderHal,
853         &RenderHalSurface,
854         pSurfaceParams,
855         &pSurfaceEntry));
856 
857     if (m_renderHal->isBindlessHeapInUse == false)
858     {
859         // Bind surface state-------------------------------------------------------
860         RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnBindSurfaceState(
861             m_renderHal,
862             m_renderData.bindingTable,
863             m_renderData.bindingTableEntry,
864             pSurfaceEntry));
865 
866         pRenderSurface->Index = m_renderData.bindingTableEntry;
867 
868         m_renderData.bindingTableEntry++;
869     }
870     else
871     {
872         stateOffsets.insert(pSurfaceEntry->dwSurfStateOffset);
873     }
874     return pRenderSurface->Index;
875 }
876 
SetBufferForHwAccess(PMOS_SURFACE buffer,PRENDERHAL_SURFACE_NEXT pRenderSurface,PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,uint32_t bindingIndex,bool bWrite)877 uint32_t RenderCmdPacket::SetBufferForHwAccess(PMOS_SURFACE buffer, PRENDERHAL_SURFACE_NEXT pRenderSurface, PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams, uint32_t bindingIndex, bool bWrite)
878 {
879     RENDERHAL_SURFACE              RenderHalSurface;
880     RENDERHAL_SURFACE_STATE_PARAMS SurfaceParam;
881     PRENDERHAL_SURFACE_STATE_ENTRY pSurfaceEntry;
882 
883     RENDER_PACKET_CHK_NULL_RETURN(m_osInterface);
884     RENDER_PACKET_CHK_NULL_RETURN(m_osInterface->osCpInterface);
885     RENDER_PACKET_CHK_NULL_RETURN(buffer);
886 
887     MOS_ZeroMemory(&RenderHalSurface, sizeof(RenderHalSurface));
888 
889     // Register surfaces for rendering (GfxAddress/Allocation index)
890     // Register resource
891     RENDER_PACKET_CHK_STATUS_RETURN(m_osInterface->pfnRegisterResource(
892         m_osInterface,
893         &buffer->OsResource,
894         bWrite,
895         true));
896 
897     // Setup Buffer surface-----------------------------------------------------
898     if (pSurfaceParams == nullptr)
899     {
900         MOS_ZeroMemory(&SurfaceParam, sizeof(SurfaceParam));
901 
902         //set mem object control for cache
903         SurfaceParam.MemObjCtl = (m_renderHal->pOsInterface->pfnCachePolicyGetMemoryObject(
904                                       MOS_HW_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER,
905                                       m_renderHal->pOsInterface->pfnGetGmmClientContext(m_renderHal->pOsInterface))).DwordValue;
906 
907         pSurfaceParams = &SurfaceParam;
908     }
909 
910     RENDER_PACKET_CHK_STATUS_RETURN(InitRenderHalSurface(
911         *buffer,
912         &RenderHalSurface));
913 
914     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetupBufferSurfaceState(
915         m_renderHal,
916         &RenderHalSurface,
917         pSurfaceParams,
918         &pSurfaceEntry));
919 
920     uint32_t iBTEntry = bindingIndex;
921     // Bind surface state-------------------------------------------------------
922     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnBindSurfaceState(
923         m_renderHal,
924         m_renderData.bindingTable,
925         iBTEntry,
926         pSurfaceEntry));
927 
928     pRenderSurface->Index = bindingIndex;
929 
930     return bindingIndex;
931 }
932 
SetBufferForHwAccess(PMOS_SURFACE buffer,PRENDERHAL_SURFACE_NEXT pRenderSurface,PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,std::set<uint32_t> & bindingIndexes,bool bWrite,std::set<uint32_t> & stateOffsets)933 MOS_STATUS RenderCmdPacket::SetBufferForHwAccess(
934     PMOS_SURFACE                    buffer,
935     PRENDERHAL_SURFACE_NEXT         pRenderSurface,
936     PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,
937     std::set<uint32_t>             &bindingIndexes,
938     bool                            bWrite,
939     std::set<uint32_t>             &stateOffsets)
940 {
941     RENDERHAL_SURFACE              RenderHalSurface = {};
942     RENDERHAL_SURFACE_STATE_PARAMS SurfaceParam     = {};
943     PRENDERHAL_SURFACE_STATE_ENTRY pSurfaceEntry    = {};
944     MOS_STATUS                     eStatus          = MOS_STATUS_SUCCESS;
945 
946     RENDER_PACKET_CHK_NULL_RETURN(m_osInterface);
947     RENDER_PACKET_CHK_NULL_RETURN(m_osInterface->osCpInterface);
948     RENDER_PACKET_CHK_NULL_RETURN(buffer);
949 
950     MOS_ZeroMemory(&RenderHalSurface, sizeof(RenderHalSurface));
951 
952     // Register surfaces for rendering (GfxAddress/Allocation index)
953     // Register resource
954     RENDER_PACKET_CHK_STATUS_RETURN(m_osInterface->pfnRegisterResource(
955         m_osInterface,
956         &buffer->OsResource,
957         bWrite,
958         true));
959 
960     // Setup Buffer surface-----------------------------------------------------
961     if (pSurfaceParams == nullptr)
962     {
963         MOS_ZeroMemory(&SurfaceParam, sizeof(SurfaceParam));
964 
965         //set mem object control for cache
966         SurfaceParam.MemObjCtl = (m_renderHal->pOsInterface->pfnCachePolicyGetMemoryObject(
967                                       MOS_HW_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER,
968                                       m_renderHal->pOsInterface->pfnGetGmmClientContext(m_renderHal->pOsInterface)))
969                                      .DwordValue;
970 
971         pSurfaceParams = &SurfaceParam;
972     }
973 
974     RENDER_PACKET_CHK_STATUS_RETURN(InitRenderHalSurface(
975         *buffer,
976         &RenderHalSurface));
977 
978     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetupBufferSurfaceState(
979         m_renderHal,
980         &RenderHalSurface,
981         pSurfaceParams,
982         &pSurfaceEntry));
983 
984     if (m_renderHal->isBindlessHeapInUse == false)
985     {
986         for (uint32_t const &bindingIndex : bindingIndexes)
987         {
988             uint32_t iBTEntry = bindingIndex;
989             // Bind surface state-------------------------------------------------------
990             RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnBindSurfaceState(
991                 m_renderHal,
992                 m_renderData.bindingTable,
993                 iBTEntry,
994                 pSurfaceEntry));
995 
996             pRenderSurface->Index = bindingIndex;
997         }
998     }
999     else
1000     {
1001         stateOffsets.insert(pSurfaceEntry->dwSurfStateOffset);
1002     }
1003 
1004     return eStatus;
1005 }
1006 
SetBufferForHwAccess(MOS_BUFFER buffer,PRENDERHAL_SURFACE_NEXT pRenderSurface,PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams,bool bWrite)1007 uint32_t RenderCmdPacket::SetBufferForHwAccess(MOS_BUFFER buffer, PRENDERHAL_SURFACE_NEXT pRenderSurface, PRENDERHAL_SURFACE_STATE_PARAMS pSurfaceParams, bool bWrite)
1008 {
1009     RENDERHAL_SURFACE              RenderHalSurface;
1010     RENDERHAL_SURFACE_STATE_PARAMS SurfaceParam;
1011     PRENDERHAL_SURFACE_STATE_ENTRY pSurfaceEntry;
1012 
1013     RENDER_PACKET_CHK_NULL_RETURN(m_osInterface);
1014     RENDER_PACKET_CHK_NULL_RETURN(m_osInterface->osCpInterface);
1015 
1016     MOS_ZeroMemory(&RenderHalSurface, sizeof(RenderHalSurface));
1017 
1018     // not support CP yet
1019     if (m_osInterface->osCpInterface->IsHMEnabled())
1020     {
1021         RENDER_PACKET_ASSERTMESSAGE("ERROR, need to use VpHal_CommonSetBufferSurfaceForHwAccess if under CP HM.");
1022     }
1023 
1024     // Register surfaces for rendering (GfxAddress/Allocation index)
1025     // Register resource
1026     RENDER_PACKET_CHK_STATUS_RETURN(m_osInterface->pfnRegisterResource(
1027         m_osInterface,
1028         &buffer.OsResource,
1029         bWrite,
1030         true));
1031 
1032     // Setup Buffer surface-----------------------------------------------------
1033     if (pSurfaceParams == nullptr)
1034     {
1035         MOS_ZeroMemory(&SurfaceParam, sizeof(SurfaceParam));
1036 
1037         //set mem object control for cache
1038         SurfaceParam.MemObjCtl = (m_renderHal->pOsInterface->pfnCachePolicyGetMemoryObject(
1039                                       MOS_HW_RESOURCE_USAGE_VP_INTERNAL_READ_WRITE_RENDER,
1040                                       m_renderHal->pOsInterface->pfnGetGmmClientContext(m_renderHal->pOsInterface))).DwordValue;
1041 
1042         pSurfaceParams = &SurfaceParam;
1043     }
1044 
1045     RENDER_PACKET_CHK_STATUS_RETURN(InitRenderHalBuffer(
1046         buffer,
1047         &RenderHalSurface));
1048 
1049     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetupBufferSurfaceState(
1050         m_renderHal,
1051         &RenderHalSurface,
1052         pSurfaceParams,
1053         &pSurfaceEntry));
1054 
1055     // Bind surface state-------------------------------------------------------
1056     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnBindSurfaceState(
1057         m_renderHal,
1058         m_renderData.bindingTable,
1059         m_renderData.bindingTableEntry,
1060         pSurfaceEntry));
1061 
1062     pRenderSurface->Index = m_renderData.bindingTableEntry;
1063 
1064     m_renderData.bindingTableEntry++;
1065     return pRenderSurface->Index;
1066 }
1067 
SetupCurbe(void * pData,uint32_t curbeLength,uint32_t maximumNumberofThreads)1068 MOS_STATUS RenderCmdPacket::SetupCurbe(void *pData, uint32_t curbeLength, uint32_t maximumNumberofThreads)
1069 {
1070     m_renderData.iCurbeOffset = m_renderHal->pfnLoadCurbeData(
1071         m_renderHal,
1072         m_renderData.mediaState,
1073         pData,
1074         curbeLength);
1075 
1076     if (m_renderData.iCurbeOffset < 0)
1077     {
1078         RENDER_PACKET_ASSERTMESSAGE("Curbe Set Fail, return error");
1079         return MOS_STATUS_UNKNOWN;
1080     }
1081 
1082     m_renderData.iCurbeLength = curbeLength;
1083 
1084     RENDER_PACKET_CHK_STATUS_RETURN(m_renderHal->pfnSetVfeStateParams(
1085         m_renderHal,
1086         MEDIASTATE_DEBUG_COUNTER_FREE_RUNNING,
1087         maximumNumberofThreads,
1088         m_renderData.iCurbeLength,
1089         m_renderData.iInlineLength,
1090         nullptr));
1091 
1092     return MOS_STATUS_SUCCESS;
1093 }
1094 
PrepareMediaWalkerParams(KERNEL_WALKER_PARAMS params,MHW_WALKER_PARAMS & mediaWalker)1095 MOS_STATUS RenderCmdPacket::PrepareMediaWalkerParams(KERNEL_WALKER_PARAMS params, MHW_WALKER_PARAMS &mediaWalker)
1096 {
1097     uint32_t uiMediaWalkerBlockSize;
1098     RECT     alignedRect      = {};
1099     bool     bVerticalPattern = false;
1100 
1101     uiMediaWalkerBlockSize = m_renderHal->pHwSizes->dwSizeMediaWalkerBlock;
1102     alignedRect            = params.alignedRect;
1103     bVerticalPattern       = params.isVerticalPattern;
1104 
1105     // Calculate aligned output area in order to determine the total # blocks
1106     // to process in case of non-16x16 aligned target.
1107     alignedRect.right += uiMediaWalkerBlockSize - 1;
1108     alignedRect.bottom += uiMediaWalkerBlockSize - 1;
1109     alignedRect.left -= alignedRect.left % uiMediaWalkerBlockSize;
1110     alignedRect.top -= alignedRect.top % uiMediaWalkerBlockSize;
1111     alignedRect.right -= alignedRect.right % uiMediaWalkerBlockSize;
1112     alignedRect.bottom -= alignedRect.bottom % uiMediaWalkerBlockSize;
1113 
1114     if (params.calculateBlockXYByAlignedRect)
1115     {
1116         // Set number of blocks
1117         params.iBlocksX = (alignedRect.right - alignedRect.left) / uiMediaWalkerBlockSize;
1118         params.iBlocksY = (alignedRect.bottom - alignedRect.top) / uiMediaWalkerBlockSize;
1119     }
1120 
1121     // Set walker cmd params - Rasterscan
1122     mediaWalker.InterfaceDescriptorOffset = params.iMediaID;
1123 
1124     mediaWalker.dwGlobalLoopExecCount = 1;
1125 
1126     if (uiMediaWalkerBlockSize == 32)
1127     {
1128         mediaWalker.ColorCountMinusOne = 3;
1129     }
1130     else
1131     {
1132         mediaWalker.ColorCountMinusOne = 0;
1133     }
1134 
1135     if (alignedRect.left != 0 || alignedRect.top != 0)
1136     {
1137         // if the rect starts from any other macro  block other than the first
1138         // then the global resolution should be the whole frame and the global
1139         // start should be the rect start.
1140         mediaWalker.GlobalResolution.x =
1141             (alignedRect.right / uiMediaWalkerBlockSize);
1142         mediaWalker.GlobalResolution.y =
1143             (alignedRect.bottom / uiMediaWalkerBlockSize);
1144     }
1145     else
1146     {
1147         mediaWalker.GlobalResolution.x = params.iBlocksX;
1148         mediaWalker.GlobalResolution.y = params.iBlocksY;
1149     }
1150 
1151     mediaWalker.GlobalStart.x =
1152         (alignedRect.left / uiMediaWalkerBlockSize);
1153     mediaWalker.GlobalStart.y =
1154         (alignedRect.top / uiMediaWalkerBlockSize);
1155 
1156     mediaWalker.GlobalOutlerLoopStride.x = params.iBlocksX;
1157     mediaWalker.GlobalOutlerLoopStride.y = 0;
1158 
1159     mediaWalker.GlobalInnerLoopUnit.x = 0;
1160     mediaWalker.GlobalInnerLoopUnit.y = params.iBlocksY;
1161 
1162     mediaWalker.BlockResolution.x = params.iBlocksX;
1163     mediaWalker.BlockResolution.y = params.iBlocksY;
1164 
1165     mediaWalker.LocalStart.x = 0;
1166     mediaWalker.LocalStart.y = 0;
1167 
1168     if (bVerticalPattern)
1169     {
1170         mediaWalker.LocalOutLoopStride.x = 1;
1171         mediaWalker.LocalOutLoopStride.y = 0;
1172 
1173         mediaWalker.LocalInnerLoopUnit.x = 0;
1174         mediaWalker.LocalInnerLoopUnit.y = 1;
1175 
1176         mediaWalker.dwLocalLoopExecCount = params.iBlocksX - 1;
1177         mediaWalker.LocalEnd.x           = 0;
1178         mediaWalker.LocalEnd.y           = params.iBlocksY - 1;
1179     }
1180     else
1181     {
1182         mediaWalker.LocalOutLoopStride.x = 0;
1183         mediaWalker.LocalOutLoopStride.y = 1;
1184 
1185         mediaWalker.LocalInnerLoopUnit.x = 1;
1186         mediaWalker.LocalInnerLoopUnit.y = 0;
1187 
1188         mediaWalker.dwLocalLoopExecCount = params.iBlocksY - 1;
1189         mediaWalker.LocalEnd.x           = params.iBlocksX - 1;
1190         mediaWalker.LocalEnd.y           = 0;
1191     }
1192 
1193     mediaWalker.UseScoreboard  = m_renderHal->VfeScoreboard.ScoreboardEnable;
1194     mediaWalker.ScoreboardMask = m_renderHal->VfeScoreboard.ScoreboardMask;
1195 
1196     return MOS_STATUS_SUCCESS;
1197 }
1198 
PrepareComputeWalkerParams(KERNEL_WALKER_PARAMS params,MHW_GPGPU_WALKER_PARAMS & gpgpuWalker)1199 MOS_STATUS RenderCmdPacket::PrepareComputeWalkerParams(KERNEL_WALKER_PARAMS params, MHW_GPGPU_WALKER_PARAMS &gpgpuWalker)
1200 {
1201     uint32_t uiMediaWalkerBlockSize;
1202     RECT     alignedRect = {};
1203     // Get media walker kernel block size
1204     uiMediaWalkerBlockSize = m_renderHal->pHwSizes->dwSizeMediaWalkerBlock;
1205     alignedRect            = params.alignedRect;
1206 
1207     // Calculate aligned output area in order to determine the total # blocks
1208     // to process in case of non-16x16 aligned target.
1209     alignedRect.right += uiMediaWalkerBlockSize - 1;
1210     alignedRect.bottom += uiMediaWalkerBlockSize - 1;
1211     alignedRect.left -= alignedRect.left % uiMediaWalkerBlockSize;
1212     alignedRect.top -= alignedRect.top % uiMediaWalkerBlockSize;
1213     alignedRect.right -= alignedRect.right % uiMediaWalkerBlockSize;
1214     alignedRect.bottom -= alignedRect.bottom % uiMediaWalkerBlockSize;
1215 
1216     if (params.calculateBlockXYByAlignedRect)
1217     {
1218         // Set number of blocks
1219         params.iBlocksX = (alignedRect.right - alignedRect.left) / uiMediaWalkerBlockSize;
1220         params.iBlocksY = (alignedRect.bottom - alignedRect.top) / uiMediaWalkerBlockSize;
1221     }
1222 
1223     // Set walker cmd params - Rasterscan
1224     gpgpuWalker.InterfaceDescriptorOffset = params.iMediaID;
1225 
1226     // Specifies the initial value of the X component of the thread group when walker is started.
1227     // During the walker operation, when X is incremented to the X Dimension limit, on the next step
1228     // it is re-loaded with theStarting Xvalue. Same as GroupStartingY.
1229     gpgpuWalker.GroupStartingX = (alignedRect.left / uiMediaWalkerBlockSize);
1230     gpgpuWalker.GroupStartingY = (alignedRect.top / uiMediaWalkerBlockSize);
1231     // The X dimension of the thread group (maximum X is dimension -1), same as GroupHeight.
1232     gpgpuWalker.GroupWidth  = params.iBlocksX;
1233     gpgpuWalker.GroupHeight = params.iBlocksY;
1234     if (params.isGroupStartInvolvedInGroupSize)
1235     {
1236         gpgpuWalker.GroupWidth += gpgpuWalker.GroupStartingX;
1237         gpgpuWalker.GroupHeight += gpgpuWalker.GroupStartingY;
1238     }
1239 
1240     if (params.threadDepth && params.threadWidth && params.threadHeight)
1241     {
1242         gpgpuWalker.ThreadWidth  = params.threadWidth;
1243         gpgpuWalker.ThreadHeight = params.threadHeight;
1244         gpgpuWalker.ThreadDepth  = params.threadDepth;
1245     }
1246     else
1247     {
1248         gpgpuWalker.ThreadWidth  = COMPUTE_WALKER_THREAD_SPACE_WIDTH;
1249         gpgpuWalker.ThreadHeight = COMPUTE_WALKER_THREAD_SPACE_HEIGHT;
1250         gpgpuWalker.ThreadDepth  = COMPUTE_WALKER_THREAD_SPACE_DEPTH;
1251     }
1252     gpgpuWalker.IndirectDataStartAddress = params.iCurbeOffset;
1253     // Indirect Data Length is a multiple of 64 bytes (size of L3 cacheline). Bits [5:0] are zero.
1254     gpgpuWalker.IndirectDataLength = MOS_ALIGN_CEIL(params.iCurbeLength, 1 << MHW_COMPUTE_INDIRECT_SHIFT);
1255     gpgpuWalker.BindingTableID     = params.iBindingTable;
1256     gpgpuWalker.ForcePreferredSLMZero = params.forcePreferredSLMZero;
1257 
1258     gpgpuWalker.isEmitInlineParameter = params.isEmitInlineParameter;
1259     gpgpuWalker.inlineDataLength      = params.inlineDataLength;
1260     gpgpuWalker.inlineData            = params.inlineData;
1261 
1262     gpgpuWalker.isGenerateLocalID = params.isGenerateLocalID;
1263     gpgpuWalker.emitLocal         = params.emitLocal;
1264 
1265     gpgpuWalker.SLMSize           = params.slmSize;
1266     gpgpuWalker.hasBarrier        = params.hasBarrier;
1267     gpgpuWalker.inlineDataParamBase   = params.inlineDataParamBase;
1268     gpgpuWalker.inlineDataParamSize = params.inlineDataParamSize;
1269     return MOS_STATUS_SUCCESS;
1270 }
1271 
UpdateKernelConfigParam(RENDERHAL_KERNEL_PARAM & kernelParam)1272 void RenderCmdPacket::UpdateKernelConfigParam(RENDERHAL_KERNEL_PARAM &kernelParam)
1273 {
1274     // CURBE_Length is set as the size of curbe buffer. 32 alignment with 5 bits right shift need be done before it being used.
1275     kernelParam.CURBE_Length = (kernelParam.CURBE_Length + 31) >> 5;
1276 }
1277 
LoadKernel()1278 MOS_STATUS RenderCmdPacket::LoadKernel()
1279 {
1280     int32_t                iKrnAllocation = 0;
1281     MHW_KERNEL_PARAM       MhwKernelParam = {};
1282     RENDERHAL_KERNEL_PARAM KernelParam    = m_renderData.KernelParam;
1283     // Load kernel to GSH
1284     INIT_MHW_KERNEL_PARAM(MhwKernelParam, &m_renderData.KernelEntry);
1285     UpdateKernelConfigParam(KernelParam);
1286     iKrnAllocation = m_renderHal->pfnLoadKernel(
1287         m_renderHal,
1288         &KernelParam,
1289         &MhwKernelParam,
1290         nullptr);
1291 
1292     if (iKrnAllocation < 0)
1293     {
1294         RENDER_PACKET_ASSERTMESSAGE("kernel load failed");
1295         return MOS_STATUS_UNKNOWN;
1296     }
1297 
1298     if (m_renderData.iCurbeOffset < 0)
1299     {
1300         RENDER_PACKET_ASSERTMESSAGE("Curbe Set Fail, return error");
1301         return MOS_STATUS_UNKNOWN;
1302     }
1303     // Allocate Media ID, link to kernel
1304     m_renderData.mediaID = m_renderHal->pfnAllocateMediaID(
1305         m_renderHal,
1306         iKrnAllocation,
1307         m_renderData.bindingTable,
1308         m_renderData.iCurbeOffset,
1309         (m_renderData.iCurbeLength),
1310         0,
1311         nullptr);
1312 
1313     if (m_renderData.mediaID < 0)
1314     {
1315         RENDER_PACKET_ASSERTMESSAGE("Allocate Media ID failed, return error");
1316         return MOS_STATUS_UNKNOWN;
1317     }
1318 
1319     return MOS_STATUS_SUCCESS;
1320 }
1321 
InitRenderHalSurface(MOS_SURFACE surface,PRENDERHAL_SURFACE pRenderSurface)1322 MOS_STATUS RenderCmdPacket::InitRenderHalSurface(MOS_SURFACE surface, PRENDERHAL_SURFACE pRenderSurface)
1323 {
1324     RENDER_PACKET_CHK_NULL_RETURN(pRenderSurface);
1325 
1326     RENDERHAL_GET_SURFACE_INFO info;
1327     MOS_ZeroMemory(&info, sizeof(info));
1328     RENDER_PACKET_CHK_STATUS_RETURN(RenderHal_GetSurfaceInfo(
1329         m_renderHal->pOsInterface,
1330         &info,
1331         &surface));
1332 
1333     if (Mos_ResourceIsNull(&pRenderSurface->OsSurface.OsResource))
1334     {
1335         pRenderSurface->OsSurface = surface;
1336     }
1337 
1338     return MOS_STATUS_SUCCESS;
1339 }
1340 
InitRenderHalBuffer(MOS_BUFFER surface,PRENDERHAL_SURFACE pRenderSurface)1341 MOS_STATUS RenderCmdPacket::InitRenderHalBuffer(MOS_BUFFER surface, PRENDERHAL_SURFACE pRenderSurface)
1342 {
1343     RENDER_PACKET_CHK_NULL_RETURN(pRenderSurface);
1344     pRenderSurface->OsSurface.OsResource = surface.OsResource;
1345     pRenderSurface->OsSurface.dwWidth    = surface.size;
1346     pRenderSurface->OsSurface.dwHeight   = 1;
1347     pRenderSurface->OsSurface.dwPitch    = surface.size;
1348     pRenderSurface->OsSurface.Format     = Format_RAW;
1349 
1350     return MOS_STATUS_SUCCESS;
1351 }