1 /*
2 * Copyright (c) 2020-2022, 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     media_debug_interface.cpp
24 //! \brief    Defines the debug interface shared by all of Media.
25 //! \details  The debug interface dumps output from Media based on input config file.
26 //!
27 #include "media_debug_interface.h"
28 #if USE_MEDIA_DEBUG_TOOL
29 #include "media_debug_config_manager.h"
30 #include "media_debug_fast_dump.h"
31 #include "codec_hw_next.h"
32 #include <fstream>
33 #include <sstream>
34 #include <iomanip>
35 
36 #if !defined(LINUX) && !defined(ANDROID)
37 #include "UmdStateSeparation.h"
38 #endif
39 
MediaDebugInterface()40 MediaDebugInterface::MediaDebugInterface()
41 {
42     memset(m_fileName, 0, sizeof(m_fileName));
43     memset(m_path, 0, sizeof(m_path));
44     InitCRCTable(m_crcTable);
45 }
46 
~MediaDebugInterface()47 MediaDebugInterface::~MediaDebugInterface()
48 {
49     if (nullptr != m_configMgr)
50     {
51         MOS_Delete(m_configMgr);
52     }
53 
54     if (!Mos_ResourceIsNull(&m_temp2DSurfForCopy.OsResource))
55     {
56         m_osInterface->pfnFreeResource(m_osInterface, &m_temp2DSurfForCopy.OsResource);
57     }
58 }
59 
InitDumpLocation()60 MOS_STATUS MediaDebugInterface::InitDumpLocation()
61 {
62     m_crcGoldenRefFileName = m_outputFilePath + std::string("GoldenReference.txt");
63     if (m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpToThreadFolder))
64     {
65         std::string ThreadSubFolder = "T" + std::to_string(MosUtilities::MosGetCurrentThreadId()) + MOS_DIRECTORY_DELIMITER;
66         m_outputFilePath            = m_outputFilePath + ThreadSubFolder;
67         MosUtilities::MosCreateDirectory(const_cast<char *>(m_outputFilePath.c_str()));
68     }
69 
70     m_ddiFileName = m_outputFilePath + "ddi.par";
71     std::ofstream ofs(m_ddiFileName, std::ios::out);
72     ofs << "ParamFilePath"
73         << " = \"" << m_fileName << "\"" << std::endl;
74     ofs.close();
75 
76     return MOS_STATUS_SUCCESS;
77 }
78 
SetOutputFilePath()79 MOS_STATUS MediaDebugInterface::SetOutputFilePath()
80 {
81     std::string          outputPathKey;
82     std::string          dumpFilePath;
83 
84     MEDIA_DEBUG_FUNCTION_ENTER;
85 
86 #ifdef LINUX
87     char *customizedOutputPath = getenv("MOS_DEBUG_OUTPUT_LOCATION");
88     if (customizedOutputPath != nullptr && strlen(customizedOutputPath) != 0)
89     {
90         m_outputFilePath = customizedOutputPath;
91         m_outputFilePath.erase(m_outputFilePath.find_last_not_of(" \n\r\t") + 1);
92         if (m_outputFilePath[m_outputFilePath.length() - 1] != MOS_DIRECTORY_DELIMITER)
93             m_outputFilePath += MOS_DIRECTORY_DELIMITER;
94     }
95     else
96 #endif
97     {
98         outputPathKey = SetOutputPathKey();
99         ReadUserSettingForDebug(
100             m_userSettingPtr,
101             m_outputFilePath,
102             outputPathKey,
103             MediaUserSetting::Group::Device);
104 
105         if (!m_outputFilePath.empty())
106         {
107             if (m_outputFilePath.back() != MOS_DIRECTORY_DELIMITER)
108             {
109                 m_outputFilePath += MOS_DIRECTORY_DELIMITER;
110             }
111         }
112         else
113         {
114 #if defined(LINUX) || defined(ANDROID)
115             m_outputFilePath = MOS_DEBUG_DEFAULT_OUTPUT_LOCATION;
116 #else
117             // Use state separation APIs to obtain appropriate storage location
118             if (SUCCEEDED(GetDriverPersistentStorageLocation(dumpFilePath)))
119             {
120                 m_outputFilePath = dumpFilePath.c_str();
121                 outputPathKey    = InitDefaultOutput();
122                 ReportUserSettingForDebug(
123                     m_userSettingPtr,
124                     outputPathKey,
125                     m_outputFilePath,
126                     MediaUserSetting::Group::Device);
127             }
128             else
129             {
130                 return MOS_STATUS_UNKNOWN;
131             }
132 #endif
133         }
134     }
135     return MOS_STATUS_SUCCESS;
136 }
137 
PackGoldenReferences(std::initializer_list<std::vector<uint32_t>> goldenReference)138 void MediaDebugInterface::PackGoldenReferences(std::initializer_list<std::vector<uint32_t>> goldenReference)
139 {
140     for (auto beg = goldenReference.begin(); beg != goldenReference.end(); beg++)
141     {
142         m_goldenReferences.push_back(*beg);
143     }
144 }
145 
CaptureGoldenReference(uint8_t * buf,uint32_t size,uint32_t hwCrcValue)146 MOS_STATUS MediaDebugInterface::CaptureGoldenReference(uint8_t *buf, uint32_t size, uint32_t hwCrcValue)
147 {
148     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
149     if (m_enableHwDebugHooks &&
150         !m_goldenReferenceExist)
151     {
152         if (m_swCRC)
153         {
154             if (buf == nullptr)
155             {
156                 return MOS_STATUS_NULL_POINTER;
157             }
158             auto crcVal = CalculateCRC(buf, size, 0);
159             m_crcGoldenReference.push_back(crcVal);
160         }
161         else
162         {
163             m_crcGoldenReference.push_back(hwCrcValue);
164         }
165     }
166     return eStatus;
167 }
168 
FillSemaResource(std::vector<uint32_t * > & vSemaData,std::vector<uint32_t> & data)169 MOS_STATUS MediaDebugInterface::FillSemaResource(std::vector<uint32_t*> &vSemaData, std::vector<uint32_t> &data)
170 {
171     if (vSemaData.size() != data.size())
172     {
173         return MOS_STATUS_INVALID_PARAMETER;
174     }
175     for (uint32_t i = 0; i < vSemaData.size(); i++)
176     {
177         MEDIA_DEBUG_CHK_NULL(vSemaData[i]);
178         *vSemaData[i] = data[i];
179     }
180     return MOS_STATUS_SUCCESS;
181 }
182 
LockResource(uint32_t * semaData,PMOS_RESOURCE reSemaphore)183 MOS_STATUS MediaDebugInterface::LockResource(uint32_t *semaData, PMOS_RESOURCE reSemaphore)
184 {
185     CodechalResLock semaLock(m_osInterface, reSemaphore);
186     semaData = (uint32_t *)semaLock.Lock(CodechalResLock::writeOnly);
187     return MOS_STATUS_SUCCESS;
188 }
189 
LockSemaResource(std::vector<uint32_t * > & vSemaData,std::vector<MOS_RESOURCE> & vResource)190 MOS_STATUS MediaDebugInterface::LockSemaResource(std::vector<uint32_t *> &vSemaData, std::vector<MOS_RESOURCE> &vResource)
191 {
192     for (uint32_t i = 0; i < vResource.size(); i++)
193     {
194         CodechalResLock semaLock(m_osInterface, &vResource[i]);
195         uint32_t *      smData = (uint32_t *)semaLock.Lock(CodechalResLock::writeOnly);
196         LockResource(smData, &vResource[i]);
197         vSemaData.push_back(smData);
198     }
199     return MOS_STATUS_SUCCESS;
200 }
201 
DumpToFile(const GoldenReferences & goldenReferences)202 MOS_STATUS MediaDebugInterface::DumpToFile(const GoldenReferences &goldenReferences)
203 {
204     std::ofstream ofs(m_crcGoldenRefFileName, std::ios_base::out);
205     if (goldenReferences.size() <= 0)
206     {
207         return MOS_STATUS_INVALID_PARAMETER;
208     }
209     for (uint32_t i = 0; i < goldenReferences[0].size(); i++)
210     {
211         for (auto golden : goldenReferences)
212         {
213             ofs << golden[i] << '\t';
214         }
215         ofs << '\n';
216     }
217     ofs.close();
218     return MOS_STATUS_SUCCESS;
219 }
220 
DumpGoldenReference()221 MOS_STATUS MediaDebugInterface::DumpGoldenReference()
222 {
223     if (m_enableHwDebugHooks && !m_goldenReferenceExist && m_goldenReferences.size() > 0)
224     {
225         return DumpToFile(m_goldenReferences);
226     }
227     else
228     {
229         return MOS_STATUS_UNKNOWN;
230     }
231 }
232 
LoadGoldenReference()233 MOS_STATUS MediaDebugInterface::LoadGoldenReference()
234 {
235     std::ifstream ifs(m_crcGoldenRefFileName, std::ios_base::in);
236     std::vector<uint32_t> lines;
237     std::string           str;
238     uint32_t              num;
239     if (!ifs)
240     {
241         return MOS_STATUS_FILE_OPEN_FAILED;
242     }
243     uint32_t crc;
244     while (!ifs.eof())
245     {
246         std::getline(ifs, str);
247         std::stringstream stringin(str);
248         lines.clear();
249         while (stringin >> num)
250         {
251             lines.push_back(num);
252         }
253         m_goldenReferences.push_back(lines);
254     }
255     ifs.close();
256     m_goldenReferences.pop_back();
257     return MOS_STATUS_SUCCESS;
258 }
259 
DumpUncompressedYUVSurface(PMOS_SURFACE surface)260 MOS_STATUS MediaDebugInterface::DumpUncompressedYUVSurface(PMOS_SURFACE surface)
261 {
262     if (surface == nullptr)
263     {
264         MEDIA_DEBUG_ASSERTMESSAGE("Surface for Dump is nullptr");
265         return MOS_STATUS_NULL_POINTER;
266     }
267 
268     MEDIA_DEBUG_CHK_STATUS(ReAllocateSurface(
269         &m_temp2DSurfForCopy,
270         surface,
271         "Temp2DSurfForSurfDumper",
272         MOS_GFXRES_2D,
273         true));
274 
275     MEDIA_DEBUG_VERBOSEMESSAGE("Temp2DSurfaceForCopy width %d, height %d, pitch %d, TileType %d, bIsCompressed %d, CompressionMode %d",
276         m_temp2DSurfForCopy.dwWidth,
277         m_temp2DSurfForCopy.dwHeight,
278         m_temp2DSurfForCopy.dwPitch,
279         m_temp2DSurfForCopy.TileType,
280         m_temp2DSurfForCopy.bIsCompressed,
281         m_temp2DSurfForCopy.CompressionMode);
282 
283     // Copy Original Surface to a Linear/Uncompressed surface, later lock can use m_temp2DSurfForCopy for uncompresed surfaces
284     // if the return is not supported, then the lock should only happen on origianl resource
285     MEDIA_DEBUG_CHK_STATUS(m_osInterface->pfnDoubleBufferCopyResource(
286         m_osInterface,
287         &surface->OsResource,
288         &m_temp2DSurfForCopy.OsResource,
289         false));
290 
291     return MOS_STATUS_SUCCESS;
292 }
293 
294 #define FIELD_TO_OFS(name, shift) ofs << shift #name << ": " << (int64_t)ptr->name << std::endl;
295 #define EMPTY_TO_OFS()
296 #define UNION_STRUCT_START_TO_OFS()     ofs << "union"      << std::endl \
297                                             << "{"          << std::endl \
298                                             << "    struct" << std::endl \
299                                             << "    {"      << std::endl;
300 
301 #define UNION_STRUCT_FIELD_TO_OFS(name) ofs << "        "#name << ": " << ptr->name << std::endl;
302 #define UNION_END_TO_OFS(name)          ofs << "    }"      << std::endl \
303                                             << "    "#name << ": " << ptr->name << std::endl \
304                                             << "}" << std::endl;
305 #define OFFSET_FIELD_TO_OFS(class_name, f_name, shift) << shift "                 "#f_name": " << ptr->class_name.f_name << std::endl
306 #define PLANE_OFFSET_TO_OFS(name) ofs << "MOS_PLANE_OFFSET "#name << std::endl \
307                                                             OFFSET_FIELD_TO_OFS(name, iSurfaceOffset,)    \
308                                                             OFFSET_FIELD_TO_OFS(name, iXOffset,)          \
309                                                             OFFSET_FIELD_TO_OFS(name, iYOffset,)          \
310                                                             OFFSET_FIELD_TO_OFS(name, iLockSurfaceOffset,);
311 #define RESOURCE_OFFSET_TO_OFS(name, shift) ofs << shift "MOS_RESOURCE_OFFSETS "#name << std::endl \
312                                                                                 OFFSET_FIELD_TO_OFS(name, BaseOffset, shift) \
313                                                                                 OFFSET_FIELD_TO_OFS(name, XOffset, shift)    \
314                                                                                 OFFSET_FIELD_TO_OFS(name, YOffset, shift);
315 
316 #define FIELD_TO_OFS_8SHIFT(name) FIELD_TO_OFS(name, "        ")
DumpMosSpecificResourceInfoToOfs(PMOS_RESOURCE pOsResource,std::ofstream & ofs)317 MOS_STATUS MediaDebugInterface::DumpMosSpecificResourceInfoToOfs(
318     PMOS_RESOURCE pOsResource,
319     std::ofstream& ofs)
320 {
321     MEDIA_DEBUG_FUNCTION_ENTER;
322     if (Mos_ResourceIsNull(pOsResource))
323     {
324         MEDIA_DEBUG_ASSERTMESSAGE("pOsResource is null");
325         return MOS_STATUS_INVALID_PARAMETER;
326     }
327 
328     PMOS_RESOURCE ptr = pOsResource;
329     ofs << "MOS_RESOURCE:" << std::endl;
330 #if !defined(LINUX) || defined(WDDM_LINUX)
331     FIELD_TO_OFS(RunTimeHandle, );
332 #else
333     FIELD_TO_OFS(iWidth, );
334     FIELD_TO_OFS(iHeight, );
335     FIELD_TO_OFS(iSize, );
336     FIELD_TO_OFS(iPitch, );
337     FIELD_TO_OFS(iDepth, );
338     FIELD_TO_OFS(Format, );
339     FIELD_TO_OFS(iCount, );
340     FIELD_TO_OFS(dwGfxAddress, );
341     FIELD_TO_OFS(isTiled, );
342     FIELD_TO_OFS(TileType, );
343     FIELD_TO_OFS(bMapped, );
344 
345     EMPTY_TO_OFS();
346     FIELD_TO_OFS(bo->size, );
347     FIELD_TO_OFS(bo->align, );
348     FIELD_TO_OFS(bo->offset, );
349     FIELD_TO_OFS(bo->handle, );
350     FIELD_TO_OFS(bo->offset64, );
351     FIELD_TO_OFS(bo->aux_mapped, );
352 #endif
353     ofs << "iAllocationIndex[MOS_GPU_CONTEXT_MAX == " << (uint32_t)MOS_GPU_CONTEXT_MAX << "]: {";
354     for (int i = 0; i < MOS_GPU_CONTEXT_MAX; ++i)
355         ofs << ptr->iAllocationIndex[i] << ", ";
356     ofs << "}" << std::endl;
357 
358     {
359         PGMM_RESOURCE_INFO ptr = pOsResource->pGmmResInfo;
360         EMPTY_TO_OFS();
361         ofs << "GMM_RESOURCE_INFO:" << std::endl;
362         FIELD_TO_OFS(GetResourceType(), );
363         FIELD_TO_OFS(GetResourceFormat(), );
364         FIELD_TO_OFS(GetBitsPerPixel(), );
365 
366         {
367             GMM_RESOURCE_FLAG flags = pOsResource->pGmmResInfo->GetResFlags();
368             GMM_RESOURCE_FLAG* ptr = &flags;
369 
370             EMPTY_TO_OFS();
371             ofs << "    GMM_RESOURCE_FLAG:" << std::endl;
372             FIELD_TO_OFS_8SHIFT(Gpu.CameraCapture);
373             FIELD_TO_OFS_8SHIFT(Gpu.CCS);
374             FIELD_TO_OFS_8SHIFT(Gpu.ColorDiscard);
375             FIELD_TO_OFS_8SHIFT(Gpu.ColorSeparation);
376             FIELD_TO_OFS_8SHIFT(Gpu.ColorSeparationRGBX);
377             FIELD_TO_OFS_8SHIFT(Gpu.Constant);
378             FIELD_TO_OFS_8SHIFT(Gpu.Depth);
379             FIELD_TO_OFS_8SHIFT(Gpu.FlipChain);
380             FIELD_TO_OFS_8SHIFT(Gpu.FlipChainPreferred);
381             FIELD_TO_OFS_8SHIFT(Gpu.HistoryBuffer);
382             FIELD_TO_OFS_8SHIFT(Gpu.HiZ);
383             FIELD_TO_OFS_8SHIFT(Gpu.Index);
384             FIELD_TO_OFS_8SHIFT(Gpu.IndirectClearColor);
385             FIELD_TO_OFS_8SHIFT(Gpu.InstructionFlat);
386             FIELD_TO_OFS_8SHIFT(Gpu.InterlacedScan);
387             FIELD_TO_OFS_8SHIFT(Gpu.MCS);
388             FIELD_TO_OFS_8SHIFT(Gpu.MMC);
389             FIELD_TO_OFS_8SHIFT(Gpu.MotionComp);
390             FIELD_TO_OFS_8SHIFT(Gpu.NoRestriction);
391             FIELD_TO_OFS_8SHIFT(Gpu.Overlay);
392             FIELD_TO_OFS_8SHIFT(Gpu.Presentable);
393             FIELD_TO_OFS_8SHIFT(Gpu.ProceduralTexture);
394             FIELD_TO_OFS_8SHIFT(Gpu.Query);
395             FIELD_TO_OFS_8SHIFT(Gpu.RenderTarget);
396             FIELD_TO_OFS_8SHIFT(Gpu.S3d);
397             FIELD_TO_OFS_8SHIFT(Gpu.S3dDx);
398             FIELD_TO_OFS_8SHIFT(Gpu.__S3dNonPacked);
399             FIELD_TO_OFS_8SHIFT(Gpu.ScratchFlat);
400             FIELD_TO_OFS_8SHIFT(Gpu.SeparateStencil);
401             FIELD_TO_OFS_8SHIFT(Gpu.State);
402             FIELD_TO_OFS_8SHIFT(Gpu.Stream);
403             FIELD_TO_OFS_8SHIFT(Gpu.TextApi);
404             FIELD_TO_OFS_8SHIFT(Gpu.Texture);
405             FIELD_TO_OFS_8SHIFT(Gpu.TiledResource);
406             FIELD_TO_OFS_8SHIFT(Gpu.TilePool);
407             FIELD_TO_OFS_8SHIFT(Gpu.UnifiedAuxSurface);
408             FIELD_TO_OFS_8SHIFT(Gpu.Vertex);
409             FIELD_TO_OFS_8SHIFT(Gpu.Video);
410             FIELD_TO_OFS_8SHIFT(Gpu.__NonMsaaTileXCcs);
411             FIELD_TO_OFS_8SHIFT(Gpu.__NonMsaaTileYCcs);
412             FIELD_TO_OFS_8SHIFT(Gpu.__MsaaTileMcs);
413             FIELD_TO_OFS_8SHIFT(Gpu.__NonMsaaLinearCCS);
414             FIELD_TO_OFS_8SHIFT(Gpu.__Remaining);
415 
416             EMPTY_TO_OFS();
417             FIELD_TO_OFS_8SHIFT(Info.AllowVirtualPadding);
418             FIELD_TO_OFS_8SHIFT(Info.BigPage);
419             FIELD_TO_OFS_8SHIFT(Info.Cacheable);
420             FIELD_TO_OFS_8SHIFT(Info.ContigPhysMemoryForiDART);
421             FIELD_TO_OFS_8SHIFT(Info.CornerTexelMode);
422             FIELD_TO_OFS_8SHIFT(Info.ExistingSysMem);
423             FIELD_TO_OFS_8SHIFT(Info.ForceResidency);
424             FIELD_TO_OFS_8SHIFT(Info.Gfdt);
425             FIELD_TO_OFS_8SHIFT(Info.GttMapType);
426             FIELD_TO_OFS_8SHIFT(Info.HardwareProtected);
427             FIELD_TO_OFS_8SHIFT(Info.KernelModeMapped);
428             FIELD_TO_OFS_8SHIFT(Info.LayoutBelow);
429             FIELD_TO_OFS_8SHIFT(Info.LayoutMono);
430             FIELD_TO_OFS_8SHIFT(Info.LayoutRight);
431             FIELD_TO_OFS_8SHIFT(Info.LocalOnly);
432             FIELD_TO_OFS_8SHIFT(Info.Linear);
433             FIELD_TO_OFS_8SHIFT(Info.MediaCompressed);
434             FIELD_TO_OFS_8SHIFT(Info.NoOptimizationPadding);
435             FIELD_TO_OFS_8SHIFT(Info.NoPhysMemory);
436             FIELD_TO_OFS_8SHIFT(Info.NotLockable);
437             FIELD_TO_OFS_8SHIFT(Info.NonLocalOnly);
438             FIELD_TO_OFS_8SHIFT(Info.StdSwizzle);
439             FIELD_TO_OFS_8SHIFT(Info.PseudoStdSwizzle);
440             FIELD_TO_OFS_8SHIFT(Info.Undefined64KBSwizzle);
441             FIELD_TO_OFS_8SHIFT(Info.RedecribedPlanes);
442             FIELD_TO_OFS_8SHIFT(Info.RenderCompressed);
443             FIELD_TO_OFS_8SHIFT(Info.Rotated);
444             FIELD_TO_OFS_8SHIFT(Info.Shared);
445             FIELD_TO_OFS_8SHIFT(Info.SoftwareProtected);
446             FIELD_TO_OFS_8SHIFT(Info.SVM);
447 #if !defined(LINUX)
448             FIELD_TO_OFS_8SHIFT(Info.Tile4);
449             FIELD_TO_OFS_8SHIFT(Info.Tile64);
450 #endif
451             FIELD_TO_OFS_8SHIFT(Info.TiledW);
452             FIELD_TO_OFS_8SHIFT(Info.TiledX);
453             FIELD_TO_OFS_8SHIFT(Info.TiledY);
454             FIELD_TO_OFS_8SHIFT(Info.TiledYf);
455             FIELD_TO_OFS_8SHIFT(Info.TiledYs);
456             FIELD_TO_OFS_8SHIFT(Info.XAdapter);
457             FIELD_TO_OFS_8SHIFT(Info.__PreallocatedResInfo);
458 #if !defined(LINUX)
459             FIELD_TO_OFS_8SHIFT(Info.LMemBarPreferred);
460             FIELD_TO_OFS_8SHIFT(Info.LMemBarOrNonlocalOnly);
461             FIELD_TO_OFS_8SHIFT(Info.LMemBarIndifferent);
462             FIELD_TO_OFS_8SHIFT(Info.CpuVisibleOnDemand);
463             FIELD_TO_OFS_8SHIFT(Info.DwmFbrResource);
464 #endif
465 
466             EMPTY_TO_OFS();
467             FIELD_TO_OFS_8SHIFT(Wa.GTMfx2ndLevelBatchRingSizeAlign);
468             FIELD_TO_OFS_8SHIFT(Wa.ILKNeedAvcMprRowStore32KAlign);
469             FIELD_TO_OFS_8SHIFT(Wa.ILKNeedAvcDmvBuffer32KAlign);
470             FIELD_TO_OFS_8SHIFT(Wa.NoBufferSamplerPadding);
471             FIELD_TO_OFS_8SHIFT(Wa.NoLegacyPlanarLinearVideoRestrictions);
472             FIELD_TO_OFS_8SHIFT(Wa.CHVAstcSkipVirtualMips);
473             FIELD_TO_OFS_8SHIFT(Wa.DisablePackedMipTail);
474             FIELD_TO_OFS_8SHIFT(Wa.__ForceOtherHVALIGN4);
475             FIELD_TO_OFS_8SHIFT(Wa.DisableDisplayCcsClearColor);
476             FIELD_TO_OFS_8SHIFT(Wa.DisableDisplayCcsCompression);
477             FIELD_TO_OFS_8SHIFT(Wa.PreGen12FastClearOnly);
478 #if !defined(LINUX)
479             FIELD_TO_OFS_8SHIFT(Wa.ForceStdAllocAlign);
480 #endif
481         }
482 
483         FIELD_TO_OFS(GetBaseWidth(), );
484         FIELD_TO_OFS(GetBaseHeight(), );
485         FIELD_TO_OFS(GetBaseDepth(), );
486         FIELD_TO_OFS(GetMaxLod(), );
487         FIELD_TO_OFS(GetArraySize(), );
488         FIELD_TO_OFS(GetSetCpSurfTag(0, 0), );
489         FIELD_TO_OFS(GetCachePolicyUsage(), );
490         FIELD_TO_OFS(GetNumSamples(), );
491         FIELD_TO_OFS(GetSamplePattern(), );
492 
493         EMPTY_TO_OFS();
494         FIELD_TO_OFS(IsArraySpacingSingleLod(), );
495         FIELD_TO_OFS(GetBaseAlignment(), );
496         FIELD_TO_OFS(GetHAlign(), );
497         FIELD_TO_OFS(GetVAlign(), );
498         FIELD_TO_OFS(GetMipTailStartLodSurfaceState(), );
499         FIELD_TO_OFS(GetQPitch(), );
500 
501         EMPTY_TO_OFS();
502         ofs << "MmcMode[GMM_MAX_MMC_INDEX == " << GMM_MAX_MMC_INDEX << "]: {";
503         for (uint32_t i = 0; i < GMM_MAX_MMC_INDEX; ++i)
504             ofs << (uint32_t)ptr->GetMmcMode(i) << ", ";
505         ofs << "}" << std::endl;
506 
507         ofs << "MmcHint[GMM_MAX_MMC_INDEX == " << GMM_MAX_MMC_INDEX << "]: {";
508         for (uint32_t i = 0; i < GMM_MAX_MMC_INDEX; ++i)
509             ofs << (uint32_t)ptr->GetMmcHint(i) << ", ";
510         ofs << "}" << std::endl;
511 
512         FIELD_TO_OFS(GetRenderPitch(), );
513         FIELD_TO_OFS(GetSizeMainSurface(), );
514         FIELD_TO_OFS(GmmGetTileMode(), );
515 
516 #if !defined(LINUX)
517         EMPTY_TO_OFS();
518         FIELD_TO_OFS(GetMultiTileArch().Enable, );
519 #endif
520     }
521 
522     return MOS_STATUS_SUCCESS;
523 }
524 #undef FIELD_TO_OFS_8SHIFT
525 #undef RESOURCE_OFFSET_TO_OFS
526 #undef PLANE_OFFSET_TO_OFS
527 #undef OFFSET_FIELD_TO_OFS
528 #undef UNION_END_TO_OFS
529 #undef UNION_STRUCT_FIELD_TO_OFS
530 #undef UNION_STRUCT_START_TO_OFS
531 #undef EMPTY_TO_OFS
532 #undef FIELD_TO_OFS
533 
DumpBltOutput(PMOS_SURFACE surface,const char * attrName)534 MOS_STATUS MediaDebugInterface::DumpBltOutput(
535     PMOS_SURFACE surface,
536     const char * attrName)
537 {
538     return MOS_STATUS_SUCCESS;
539 }
540 
DeleteCfgLinkNode(uint32_t frameIdx)541 MOS_STATUS MediaDebugInterface::DeleteCfgLinkNode(uint32_t frameIdx)
542 {
543     return m_configMgr->DeleteCfgNode(frameIdx);
544 }
545 
ReAllocateSurface(PMOS_SURFACE pSurface,PMOS_SURFACE pSrcSurf,PCCHAR pSurfaceName,MOS_GFXRES_TYPE defaultResType,bool useLinearResource)546 MOS_STATUS MediaDebugInterface::ReAllocateSurface(
547     PMOS_SURFACE    pSurface,
548     PMOS_SURFACE    pSrcSurf,
549     PCCHAR          pSurfaceName,
550     MOS_GFXRES_TYPE defaultResType,
551     bool            useLinearResource)
552 {
553     MOS_ALLOC_GFXRES_PARAMS allocParams;
554 
555     MEDIA_DEBUG_ASSERT(m_osInterface);
556     MEDIA_DEBUG_ASSERT(&pSurface->OsResource);
557 
558     // bCompressible should be compared with bCompressible since it is inited by bCompressible in previous call
559     // TileType of surface should be compared since we need to reallocate surface if TileType changes
560     if (!Mos_ResourceIsNull(&pSurface->OsResource) &&
561         (pSurface->dwWidth == pSrcSurf->dwWidth) &&
562         (pSurface->dwHeight == pSrcSurf->dwHeight) &&
563         (pSurface->Format == pSrcSurf->Format) &&
564         ((pSurface->bCompressible == pSrcSurf->bCompressible) || useLinearResource) && // omit this check as linear surface is uncompressible
565         (pSurface->CompressionMode == pSrcSurf->CompressionMode) &&
566         ((pSurface->TileType == pSrcSurf->TileType) ||
567          (pSurface->TileType == MOS_TILE_LINEAR && useLinearResource))) // when useLinearResource no reallocation needed
568     {
569         MEDIA_DEBUG_VERBOSEMESSAGE("Skip to reallocate temp surface.");
570         return MOS_STATUS_SUCCESS;
571     }
572     MOS_ZeroMemory(&allocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
573 
574 #if !EMUL
575     //  Need to reallocate surface according to expected tiletype instead of tiletype of the surface what we have
576     if ((pSurface->OsResource.pGmmResInfo != nullptr) &&
577         (pSurface->TileType == pSrcSurf->TileType))
578     {
579         // Reallocate but use same tile type and resource type as current
580         allocParams.TileType = pSurface->OsResource.TileType;
581         allocParams.Type     = defaultResType;
582     }
583     else
584 #endif
585     {
586         // First time allocation. Caller must specify default params
587         allocParams.TileType = pSrcSurf->TileType;
588         allocParams.Type     = defaultResType;
589     }
590 
591     // Force to use tile linear for reallocated resource
592     if (useLinearResource)
593     {
594         allocParams.TileType = MOS_TILE_LINEAR;
595         allocParams.Type = MOS_GFXRES_2D;
596     }
597 
598     allocParams.dwWidth         = pSrcSurf->dwWidth;
599     allocParams.dwHeight        = pSrcSurf->dwHeight;
600     allocParams.Format          = pSrcSurf->Format;
601     allocParams.bIsCompressible = pSrcSurf->bCompressible;
602     allocParams.CompressionMode = pSrcSurf->CompressionMode;
603     allocParams.pBufName        = pSurfaceName;
604     allocParams.dwArraySize     = 1;
605 
606     // Delete resource if already allocated
607     m_osInterface->pfnFreeResource(m_osInterface, &(pSurface->OsResource));
608 
609     // Allocate surface
610     CODECHAL_PUBLIC_CHK_STATUS_RETURN(m_osInterface->pfnAllocateResource(
611         m_osInterface,
612         &allocParams,
613         &pSurface->OsResource));
614 
615     if (!m_osInterface->apoMosEnabled && !m_osInterface->apoMosForLegacyRuntime)
616     {
617         MOS_SURFACE details;
618         MOS_ZeroMemory(&details, sizeof(details));
619         details.Format = Format_Invalid;
620 
621         MEDIA_DEBUG_CHK_STATUS(m_osInterface->pfnGetResourceInfo(m_osInterface, &pSurface->OsResource, &details));
622 
623         pSurface->dwWidth                     = details.dwWidth;
624         pSurface->dwHeight                    = details.dwHeight;
625         pSurface->dwPitch                     = details.dwPitch;
626         pSurface->dwDepth                     = details.dwDepth;
627         pSurface->dwQPitch                    = details.dwQPitch;
628         pSurface->bArraySpacing               = details.bArraySpacing;
629         pSurface->bCompressible               = details.bCompressible;
630         pSurface->CompressionMode             = details.CompressionMode;
631         pSurface->bIsCompressed               = details.bIsCompressed;
632         pSurface->Format                      = details.Format;
633         pSurface->TileType                    = details.TileType;
634         pSurface->dwOffset                    = details.RenderOffset.YUV.Y.BaseOffset;
635         pSurface->YPlaneOffset.iSurfaceOffset = details.RenderOffset.YUV.Y.BaseOffset;
636         pSurface->YPlaneOffset.iXOffset       = details.RenderOffset.YUV.Y.XOffset;
637         pSurface->YPlaneOffset.iYOffset =
638             (pSurface->YPlaneOffset.iSurfaceOffset - pSurface->dwOffset) / pSurface->dwPitch +
639             details.RenderOffset.YUV.Y.YOffset;
640         pSurface->UPlaneOffset.iSurfaceOffset = details.RenderOffset.YUV.U.BaseOffset;
641         pSurface->UPlaneOffset.iXOffset       = details.RenderOffset.YUV.U.XOffset;
642         pSurface->UPlaneOffset.iYOffset =
643             (pSurface->UPlaneOffset.iSurfaceOffset - pSurface->dwOffset) / pSurface->dwPitch +
644             details.RenderOffset.YUV.U.YOffset;
645         pSurface->UPlaneOffset.iLockSurfaceOffset = details.LockOffset.YUV.U;
646         pSurface->VPlaneOffset.iSurfaceOffset     = details.RenderOffset.YUV.V.BaseOffset;
647         pSurface->VPlaneOffset.iXOffset           = details.RenderOffset.YUV.V.XOffset;
648         pSurface->VPlaneOffset.iYOffset =
649             (pSurface->VPlaneOffset.iSurfaceOffset - pSurface->dwOffset) / pSurface->dwPitch +
650             details.RenderOffset.YUV.V.YOffset;
651         pSurface->VPlaneOffset.iLockSurfaceOffset = details.LockOffset.YUV.V;
652     }
653     else
654     {
655         pSurface->Format = Format_Invalid;
656         MEDIA_DEBUG_CHK_STATUS(m_osInterface->pfnGetResourceInfo(m_osInterface, &pSurface->OsResource, pSurface));
657     }
658 
659     return MOS_STATUS_SUCCESS;
660 }
661 
Dump2DBufferInBinary(uint8_t * data,uint32_t width,uint32_t height,uint32_t pitch)662 MOS_STATUS MediaDebugInterface::Dump2DBufferInBinary(
663     uint8_t *data,
664     uint32_t width,
665     uint32_t height,
666     uint32_t pitch)
667 {
668     MEDIA_DEBUG_CHK_NULL(data);
669 
670     const char *filePath = m_outputFileName.c_str();
671 
672     if (width == 0 || height == 0 || pitch == 0)
673     {
674         return MOS_STATUS_UNKNOWN;
675     }
676 
677     std::ofstream ofs(filePath, std::ios_base::out | std::ios_base::binary);
678     if (ofs.fail())
679     {
680         return MOS_STATUS_UNKNOWN;
681     }
682 
683     for (uint32_t h = 0; h < height; h++)
684     {
685         ofs.write((char *)data, width);
686         data += pitch;
687     }
688 
689     ofs.close();
690     return MOS_STATUS_SUCCESS;
691 }
692 
DumpBufferInBinary(uint8_t * data,uint32_t size)693 MOS_STATUS MediaDebugInterface::DumpBufferInBinary(uint8_t *data, uint32_t size)
694 {
695     MEDIA_DEBUG_CHK_NULL(data);
696 
697     const char *filePath = m_outputFileName.c_str();
698 
699     if (size == 0)
700     {
701         return MOS_STATUS_UNKNOWN;
702     }
703 
704     std::ofstream ofs(filePath, std::ios_base::out | std::ios_base::binary);
705     if (ofs.fail())
706     {
707         return MOS_STATUS_UNKNOWN;
708     }
709 
710     ofs.write((char *)data, size);
711     ofs.close();
712     return MOS_STATUS_SUCCESS;
713 }
714 
DumpBufferInHexDwords(uint8_t * data,uint32_t size)715 MOS_STATUS MediaDebugInterface::DumpBufferInHexDwords(uint8_t *data, uint32_t size)
716 {
717     MEDIA_DEBUG_CHK_NULL(data);
718 
719     const char *filePath = m_outputFileName.c_str();
720 
721     if (size == 0)
722     {
723         return MOS_STATUS_UNKNOWN;
724     }
725 
726     std::ofstream ofs(filePath);
727 
728     if (ofs.fail())
729     {
730         return MOS_STATUS_UNKNOWN;
731     }
732 
733     uint32_t dwordSize  = size / sizeof(uint32_t);
734     uint32_t remainSize = size % sizeof(uint32_t);
735 
736     uint32_t *dwordData = (uint32_t *)data;
737     uint32_t  i;
738     for (i = 0; i < dwordSize; i++)
739     {
740         ofs << std::hex << std::setw(8) << std::setfill('0') << +dwordData[i]
741             << ((i + 1) % 4 == 0 ? "\n" : " ");
742     }
743 
744     if (remainSize > 0)
745     {
746         uint32_t lastWord = dwordData[i] & (0xFFFFFFFF << ((8 - remainSize * 2) * 4));
747         ofs << std::hex << std::setw(8) << std::setfill('0') << +lastWord << std::endl;
748     }
749 
750     ofs.close();
751 
752     return MOS_STATUS_SUCCESS;
753 }
754 
InitCRCTable(uint32_t crcTable[256])755 MOS_STATUS MediaDebugInterface::InitCRCTable(uint32_t crcTable[256])
756 {
757     uint32_t polynomial = 0xEDB88320;
758     for (uint32_t i = 0; i < 256; i++)
759     {
760         uint32_t c = i;
761         for (size_t j = 0; j < 8; j++)
762         {
763             if (c & 1)
764             {
765                 c = polynomial ^ (c >> 1);
766             }
767             else
768             {
769                 c >>= 1;
770             }
771         }
772         crcTable[i] = c;
773     }
774     return MOS_STATUS_SUCCESS;
775 }
776 
CalculateCRC(const void * buf,size_t len,uint32_t initial)777 uint32_t MediaDebugInterface::CalculateCRC(const void *buf, size_t len, uint32_t initial)
778 {
779     uint32_t       c = initial ^ 0xFFFFFFFF;
780     const uint8_t *u = static_cast<const uint8_t *>(buf);
781     for (size_t i = 0; i < len; ++i)
782     {
783         c = m_crcTable[(c ^ u[i]) & 0xFF] ^ (c >> 8);
784     }
785     return c ^ 0xFFFFFFFF;
786 }
787 
788 #endif  // USE_MEDIA_DEBUG_TOOL
789