xref: /aosp_15_r20/external/intel-media-driver/media_softlet/agnostic/common/os/mos_oca_rtlog_mgr.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2022-2023, 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_oca_rtlog_mgr.cpp
24 //! \brief    OCA buffer manager class
25 //!
26 
27 #include "mos_oca_rtlog_mgr.h"
28 #include "oca_rtlog_section_mgr.h"
29 #include "mos_context_specific_next.h"
30 
31 /****************************************************************************************************/
32 /*                                      MosOcaRTLogMgr                                              */
33 /****************************************************************************************************/
RegisterCtx(OsContextNext * osDriverContext,MOS_CONTEXT * osContext)34 MOS_STATUS MosOcaRTLogMgr::RegisterCtx(OsContextNext *osDriverContext, MOS_CONTEXT *osContext)
35 {
36     MOS_OCA_RTLOG_RES_AND_INTERFACE resInterface = {};
37     MOS_OS_CHK_STATUS_RETURN(RegisterRes(osDriverContext, &resInterface, osContext));
38     return MOS_STATUS_SUCCESS;
39 }
40 
UnRegisterCtx(OsContextNext * osDriverContext)41 MOS_STATUS MosOcaRTLogMgr::UnRegisterCtx(OsContextNext *osDriverContext)
42 {
43     if (!osDriverContext->GetOcaRTLogResource())
44     {
45         return MOS_STATUS_NULL_POINTER;
46     }
47     UnregisterRes(osDriverContext);
48 
49     return MOS_STATUS_SUCCESS;
50 }
51 
RegisterRes(OsContextNext * osDriverContext,MOS_OCA_RTLOG_RES_AND_INTERFACE * resInterface,MOS_CONTEXT * osContext)52 MOS_STATUS MosOcaRTLogMgr::RegisterRes(OsContextNext *osDriverContext, MOS_OCA_RTLOG_RES_AND_INTERFACE *resInterface, MOS_CONTEXT *osContext)
53 {
54     if (osDriverContext->GetOcaRTLogResource())
55     {
56         return MOS_STATUS_SUCCESS;
57     }
58 
59     resInterface->osInterface = (PMOS_INTERFACE)MOS_AllocAndZeroMemory(sizeof(MOS_INTERFACE));
60     MOS_OS_CHK_NULL_RETURN(resInterface->osInterface);
61     MOS_STATUS status         = Mos_InitInterface(resInterface->osInterface, osContext, COMPONENT_OCA);
62     if (MOS_FAILED(status))
63     {
64         MOS_SafeFreeMemory(resInterface->osInterface);
65         MOS_OS_CHK_STATUS_RETURN(status);
66     }
67 
68     MOS_ALLOC_GFXRES_PARAMS sParams = {};
69     sParams.Type                    = MOS_GFXRES_BUFFER;
70     sParams.dwBytes                 = MAX_OCA_RT_SIZE;
71     sParams.pSystemMemory           = (void *)m_heapAddr;
72     sParams.TileType                = MOS_TILE_LINEAR;
73     sParams.Format                  = Format_Buffer;
74     sParams.pBufName                = "OcaRtlog";
75     sParams.bIsPersistent           = 1;
76     // Use encode output bitstream to ensure coherency being enabled for CPU catchable surface, which
77     // will be checked during MapGpuVirtualAddress w/ critical message for invalid case.
78     sParams.ResUsageType            = MOS_HW_RESOURCE_USAGE_ENCODE_OUTPUT_BITSTREAM;
79     resInterface->ocaRTLogResource  = (PMOS_RESOURCE)MOS_AllocAndZeroMemory(sizeof(MOS_RESOURCE));
80     if (nullptr == resInterface->ocaRTLogResource)
81     {
82         Mos_DestroyInterface(resInterface->osInterface);
83         MOS_SafeFreeMemory(resInterface->osInterface);
84         MOS_OS_CHK_NULL_RETURN(resInterface->ocaRTLogResource);
85     }
86 
87     // Allocate resource
88     status = resInterface->osInterface->pfnAllocateResource(resInterface->osInterface, &sParams, resInterface->ocaRTLogResource);
89     if (MOS_FAILED(status))
90     {
91         MOS_SafeFreeMemory(resInterface->ocaRTLogResource);
92         Mos_DestroyInterface(resInterface->osInterface);
93         MOS_SafeFreeMemory(resInterface->osInterface);
94         MOS_OS_CHK_STATUS_RETURN(status);
95     }
96     status = MapGfxVa(resInterface->ocaRTLogResource, osDriverContext);
97     if (MOS_FAILED(status))
98     {
99         resInterface->osInterface->pfnFreeResource(resInterface->osInterface, resInterface->ocaRTLogResource);
100         MOS_SafeFreeMemory(resInterface->ocaRTLogResource);
101         Mos_DestroyInterface(resInterface->osInterface);
102         MOS_SafeFreeMemory(resInterface->osInterface);
103         MOS_OS_CHK_STATUS_RETURN(status);
104     }
105     s_ocaMutex.Lock();
106     m_resMap.insert(std::make_pair(osDriverContext, *resInterface));
107     s_ocaMutex.Unlock();
108     osDriverContext->SetRtLogRes(resInterface->ocaRTLogResource);
109     return MOS_STATUS_SUCCESS;
110 }
111 
UnregisterRes(OsContextNext * osDriverContext)112 void MosOcaRTLogMgr::UnregisterRes(OsContextNext *osDriverContext)
113 {
114     MOS_OCA_RTLOG_RES_AND_INTERFACE resInterface = {};
115     auto iter = m_resMap.find(osDriverContext);
116     if (iter == m_resMap.end())
117     {
118         return;
119     }
120     resInterface = iter->second;
121     s_ocaMutex.Lock();
122     m_resMap.erase(osDriverContext);
123     s_ocaMutex.Unlock();
124     if (!resInterface.osInterface || !resInterface.osInterface->pfnFreeResource)
125     {
126         MOS_SafeFreeMemory(resInterface.ocaRTLogResource);
127         resInterface.ocaRTLogResource = nullptr;
128         MOS_SafeFreeMemory(resInterface.osInterface);
129         resInterface.osInterface = nullptr;
130         return;
131     }
132     resInterface.osInterface->pfnFreeResource(resInterface.osInterface, resInterface.ocaRTLogResource);
133     MOS_SafeFreeMemory(resInterface.ocaRTLogResource);
134     resInterface.ocaRTLogResource = nullptr;
135     Mos_DestroyInterface(resInterface.osInterface);
136     MOS_SafeFreeMemory(resInterface.osInterface);
137     resInterface.osInterface = nullptr;
138 }
139 
MosOcaRTLogMgr()140 MosOcaRTLogMgr::MosOcaRTLogMgr()
141 {
142     m_heapAddr = OcaRtLogSectionMgr::GetMemAddress();
143 
144     m_isMgrInitialized = true;
145 }
146 
MosOcaRTLogMgr(MosOcaRTLogMgr &)147 MosOcaRTLogMgr::MosOcaRTLogMgr(MosOcaRTLogMgr &)
148 {
149 }
150 
~MosOcaRTLogMgr()151 MosOcaRTLogMgr::~MosOcaRTLogMgr()
152 {
153     m_globleIndex = -1;
154     m_isMgrInitialized = false;
155     s_isOcaRtlogMgrDestoryed = true;
156 }
157 
158 
operator =(MosOcaRTLogMgr &)159 MosOcaRTLogMgr& MosOcaRTLogMgr::operator= (MosOcaRTLogMgr&)
160 {
161     return *this;
162 }
163 
GetInstance()164 MosOcaRTLogMgr *MosOcaRTLogMgr::GetInstance()
165 {
166     static MosOcaRTLogMgr mgr;
167     return &mgr;
168 }
169 
RegisterContext(OsContextNext * osDriverContext,MOS_CONTEXT * osContext)170 void MosOcaRTLogMgr::RegisterContext(OsContextNext *osDriverContext, MOS_CONTEXT *osContext)
171 {
172     if (!s_enableOcaRTLog)
173     {
174         return;
175     }
176     if (!osContext)
177     {
178         return;
179     }
180     MosOcaRTLogMgr *ocaRTLogMgr = GetInstance();
181     MOS_STATUS      status      = ocaRTLogMgr->RegisterCtx(osDriverContext, osContext);
182     if (status != MOS_STATUS_SUCCESS)
183     {
184         MOS_OS_NORMALMESSAGE("MosOcaRTLogMgr RegisterContext failed!");
185     }
186 }
187 
UnRegisterContext(OsContextNext * osDriverContext)188 void MosOcaRTLogMgr::UnRegisterContext(OsContextNext *osDriverContext)
189 {
190     if (!s_enableOcaRTLog)
191     {
192         return;
193     }
194     if (s_isOcaRtlogMgrDestoryed)
195     {
196         MOS_OS_NORMALMESSAGE("MosOcaRTLogMgr have be destroyed!");
197         return;
198     }
199     MosOcaRTLogMgr *ocaRTLogMgr = GetInstance();
200     MOS_STATUS      status      = ocaRTLogMgr->UnRegisterCtx(osDriverContext);
201     if (status != MOS_STATUS_SUCCESS)
202     {
203         MOS_OS_NORMALMESSAGE("MosOcaRTLogMgr UnRegisterContext failed!");
204     }
205 }