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_kernel_data.cpp 24 //! \brief Contains Class CmKernelData definitions 25 //! 26 27 #include "cm_kernel_data.h" 28 29 #include "cm_device.h" 30 #include "cm_mem.h" 31 32 #define minimum(a,b) (((a)<(b))?(a):(b)) 33 34 namespace CMRT_UMD 35 { 36 //*----------------------------------------------------------------------------- 37 //| Purpose: Create Kernel Data 38 //| Returns: Result of the operation. 39 //*----------------------------------------------------------------------------- Create(CmKernelRT * kernel,CmKernelData * & kernelData)40 int32_t CmKernelData::Create( CmKernelRT* kernel, CmKernelData*& kernelData ) 41 { 42 if(!kernel) 43 { 44 CM_ASSERTMESSAGE("Error: Invalid CmKernel."); 45 return CM_NULL_POINTER; 46 } 47 48 int32_t result = CM_SUCCESS; 49 kernelData = new (std::nothrow) CmKernelData( kernel ); 50 if( kernelData ) 51 { 52 kernelData->Acquire(); 53 result = kernelData->Initialize(); 54 if( result != CM_SUCCESS ) 55 { 56 CmKernelData::Destroy( kernelData ); 57 } 58 } 59 else 60 { 61 CM_ASSERTMESSAGE("Error: Failed to create CmKernelData due to out of system memory."); 62 result = CM_OUT_OF_HOST_MEMORY; 63 } 64 return result; 65 } 66 67 //*----------------------------------------------------------------------------- 68 //| Purpose: Destroy CM Kernel Data 69 //| Returns: Result of the operation. 70 //*----------------------------------------------------------------------------- Destroy(CmKernelData * & kernelData)71 int32_t CmKernelData::Destroy( CmKernelData* &kernelData ) 72 { 73 if(kernelData) 74 { 75 uint32_t refCount; 76 refCount = kernelData->SafeRelease(); 77 if (refCount == 0) 78 { 79 kernelData = nullptr; 80 } 81 } 82 83 return CM_SUCCESS; 84 } 85 86 //*----------------------------------------------------------------------------- 87 //| Purpose: CM Kernel Data constructor 88 //| Returns: Result of the operation. 89 //*----------------------------------------------------------------------------- CmKernelData(CmKernelRT * kernel)90 CmKernelData::CmKernelData( CmKernelRT* kernel ): 91 m_kerneldatasize( 0 ), 92 m_kernel(kernel), 93 m_refCount(0), 94 m_isInUse(true) 95 { 96 CmSafeMemSet(&m_halKernelParam, 0, sizeof(CM_HAL_KERNEL_PARAM)); 97 m_halKernelParam.samplerHeap = MOS_New( std::list<SamplerParam> ); 98 } 99 100 //*----------------------------------------------------------------------------- 101 //| Purpose: CM Kernel Data Destructor 102 //| Returns: Result of the operation. 103 //*----------------------------------------------------------------------------- ~CmKernelData(void)104 CmKernelData::~CmKernelData( void ) 105 { 106 //Free memory space for kernel arguments 107 for(uint32_t i = 0; i< m_halKernelParam.numArgs; i++) 108 { 109 MosSafeDeleteArray(m_halKernelParam.argParams[i].firstValue); 110 } 111 for(uint32_t i = m_halKernelParam.numArgs; i< minimum(m_halKernelParam.numArgs+6, CM_MAX_ARGS_PER_KERNEL); i++) 112 { 113 MosSafeDeleteArray(m_halKernelParam.argParams[i].firstValue); 114 } 115 116 //Free memory for indirect data 117 MosSafeDeleteArray(m_halKernelParam.indirectDataParam.indirectData); 118 MosSafeDeleteArray(m_halKernelParam.indirectDataParam.surfaceInfo); 119 120 //Free memory for thread space param 121 MosSafeDeleteArray(m_halKernelParam.kernelThreadSpaceParam.dispatchInfo.numThreadsInWave); 122 MosSafeDeleteArray(m_halKernelParam.kernelThreadSpaceParam.threadCoordinates); 123 124 // Free memory for move instructions 125 MosSafeDeleteArray(m_halKernelParam.movInsData); 126 127 //Frees memory for sampler heap 128 MosSafeDelete(m_halKernelParam.samplerHeap); 129 } 130 131 //*----------------------------------------------------------------------------- 132 //| Purpose: Do nothing in initialization 133 //| Returns: CM_SUCCESS 134 //*----------------------------------------------------------------------------- Initialize(void)135 int32_t CmKernelData::Initialize( void ) 136 { 137 138 return CM_SUCCESS; 139 } 140 141 //*----------------------------------------------------------------------------- 142 //| Purpose: Get Kernel Data pointer 143 //| Returns: Result of the operation. 144 //*----------------------------------------------------------------------------- GetCmKernel(CmKernelRT * & kernel)145 int32_t CmKernelData::GetCmKernel( CmKernelRT*& kernel ) 146 { 147 kernel = m_kernel; 148 return CM_SUCCESS; 149 } 150 151 //*----------------------------------------------------------------------------- 152 //| Purpose: SET Kernel Data pointer 153 //| Returns: Result of the operation. 154 //*----------------------------------------------------------------------------- SetKernelDataSize(int32_t value)155 int32_t CmKernelData::SetKernelDataSize(int32_t value) 156 { 157 m_kerneldatasize = value; 158 return CM_SUCCESS; 159 } 160 161 //*----------------------------------------------------------------------------- 162 //| Purpose: Get Kernel Data pointer 163 //| Returns: Result of the operation. 164 //*----------------------------------------------------------------------------- GetKernelDataSize()165 int32_t CmKernelData::GetKernelDataSize() 166 { 167 return m_kerneldatasize; 168 } 169 170 //*----------------------------------------------------------------------------- 171 //| Purpose: Increase Reference count 172 //| Returns: Result of the operation. 173 //*----------------------------------------------------------------------------- Acquire(void)174 uint32_t CmKernelData::Acquire( void ) 175 { 176 ++m_refCount; 177 178 m_isInUse = true; // reused or created 179 180 return m_refCount; 181 } 182 183 //*----------------------------------------------------------------------------- 184 //| Purpose: Destroy of Cm kernel data 185 //| Returns: Result of the operation. 186 //*----------------------------------------------------------------------------- SafeRelease()187 uint32_t CmKernelData::SafeRelease( ) 188 { 189 190 --m_refCount; 191 if( m_refCount == 0 ) 192 { 193 delete this; 194 return 0; 195 } 196 else 197 { 198 return m_refCount; 199 } 200 } 201 202 //*----------------------------------------------------------------------------- 203 //| Purpose: Get HalCmKernelData 204 //| Returns: Result of the operation. 205 //*----------------------------------------------------------------------------- GetHalCmKernelData()206 PCM_HAL_KERNEL_PARAM CmKernelData::GetHalCmKernelData( ) 207 { 208 return &m_halKernelParam; 209 } 210 211 //*----------------------------------------------------------------------------- 212 //| Purpose: Whether the kernel data is in use 213 //| Returns: Result of the operation. 214 //*----------------------------------------------------------------------------- IsInUse()215 bool CmKernelData::IsInUse() 216 { 217 return m_isInUse; 218 } 219 220 //*----------------------------------------------------------------------------- 221 //| Purpose: Get Curbe Size from kernel data 222 //| Returns: Result of the operation. 223 //*----------------------------------------------------------------------------- GetKernelCurbeSize(void)224 uint32_t CmKernelData::GetKernelCurbeSize( void ) 225 { 226 return m_halKernelParam.totalCurbeSize; 227 } 228 229 //*----------------------------------------------------------------------------- 230 //| Purpose: Reset Kernel data status to IDLE. It will be called once kernel data flushed. 231 //| Returns: CM_SUCCESS. 232 //*----------------------------------------------------------------------------- ResetStatus(void)233 int32_t CmKernelData::ResetStatus( void ) 234 { 235 m_isInUse = false; 236 237 return CM_SUCCESS; 238 } 239 } // namespace 240