2019-02-14 11:38:04 +01:00
|
|
|
/*
|
2022-01-11 14:13:30 +00:00
|
|
|
* Copyright (C) 2020-2022 Intel Corporation
|
2019-02-14 11:38:04 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/linux/drm_allocation.h"
|
2019-02-27 11:39:32 +01:00
|
|
|
|
2020-06-26 09:14:36 +02:00
|
|
|
#include "shared/source/memory_manager/residency.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/linux/drm_buffer_object.h"
|
|
|
|
|
#include "shared/source/os_interface/linux/drm_memory_manager.h"
|
2021-12-14 14:09:19 +00:00
|
|
|
#include "shared/source/os_interface/linux/ioctl_helper.h"
|
2020-08-11 14:00:41 +02:00
|
|
|
#include "shared/source/os_interface/os_context.h"
|
2019-02-14 11:38:04 +01:00
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
namespace NEO {
|
2022-01-11 14:13:30 +00:00
|
|
|
|
|
|
|
|
DrmAllocation::~DrmAllocation() {
|
|
|
|
|
[[maybe_unused]] int retCode;
|
|
|
|
|
for (auto &memory : this->memoryToUnmap) {
|
|
|
|
|
retCode = memory.unmapFunction(memory.pointer, memory.size);
|
|
|
|
|
DEBUG_BREAK_IF(retCode != 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 11:38:04 +01:00
|
|
|
std::string DrmAllocation::getAllocationInfoString() const {
|
|
|
|
|
std::stringstream ss;
|
2019-09-01 21:36:15 +02:00
|
|
|
for (auto bo : bufferObjects) {
|
|
|
|
|
if (bo != nullptr) {
|
|
|
|
|
ss << " Handle: " << bo->peekHandle();
|
|
|
|
|
}
|
2019-02-14 11:38:04 +01:00
|
|
|
}
|
|
|
|
|
return ss.str();
|
|
|
|
|
}
|
2019-06-04 13:42:57 +02:00
|
|
|
|
2019-06-06 16:26:47 +02:00
|
|
|
uint64_t DrmAllocation::peekInternalHandle(MemoryManager *memoryManager) {
|
2020-01-07 07:42:40 +01:00
|
|
|
return static_cast<uint64_t>((static_cast<DrmMemoryManager *>(memoryManager))->obtainFdFromHandle(getBO()->peekHandle(), this->rootDeviceIndex));
|
2019-06-06 16:26:47 +02:00
|
|
|
}
|
2020-06-26 09:14:36 +02:00
|
|
|
|
2021-10-06 23:22:22 +00:00
|
|
|
void DrmAllocation::setCachePolicy(CachePolicy memType) {
|
|
|
|
|
for (auto bo : bufferObjects) {
|
|
|
|
|
if (bo != nullptr) {
|
|
|
|
|
bo->setCachePolicy(memType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-09 00:31:32 +00:00
|
|
|
bool DrmAllocation::setCacheAdvice(Drm *drm, size_t regionSize, CacheRegion regionIndex) {
|
2021-01-29 22:23:06 +00:00
|
|
|
if (!drm->getCacheInfo()->getCacheRegion(regionSize, regionIndex)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 22:58:32 +00:00
|
|
|
if (fragmentsStorage.fragmentCount > 0) {
|
|
|
|
|
for (uint32_t i = 0; i < fragmentsStorage.fragmentCount; i++) {
|
2021-04-02 17:01:51 +00:00
|
|
|
auto bo = static_cast<OsHandleLinux *>(fragmentsStorage.fragmentStorageData[i].osHandleStorage)->bo;
|
2021-02-16 22:58:32 +00:00
|
|
|
bo->setCacheRegion(regionIndex);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-29 22:23:06 +00:00
|
|
|
for (auto bo : bufferObjects) {
|
|
|
|
|
if (bo != nullptr) {
|
|
|
|
|
bo->setCacheRegion(regionIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 14:53:31 +00:00
|
|
|
int DrmAllocation::makeBOsResident(OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind) {
|
2020-06-26 09:14:36 +02:00
|
|
|
if (this->fragmentsStorage.fragmentCount) {
|
|
|
|
|
for (unsigned int f = 0; f < this->fragmentsStorage.fragmentCount; f++) {
|
2020-08-11 14:00:41 +02:00
|
|
|
if (!this->fragmentsStorage.fragmentStorageData[f].residency->resident[osContext->getContextId()]) {
|
2022-01-07 14:53:31 +00:00
|
|
|
int retVal = bindBO(static_cast<OsHandleLinux *>(this->fragmentsStorage.fragmentStorageData[f].osHandleStorage)->bo, osContext, vmHandleId, bufferObjects, bind);
|
|
|
|
|
if (retVal) {
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
2020-08-11 14:00:41 +02:00
|
|
|
this->fragmentsStorage.fragmentStorageData[f].residency->resident[osContext->getContextId()] = true;
|
2020-06-26 09:14:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-01-07 14:53:31 +00:00
|
|
|
int retVal = bindBOs(osContext, vmHandleId, bufferObjects, bind);
|
|
|
|
|
if (retVal) {
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
2020-06-26 09:14:36 +02:00
|
|
|
}
|
2022-01-07 14:53:31 +00:00
|
|
|
|
|
|
|
|
return 0;
|
2020-06-26 09:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 14:53:31 +00:00
|
|
|
int DrmAllocation::bindBO(BufferObject *bo, OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind) {
|
|
|
|
|
auto retVal = 0;
|
2020-06-26 09:14:36 +02:00
|
|
|
if (bo) {
|
2021-11-08 21:42:07 +00:00
|
|
|
bo->requireExplicitResidency(bo->peekDrm()->hasPageFaultSupport() && !shouldAllocationPageFault(bo->peekDrm()));
|
2020-07-02 11:49:46 +02:00
|
|
|
if (bufferObjects) {
|
|
|
|
|
if (bo->peekIsReusableAllocation()) {
|
|
|
|
|
for (auto bufferObject : *bufferObjects) {
|
|
|
|
|
if (bufferObject == bo) {
|
2022-01-07 14:53:31 +00:00
|
|
|
return 0;
|
2020-07-02 11:49:46 +02:00
|
|
|
}
|
2020-06-26 09:14:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-02 11:49:46 +02:00
|
|
|
bufferObjects->push_back(bo);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
if (bind) {
|
2020-10-16 12:59:27 +02:00
|
|
|
retVal = bo->bind(osContext, vmHandleId);
|
2020-07-02 11:49:46 +02:00
|
|
|
} else {
|
2020-10-16 12:59:27 +02:00
|
|
|
retVal = bo->unbind(osContext, vmHandleId);
|
2020-07-02 11:49:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-26 09:14:36 +02:00
|
|
|
}
|
2022-01-07 14:53:31 +00:00
|
|
|
return retVal;
|
2020-06-26 09:14:36 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-14 13:28:47 +02:00
|
|
|
void DrmAllocation::registerBOBindExtHandle(Drm *drm) {
|
|
|
|
|
if (!drm->resourceRegistrationEnabled()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Drm::ResourceClass resourceClass = Drm::ResourceClass::MaxSize;
|
|
|
|
|
|
|
|
|
|
switch (this->allocationType) {
|
|
|
|
|
case GraphicsAllocation::AllocationType::DEBUG_CONTEXT_SAVE_AREA:
|
|
|
|
|
resourceClass = Drm::ResourceClass::ContextSaveArea;
|
|
|
|
|
break;
|
|
|
|
|
case GraphicsAllocation::AllocationType::DEBUG_SBA_TRACKING_BUFFER:
|
|
|
|
|
resourceClass = Drm::ResourceClass::SbaTrackingBuffer;
|
|
|
|
|
break;
|
|
|
|
|
case GraphicsAllocation::AllocationType::KERNEL_ISA:
|
|
|
|
|
resourceClass = Drm::ResourceClass::Isa;
|
|
|
|
|
break;
|
2020-10-14 16:46:11 +02:00
|
|
|
case GraphicsAllocation::AllocationType::DEBUG_MODULE_AREA:
|
|
|
|
|
resourceClass = Drm::ResourceClass::ModuleHeapDebugArea;
|
|
|
|
|
break;
|
2020-09-14 13:28:47 +02:00
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (resourceClass != Drm::ResourceClass::MaxSize) {
|
|
|
|
|
uint64_t gpuAddress = getGpuAddress();
|
|
|
|
|
auto handle = drm->registerResource(resourceClass, &gpuAddress, sizeof(gpuAddress));
|
2020-09-17 13:27:32 +02:00
|
|
|
registeredBoBindHandles.push_back(handle);
|
2020-10-26 14:48:20 +01:00
|
|
|
|
2020-09-14 13:28:47 +02:00
|
|
|
auto &bos = getBOs();
|
|
|
|
|
|
|
|
|
|
for (auto bo : bos) {
|
2020-09-17 13:27:32 +02:00
|
|
|
if (bo) {
|
|
|
|
|
bo->addBindExtHandle(handle);
|
2020-10-09 10:48:37 +02:00
|
|
|
bo->markForCapture();
|
2020-10-26 14:48:20 +01:00
|
|
|
if (resourceClass == Drm::ResourceClass::Isa) {
|
|
|
|
|
auto cookieHandle = drm->registerIsaCookie(handle);
|
|
|
|
|
bo->addBindExtHandle(cookieHandle);
|
|
|
|
|
registeredBoBindHandles.push_back(cookieHandle);
|
|
|
|
|
}
|
2021-06-17 12:23:51 +00:00
|
|
|
|
|
|
|
|
bo->requireImmediateBinding(true);
|
2020-09-17 13:27:32 +02:00
|
|
|
}
|
2020-09-14 13:28:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 15:52:18 -07:00
|
|
|
void DrmAllocation::linkWithRegisteredHandle(uint32_t handle) {
|
|
|
|
|
auto &bos = getBOs();
|
|
|
|
|
for (auto bo : bos) {
|
|
|
|
|
if (bo) {
|
|
|
|
|
bo->addBindExtHandle(handle);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-17 13:27:32 +02:00
|
|
|
void DrmAllocation::freeRegisteredBOBindExtHandles(Drm *drm) {
|
2021-06-15 10:15:20 +00:00
|
|
|
for (auto it = registeredBoBindHandles.rbegin(); it != registeredBoBindHandles.rend(); ++it) {
|
|
|
|
|
drm->unregisterResource(*it);
|
2020-09-17 13:27:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-03-09 23:02:59 +00:00
|
|
|
|
|
|
|
|
void DrmAllocation::markForCapture() {
|
|
|
|
|
auto &bos = getBOs();
|
|
|
|
|
for (auto bo : bos) {
|
|
|
|
|
if (bo) {
|
|
|
|
|
bo->markForCapture();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-14 14:09:19 +00:00
|
|
|
|
|
|
|
|
bool DrmAllocation::setMemAdvise(Drm *drm, MemAdviseFlags flags) {
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
|
|
if (flags.cached_memory != enabledMemAdviseFlags.cached_memory) {
|
|
|
|
|
CachePolicy memType = flags.cached_memory ? CachePolicy::WriteBack : CachePolicy::Uncached;
|
|
|
|
|
setCachePolicy(memType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto ioctlHelper = IoctlHelper::get(drm);
|
|
|
|
|
if (flags.non_atomic != enabledMemAdviseFlags.non_atomic) {
|
|
|
|
|
for (auto bo : bufferObjects) {
|
|
|
|
|
if (bo != nullptr) {
|
|
|
|
|
success &= ioctlHelper->setVmBoAdvise(drm, bo->peekHandle(), ioctlHelper->getAtomicAdvise(flags.non_atomic), nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flags.device_preferred_location != enabledMemAdviseFlags.device_preferred_location) {
|
|
|
|
|
drm_i915_gem_memory_class_instance region{};
|
|
|
|
|
for (auto handleId = 0u; handleId < EngineLimits::maxHandleCount; handleId++) {
|
|
|
|
|
auto bo = bufferObjects[handleId];
|
|
|
|
|
if (bo != nullptr) {
|
|
|
|
|
if (flags.device_preferred_location) {
|
|
|
|
|
region.memory_class = I915_MEMORY_CLASS_DEVICE;
|
|
|
|
|
region.memory_instance = handleId;
|
|
|
|
|
} else {
|
|
|
|
|
region.memory_class = -1;
|
|
|
|
|
region.memory_instance = 0;
|
|
|
|
|
}
|
|
|
|
|
success &= ioctlHelper->setVmBoAdvise(drm, bo->peekHandle(), ioctlHelper->getPreferredLocationAdvise(), ®ion);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
|
enabledMemAdviseFlags = flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 14:13:30 +00:00
|
|
|
void DrmAllocation::registerMemoryToUnmap(void *pointer, size_t size, DrmAllocation::MemoryUnmapFunction unmapFunction) {
|
|
|
|
|
this->memoryToUnmap.push_back({pointer, size, unmapFunction});
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
} // namespace NEO
|