Debian Codegraph @

media_softlet/linux/common/os/mos_gpucontext_specific_next.cpp

raw
1 /*
2 * Copyright (c) 2019-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 mos_gpucontext_specific_next.cpp
24 //! \brief Container class for the Linux specific gpu context
25 //!
26
27 #include <unistd.h>
28 #include "mos_gpucontext_specific_next.h"
29 #include "mos_context_specific_next.h"
30 #include "mos_graphicsresource_specific_next.h"
31 #include "mos_commandbuffer_specific_next.h"
32 #include "mos_util_devult_specific_next.h"
33 #include "mos_cmdbufmgr_next.h"
34 #include "mos_os_virtualengine_next.h"
35 #include "mos_interface.h"
36 #include "mos_os_cp_interface_specific.h"
37 #include "mos_gpucontext_specific_next_xe.h"
38
39 #define MI_BATCHBUFFER_END 0x05000000
40 static pthread_mutex_t command_dump_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42 void GpuContextSpecificNext::StoreCreateOptions(PMOS_GPUCTX_CREATOPTIONS createoption)
43 {
44 if (typeid(*createoption) == typeid(MOS_GPUCTX_CREATOPTIONS_ENHANCED))
45 {
46 m_bEnhancedUsed = true;
47 MosUtilities::MosSecureMemcpy(&m_createOptionEnhanced, sizeof(MOS_GPUCTX_CREATOPTIONS_ENHANCED), createoption, sizeof(MOS_GPUCTX_CREATOPTIONS_ENHANCED));
48 }
49 else
50 {
51 MosUtilities::MosSecureMemcpy(&m_createOption, sizeof(MOS_GPUCTX_CREATOPTIONS), createoption, sizeof(MOS_GPUCTX_CREATOPTIONS));
52 }
53 }
54
55 GpuContextSpecificNext::GpuContextSpecificNext(
56 const MOS_GPU_NODE gpuNode,
57 CmdBufMgrNext *cmdBufMgr,
58 GpuContextNext *reusedContext)
59 {
60 MOS_OS_FUNCTION_ENTER;
61
62 m_nodeOrdinal = gpuNode;
63 m_cmdBufMgr = cmdBufMgr;
64 m_statusBufferResource = nullptr;
65 m_maxPatchLocationsize = PATCHLOCATIONLIST_SIZE;
66
67 if (reusedContext)
68 {
69 MOS_OS_NORMALMESSAGE("gpucontex reusing not enabled on Linux.");
70 }
71
72 #if (_DEBUG || _RELEASE_INTERNAL)
73 // get user engine instance setting from environment variable
74 char *engineInstances = getenv("INTEL_ENGINE_INSTANCE");
75 if (engineInstances != nullptr)
76 {
77 errno = 0;
78 long int instance = strtol(engineInstances, nullptr, 16);
79 /* Check for various possible errors. */
80 if ((errno == ERANGE && instance == LONG_MAX) || (instance < 0))
81 {
82 MOS_OS_NORMALMESSAGE("Invalid INTEL_ENGINE_INSTANCE setting.(%s)\n", engineInstances);
83 m_engineInstanceSelect = 0x0;
84 }
85 else
86 {
87 m_engineInstanceSelect = (uint32_t)instance;
88 }
89 }
90 #endif
91 }
92
93 GpuContextSpecificNext::~GpuContextSpecificNext()
94 {
95 MOS_OS_FUNCTION_ENTER;
96
97 Clear();
98 }
99
100 GpuContextNext *GpuContextSpecificNext::Create(
101 const MOS_GPU_NODE gpuNode,
102 CmdBufMgrNext *cmdBufMgr,
103 GpuContextNext *reusedContext)
104 {
105 MOS_OS_FUNCTION_ENTER;
106 if (nullptr == cmdBufMgr)
107 {
108 return nullptr;
109 }
110 OsContextSpecificNext *osDeviceContext = dynamic_cast<OsContextSpecificNext*>(cmdBufMgr->m_osContext);
111 if (nullptr == osDeviceContext)
112 {
113 return nullptr;
114 }
115 int type = osDeviceContext->GetDeviceType();
116 if (DEVICE_TYPE_I915 == type)
117 {
118 return MOS_New(GpuContextSpecificNext, gpuNode, cmdBufMgr, reusedContext);
119 }
120 else if (DEVICE_TYPE_XE == type)
121 {
122 return MOS_New(GpuContextSpecificNextXe, gpuNode, cmdBufMgr, reusedContext);
123 }
124
125 return nullptr;
126 }
127
128 MOS_STATUS GpuContextSpecificNext::RecreateContext(bool bIsProtected, MOS_STREAM_HANDLE streamState)
129 {
130 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
131 // clear existing context
132 Clear();
133 m_bProtectedContext = bIsProtected;
134 PMOS_GPUCTX_CREATOPTIONS createOption;
135 if (m_bEnhancedUsed)
136 {
137 createOption = &m_createOptionEnhanced;
138 }
139 else
140 {
141 createOption = &m_createOption;
142 }
143 eStatus = Init(m_osContext, streamState, createOption);
144 return eStatus;
145 }
146
147 MOS_STATUS GpuContextSpecificNext::PatchGPUContextProtection(MOS_STREAM_HANDLE streamState)
148 {
149 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
150 MOS_OS_CHK_NULL_RETURN(streamState);
151 auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
152 MOS_OS_CHK_NULL_RETURN(osParameters);
153
154 // clean up clear gem context and create protected gem context if CP is enabled
155 if (streamState->osCpInterface &&
156 streamState->osCpInterface->IsCpEnabled()) // Check if CP is enabled as protected GEM context is only needed when CP is enabled
157 {
158 if (streamState->ctxBasedScheduling)
159 {
160 if (m_bProtectedContext == false) // Check if GEM context is already protected or not
161 {
162 // Context is not protected, recreate it as protected
163 eStatus = RecreateContext(true, streamState);
164 if (eStatus == MOS_STATUS_SUCCESS)
165 {
166 //Register Protected Context
167 streamState->osCpInterface->RegisterAndCheckProtectedGemCtx(true, (void*)this, nullptr);
168 }
169 }
170 //If m_bProtectedContext == true then check if is stale context or not.
171 //If it is stale protected context then recreate another one
172 else
173 {
174 bool bIsContextStale = false;
175 //Check protected context
176 streamState->osCpInterface->RegisterAndCheckProtectedGemCtx(false, (void*)this, &bIsContextStale);
177
178 //Recreate protected context
179 if (bIsContextStale)
180 {
181 eStatus = RecreateContext(true, streamState);
182 if (eStatus == MOS_STATUS_SUCCESS)
183 {
184 //Register Protected Context
185 streamState->osCpInterface->RegisterAndCheckProtectedGemCtx(true, (void*)this, nullptr);
186 }
187 }
188 }
189 }
190 else
191 {
192 if (osParameters->m_protectedGEMContext == false)
193 {
194 // for non context based scheduling protected context is always created as protected during Initialization if needed
195 // If it is not created during Initialization then do nothing and add a comment for Debug purposes
196 MOS_OS_CRITICALMESSAGE("Using Clear GEM context when protected Context is needed");
197 eStatus = MOS_STATUS_SUCCESS;
198 }
199 }
200 }
201
202 // clean up protected gem context and recreate clear gem context if CP is disabled
203 if (streamState->osCpInterface &&
204 !streamState->osCpInterface->IsCpEnabled() &&
205 streamState->ctxBasedScheduling &&
206 m_bProtectedContext == true) // Check if GEM context is protected or not
207 {
208 // Context is protected, recreate it as clear
209 eStatus = RecreateContext(false, streamState);
210 }
211
212 return eStatus;
213 }
214
215 MOS_STATUS GpuContextSpecificNext::Init3DCtx(PMOS_CONTEXT osParameters,
216 PMOS_GPUCTX_CREATOPTIONS createOption,
217 unsigned int *nengine,
218 void *engine_map)
219 {
220 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
221
222 m_i915Context[0] = mos_context_create_shared(osParameters->bufmgr,
223 osParameters->intel_context,
224 I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE,
225 m_bProtectedContext,
226 engine_map,
227 1,
228 *nengine,
229 0);
230 if (m_i915Context[0] == nullptr)
231 {
232 MOS_OS_ASSERTMESSAGE("Failed to create context.\n");
233 return MOS_STATUS_UNKNOWN;
234 }
235 m_i915Context[0]->pOsContext = osParameters;
236
237 __u16 engine_class = I915_ENGINE_CLASS_RENDER;
238 __u64 caps = 0;
239
240 if (mos_query_engines(osParameters->bufmgr, engine_class, caps, nengine, engine_map))
241 {
242 MOS_OS_ASSERTMESSAGE("Failed to query engines.\n");
243 return MOS_STATUS_UNKNOWN;
244 }
245
246 if (mos_set_context_param_load_balance(m_i915Context[0], (struct i915_engine_class_instance *)engine_map, *nengine))
247 {
248 MOS_OS_ASSERTMESSAGE("Failed to set balancer extension.\n");
249 return MOS_STATUS_UNKNOWN;
250 }
251
252 if (createOption->SSEUValue != 0)
253 {
254 struct drm_i915_gem_context_param_sseu sseu;
255 MosUtilities::MosZeroMemory(&sseu, sizeof(sseu));
256 sseu.flags = I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX;
257 sseu.engine.engine_instance = m_i915ExecFlag;
258
259 if (mos_get_context_param_sseu(m_i915Context[0], &sseu))
260 {
261 MOS_OS_ASSERTMESSAGE("Failed to get sseu configuration.");
262 return MOS_STATUS_UNKNOWN;
263 }
264
265 if (mos_hweight8(m_i915Context[0], sseu.subslice_mask) > createOption->packed.SubSliceCount)
266 {
267 sseu.subslice_mask = mos_switch_off_n_bits(m_i915Context[0], sseu.subslice_mask,
268 mos_hweight8(m_i915Context[0], sseu.subslice_mask)-createOption->packed.SubSliceCount);
269 }
270
271 if (mos_set_context_param_sseu(m_i915Context[0], sseu))
272 {
273 MOS_OS_ASSERTMESSAGE("Failed to set sseu configuration.");
274 return MOS_STATUS_UNKNOWN;
275 }
276 }
277
278 return eStatus;
279 }
280
281 MOS_STATUS GpuContextSpecificNext::InitComputeCtx(PMOS_CONTEXT osParameters,
282 unsigned int *nengine,
283 void *engine_map,
284 MOS_GPU_NODE gpuNode,
285 bool *isEngineSelectEnable)
286 {
287 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
288
289 m_i915Context[0] = mos_context_create_shared(osParameters->bufmgr,
290 osParameters->intel_context,
291 I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE,
292 m_bProtectedContext,
293 engine_map,
294 1,
295 *nengine,
296 0);
297 if (m_i915Context[0] == nullptr)
298 {
299 MOS_OS_ASSERTMESSAGE("Failed to create context.\n");
300 return MOS_STATUS_UNKNOWN;
301 }
302 m_i915Context[0]->pOsContext = osParameters;
303
304 __u16 engine_class = 4; //To change later when linux define the name
305 __u64 caps = 0;
306
307 if (mos_query_engines(osParameters->bufmgr, engine_class, caps, nengine, engine_map))
308 {
309 MOS_OS_ASSERTMESSAGE("Failed to query engines.\n");
310 return MOS_STATUS_UNKNOWN;
311 }
312
313 #if (_DEBUG || _RELEASE_INTERNAL)
314 *isEngineSelectEnable = SelectEngineInstanceByUser(engine_map, nengine, m_engineInstanceSelect, gpuNode);
315 #endif
316 if (mos_set_context_param_load_balance(m_i915Context[0], (struct i915_engine_class_instance *)engine_map, *nengine))
317 {
318 MOS_OS_ASSERTMESSAGE("Failed to set balancer extension.\n");
319 return MOS_STATUS_UNKNOWN;
320 }
321
322 return eStatus;
323 }
324
325 MOS_STATUS GpuContextSpecificNext::InitVdVeCtx(PMOS_CONTEXT osParameters,
326 MOS_STREAM_HANDLE streamState,
327 PMOS_GPUCTX_CREATOPTIONS createOption,
328 unsigned int *nengine,
329 void *engine_map,
330 MOS_GPU_NODE gpuNode,
331 bool *isEngineSelectEnable)
332 {
333 MOS_OS_FUNCTION_ENTER;
334
335 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
336
337 m_i915Context[0] = mos_context_create_shared(osParameters->bufmgr,
338 osParameters->intel_context,
339 I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE,
340 m_bProtectedContext,
341 engine_map,
342 1,
343 *nengine,
344 0);
345 if (m_i915Context[0] == nullptr)
346 {
347 MOS_OS_ASSERTMESSAGE("Failed to create context.\n");
348 return MOS_STATUS_UNKNOWN;
349 }
350 struct i915_engine_class_instance *_engine_map = (struct i915_engine_class_instance *)engine_map;
351 m_i915Context[0]->pOsContext = osParameters;
352
353 __u16 engine_class = (gpuNode == MOS_GPU_NODE_VE)? I915_ENGINE_CLASS_VIDEO_ENHANCE : I915_ENGINE_CLASS_VIDEO;
354 __u64 caps = 0;
355
356 SetEngineQueryFlags(createOption, caps);
357
358 if (mos_query_engines(osParameters->bufmgr, engine_class, caps, nengine, (void *)_engine_map))
359 {
360 MOS_OS_ASSERTMESSAGE("Failed to query engines.\n");
361 return MOS_STATUS_UNKNOWN;
362 }
363
364 #if (_DEBUG || _RELEASE_INTERNAL)
365 *isEngineSelectEnable = SelectEngineInstanceByUser((void *)_engine_map, nengine, m_engineInstanceSelect, gpuNode);
366 #endif
367 if (mos_set_context_param_load_balance(m_i915Context[0], _engine_map, *nengine))
368 {
369 MOS_OS_ASSERTMESSAGE("Failed to set balancer extension.\n");
370 return MOS_STATUS_UNKNOWN;
371 }
372
373 if (*nengine >= 2 && *nengine <= MAX_ENGINE_INSTANCE_NUM)
374 {
375 int i;
376 //master queue
377 m_i915Context[1] = mos_context_create_shared(osParameters->bufmgr,
378 osParameters->intel_context,
379 I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE,
380 m_bProtectedContext,
381 (void *)_engine_map,
382 1,
383 1,
384 0);
385 if (m_i915Context[1] == nullptr)
386 {
387 MOS_OS_ASSERTMESSAGE("Failed to create master context.\n");
388 return MOS_STATUS_UNKNOWN;
389 }
390 m_i915Context[1]->pOsContext = osParameters;
391
392 if (mos_set_context_param_load_balance(m_i915Context[1], _engine_map, 1))
393 {
394 MOS_OS_ASSERTMESSAGE("Failed to set master context bond extension.\n");
395 return MOS_STATUS_UNKNOWN;
396 }
397
398 //slave queue
399 for (i=1; i < *nengine; i++)
400 {
401 m_i915Context[i+1] = mos_context_create_shared(osParameters->bufmgr,
402 osParameters->intel_context,
403 I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE,
404 m_bProtectedContext,
405 (void *)_engine_map,
406 1,
407 1,
408 0);
409 if (m_i915Context[i+1] == nullptr)
410 {
411 MOS_OS_ASSERTMESSAGE("Failed to create slave context.\n");
412 return MOS_STATUS_UNKNOWN;
413 }
414 m_i915Context[i+1]->pOsContext = osParameters;
415
416 if (mos_set_context_param_bond(m_i915Context[i+1], _engine_map[0], &_engine_map[i], 1) != S_SUCCESS)
417 {
418 int err = errno;
419 if (err == ENODEV)
420 {
421 mos_context_destroy(m_i915Context[1]);
422 mos_context_destroy(m_i915Context[i+1]);
423 m_i915Context[i+1] = nullptr;
424 break;
425 }
426 else
427 {
428 MOS_OS_ASSERTMESSAGE("Failed to set slave context bond extension. errno=%d\n",err);
429 return MOS_STATUS_UNKNOWN;
430 }
431 }
432 }
433 if (i == *nengine)
434 {
435 streamState->bParallelSubmission = false;
436 }
437 else
438 {
439 streamState->bParallelSubmission = true;
440 //create context with different width
441 for(i = 1; i < *nengine; i++)
442 {
443 unsigned int ctxWidth = i + 1;
444 m_i915Context[i] = mos_context_create_shared(osParameters->bufmgr,
445 osParameters->intel_context,
446 0, // I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE not allowed for parallel submission
447 m_bProtectedContext,
448 (void *)_engine_map,
449 ctxWidth,
450 1,
451 0);
452 if (mos_set_context_param_parallel(m_i915Context[i], _engine_map, ctxWidth) != S_SUCCESS)
453 {
454 MOS_OS_ASSERTMESSAGE("Failed to set parallel extension since discontinuous logical engine.\n");
455 mos_context_destroy(m_i915Context[i]);
456 m_i915Context[i] = nullptr;
457 break;
458 }
459 }
460 }
461 }
462
463 return eStatus;
464 }
465
466 MOS_STATUS GpuContextSpecificNext::InitBltCtx(PMOS_CONTEXT osParameters,
467 unsigned int *nengine,
468 void *engine_map)
469 {
470 MOS_OS_FUNCTION_ENTER;
471
472 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
473
474 m_i915Context[0] = mos_context_create_shared(osParameters->bufmgr,
475 osParameters->intel_context,
476 I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE,
477 m_bProtectedContext,
478 engine_map,
479 1,
480 *nengine,
481 0);
482 if (m_i915Context[0] == nullptr)
483 {
484 MOS_OS_ASSERTMESSAGE("Failed to create context.\n");
485 return MOS_STATUS_UNKNOWN;
486 }
487 m_i915Context[0]->pOsContext = osParameters;
488
489 __u16 engine_class = I915_ENGINE_CLASS_COPY;
490 __u64 caps = 0;
491
492 if (mos_query_engines(osParameters->bufmgr, engine_class, caps, nengine, engine_map))
493 {
494 MOS_OS_ASSERTMESSAGE("Failed to query engines.\n");
495 return MOS_STATUS_UNKNOWN;
496 }
497
498 if (mos_set_context_param_load_balance(m_i915Context[0], (struct i915_engine_class_instance *)engine_map, *nengine))
499 {
500 MOS_OS_ASSERTMESSAGE("Failed to set balancer extension.\n");
501 return MOS_STATUS_UNKNOWN;
502 }
503
504 return eStatus;
505 }
506
507
508 MOS_STATUS GpuContextSpecificNext::Init(OsContextNext *osContext,
509 MOS_STREAM_HANDLE streamState,
510 PMOS_GPUCTX_CREATOPTIONS createOption)
511 {
512 MOS_OS_FUNCTION_ENTER;
513
514 MOS_OS_CHK_NULL_RETURN(osContext);
515 MOS_OS_CHK_NULL_RETURN(streamState);
516 MOS_OS_CHK_NULL_RETURN(createOption);
517
518 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
519
520 auto osParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
521 MOS_OS_CHK_NULL_RETURN(osParameters);
522
523 m_osParameters = osParameters;
524
525 MOS_GPU_NODE gpuNode = MOS_GPU_NODE_3D;
526 gpuNode = static_cast<MOS_GPU_NODE>(createOption->gpuNode);
527
528 if (m_cmdBufPoolMutex == nullptr)
529 {
530 m_cmdBufPoolMutex = MosUtilities::MosCreateMutex();
531 }
532
533 MOS_OS_CHK_NULL_RETURN(m_cmdBufPoolMutex);
534
535 MosUtilities::MosLockMutex(m_cmdBufPoolMutex);
536
537 m_cmdBufPool.clear();
538
539 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
540
541 m_ocaLogSectionSupported = osContext->m_ocaLogSectionSupported;
542 if (m_ocaLogSectionSupported)
543 {
544 // increase size for oca log section
545 m_commandBufferSize = MosOcaInterfaceSpecific::IncreaseSize(COMMAND_BUFFER_SIZE);
546 }
547 else
548 {
549 m_commandBufferSize = COMMAND_BUFFER_SIZE;
550 }
551
552 m_nextFetchIndex = 0;
553
554 m_cmdBufFlushed = true;
555
556 m_osContext = osContext;
557
558 MOS_OS_CHK_STATUS_RETURN(AllocateGPUStatusBuf());
559
560 m_commandBuffer = (PMOS_COMMAND_BUFFER)MOS_AllocAndZeroMemory(sizeof(MOS_COMMAND_BUFFER));
561
562 MOS_OS_CHK_NULL_RETURN(m_commandBuffer);
563
564 m_IndirectHeapSize = 0;
565
566 // each thread has its own GPU context, so do not need any lock as guarder here
567 m_allocationList = (ALLOCATION_LIST *)MOS_AllocAndZeroMemory(sizeof(ALLOCATION_LIST) * ALLOCATIONLIST_SIZE);
568 MOS_OS_CHK_NULL_RETURN(m_allocationList);
569 m_maxNumAllocations = ALLOCATIONLIST_SIZE;
570
571 m_patchLocationList = (PATCHLOCATIONLIST *)MOS_AllocAndZeroMemory(sizeof(PATCHLOCATIONLIST) * PATCHLOCATIONLIST_SIZE);
572 MOS_OS_CHK_NULL_RETURN(m_patchLocationList);
573 m_maxPatchLocationsize = PATCHLOCATIONLIST_SIZE;
574
575 m_attachedResources = (PMOS_RESOURCE)MOS_AllocAndZeroMemory(sizeof(MOS_RESOURCE) * ALLOCATIONLIST_SIZE);
576 MOS_OS_CHK_NULL_RETURN(m_attachedResources);
577
578 m_writeModeList = (bool *)MOS_AllocAndZeroMemory(sizeof(bool) * ALLOCATIONLIST_SIZE);
579 MOS_OS_CHK_NULL_RETURN(m_writeModeList);
580
581 m_GPUStatusTag = 1;
582
583 StoreCreateOptions(createOption);
584
585 for (int i=0; i<MAX_ENGINE_INSTANCE_NUM+1; i++)
586 {
587 m_i915Context[i] = nullptr;
588 }
589
590 if (streamState->ctxBasedScheduling)
591 {
592 bool isEngineSelectEnable = false;
593 unsigned int nengine = 0;
594 size_t engine_class_size = 0;
595 void *engine_map = nullptr;
596
597 MOS_TraceEventExt(EVENT_GPU_CONTEXT_CREATE, EVENT_TYPE_START,
598 &gpuNode, sizeof(gpuNode), nullptr, 0);
599
600 m_i915ExecFlag = I915_EXEC_DEFAULT;
601
602 if (mos_query_engines_count(osParameters->bufmgr, &nengine))
603 {
604 MOS_OS_ASSERTMESSAGE("Failed to query engines count.\n");
605 return MOS_STATUS_UNKNOWN;
606 }
607 engine_class_size = mos_get_engine_class_size(osParameters->bufmgr);
608 if (!engine_class_size)
609 {
610 MOS_OS_ASSERTMESSAGE("Failed to get engine class instance size.\n");
611 return MOS_STATUS_UNKNOWN;
612 }
613 engine_map = MOS_AllocAndZeroMemory(nengine * engine_class_size);
614 MOS_OS_CHK_NULL_RETURN(engine_map);
615
616 if (gpuNode == MOS_GPU_NODE_3D)
617 {
618 eStatus = Init3DCtx(osParameters, createOption, &nengine, engine_map);
619 }
620 else if (gpuNode == MOS_GPU_NODE_COMPUTE)
621 {
622 eStatus = InitComputeCtx(osParameters, &nengine, engine_map, gpuNode, &isEngineSelectEnable);
623 }
624 else if (gpuNode == MOS_GPU_NODE_VIDEO || gpuNode == MOS_GPU_NODE_VIDEO2
625 || gpuNode == MOS_GPU_NODE_VE)
626 {
627 eStatus = InitVdVeCtx(osParameters, streamState, createOption, &nengine, engine_map, gpuNode, &isEngineSelectEnable);
628 }
629 else if (gpuNode == MOS_GPU_NODE_BLT)
630 {
631 eStatus = InitBltCtx(osParameters, &nengine, engine_map);
632 }
633 else
634 {
635 MOS_OS_ASSERTMESSAGE("Unknown engine class.\n");
636 MOS_SafeFreeMemory(engine_map);
637 return MOS_STATUS_UNKNOWN;
638 }
639
640 if (eStatus == MOS_STATUS_SUCCESS)
641 {
642 MOS_OS_CHK_STATUS_RETURN(ReportEngineInfo(engine_map, nengine, isEngineSelectEnable));
643 }
644 MOS_SafeFreeMemory(engine_map);
645 MOS_TraceEventExt(EVENT_GPU_CONTEXT_CREATE, EVENT_TYPE_END,
646 m_i915Context, sizeof(void *),
647 &nengine, sizeof(nengine));
648 }
649
650 return eStatus;
651 }
652
653 void GpuContextSpecificNext::Clear()
654 {
655 MOS_OS_FUNCTION_ENTER;
656
657 MOS_TraceEventExt(EVENT_GPU_CONTEXT_DESTROY, EVENT_TYPE_START,
658 m_i915Context, sizeof(void *), nullptr, 0);
659 // hanlde the status buf bundled w/ the specified gpucontext
660 if (m_statusBufferResource && m_statusBufferResource->pGfxResourceNext)
661 {
662 if (m_statusBufferResource->pGfxResourceNext->Unlock(m_osContext) != MOS_STATUS_SUCCESS)
663 {
664 MOS_OS_ASSERTMESSAGE("failed to unlock the status buf bundled w/ the specified gpucontext");
665 }
666 m_statusBufferResource->pGfxResourceNext->Free(m_osContext, 0);
667 MOS_Delete(m_statusBufferResource->pGfxResourceNext);
668 }
669 MOS_FreeMemAndSetNull(m_statusBufferResource);
670
671 if(m_cmdBufPoolMutex)
672 {
673 MosUtilities::MosLockMutex(m_cmdBufPoolMutex);
674
675 if (m_cmdBufMgr)
676 {
677 for (auto& curCommandBuffer : m_cmdBufPool)
678 {
679 auto curCommandBufferSpecific = static_cast<CommandBufferSpecificNext *>(curCommandBuffer);
680 if (curCommandBufferSpecific == nullptr)
681 continue;
682 curCommandBufferSpecific->waitReady(); // wait ready and return to comamnd buffer manager.
683 m_cmdBufMgr->ReleaseCmdBuf(curCommandBuffer);
684 }
685 }
686
687 m_cmdBufPool.clear();
688
689 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
690 MosUtilities::MosDestroyMutex(m_cmdBufPoolMutex);
691 m_cmdBufPoolMutex = nullptr;
692 }
693
694 MOS_SafeFreeMemory(m_commandBuffer);
695 m_commandBuffer = nullptr;
696 MOS_SafeFreeMemory(m_allocationList);
697 m_allocationList = nullptr;
698 MOS_SafeFreeMemory(m_patchLocationList);
699 m_patchLocationList = nullptr;
700 MOS_SafeFreeMemory(m_attachedResources);
701 m_attachedResources = nullptr;
702 MOS_SafeFreeMemory(m_writeModeList);
703 m_writeModeList = nullptr;
704
705 for (int i=0; i<MAX_ENGINE_INSTANCE_NUM; i++)
706 {
707 if (m_i915Context[i])
708 {
709 mos_context_destroy(m_i915Context[i]);
710 m_i915Context[i] = nullptr;
711 }
712 }
713 MOS_TraceEventExt(EVENT_GPU_CONTEXT_DESTROY, EVENT_TYPE_END,
714 nullptr, 0, nullptr, 0);
715 }
716
717 MOS_STATUS GpuContextSpecificNext::RegisterResource(
718 PMOS_RESOURCE osResource,
719 bool writeFlag)
720 {
721 MOS_OS_FUNCTION_ENTER;
722
723 MOS_OS_CHK_NULL_RETURN(osResource);
724
725 MOS_OS_CHK_NULL_RETURN(m_attachedResources);
726
727 PMOS_RESOURCE registeredResources = m_attachedResources;
728 uint32_t allocationIndex = 0;
729
730 for ( allocationIndex = 0; allocationIndex < m_resCount; allocationIndex++, registeredResources++)
731 {
732 if (osResource->bo == registeredResources->bo)
733 {
734 break;
735 }
736 }
737
738 // Allocation list to be updated
739 if (allocationIndex < m_maxNumAllocations)
740 {
741 // New buffer
742 if (allocationIndex == m_resCount)
743 {
744 m_resCount++;
745 }
746
747 // Set allocation
748 if (m_gpuContext >= MOS_GPU_CONTEXT_MAX)
749 {
750 MOS_OS_ASSERTMESSAGE("Gpu context exceeds max.");
751 return MOS_STATUS_UNKNOWN;
752 }
753
754 osResource->iAllocationIndex[m_gpuContext] = (allocationIndex);
755 m_attachedResources[allocationIndex] = *osResource;
756 m_writeModeList[allocationIndex] |= writeFlag;
757 m_allocationList[allocationIndex].hAllocation = &m_attachedResources[allocationIndex];
758 m_allocationList[allocationIndex].WriteOperation |= writeFlag;
759 m_numAllocations = m_resCount;
760 }
761 else
762 {
763 MOS_OS_ASSERTMESSAGE("Reached max # registrations.");
764 return MOS_STATUS_UNKNOWN;
765 }
766
767 return MOS_STATUS_SUCCESS;
768 }
769
770 MOS_STATUS GpuContextSpecificNext::SetPatchEntry(
771 MOS_STREAM_HANDLE streamState,
772 PMOS_PATCH_ENTRY_PARAMS params)
773 {
774 MOS_OS_FUNCTION_ENTER;
775
776 MOS_OS_CHK_NULL_RETURN(m_patchLocationList);
777 MOS_OS_CHK_NULL_RETURN(streamState);
778 MOS_OS_CHK_NULL_RETURN(params);
779
780 m_patchLocationList[m_currentNumPatchLocations].AllocationIndex = params->uiAllocationIndex;
781 m_patchLocationList[m_currentNumPatchLocations].AllocationOffset = params->uiResourceOffset;
782 m_patchLocationList[m_currentNumPatchLocations].PatchOffset = params->uiPatchOffset;
783 m_patchLocationList[m_currentNumPatchLocations].uiWriteOperation = params->bWrite ? true: false;
784 m_patchLocationList[m_currentNumPatchLocations].cmdBo =
785 params->cmdBuffer != nullptr ? params->cmdBuffer->OsResource.bo : nullptr;
786
787 if (streamState->osCpInterface &&
788 streamState->osCpInterface->IsHMEnabled())
789 {
790 if (MOS_STATUS_SUCCESS != streamState->osCpInterface->RegisterPatchForHM(
791 (uint32_t *)(params->cmdBufBase + params->uiPatchOffset),
792 params->bWrite,
793 params->HwCommandType,
794 params->forceDwordOffset,
795 params->presResource,
796 &m_patchLocationList[m_currentNumPatchLocations]))
797 {
798 MOS_OS_ASSERTMESSAGE("Failed to RegisterPatchForHM.");
799 }
800 }
801
802 m_currentNumPatchLocations++;
803
804 return MOS_STATUS_SUCCESS;
805 }
806
807 MOS_STATUS GpuContextSpecificNext::GetCommandBuffer(
808 PMOS_COMMAND_BUFFER comamndBuffer,
809 uint32_t flags)
810 {
811 MOS_OS_FUNCTION_ENTER;
812
813 MOS_OS_CHK_NULL_RETURN(comamndBuffer);
814 MOS_OS_CHK_NULL_RETURN(m_cmdBufMgr);
815 MOS_OS_CHK_NULL_RETURN(m_commandBuffer);
816
817 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
818 CommandBufferNext* cmdBuf = nullptr;
819
820 uint32_t secondaryIdx = flags;
821 bool isPrimaryCmdBuffer = (secondaryIdx == 0);
822 bool hasSecondaryCmdBuffer = (!isPrimaryCmdBuffer &&
823 (m_secondaryCmdBufs.count(secondaryIdx) != 0));
824
825 bool needToAlloc = ((isPrimaryCmdBuffer && m_cmdBufFlushed) ||
826 (!isPrimaryCmdBuffer && !hasSecondaryCmdBuffer));
827
828 if (needToAlloc)
829 {
830 MosUtilities::MosLockMutex(m_cmdBufPoolMutex);
831 if (m_cmdBufPool.size() < MAX_CMD_BUF_NUM)
832 {
833 cmdBuf = m_cmdBufMgr->PickupOneCmdBuf(m_commandBufferSize);
834 if (cmdBuf == nullptr)
835 {
836 MOS_OS_ASSERTMESSAGE("Invalid (nullptr) Pointer.");
837 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
838 return MOS_STATUS_NULL_POINTER;
839 }
840 if ((eStatus = cmdBuf->BindToGpuContext(this)) != MOS_STATUS_SUCCESS)
841 {
842 MOS_OS_ASSERTMESSAGE("Invalid status of BindToGpuContext.");
843 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
844 return eStatus;
845 }
846 m_cmdBufPool.push_back(cmdBuf);
847 }
848 else if (m_cmdBufPool.size() == MAX_CMD_BUF_NUM && m_nextFetchIndex < m_cmdBufPool.size())
849 {
850 auto cmdBufOld = m_cmdBufPool[m_nextFetchIndex];
851 auto cmdBufSpecificOld = static_cast<CommandBufferSpecificNext *>(cmdBufOld);
852 if (cmdBufSpecificOld == nullptr)
853 {
854 MOS_OS_ASSERTMESSAGE("Invalid (nullptr) Pointer.");
855 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
856 return MOS_STATUS_NULL_POINTER;
857 }
858 cmdBufSpecificOld->waitReady();
859 cmdBufSpecificOld->UnBindToGpuContext();
860 m_cmdBufMgr->ReleaseCmdBuf(cmdBufOld); // here just return old command buffer to available pool
861
862 //pick up new comamnd buffer
863 cmdBuf = m_cmdBufMgr->PickupOneCmdBuf(m_commandBufferSize);
864 if (cmdBuf == nullptr)
865 {
866 MOS_OS_ASSERTMESSAGE("Invalid (nullptr) Pointer.");
867 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
868 return MOS_STATUS_NULL_POINTER;
869 }
870 if ((eStatus = cmdBuf->BindToGpuContext(this)) != MOS_STATUS_SUCCESS)
871 {
872 MOS_OS_ASSERTMESSAGE("Invalid status of BindToGpuContext.");
873 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
874 return eStatus;
875 }
876 m_cmdBufPool[m_nextFetchIndex] = cmdBuf;
877 }
878 else
879 {
880 MOS_OS_ASSERTMESSAGE("Command buffer bool size exceed max.");
881 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
882 return MOS_STATUS_UNKNOWN;
883 }
884 MosUtilities::MosUnlockMutex(m_cmdBufPoolMutex);
885
886 // util now, we got new command buffer from CmdBufMgr, next step to fill in the input command buffer
887 MOS_OS_CHK_STATUS_RETURN(cmdBuf->GetResource()->ConvertToMosResource(&comamndBuffer->OsResource));
888 comamndBuffer->pCmdBase = (uint32_t *)cmdBuf->GetResource()->GetLockedAddr();
889 comamndBuffer->pCmdPtr = (uint32_t *)cmdBuf->GetResource()->GetLockedAddr();
890 comamndBuffer->iOffset = 0;
891 comamndBuffer->iRemaining = cmdBuf->GetCmdBufSize();
892 comamndBuffer->iCmdIndex = m_nextFetchIndex;
893 comamndBuffer->iVdboxNodeIndex = MOS_VDBOX_NODE_INVALID;
894 comamndBuffer->iVeboxNodeIndex = MOS_VEBOX_NODE_INVALID;
895 comamndBuffer->Attributes.pAttriVe = nullptr;
896 comamndBuffer->is1stLvlBB = true;
897
898 // zero comamnd buffer
899 MosUtilities::MosZeroMemory(comamndBuffer->pCmdBase, comamndBuffer->iRemaining);
900 comamndBuffer->iSubmissionType = SUBMISSION_TYPE_SINGLE_PIPE;
901 MosUtilities::MosZeroMemory(&comamndBuffer->Attributes,sizeof(comamndBuffer->Attributes));
902
903 if (isPrimaryCmdBuffer)
904 {
905 // update command buffer relared filed in GPU context
906 m_cmdBufFlushed = false;
907
908 // keep a copy in GPU context
909 MosUtilities::MosSecureMemcpy(m_commandBuffer, sizeof(MOS_COMMAND_BUFFER), comamndBuffer, sizeof(MOS_COMMAND_BUFFER));
910 }
911 else
912 {
913 PMOS_COMMAND_BUFFER tempCmdBuf = (PMOS_COMMAND_BUFFER)MOS_AllocAndZeroMemory(sizeof(MOS_COMMAND_BUFFER));
914 MOS_OS_CHK_NULL_RETURN(tempCmdBuf);
915 m_secondaryCmdBufs[secondaryIdx] = tempCmdBuf;
916 MosUtilities::MosSecureMemcpy(tempCmdBuf, sizeof(MOS_COMMAND_BUFFER), comamndBuffer, sizeof(MOS_COMMAND_BUFFER));
917 }
918
919 if (m_ocaLogSectionSupported)
920 {
921 MOS_LINUX_BO *boTemp = ((GraphicsResourceSpecificNext *)cmdBuf->GetResource())->GetBufferObject();
922 MosOcaInterfaceSpecific::InitOcaLogSection(boTemp);
923 }
924
925 // Command buffers are treated as cyclical buffers, the CB after the just submitted one
926 // has the minimal fence value that we should wait
927 m_nextFetchIndex++;
928 if (m_nextFetchIndex >= MAX_CMD_BUF_NUM)
929 {
930 m_nextFetchIndex = 0;
931 }
932 }
933 else
934 {
935 // current command buffer still active, directly copy to comamndBuffer
936 if (isPrimaryCmdBuffer)
937 {
938 MosUtilities::MosSecureMemcpy(comamndBuffer, sizeof(MOS_COMMAND_BUFFER), m_commandBuffer, sizeof(MOS_COMMAND_BUFFER));
939 }
940 else
941 {
942 MosUtilities::MosSecureMemcpy(comamndBuffer, sizeof(MOS_COMMAND_BUFFER), m_secondaryCmdBufs[secondaryIdx], sizeof(MOS_COMMAND_BUFFER));
943 }
944 }
945
946 if (isPrimaryCmdBuffer)
947 {
948 MOS_OS_CHK_STATUS_RETURN(RegisterResource(&m_commandBuffer->OsResource, false));
949 }
950 else
951 {
952 MOS_OS_CHK_STATUS_RETURN(RegisterResource(&m_secondaryCmdBufs[secondaryIdx]->OsResource, false));
953 }
954
955 return MOS_STATUS_SUCCESS;
956 }
957
958 void GpuContextSpecificNext::ReturnCommandBuffer(
959 PMOS_COMMAND_BUFFER cmdBuffer,
960 uint32_t flags)
961 {
962 MOS_OS_FUNCTION_ENTER;
963
964 MOS_OS_ASSERT(cmdBuffer);
965 MOS_OS_ASSERT(m_commandBuffer);
966
967 bool isPrimaryCmdBuf = (flags == 0);
968
969 if (isPrimaryCmdBuf)
970 {
971 m_commandBuffer->iOffset = cmdBuffer->iOffset;
972 m_commandBuffer->iRemaining = cmdBuffer->iRemaining;
973 m_commandBuffer->pCmdPtr = cmdBuffer->pCmdPtr;
974 m_commandBuffer->iVdboxNodeIndex = cmdBuffer->iVdboxNodeIndex;
975 m_commandBuffer->iVeboxNodeIndex = cmdBuffer->iVeboxNodeIndex;
976 }
977 else
978 {
979 uint32_t secondaryIdx = flags;
980 MOS_OS_ASSERT(m_secondaryCmdBufs.count(secondaryIdx));
981
982 MosUtilities::MosSecureMemcpy(m_secondaryCmdBufs[secondaryIdx], sizeof(MOS_COMMAND_BUFFER), cmdBuffer, sizeof(MOS_COMMAND_BUFFER));
983 }
984 }
985
986 MOS_STATUS GpuContextSpecificNext::ResetCommandBuffer()
987 {
988 m_cmdBufFlushed = true;
989 auto it = m_secondaryCmdBufs.begin();
990 while(it != m_secondaryCmdBufs.end())
991 {
992 MOS_FreeMemory(it->second);
993 it++;
994 }
995 m_secondaryCmdBufs.clear();
996 return MOS_STATUS_SUCCESS;
997 }
998
999 MOS_STATUS GpuContextSpecificNext::SetIndirectStateSize(const uint32_t size)
1000 {
1001 if (m_ocaLogSectionSupported)
1002 {
1003 if(MosOcaInterfaceSpecific::IncreaseSize(size) < m_commandBufferSize)
1004 {
1005 m_IndirectHeapSize = size;
1006 return MOS_STATUS_SUCCESS;
1007 }
1008 else
1009 {
1010 MOS_OS_ASSERTMESSAGE("Indirect State Size if out of boundry!");
1011 return MOS_STATUS_UNKNOWN;
1012 }
1013 }
1014 else
1015 {
1016 if(size < m_commandBufferSize)
1017 {
1018 m_IndirectHeapSize = size;
1019 return MOS_STATUS_SUCCESS;
1020 }
1021 else
1022 {
1023 MOS_OS_ASSERTMESSAGE("Indirect State Size if out of boundry!");
1024 return MOS_STATUS_UNKNOWN;
1025 }
1026 }
1027 }
1028
1029 MOS_STATUS GpuContextSpecificNext::GetIndirectState(
1030 uint32_t &offset,
1031 uint32_t &size)
1032 {
1033 MOS_OS_FUNCTION_ENTER;
1034
1035 if (m_ocaLogSectionSupported)
1036 {
1037 offset = m_commandBufferSize - m_IndirectHeapSize - OCA_LOG_SECTION_SIZE_MAX;
1038 }
1039 else
1040 {
1041 offset = m_commandBufferSize - m_IndirectHeapSize;
1042 }
1043 size = m_IndirectHeapSize;
1044
1045 return MOS_STATUS_SUCCESS;
1046 }
1047
1048 MOS_STATUS GpuContextSpecificNext::GetIndirectStatePointer(
1049 uint8_t **indirectState)
1050 {
1051 MOS_OS_FUNCTION_ENTER;
1052
1053 MOS_OS_CHK_NULL_RETURN(indirectState);
1054
1055 if (m_ocaLogSectionSupported)
1056 {
1057 *indirectState = (uint8_t *)m_commandBuffer->pCmdBase + m_commandBufferSize - m_IndirectHeapSize - OCA_LOG_SECTION_SIZE_MAX;
1058 }
1059 else
1060 {
1061 *indirectState = (uint8_t *)m_commandBuffer->pCmdBase + m_commandBufferSize - m_IndirectHeapSize;
1062 }
1063
1064 return MOS_STATUS_SUCCESS;
1065 }
1066
1067 MOS_STATUS GpuContextSpecificNext::ResizeCommandBufferAndPatchList(
1068 uint32_t requestedCommandBufferSize,
1069 uint32_t requestedPatchListSize,
1070 uint32_t flags)
1071 {
1072 MOS_OS_FUNCTION_ENTER;
1073
1074 // m_commandBufferSize is used for allocate command buffer and submit command buffer, in this moment, command buffer has not allocated yet.
1075 // Linux KMD requires command buffer size align to 8 bytes, or it will not execute the commands.
1076 if (m_ocaLogSectionSupported /*&& !m_ocaSizeIncreaseDone*/)
1077 {
1078 m_commandBufferSize = MOS_ALIGN_CEIL(MosOcaInterfaceSpecific::IncreaseSize(requestedCommandBufferSize), 8);
1079 // m_ocaSizeIncreaseDone = true;
1080 }
1081 else
1082 {
1083 m_commandBufferSize = MOS_ALIGN_CEIL(requestedCommandBufferSize, 8);
1084 }
1085
1086 if (requestedPatchListSize > m_maxPatchLocationsize)
1087 {
1088 PPATCHLOCATIONLIST newPatchList = (PPATCHLOCATIONLIST)MOS_ReallocMemory(m_patchLocationList, sizeof(PATCHLOCATIONLIST) * requestedPatchListSize);
1089 MOS_OS_CHK_NULL_RETURN(newPatchList);
1090
1091 m_patchLocationList = newPatchList;
1092
1093 // now zero the extended portion
1094 MosUtilities::MosZeroMemory((m_patchLocationList + m_maxPatchLocationsize), sizeof(PATCHLOCATIONLIST) * (requestedPatchListSize - m_maxPatchLocationsize));
1095 m_maxPatchLocationsize = requestedPatchListSize;
1096 }
1097
1098 return MOS_STATUS_SUCCESS;
1099 }
1100
1101 MOS_STATUS GpuContextSpecificNext::ResizeCommandBuffer(uint32_t requestedSize)
1102 {
1103 MOS_OS_FUNCTION_ENTER;
1104
1105 m_commandBufferSize = requestedSize;
1106
1107 return MOS_STATUS_SUCCESS;
1108 }
1109
1110 MOS_VDBOX_NODE_IND GpuContextSpecificNext::GetVdboxNodeId(
1111 PMOS_COMMAND_BUFFER cmdBuffer)
1112 {
1113 MOS_VDBOX_NODE_IND idx = MOS_VDBOX_NODE_INVALID;
1114
1115 if (cmdBuffer == nullptr)
1116 {
1117 MOS_OS_ASSERTMESSAGE("No cmd buffer provided in GetVdboxNodeId!");
1118 return idx;
1119 }
1120
1121 // If we have assigned vdbox index for the given cmdbuf, return it immediately
1122 if (MOS_VDBOX_NODE_INVALID != cmdBuffer->iVdboxNodeIndex) {
1123 idx = cmdBuffer->iVdboxNodeIndex;
1124 return idx;
1125 }
1126
1127 return idx;
1128 }
1129
1130 uint32_t GpuContextSpecificNext::GetVcsExecFlag(
1131 PMOS_COMMAND_BUFFER cmdBuffer,
1132 MOS_GPU_NODE gpuNode)
1133 {
1134 if (cmdBuffer == 0)
1135 {
1136 MOS_OS_ASSERTMESSAGE("Input invalid(null) parameter.");
1137 return I915_EXEC_DEFAULT;
1138 }
1139
1140 uint32_t vcsExecFlag = I915_EXEC_BSD | I915_EXEC_BSD_RING1;
1141
1142 if (MOS_VDBOX_NODE_INVALID == cmdBuffer->iVdboxNodeIndex)
1143 {
1144 // That's those case when BB did not have any VDBOX# specific commands.
1145 // Thus, we need to select VDBOX# here. Alternatively we can rely on KMD
1146 // to make balancing for us, i.e. rely on Virtual Engine support.
1147 cmdBuffer->iVdboxNodeIndex = GetVdboxNodeId(cmdBuffer);
1148 if (MOS_VDBOX_NODE_INVALID == cmdBuffer->iVdboxNodeIndex)
1149 {
1150 cmdBuffer->iVdboxNodeIndex = (gpuNode == MOS_GPU_NODE_VIDEO)?
1151 MOS_VDBOX_NODE_1: MOS_VDBOX_NODE_2;
1152 }
1153 }
1154
1155 if (MOS_VDBOX_NODE_1 == cmdBuffer->iVdboxNodeIndex)
1156 {
1157 vcsExecFlag = I915_EXEC_BSD | I915_EXEC_BSD_RING1;
1158 }
1159 else if (MOS_VDBOX_NODE_2 == cmdBuffer->iVdboxNodeIndex)
1160 {
1161 vcsExecFlag = I915_EXEC_BSD | I915_EXEC_BSD_RING2;
1162 }
1163
1164 return vcsExecFlag;
1165 }
1166
1167 #if (_DEBUG || _RELEASE_INTERNAL)
1168 MOS_LINUX_BO* GpuContextSpecificNext::GetNopCommandBuffer(
1169 MOS_STREAM_HANDLE streamState)
1170 {
1171 int j;
1172 uint32_t *buf = nullptr;
1173 MOS_LINUX_BO* bo = nullptr;
1174
1175 j = 0;
1176
1177 if(streamState == nullptr || streamState->perStreamParameters == nullptr)
1178 {
1179 return nullptr;
1180 }
1181
1182 auto perStreamParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
1183 struct mos_drm_bo_alloc alloc;
1184 alloc.name = "NOP_CMD_BO";
1185 alloc.size = 4096;
1186 alloc.alignment = 4096;
1187 alloc.ext.mem_type = MOS_MEMPOOL_VIDEOMEMORY;
1188 bo = mos_bo_alloc(perStreamParameters->bufmgr, &alloc);
1189 if(bo == nullptr)
1190 {
1191 return nullptr;
1192 }
1193
1194 mos_bo_map(bo, 1);
1195 buf = (uint32_t*)bo->virt;
1196 if(buf == nullptr)
1197 {
1198 mos_bo_unreference(bo);
1199 return nullptr;
1200 }
1201
1202 buf[j++] = 0x05000000; // MI_BATCH_BUFFER_END
1203
1204 mos_bo_unmap(bo);
1205
1206 return bo;
1207 }
1208 #endif // _DEBUG || _RELEASE_INTERNAL
1209
1210
1211 MOS_STATUS GpuContextSpecificNext::MapResourcesToAuxTable(mos_linux_bo *cmd_bo)
1212 {
1213 MOS_OS_CHK_NULL_RETURN(cmd_bo);
1214
1215 OsContextSpecificNext *osCtx = static_cast<OsContextSpecificNext*>(m_osContext);
1216 MOS_OS_CHK_NULL_RETURN(osCtx);
1217
1218 AuxTableMgr *auxTableMgr = osCtx->GetAuxTableMgr();
1219 if (auxTableMgr && m_currentNumPatchLocations > 0)
1220 {
1221 // Map compress allocations to aux table if it is not mapped.
1222 for (uint32_t i = 0; i < m_numAllocations; i++)
1223 {
1224 auto res = (PMOS_RESOURCE)m_allocationList[i].hAllocation;
1225 MOS_OS_CHK_NULL_RETURN(res);
1226 MOS_OS_CHK_STATUS_RETURN(auxTableMgr->MapResource(res->pGmmResInfo, res->bo));
1227 }
1228 MOS_OS_CHK_STATUS_RETURN(auxTableMgr->EmitAuxTableBOList(cmd_bo));
1229 }
1230 return MOS_STATUS_SUCCESS;
1231 }
1232
1233 MOS_STATUS GpuContextSpecificNext::SubmitCommandBuffer(
1234 MOS_STREAM_HANDLE streamState,
1235 PMOS_COMMAND_BUFFER cmdBuffer,
1236 bool nullRendering)
1237 {
1238 MOS_OS_FUNCTION_ENTER;
1239
1240 MOS_TraceEventExt(EVENT_MOS_BATCH_SUBMIT, EVENT_TYPE_START, nullptr, 0, nullptr, 0);
1241
1242 MOS_OS_CHK_NULL_RETURN(streamState);
1243 auto perStreamParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
1244 MOS_OS_CHK_NULL_RETURN(perStreamParameters);
1245 MOS_OS_CHK_NULL_RETURN(cmdBuffer);
1246 MOS_OS_CHK_NULL_RETURN(m_patchLocationList);
1247
1248 MOS_GPU_NODE gpuNode = OSKMGetGpuNode(m_gpuContext);
1249 uint32_t execFlag = gpuNode;
1250 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
1251 int32_t ret = 0;
1252 bool scalaEnabled = false;
1253 auto it = m_secondaryCmdBufs.begin();
1254
1255 // Command buffer object DRM pointer
1256 m_cmdBufFlushed = true;
1257 auto cmd_bo = cmdBuffer->OsResource.bo;
1258
1259 // Map Resource to Aux if needed
1260 MapResourcesToAuxTable(cmd_bo);
1261 for(auto it : m_secondaryCmdBufs)
1262 {
1263 MapResourcesToAuxTable(it.second->OsResource.bo);
1264 }
1265
1266 if (m_secondaryCmdBufs.size() >= 2)
1267 {
1268 scalaEnabled = true;
1269 cmdBuffer->iSubmissionType = SUBMISSION_TYPE_MULTI_PIPE_MASTER;
1270 }
1271
1272 std::vector<PMOS_RESOURCE> mappedResList;
1273 std::vector<MOS_LINUX_BO *> skipSyncBoList;
1274
1275 // Now, the patching will be done, based on the patch list.
1276 for (uint32_t patchIndex = 0; patchIndex < m_currentNumPatchLocations; patchIndex++)
1277 {
1278 auto currentPatch = &m_patchLocationList[patchIndex];
1279 MOS_OS_CHK_NULL_RETURN(currentPatch);
1280
1281 auto tempCmdBo = currentPatch->cmdBo == nullptr ? cmd_bo : currentPatch->cmdBo;
1282
1283 // Following are for Nested BB buffer, if it's nested BB, we need to ensure it's locked.
1284 if (tempCmdBo != cmd_bo)
1285 {
1286 bool isSecondaryCmdBuf = false;
1287 it = m_secondaryCmdBufs.begin();
1288 while(it != m_secondaryCmdBufs.end())
1289 {
1290 if (it->second->OsResource.bo == tempCmdBo)
1291 {
1292 isSecondaryCmdBuf = true;
1293 break;
1294 }
1295 it++;
1296 }
1297
1298 for(auto allocIdx = 0; allocIdx < m_numAllocations && (!isSecondaryCmdBuf); allocIdx++)
1299 {
1300 auto tempRes = (PMOS_RESOURCE)m_allocationList[allocIdx].hAllocation;
1301 if (tempCmdBo == tempRes->bo)
1302 {
1303 GraphicsResourceNext::LockParams param;
1304 param.m_writeRequest = true;
1305 tempRes->pGfxResourceNext->Lock(m_osContext, param);
1306 mappedResList.push_back(tempRes);
1307 break;
1308 }
1309 }
1310 }
1311
1312 // This is the resource for which patching will be done
1313 auto resource = (PMOS_RESOURCE)m_allocationList[currentPatch->AllocationIndex].hAllocation;
1314 MOS_OS_CHK_NULL_RETURN(resource);
1315
1316 // For now, we'll assume the system memory's DRM bo pointer
1317 // is NULL. If nullptr is detected, then the resource has been
1318 // placed inside the command buffer's indirect state area.
1319 // We'll simply set alloc_bo to the command buffer's bo pointer.
1320 MOS_OS_ASSERT(resource->bo);
1321
1322 auto alloc_bo = (resource->bo) ? resource->bo : tempCmdBo;
1323
1324 MOS_OS_CHK_STATUS_RETURN(streamState->osCpInterface->PermeatePatchForHM(
1325 tempCmdBo->virt,
1326 currentPatch,
1327 resource));
1328
1329 uint64_t boOffset = alloc_bo->offset64;
1330 if (!mos_bo_is_softpin(alloc_bo))
1331 {
1332 if (alloc_bo != tempCmdBo)
1333 {
1334 auto item_ctx = perStreamParameters->contextOffsetList.begin();
1335 for (; item_ctx != perStreamParameters->contextOffsetList.end(); item_ctx++)
1336 {
1337 if (item_ctx->intel_context == perStreamParameters->intel_context && item_ctx->target_bo == alloc_bo)
1338 {
1339 boOffset = item_ctx->offset64;
1340 break;
1341 }
1342 }
1343 }
1344 }
1345
1346 MOS_OS_CHK_NULL_RETURN(tempCmdBo->virt);
1347 if (perStreamParameters->bUse64BitRelocs)
1348 {
1349 *((uint64_t *)((uint8_t *)tempCmdBo->virt + currentPatch->PatchOffset)) =
1350 boOffset + currentPatch->AllocationOffset;
1351 }
1352 else
1353 {
1354 *((uint32_t *)((uint8_t *)tempCmdBo->virt + currentPatch->PatchOffset)) =
1355 boOffset + currentPatch->AllocationOffset;
1356 }
1357
1358 if (scalaEnabled)
1359 {
1360 it = m_secondaryCmdBufs.begin();
1361 while(it != m_secondaryCmdBufs.end())
1362 {
1363 if (it->second->OsResource.bo == tempCmdBo &&
1364 it->second->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_SLAVE &&
1365 !mos_bo_is_exec_object_async(alloc_bo))
1366 {
1367 skipSyncBoList.push_back(alloc_bo);
1368 break;
1369 }
1370 it++;
1371 }
1372 }
1373 else if (cmdBuffer->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_SLAVE &&
1374 !mos_bo_is_exec_object_async(alloc_bo))
1375 {
1376 skipSyncBoList.push_back(alloc_bo);
1377 }
1378
1379 #if (_DEBUG || _RELEASE_INTERNAL)
1380 {
1381 uint32_t evtData[] = {alloc_bo->handle, currentPatch->uiWriteOperation, currentPatch->AllocationOffset};
1382 MOS_TraceEventExt(EVENT_MOS_BATCH_SUBMIT, EVENT_TYPE_INFO,
1383 evtData, sizeof(evtData),
1384 &boOffset, sizeof(boOffset));
1385 }
1386 #endif
1387
1388 if(mos_bo_is_softpin(alloc_bo))
1389 {
1390 if (alloc_bo != tempCmdBo)
1391 {
1392 ret = mos_bo_add_softpin_target(tempCmdBo, alloc_bo, currentPatch->uiWriteOperation);
1393 }
1394 }
1395 else
1396 {
1397 // This call will patch the command buffer with the offsets of the indirect state region of the command buffer
1398 ret = mos_bo_emit_reloc(
1399 tempCmdBo, // Command buffer
1400 currentPatch->PatchOffset, // Offset in the command buffer
1401 alloc_bo, // Allocation object for which the patch will be made.
1402 currentPatch->AllocationOffset, // Offset to the indirect state
1403 I915_GEM_DOMAIN_RENDER, // Read domain
1404 (currentPatch->uiWriteOperation) ? I915_GEM_DOMAIN_RENDER : 0x0, // Write domain
1405 boOffset);
1406 }
1407
1408 if (ret != 0)
1409 {
1410 MOS_OS_ASSERTMESSAGE("Error patching alloc_bo = 0x%x, cmd_bo = 0x%x.",
1411 (uintptr_t)alloc_bo,
1412 (uintptr_t)tempCmdBo);
1413 return MOS_STATUS_UNKNOWN;
1414 }
1415 }
1416
1417 for(auto res: mappedResList)
1418 {
1419 res->pGfxResourceNext->Unlock(m_osContext);
1420 }
1421 mappedResList.clear();
1422
1423 if (scalaEnabled)
1424 {
1425 it = m_secondaryCmdBufs.begin();
1426 while(it != m_secondaryCmdBufs.end())
1427 {
1428 //Add Batch buffer End Command
1429 uint32_t batchBufferEndCmd = MI_BATCHBUFFER_END;
1430 if (MOS_FAILED(Mos_AddCommand(
1431 it->second,
1432 &batchBufferEndCmd,
1433 sizeof(uint32_t))))
1434 {
1435 MOS_OS_ASSERTMESSAGE("Inserting BB_END failed!");
1436 return MOS_STATUS_UNKNOWN;
1437 }
1438 it++;
1439 }
1440 }
1441 else
1442 {
1443 //Add Batch buffer End Command
1444 uint32_t batchBufferEndCmd = MI_BATCHBUFFER_END;
1445 if (MOS_FAILED(Mos_AddCommand(
1446 cmdBuffer,
1447 &batchBufferEndCmd,
1448 sizeof(uint32_t))))
1449 {
1450 MOS_OS_ASSERTMESSAGE("Inserting BB_END failed!");
1451 return MOS_STATUS_UNKNOWN;
1452 }
1453 }
1454 // dump before cmd buffer unmap
1455 MOS_TraceDumpExt("CmdBuffer", m_gpuContext, cmdBuffer->pCmdBase, cmdBuffer->iOffset);
1456
1457 // Now, we can unmap the video command buffer, since we don't need CPU access anymore.
1458 MOS_OS_CHK_NULL_RETURN(cmdBuffer->OsResource.pGfxResourceNext);
1459
1460 cmdBuffer->OsResource.pGfxResourceNext->Unlock(m_osContext);
1461
1462 it = m_secondaryCmdBufs.begin();
1463 while(it != m_secondaryCmdBufs.end())
1464 {
1465 MOS_OS_CHK_NULL_RETURN(it->second->OsResource.pGfxResourceNext);
1466 it->second->OsResource.pGfxResourceNext->Unlock(m_osContext);
1467
1468 it++;
1469 }
1470
1471 int32_t perfData;
1472 if (perStreamParameters->pPerfData != nullptr)
1473 {
1474 perfData = *(int32_t *)(perStreamParameters->pPerfData);
1475 }
1476 else
1477 {
1478 perfData = 0;
1479 }
1480
1481 drm_clip_rect_t *cliprects = nullptr;
1482 int32_t num_cliprects = 0;
1483 int32_t DR4 = perStreamParameters->uEnablePerfTag ? perfData : 0;
1484
1485 //Since CB2 command is not supported, remove it and set cliprects to nullprt as default.
1486 if ((gpuNode == MOS_GPU_NODE_VIDEO || gpuNode == MOS_GPU_NODE_VIDEO2) &&
1487 (cmdBuffer->iSubmissionType & SUBMISSION_TYPE_SINGLE_PIPE_MASK))
1488 {
1489 if (perStreamParameters->bKMDHasVCS2)
1490 {
1491 if (perStreamParameters->bPerCmdBufferBalancing)
1492 {
1493 execFlag = GetVcsExecFlag(cmdBuffer, gpuNode);
1494 }
1495 else if (gpuNode == MOS_GPU_NODE_VIDEO)
1496 {
1497 execFlag = I915_EXEC_BSD | I915_EXEC_BSD_RING1;
1498 }
1499 else if (gpuNode == MOS_GPU_NODE_VIDEO2)
1500 {
1501 execFlag = I915_EXEC_BSD | I915_EXEC_BSD_RING2;
1502 }
1503 }
1504 else
1505 {
1506 execFlag = I915_EXEC_BSD | I915_EXEC_BSD_RING1;
1507 }
1508 }
1509
1510 #if (_DEBUG || _RELEASE_INTERNAL)
1511
1512 MOS_LINUX_BO *nop_cmd_bo = nullptr;
1513
1514 if (nullRendering == true)
1515 {
1516 nop_cmd_bo = GetNopCommandBuffer(streamState);
1517
1518 if (nop_cmd_bo)
1519 {
1520 ret = mos_bo_mrb_exec(nop_cmd_bo,
1521 4096,
1522 nullptr,
1523 0,
1524 0,
1525 execFlag);
1526 }
1527 else
1528 {
1529 MOS_OS_ASSERTMESSAGE("Mos_GetNopCommandBuffer_Linux failed!");
1530 }
1531 }
1532
1533 #endif //(_DEBUG || _RELEASE_INTERNAL)
1534
1535 if (gpuNode != I915_EXEC_RENDER &&
1536 streamState->osCpInterface->IsTearDownHappen())
1537 {
1538 // skip PAK command when CP tear down happen to avoid of GPU hang
1539 // conditonal batch buffer start PoC is in progress
1540 }
1541 else if (nullRendering == false)
1542 {
1543 UnlockPendingOcaBuffers(cmdBuffer, perStreamParameters);
1544 if (streamState->ctxBasedScheduling && m_i915Context[0] != nullptr)
1545 {
1546 if (cmdBuffer->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_MASK)
1547 {
1548 if (scalaEnabled && !streamState->bParallelSubmission)
1549 {
1550 uint32_t secondaryIndex = 0;
1551 it = m_secondaryCmdBufs.begin();
1552 while(it != m_secondaryCmdBufs.end())
1553 {
1554 if (it->second->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_SLAVE)
1555 {
1556 if(execFlag == MOS_GPU_NODE_VE)
1557 {
1558 // decode excluded since init in other place
1559 it->second->iSubmissionType |= (secondaryIndex << SUBMISSION_TYPE_MULTI_PIPE_SLAVE_INDEX_SHIFT);
1560 secondaryIndex++;
1561 }
1562 }
1563 ret = SubmitPipeCommands(it->second,
1564 it->second->OsResource.bo,
1565 perStreamParameters,
1566 skipSyncBoList,
1567 execFlag,
1568 DR4);
1569 it++;
1570 }
1571 }
1572 else if(scalaEnabled && streamState->bParallelSubmission)
1573 {
1574 ret = ParallelSubmitCommands(m_secondaryCmdBufs,
1575 perStreamParameters,
1576 execFlag,
1577 DR4);
1578 }
1579 else
1580 {
1581 ret = SubmitPipeCommands(cmdBuffer,
1582 cmd_bo,
1583 perStreamParameters,
1584 skipSyncBoList,
1585 execFlag,
1586 DR4);
1587 }
1588 }
1589 else
1590 {
1591 ret = mos_bo_context_exec2(cmd_bo,
1592 m_commandBufferSize,
1593 m_i915Context[0],
1594 cliprects,
1595 num_cliprects,
1596 DR4,
1597 m_i915ExecFlag,
1598 nullptr);
1599 }
1600 }
1601 else
1602 {
1603 ret = mos_bo_context_exec2(cmd_bo,
1604 m_commandBufferSize,
1605 perStreamParameters->intel_context,
1606 cliprects,
1607 num_cliprects,
1608 DR4,
1609 execFlag,
1610 nullptr);
1611 }
1612 if (ret != 0)
1613 {
1614 eStatus = MOS_STATUS_UNKNOWN;
1615 }
1616 }
1617
1618 if (eStatus != MOS_STATUS_SUCCESS)
1619 {
1620 MOS_OS_ASSERTMESSAGE("Command buffer submission failed!");
1621 }
1622
1623 MosUtilDevUltSpecific::MOS_DEVULT_FuncCall(pfnUltGetCmdBuf, cmdBuffer);
1624
1625 #if MOS_COMMAND_BUFFER_DUMP_SUPPORTED
1626 pthread_mutex_lock(&command_dump_mutex);
1627 if (streamState->dumpCommandBuffer)
1628 {
1629 if (scalaEnabled)
1630 {
1631 it = m_secondaryCmdBufs.begin();
1632 while(it != m_secondaryCmdBufs.end())
1633 {
1634 mos_bo_map(it->second->OsResource.bo, 0);
1635 MosInterface::DumpCommandBuffer(streamState, it->second);
1636 mos_bo_unmap(it->second->OsResource.bo);
1637 it++;
1638 }
1639 }
1640 else
1641 {
1642 mos_bo_map(cmd_bo, 0);
1643 MosInterface::DumpCommandBuffer(streamState, cmdBuffer);
1644 mos_bo_unmap(cmd_bo);
1645 }
1646 }
1647 pthread_mutex_unlock(&command_dump_mutex);
1648 #endif // MOS_COMMAND_BUFFER_DUMP_SUPPORTED
1649
1650 #if (_DEBUG || _RELEASE_INTERNAL)
1651 if (nop_cmd_bo)
1652 {
1653 mos_bo_unreference(nop_cmd_bo);
1654 }
1655 #endif //(_DEBUG || _RELEASE_INTERNAL)
1656
1657 //clear command buffer relocations to fix memory leak issue
1658 for (uint32_t patchIndex = 0; patchIndex < m_currentNumPatchLocations; patchIndex++)
1659 {
1660 auto currentPatch = &m_patchLocationList[patchIndex];
1661 MOS_OS_CHK_NULL_RETURN(currentPatch);
1662
1663 if(currentPatch->cmdBo)
1664 mos_bo_clear_relocs(currentPatch->cmdBo, 0);
1665 }
1666
1667 it = m_secondaryCmdBufs.begin();
1668 while(it != m_secondaryCmdBufs.end())
1669 {
1670 MOS_FreeMemory(it->second);
1671 it++;
1672 }
1673 m_secondaryCmdBufs.clear();
1674
1675 skipSyncBoList.clear();
1676
1677 // Reset resource allocation
1678 m_numAllocations = 0;
1679 MosUtilities::MosZeroMemory(m_allocationList, sizeof(ALLOCATION_LIST) * m_maxNumAllocations);
1680 m_currentNumPatchLocations = 0;
1681 MosUtilities::MosZeroMemory(m_patchLocationList, sizeof(PATCHLOCATIONLIST) * m_maxNumAllocations);
1682 m_resCount = 0;
1683
1684 MosUtilities::MosZeroMemory(m_writeModeList, sizeof(bool) * m_maxNumAllocations);
1685 finish:
1686 MOS_TraceEventExt(EVENT_MOS_BATCH_SUBMIT, EVENT_TYPE_END, &eStatus, sizeof(eStatus), nullptr, 0);
1687 return eStatus;
1688 }
1689
1690 void GpuContextSpecificNext::UnlockPendingOcaBuffers(PMOS_COMMAND_BUFFER cmdBuffer, PMOS_CONTEXT mosContext)
1691 {
1692 MOS_OS_CHK_NULL_NO_STATUS_RETURN(cmdBuffer);
1693 MOS_OS_CHK_NULL_NO_STATUS_RETURN(mosContext);
1694 MosOcaInterface *pOcaInterface = &MosOcaInterfaceSpecific::GetInstance();
1695 if (nullptr == pOcaInterface || !((MosOcaInterfaceSpecific*)pOcaInterface)->IsOcaEnabled())
1696 {
1697 // Will come here for UMD_OCA not being enabled case.
1698 return;
1699 }
1700
1701 int count = 0;
1702 struct MOS_OCA_EXEC_LIST_INFO *info = nullptr;
1703 if ((cmdBuffer->iSubmissionType & SUBMISSION_TYPE_SINGLE_PIPE_MASK) && ((MosOcaInterfaceSpecific*)pOcaInterface)->IsOcaDumpExecListInfoEnabled())
1704 {
1705 info = mos_bo_get_softpin_targets_info(cmdBuffer->OsResource.bo, &count);
1706 }
1707
1708 pOcaInterface->UnlockPendingOcaBuffers(mosContext, info, count);
1709
1710 if(info)
1711 {
1712 free(info);
1713 }
1714 }
1715
1716 int32_t GpuContextSpecificNext::SubmitPipeCommands(
1717 MOS_COMMAND_BUFFER *cmdBuffer,
1718 MOS_LINUX_BO *cmdBo,
1719 PMOS_CONTEXT osContext,
1720 const std::vector<MOS_LINUX_BO *> &skipSyncBoList,
1721 uint32_t execFlag,
1722 int32_t dr4)
1723 {
1724 int32_t ret = 0;
1725 int fence = -1;
1726 unsigned int fence_flag = 0;
1727
1728 MOS_LINUX_CONTEXT *queue = m_i915Context[0];
1729 bool isVeboxSubmission = false;
1730
1731 if (execFlag == MOS_GPU_NODE_VIDEO || execFlag == MOS_GPU_NODE_VIDEO2)
1732 {
1733 execFlag = I915_EXEC_DEFAULT;
1734 }
1735 if (execFlag == MOS_GPU_NODE_VE)
1736 {
1737 execFlag = I915_EXEC_DEFAULT;
1738 isVeboxSubmission = true;
1739 }
1740
1741 if(cmdBuffer->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_SLAVE)
1742 {
1743 fence = osContext->submit_fence;
1744 fence_flag = I915_EXEC_FENCE_SUBMIT;
1745 int slave_index = (cmdBuffer->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_SLAVE_INDEX_MASK) >> SUBMISSION_TYPE_MULTI_PIPE_SLAVE_INDEX_SHIFT;
1746 if(slave_index < 7)
1747 {
1748 queue = m_i915Context[2 + slave_index]; //0 is for single pipe, 1 is for master, slave starts from 2
1749 }
1750 else
1751 {
1752 MOS_OS_ASSERTMESSAGE("slave_index value: %s is invalid!", slave_index);
1753 return -1;
1754 }
1755
1756 if (isVeboxSubmission)
1757 {
1758 queue = m_i915Context[cmdBuffer->iVeboxNodeIndex + 1];
1759 }
1760
1761 for(auto bo: skipSyncBoList)
1762 {
1763 mos_bo_set_exec_object_async(cmdBo, bo);
1764 }
1765 }
1766
1767 if(cmdBuffer->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_MASTER)
1768 {
1769 //Only master pipe needs fence out flag
1770 fence_flag = I915_EXEC_FENCE_OUT;
1771 queue = m_i915Context[1];
1772 }
1773
1774 ret = mos_bo_context_exec2(cmdBo,
1775 cmdBo->size,
1776 queue,
1777 nullptr,
1778 0,
1779 dr4,
1780 execFlag | fence_flag,
1781 &fence);
1782
1783 if(cmdBuffer->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_MASTER)
1784 {
1785 osContext->submit_fence = fence;
1786 }
1787
1788 if(cmdBuffer->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_FLAGS_LAST_PIPE)
1789 {
1790 close(fence);
1791 }
1792
1793 return ret;
1794 }
1795
1796 int32_t GpuContextSpecificNext::ParallelSubmitCommands(
1797 std::map<uint32_t, PMOS_COMMAND_BUFFER> secondaryCmdBufs,
1798 PMOS_CONTEXT osContext,
1799 uint32_t execFlag,
1800 int32_t dr4)
1801 {
1802 int32_t ret = 0;
1803 int fence = -1;
1804 unsigned int fenceFlag = 0;
1805 auto it = m_secondaryCmdBufs.begin();
1806 MOS_LINUX_BO *cmdBos[MAX_PARALLEN_CMD_BO_NUM];
1807 int numBos = 0; // exclude FE bo
1808
1809 MOS_LINUX_CONTEXT *queue = m_i915Context[0];
1810 bool isVeboxSubmission = false;
1811
1812 if (execFlag == MOS_GPU_NODE_VIDEO || execFlag == MOS_GPU_NODE_VIDEO2)
1813 {
1814 execFlag = I915_EXEC_DEFAULT;
1815 }
1816 if (execFlag == MOS_GPU_NODE_VE)
1817 {
1818 execFlag = I915_EXEC_DEFAULT;
1819 isVeboxSubmission = true;
1820 }
1821
1822 while(it != m_secondaryCmdBufs.end())
1823 {
1824 if(it->second->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_ALONE)
1825 {
1826 fenceFlag = I915_EXEC_FENCE_OUT;
1827 queue = m_i915Context[0];
1828
1829 ret = mos_bo_context_exec2(it->second->OsResource.bo,
1830 it->second->OsResource.bo->size,
1831 queue,
1832 nullptr,
1833 0,
1834 dr4,
1835 execFlag | fenceFlag,
1836 &fence);
1837
1838 osContext->submit_fence = fence;
1839 }
1840
1841 if((it->second->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_MASTER)
1842 || (it->second->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_SLAVE))
1843 {
1844 cmdBos[numBos++] = it->second->OsResource.bo;
1845
1846 if(it->second->iSubmissionType & SUBMISSION_TYPE_MULTI_PIPE_FLAGS_LAST_PIPE)
1847 {
1848 queue = m_i915Context[numBos - 1];
1849 MOS_OS_CHK_NULL_RETURN(queue);
1850 if(-1 != fence)
1851 {
1852 fenceFlag = I915_EXEC_FENCE_IN;
1853 }
1854
1855 ret = mos_bo_context_exec3(cmdBos,
1856 numBos,
1857 queue,
1858 nullptr,
1859 0,
1860 dr4,
1861 execFlag | fenceFlag,
1862 &fence);
1863
1864 for(int i = 0; i < numBos; i++)
1865 {
1866 cmdBos[i] = nullptr;
1867 }
1868 numBos = 0;
1869
1870 if(-1 != fence)
1871 {
1872 close(fence);
1873 }
1874 }
1875 }
1876
1877 it++;
1878 }
1879
1880 return ret;
1881 }
1882
1883 void GpuContextSpecificNext::IncrementGpuStatusTag()
1884 {
1885 m_GPUStatusTag = m_GPUStatusTag % UINT_MAX + 1;
1886 if (m_GPUStatusTag == 0)
1887 {
1888 m_GPUStatusTag = 1;
1889 }
1890 }
1891
1892 void GpuContextSpecificNext::UpdatePriority(int32_t priority)
1893 {
1894 if(m_currCtxPriority == priority)
1895 {
1896 return;
1897 }
1898
1899 for (int32_t i=0; i<MAX_ENGINE_INSTANCE_NUM+1; i++)
1900 {
1901 if (m_i915Context[i] != nullptr)
1902 {
1903 int32_t ret = mos_set_context_param(m_i915Context[i], 0, I915_CONTEXT_PARAM_PRIORITY,(uint64_t)priority);
1904 if (ret != 0)
1905 {
1906 MOS_OS_ASSERTMESSAGE("failed to set the gpu priority, errno is %d", ret);
1907 break;
1908 }
1909 }
1910 }
1911 m_currCtxPriority = priority;
1912 }
1913
1914 void GpuContextSpecificNext::ResetGpuContextStatus()
1915 {
1916 MosUtilities::MosZeroMemory(m_allocationList, sizeof(ALLOCATION_LIST) * ALLOCATIONLIST_SIZE);
1917 m_numAllocations = 0;
1918 MosUtilities::MosZeroMemory(m_patchLocationList, sizeof(PATCHLOCATIONLIST) * PATCHLOCATIONLIST_SIZE);
1919 m_currentNumPatchLocations = 0;
1920
1921 MosUtilities::MosZeroMemory(m_attachedResources, sizeof(MOS_RESOURCE) * ALLOCATIONLIST_SIZE);
1922 m_resCount = 0;
1923
1924 MosUtilities::MosZeroMemory(m_writeModeList, sizeof(bool) * ALLOCATIONLIST_SIZE);
1925
1926 if ((m_cmdBufFlushed == true) && m_commandBuffer->OsResource.bo)
1927 {
1928 m_commandBuffer->OsResource.bo = nullptr;
1929 }
1930 }
1931
1932 MOS_STATUS GpuContextSpecificNext::AllocateGPUStatusBuf()
1933 {
1934 MOS_OS_FUNCTION_ENTER;
1935
1936 m_statusBufferResource = (PMOS_RESOURCE)MOS_AllocAndZeroMemory(sizeof(MOS_RESOURCE));
1937 MOS_OS_CHK_NULL_RETURN(m_statusBufferResource);
1938
1939 GraphicsResourceNext::CreateParams params;
1940 params.m_tileType = MOS_TILE_LINEAR;
1941 params.m_type = MOS_GFXRES_BUFFER;
1942 params.m_format = Format_Buffer;
1943 params.m_width = sizeof(MOS_GPU_STATUS_DATA);
1944 params.m_height = 1;
1945 params.m_depth = 1;
1946 params.m_arraySize = 1;
1947 params.m_name = "GPU Status Buffer";
1948
1949 GraphicsResourceNext *graphicsResource = GraphicsResourceNext::CreateGraphicResource(GraphicsResourceNext::osSpecificResource);
1950 MOS_OS_CHK_NULL_RETURN(graphicsResource);
1951
1952 MOS_OS_CHK_STATUS_RETURN(graphicsResource->Allocate(m_osContext, params));
1953
1954 GraphicsResourceNext::LockParams lockParams;
1955 lockParams.m_writeRequest = true;
1956 auto gpuStatusData = (MOS_GPU_STATUS_DATA *)graphicsResource->Lock(m_osContext, lockParams);
1957 if (gpuStatusData == nullptr)
1958 {
1959 MOS_OS_ASSERTMESSAGE("Unable to lock gpu eStatus buffer for read.");
1960 graphicsResource->Free(m_osContext);
1961 MOS_Delete(graphicsResource);
1962 return MOS_STATUS_UNKNOWN;
1963 }
1964
1965 MOS_STATUS eStatus = graphicsResource->ConvertToMosResource(m_statusBufferResource);
1966 MOS_OS_CHK_STATUS_RETURN(eStatus);
1967
1968 return MOS_STATUS_SUCCESS;
1969 }
1970
1971 PMOS_RESOURCE GpuContextSpecificNext::GetOcaRTLogResource(PMOS_RESOURCE globalInst)
1972 {
1973 // OcaRTLogResources are shared w/ different video processors.
1974 // iAllocationIndex array in MOS_RESOURCE indexed by gpu_context type. When resource being accessed
1975 // in GpuContextSpecificNext::RegisterResource and Mos_Specific_GetResourceAllocationIndex w/ more
1976 // than 2 video processors, the value may be overwritten and wrong allocation Index in array may be used.
1977 // To avoid this, use duplicate MOS_RESOURCE instance in GPU Context to ensure differnt iAllocationIndex
1978 // array of OcaRTLogResources being used for different GPU Context.
1979 if (!m_ocaRtLogResInited && globalInst)
1980 {
1981 m_ocaRtLogResource = *globalInst;
1982 m_ocaRtLogResInited = true;
1983 }
1984 return &m_ocaRtLogResource;
1985 }
1986
1987 #if (_DEBUG || _RELEASE_INTERNAL)
1988 bool GpuContextSpecificNext::SelectEngineInstanceByUser(void *engine_map,
1989 uint32_t *engineNum, uint32_t userEngineInstance, MOS_GPU_NODE gpuNode)
1990 {
1991 uint32_t engineInstance = 0x0;
1992
1993 if (userEngineInstance && m_osParameters)
1994 {
1995 if(gpuNode == MOS_GPU_NODE_COMPUTE)
1996 {
1997 engineInstance = (userEngineInstance >> ENGINE_INSTANCE_SELECT_COMPUTE_INSTANCE_SHIFT)
1998 & (ENGINE_INSTANCE_SELECT_ENABLE_MASK >> (MAX_ENGINE_INSTANCE_NUM - *engineNum));
1999 }
2000 else if(gpuNode == MOS_GPU_NODE_VE)
2001 {
2002 engineInstance = (userEngineInstance >> ENGINE_INSTANCE_SELECT_VEBOX_INSTANCE_SHIFT)
2003 & (ENGINE_INSTANCE_SELECT_ENABLE_MASK >> (MAX_ENGINE_INSTANCE_NUM - *engineNum));
2004 }
2005 else if(gpuNode == MOS_GPU_NODE_VIDEO || gpuNode == MOS_GPU_NODE_VIDEO2)
2006 {
2007 engineInstance = (userEngineInstance >> ENGINE_INSTANCE_SELECT_VDBOX_INSTANCE_SHIFT)
2008 & (ENGINE_INSTANCE_SELECT_ENABLE_MASK >> (MAX_ENGINE_INSTANCE_NUM - *engineNum));
2009 }
2010 else
2011 {
2012 MOS_OS_NORMALMESSAGE("Invalid gpu node in use.");
2013 }
2014
2015 mos_select_fixed_engine(m_osParameters->bufmgr, engine_map, engineNum, engineInstance);
2016 }
2017
2018 return engineInstance;
2019 }
2020 #endif