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 //! \file      cm_program.h
24 //! \brief     Contains Class CmProgram definitions
25 //!
26 
27 #ifndef MEDIADRIVER_AGNOSTIC_COMMON_CM_CMPROGRAM_H_
28 #define MEDIADRIVER_AGNOSTIC_COMMON_CM_CMPROGRAM_H_
29 
30 #include "cm_def.h"
31 #include "cm_array.h"
32 #include "cm_jitter_info.h"
33 #include "cm_visa.h"
34 
35 struct attribute_info_t
36 {
37     unsigned short nameIndex;
38     unsigned char size;
39     unsigned char* values;
40     char *name;
41 };
42 
43 struct gen_var_info_t
44 {
45     unsigned short nameIndex;
46     unsigned char bitProperties;
47     unsigned short numElements;
48     unsigned short aliasIndex;
49     unsigned short aliasOffset;
50     unsigned char attributeCount;
51     attribute_info_t* attributes;
52 } ;
53 
54 struct spec_var_info_t
55 {
56     unsigned short nameIndex;
57     unsigned short numElements;
58     unsigned char attributeCount;
59     attribute_info_t* attributes;
60 };
61 
62 struct label_info_t
63 {
64     unsigned short nameIndex;
65     unsigned char kind;
66     unsigned char attributeCount;
67     attribute_info_t* attributes;
68 };
69 
70 struct CM_KERNEL_INFO
71 {
72     char kernelName[ CM_MAX_KERNEL_NAME_SIZE_IN_BYTE ];
73     uint32_t inputCountOffset;
74 
75     //Used to store the input for the jitter from CISA
76     uint32_t kernelIsaOffset;
77     uint32_t kernelIsaSize;
78 
79     //Binary Size
80     union
81     {
82         uint32_t jitBinarySize;
83         uint32_t genxBinarySize;
84     };
85 
86     union
87     {
88         void* jitBinaryCode;   //pointer to code created by jitter
89         uint32_t genxBinaryOffset; //pointer to binary offset in CISA (use when jit is not enabled)
90     };
91 
92     //Just a copy for original binary pointer and size (GTPin using only)
93     void* origBinary;
94     uint32_t origBinarySize;
95 
96     uint32_t globalStringCount;
97     const char** globalStrings;
98     char kernelASMName[CM_MAX_KERNEL_NAME_SIZE_IN_BYTE + 1];        //The name of the Gen assembly file for this kernel (no extension)
99     uint8_t kernelSLMSize;     //Size of the SLM used by each thread group
100     bool blNoBarrier;       //Indicate if the barrier is used in kernel: true means no barrier used, false means barrier is used.
101 
102     FINALIZER_INFO *jitInfo;
103 
104     uint32_t variableCount;
105     gen_var_info_t *variables;
106     uint32_t addressCount;
107     spec_var_info_t *address;
108     uint32_t predicateCount;
109     spec_var_info_t *predicates;
110     uint32_t labelCount;
111     label_info_t *label;
112     uint32_t samplerCount;
113     spec_var_info_t *sampler;
114     uint32_t surfaceCount;
115     spec_var_info_t *surface;
116     uint32_t vmeCount;
117     spec_var_info_t *vme;
118 
119     uint32_t kernelInfoRefCount;    //reference counter for kernel info to reuse kernel info and jitbinary
120     uint64_t hashValue;
121 };
122 
123 //Function pointer definition for jitter compilation functions.
124 typedef int (__cdecl *pJITCompile)(const char *kernelName,
125                                    const void *kernelIsa,
126                                    uint32_t kernelIsaSize,
127                                    void* &genBinary,
128                                    uint32_t &genBinarySize,
129                                    const char *platform,
130                                    int majorVersion,
131                                    int minorVersion,
132                                    int numArgs,
133                                    const char *args[],
134                                    char *errorMsg,
135                                    FINALIZER_INFO *jitInfo);
136 
137 typedef int (__cdecl *pJITCompile_v2)(const char *kernelName,
138                                    const void *kernelIsa,
139                                    uint32_t kernelIsaSize,
140                                    void* &genBinary,
141                                    uint32_t &genBinarySize,
142                                    const char *platform,
143                                    int majorVersion,
144                                    int minorVersion,
145                                    int numArgs,
146                                    const char *args[],
147                                    char *errorMsg,
148                                    FINALIZER_INFO *jitInfo,
149                                    void *extra_info);
150 
151 typedef void (__cdecl *pFreeBlock)(void*);
152 
153 typedef void (__cdecl *pJITVersion)(unsigned int &majorV,
154                                     unsigned int &minorV);
155 
156 #define CM_JIT_FLAG_SIZE           256
157 #define CM_JIT_ERROR_MESSAGE_SIZE  512
158 #define CM_JIT_PROF_INFO_SIZE      4096
159 #define CM_RT_JITTER_MAX_NUM_FLAGS 30
160 
161 #define JITCOMPILE_FUNCTION_STR "JITCompile"
162 #define JITCOMPILEV2_FUNCTION_STR "JITCompile_v2"
163 #define FREEBLOCK_FUNCTION_STR  "freeBlock"
164 #define JITVERSION_FUNCTION_STR "getJITVersion"
165 
166 namespace CMRT_UMD
167 {
168 class CmDeviceRT;
169 
170 class CmProgram
171 {
172 public:
173     virtual int32_t GetCommonISACode(void* & commonISACode, uint32_t & size) = 0;
174 };
175 
176 //*-----------------------------------------------------------------------------
177 //! CM Program
178 //*-----------------------------------------------------------------------------
179 class CmProgramRT : public CmProgram
180 {
181 public:
182     static int32_t Create( CmDeviceRT* device, void* cisaCode, const uint32_t cisaCodeSize, CmProgramRT*& program,  const char* options, const uint32_t programId );
183     static int32_t Destroy( CmProgramRT* &program );
184 
185     int32_t GetCommonISACode( void* & commonISACode, uint32_t & size );
186     int32_t GetKernelCount( uint32_t& kernelCount );
187     int32_t GetKernelInfo( uint32_t index, CM_KERNEL_INFO*& kernelInfo );
188     int32_t GetIsaFileName( char* & kernelName );
189     int32_t GetKernelOptions( char* & kernelOptions );
190 
191     uint32_t GetSurfaceCount(void);
192     int32_t SetSurfaceCount(uint32_t count);
193 
IsJitterEnabled(void)194     bool IsJitterEnabled( void ){ return m_isJitterEnabled; }
IsHwDebugEnabled(void)195     bool IsHwDebugEnabled (void ){ return m_isHwDebugEnabled;}
196 
197     uint32_t AcquireKernelInfo(uint32_t index);
198     uint32_t ReleaseKernelInfo(uint32_t index);
199     int32_t GetKernelInfoRefCount(uint32_t index, uint32_t& refCount);
200 
201     int32_t GetCISAVersion(uint32_t& majorVersion, uint32_t& minorVersion);
202 
203     int32_t Acquire( void);
204     int32_t SafeRelease( void);
205 
206     uint32_t GetProgramIndex();
207 
GetKernelStartIndex()208     uint32_t GetKernelStartIndex()
209     {
210         return m_kernelIndex;
211     }
212 
213     //! \brief    get m_isaFile object
214     //! \detail   m_isaFile object provides methods to read, parse and write ISA files.
215     //! \return   Pointer to m_isaFile object
216     vISA::ISAfile *getISAfile();
217 
218 protected:
219     CmProgramRT( CmDeviceRT* device, uint32_t programId );
220     ~CmProgramRT( void );
221 
222     int32_t Initialize( void* cisaCode, const uint32_t cisaCodeSize, const char* options );
223 #if USE_EXTENSION_CODE
224     int InitForGTPin(const char *jitFlags[CM_RT_JITTER_MAX_NUM_FLAGS], int &numJitFlags);
225 #endif
226     uint32_t GetKernelInfoHash(CM_KERNEL_INFO *kernelInfo);
227 
228     CmDeviceRT* m_device;
229 
230     uint32_t m_programCodeSize;
231     uint8_t *m_programCode;
232     vISA::ISAfile* m_isaFile;
233     char* m_options;
234     char m_isaFileName[ CM_MAX_ISA_FILE_NAME_SIZE_IN_BYTE ];
235     uint32_t m_surfaceCount;
236 
237     uint32_t m_kernelCount;
238     CmDynamicArray m_kernelInfo;
239 
240     bool m_isJitterEnabled;
241     bool m_isHwDebugEnabled;
242 
243     uint32_t m_refCount;
244 
245     //program array starting slot index
246     uint32_t m_programIndex;
247 
248     //kernel array starting slot index
249     uint32_t m_kernelIndex;
250 
251     //Function point to JIT compiling functions, get from CmDevice_RT or CmDeviceSim
252     pJITCompile     m_fJITCompile;
253     pFreeBlock      m_fFreeBlock;
254     pJITVersion     m_fJITVersion;
255     pJITCompile_v2  m_fJITCompile_v2;
256 
257 public:
258     uint32_t m_cisaMagicNumber;
259     uint8_t m_cisaMajorVersion;
260     uint8_t m_cisaMinorVersion;
261 
262 private:
263     CmProgramRT (const CmProgramRT& other);
264     CmProgramRT& operator= (const CmProgramRT& other);
265 };
266 }; //namespace
267 
268 #endif  // #ifndef MEDIADRIVER_AGNOSTIC_COMMON_CM_CMPROGRAM_H_
269