Debian Codegraph @

media_softlet/linux/common/os/mos_graphicsresource_specific_next.cpp

raw
1 /*
2 * Copyright (c) 2019-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 mos_graphicsresource_specific_next.cpp
24 //! \brief Container class for the linux/Android specfic graphic resource
25 //!
26
27 #include "mos_defs.h"
28 #include "mos_util_debug.h"
29
30 #include "mos_graphicsresource_specific_next.h"
31 #include "mos_context_specific_next.h"
32 #include "memory_policy_manager.h"
33
34 GraphicsResourceSpecificNext::GraphicsResourceSpecificNext()
35 {
36 MOS_OS_FUNCTION_ENTER;
37 }
38
39 GraphicsResourceSpecificNext::~GraphicsResourceSpecificNext()
40 {
41 MOS_OS_FUNCTION_ENTER;
42 }
43
44 bool GraphicsResourceSpecificNext::ResourceIsNull()
45 {
46 return ((m_bo == nullptr)
47 #if (_DEBUG || _RELEASE_INTERNAL)
48 && ((m_pData == nullptr) )
49 #endif // (_DEBUG || _RELEASE_INTERNAL)
50 );
51
52 }
53
54 MOS_STATUS GraphicsResourceSpecificNext::Allocate(OsContextNext* osContextPtr, CreateParams& params)
55 {
56 MOS_OS_FUNCTION_ENTER;
57
58 if (osContextPtr == nullptr)
59 {
60 MOS_OS_ASSERTMESSAGE("Unable to get the active OS context.");
61 return MOS_STATUS_INVALID_HANDLE;
62 }
63
64 if (osContextPtr->GetOsContextValid() == false)
65 {
66 MOS_OS_ASSERTMESSAGE("The OS context got is not valid.");
67 return MOS_STATUS_INVALID_HANDLE;
68 }
69
70 OsContextSpecificNext *pOsContextSpecific = static_cast<OsContextSpecificNext *>(osContextPtr);
71
72 GMM_CLIENT_CONTEXT *gmmClientContext = pOsContextSpecific->GetGmmClientContext();
73 if (nullptr == gmmClientContext)
74 {
75 MOS_OS_ASSERTMESSAGE("Get GMM Client Context failed.");
76 return MOS_STATUS_INVALID_HANDLE;
77 }
78
79 MOS_STATUS status = MOS_STATUS_SUCCESS;
80 uint32_t tileFormatLinux = TILING_NONE;
81 uint32_t alignedHeight = params.m_height;
82 uint32_t bufHeight = params.m_height;
83 GMM_RESOURCE_TYPE resourceType = RESOURCE_2D;
84 int32_t mem_type = MOS_MEMPOOL_VIDEOMEMORY;
85
86 GMM_RESCREATE_PARAMS gmmParams;
87 MosUtilities::MosZeroMemory(&gmmParams, sizeof(gmmParams));
88
89 gmmParams.Usage = params.m_gmmResUsageType;
90
91 switch (params.m_type)
92 {
93 case MOS_GFXRES_BUFFER:
94 case MOS_GFXRES_SCRATCH:
95 gmmParams.Type = RESOURCE_BUFFER;
96 gmmParams.Flags.Gpu.State = true;
97 alignedHeight = 1;
98 break;
99
100 case MOS_GFXRES_2D:
101 gmmParams.Type = RESOURCE_2D;
102 gmmParams.Flags.Gpu.Video = true;
103 break;
104
105 case MOS_GFXRES_VOLUME:
106 gmmParams.Type = RESOURCE_3D;
107 gmmParams.Flags.Gpu.Video = true;
108 gmmParams.Depth = params.m_depth;
109 break;
110
111 default:
112 MOS_OS_ASSERTMESSAGE("Unknown surface type");
113 return MOS_STATUS_UNKNOWN;
114 }
115
116 // Create GmmResourceInfo
117 gmmParams.Format = MosInterface::MosFmtToGmmFmt(params.m_format);
118 if (gmmParams.Format == GMM_FORMAT_INVALID)
119 {
120 MOS_OS_ASSERTMESSAGE("Unsupported format");
121 return MOS_STATUS_UNIMPLEMENTED;
122 }
123 gmmParams.BaseWidth = params.m_width;
124 gmmParams.BaseHeight = alignedHeight;
125 gmmParams.ArraySize = 1;
126
127 if (MEDIA_IS_SKU(pOsContextSpecific->GetSkuTable(), FtrXe2Compression))
128 {
129 // Init NotCompressed flag as true to default Create as uncompressed surface on Xe2 Compression.
130 gmmParams.Flags.Info.NotCompressed = 1;
131 }
132
133 MOS_TILE_TYPE tileformat = params.m_tileType;
134 MOS_OS_NORMALMESSAGE("tilemode: tileformat = %d for %s, tileModeByForce = %d", tileformat, params.m_name.c_str(), params.m_tileModeByForce);
135 switch (tileformat)
136 {
137 case MOS_TILE_Y:
138 tileFormatLinux = TILING_Y;
139 if (params.m_isCompressible &&
140 MEDIA_IS_SKU(pOsContextSpecific->GetSkuTable(), FtrE2ECompression) &&
141 MEDIA_IS_SKU(pOsContextSpecific->GetSkuTable(), FtrCompressibleSurfaceDefault))
142 {
143 gmmParams.Flags.Gpu.MMC = 1;
144
145 if (params.m_compressionMode == MOS_MMC_RC)
146 {
147 gmmParams.Flags.Info.MediaCompressed = 0;
148 gmmParams.Flags.Info.RenderCompressed = 1;
149 }
150 else
151 {
152 gmmParams.Flags.Info.MediaCompressed = 1;
153 gmmParams.Flags.Info.RenderCompressed = 0;
154 }
155
156 gmmParams.Flags.Gpu.CCS = 1;
157 gmmParams.Flags.Gpu.UnifiedAuxSurface = 1;
158 gmmParams.Flags.Gpu.RenderTarget = 1;
159
160 if(MEDIA_IS_SKU(pOsContextSpecific->GetSkuTable(), FtrFlatPhysCCS))
161 {
162 gmmParams.Flags.Gpu.UnifiedAuxSurface = 0;
163 }
164
165 if(MEDIA_IS_SKU(pOsContextSpecific->GetSkuTable(), FtrXe2Compression))
166 {
167 gmmParams.Flags.Info.NotCompressed = 0;
168 }
169 }
170 SetTileModebyForce(gmmParams, params.m_tileModeByForce);
171 break;
172 case MOS_TILE_X:
173 gmmParams.Flags.Info.TiledX = true;
174 tileFormatLinux = TILING_X;
175 break;
176 default:
177 gmmParams.Flags.Info.Linear = true;
178 tileFormatLinux = TILING_NONE;
179 }
180
181 if (nullptr != params.m_pSystemMemory)
182 {
183 // If user provides a system memory pointer, the gfx resource is backed
184 // by the system memory pages. The resource is required to be linear.
185 gmmParams.Flags.Info.Linear = true;
186 gmmParams.Flags.Info.Cacheable = true;
187 gmmParams.NoGfxMemory = true;
188 GMM_RESOURCE_INFO *tmpGmmResInfoPtr = pOsContextSpecific
189 ->GetGmmClientContext()->CreateResInfoObject(&gmmParams);
190 if (tmpGmmResInfoPtr == nullptr)
191 {
192 MOS_OS_ASSERTMESSAGE("Create GmmResInfo failed");
193 return MOS_STATUS_UNKNOWN;
194 }
195
196 gmmParams.ExistingSysMemSize = GFX_ULONG_CAST(tmpGmmResInfoPtr->GetSizeSurface());
197 gmmParams.pExistingSysMem = (GMM_VOIDPTR64)params.m_pSystemMemory;
198 gmmParams.NoGfxMemory = false;
199 gmmParams.Flags.Info.ExistingSysMem = true;
200
201 pOsContextSpecific->GetGmmClientContext()
202 ->DestroyResInfoObject(tmpGmmResInfoPtr);
203 }
204 else
205 {
206 gmmParams.Flags.Info.LocalOnly = MEDIA_IS_SKU(pOsContextSpecific->GetSkuTable(), FtrLocalMemory);
207 }
208
209 GMM_RESOURCE_INFO* gmmResourceInfoPtr = pOsContextSpecific->GetGmmClientContext()->CreateResInfoObject(&gmmParams);
210
211 if (gmmResourceInfoPtr == nullptr)
212 {
213 MOS_OS_ASSERTMESSAGE("Get gmmResourceInfoPtr failed.");
214 return MOS_STATUS_INVALID_PARAMETER;
215 }
216
217 switch (gmmResourceInfoPtr->GetTileType())
218 {
219 case GMM_TILED_X:
220 tileformat = MOS_TILE_X;
221 tileFormatLinux = TILING_X;
222 break;
223 case GMM_TILED_Y:
224 tileformat = MOS_TILE_Y;
225 tileFormatLinux = TILING_Y;
226 break;
227 case GMM_NOT_TILED:
228 tileformat = MOS_TILE_LINEAR;
229 tileFormatLinux = TILING_NONE;
230 break;
231 default:
232 tileformat = MOS_TILE_Y;
233 tileFormatLinux = TILING_Y;
234 break;
235 }
236
237 if (params.m_tileType== MOS_TILE_Y)
238 {
239 gmmResourceInfoPtr->SetMmcMode((GMM_RESOURCE_MMC_INFO)params.m_compressionMode, 0);
240 }
241
242 if(!params.m_pSystemMemory)
243 {
244 MemoryPolicyParameter memPolicyPar;
245 MOS_ZeroMemory(&memPolicyPar, sizeof(MemoryPolicyParameter));
246
247 memPolicyPar.skuTable = pOsContextSpecific->GetSkuTable();
248 memPolicyPar.waTable = pOsContextSpecific->GetWaTable();
249 memPolicyPar.resInfo = gmmResourceInfoPtr;
250 memPolicyPar.resName = params.m_name.c_str();
251 memPolicyPar.preferredMemType = params.m_memType;
252 memPolicyPar.isServer = PLATFORM_INFORMATION_IS_SERVER & mos_get_platform_information(pOsContextSpecific->GetBufMgr());
253
254 mem_type = MemoryPolicyManager::UpdateMemoryPolicy(&memPolicyPar);
255 }
256
257 uint32_t bufPitch = GFX_ULONG_CAST(gmmResourceInfoPtr->GetRenderPitch());
258 uint32_t bufSize = GFX_ULONG_CAST(gmmResourceInfoPtr->GetSizeSurface());
259 bufHeight = gmmResourceInfoPtr->GetBaseHeight();
260 MOS_LINUX_BO* boPtr = nullptr;
261
262 char bufName[m_maxBufNameLength];
263 MosUtilities::MosSecureStrcpy(bufName, m_maxBufNameLength, params.m_name.c_str());
264
265 unsigned int patIndex = MosInterface::GetPATIndexFromGmm(gmmClientContext, gmmResourceInfoPtr);
266 bool isCpuCacheable = gmmResourceInfoPtr->GetResFlags().Info.Cacheable;
267
268 MOS_TraceEventExt(EVENT_RESOURCE_ALLOCATE, EVENT_TYPE_START, nullptr, 0, nullptr, 0);
269 if (nullptr != params.m_pSystemMemory)
270 {
271 struct mos_drm_bo_alloc_userptr alloc_uptr;
272 alloc_uptr.name = bufName;
273 alloc_uptr.addr = params.m_pSystemMemory;
274 alloc_uptr.tiling_mode = tileFormatLinux;
275 alloc_uptr.stride = bufPitch;
276 alloc_uptr.size = bufSize;
277 alloc_uptr.pat_index = patIndex;
278
279 boPtr = mos_bo_alloc_userptr(pOsContextSpecific->m_bufmgr, &alloc_uptr);
280 }
281 // Only Linear and Y TILE supported
282 else if (tileFormatLinux == TILING_NONE)
283 {
284 struct mos_drm_bo_alloc alloc;
285 alloc.name = bufName;
286 alloc.size = bufSize;
287 alloc.alignment = 4096;
288 alloc.ext.mem_type = mem_type;
289 alloc.ext.pat_index = patIndex;
290 alloc.ext.cpu_cacheable = isCpuCacheable;
291 boPtr = mos_bo_alloc(pOsContextSpecific->m_bufmgr, &alloc);
292 }
293 else
294 {
295 struct mos_drm_bo_alloc_tiled alloc_tiled;
296 alloc_tiled.name = bufName;
297 alloc_tiled.x = bufPitch;
298 alloc_tiled.y = bufSize/bufPitch;
299 alloc_tiled.cpp = 1;
300 alloc_tiled.ext.tiling_mode = tileFormatLinux;
301 alloc_tiled.ext.mem_type = mem_type;
302 alloc_tiled.ext.pat_index = patIndex;
303 alloc_tiled.ext.cpu_cacheable = isCpuCacheable;
304
305 boPtr = mos_bo_alloc_tiled(pOsContextSpecific->m_bufmgr, &alloc_tiled);
306 bufPitch = (uint32_t)alloc_tiled.pitch;
307 }
308
309 m_mapped = false;
310 if (boPtr)
311 {
312 m_format = params.m_format;
313 m_width = params.m_width;
314 m_height = bufHeight;
315 m_pitch = bufPitch;
316 m_count = 0;
317 m_bo = boPtr;
318 m_name = params.m_name;
319 m_pData = (uint8_t*) boPtr->virt;
320
321 m_gmmResInfo = gmmResourceInfoPtr;
322 m_mapped = false;
323 m_mmapOperation = MOS_MMAP_OPERATION_NONE;
324
325 m_arraySize = 1;
326 m_depth = MOS_MAX(1, gmmResourceInfoPtr->GetBaseDepth());
327 m_size = (uint32_t)gmmResourceInfoPtr->GetSizeSurface();
328 m_tileType = tileformat;
329 m_tileModeGMM = (MOS_TILE_MODE_GMM)gmmResourceInfoPtr->GetTileModeSurfaceState();
330 m_isGMMTileEnabled = true;
331
332 m_compressible = gmmParams.Flags.Gpu.MMC ?
333 (gmmResourceInfoPtr->GetMmcHint(0) == GMM_MMC_HINT_ON) : false;
334 m_isCompressed = gmmResourceInfoPtr->IsMediaMemoryCompressed(0);
335 m_compressionMode = (MOS_RESOURCE_MMC_MODE)gmmResourceInfoPtr->GetMmcMode(0);
336 m_memObjCtrlState = MosInterface::GetCachePolicyMemoryObject(pOsContextSpecific->GetGmmClientContext(), params.m_mocsMosResUsageType);
337
338 MOS_OS_VERBOSEMESSAGE("Alloc %7d bytes (%d x %d resource), tile encoding %d.", bufSize, params.m_width, bufHeight, m_tileModeGMM);
339
340 struct {
341 uint32_t m_handle;
342 uint32_t m_resFormat;
343 uint32_t m_baseWidth;
344 uint32_t m_baseHeight;
345 uint32_t m_pitch;
346 uint32_t m_size;
347 uint32_t m_resTileType;
348 GMM_RESOURCE_FLAG m_resFlag;
349 uint32_t m_reserve;
350 } eventData;
351
352 eventData.m_handle = boPtr->handle;
353 eventData.m_baseWidth = m_width;
354 eventData.m_baseHeight = m_height;
355 eventData.m_pitch = m_pitch;
356 eventData.m_size = m_size;
357 eventData.m_resFormat = m_format;
358 eventData.m_resTileType = m_tileType;
359 eventData.m_resFlag = gmmResourceInfoPtr->GetResFlags();
360 eventData.m_reserve = 0;
361 MOS_TraceEventExt(EVENT_RESOURCE_ALLOCATE,
362 EVENT_TYPE_INFO,
363 &eventData,
364 sizeof(eventData),
365 params.m_name.c_str(),
366 params.m_name.size() + 1);
367 }
368 else
369 {
370 MOS_OS_ASSERTMESSAGE("Fail to Alloc %7d bytes (%d x %d resource).",bufSize, params.m_width, params.m_height);
371 status = MOS_STATUS_NO_SPACE;
372 }
373 MOS_TraceEventExt(EVENT_RESOURCE_ALLOCATE, EVENT_TYPE_END, &status, sizeof(status), nullptr, 0);
374
375 m_memAllocCounterGfx++;
376 return status;
377 }
378
379 void GraphicsResourceSpecificNext::Free(OsContextNext* osContextPtr, uint32_t freeFlag)
380 {
381 MOS_OS_FUNCTION_ENTER;
382
383 MOS_UNUSED(osContextPtr);
384 MOS_UNUSED(freeFlag);
385
386 OsContextSpecificNext *pOsContextSpecific = static_cast<OsContextSpecificNext *>(osContextPtr);
387
388 MOS_LINUX_BO* boPtr = m_bo;
389
390 if (boPtr)
391 {
392 AuxTableMgr *auxTableMgr = pOsContextSpecific->GetAuxTableMgr();
393 if (auxTableMgr)
394 {
395 auxTableMgr->UnmapResource(m_gmmResInfo, boPtr);
396 }
397 mos_bo_unreference(boPtr);
398 m_bo = nullptr;
399 if (nullptr != m_gmmResInfo)
400 {
401 pOsContextSpecific->GetGmmClientContext()->DestroyResInfoObject(m_gmmResInfo);
402 m_gmmResInfo = nullptr;
403 m_memAllocCounterGfx--;
404 }
405 }
406 return;
407 }
408
409 bool GraphicsResourceSpecificNext::IsEqual(GraphicsResourceNext* toCompare)
410 {
411 if (toCompare == nullptr)
412 {
413 return false;
414 }
415
416 GraphicsResourceSpecificNext *resSpecificPtr = static_cast<GraphicsResourceSpecificNext *>(toCompare);
417
418 return (m_bo == resSpecificPtr->m_bo);
419 }
420
421 bool GraphicsResourceSpecificNext::IsValid()
422 {
423 return (m_bo != nullptr);
424 }
425
426 MOS_STATUS GraphicsResourceSpecificNext::ConvertToMosResource(MOS_RESOURCE* pMosResource)
427 {
428 if (pMosResource == nullptr)
429 {
430 return MOS_STATUS_INVALID_PARAMETER;
431 }
432
433 pMosResource->Format = m_format;
434 pMosResource->iWidth = m_width;
435 pMosResource->iHeight = m_height;
436 pMosResource->iSize = m_size;
437 pMosResource->iPitch = m_pitch;
438 pMosResource->iDepth = m_depth;
439 pMosResource->TileType = m_tileType;
440 pMosResource->TileModeGMM = m_tileModeGMM;
441 pMosResource->bGMMTileEnabled = m_isGMMTileEnabled;
442 pMosResource->iCount = 0;
443 pMosResource->pData = m_pData;
444 pMosResource->bufname = m_name.c_str();
445 pMosResource->bo = m_bo;
446 pMosResource->bMapped = m_mapped;
447 pMosResource->MmapOperation = m_mmapOperation;
448 pMosResource->pGmmResInfo = m_gmmResInfo;
449
450 pMosResource->user_provided_va = m_userProvidedVA;
451
452 pMosResource->memObjCtrlState = m_memObjCtrlState;
453 pMosResource->mocsMosResUsageType = m_mocsMosResUsageType;
454
455 pMosResource->pGfxResourceNext = this;
456
457 return MOS_STATUS_SUCCESS;
458 }
459
460 void* GraphicsResourceSpecificNext::Lock(OsContextNext* osContextPtr, LockParams& params)
461 {
462 MOS_OS_FUNCTION_ENTER;
463
464 if (osContextPtr == nullptr)
465 {
466 MOS_OS_ASSERTMESSAGE("Unable to get the active OS context.");
467 return nullptr;
468 }
469
470 if (osContextPtr ->GetOsContextValid() == false)
471 {
472 MOS_OS_ASSERTMESSAGE("The OS context got is not valid.");
473 return nullptr;
474 }
475
476 OsContextSpecificNext *pOsContextSpecific = static_cast<OsContextSpecificNext *>(osContextPtr);
477
478 void* dataPtr = nullptr;
479 MOS_LINUX_BO* boPtr = m_bo;
480
481 if (boPtr)
482 {
483 // Do decompression for a compressed surface before lock
484 const auto pGmmResInfo = m_gmmResInfo;
485 MOS_OS_ASSERT(pGmmResInfo);
486 GMM_RESOURCE_FLAG GmmFlags = pGmmResInfo->GetResFlags();
487
488 if (!params.m_noDecompress &&
489 (((GmmFlags.Gpu.MMC || GmmFlags.Gpu.CCS) && GmmFlags.Info.MediaCompressed) ||
490 pGmmResInfo->IsMediaMemoryCompressed(0)))
491 {
492 MOS_RESOURCE mosResource = {};
493 ConvertToMosResource(&mosResource);
494
495 MosDecompression *mosDecompression = pOsContextSpecific->GetMosDecompression();
496 if (nullptr == mosDecompression)
497 {
498 MOS_OS_ASSERTMESSAGE("mosDecompression is NULL.");
499 return nullptr;
500 }
501 mosDecompression->MemoryDecompress(&mosResource);
502 }
503
504 if(false == m_mapped)
505 {
506 if (pOsContextSpecific->IsAtomSoc())
507 {
508 mos_bo_map_gtt(boPtr);
509 }
510 else
511 {
512 if (m_tileType != MOS_TILE_LINEAR && !params.m_tileAsTiled)
513 {
514 if (pOsContextSpecific->UseSwSwizzling())
515 {
516 mos_bo_map(boPtr, ( OSKM_LOCKFLAG_WRITEONLY & params.m_writeRequest ));
517 m_mmapOperation = MOS_MMAP_OPERATION_MMAP;
518 if (m_systemShadow == nullptr)
519 {
520 m_systemShadow = (uint8_t *)MOS_AllocMemory(boPtr->size);
521 MOS_OS_CHECK_CONDITION((m_systemShadow == nullptr), "Failed to allocate shadow surface", nullptr);
522 }
523 if (m_systemShadow)
524 {
525 int32_t flags = pOsContextSpecific->GetTileYFlag() ? 0 : 1;
526 uint64_t surfSize = m_gmmResInfo->GetSizeMainSurface();
527 MOS_OS_CHECK_CONDITION((m_tileType != MOS_TILE_Y), "Unsupported tile type", nullptr);
528 MOS_OS_CHECK_CONDITION((boPtr->size <= 0 || m_pitch <= 0), "Invalid BO size or pitch", nullptr);
529 MosUtilities::MosSwizzleData((uint8_t*)boPtr->virt, m_systemShadow,
530 MOS_TILE_Y, MOS_TILE_LINEAR,
531 (int32_t)(surfSize / m_pitch), m_pitch, flags);
532 }
533 }
534 else
535 {
536 mos_bo_map_gtt(boPtr);
537 m_mmapOperation = MOS_MMAP_OPERATION_MMAP_GTT;
538 }
539 }
540 else if (params.m_uncached)
541 {
542 mos_bo_map_wc(boPtr);
543 m_mmapOperation = MOS_MMAP_OPERATION_MMAP_WC;
544 }
545 else
546 {
547 mos_bo_map(boPtr, ( OSKM_LOCKFLAG_WRITEONLY & params.m_writeRequest ));
548 m_mmapOperation = MOS_MMAP_OPERATION_MMAP;
549 }
550 }
551 m_mapped = true;
552 m_pData = m_systemShadow ? m_systemShadow : (uint8_t *)boPtr->virt;
553 }
554
555 dataPtr = m_pData;
556 }
557
558 MOS_OS_ASSERT(dataPtr);
559 return dataPtr;
560 }
561
562 MOS_STATUS GraphicsResourceSpecificNext::Unlock(OsContextNext* osContextPtr)
563 {
564 MOS_OS_FUNCTION_ENTER;
565
566 if (osContextPtr == nullptr)
567 {
568 MOS_OS_ASSERTMESSAGE("Unable to get the active OS context.");
569 return MOS_STATUS_INVALID_HANDLE;
570 }
571
572 if (osContextPtr ->GetOsContextValid() == false)
573 {
574 MOS_OS_ASSERTMESSAGE("The OS context got is not valid.");
575 return MOS_STATUS_INVALID_HANDLE;
576 }
577
578 OsContextSpecificNext *pOsContextSpecific = static_cast<OsContextSpecificNext *>(osContextPtr);
579
580 MOS_LINUX_BO* boPtr = m_bo;
581 if (boPtr)
582 {
583 if (m_mapped)
584 {
585 if (pOsContextSpecific->IsAtomSoc())
586 {
587 mos_bo_unmap_gtt(boPtr);
588 }
589 else
590 {
591
592 if (m_systemShadow)
593 {
594 int32_t flags = pOsContextSpecific->GetTileYFlag() ? 0 : 1;
595 uint64_t surfSize = m_gmmResInfo->GetSizeMainSurface();
596 MosUtilities::MosSwizzleData(m_systemShadow, (uint8_t*)boPtr->virt,
597 MOS_TILE_LINEAR, MOS_TILE_Y,
598 (int32_t)(surfSize / m_pitch), m_pitch, flags);
599 MOS_FreeMemory(m_systemShadow);
600 m_systemShadow = nullptr;
601 }
602
603 switch(m_mmapOperation)
604 {
605 case MOS_MMAP_OPERATION_MMAP_GTT:
606 mos_bo_unmap_gtt(boPtr);
607 break;
608 case MOS_MMAP_OPERATION_MMAP_WC:
609 mos_bo_unmap_wc(boPtr);
610 break;
611 case MOS_MMAP_OPERATION_MMAP:
612 mos_bo_unmap(boPtr);
613 break;
614 default:
615 MOS_OS_ASSERTMESSAGE("Invalid mmap operation type");
616 break;
617 }
618 }
619
620 m_mapped = false;
621 m_mmapOperation = MOS_MMAP_OPERATION_NONE;
622
623 boPtr->virt = nullptr;
624 m_bo = boPtr;
625 }
626
627 m_pData = nullptr;
628 }
629
630 return MOS_STATUS_SUCCESS;
631 }
632
633 MOS_STATUS GraphicsResourceSpecificNext::AllocateExternalResource(
634 MOS_STREAM_HANDLE streamState,
635 PMOS_ALLOC_GFXRES_PARAMS params,
636 MOS_RESOURCE_HANDLE& resource)
637 {
638 MOS_OS_FUNCTION_ENTER;
639
640 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
641
642 MOS_OS_CHK_NULL_RETURN(resource);
643 MOS_OS_CHK_NULL_RETURN(streamState);
644 MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
645
646 const char *bufname = params->pBufName;;
647 int32_t iSize = 0;
648 int32_t iPitch = 0;
649 MOS_LINUX_BO *bo = nullptr;
650 MOS_TILE_TYPE tileformat = params->TileType;
651 uint32_t tileformat_linux = TILING_NONE;
652 int32_t iHeight = params->dwHeight;
653 int32_t iAlignedHeight = 0;
654 GMM_RESCREATE_PARAMS gmmParams;
655 GMM_RESOURCE_INFO *gmmResourceInfo = nullptr;
656 GMM_RESOURCE_TYPE resourceType = RESOURCE_2D;
657 unsigned int patIndex = PAT_INDEX_INVALID;
658 bool isCpuCacheable = true;
659
660 MosUtilities::MosZeroMemory(&gmmParams, sizeof(gmmParams));
661
662 MOS_OS_CHK_NULL_RETURN(streamState->perStreamParameters);
663 auto perStreamParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
664
665 gmmParams.Usage = MosInterface::GetGmmResourceUsageType(params->ResUsageType);
666
667 switch (params->Format)
668 {
669 case Format_Buffer:
670 case Format_RAW:
671 resourceType = RESOURCE_BUFFER;
672 iAlignedHeight = 1;
673 //indicate buffer Restriction is Vertex.
674 gmmParams.Flags.Gpu.State = true;
675 break;
676 case Format_L8:
677 case Format_L16:
678 case Format_STMM:
679 case Format_AI44:
680 case Format_IA44:
681 case Format_R5G6B5:
682 case Format_R8G8B8:
683 case Format_X8R8G8B8:
684 case Format_A8R8G8B8:
685 case Format_X8B8G8R8:
686 case Format_A8B8G8R8:
687 case Format_R32S:
688 case Format_R32F:
689 case Format_V8U8:
690 case Format_YUY2:
691 case Format_UYVY:
692 case Format_P8:
693 case Format_A8:
694 case Format_AYUV:
695 case Format_NV12:
696 case Format_NV21:
697 case Format_YV12:
698 case Format_Buffer_2D:
699 case Format_R32U:
700 case Format_444P:
701 case Format_422H:
702 case Format_422V:
703 case Format_IMC3:
704 case Format_411P:
705 case Format_411R:
706 case Format_RGBP:
707 case Format_BGRP:
708 case Format_R16U:
709 case Format_R8U:
710 case Format_R8UN:
711 case Format_P010:
712 case Format_P016:
713 case Format_Y216:
714 case Format_Y416:
715 case Format_P208:
716 case Format_Y210:
717 case Format_Y410:
718 case Format_R16F:
719 resourceType = RESOURCE_2D;
720 //indicate buffer Restriction is Planar surface restrictions.
721 gmmParams.Flags.Gpu.Video = true;
722 break;
723 default:
724 MOS_OS_ASSERTMESSAGE("Unsupported format");
725 eStatus = MOS_STATUS_UNIMPLEMENTED;
726 return eStatus;
727 }
728
729 // Create GmmResourceInfo
730 gmmParams.BaseWidth = params->dwWidth;
731 gmmParams.BaseHeight = iAlignedHeight;
732 gmmParams.ArraySize = 1;
733 gmmParams.Type = resourceType;
734 gmmParams.Format = MosInterface::MosFmtToGmmFmt(params->Format);
735
736 MOS_OS_CHECK_CONDITION(gmmParams.Format == GMM_FORMAT_INVALID,
737 "Unsupported format",
738 MOS_STATUS_UNKNOWN);
739
740 switch (tileformat)
741 {
742 case MOS_TILE_Y:
743 gmmParams.Flags.Gpu.MMC = params->bIsCompressible;
744 tileformat_linux = TILING_Y;
745 break;
746 case MOS_TILE_X:
747 gmmParams.Flags.Info.TiledX = true;
748 tileformat_linux = TILING_X;
749 break;
750 default:
751 gmmParams.Flags.Info.Linear = true;
752 tileformat_linux = TILING_NONE;
753 }
754 gmmParams.Flags.Info.LocalOnly = MEDIA_IS_SKU(&perStreamParameters->m_skuTable, FtrLocalMemory);
755
756 MOS_OS_CHK_NULL_RETURN(perStreamParameters->pGmmClientContext);
757 resource->pGmmResInfo = gmmResourceInfo = perStreamParameters->pGmmClientContext->CreateResInfoObject(&gmmParams);
758
759 MOS_OS_CHK_NULL_RETURN(gmmResourceInfo);
760
761 switch (gmmResourceInfo->GetTileType())
762 {
763 case GMM_TILED_X:
764 tileformat = MOS_TILE_X;
765 tileformat_linux = TILING_X;
766 break;
767 case GMM_TILED_Y:
768 tileformat = MOS_TILE_Y;
769 tileformat_linux = TILING_Y;
770 break;
771 case GMM_NOT_TILED:
772 tileformat = MOS_TILE_LINEAR;
773 tileformat_linux = TILING_NONE;
774 break;
775 default:
776 tileformat = MOS_TILE_Y;
777 tileformat_linux = TILING_Y;
778 break;
779 }
780
781 if (params->TileType == MOS_TILE_Y)
782 {
783 gmmResourceInfo->SetMmcMode((GMM_RESOURCE_MMC_INFO)params->CompressionMode, 0);
784 }
785
786 iPitch = GFX_ULONG_CAST(gmmResourceInfo->GetRenderPitch());
787 iSize = GFX_ULONG_CAST(gmmResourceInfo->GetSizeSurface());
788 iHeight = gmmResourceInfo->GetBaseHeight();
789
790 patIndex = MosInterface::GetPATIndexFromGmm(perStreamParameters->pGmmClientContext, gmmResourceInfo);
791 isCpuCacheable = gmmResourceInfo->GetResFlags().Info.Cacheable;
792
793 // Only Linear and Y TILE supported
794 if (tileformat_linux == TILING_NONE)
795 {
796 struct mos_drm_bo_alloc alloc;
797 alloc.name = bufname;
798 alloc.size = iSize;
799 alloc.alignment = 4096;
800 alloc.ext.mem_type = MOS_MEMPOOL_VIDEOMEMORY;
801 alloc.ext.pat_index = patIndex;
802 alloc.ext.cpu_cacheable = isCpuCacheable;
803 bo = mos_bo_alloc(perStreamParameters->bufmgr, &alloc);
804 }
805 else
806 {
807 struct mos_drm_bo_alloc_tiled alloc_tiled;
808 alloc_tiled.name = bufname;
809 alloc_tiled.x = iPitch;
810 alloc_tiled.y = iSize / iPitch;
811 alloc_tiled.cpp = 1;
812 alloc_tiled.ext.tiling_mode = tileformat_linux;
813 alloc_tiled.ext.mem_type = MOS_MEMPOOL_VIDEOMEMORY;
814 alloc_tiled.ext.pat_index = patIndex;
815 alloc_tiled.ext.cpu_cacheable = isCpuCacheable;
816
817 bo = mos_bo_alloc_tiled(perStreamParameters->bufmgr, &alloc_tiled);
818 iPitch = (int32_t)alloc_tiled.pitch;
819 }
820
821 resource->bMapped = false;
822 if (bo)
823 {
824 resource->Format = params->Format;
825 resource->iWidth = params->dwWidth;
826 resource->iHeight = iHeight;
827 resource->iPitch = iPitch;
828 resource->iCount = 0;
829 resource->bufname = bufname;
830 resource->bo = bo;
831 resource->TileType = tileformat;
832 resource->TileModeGMM = (MOS_TILE_MODE_GMM)gmmResourceInfo->GetTileModeSurfaceState();
833 resource->bGMMTileEnabled = true;
834 resource->pData = (uint8_t *)bo->virt; //It is useful for batch buffer to fill commands
835 if (params->ResUsageType == MOS_CODEC_RESOURCE_USAGE_BEGIN_CODEC ||
836 params->ResUsageType >= MOS_HW_RESOURCE_USAGE_MEDIA_BATCH_BUFFERS)
837 {
838 resource->memObjCtrlState = MosInterface::GetCachePolicyMemoryObject(perStreamParameters->pGmmClientContext, MOS_MP_RESOURCE_USAGE_DEFAULT);
839 resource->mocsMosResUsageType = MOS_MP_RESOURCE_USAGE_DEFAULT;
840 }
841 else
842 {
843 resource->memObjCtrlState = MosInterface::GetCachePolicyMemoryObject(perStreamParameters->pGmmClientContext, params->ResUsageType);
844 resource->mocsMosResUsageType = params->ResUsageType;
845 }
846 MOS_OS_VERBOSEMESSAGE("Alloc %7d bytes (%d x %d resource), tile encoding.", iSize, params->dwWidth, iHeight, resource->TileModeGMM);
847 }
848 else
849 {
850 MOS_OS_ASSERTMESSAGE("Fail to Alloc %7d bytes (%d x %d resource).", iSize, params->dwWidth, params->dwHeight);
851 eStatus = MOS_STATUS_NO_SPACE;
852 }
853
854 return eStatus;
855 }
856
857 MOS_STATUS GraphicsResourceSpecificNext::FreeExternalResource(
858 MOS_STREAM_HANDLE streamState,
859 MOS_RESOURCE_HANDLE resource,
860 uint32_t flag)
861 {
862 MOS_OS_FUNCTION_ENTER;
863
864 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
865
866 MOS_OS_CHK_NULL_RETURN(resource);
867 MOS_OS_CHK_NULL_RETURN(streamState);
868 MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
869
870 if (resource && resource->bo)
871 {
872 OsContextSpecificNext *osCtx = static_cast<OsContextSpecificNext *>(streamState->osDeviceContext);
873 if (osCtx == nullptr)
874 {
875 MOS_OS_ASSERTMESSAGE("osCtx is nullptr!");
876 return MOS_STATUS_NULL_POINTER;
877 }
878 else
879 {
880 AuxTableMgr *auxTableMgr = osCtx->GetAuxTableMgr();
881
882 // Unmap Resource from Aux Table
883 if (auxTableMgr)
884 {
885 auxTableMgr->UnmapResource(resource->pGmmResInfo, resource->bo);
886 }
887 }
888
889 mos_bo_unreference((MOS_LINUX_BO *)(resource->bo));
890
891 MOS_OS_CHK_NULL_RETURN(streamState->perStreamParameters);
892 auto perStreamParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
893
894 if (perStreamParameters != nullptr && perStreamParameters->contextOffsetList.size())
895 {
896 MOS_CONTEXT *pOsCtx = perStreamParameters;
897 auto item_ctx = pOsCtx->contextOffsetList.begin();
898
899 for (; item_ctx != pOsCtx->contextOffsetList.end();)
900 {
901 if (item_ctx->target_bo == resource->bo)
902 {
903 item_ctx = pOsCtx->contextOffsetList.erase(item_ctx);
904 }
905 else
906 {
907 item_ctx++;
908 }
909 }
910 }
911
912 resource->bo = nullptr;
913 }
914
915 return eStatus;
916 }
917
918 void* GraphicsResourceSpecificNext::LockExternalResource(
919 MOS_STREAM_HANDLE streamState,
920 MOS_RESOURCE_HANDLE resource,
921 PMOS_LOCK_PARAMS flags)
922 {
923 MOS_OS_FUNCTION_ENTER;
924
925 void *pData = nullptr;
926 if (nullptr == streamState)
927 {
928 MOS_OS_ASSERTMESSAGE("input parameter streamState is NULL.");
929 return nullptr;
930 }
931
932 if (nullptr == resource)
933 {
934 MOS_OS_ASSERTMESSAGE("input parameter resource is NULL.");
935 return nullptr;
936 }
937
938 if (streamState->perStreamParameters == nullptr)
939 {
940 MOS_OS_ASSERTMESSAGE("perStreamParameters is nullptr, skip lock");
941 return nullptr;
942 }
943 auto perStreamParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
944 if (resource && resource->bo && resource->pGmmResInfo)
945 {
946 MOS_LINUX_BO *bo = resource->bo;
947 GMM_RESOURCE_FLAG GmmFlags = resource->pGmmResInfo->GetResFlags();
948
949 // Do decompression for a compressed surface before lock
950 if (!flags->NoDecompress &&
951 (((GmmFlags.Gpu.MMC || GmmFlags.Gpu.CCS) && GmmFlags.Info.MediaCompressed) ||
952 resource->pGmmResInfo->IsMediaMemoryCompressed(0)))
953 {
954 MosDecompression *mosDecompression = nullptr;
955 MOS_STATUS status = MosInterface::GetMosDecompressionFromStreamState(streamState, mosDecompression);
956 if (status != MOS_STATUS_SUCCESS)
957 {
958 MOS_OS_ASSERTMESSAGE("Get Mos Decompression From StreamState failed, skip lock");
959 return nullptr;
960 }
961 if (nullptr == mosDecompression)
962 {
963 MOS_OS_ASSERTMESSAGE("mosDecompression is NULL.");
964 return nullptr;
965 }
966 mosDecompression->MemoryDecompress(resource);
967 }
968
969 if (false == resource->bMapped)
970 {
971 if (perStreamParameters->bIsAtomSOC)
972 {
973 mos_bo_map_gtt(bo);
974 }
975 else
976 {
977 if (resource->TileType != MOS_TILE_LINEAR && !flags->TiledAsTiled)
978 {
979 if (perStreamParameters->bUseSwSwizzling)
980 {
981 mos_bo_map(bo, (OSKM_LOCKFLAG_WRITEONLY & flags->WriteOnly));
982 resource->MmapOperation = MOS_MMAP_OPERATION_MMAP;
983 if (resource->pSystemShadow == nullptr)
984 {
985 resource->pSystemShadow = (uint8_t *)MOS_AllocMemory(bo->size);
986 MOS_OS_CHECK_CONDITION((resource->pSystemShadow == nullptr), "Failed to allocate shadow surface", nullptr);
987 }
988 if (resource->pSystemShadow)
989 {
990 int32_t swizzleflags = perStreamParameters->bTileYFlag ? 0 : 1;
991 MOS_OS_CHECK_CONDITION((resource->TileType != MOS_TILE_Y), "Unsupported tile type", nullptr);
992 MOS_OS_CHECK_CONDITION((bo->size <= 0 || resource->iPitch <= 0), "Invalid BO size or pitch", nullptr);
993 MosUtilities::MosSwizzleData((uint8_t *)bo->virt, resource->pSystemShadow, MOS_TILE_Y, MOS_TILE_LINEAR, bo->size / resource->iPitch, resource->iPitch, swizzleflags);
994 }
995 }
996 else
997 {
998 mos_bo_map_gtt(bo);
999 resource->MmapOperation = MOS_MMAP_OPERATION_MMAP_GTT;
1000 }
1001 }
1002 else if (flags->Uncached)
1003 {
1004 mos_bo_map_wc(bo);
1005 resource->MmapOperation = MOS_MMAP_OPERATION_MMAP_WC;
1006 }
1007 else
1008 {
1009 mos_bo_map(bo, (OSKM_LOCKFLAG_WRITEONLY & flags->WriteOnly));
1010 resource->MmapOperation = MOS_MMAP_OPERATION_MMAP;
1011 }
1012 }
1013 resource->pData = resource->pSystemShadow ? resource->pSystemShadow : (uint8_t *)bo->virt;
1014 resource->bMapped = true;
1015 }
1016
1017 pData = resource->pData;
1018 }
1019
1020 MOS_OS_ASSERT(pData);
1021 return pData;
1022 }
1023
1024 MOS_STATUS GraphicsResourceSpecificNext::UnlockExternalResource(
1025 MOS_STREAM_HANDLE streamState,
1026 MOS_RESOURCE_HANDLE resource)
1027 {
1028 MOS_OS_FUNCTION_ENTER;
1029
1030 MOS_OS_CHK_NULL_RETURN(resource);
1031 MOS_OS_CHK_NULL_RETURN(streamState);
1032 MOS_OS_CHK_NULL_RETURN(streamState->osDeviceContext);
1033
1034 MOS_OS_CHK_NULL_RETURN(streamState->perStreamParameters);
1035 auto perStreamParameters = (PMOS_CONTEXT)streamState->perStreamParameters;
1036
1037 if (resource->bo)
1038 {
1039 if (true == resource->bMapped)
1040 {
1041 if (perStreamParameters->bIsAtomSOC)
1042 {
1043 mos_bo_unmap_gtt(resource->bo);
1044 }
1045 else
1046 {
1047 if (resource->pSystemShadow)
1048 {
1049 int32_t flags = perStreamParameters->bTileYFlag ? 0 : 1;
1050 MosUtilities::MosSwizzleData(resource->pSystemShadow, (uint8_t *)resource->bo->virt, MOS_TILE_LINEAR, MOS_TILE_Y, resource->bo->size / resource->iPitch, resource->iPitch, flags);
1051 MOS_FreeMemory(resource->pSystemShadow);
1052 resource->pSystemShadow = nullptr;
1053 }
1054
1055 switch (resource->MmapOperation)
1056 {
1057 case MOS_MMAP_OPERATION_MMAP_GTT:
1058 mos_bo_unmap_gtt(resource->bo);
1059 break;
1060 case MOS_MMAP_OPERATION_MMAP_WC:
1061 mos_bo_unmap_wc(resource->bo);
1062 break;
1063 case MOS_MMAP_OPERATION_MMAP:
1064 mos_bo_unmap(resource->bo);
1065 break;
1066 default:
1067 MOS_OS_ASSERTMESSAGE("Invalid mmap operation type");
1068 break;
1069 }
1070 }
1071 resource->bo->virt = nullptr;
1072 resource->bMapped = false;
1073 }
1074 resource->pData = nullptr;
1075 }
1076
1077 return MOS_STATUS_SUCCESS;
1078 }
1079
1080 MOS_STATUS GraphicsResourceSpecificNext::SetTileModebyForce(GMM_RESCREATE_PARAMS &gmmParams, MOS_TILE_MODE_GMM tileMode)
1081 {
1082 if (tileMode == MOS_TILE_64_GMM)
1083 {
1084 gmmParams.Flags.Info.Tile64 = true;
1085 }
1086 else if (tileMode == MOS_TILE_4_GMM)
1087 {
1088 gmmParams.Flags.Info.Tile4 = true;
1089 }
1090 return MOS_STATUS_SUCCESS;
1091 }