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 mos_graphicsresource_next.cpp
24 //! \brief Container class for the basic gpu resource
25 //!
26
27 #include <string>
28 #include <fcntl.h>
29 #include <tuple>
30 #include "mos_graphicsresource_next.h"
31 #include "mos_graphicsresource_specific_next.h"
32 #include "mos_interface.h"
33 #include "mos_util_debug.h"
34
35 uint32_t GraphicsResourceNext::m_memAllocCounterGfx = 0;
36
CreateParams(PMOS_ALLOC_GFXRES_PARAMS pParams)37 GraphicsResourceNext::CreateParams::CreateParams(PMOS_ALLOC_GFXRES_PARAMS pParams)
38 {
39 m_arraySize = pParams->dwArraySize;
40 m_compressionMode = pParams->CompressionMode;
41 m_depth = pParams->dwDepth;
42 m_format = pParams->Format;
43 m_height = pParams->dwHeight;
44 m_isCompressible = (pParams->bIsCompressible == 1) ? true : false;
45 m_isPersistent = (pParams->bIsPersistent == 1) ? true : false;
46 if (pParams->pBufName != nullptr)
47 {
48 m_name = pParams->pBufName;
49 }
50 m_pSystemMemory = pParams->pSystemMemory;
51 m_tileType = pParams->TileType;
52 m_tileModeByForce = pParams->m_tileModeByForce;
53 m_type = pParams->Type;
54 m_flags = pParams->Flags;
55 m_width = pParams->dwWidth;
56 m_memType = pParams->dwMemType;
57
58 if (pParams->ResUsageType == MOS_CODEC_RESOURCE_USAGE_BEGIN_CODEC ||
59 pParams->ResUsageType == MOS_HW_RESOURCE_DEF_MAX) // the usage type is invalid, set to default usage.
60 {
61 m_mocsMosResUsageType = MOS_MP_RESOURCE_USAGE_DEFAULT;
62 }
63 else // the usage is a valid mocs usage or pat index usage
64 {
65 m_mocsMosResUsageType = pParams->ResUsageType;
66 }
67
68 m_gmmResUsageType = MosInterface::GetGmmResourceUsageType(m_mocsMosResUsageType);
69 m_hardwareProtected = pParams->hardwareProtected;
70 };
71
GraphicsResourceNext()72 GraphicsResourceNext::GraphicsResourceNext()
73 {
74 MOS_OS_FUNCTION_ENTER;
75 m_allocationIndexMutex = MosUtilities::MosCreateMutex();
76 MOS_OS_CHK_NULL_NO_STATUS_RETURN(m_allocationIndexMutex);
77 }
78
~GraphicsResourceNext()79 GraphicsResourceNext::~GraphicsResourceNext()
80 {
81 MOS_OS_FUNCTION_ENTER;
82 MosUtilities::MosDestroyMutex(m_allocationIndexMutex);
83 m_allocationIndexMutex = nullptr;
84 }
85
Dump(OsContextNext * osContextPtr,uint32_t overrideOffset,uint32_t overrideSize,std::string outputFileName,std::string outputPath)86 MOS_STATUS GraphicsResourceNext::Dump(OsContextNext* osContextPtr, uint32_t overrideOffset, uint32_t overrideSize, std::string outputFileName, std::string outputPath)
87 {
88 MOS_OS_FUNCTION_ENTER;
89
90 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
91 if (overrideSize == 0)
92 {
93 eStatus = MOS_STATUS_UNKNOWN;
94 return eStatus;
95 }
96
97 char sPath[MOS_MAX_PATH_LENGTH + 1];
98 uint32_t dwWritten = 0;
99 MosUtilities::MosSecureStringPrint(
100 sPath,
101 sizeof(sPath),
102 sizeof(sPath) - 1,
103 "%s%s",
104 outputPath.c_str(),
105 outputFileName.c_str());
106
107 void* hFile = nullptr;
108 // Open file for writing
109 eStatus = MosUtilities::MosCreateFile(
110 &hFile,
111 sPath,
112 O_WRONLY|O_CREAT);
113
114 if (eStatus != MOS_STATUS_SUCCESS)
115 {
116 MOS_OS_ASSERTMESSAGE("Failed to open file '%s'.", sPath);
117 eStatus = MOS_STATUS_FILE_OPEN_FAILED;
118 if (hFile != nullptr)
119 {
120 MosUtilities::MosCloseHandle(hFile);
121 hFile = nullptr;
122 }
123 return eStatus;
124 }
125
126 char* pbData = nullptr;
127 LockParams params = {0};
128 params.m_readRequest = true;
129 pbData = (char *)this->Lock(osContextPtr, params);
130 if (pbData == nullptr)
131 {
132 MOS_OS_ASSERTMESSAGE("Failed to lock the gpu resource");
133 if (hFile != nullptr)
134 {
135 MosUtilities::MosCloseHandle(hFile);
136 hFile = nullptr;
137 }
138 return MOS_STATUS_UNKNOWN;
139 }
140
141 pbData += overrideOffset;
142
143 // Write the file
144 if ((eStatus = MosUtilities::MosWriteFile(
145 hFile,
146 pbData,
147 overrideSize,
148 &dwWritten,
149 nullptr)) != MOS_STATUS_SUCCESS)
150 {
151 MOS_OS_ASSERTMESSAGE("Failed to write to file '%s'.", sPath);
152 if (hFile != nullptr)
153 {
154 MosUtilities::MosCloseHandle(hFile);
155 hFile = nullptr;
156 }
157 }
158
159 eStatus = this->Unlock(osContextPtr);
160 if (eStatus != MOS_STATUS_SUCCESS)
161 {
162 MOS_OS_ASSERTMESSAGE("Failed to unlock the gpu resource");
163 }
164
165 if (hFile != nullptr)
166 {
167 MosUtilities::MosCloseHandle(hFile);
168 hFile = nullptr;
169 }
170
171 return eStatus;
172 }
173
CreateGraphicResource(GraphicsResourceNext::ResourceType resourceType)174 class GraphicsResourceNext *GraphicsResourceNext::CreateGraphicResource(GraphicsResourceNext::ResourceType resourceType)
175 {
176 MOS_OS_FUNCTION_ENTER;
177
178 class GraphicsResourceNext* pResource = nullptr;
179
180 switch (resourceType)
181 {
182 case osSpecificResource:
183 pResource = MOS_New(GraphicsResourceSpecificNext);
184 break;
185 default:
186 MOS_OS_ASSERTMESSAGE("Unknown Graphic Reosurce type %u passed in", resourceType);
187 pResource = nullptr;
188 }
189
190 return pResource;
191 }
192
GetAllocationIndex(GPU_CONTEXT_HANDLE gpuContextHandle)193 int32_t GraphicsResourceNext::GetAllocationIndex(GPU_CONTEXT_HANDLE gpuContextHandle)
194 {
195 MOS_OS_FUNCTION_ENTER;
196
197 GPU_CONTEXT_HANDLE curGpuContext = 0;
198 int32_t curAllocIndex = MOS_INVALID_ALLOC_INDEX;
199 int32_t ret = MOS_INVALID_ALLOC_INDEX;
200
201 MosUtilities::MosLockMutex(m_allocationIndexMutex);
202 for (auto& curAllocationIndexTp : m_allocationIndexArray)
203 {
204 std::tie(curGpuContext, curAllocIndex) = curAllocationIndexTp ;
205 if (curGpuContext == gpuContextHandle)
206 {
207 ret = curAllocIndex;
208 break;
209 }
210 }
211
212 MosUtilities::MosUnlockMutex(m_allocationIndexMutex);
213 return ret;
214 }
215
ResetResourceAllocationIndex()216 void GraphicsResourceNext::ResetResourceAllocationIndex()
217 {
218 MOS_OS_FUNCTION_ENTER;
219 MosUtilities::MosLockMutex(m_allocationIndexMutex);
220 m_allocationIndexArray.clear();
221 MosUtilities::MosUnlockMutex(m_allocationIndexMutex);
222 }
223
224