xref: /aosp_15_r20/external/intel-media-driver/cmrtlib/linux/hardware/cm_device_export_os.cpp (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2017, 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 #include "cm_device.h"
24 #include "cm_timer.h"
25 
26 //!
27 //! \brief      Creates CM Device for hardware mode in linux
28 //! \details    Creates a CmDevice from scratch or creates a CmDevice based on the input
29 //!             vaDisplay interface. If CmDevice is created from scratch, an
30 //!             internal vaDisplay interface will be created by the runtime.
31 //!             Creation of more than one CmDevice for concurrent use is supported.
32 //!             The CM API version supported by the library will be returned
33 //!             in parameter version.
34 //! \param      [out] device
35 //!             Reference to the pointer to the CmDevice to be created
36 //! \param      [out] version
37 //!             Reference to CM API version supported by the runtime library
38 //! \param      [in] vaDisplay
39 //!             Reference to a given VADisplay from vaInitialize if NOT nullptr
40 //! \retval     CM_SUCCESS if device successfully created
41 //! \retval     CM_OUT_OF_HOST_MEMORY if out of system memory
42 //! \retval     CM_INVALID_LIBVA_INITIALIZE if vaInitialize failed
43 //! \retval     CM_FAILURE otherwise
44 //!
45 extern "C"
GetCmSupportedAdapters(uint32_t & count)46 CM_RT_API int32_t GetCmSupportedAdapters(uint32_t& count)
47 {
48     INSERT_PROFILER_RECORD();
49     return CmDevice_RT::GetSupportedAdapters(count);
50 }
51 
52 
53 //!
54 //! \brief      Creates CM Device according to user specified GPU adapter in hardware mode.
55 //! \details    This API is supported from Cm 4.0. The DevCreateOption is supported
56 //!
57 //! \param      [out] pCmDev
58 //!             Reference to the pointer to the CmDevice to be created
59 //! \param      [out] version
60 //!             Reference to CM API version supported by the runtime library
61 //! \param      [in] adapterIndex
62 //!             Specify an index number of the GPU adapter to use from the supported GPU adapter list
63 //! \param      [in] DevCreateOption
64 //!             Device creation option flag
65 //! \retval     CM_SUCCESS if the CmDevice is successfully created.
66 //! \retval     CM_OUT_OF_HOST_MEMORY if out of system memory.
67 //! \retval     CM_FAILURE otherwise.
68 //!
69 extern "C"
CreateCmDeviceFromAdapter(CmDevice * & pCmDev,uint32_t & version,int32_t adapterIndex,uint32_t CreateOption=0)70 CM_RT_API int32_t CreateCmDeviceFromAdapter(CmDevice* &pCmDev, uint32_t& version, int32_t adapterIndex, uint32_t CreateOption = 0)
71 {
72     INSERT_PROFILER_RECORD();
73 
74     int32_t result = CM_FAILURE;
75     uint32_t count = 0;
76     CmDevice_RT* pDev = nullptr;
77     if (GetCmSupportedAdapters(count) == CM_SUCCESS)
78     {
79         if (adapterIndex <= count)
80         {
81             result = CmDevice_RT::CreateCmDeviceFromAdapter(pDev, adapterIndex, CreateOption);
82 
83             pCmDev = static_cast<CmDevice*>(pDev);
84             if (result == CM_SUCCESS)
85             {
86                 version = CURRENT_CM_VERSION;
87             }
88             else
89             {
90                 version = 0;
91             }
92         }
93         else
94         {
95             result = CM_INVALID_ARG_VALUE;
96         }
97     }
98     return result;
99 }
100 
101 
102 //!
103 //! \brief      Creates CM Device for hardware mode in linux
104 //! \details    Creates a CmDevice from scratch or creates a CmDevice based on the input
105 //!             vaDisplay interface. If CmDevice is created from scratch, an
106 //!             internal vaDisplay interface will be created by the runtime.
107 //!             Creation of more than one CmDevice for concurrent use is supported.
108 //!             The CM API version supported by the library will be returned
109 //!             in parameter version.
110 //! \param      [out] device
111 //!             Reference to the pointer to the CmDevice to be created
112 //! \param      [out] version
113 //!             Reference to CM API version supported by the runtime library
114 //! \param      [in] vaDisplay
115 //!             Reference to a given VADisplay from vaInitialize if NOT nullptr
116 //! \retval     CM_SUCCESS if device successfully created
117 //! \retval     CM_OUT_OF_HOST_MEMORY if out of system memory
118 //! \retval     CM_INVALID_LIBVA_INITIALIZE if vaInitialize failed
119 //! \retval     CM_FAILURE otherwise
120 //!
121 extern "C"
CreateCmDevice(CmDevice * & device,uint32_t & version,VADisplay vaDisplay=nullptr)122 CM_RT_API int32_t CreateCmDevice(CmDevice* &device, uint32_t & version, VADisplay vaDisplay = nullptr)
123 {
124     INSERT_PROFILER_RECORD();
125 
126     CmDevice_RT* pDev = nullptr;
127     int32_t result = CM_FAILURE;
128     uint32_t count = 0;
129 
130     if (GetCmSupportedAdapters(count) == CM_SUCCESS)
131     {
132         if (count > 0)
133         {
134             if (vaDisplay == nullptr)
135             {
136                 result = CmDevice_RT::Create(pDev, CM_DEVICE_CREATE_OPTION_DEFAULT);
137             }
138             else
139             {
140                 result = CmDevice_RT::Create(vaDisplay, pDev, CM_DEVICE_CREATE_OPTION_DEFAULT);
141             }
142 
143             device = static_cast<CmDevice*>(pDev);
144             if (result == CM_SUCCESS)
145             {
146                 version = CURRENT_CM_VERSION;
147             }
148             else
149             {
150                 version = 0;
151             }
152         }
153     }
154     return result;
155 }
156 
157 
158 //!
159 //! \brief      Creates CM Device according to user specified config for hardware mode in linux
160 //! \details    This API is supported from Cm 4.0. The definition of DevCreateOption is described
161 //!             in below table. Scratch memory space is used to provide the memory for kernel's
162 //!             spill code per thread. If there is no spill code exists in kernel, it is wise to
163 //!             disable scratch space in device creation.The benefit is that video memory could
164 //!             be saved and the time spent on device creation could be shortened.The default
165 //!             scratch space size for HSW is 128K per hardware thread.If the kernel does not
166 //!             need such big memory, it is recommended that programmer to specify the
167 //!             scratch space size which kernel actually need by setting the bits[1:3].\n
168 //!       <table>
169 //!             <TR>
170 //!                    <TH> Usage </TH>
171 //!                    <TH> Bits </TH>
172 //!                    <TH> Length </TH>
173 //!                    <TH> Notes </TH>
174 //!             </TR>
175 //!                <TR>
176 //!                    <TD> Flag to disable scratch space </TD>
177 //!                    <TD> [0] </TD>
178 //!                    <TD> 1 </TD>
179 //!                    <TD> 0: enable(default) 1 : disable </TD>
180 //!             </TR>
181 //!                <TR>
182 //!                    <TD> Max scratch space size(HSW only) </TD>
183 //!                    <TD> [1:3] </TD>
184 //!                    <TD> 3 </TD>
185 //!                    <TD> if[1:3] > 0 MaxScratchSpaceSize = [1:3] * 16K;
186 //!                         if[1:3] = 0 MaxScratchSpaceSize=128K(default); </TD>
187 //!             </TR>
188 //!                <TR>
189 //!                    <TD> Max Task Number </TD>
190 //!                    <TD> [4:5] </TD>
191 //!                    <TD> 2 </TD>
192 //!                    <TD> MaxTaskNumber = [4:5] * 4 + 4 </TD>
193 //!       </TR>
194 //!                <TR>
195 //!                    <TD> Reserved on Linux </TD>
196 //!                    <TD> [6] </TD>
197 //!                    <TD> 1 </TD>
198 //!                    <TD> N/A </TD>
199 //!       </TR>
200 //!                <TR>
201 //!                    <TD> media reset enable </TD>
202 //!                    <TD> [7] </TD>
203 //!                    <TD> 1   </TD>
204 //!                    <TD> 1: enable; 0: disable(default) </TD>
205 //!                </TR>
206 //!                <TR>
207 //!                    <TD> adding extra Task Number </TD>
208 //!                    <TD> [8:9] </TD>
209 //!                    <TD> 2 </TD>
210 //!                    <TD> extra task number = [4:5] * (Max task + 1) </TD>
211 //!       </TR>
212 //!                <TR>
213 //!                    <TD> slice shut down enable </TD>
214 //!                    <TD> [10] </TD>
215 //!                    <TD> 1   </TD>
216 //!                    <TD> 1: enable; 0: disble </TD>
217 //!                </TR>
218 //!                <TR>
219 //!                    <TD> surface reuse enable </TD>
220 //!                    <TD> [11] </TD>
221 //!                    <TD> 1   </TD>
222 //!                    <TD> 1: enable; 0: disble </TD>
223 //!                </TR>
224 //!                <TR>
225 //!                    <TD> GPU context enable </TD>
226 //!                    <TD> [12] </TD>
227 //!                    <TD> 1    </TD>
228 //!                    <TD> 1: GPU context enable; 0: GPU context disable (default) </TD>
229 //!                </TR>
230 //!                <TR>
231 //!                    <TD> kernel binary size in GSH </TD>
232 //!                    <TD> [13:20] </TD>
233 //!                    <TD> 8 </TD>
234 //!                    <TD> GSH kernel size is [13:20] * 2MB </TD>
235 //!                </TR>
236 //!                <TR>
237 //!                    <TD> DSH disable  </TD>
238 //!                    <TD> [21] </TD>
239 //!                    <TD> 1 </TD>
240 //!                    <TD> 1: DSH disable; 0: DSH enable (default) </TD>
241 //!                </TR>
242 //!                <TR>
243 //!                    <TD> Disable mid-thread level preemption </TD>
244 //!                    <TD> [22] </TD>
245 //!                    <TD> 1    </TD>
246 //!                    <TD> 1: disable mid-thread preemption; 0: enable (default) </TD>
247 //!                </TR>
248 //!                <TR>
249 //!                    <TD> kernel debug enable </TD>
250 //!                    <TD> [23] </TD>
251 //!                 <TD> 1 </TD>
252 //!                 <TD> 1: enable kernel debug; 0: disable (default) </TD>
253 //!                </TR>
254 //!                <TR>
255 //!                    <TD> Reserved </TD>
256 //!                    <TD> [24:27] </TD>
257 //!                    <TD> 4 </TD>
258 //!                    <TD> Must to be set as Zero </TD>
259 //!                </TR>
260 //!                <TR>
261 //!                    <TD> Disable VEBOX interfaces in CM </TD>
262 //!                    <TD> [28] </TD>
263 //!                    <TD> 1 </TD>
264 //!                    <TD> 1: disable VEBOX interfaces; 0: enable (default) </TD>
265 //!                </TR>
266 //!                <TR>
267 //!                    <TD> Disable FastCopy interfaces in CM </TD>
268 //!                    <TD> [29] </TD>
269 //!                    <TD> 1 </TD>
270 //!                    <TD> 1: disable FastCopy interfaces; 0: enable (default) </TD>
271 //!                </TR>
272 //!                <TR>
273 //!                    <TD> Enabel Fast Path in CM </TD>
274 //!                    <TD> [30] </TD>
275 //!                    <TD> 1 </TD>
276 //!                    <TD> 1: enable fast path; 0: disable (default) </TD>
277 //!                </TR>
278 //!       </table>
279 //!
280 //! \param      [out] device
281 //!             Reference to the pointer to the CmDevice to be created
282 //! \param      [out] version
283 //!             Reference to CM API version supported by the runtime library
284 //! \param      [in] vaDisplay
285 //!             VReference to a given VADisplay from vaInitialize if NOT nullptr
286 //! \param      [in] DevCreateOption
287 //!             Device creation option.
288 //! \retval     CM_SUCCESS if device successfully created
289 //! \retval     CM_OUT_OF_HOST_MEMORY if out of system memory
290 //!    \retval  CM_INVALID_LIBVA_INITIALIZE if vaInitialize failed
291 //! \retval     CM_FAILURE otherwise
292 //!
293 extern "C"
CreateCmDeviceEx(CmDevice * & device,uint32_t & version,VADisplay vaDisplay,uint32_t createOption=CM_DEVICE_CREATE_OPTION_DEFAULT)294 CM_RT_API int32_t CreateCmDeviceEx(CmDevice* &device, uint32_t & version, VADisplay vaDisplay , uint32_t createOption = CM_DEVICE_CREATE_OPTION_DEFAULT )
295 {
296     INSERT_PROFILER_RECORD();
297 
298     CmDevice_RT* pDev = nullptr;
299     int32_t result = CM_FAILURE;
300 
301     if ( vaDisplay == nullptr)
302     {
303         result = CmDevice_RT::Create(pDev, createOption);
304     }
305     else
306     {
307         result = CmDevice_RT::Create(vaDisplay, pDev, createOption);
308     }
309 
310     device = static_cast< CmDevice* >(pDev);
311     if( result == CM_SUCCESS )
312     {
313         version = CURRENT_CM_VERSION;
314     }
315     else
316     {
317         version = 0;
318     }
319 
320     return result;
321 }
322 
323 
324 //!
325 //! \brief      Query the GPU adapter information by adapter index from supported adapter list in hardware mode
326 //! \details    This API is supported from Cm 4.0.  give caller a copy of requested GPU adapter hardware info.
327 //!
328 //! \param      [in] adapterIndex
329 //!             Adapter index number from supported GPU adapter list
330 //! \param      [in] infoName
331 //!             Adapter hardware information type
332 //! \param      [in/out] *info
333 //!             pointer to info from caller to get a copy of specified adapter infomation content
334 //!
335 //! \param      [in] infoSize
336 //!             provide the memroy size of the variable info from the caller
337 //! \param      [out] OutInfoSize
338 //!             pointer to caller provided variable to return actual memroy size of the returned information element
339 //! \retval     CM_SUCCESS if the adapter exists
340 //! \retval     CM_FAILURE otherwise.
341 //!
342 extern "C"
QueryCmAdapterInfo(uint32_t adapterIndex,AdapterInfoType infoName,void * info,uint32_t infoSize,uint32_t * OutInfoSize)343 CM_RT_API int32_t QueryCmAdapterInfo(uint32_t adapterIndex, AdapterInfoType infoName, void *info, uint32_t infoSize, uint32_t *OutInfoSize)
344 {
345     INSERT_PROFILER_RECORD();
346     uint32_t count = 0;
347     int32_t result = CM_FAILURE;
348 
349     if (GetCmSupportedAdapters(count) == CM_SUCCESS)
350     {
351         if (adapterIndex <= count)
352         {
353             result = CmDevice_RT::QueryAdapterInfo(adapterIndex, infoName, info, infoSize, OutInfoSize);
354         }
355     }
356     return result;
357 }
358