xref: /aosp_15_r20/external/intel-media-driver/media_softlet/linux/common/os/mos_context_specific_next.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
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_context_specific_next.cpp
24 //! \brief    Container for Linux/Android specific parameters shared across different GPU contexts of the same device instance
25 //!
26 
27 #include "mos_os.h"
28 #include "mos_util_debug.h"
29 #include "mos_resource_defs.h"
30 #include <unistd.h>
31 #include <dlfcn.h>
32 #include "hwinfo_linux.h"
33 #include "mos_interface.h"
34 #include <stdlib.h>
35 
36 #include <sys/ipc.h>
37 #include <sys/shm.h>
38 #include <sys/sem.h>
39 #include <sys/types.h>
40 #include <time.h>
41 
42 #if MOS_MEDIASOLO_SUPPORTED
43 #include "mos_os_solo.h"
44 #endif // MOS_MEDIASOLO_SUPPORTED
45 #include "mos_solo_generic.h"
46 #include "mos_context_specific_next.h"
47 #include "mos_gpucontextmgr_next.h"
48 #include "mos_cmdbufmgr_next.h"
49 #include "mos_oca_rtlog_mgr.h"
50 #include "mos_oca_interface_specific.h"
51 #define BATCH_BUFFER_SIZE 0x80000
52 
OsContextSpecificNext()53 OsContextSpecificNext::OsContextSpecificNext()
54 {
55     MOS_OS_FUNCTION_ENTER;
56 }
57 
~OsContextSpecificNext()58 OsContextSpecificNext::~OsContextSpecificNext()
59 {
60     MOS_OS_FUNCTION_ENTER;
61 }
62 
Init(DDI_DEVICE_CONTEXT ddiDriverContext)63 MOS_STATUS OsContextSpecificNext::Init(DDI_DEVICE_CONTEXT ddiDriverContext)
64 {
65     uint32_t      iDeviceId = 0;
66     MOS_STATUS    eStatus   = MOS_STATUS_SUCCESS;
67     uint32_t      value     = 0;
68     uint32_t      mode      = 0;
69     MediaUserSettingSharedPtr   userSettingPtr = nullptr;
70 
71     MOS_OS_FUNCTION_ENTER;
72 
73     eStatus = MOS_STATUS_SUCCESS;
74 
75     PMOS_CONTEXT osDriverContext = (PMOS_CONTEXT)ddiDriverContext;
76 
77     if (GetOsContextValid() == false)
78     {
79         uint32_t commandBufferSize = 0;
80         m_skuTable.reset();
81         m_waTable.reset();
82         MosUtilities::MosZeroMemory(&m_platformInfo, sizeof(m_platformInfo));
83         MosUtilities::MosZeroMemory(&m_gtSystemInfo, sizeof(m_gtSystemInfo));
84 
85         if( nullptr == osDriverContext  ||
86             0 > osDriverContext->fd )
87         {
88             MOS_OS_ASSERT(false);
89             return MOS_STATUS_INVALID_HANDLE;
90         }
91         m_fd = osDriverContext->fd;
92 
93         userSettingPtr = MosInterface::MosGetUserSettingInstance(osDriverContext);
94 
95         m_bufmgr = mos_bufmgr_gem_init(m_fd, BATCH_BUFFER_SIZE, &m_deviceType);
96         if (nullptr == m_bufmgr)
97         {
98             MOS_OS_ASSERTMESSAGE("Not able to allocate buffer manager, fd=0x%d", m_fd);
99             return MOS_STATUS_INVALID_PARAMETER;
100         }
101         mos_bufmgr_enable_reuse(m_bufmgr);
102 
103         osDriverContext->bufmgr                 = m_bufmgr;
104 
105         //Latency reducation:replace HWGetDeviceID to get device using ioctl from drm.
106         iDeviceId   = mos_bufmgr_get_devid(m_bufmgr);
107         m_isAtomSOC = IS_ATOMSOC(iDeviceId);
108 
109         eStatus = NullHwInit((MOS_CONTEXT_HANDLE)osDriverContext);
110         if (!GetNullHwIsEnabled())
111         {
112             eStatus = HWInfo_GetGfxInfo(m_fd, m_bufmgr, &m_platformInfo, &m_skuTable, &m_waTable, &m_gtSystemInfo, userSettingPtr);
113         }
114         else
115         {
116             m_platformInfo  = osDriverContext->m_platform;
117             m_skuTable      = osDriverContext->m_skuTable;
118             m_waTable       = osDriverContext->m_waTable;
119             m_gtSystemInfo  = osDriverContext->m_gtSystemInfo;
120             iDeviceId       = osDriverContext->iDeviceId;
121         }
122 
123         // replace platform/sku/wa/gtsysinfo for os context
124         if (Mos_Solo_IsEnabled(nullptr))
125         {
126             m_skuTable.reset();
127             m_waTable.reset();
128             Mos_Solo_SetPlatform(&m_platformInfo, (PMOS_CONTEXT)osDriverContext);
129             Mos_Solo_SetSkuwaGtInfo((PMOS_CONTEXT)osDriverContext, m_platformInfo, m_skuTable, m_waTable, m_gtSystemInfo, iDeviceId);
130         }
131 
132         if (eStatus != MOS_STATUS_SUCCESS)
133         {
134             MOS_OS_ASSERTMESSAGE("Fatal error - unsuccesfull Sku/Wa/GtSystemInfo initialization");
135             return eStatus;
136         }
137 
138         if (m_platformInfo.eProductFamily == IGFX_METEORLAKE ||
139             m_platformInfo.eProductFamily == IGFX_ARROWLAKE)
140         {
141             ReadUserSetting(
142                 userSettingPtr,
143                 value,
144                 "INTEL MEDIA ALLOC MODE",
145                 MediaUserSetting::Group::Device);
146 
147             if (value)
148             {
149                 mode = (value & 0x000000ff);
150             }
151 
152             // Realloc cache only if it's not mode 0
153             if (mode)
154             {
155                 mos_bufmgr_realloc_cache(m_bufmgr, mode);
156             }
157         }
158 
159         ReadUserSetting(
160             userSettingPtr,
161             value,
162             __MEDIA_USER_FEATURE_VALUE_ENABLE_SOFTPIN,
163             MediaUserSetting::Group::Device);
164 
165         if (value)
166         {
167             bool softpin_va1Malign = false;
168             if (MEDIA_IS_SKU(&m_skuTable, Ftr1MGranularAuxTable))
169             {
170                 softpin_va1Malign = true;
171             }
172 
173             mos_bufmgr_enable_softpin(m_bufmgr, softpin_va1Malign);
174 
175             ReadUserSetting(
176                 userSettingPtr,
177                 value,
178                 __MEDIA_USER_FEATURE_VALUE_ENABLE_VM_BIND,
179                 MediaUserSetting::Group::Device);
180 
181             if (value)
182             {
183                  mos_bufmgr_enable_vmbind(m_bufmgr);
184                  MOS_OS_NORMALMESSAGE("mos_bufmg_enable_vmbind");
185             }
186         }
187 
188         uint64_t isRecoverableContextEnabled = 0;
189         MOS_LINUX_CONTEXT *intel_context = mos_context_create_ext(m_bufmgr, 0, false);
190         int ret = mos_get_context_param(intel_context, 0, DRM_CONTEXT_PARAM_RECOVERABLE, &isRecoverableContextEnabled);
191         if (ret == -EINVAL)
192         {
193             isRecoverableContextEnabled = 1;
194         }
195         if (intel_context)
196         {
197             mos_context_destroy(intel_context);
198         }
199         // set recoverablecontext disabled if want disable object capture
200         if (MEDIA_IS_WA(&m_waTable, WaDisableSetObjectCapture) && isRecoverableContextEnabled)
201         {
202             mos_bufmgr_disable_object_capture(m_bufmgr);
203         }
204 
205         if (MEDIA_IS_SKU(&m_skuTable, FtrEnableMediaKernels) == 0)
206         {
207             MEDIA_WR_WA(&m_waTable, WaHucStreamoutOnlyDisable, 0);
208         }
209 
210         MosUtilities::MosTraceSetupInfo(
211             (VA_MAJOR_VERSION << 16) | VA_MINOR_VERSION,
212             m_platformInfo.eProductFamily,
213             m_platformInfo.eRenderCoreFamily,
214             (m_platformInfo.usRevId << 16) | m_platformInfo.usDeviceID);
215 
216         GMM_SKU_FEATURE_TABLE   gmmSkuTable   = {};
217         GMM_WA_TABLE            gmmWaTable    = {};
218         GMM_GT_SYSTEM_INFO      gmmGtInfo     = {};
219         GMM_ADAPTER_BDF         gmmAdapterBDF = {};
220         eStatus = HWInfo_GetGmmInfo(m_bufmgr, &gmmSkuTable, &gmmWaTable, &gmmGtInfo);
221         if (MOS_STATUS_SUCCESS != eStatus)
222         {
223             MOS_OS_ASSERTMESSAGE("Fatal error - unsuccesfull Gmm Sku/Wa/GtSystemInfo initialization");
224             return eStatus;
225         }
226 
227         eStatus = MosInterface::GetAdapterBDF(osDriverContext, &gmmAdapterBDF);
228         if (MOS_STATUS_SUCCESS != eStatus)
229         {
230             MOS_OS_ASSERTMESSAGE("Fatal error - unsuccesfull Gmm Adapter BDF initialization");
231             return eStatus;
232         }
233 
234         // Initialize Gmm context
235         GMM_INIT_IN_ARGS  gmmInitAgrs = {};
236         GMM_INIT_OUT_ARGS gmmOutArgs  = {};
237         gmmInitAgrs.Platform          = m_platformInfo;
238         gmmInitAgrs.pSkuTable         = &gmmSkuTable;
239         gmmInitAgrs.pWaTable          = &gmmWaTable;
240         gmmInitAgrs.pGtSysInfo        = &gmmGtInfo;
241         gmmInitAgrs.FileDescriptor    = gmmAdapterBDF.Data;
242         gmmInitAgrs.ClientType        = (GMM_CLIENT)GMM_LIBVA_LINUX;
243 
244         // replace sku/wa/gtsysinfo for gmm client context
245         if (Mos_Solo_IsEnabled(nullptr))
246         {
247             gmmInitAgrs.pSkuTable         = &m_skuTable;
248             gmmInitAgrs.pWaTable          = &m_waTable;
249             gmmInitAgrs.pGtSysInfo        = &m_gtSystemInfo;
250         }
251 
252         GMM_STATUS status = InitializeGmm(&gmmInitAgrs, &gmmOutArgs);
253         if (status != GMM_SUCCESS)
254         {
255             MOS_OS_ASSERTMESSAGE("Fatal error - InitializeGmm fail.");
256             return MOS_STATUS_INVALID_PARAMETER;
257         }
258         m_gmmClientContext = gmmOutArgs.pGmmClientContext;
259 
260         m_auxTableMgr = AuxTableMgr::CreateAuxTableMgr(m_bufmgr, &m_skuTable, m_gmmClientContext);
261 
262 #if (_DEBUG || _RELEASE_INTERNAL)
263         ReadUserSettingForDebug(
264             userSettingPtr,
265             osDriverContext->bSimIsActive,
266             __MEDIA_USER_FEATURE_VALUE_SIM_ENABLE,
267             MediaUserSetting::Group::Device);
268 #endif
269 
270         m_useSwSwizzling = osDriverContext->bSimIsActive || MEDIA_IS_SKU(&m_skuTable, FtrUseSwSwizzling);
271 
272         m_tileYFlag      = MEDIA_IS_SKU(&m_skuTable, FtrTileY);
273 
274         m_use64BitRelocs = true;
275 
276         if (!GetNullHwIsEnabled())
277         {
278             osDriverContext->iDeviceId              = iDeviceId;
279             osDriverContext->m_skuTable             = m_skuTable;
280             osDriverContext->m_waTable              = m_waTable;
281             osDriverContext->m_gtSystemInfo         = m_gtSystemInfo;
282             osDriverContext->m_platform             = m_platformInfo;
283         }
284         osDriverContext->pGmmClientContext      = m_gmmClientContext;
285         osDriverContext->m_auxTableMgr          = m_auxTableMgr;
286         osDriverContext->bUseSwSwizzling        = m_useSwSwizzling;
287         osDriverContext->bTileYFlag             = m_tileYFlag;
288         osDriverContext->bIsAtomSOC             = m_isAtomSOC;
289         osDriverContext->m_osDeviceContext      = this;
290 
291         m_usesPatchList             = true;
292         m_usesGfxAddress            = false;
293 
294         SetOsContextValid(true);
295         // Prepare the command buffer manager
296         if (m_ocaLogSectionSupported)
297         {
298             // increase size for oca log section
299             commandBufferSize = MosOcaInterfaceSpecific::IncreaseSize(COMMAND_BUFFER_SIZE);
300         }
301         else
302         {
303             commandBufferSize = COMMAND_BUFFER_SIZE;
304         }
305         m_cmdBufMgr = CmdBufMgrNext::GetObject();
306         MOS_OS_CHK_NULL_RETURN(m_cmdBufMgr);
307         MOS_OS_CHK_STATUS_RETURN(m_cmdBufMgr->Initialize(this, commandBufferSize));
308 
309         // Prepare the gpu Context manager
310         m_gpuContextMgr = GpuContextMgrNext::GetObject(this);
311         MOS_OS_CHK_NULL_RETURN(m_gpuContextMgr);
312 
313         m_perfData = (PERF_DATA*)MOS_AllocAndZeroMemory(sizeof(PERF_DATA));
314         MOS_OS_CHK_NULL_RETURN(m_perfData);
315         osDriverContext->pPerfData = m_perfData;
316 
317         //It must be done with m_gpuContextMgr ready. Insides it will create gpu context.
318 #ifdef _MMC_SUPPORTED
319         m_mosDecompression = MOS_New(MosDecompression, osDriverContext);
320         MOS_OS_CHK_NULL_RETURN(m_mosDecompression);
321         osDriverContext->ppMediaMemDecompState = m_mosDecompression->GetMediaMemDecompState();
322         MOS_OS_CHK_NULL_RETURN(osDriverContext->ppMediaMemDecompState);
323         if (*osDriverContext->ppMediaMemDecompState == nullptr)
324         {
325             MOS_OS_NORMALMESSAGE("Decomp state creation failed");
326         }
327 #endif
328         m_mosMediaCopy = MOS_New(MosMediaCopy, osDriverContext);
329         if (nullptr == m_mosMediaCopy)
330         {
331             MOS_OS_NORMALMESSAGE("m_mosMediaCopy creation failed");
332         }
333         else
334         {
335             osDriverContext->ppMediaCopyState = (void **)m_mosMediaCopy->GetMediaCopyState();
336             if ((nullptr == osDriverContext->ppMediaCopyState) || (nullptr == *osDriverContext->ppMediaCopyState))
337             {
338                 MOS_OS_NORMALMESSAGE("Media Copy state creation failed");
339             }
340         }
341     }
342     MosOcaRTLogMgr::RegisterContext(this, osDriverContext);
343     return eStatus;
344 }
345 
Destroy()346 void OsContextSpecificNext::Destroy()
347 {
348     MOS_OS_FUNCTION_ENTER;
349 
350     if (GetOsContextValid() == true)
351     {
352         if (m_auxTableMgr != nullptr)
353         {
354             MOS_Delete(m_auxTableMgr);
355             m_auxTableMgr = nullptr;
356         }
357 
358         m_skuTable.reset();
359         m_waTable.reset();
360 
361         mos_bufmgr_destroy(m_bufmgr);
362 
363         // Delete Gmm context
364         GMM_INIT_OUT_ARGS gmmOutArgs = {};
365         gmmOutArgs.pGmmClientContext = m_gmmClientContext;
366         GmmAdapterDestroy(&gmmOutArgs);
367         m_gmmClientContext = nullptr;
368 
369         SetOsContextValid(false);
370 
371         if (m_perfData != nullptr)
372         {
373            MOS_FreeMemory(m_perfData);
374            m_perfData = nullptr;
375         }
376 
377         if (m_mosMediaCopy != nullptr)
378         {
379            MOS_Delete(m_mosMediaCopy);
380         }
381     }
382 
383 }
384 
385