xref: /aosp_15_r20/external/intel-media-driver/media_driver/linux/common/cm/hal/osservice/cm_mem_os.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2007-2020, 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      cm_mem_os.h
24 //! \brief     Contains CM memory function definitions
25 //!
26 #pragma once
27 
28 #include <iostream>
29 #include "cpuid.h"
30 #include <smmintrin.h>
31 
32 typedef uintptr_t           UINT_PTR;
33 #define __fastcall
34 #define __noop
35 
36 #ifdef __try
37     #undef __try
38 #endif
39 #define __try           try
40 
41 #ifdef __except
42     #undef __except
43 #endif
44 
45 #define  EXCEPTION_EXECUTE_HANDLER std::exception const& e
46 
47 #define __except(e)     catch(e)
48 
49 #ifndef __EXCEPTIONS
50 // If -fno-exceptions, transform error handling code to work without it
51 #define NO_EXCEPTION_HANDLING  1
52 #endif
53 
54 #if NO_EXCEPTION_HANDLING || ANDROID
55     #define try         if (true)
56 
57     #ifndef catch
58     #define catch(x)    if (false)
59     #endif
60 
61     #define throw(...)
62 #endif
63 
64 #define CM_CPU_FASTCOPY_THRESHOLD 1024
65 
66 /*****************************************************************************\
67 Inline Function:
68     Prefetch
69 
70 Description:
71     executes __asm prefetchnta
72 \*****************************************************************************/
Prefetch(const void * ptr)73 inline void Prefetch( const void* ptr )
74 {
75     __asm__("prefetchnta 0(%0)"::"r"(ptr));
76 }
77 
TestSSE4_1(void)78 inline bool TestSSE4_1( void )
79 {
80     bool success = true;
81 
82 #ifndef NO_EXCEPTION_HANDLING
83     __try
84     {
85 #endif //NO_EXCEPTION_HANDLING
86 
87     if ( sizeof(void *) == 4)  //32-bit Linux
88     {
89         __asm__ __volatile__ (".byte 0x66");
90         __asm__ __volatile__ (".byte 0x0f");
91         __asm__ __volatile__ (".byte 0x38");
92         __asm__ __volatile__ (".byte 0x17");
93         __asm__ __volatile__ (".byte 0xc1");
94     }
95     else //64-bit Linux
96     {
97         success = true;
98     }
99 
100 #ifndef NO_EXCEPTION_HANDLING
101     }
102     __except( EXCEPTION_EXECUTE_HANDLER )
103     {
104         success = false;
105     }
106 #endif // NO_EXCEPTION_HANDLING
107     return success;
108 }
109 
110 /*****************************************************************************\
111 Inline Function:
112     GetCPUID
113 
114 Description:
115     Retrieves cpu information and capabilities supported
116 Input:
117     int infoType - type of information requested
118 Output:
119     int cpuInfo[4] - requested info
120 \*****************************************************************************/
GetCPUID(int cpuInfo[4],int infoType)121 inline void GetCPUID(int cpuInfo[4], int infoType)
122 {
123 #ifndef NO_EXCEPTION_HANDLING
124     __try
125     {
126 #endif //NO_EXCEPTION_HANDLING
127 
128     __get_cpuid(infoType, (unsigned int*)cpuInfo, (unsigned int*)cpuInfo + 1, (unsigned int*)cpuInfo + 2, (unsigned int*)cpuInfo + 3);
129 
130 #ifndef NO_EXCEPTION_HANDLING
131     }
132     __except( EXCEPTION_EXECUTE_HANDLER )
133     {
134         // cpuid failed!
135         return;
136     }
137 #endif  //NO_EXCEPTION_HANDLING
138 }
139 
140 void CmFastMemCopyFromWC( void* dst, const void* src, const size_t bytes, CPU_INSTRUCTION_LEVEL cpuInstructionLevel );
141