mirror of
https://github.com/intel/compute-runtime.git
synced 2025-12-19 16:24:18 +08:00
Move free function to DrmMemoryManager
Signed-off-by: Szymon Morek <szymon.morek@intel.com>
This commit is contained in:
committed by
Compute-Runtime-Automation
parent
263becc3f8
commit
e368e6c20b
@@ -39,14 +39,6 @@ class DrmMockCustomImpl : public DrmMockCustom {
|
||||
__u32 createExtHandle = 0;
|
||||
__u64 createExtExtensions = 0;
|
||||
|
||||
//DRM_IOCTL_I915_GEM_MMAP_OFFSET
|
||||
__u32 mmapOffsetHandle = 0;
|
||||
__u32 mmapOffsetPad = 0;
|
||||
__u64 mmapOffsetOffset = 0;
|
||||
__u64 mmapOffsetFlags = 0;
|
||||
|
||||
bool failOnMmapOffset = false;
|
||||
|
||||
int ioctlExtra(unsigned long request, void *arg) override {
|
||||
switch (request) {
|
||||
case DRM_IOCTL_I915_GEM_CREATE_EXT: {
|
||||
@@ -56,17 +48,6 @@ class DrmMockCustomImpl : public DrmMockCustom {
|
||||
createExtExtensions = createExtParams->extensions;
|
||||
ioctlImpl_cnt.gemCreateExt++;
|
||||
} break;
|
||||
case DRM_IOCTL_I915_GEM_MMAP_OFFSET: {
|
||||
auto mmapOffsetParams = reinterpret_cast<drm_i915_gem_mmap_offset *>(arg);
|
||||
mmapOffsetHandle = mmapOffsetParams->handle;
|
||||
mmapOffsetPad = mmapOffsetParams->pad;
|
||||
mmapOffsetOffset = mmapOffsetParams->offset;
|
||||
mmapOffsetFlags = mmapOffsetParams->flags;
|
||||
ioctlImpl_cnt.gemMmapOffset++;
|
||||
if (failOnMmapOffset == true) {
|
||||
return -1;
|
||||
}
|
||||
} break;
|
||||
default: {
|
||||
std::cout << "unexpected IOCTL: " << std::hex << request << std::endl;
|
||||
UNRECOVERABLE_IF(true);
|
||||
|
||||
@@ -88,48 +88,6 @@ class DrmMemoryManagerLocalMemoryWithCustomMockTest : public ::testing::Test {
|
||||
std::unique_ptr<TestedDrmMemoryManager> memoryManager;
|
||||
};
|
||||
|
||||
extern bool retrieveMmapOffsetForBufferObject(Drm &drm, BufferObject &bo, uint64_t flags, uint64_t &offset);
|
||||
|
||||
TEST_F(DrmMemoryManagerLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectSucceedsThenReturnTrueAndCorrectOffset) {
|
||||
BufferObject bo(mock, 1, 1024, 0);
|
||||
mock->offset = 21;
|
||||
|
||||
uint64_t offset = 0;
|
||||
auto ret = retrieveMmapOffsetForBufferObject(*mock, bo, 0, offset);
|
||||
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_EQ(21u, offset);
|
||||
}
|
||||
|
||||
TEST_F(DrmMemoryManagerLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectFailsThenReturnFalse) {
|
||||
BufferObject bo(mock, 1, 1024, 0);
|
||||
mock->mmapOffsetRetVal = -1;
|
||||
|
||||
uint64_t offset = 0;
|
||||
auto ret = retrieveMmapOffsetForBufferObject(*mock, bo, 0, offset);
|
||||
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
TEST_F(DrmMemoryManagerLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectIsCalledThenApplyCorrectFlags) {
|
||||
BufferObject bo(mock, 1, 1024, 0);
|
||||
|
||||
uint64_t offset = 0;
|
||||
auto ret = retrieveMmapOffsetForBufferObject(*mock, bo, 0, offset);
|
||||
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_EQ(4u, mock->mmapOffsetFlagsReceived);
|
||||
|
||||
mock->mmapOffsetRetVal = -1;
|
||||
|
||||
for (uint64_t flags : {I915_MMAP_OFFSET_WC, I915_MMAP_OFFSET_WB}) {
|
||||
ret = retrieveMmapOffsetForBufferObject(*mock, bo, flags, offset);
|
||||
|
||||
EXPECT_FALSE(ret);
|
||||
EXPECT_EQ(flags, mock->mmapOffsetFlagsReceived);
|
||||
}
|
||||
}
|
||||
|
||||
HWTEST2_F(DrmMemoryManagerLocalMemoryTest, givenDrmMemoryManagerWhenCreateBufferObjectInMemoryRegionIsCalledThenBufferObjectWithAGivenGpuAddressAndSizeIsCreatedAndAllocatedInASpecifiedMemoryRegion, NonDefaultIoctlsSupported) {
|
||||
DebugManagerStateRestore restorer;
|
||||
DebugManager.flags.EnableLocalMemory.set(1);
|
||||
@@ -822,7 +780,7 @@ HWTEST2_F(DrmMemoryManagerTestImpl, givenDrmMemoryManagerWhenLockUnlockIsCalledO
|
||||
mockExp->ioctlImpl_expected.gemCreateExt = 1;
|
||||
mockExp->ioctl_expected.gemWait = 1;
|
||||
mockExp->ioctl_expected.gemClose = 1;
|
||||
mockExp->ioctlImpl_expected.gemMmapOffset = 1;
|
||||
mockExp->ioctl_expected.gemMmapOffset = 1;
|
||||
mockExp->memoryInfo.reset(new MockMemoryInfo());
|
||||
|
||||
AllocationData allocData;
|
||||
@@ -845,7 +803,7 @@ HWTEST2_F(DrmMemoryManagerTestImpl, givenDrmMemoryManagerWhenLockUnlockIsCalledO
|
||||
|
||||
EXPECT_EQ(static_cast<uint32_t>(drmAllocation->getBO()->peekHandle()), mockExp->mmapOffsetHandle);
|
||||
EXPECT_EQ(0u, mockExp->mmapOffsetPad);
|
||||
EXPECT_EQ(0u, mockExp->mmapOffsetOffset);
|
||||
EXPECT_EQ(0u, mockExp->mmapOffsetExpected);
|
||||
EXPECT_EQ(4u, mockExp->mmapOffsetFlags);
|
||||
|
||||
memoryManager->unlockResource(allocation);
|
||||
@@ -855,7 +813,7 @@ HWTEST2_F(DrmMemoryManagerTestImpl, givenDrmMemoryManagerWhenLockUnlockIsCalledO
|
||||
}
|
||||
|
||||
TEST_F(DrmMemoryManagerTestImpl, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAllocationInLocalMemoryButFailsOnMmapThenReturnNullPtr) {
|
||||
mockExp->ioctlImpl_expected.gemMmapOffset = 2;
|
||||
mockExp->ioctl_expected.gemMmapOffset = 2;
|
||||
this->ioctlResExt = {mockExp->ioctl_cnt.total, -1};
|
||||
mockExp->ioctl_res_ext = &ioctlResExt;
|
||||
|
||||
@@ -871,7 +829,7 @@ TEST_F(DrmMemoryManagerTestImpl, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAl
|
||||
}
|
||||
|
||||
TEST_F(DrmMemoryManagerTestImpl, givenDrmMemoryManagerWhenLockUnlockIsCalledOnAllocationInLocalMemoryButFailsOnIoctlMmapFunctionOffsetThenReturnNullPtr) {
|
||||
mockExp->ioctlImpl_expected.gemMmapOffset = 2;
|
||||
mockExp->ioctl_expected.gemMmapOffset = 2;
|
||||
mockExp->returnIoctlExtraErrorValue = true;
|
||||
mockExp->failOnMmapOffset = true;
|
||||
|
||||
|
||||
@@ -5543,4 +5543,72 @@ TEST(DrmMemoryManagerCopyMemoryToAllocationBanksTest, givenDrmMemoryManagerWhenC
|
||||
delete mockAllocation.bufferObjects[index];
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(DrmMemoryManagerWithLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectSucceedsThenReturnTrueAndCorrectOffset) {
|
||||
mock->ioctl_expected.gemMmapOffset = 1;
|
||||
BufferObject bo(mock, 1, 1024, 0);
|
||||
mock->mmapOffsetExpected = 21;
|
||||
|
||||
uint64_t offset = 0;
|
||||
auto ret = memoryManager->retrieveMmapOffsetForBufferObject(rootDeviceIndex, bo, 0, offset);
|
||||
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_EQ(21u, offset);
|
||||
}
|
||||
|
||||
TEST_F(DrmMemoryManagerWithLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectFailsThenReturnFalse) {
|
||||
mock->ioctl_expected.gemMmapOffset = 2;
|
||||
BufferObject bo(mock, 1, 1024, 0);
|
||||
mock->failOnMmapOffset = true;
|
||||
|
||||
uint64_t offset = 0;
|
||||
auto ret = memoryManager->retrieveMmapOffsetForBufferObject(rootDeviceIndex, bo, 0, offset);
|
||||
|
||||
EXPECT_FALSE(ret);
|
||||
}
|
||||
|
||||
TEST_F(DrmMemoryManagerWithLocalMemoryTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectIsCalledForLocalMemoryThenApplyCorrectFlags) {
|
||||
mock->ioctl_expected.gemMmapOffset = 5;
|
||||
BufferObject bo(mock, 1, 1024, 0);
|
||||
|
||||
uint64_t offset = 0;
|
||||
auto ret = memoryManager->retrieveMmapOffsetForBufferObject(rootDeviceIndex, bo, 0, offset);
|
||||
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_EQ(4u, mock->mmapOffsetFlags);
|
||||
|
||||
mock->failOnMmapOffset = true;
|
||||
|
||||
for (uint64_t flags : {I915_MMAP_OFFSET_WC, I915_MMAP_OFFSET_WB}) {
|
||||
ret = memoryManager->retrieveMmapOffsetForBufferObject(rootDeviceIndex, bo, flags, offset);
|
||||
|
||||
EXPECT_FALSE(ret);
|
||||
EXPECT_EQ(flags, mock->mmapOffsetFlags);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(DrmMemoryManagerTest, givenDrmWhenRetrieveMmapOffsetForBufferObjectIsCalledForSystemMemoryThenApplyCorrectFlags) {
|
||||
mock->ioctl_expected.gemMmapOffset = 4;
|
||||
BufferObject bo(mock, 1, 1024, 0);
|
||||
|
||||
uint64_t offset = 0;
|
||||
bool ret = false;
|
||||
|
||||
for (uint64_t flags : {I915_MMAP_OFFSET_WC, I915_MMAP_OFFSET_WB}) {
|
||||
ret = memoryManager->retrieveMmapOffsetForBufferObject(rootDeviceIndex, bo, flags, offset);
|
||||
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_EQ(flags, mock->mmapOffsetFlags);
|
||||
}
|
||||
|
||||
mock->failOnMmapOffset = true;
|
||||
|
||||
for (uint64_t flags : {I915_MMAP_OFFSET_WC, I915_MMAP_OFFSET_WB}) {
|
||||
ret = memoryManager->retrieveMmapOffsetForBufferObject(rootDeviceIndex, bo, flags, offset);
|
||||
|
||||
EXPECT_FALSE(ret);
|
||||
EXPECT_EQ(flags, mock->mmapOffsetFlags);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace NEO
|
||||
|
||||
@@ -1424,4 +1424,27 @@ bool DrmMemoryManager::createDrmAllocation(Drm *drm, DrmAllocation *allocation,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DrmMemoryManager::retrieveMmapOffsetForBufferObject(uint32_t rootDeviceIndex, BufferObject &bo, uint64_t flags, uint64_t &offset) {
|
||||
constexpr uint64_t mmapOffsetFixed = 4;
|
||||
|
||||
drm_i915_gem_mmap_offset mmapOffset = {};
|
||||
mmapOffset.handle = bo.peekHandle();
|
||||
mmapOffset.flags = isLocalMemorySupported(rootDeviceIndex) ? mmapOffsetFixed : flags;
|
||||
auto &drm = getDrm(rootDeviceIndex);
|
||||
auto ret = drm.ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET, &mmapOffset);
|
||||
if (ret != 0 && isLocalMemorySupported(rootDeviceIndex)) {
|
||||
mmapOffset.flags = flags;
|
||||
ret = drm.ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET, &mmapOffset);
|
||||
}
|
||||
if (ret != 0) {
|
||||
int err = drm.getErrno();
|
||||
PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET) failed with %d. errno=%d(%s)\n", ret, err, strerror(err));
|
||||
DEBUG_BREAK_IF(ret != 0);
|
||||
return false;
|
||||
}
|
||||
|
||||
offset = mmapOffset.offset;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace NEO
|
||||
|
||||
@@ -123,6 +123,7 @@ class DrmMemoryManager : public MemoryManager {
|
||||
uint32_t getRootDeviceIndex(const Drm *drm);
|
||||
BufferObject *createRootDeviceBufferObject(uint32_t rootDeviceIndex);
|
||||
void releaseBufferObject(uint32_t rootDeviceIndex);
|
||||
bool retrieveMmapOffsetForBufferObject(uint32_t rootDeviceIndex, BufferObject &bo, uint64_t flags, uint64_t &offset);
|
||||
|
||||
std::vector<BufferObject *> pinBBs;
|
||||
std::vector<void *> memoryForPinBBs;
|
||||
|
||||
@@ -10,27 +10,6 @@
|
||||
|
||||
namespace NEO {
|
||||
|
||||
bool retrieveMmapOffsetForBufferObject(Drm &drm, BufferObject &bo, uint64_t flags, uint64_t &offset) {
|
||||
drm_i915_gem_mmap_offset mmapOffset = {};
|
||||
mmapOffset.handle = bo.peekHandle();
|
||||
mmapOffset.flags = I915_MMAP_OFFSET_FIXED;
|
||||
|
||||
auto ret = drm.ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET, &mmapOffset);
|
||||
if (ret != 0) {
|
||||
mmapOffset.flags = flags;
|
||||
ret = drm.ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET, &mmapOffset);
|
||||
if (ret != 0) {
|
||||
int err = drm.getErrno();
|
||||
PRINT_DEBUG_STRING(DebugManager.flags.PrintDebugMessages.get(), stderr, "ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET) failed with %d. errno=%d(%s)\n", ret, err, strerror(err));
|
||||
DEBUG_BREAK_IF(ret != 0);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
offset = mmapOffset.offset;
|
||||
return true;
|
||||
}
|
||||
|
||||
DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool hasMappedPtr) {
|
||||
drm_prime_handle openFd = {0, 0, 0};
|
||||
openFd.fd = handle;
|
||||
@@ -74,7 +53,7 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
|
||||
}
|
||||
|
||||
uint64_t offset = 0;
|
||||
if (!retrieveMmapOffsetForBufferObject(this->getDrm(allocationData.rootDeviceIndex), *bo, I915_MMAP_OFFSET_WB, offset)) {
|
||||
if (!retrieveMmapOffsetForBufferObject(allocationData.rootDeviceIndex, *bo, I915_MMAP_OFFSET_WB, offset)) {
|
||||
this->munmapFunction(cpuPointer, size);
|
||||
return nullptr;
|
||||
}
|
||||
@@ -116,7 +95,7 @@ void *DrmMemoryManager::lockResourceInLocalMemoryImpl(BufferObject *bo) {
|
||||
auto rootDeviceIndex = this->getRootDeviceIndex(bo->peekDrm());
|
||||
|
||||
uint64_t offset = 0;
|
||||
if (!retrieveMmapOffsetForBufferObject(this->getDrm(rootDeviceIndex), *bo, I915_MMAP_OFFSET_WC, offset)) {
|
||||
if (!retrieveMmapOffsetForBufferObject(rootDeviceIndex, *bo, I915_MMAP_OFFSET_WC, offset)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,7 @@ class TestedDrmMemoryManager : public MemoryManagerCreate<DrmMemoryManager> {
|
||||
using DrmMemoryManager::pushSharedBufferObject;
|
||||
using DrmMemoryManager::registerAllocationInOs;
|
||||
using DrmMemoryManager::releaseGpuRange;
|
||||
using DrmMemoryManager::retrieveMmapOffsetForBufferObject;
|
||||
using DrmMemoryManager::setDomainCpu;
|
||||
using DrmMemoryManager::sharingBufferObjects;
|
||||
using DrmMemoryManager::supportsMultiStorageResources;
|
||||
|
||||
@@ -20,6 +20,7 @@ void DrmMockCustom::Ioctls::reset() {
|
||||
primeFdToHandle = 0;
|
||||
handleToPrimeFd = 0;
|
||||
gemMmap = 0;
|
||||
gemMmapOffset = 0;
|
||||
gemSetDomain = 0;
|
||||
gemWait = 0;
|
||||
gemClose = 0;
|
||||
@@ -46,6 +47,7 @@ void DrmMockCustom::testIoctls() {
|
||||
NEO_IOCTL_EXPECT_EQ(primeFdToHandle);
|
||||
NEO_IOCTL_EXPECT_EQ(handleToPrimeFd);
|
||||
NEO_IOCTL_EXPECT_EQ(gemMmap);
|
||||
NEO_IOCTL_EXPECT_EQ(gemMmapOffset);
|
||||
NEO_IOCTL_EXPECT_EQ(gemSetDomain);
|
||||
NEO_IOCTL_EXPECT_EQ(gemWait);
|
||||
NEO_IOCTL_EXPECT_EQ(gemClose);
|
||||
@@ -167,7 +169,16 @@ int DrmMockCustom::ioctl(unsigned long request, void *arg) {
|
||||
case DRM_IOCTL_I915_GEM_CONTEXT_DESTROY: {
|
||||
ioctl_cnt.contextDestroy++;
|
||||
} break;
|
||||
|
||||
case DRM_IOCTL_I915_GEM_MMAP_OFFSET: {
|
||||
auto mmapOffsetParams = reinterpret_cast<drm_i915_gem_mmap_offset *>(arg);
|
||||
mmapOffsetParams->handle = mmapOffsetHandle;
|
||||
mmapOffsetParams->offset = mmapOffsetExpected;
|
||||
mmapOffsetFlags = mmapOffsetParams->flags;
|
||||
ioctl_cnt.gemMmapOffset++;
|
||||
if (failOnMmapOffset == true) {
|
||||
return -1;
|
||||
}
|
||||
} break;
|
||||
default:
|
||||
int res = ioctlExtra(request, arg);
|
||||
if (returnIoctlExtraErrorValue) {
|
||||
|
||||
@@ -90,6 +90,7 @@ class DrmMockCustom : public Drm {
|
||||
std::atomic<int32_t> primeFdToHandle;
|
||||
std::atomic<int32_t> handleToPrimeFd;
|
||||
std::atomic<int32_t> gemMmap;
|
||||
std::atomic<int32_t> gemMmapOffset;
|
||||
std::atomic<int32_t> gemSetDomain;
|
||||
std::atomic<int32_t> gemWait;
|
||||
std::atomic<int32_t> gemClose;
|
||||
@@ -197,6 +198,12 @@ class DrmMockCustom : public Drm {
|
||||
__u64 getContextParamRetValue = 0;
|
||||
//DRM_IOCTL_I915_GEM_WAIT
|
||||
int64_t gemWaitTimeout = 0;
|
||||
//DRM_IOCTL_I915_GEM_MMAP_OFFSET
|
||||
__u32 mmapOffsetHandle = 0;
|
||||
__u32 mmapOffsetPad = 0;
|
||||
__u64 mmapOffsetExpected = 0;
|
||||
__u64 mmapOffsetFlags = 0;
|
||||
bool failOnMmapOffset = false;
|
||||
|
||||
int errnoValue = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user