| 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 cm_scratch_space.cpp |
| 24 | //! \brief Contains Class CmScratchSpace definitions |
| 25 | //! |
| 26 | #include "cm_scratch_space.h" |
| 27 | #include "cm_device_rt.h" |
| 28 | #include "cm_buffer_rt.h" |
| 29 | #include "cm_kernel_ex.h" |
| 30 | |
| 31 | using namespace CMRT_UMD; |
| 32 | |
| 33 | CmScratchSpace::CmScratchSpace(): |
| 34 | m_device(nullptr), |
| 35 | m_cmhal(nullptr), |
| 36 | m_isSeparated(false), |
| 37 | m_resource(nullptr), |
| 38 | m_scratchSize(0), |
| 39 | m_buffer(nullptr) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | CmScratchSpace::~CmScratchSpace() |
| 44 | { |
| 45 | if (m_device && m_buffer) |
| 46 | { |
| 47 | m_device->DestroySurface(m_buffer); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | MOS_STATUS CmScratchSpace::Initialize(CmDeviceRT *device) |
| 52 | { |
| 53 | CM_CHK_NULL_RETURN_MOSERROR(device); |
| 54 | m_device = device; |
| 55 | CM_CHK_NULL_RETURN_MOSERROR(device->GetAccelData()); |
| 56 | m_cmhal = ((PCM_CONTEXT_DATA)device->GetAccelData())->cmHalState; |
| 57 | CM_CHK_NULL_RETURN_MOSERROR(m_cmhal); |
| 58 | if (m_cmhal->cmHalInterface->IsSeparateScratch()) |
| 59 | { |
| 60 | m_isSeparated = true; |
| 61 | return MOS_STATUS_SUCCESS; |
| 62 | } |
| 63 | return MOS_STATUS_UNIMPLEMENTED; |
| 64 | } |
| 65 | |
| 66 | MOS_STATUS CmScratchSpace::Allocate(CmKernelEx **kernels, uint32_t count) |
| 67 | { |
| 68 | if (!m_isSeparated) |
| 69 | { |
| 70 | return MOS_STATUS_UNIMPLEMENTED; |
| 71 | } |
| 72 | uint32_t maxSpillSize = 0; |
| 73 | for (uint32_t i = 0; i < count; i++) |
| 74 | { |
| 75 | CmKernelEx *kernel = kernels[i]; |
| 76 | // get the spill size |
| 77 | maxSpillSize = MOS_MAX(maxSpillSize, kernel->GetSpillMemUsed()); |
| 78 | } |
| 79 | |
| 80 | if (maxSpillSize == 0) |
| 81 | { |
| 82 | return MOS_STATUS_UNIMPLEMENTED; |
| 83 | } |
| 84 | |
| 85 | uint32_t perThreadScratchSpace = 64; |
| 86 | for (perThreadScratchSpace; perThreadScratchSpace < maxSpillSize; perThreadScratchSpace <<= 1); |
| 87 | |
| 88 | // get max thread number |
| 89 | MEDIA_SYSTEM_INFO *gtSystemInfo = m_cmhal->osInterface->pfnGetGtSystemInfo(m_cmhal->osInterface); |
| 90 | uint32_t numHWThreadsPerEU = gtSystemInfo->ThreadCount / gtSystemInfo->EUCount; |
| 91 | uint32_t maxHWThreads = gtSystemInfo->MaxEuPerSubSlice * numHWThreadsPerEU * gtSystemInfo->MaxSubSlicesSupported; |
| 92 | m_scratchSize = maxHWThreads * perThreadScratchSpace; |
| 93 | |
| 94 | // create the scratch space by CmBuffer |
| 95 | m_device->CreateBuffer(m_scratchSize, m_buffer); |
| 96 | CM_CHK_NULL_RETURN_MOSERROR(m_buffer); |
| 97 | CmBuffer_RT *bufferRT = static_cast<CmBuffer_RT *>(m_buffer); |
| 98 | uint32_t handle = 0; |
| 99 | bufferRT->GetHandle(handle); |
| 100 | m_resource = &m_cmhal->bufferTable[handle].osResource; |
| 101 | |
| 102 | return MOS_STATUS_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | void CmScratchSpace::Submit(uint32_t trackerIndex, uint32_t tracker) |
| 106 | { |
| 107 | if (m_buffer == nullptr || m_scratchSize == 0) |
| 108 | { |
| 109 | return; |
| 110 | } |
| 111 | CmBuffer_RT *bufferRT = static_cast<CmBuffer_RT *>(m_buffer); |
| 112 | bufferRT->SetFastTracker(trackerIndex, tracker); |
| 113 | } |