2018-08-01 20:47:14 +08:00
|
|
|
/*
|
2019-01-22 19:40:17 +08:00
|
|
|
* Copyright (C) 2018-2019 Intel Corporation
|
2018-09-18 15:11:08 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
2018-08-01 20:47:14 +08:00
|
|
|
|
|
|
|
#include "runtime/os_interface/linux/drm_memory_manager.h"
|
2018-12-21 17:16:27 +08:00
|
|
|
#include "unit_tests/mocks/mock_allocation_properties.h"
|
2018-10-24 14:46:54 +08:00
|
|
|
#include "unit_tests/mocks/mock_host_ptr_manager.h"
|
2018-11-15 22:11:14 +08:00
|
|
|
#include <atomic>
|
2018-08-01 20:47:14 +08:00
|
|
|
|
|
|
|
namespace OCLRT {
|
|
|
|
static off_t lseekReturn = 4096u;
|
2018-11-15 22:11:14 +08:00
|
|
|
static std::atomic<int> lseekCalledCount(0);
|
|
|
|
static std::atomic<int> mmapMockCallCount(0);
|
|
|
|
static std::atomic<int> munmapMockCallCount(0);
|
2018-08-01 20:47:14 +08:00
|
|
|
|
|
|
|
off_t lseekMock(int fd, off_t offset, int whence) noexcept {
|
|
|
|
lseekCalledCount++;
|
|
|
|
return lseekReturn;
|
|
|
|
}
|
|
|
|
void *mmapMock(void *addr, size_t length, int prot, int flags,
|
|
|
|
int fd, long offset) noexcept {
|
|
|
|
mmapMockCallCount++;
|
|
|
|
return reinterpret_cast<void *>(0x1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
int munmapMock(void *addr, size_t length) noexcept {
|
|
|
|
munmapMockCallCount++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int closeMock(int) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestedDrmMemoryManager : public DrmMemoryManager {
|
|
|
|
public:
|
2018-12-12 01:56:37 +08:00
|
|
|
using DrmMemoryManager::allocateGraphicsMemory;
|
2018-12-06 22:03:06 +08:00
|
|
|
using DrmMemoryManager::allocateGraphicsMemory64kb;
|
2019-01-22 19:40:17 +08:00
|
|
|
using DrmMemoryManager::allocateGraphicsMemoryForImage;
|
2018-11-30 18:01:33 +08:00
|
|
|
using DrmMemoryManager::allocateGraphicsMemoryWithHostPtr;
|
2018-12-06 22:03:06 +08:00
|
|
|
using DrmMemoryManager::AllocationData;
|
2018-08-01 20:47:14 +08:00
|
|
|
using DrmMemoryManager::allocUserptr;
|
2019-02-08 03:16:25 +08:00
|
|
|
using DrmMemoryManager::internal32bitAllocator;
|
|
|
|
using DrmMemoryManager::limitedGpuAddressRangeAllocator;
|
2018-12-21 18:52:46 +08:00
|
|
|
using DrmMemoryManager::pinThreshold;
|
2018-08-01 20:47:14 +08:00
|
|
|
using DrmMemoryManager::setDomainCpu;
|
|
|
|
using DrmMemoryManager::sharingBufferObjects;
|
2019-01-22 19:40:17 +08:00
|
|
|
using MemoryManager::allocateGraphicsMemoryInDevicePool;
|
2018-08-01 20:47:14 +08:00
|
|
|
|
2018-10-01 22:10:54 +08:00
|
|
|
TestedDrmMemoryManager(Drm *drm, ExecutionEnvironment &executionEnvironment) : DrmMemoryManager(drm, gemCloseWorkerMode::gemCloseWorkerInactive, false, false, executionEnvironment) {
|
2018-08-01 20:47:14 +08:00
|
|
|
this->lseekFunction = &lseekMock;
|
|
|
|
this->mmapFunction = &mmapMock;
|
|
|
|
this->munmapFunction = &munmapMock;
|
|
|
|
this->closeFunction = &closeMock;
|
|
|
|
lseekReturn = 4096;
|
|
|
|
lseekCalledCount = 0;
|
|
|
|
mmapMockCallCount = 0;
|
|
|
|
munmapMockCallCount = 0;
|
2018-10-24 14:46:54 +08:00
|
|
|
hostPtrManager.reset(new MockHostPtrManager);
|
2018-08-01 20:47:14 +08:00
|
|
|
};
|
2018-10-01 22:10:54 +08:00
|
|
|
TestedDrmMemoryManager(Drm *drm, bool allowForcePin, bool validateHostPtrMemory, ExecutionEnvironment &executionEnvironment) : DrmMemoryManager(drm, gemCloseWorkerMode::gemCloseWorkerInactive, allowForcePin, validateHostPtrMemory, executionEnvironment) {
|
2018-08-01 20:47:14 +08:00
|
|
|
this->lseekFunction = &lseekMock;
|
|
|
|
this->mmapFunction = &mmapMock;
|
|
|
|
this->munmapFunction = &munmapMock;
|
|
|
|
this->closeFunction = &closeMock;
|
|
|
|
lseekReturn = 4096;
|
|
|
|
lseekCalledCount = 0;
|
|
|
|
mmapMockCallCount = 0;
|
|
|
|
munmapMockCallCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unreference(BufferObject *bo) {
|
|
|
|
DrmMemoryManager::unreference(bo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void injectPinBB(BufferObject *newPinBB) {
|
|
|
|
BufferObject *currentPinBB = pinBB;
|
|
|
|
pinBB = nullptr;
|
|
|
|
DrmMemoryManager::unreference(currentPinBB);
|
|
|
|
pinBB = newPinBB;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrmGemCloseWorker *getgemCloseWorker() { return this->gemCloseWorker.get(); }
|
2018-11-16 02:43:12 +08:00
|
|
|
void forceLimitedRangeAllocator(uint64_t range) { initInternalRangeAllocator(range); }
|
2018-08-01 20:47:14 +08:00
|
|
|
|
2018-11-16 02:43:12 +08:00
|
|
|
Allocator32bit *getDrmInternal32BitAllocator() const { return internal32bitAllocator.get(); }
|
|
|
|
AllocatorLimitedRange *getDrmLimitedRangeAllocator() const { return limitedGpuAddressRangeAllocator.get(); }
|
2018-12-21 17:16:27 +08:00
|
|
|
DrmAllocation *allocate32BitGraphicsMemory(size_t size, const void *ptr, AllocationOrigin allocationOrigin) {
|
|
|
|
bool allocateMemory = ptr == nullptr;
|
|
|
|
AllocationData allocationData;
|
|
|
|
if (allocationOrigin == AllocationOrigin::EXTERNAL_ALLOCATION) {
|
2019-02-15 18:31:47 +08:00
|
|
|
getAllocationData(allocationData, MockAllocationProperties::getPropertiesFor32BitExternalAllocation(size, allocateMemory), {}, ptr);
|
2018-12-21 17:16:27 +08:00
|
|
|
} else {
|
2019-02-15 18:31:47 +08:00
|
|
|
getAllocationData(allocationData, MockAllocationProperties::getPropertiesFor32BitInternalAllocation(size, allocateMemory), {}, ptr);
|
2018-12-21 17:16:27 +08:00
|
|
|
}
|
|
|
|
return allocate32BitGraphicsMemoryImpl(allocationData);
|
|
|
|
}
|
2018-08-01 20:47:14 +08:00
|
|
|
};
|
|
|
|
} // namespace OCLRT
|