1 /*
2 * Copyright (c) 2019, 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_utilities_specific.h
24 //! \brief       This module defines MOS interface on Linux/Android platform
25 //!
26 #ifndef __MOS_UTILITIES_SPECIFIC_H__
27 #define __MOS_UTILITIES_SPECIFIC_H__
28 
29 #include <map>
30 #include <malloc.h>
31 #include "mos_defs.h"
32 #include "media_class_trace.h"
33 
34 #define LINUX_OS_VERSION_FILE                "/proc/version"
35 
36 #define NOT_FOUND            -1
37 
38 #define _aligned_malloc(size, alignment)  memalign(alignment, size)
39 #define _aligned_free(ptr)                free(ptr)
40 
41 typedef void (*MOS_UserFeatureCallback)( void*, bool);
42 
43 class MosMutex;
44 
45 #define USER_FEATURE_KEY_INTERNAL            "UFKEY_INTERNAL\\"
46 #define USER_FEATURE_KEY_EXTERNAL            "UFKEY_EXTERNAL\\"
47 
48 //user feature
49 #if ANDROID_VERSION >= 800
50 #define USER_FEATURE_FILE                   "/data/igfx_user_feature.txt"
51 #else
52 #define USER_FEATURE_FILE                   "/etc/igfx_user_feature.txt"
53 #define USER_FEATURE_FILE_NEXT              "/etc/igfx_user_feature_next.txt"
54 #define USER_FEATURE_FILE_REPORT            "/etc/igfx_user_feature_report.txt"
55 #endif
56 #define UF_KEY_ID                           "[KEY]"
57 #define UF_VALUE_ID                         "[VALUE]"
58 #define UF_CAPABILITY                       64
59 #define MAX_USERFEATURE_LINE_LENGTH         256
60 #define MAX_UF_LINE_STRING_FORMAT           "%255[^\n]\n"
61 
62 #define UF_NONE                             ( 0 )   // No value type
63 #define UF_SZ                               ( 1 )   // Unicode nul terminated string
64 #define UF_EXPAND_SZ                        ( 2 )   // Unicode nul terminated string
65                                                     // (with environment variable references)
66 #define UF_BINARY                           ( 3 )   // Free form binary
67 #define UF_DWORD                            ( 4 )   // 32-bit number
68 #define UF_DWORD_LITTLE_ENDIAN              ( 4 )   // 32-bit number (same as UF_DWORD)
69 #define UF_DWORD_BIG_ENDIAN                 ( 5 )   // 32-bit number
70 #define UF_LINK                             ( 6 )   // Symbolic Link (unicode)
71 #define UF_MULTI_SZ                         ( 7 )   // Multiple Unicode strings
72 #define UF_RESOURCE_LIST                    ( 8 )   // Resource list in the resource map
73 #define UF_FULL_RESOURCE_DESCRIPTOR         ( 9 )   // Resource list in the hardware description
74 #define UF_RESOURCE_REQUIREMENTS_LIST       ( 10 )
75 #define UF_QWORD                            ( 11 )  // 64-bit number
76 #define UF_QWORD_LITTLE_ENDIAN              ( 11 )  // 64-bit number (same as UF_QWORD)
77 
78 #define RRF_RT_UF_NONE                      0x00000001
79 #define RRF_RT_UF_SZ                        0x00000002
80 #define RRF_RT_UF_EXPAND_SZ                 0x00000004
81 #define RRF_RT_UF_BINARY                    0x00000008
82 #define RRF_RT_UF_DWORD                     0x00000010
83 #define RRF_RT_UF_MULTI_SZ                  0x00000020
84 #define RRF_RT_UF_QWORD                     0x00000040
85 
86 #define KEY_READ                             0
87 #define KEY_WRITE                            1
88 
89 #define UFKEY_INTERNAL                       (( uint32_t ) 0x80000001 )
90 #define UFKEY_EXTERNAL                       (( uint32_t ) 0x80000002 )
91 
92 #define UFKEY_INTERNAL_NEXT                   ""
93 #define USER_SETTING_CONFIG_PATH             "[config]"
94 #define USER_SETTING_CONFIG_PERF_PATH        "[config]"
95 #define USER_SETTING_CONFIG_PERMANENT_PATH   "[config]"
96 #define USER_SETTING_REPORT_PATH             "[report]"
97 
98 #define MAX_UF_PATH                 256
99 #define DEFAULT_SUBKEY              "LibVa"
100 #define UFINT_PATH_LINUX            "UFINT"
101 #define UFEXT_PATH_LINUX            "UFEXT"
102 
103 using RegBufferMap = std::map<std::string, std::map<std::string, std::string>>;
104 
105 #define __MEDIA_USER_FEATURE_SUBKEY_INTERNAL                        "LibVa"
106 #define __MEDIA_USER_FEATURE_SUBKEY_PERFORMANCE                     ""
107 #define __MEDIA_USER_FEATURE_SUBKEY_PERMANENT                       ""
108 #define __MEDIA_USER_FEATURE_SUBKEY_REPORT                          "Report"
109 #define __MOS_USER_FEATURE_KEY_XML_FILEPATH_LOCATION                "LibVa"
110 #define __MEDIA_USER_FEATURE_SUBKEY_REPORT_EXTERNAL                 ""
111 
112 #define __MOS_USER_FEATURE_VALUE_ADAPTIVE_TRANSFORM_DECISION_ENABLE_DEFAULT_VALUE "0"
113 
114 //!
115 //! \brief User feature keys to define debug message levels and assertions.
116 //!
117 
118 #if MOS_MESSAGES_ENABLED
119 //!
120 //! \brief Default setting is only critical prints are enabled, so users are not bombarded with irrelevant messages
121 //!
122 #define __MOS_USER_FEATURE_KEY_MESSAGE_DEFAULT_VALUE                (0x1)
123 #endif // MOS_MESSAGES_ENABLED
124 
125 typedef struct _MOS_UF_VALUE {
126     char              pcValueName[MAX_USERFEATURE_LINE_LENGTH];
127     uint32_t          ulValueLen;
128     void              *ulValueBuf;
129     uint32_t          ulValueType;
130 } MOS_UF_VALUE;
131 
132 typedef struct _MOS_UF_KEY {
133     void              *UFKey;
134     char              pcKeyName[MAX_USERFEATURE_LINE_LENGTH];
135     int32_t           valueNum;
136     MOS_UF_VALUE     *pValueArray;
137 } MOS_UF_KEY;
138 
139 typedef struct _MOS_UF_KEY_NODE_T
140 {
141     MOS_UF_KEY*                 pElem;
142     struct _MOS_UF_KEY_NODE_T*  pNext;
143 } MOS_UF_KEYNODE;
144 
145 typedef MOS_UF_KEYNODE* MOS_PUF_KEYLIST;
146 
147 class MosUtilitiesSpecificNext
148 {
149 public:
150 
151 /*----------------------------------------------------------------------------
152 | Name      : MosUserFeatureSetValueExFile
153 | Purpose   : Sets the data and type of a specified value under a user feature key.
154 | Arguments : UFKey        [in] A handle to an open user feature key.
155 |             lpValueName  [in] The name of the user feature value.
156 |             Reserved      in] This parameter is reserved and must be NULL.
157 |             dwType       [in] The type of data pointed to by the lpData
158 |                               parameter.
159 |             lpData       [in] The data to be stored.
160 |             cbData       [in] The size of the information pointed to by the
161 |                               lpData parameter, in bytes.
162 | Returns   : If the function succeeds, the return value is MOS_STATUS_SUCCESS.
163 |             If the function fails, the return value is a error code defined
164 |             in mos_utilities.h.
165 | Comments  :
166 \---------------------------------------------------------------------------*/
167 static MOS_STATUS MosUserFeatureSetValueExFile(
168     void            *UFKey,
169     const char      *lpValueName,
170     uint32_t        Reserved,
171     uint32_t        dwType,
172     uint8_t         *lpData,
173     uint32_t        cbData);
174 
175 
176 /*----------------------------------------------------------------------------
177 | Name      : MosUserFeatureGetValueFile
178 | Purpose   : Retrieves the type and data for the specified user feature value.
179 | Arguments : UFKey     [in]  A handle to an open user feature key.
180 |             lpSubKey  [in]  The name of the user feature key. This key must be a
181 |                             subkey of the key specified by the UFKey parameter
182 |             lpValue   [in]  The name of the user feature value.
183 |             dwFlags   [in]  Reserved, could be any uint32_t type value
184 |             pdwType   [out] A pointer to a variable that receives a code
185 |                             indicating the type of data stored in the
186 |                             specified value.
187 |             pvData    [out] A pointer to a buffer that receives the value's
188 |                             data.
189 |             pcbData   [out] A pointer to a variable that specifies the size
190 |                             of the buffer pointed to by the pvData parameter,
191 |                             in bytes.
192 | Returns   : If the function succeeds, the return value is MOS_STATUS_SUCCESS.
193 |             If the function fails, the return value is a error code defined
194 |             in mos_utilities.h.
195 | Comments  :
196 \---------------------------------------------------------------------------*/
197 static MOS_STATUS MosUserFeatureGetValueFile(
198     void       *UFKey,
199     const char *lpSubKey,
200     const char *lpValue,
201     uint32_t   dwFlags,
202     uint32_t   *pdwType,
203     void       *pvData,
204     uint32_t   *pcbData);
205 
206 /*----------------------------------------------------------------------------
207 | Name      : MosUserFeatureOpenKeyFile
208 | Purpose   : Opens the specified user feature key.
209 | Arguments : ufKey        [in]  A handle to an open user feature key.
210 |             lpSubKey     [in]  The name of the user feature subkey to be opened.
211 |             ulOptions    [in]  This parameter is reserved and must be zero.
212 |             samDesired   [in]  Reserved, could be any REGSAM type value
213 |             phkResult    [out] A pointer to a variable that receives a handle
214 |                                to the opened key.
215 | Returns   : If the function succeeds, the return value is MOS_STATUS_SUCCESS.
216 |             If the function fails, the return value is a error code defined
217 |             in mos_utilities.h.
218 | Comments  :
219 \---------------------------------------------------------------------------*/
220 static MOS_STATUS MosUserFeatureOpenKeyFile(
221     void       *ufKey,
222     const char *lpSubKey,
223     uint32_t   ulOptions,  // reserved
224     uint32_t   samDesired,
225     void       **phkResult);
226 
227 /*----------------------------------------------------------------------------
228     | Name      : UserFeatureDumpFile
229     | Purpose   : This function read the whole User Feature File and dump User Feature File
230     |             data to key linked list.
231     | Arguments : szFileName         [in]  User Feature File name.
232     |             pKeyList           [out] Key Linked list.
233     | Returns   : MOS_STATUS_SUCCESS           Operation success.
234     |             MOS_STATUS_USER_FEATURE_KEY_READ_FAILED  User Feature File can't be open as read.
235     |             MOS_STATUS_NO_SPACE          no space left for allocate
236     |             MOS_STATUS_UNKNOWN           unknown user feature type found in User Feature File
237     |             MOS_STATUS_INVALID_PARAMETER unknown items found in User Feature File
238     | Comments  :
239     \---------------------------------------------------------------------------*/
240 static MOS_STATUS UserFeatureDumpFile(const char *const szFileName, MOS_PUF_KEYLIST *pKeyList);
241 
242 /*----------------------------------------------------------------------------
243 | Name      : UserFeatureDumpDataToFile
244 | Purpose   : This function dump key linked list data to File.
245 | Arguments : szFileName             [in] A handle to the File.
246 |             pKeyList               [in] Reserved, any LPDWORD type value.
247 | Returns   : MOS_STATUS_SUCCESS                        Operation success.
248 |             MOS_STATUS_USER_FEATURE_KEY_WRITE_FAILED  File can't be written.
249 | Comments  :
250 \---------------------------------------------------------------------------*/
251 
252 static MOS_STATUS UserFeatureDumpDataToFile(const char *szFileName, MOS_PUF_KEYLIST pKeyList);
253 
254 /*----------------------------------------------------------------------------
255 | Name      : UserFeatureFreeKeyList
256 | Purpose   : Free key list
257 | Arguments : pKeyList           [in] key list to be free.
258 | Returns   : None
259 | Comments  :
260 \---------------------------------------------------------------------------*/
261 static void UserFeatureFreeKeyList(MOS_PUF_KEYLIST pKeyList);
262 
263 private:
264 
265     /*----------------------------------------------------------------------------
266     | Name      : UserFeatureFindKey
267     | Purpose   : This function finds a key in keys linked list according to key
268     |             name.
269     | Arguments : pKeyList   [in] Key Linked list.
270     |             pcKeyName  [in] Name to the key to find.
271     | Returns   : Matched uf_key data. otherwise return NULL.
272     | Comments  :
273     \---------------------------------------------------------------------------*/
274     static MOS_UF_KEY*UserFeatureFindKey(MOS_PUF_KEYLIST pKeyList, char * const pcKeyName);
275 
276     /*----------------------------------------------------------------------------
277     | Name      : UserFeatureFindValue
278     | Purpose   : Find a value in values array of a key. Return position in values
279     |             array
280     | Arguments : UFKey        [in] Searched Key node.
281     |             pcValueName  [in] Value name.
282     | Returns   : Matched value No. if it can be found, otherwise, return
283     |             NOT_FOUND(-1);
284     | Comments  :
285     \---------------------------------------------------------------------------*/
286     static int32_t UserFeatureFindValue(MOS_UF_KEY UFKey, char * const pcValueName);
287 
288     /*----------------------------------------------------------------------------
289     | Name      : UserFeatureAdd
290     | Purpose   : Add new key to keys' linked list.
291     | Arguments : pKeyList       [in] Key linked list.
292     |             NewKey         [in] Added new key.
293     | Returns   : MOS_STATUS_SUCCESS            success
294     |             MOS_STATUS_INVALID_PARAMETER  invalid NewKey
295     |             MOS_STATUS_NO_SPACE           no space left for allocate
296     | Comments  :
297     \---------------------------------------------------------------------------*/
298     static MOS_STATUS UserFeatureAdd(MOS_PUF_KEYLIST *pKeyList, MOS_UF_KEY *NewKey);
299 
300     /*----------------------------------------------------------------------------
301     | Name      : UserFeatureSet
302     | Purpose   : This function set a key to the key list.
303     | Arguments : pKeyList          [in] Key linked list.
304     |             NewKey            [in] Set key content.
305     | Returns   : MOS_STATUS_SUCCESS      Operation success.
306     |             MOS_STATUS_UNKNOWN      Can't find key in User Feature File.
307     |             MOS_STATUS_NO_SPACE     no space left for allocate
308     | Comments  :
309     \---------------------------------------------------------------------------*/
310     static MOS_STATUS UserFeatureSet(MOS_PUF_KEYLIST *pKeyList, MOS_UF_KEY NewKey);
311 
312     /*----------------------------------------------------------------------------
313     | Name      : UserFeatureQuery
314     | Purpose   : This function query a key's value and return matched key node
315     |             content just with matched value content.
316     | Arguments : pKeyList      [in] Key linked list.
317     |             NewKey        [in] New key content with matched value.
318     | Returns   : MOS_STATUS_SUCCESS         Operation success.
319     |             MOS_STATUS_UNKNOWN         Can't find key or value in User Feature File.
320     | Comments  :
321     \---------------------------------------------------------------------------*/
322     static MOS_STATUS UserFeatureQuery(MOS_PUF_KEYLIST pKeyList, MOS_UF_KEY *NewKey);
323 
324     static MOS_STATUS UserFeatureReadNextTokenFromFile(FILE *pFile, const char *szFormat, char  *szToken);
325 
326     /*----------------------------------------------------------------------------
327     | Name      : UserFeatureSetValue
328     | Purpose   : Modify or add a value of the specified user feature key.
329     | Arguments : strKey         [in] Pointer to user feature key name.
330     |             pcValueName    [in] Pointer to a string containing the name of
331     |                                 the value to set. If a value with this name
332     |                                 is not already present in the key, the
333     |                                 function adds it to the key.
334     |             uiValueType    [in] Type of information to be stored.
335     |             szValueData    [in] Pointer to a null-terminated string
336     |                                 containing the data to set for the default
337     |                                 value of the specified key
338     |             uiValueDataLen [in] Size of the string pointed to by the
339     |                                 szValueData parameter, not including the
340     |                                 terminating null character, in bytes
341     | Returns   : MOS_STATUS_SUCCESS           function success
342     |             MOS_STATUS_INVALID_PARAMETER invalid paramater
343     |             MOS_STATUS_USER_FEATURE_KEY_READ_FAILED  User Feature File can't be open as read.
344     |             MOS_STATUS_NO_SPACE          no space left for allocate
345     |             MOS_STATUS_UNKNOWN           unknown user feature type found in User Feature File
346     |             MOS_STATUS_INVALID_PARAMETER unknown items found in User Feature File
347     |             MOS_STATUS_USER_FEATURE_KEY_WRITE_FAILED  User Feature File can't be written.
348     | Comments  :
349     \---------------------------------------------------------------------------*/
350     static MOS_STATUS UserFeatureSetValue(
351         char * const        strKey,
352         const char * const  pcValueName,
353         uint32_t            uiValueType,
354         void                *pData,
355         int32_t             nDataSize);
356 
357     /*----------------------------------------------------------------------------
358     | Name      : UserFeatureQueryValue
359     | Purpose   : The QueryValue function retrieves the type and data for a
360     |             specified value name associated with a special user feature key.
361     | Arguments : strKey         [in]  Pointer to user feature key name.
362     |             pcValueName    [in]  Pointer to a string containing the name
363     |                                  of the value to query.
364     |             uiValueType    [out] Output Value's type
365     |             pData          [out] Output value's content
366     |             nDataSize      [out] Output the size of value's content.
367     | Returns   : MOS_STATUS_SUCCESS           function success
368     |             MOS_STATUS_INVALID_PARAMETER invalid paramater
369     |             MOS_STATUS_USER_FEATURE_KEY_READ_FAILED  User Feature File can't be open as read.
370     |             MOS_STATUS_NO_SPACE          no space left for allocate
371     |             MOS_STATUS_UNKNOWN           Can't find key or value in User Feature File.
372     | Comments  :
373     \---------------------------------------------------------------------------*/
374     static MOS_STATUS UserFeatureQueryValue(
375         char * const        strKey,
376         const char * const  pcValueName,
377         uint32_t            *uiValueType,
378         void                *pData,
379         int32_t             *nDataSize);
380 
381     /*----------------------------------------------------------------------------
382     | Name      : UserFeatureGetKeyIdbyName
383     | Purpose   : Get ID of the user feature key bu its name
384     | Arguments : pcKeyName      [in]  Pointer to user feature key name.
385     |             pUFKey         [out] A UFKEY pointer to store returned UFKey
386     | Returns   : If the function succeeds, the return value is MOS_STATUS_SUCCESS.
387     |             If the function fails, the return value is a error code defined
388     |             in mos_utilities.h.
389     | Comments  :
390     \---------------------------------------------------------------------------*/
391     static MOS_STATUS UserFeatureGetKeyIdbyName(const char  *pcKeyName, void **pUFKey);
392 
393     /*----------------------------------------------------------------------------
394     | Name      : UserFeatureGetKeyNamebyId
395     | Purpose   : Get name of the user feature key bu its ID
396     | Arguments : UFKey      [in]  ID of the user feature key
397     |             pcKeyName  [out] To store user feature key name.
398     | Returns   : If the function succeeds, the return value is MOS_STATUS_SUCCESS.
399     |             If the function fails, the return value is a error code defined
400     |             in mos_utilities.h.
401     | Comments  :
402     \---------------------------------------------------------------------------*/
403     static MOS_STATUS UserFeatureGetKeyNamebyId(void  *UFKey, char  *pcKeyName);
404 
405 public:
406     static const char*          m_szUserFeatureFile;
407     static MOS_PUF_KEYLIST      m_ufKeyList;
408     static int32_t              m_mosTraceFd;
409     static uint64_t             m_filterEnv;
410     static uint32_t             m_levelEnv;
411     static const char* const    m_mosTracePath;
412     static std::map<std::string, std::map<std::string, std::string>> m_regBuffer;
413 
414 private:
415     static MosMutex             m_userSettingMutex;
416 MEDIA_CLASS_DEFINE_END(MosUtilitiesSpecificNext)
417 };
418 #endif // __MOS_UTILITIES_SPECIFIC_H__
419