1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
/*
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "shared/source/os_interface/linux/drm_allocation.h"
#include "shared/source/debug_settings/debug_settings_manager.h"
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/gmm_helper/gmm.h"
#include "shared/source/helpers/basic_math.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/memory_manager/residency.h"
#include "shared/source/os_interface/linux/cache_info.h"
#include "shared/source/os_interface/linux/drm_buffer_object.h"
#include "shared/source/os_interface/linux/drm_memory_manager.h"
#include "shared/source/os_interface/linux/drm_neo.h"
#include "shared/source/os_interface/linux/i915_prelim.h"
#include "shared/source/os_interface/linux/ioctl_helper.h"
#include "shared/source/os_interface/linux/memory_info.h"
#include "shared/source/os_interface/linux/os_context_linux.h"
#include "shared/source/os_interface/os_context.h"
#include "shared/source/os_interface/product_helper.h"
#include <sstream>
namespace NEO {
DrmAllocation::~DrmAllocation() {
[[maybe_unused]] int retCode;
for (auto &memory : this->memoryToUnmap) {
retCode = memory.unmapFunction(memory.pointer, memory.size);
DEBUG_BREAK_IF(retCode != 0);
}
}
std::string DrmAllocation::getAllocationInfoString() const {
std::stringstream ss;
for (auto bo : bufferObjects) {
if (bo != nullptr) {
ss << " Handle: " << bo->peekHandle();
}
}
return ss.str();
}
std::string DrmAllocation::getPatIndexInfoString(const ProductHelper &productHelper) const {
std::stringstream ss;
auto bo = getBO();
if (bo) {
ss << " PATIndex: " << bo->peekPatIndex() << ",";
}
auto gmm = getDefaultGmm();
if (gmm) {
ss << " Gmm resource usage: "
<< "[ " << gmm->getUsageTypeString() << " ],";
ss << " Cacheable: " << gmm->resourceParams.Flags.Info.Cacheable;
ss << " NotLockable: " << gmm->resourceParams.Flags.Info.NotLockable;
}
return ss.str();
}
void DrmAllocation::clearInternalHandle(uint32_t handleId) {
handles[handleId] = std::numeric_limits<uint64_t>::max();
}
int DrmAllocation::createInternalHandle(MemoryManager *memoryManager, uint32_t handleId, uint64_t &handle) {
return peekInternalHandle(memoryManager, handleId, handle);
}
int DrmAllocation::peekInternalHandle(MemoryManager *memoryManager, uint64_t &handle) {
return peekInternalHandle(memoryManager, 0u, handle);
}
int DrmAllocation::peekInternalHandle(MemoryManager *memoryManager, uint32_t handleId, uint64_t &handle) {
if (handles[handleId] != std::numeric_limits<uint64_t>::max()) {
handle = handles[handleId];
return 0;
}
int64_t ret = static_cast<int64_t>((static_cast<DrmMemoryManager *>(memoryManager))->obtainFdFromHandle(getBufferObjectToModify(handleId)->peekHandle(), this->rootDeviceIndex));
if (ret < 0) {
return -1;
}
handle = handles[handleId] = ret;
return 0;
}
void DrmAllocation::setCachePolicy(CachePolicy memType) {
for (auto bo : bufferObjects) {
if (bo != nullptr) {
bo->setCachePolicy(memType);
}
}
}
bool DrmAllocation::setPreferredLocation(Drm *drm, PreferredLocation memoryLocation) {
auto ioctlHelper = drm->getIoctlHelper();
auto remainingMemoryBanks = storageInfo.memoryBanks;
bool success = true;
auto pHwInfo = drm->getRootDeviceEnvironment().getHardwareInfo();
if (this->storageInfo.isChunked && debugManager.flags.EnableBOChunkingPreferredLocationHint.get() == 1) {
prelim_drm_i915_gem_memory_class_instance region{};
region.memory_class = NEO::PrelimI915::PRELIM_I915_MEMORY_CLASS_DEVICE;
auto banks = std::bitset<4>(remainingMemoryBanks);
MemRegionsVec memRegions{};
size_t currentBank = 0;
size_t i = 0;
while (i < banks.count()) {
if (banks.test(currentBank)) {
auto regionClassAndInstance = drm->getMemoryInfo()->getMemoryRegionClassAndInstance(1u << currentBank, *pHwInfo);
memRegions.push_back(regionClassAndInstance);
i++;
}
currentBank++;
}
for (uint32_t i = 0; i < this->storageInfo.numOfChunks; i++) {
// Depth-first
region.memory_instance = memRegions[i / (this->storageInfo.numOfChunks / memRegions.size())].memoryInstance;
uint64_t chunkLength = (bufferObjects[0]->peekSize() / this->storageInfo.numOfChunks);
uint64_t chunkStart = i * chunkLength;
printDebugString(debugManager.flags.PrintBOChunkingLogs.get(), stdout,
"Setting PRELIM_DRM_I915_GEM_VM_ADVISE for BO-%d chunk 0x%lx chunkLength %ld memory_class %d, memory_region %d\n",
bufferObjects[0]->peekHandle(),
chunkStart,
chunkLength,
region.memory_class,
region.memory_instance);
success &= ioctlHelper->setVmBoAdviseForChunking(bufferObjects[0]->peekHandle(),
chunkStart,
chunkLength,
ioctlHelper->getPreferredLocationAdvise(),
®ion);
}
return success;
}
for (uint8_t handleId = 0u; handleId < numHandles; handleId++) {
auto memoryInstance = Math::getMinLsbSet(static_cast<uint32_t>(remainingMemoryBanks.to_ulong()));
std::optional<MemoryClassInstance> region = ioctlHelper->getPreferredLocationRegion(memoryLocation, memoryInstance);
if (region != std::nullopt) {
auto bo = this->getBOs()[handleId];
success &= ioctlHelper->setVmBoAdvise(bo->peekHandle(), ioctlHelper->getPreferredLocationAdvise(), ®ion);
}
remainingMemoryBanks.reset(memoryInstance);
}
return success;
}
bool DrmAllocation::setCacheRegion(Drm *drm, CacheRegion regionIndex) {
if (regionIndex == CacheRegion::defaultRegion) {
return true;
}
auto cacheInfo = drm->getCacheInfo();
if (cacheInfo == nullptr) {
return false;
}
const auto cacheLevel{cacheInfo->getLevelForRegion(regionIndex)};
const auto maxCacheRegions{cacheInfo->getMaxReservationNumCacheRegions(cacheLevel)};
const auto maxReservationCacheSize{cacheInfo->getMaxReservationCacheSize(cacheLevel)};
const auto regionSize{(maxCacheRegions > 0) ? maxReservationCacheSize / maxCacheRegions : 0};
if (regionSize == 0) {
return false;
}
return setCacheAdvice(drm, regionSize, regionIndex, !isAllocatedInLocalMemoryPool());
}
bool DrmAllocation::setCacheAdvice(Drm *drm, size_t regionSize, CacheRegion regionIndex, bool isSystemMemoryPool) {
if (!drm->getCacheInfo()->getCacheRegion(regionSize, regionIndex)) {
return false;
}
auto patIndex = drm->getPatIndex(getDefaultGmm(), allocationType, regionIndex, CachePolicy::writeBack, true, isSystemMemoryPool);
if (fragmentsStorage.fragmentCount > 0) {
for (uint32_t i = 0; i < fragmentsStorage.fragmentCount; i++) {
auto bo = static_cast<OsHandleLinux *>(fragmentsStorage.fragmentStorageData[i].osHandleStorage)->bo;
bo->setCacheRegion(regionIndex);
bo->setPatIndex(patIndex);
}
return true;
}
for (auto bo : bufferObjects) {
if (bo != nullptr) {
bo->setCacheRegion(regionIndex);
bo->setPatIndex(patIndex);
}
}
return true;
}
bool DrmAllocation::prefetchBOWithChunking(Drm *drm) {
auto getSubDeviceIds = [](const DeviceBitfield &subDeviceBitfield) {
SubDeviceIdsVec subDeviceIds;
for (auto subDeviceId = 0u; subDeviceId < subDeviceBitfield.size(); subDeviceId++) {
if (subDeviceBitfield.test(subDeviceId)) {
subDeviceIds.push_back(subDeviceId);
}
}
return subDeviceIds;
};
auto bo = this->getBO();
auto ioctlHelper = drm->getIoctlHelper();
auto memoryClassDevice = ioctlHelper->getDrmParamValue(DrmParam::memoryClassDevice);
auto subDeviceIds = getSubDeviceIds(storageInfo.subDeviceBitfield);
uint32_t chunksPerSubDevice = this->storageInfo.numOfChunks / subDeviceIds.size();
uint64_t chunkLength = (bo->peekSize() / this->storageInfo.numOfChunks);
bool success = true;
for (uint32_t i = 0; i < this->storageInfo.numOfChunks; i++) {
uint64_t chunkStart = bo->peekAddress() + i * chunkLength;
auto subDeviceId = subDeviceIds[i / chunksPerSubDevice];
for (auto vmHandleId : subDeviceIds) {
auto region = static_cast<uint32_t>((memoryClassDevice << 16u) | subDeviceId);
auto vmId = drm->getVirtualMemoryAddressSpace(vmHandleId);
PRINT_DEBUG_STRING(debugManager.flags.PrintBOPrefetchingResult.get(), stdout,
"prefetching BO=%d to VM %u, drmVmId=%u, range: %llx - %llx, size: %lld, region: %x\n",
bo->peekHandle(), vmId, vmHandleId, chunkStart, ptrOffset(chunkStart, chunkLength), chunkLength, region);
success &= ioctlHelper->setVmPrefetch(chunkStart, chunkLength, region, vmId);
PRINT_DEBUG_STRING(debugManager.flags.PrintBOPrefetchingResult.get(), stdout,
"prefetched BO=%d to VM %u, drmVmId=%u, range: %llx - %llx, size: %lld, region: %x, result: %d\n",
bo->peekHandle(), vmId, vmHandleId, chunkStart, ptrOffset(chunkStart, chunkLength), chunkLength, region, success);
}
}
return success;
}
int DrmAllocation::makeBOsResident(OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind, const bool forcePagingFence) {
if (this->fragmentsStorage.fragmentCount) {
for (unsigned int f = 0; f < this->fragmentsStorage.fragmentCount; f++) {
if (!this->fragmentsStorage.fragmentStorageData[f].residency->resident[osContext->getContextId()]) {
int retVal = bindBO(static_cast<OsHandleLinux *>(this->fragmentsStorage.fragmentStorageData[f].osHandleStorage)->bo, osContext, vmHandleId, bufferObjects, bind, forcePagingFence);
if (retVal) {
return retVal;
}
this->fragmentsStorage.fragmentStorageData[f].residency->resident[osContext->getContextId()] = true;
}
}
} else {
int retVal = bindBOs(osContext, vmHandleId, bufferObjects, bind, forcePagingFence);
if (retVal) {
return retVal;
}
}
return 0;
}
int DrmAllocation::bindBO(BufferObject *bo, OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind, const bool forcePagingFence) {
auto retVal = 0;
if (bo) {
bo->requireExplicitResidency(bo->peekDrm()->hasPageFaultSupport() && !shouldAllocationPageFault(bo->peekDrm()));
if (bufferObjects) {
if (bo->peekIsReusableAllocation()) {
for (auto bufferObject : *bufferObjects) {
if (bufferObject == bo) {
return 0;
}
}
}
bufferObjects->push_back(bo);
} else {
if (bind) {
retVal = bo->bind(osContext, vmHandleId, forcePagingFence);
} else {
retVal = bo->unbind(osContext, vmHandleId);
}
}
}
return retVal;
}
int DrmAllocation::bindBOs(OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind, const bool forcePagingFence) {
int retVal = 0;
if (this->storageInfo.getNumBanks() > 1) {
auto &bos = this->getBOs();
if (this->storageInfo.tileInstanced) {
auto bo = bos[vmHandleId];
retVal = bindBO(bo, osContext, vmHandleId, bufferObjects, bind, forcePagingFence);
if (retVal) {
return retVal;
}
} else {
for (auto bo : bos) {
retVal = bindBO(bo, osContext, vmHandleId, bufferObjects, bind, forcePagingFence);
if (retVal) {
return retVal;
}
}
}
} else {
auto bo = this->getBO();
retVal = bindBO(bo, osContext, vmHandleId, bufferObjects, bind, forcePagingFence);
if (retVal) {
return retVal;
}
}
return 0;
}
bool DrmAllocation::prefetchBO(BufferObject *bo, uint32_t vmHandleId, uint32_t subDeviceId) {
auto drm = bo->peekDrm();
auto ioctlHelper = drm->getIoctlHelper();
auto memoryClassDevice = ioctlHelper->getDrmParamValue(DrmParam::memoryClassDevice);
auto region = static_cast<uint32_t>((memoryClassDevice << 16u) | subDeviceId);
auto vmId = drm->getVirtualMemoryAddressSpace(vmHandleId);
auto result = ioctlHelper->setVmPrefetch(bo->peekAddress(), bo->peekSize(), region, vmId);
PRINT_DEBUG_STRING(debugManager.flags.PrintBOPrefetchingResult.get(), stdout,
"prefetch BO=%d to VM %u, drmVmId=%u, range: %llx - %llx, size: %lld, region: %x, result: %d\n",
bo->peekHandle(), vmId, vmHandleId, bo->peekAddress(), ptrOffset(bo->peekAddress(), bo->peekSize()), bo->peekSize(), region, result);
return result;
}
void DrmAllocation::registerBOBindExtHandle(Drm *drm) {
if (!drm->getIoctlHelper()->resourceRegistrationEnabled()) {
return;
}
drm->getIoctlHelper()->registerBOBindHandle(drm, this);
}
void DrmAllocation::setAsReadOnly() {
auto &bos = getBOs();
for (auto &bo : bos) {
if (bo) {
bo->setAsReadOnly(true);
}
}
}
void DrmAllocation::linkWithRegisteredHandle(uint32_t handle) {
auto &bos = getBOs();
for (auto bo : bos) {
if (bo) {
bo->addBindExtHandle(handle);
bo->requireImmediateBinding(true);
}
}
}
void DrmAllocation::freeRegisteredBOBindExtHandles(Drm *drm) {
for (auto it = registeredBoBindHandles.rbegin(); it != registeredBoBindHandles.rend(); ++it) {
drm->unregisterResource(*it);
}
}
void DrmAllocation::markForCapture() {
auto &bos = getBOs();
for (auto bo : bos) {
if (bo) {
bo->markForCapture();
}
}
}
bool DrmAllocation::shouldAllocationPageFault(const Drm *drm) {
if (!drm->hasPageFaultSupport()) {
return false;
}
if (debugManager.flags.EnableImplicitMigrationOnFaultableHardware.get() != -1) {
return debugManager.flags.EnableImplicitMigrationOnFaultableHardware.get();
}
switch (this->allocationType) {
case AllocationType::unifiedSharedMemory:
return drm->hasKmdMigrationSupport();
case AllocationType::buffer:
return debugManager.flags.UseKmdMigrationForBuffers.get() > 0;
default:
return false;
}
}
bool DrmAllocation::setMemAdvise(Drm *drm, MemAdviseFlags flags) {
bool success = true;
if (flags.cachedMemory != enabledMemAdviseFlags.cachedMemory) {
CachePolicy memType = flags.cachedMemory ? CachePolicy::writeBack : CachePolicy::uncached;
setCachePolicy(memType);
}
auto ioctlHelper = drm->getIoctlHelper();
if (flags.nonAtomic != enabledMemAdviseFlags.nonAtomic) {
for (auto bo : bufferObjects) {
if (bo != nullptr) {
success &= ioctlHelper->setVmBoAdvise(bo->peekHandle(), ioctlHelper->getAtomicAdvise(flags.nonAtomic), nullptr);
}
}
}
if (flags.devicePreferredLocation != enabledMemAdviseFlags.devicePreferredLocation) {
success &= setPreferredLocation(drm, flags.devicePreferredLocation ? PreferredLocation::device : PreferredLocation::clear);
}
if (flags.systemPreferredLocation != enabledMemAdviseFlags.systemPreferredLocation) {
success &= setPreferredLocation(drm, flags.systemPreferredLocation ? PreferredLocation::system : PreferredLocation::defaultLocation);
}
if (success) {
enabledMemAdviseFlags = flags;
}
return success;
}
bool DrmAllocation::setAtomicAccess(Drm *drm, size_t size, AtomicAccessMode mode) {
bool success = true;
if (mode == AtomicAccessMode::host) {
// Host mode not currently supported by KMD
return success;
}
auto ioctlHelper = drm->getIoctlHelper();
for (auto bo : bufferObjects) {
if (bo != nullptr) {
success &= ioctlHelper->setVmBoAdvise(bo->peekHandle(), ioctlHelper->getAtomicAccess(mode), nullptr);
}
}
return success;
}
bool DrmAllocation::setMemPrefetch(Drm *drm, SubDeviceIdsVec &subDeviceIds) {
UNRECOVERABLE_IF(subDeviceIds.size() == 0);
bool success = true;
if (numHandles > 1) {
for (uint8_t handleId = 0u; handleId < numHandles; handleId++) {
auto bo = this->getBOs()[handleId];
auto subDeviceId = handleId;
if (debugManager.flags.KMDSupportForCrossTileMigrationPolicy.get() > 0) {
subDeviceId = subDeviceIds[handleId % subDeviceIds.size()];
}
for (auto vmHandleId : subDeviceIds) {
success &= prefetchBO(bo, vmHandleId, subDeviceId);
}
}
} else {
auto bo = this->getBO();
if (bo->isChunked()) {
auto drm = bo->peekDrm();
success = prefetchBOWithChunking(const_cast<Drm *>(drm));
} else {
success = prefetchBO(bo, subDeviceIds[0], subDeviceIds[0]);
}
}
return success;
}
void DrmAllocation::registerMemoryToUnmap(void *pointer, size_t size, DrmAllocation::MemoryUnmapFunction unmapFunction) {
this->memoryToUnmap.push_back({pointer, size, unmapFunction});
}
uint64_t DrmAllocation::getHandleAddressBase(uint32_t handleIndex) {
return bufferObjects[handleIndex]->peekAddress();
}
size_t DrmAllocation::getHandleSize(uint32_t handleIndex) {
return bufferObjects[handleIndex]->peekSize();
}
} // namespace NEO
|