1 /*
2  * Copyright � 2014 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
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *     Wei Lin<wei.w.lin@intel.com>
26  *     Yuting Yang<yuting.yang@intel.com>
27  */
28 
29 #ifndef __GENOS_UTILITIES_H__
30 #define __GENOS_UTILITIES_H__
31 
32 #include "os_defs.h"
33 
34 #include <va/va.h>
35 typedef VAStatus GENOS_OSRESULT;
36 #include <stdarg.h>
37 
38 #define GENOS_USER_CONTROL_MIN_DATA_SIZE	128
39 #define GENOS_USER_CONTROL_MAX_DATA_SIZE	2048
40 #define REG_FILE				"/etc/igfx_registry.txt"
41 
42 typedef enum _GENOS_USER_FEATURE_VALUE_ID {
43 	__GENOS_USER_FEATURE_KEY_INVALID_ID,
44 	__GENOS_USER_FEATURE_KEY_MESSAGE_OS_TAG,
45 	__GENOS_USER_FEATURE_KEY_BY_SUB_COMPONENT_OS,
46 	__GENOS_USER_FEATURE_KEY_SUB_COMPONENT_OS_TAG,
47 	__GENOS_USER_FEATURE_KEY_MESSAGE_HW_TAG,
48 	__GENOS_USER_FEATURE_KEY_BY_SUB_COMPONENT_HW,
49 	__GENOS_USER_FEATURE_KEY_SUB_COMPONENT_HW_TAG,
50 	__GENOS_USER_FEATURE_KEY_MESSAGE_DDI_TAG,
51 	__GENOS_USER_FEATURE_KEY_BY_SUB_COMPONENT_DDI,
52 	__GENOS_USER_FEATURE_KEY_SUB_COMPONENT_DDI_TAG,
53 	__GENOS_USER_FEATURE_KEY_MESSAGE_CM_TAG,
54 	__GENOS_USER_FEATURE_KEY_BY_SUB_COMPONENT_CM,
55 	__GENOS_USER_FEATURE_KEY_SUB_COMPONENT_CM_TAG,
56 } GENOS_USER_FEATURE_VALUE_ID;
57 
58 typedef enum
59 {
60 	GENOS_USER_FEATURE_TYPE_INVALID,
61 	GENOS_USER_FEATURE_TYPE_USER,
62 	GENOS_USER_FEATURE_TYPE_SYSTEM,
63 } GENOS_USER_FEATURE_TYPE, *PGENOS_USER_FEATURE_TYPE;
64 
65 typedef enum
66 {
67 	GENOS_USER_FEATURE_VALUE_TYPE_INVALID,
68 	GENOS_USER_FEATURE_VALUE_TYPE_BINARY,
69 	GENOS_USER_FEATURE_VALUE_TYPE_BOOL,
70 	GENOS_USER_FEATURE_VALUE_TYPE_INT32,
71 	GENOS_USER_FEATURE_VALUE_TYPE_INT64,
72 	GENOS_USER_FEATURE_VALUE_TYPE_UINT32,
73 	GENOS_USER_FEATURE_VALUE_TYPE_UINT64,
74 	GENOS_USER_FEATURE_VALUE_TYPE_FLOAT,
75 	GENOS_USER_FEATURE_VALUE_TYPE_STRING,
76 	GENOS_USER_FEATURE_VALUE_TYPE_MULTI_STRING,
77 } GENOS_USER_FEATURE_VALUE_TYPE, *PGENOS_USER_FEATURE_VALUE_TYPE;
78 
79 typedef enum
80 {
81 	GENOS_USER_FEATURE_NOTIFY_TYPE_INVALID,
82 	GENOS_USER_FEATURE_NOTIFY_TYPE_VALUE_CHANGE,
83 } GENOS_USER_FEATURE_NOTIFY_TYPE, *PGENOS_USER_FEATURE_NOTIFY_TYPE;
84 
85 typedef struct
86 {
87 	PCHAR   pStringData;
88 	DWORD   uMaxSize;
89 	DWORD   uSize;
90 } GENOS_USER_FEATURE_VALUE_STRING, *PGENOS_USER_FEATURE_VALUE_STRING;
91 
92 typedef struct
93 {
94 	PCHAR					pMultStringData;
95 	DWORD					uMaxSize;
96 	DWORD					uSize;
97 	PGENOS_USER_FEATURE_VALUE_STRING	pStrings;
98 	DWORD					uCount;
99 } GENOS_USER_FEATURE_VALUE_MULTI_STRING, *PGENOS_USER_FEATURE_VALUE_MULTI_STRING;
100 
101 typedef struct
102 {
103 	PUCHAR  pBinaryData;
104 	DWORD   uMaxSize;
105 	DWORD   uSize;
106 } GENOS_USER_FEATURE_VALUE_BINARY, *PGENOS_USER_FEATURE_VALUE_BINARY;
107 
108 typedef struct _GENOS_USER_FEATURE_VALUE_DATA
109 {
110 	union
111 	{
112 		BOOL					bData;
113 		DWORD					u32Data;
114 		QWORD					u64Data;
115 		INT32					i32Data;
116 		INT64					i64Data;
117 		FLOAT					fData;
118 		GENOS_USER_FEATURE_VALUE_STRING		StringData;
119 		GENOS_USER_FEATURE_VALUE_MULTI_STRING	MultiStringData;
120 		GENOS_USER_FEATURE_VALUE_BINARY		BinaryData;
121 	};
122 	INT32						i32DataFlag;
123 } GENOS_USER_FEATURE_VALUE_DATA, *PGENOS_USER_FEATURE_VALUE_DATA;
124 
125 typedef struct _GENOS_USER_FEATURE_VALUE_INFO
126 {
127 	PCHAR   pcName;  //store name for the bitmask/enum values
128 	DWORD   Value;
129 } GENOS_USER_FEATURE_VALUE_INFO, *PGENOS_USER_FEATURE_VALUE_INFO;
130 
131 typedef struct
132 {
133 	GENOS_USER_FEATURE_VALUE_ID	ValueID;
134 	PCCHAR				pValueName;
135 	PCCHAR				pcGroup;	//!< Reg key group - eg: MediaSolo, GENOS, Codec
136 	PCCHAR				pcPath;		//!< Reg Key Read Path
137 	PCCHAR				pcWritePath;	//!< Reg Key Write Path
138 	GENOS_USER_FEATURE_TYPE		Type;		//!< Reg Key User Feature type - eg: System, User
139 	GENOS_USER_FEATURE_VALUE_TYPE	ValueType;	//!< Reg key type - eg: bool,dword
140 	PCCHAR				DefaultValue;	//!< Reg value
141 	PCCHAR				pcDescription;	//<! Reg key description
142 	BOOL				bExists;	//<! Set if the reg key is defined in the windows reg key manager
143 	UINT				uiNumOfValues;	//<! Number of valid Reg Key values. Useful for reg keys of type bitmask and enum
144 	PGENOS_USER_FEATURE_VALUE_INFO	pValueInfo;	//<! Store information of all valid enum/bit mask values and names
145 	GENOS_STATUS (*pfnSetDefaultValueData)(PGENOS_USER_FEATURE_VALUE_DATA pValueData);
146 	union
147 	{
148 		BOOL					bData;
149 		DWORD					u32Data;
150 		QWORD					u64Data;
151 		INT32					i32Data;
152 		INT64					i64Data;
153 		FLOAT					fData;
154 		GENOS_USER_FEATURE_VALUE_STRING		StringData;
155 		GENOS_USER_FEATURE_VALUE_MULTI_STRING	MultiStringData;
156 		GENOS_USER_FEATURE_VALUE_BINARY		BinaryData;
157 	};
158 	GENOS_USER_FEATURE_VALUE_DATA			Value;			   //!< Reg value
159 } GENOS_USER_FEATURE_VALUE, *PGENOS_USER_FEATURE_VALUE;
160 
161 typedef struct
162 {
163 	PGENOS_USER_FEATURE_VALUE pUserFeatureValue;
164 } GENOS_USER_FEATURE_VALUE_MAP, *PGENOS_USER_FEATURE_VALUE_MAP;
165 
166 typedef struct
167 {
168 	GENOS_USER_FEATURE_TYPE		Type;		//!< User Feature Type
169 	PCHAR				pPath;		//!< User Feature Path
170 	GENOS_USER_FEATURE_NOTIFY_TYPE	NotifyType;	//!< Notification Type
171 	BOOL				bTriggered;	//!< Notification is triggered or not
172 	PVOID				pHandle;	//!< OS Specific Handle
173 } GENOS_USER_FEATURE_NOTIFY_DATA, *PGENOS_USER_FEATURE_NOTIFY_DATA;
174 
175 
176 //!
177 //! \brief User Feature Interface
178 //!
179 typedef struct
180 {
181 	GENOS_USER_FEATURE_TYPE		Type;		//!< User Feature Type
182 	PCCHAR				pPath;		//!< User Feature Path
183 	PGENOS_USER_FEATURE_VALUE	pValues;	//!< Array of User Feature Values
184 	DWORD				uiNumValues;	//!< Number of User Feature Values
185 } GENOS_USER_FEATURE, *PGENOS_USER_FEATURE;
186 
187 typedef struct _GENOS_USER_FEATURE_INTERFACE *PGENOS_USER_FEATURE_INTERFACE;
188 typedef struct _GENOS_USER_FEATURE_INTERFACE
189 {
190 	PVOID pOsInterface;				//!< Pointer to OS Interface
191 	BOOL bIsNotificationSupported;			//!< Whether Notification feature is supported
192 	const GENOS_USER_FEATURE *pUserFeatureInit;	//!< Initializer for Os User Feature structure
193 
194 	GENOS_STATUS (* pfnReadValue) (
195 		PGENOS_USER_FEATURE_INTERFACE pOsUserFeatureInterface,
196 		PGENOS_USER_FEATURE pUserFeature,
197 		PCCHAR pValueName,
198 		GENOS_USER_FEATURE_VALUE_TYPE ValueType);
199 
200 	GENOS_STATUS (* pfnWriteValues) (
201 		PGENOS_USER_FEATURE_INTERFACE pOsUserFeatureInterface,
202 		PGENOS_USER_FEATURE pUserFeature);
203 
204 	GENOS_STATUS (* pfnEnableNotification) (
205 		PGENOS_USER_FEATURE_INTERFACE pOsUserFeatureInterface,
206 		PGENOS_USER_FEATURE_NOTIFY_DATA pNotification);
207 
208 	GENOS_STATUS (* pfnDisableNotification) (
209 		PGENOS_USER_FEATURE_INTERFACE pOsUserFeatureInterface,
210 		PGENOS_USER_FEATURE_NOTIFY_DATA pNotification);
211 
212 	GENOS_STATUS (* pfnParsePath) (
213 		PGENOS_USER_FEATURE_INTERFACE pOsUserFeatureInterface,
214 		const PCHAR pInputPath,
215 		PGENOS_USER_FEATURE_TYPE pUserFeatureType,
216 		PPCHAR ppSubPath);
217 
218 } GENOS_USER_FEATURE_INTERFACE;
219 
220 
221 #ifdef __cplusplus
222 extern "C" {
223 #endif
224 
225 	PVOID GENOS_AlignedAllocMemory(SIZE_T size, SIZE_T alignment);
226 
227 	VOID GENOS_AlignedFreeMemory(PVOID ptr);
228 
229 	PVOID GENOS_AllocMemory(SIZE_T size);
230 
231 	PVOID GENOS_AllocAndZeroMemory(SIZE_T size);
232 
233 	VOID GENOS_FreeMemory(PVOID ptr);
234 
235 #define GENOS_FreeMemAndSetNull(ptr)            \
236     GENOS_FreeMemory(ptr);                      \
237     ptr = NULL;
238 
239 #define GENOS_SafeFreeMemory(ptr)               \
240     if (ptr) GENOS_FreeMemory(ptr);             \
241 
242 	VOID GENOS_ZeroMemory(PVOID pDestination, SIZE_T stLength);
243 
244 	VOID GENOS_FillMemory(PVOID pDestination, SIZE_T stLength, UINT8 bFill);
245 
246 	GENOS_STATUS GENOS_GetFileSize(HANDLE hFile,
247 				       PUINT32 lpFileSizeLow,
248 				       PUINT32 lpFileSizeHigh);
249 
250 	GENOS_STATUS GENOS_CreateDirectory(const PCHAR lpPathName);
251 
252 	GENOS_STATUS GENOS_CreateFile(PHANDLE pHandle,
253 				      const PCHAR lpFileName, UINT32 iOpenFlag);
254 
255 	GENOS_STATUS GENOS_ReadFile(HANDLE hFile,
256 				    PVOID lpBuffer,
257 				    UINT32 bytesToRead,
258 				    PUINT32 pbytesRead, PVOID lpOverlapped);
259 
260 	GENOS_STATUS GENOS_WriteFile(HANDLE hFile,
261 				     PVOID lpBuffer,
262 				     UINT32 bytesToWrite,
263 				     PUINT32 pbytesWritten, PVOID lpOverlapped);
264 
265 	GENOS_STATUS GENOS_SetFilePointer(HANDLE hFile,
266 					  INT32 lDistanceToMove,
267 					  PINT32 lpDistanceToMoveHigh,
268 					  INT32 dwMoveMethod);
269 
270 	BOOL GENOS_CloseHandle(HANDLE hObject);
271 
272 	GENOS_STATUS GENOS_AppendFileFromPtr(PCCHAR pFilename,
273 					     PVOID pData, DWORD dwSize);
274 
275 	GENOS_STATUS GENOS_SecureStrcat(PCHAR strDestination,
276 					SIZE_T numberOfElements,
277 					const PCCHAR strSource);
278 
279 	PCHAR GENOS_SecureStrtok(PCHAR strToken,
280 				 PCCHAR strDelimit, PCHAR * contex);
281 
282 	GENOS_STATUS GENOS_SecureStrcpy(PCHAR strDestination,
283 					SIZE_T numberOfElements,
284 					const PCCHAR strSource);
285 
286 	GENOS_STATUS GENOS_SecureMemcpy(PVOID pDestination,
287 					SIZE_T dstLength,
288 					PCVOID pSource, SIZE_T srcLength);
289 
290 	GENOS_STATUS GENOS_SecureFileOpen(PPFILE ppFile,
291 					  PCCHAR filename, PCCHAR mode);
292 
293 	INT32 GENOS_SecureStringPrint(PCHAR buffer,
294 				      SIZE_T bufSize,
295 				      SIZE_T length, const PCCHAR format, ...);
296 
297 	GENOS_STATUS GENOS_SecureVStringPrint(PCHAR buffer,
298 					      SIZE_T bufSize,
299 					      SIZE_T length,
300 					      const PCCHAR format,
301 					      va_list var_args);
302 	GENOS_STATUS GENOS_LoadLibrary(const PCCHAR lpLibFileName,
303 				       PHMODULE phModule);
304 
305 	BOOL GENOS_FreeLibrary(HMODULE hLibModule);
306 
307 	PVOID GENOS_GetProcAddress(HMODULE hModule, PCCHAR lpProcName);
308 
309 	INT32 GENOS_GetPid();
310 
311 	BOOL GENOS_QueryPerformanceFrequency(PUINT64 pFrequency);
312 
313 	BOOL GENOS_QueryPerformanceCounter(PUINT64 pPerformanceCount);
314 
315 	VOID GENOS_Sleep(UINT32 mSec);
316 
317 	HANDLE GENOS_CreateEventEx(PVOID lpEventAttributes,
318 				   PCHAR lpName, UINT32 dwFlags);
319 
320 	BOOL GENOS_RegisterWaitForSingleObject(PHANDLE phNewWaitObject,
321 					       HANDLE hObject,
322 					       PVOID Callback, PVOID Context);
323 
324 	BOOL GENOS_UnregisterWaitEx(HANDLE hWaitHandle);
325 
326 	DWORD GENOS_GetLogicalCoreNumber();
327 
328 	GENOS_THREADHANDLE GENOS_CreateThread(PVOID ThreadFunction,
329 					      PVOID ThreadData);
330 
331 	UINT GENOS_GetThreadId(GENOS_THREADHANDLE hThread);
332 
333 	UINT GENOS_GetCurrentThreadId();
334 
335 	PGENOS_MUTEX GENOS_CreateMutex();
336 
337 	HRESULT GENOS_DestroyMutex(PGENOS_MUTEX pMutex);
338 
339 	HRESULT GENOS_LockMutex(PGENOS_MUTEX pMutex);
340 
341 	HRESULT GENOS_UnlockMutex(PGENOS_MUTEX pMutex);
342 
343 	PGENOS_SEMAPHORE GENOS_CreateSemaphore(UINT uiInitialCount,
344 					       UINT uiMaximumCount);
345 
346 	HRESULT GENOS_DestroySemaphore(PGENOS_SEMAPHORE pSemaphore);
347 
348 	HRESULT GENOS_WaitSemaphore(PGENOS_SEMAPHORE pSemaphore,
349 				    UINT uiMilliseconds);
350 
351 	HRESULT GENOS_PostSemaphore(PGENOS_SEMAPHORE pSemaphore,
352 				    UINT uiPostCount);
353 
354 	UINT GENOS_WaitForSingleObject(PVOID pObject, UINT uiMilliseconds);
355 
356 	UINT GENOS_WaitForMultipleObjects(UINT uiThreadCount,
357 					  VOID ** ppObjects,
358 					  UINT bWaitAll, UINT uiMilliseconds);
359 
360 	GENOS_OSRESULT GENOS_StatusToOsResult(GENOS_STATUS eStatus);
361 
362 	GENOS_STATUS OsResultToGENOS_Status(GENOS_OSRESULT eResult);
363 
364 #ifdef __cplusplus
365 }
366 #endif
367 #endif
368