Skip to content

Commit

Permalink
Clean drm interfaces.
Browse files Browse the repository at this point in the history
- all driver allocations are using SoftPin
- remove unneeded methods.
- remove unneeded members.
- remove unneeded code paths.

Change-Id: I3369c0a4d37727210b5a26271d25537ca5218bd4
  • Loading branch information
MichalMrozek authored and Compute-Runtime-Automation committed Feb 20, 2019
1 parent c9a8f9b commit 45a0cee
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 58 deletions.
23 changes: 5 additions & 18 deletions runtime/os_interface/linux/drm_buffer_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,17 @@
namespace OCLRT {

BufferObject::BufferObject(Drm *drm, int handle, bool isAllocated) : drm(drm), refCount(1), handle(handle), isReused(false), isAllocated(isAllocated) {
this->isSoftpin = false;

this->tiling_mode = I915_TILING_NONE;
this->stride = 0;

execObjectsStorage = nullptr;

this->size = 0;
this->address = nullptr;
this->lockedAddress = nullptr;
this->offset64 = 0;
}

uint32_t BufferObject::getRefCount() const {
return this->refCount.load();
}

bool BufferObject::softPin(uint64_t offset) {
this->isSoftpin = true;
this->offset64 = offset;

return true;
};

bool BufferObject::close() {
drm_gem_close close = {};
close.handle = this->handle;
Expand Down Expand Up @@ -111,11 +98,11 @@ void BufferObject::fillExecObject(drm_i915_gem_exec_object2 &execObject, uint32_
execObject.relocation_count = 0; //No relocations, we are SoftPinning
execObject.relocs_ptr = 0ul;
execObject.alignment = 0;
execObject.offset = this->isSoftpin ? this->offset64 : 0;
execObject.flags = this->isSoftpin ? EXEC_OBJECT_PINNED : 0;
execObject.offset = this->gpuAddress;
execObject.flags = EXEC_OBJECT_PINNED;
#ifdef __x86_64__
// set EXEC_OBJECT_SUPPORTS_48B_ADDRESS flag if whole object resides in 32BIT address space boundary
execObject.flags |= (reinterpret_cast<uint64_t>(this->address) + this->size) & MemoryConstants::zoneHigh ? EXEC_OBJECT_SUPPORTS_48B_ADDRESS : 0;
execObject.flags |= (this->gpuAddress + this->size) & MemoryConstants::zoneHigh ? EXEC_OBJECT_SUPPORTS_48B_ADDRESS : 0;
#endif
execObject.rsvd1 = drmContextId;
execObject.rsvd2 = 0;
Expand Down Expand Up @@ -157,8 +144,8 @@ int BufferObject::pin(BufferObject *boToPin[], size_t numberOfBos, uint32_t drmC
drm_i915_gem_execbuffer2 execbuf = {};
StackVec<drm_i915_gem_exec_object2, maxFragmentsCount + 1> execObject;

reinterpret_cast<uint32_t *>(this->address)[0] = 0x05000000;
reinterpret_cast<uint32_t *>(this->address)[1] = 0x00000000;
reinterpret_cast<uint32_t *>(this->gpuAddress)[0] = 0x05000000;
reinterpret_cast<uint32_t *>(this->gpuAddress)[1] = 0x00000000;

execObject.resize(numberOfBos + 1);

Expand Down
15 changes: 6 additions & 9 deletions runtime/os_interface/linux/drm_buffer_object.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2018 Intel Corporation
* Copyright (C) 2017-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand Down Expand Up @@ -40,8 +40,6 @@ class BufferObject {
public:
MOCKABLE_VIRTUAL ~BufferObject(){};

bool softPin(uint64_t offset);

bool setTiling(uint32_t mode, uint32_t stride);

MOCKABLE_VIRTUAL int pin(BufferObject *boToPin[], size_t numberOfBos, uint32_t drmContextId);
Expand All @@ -59,8 +57,8 @@ class BufferObject {
bool peekIsAllocated() const { return isAllocated; }
size_t peekSize() const { return size; }
int peekHandle() const { return handle; }
void *peekAddress() const { return address; }
void setAddress(void *address) { this->address = address; }
uint64_t peekAddress() const { return gpuAddress; }
void setAddress(uint64_t address) { this->gpuAddress = address; }
void *peekLockedAddress() const { return lockedAddress; }
void setLockedAddress(void *cpuAddress) { this->lockedAddress = cpuAddress; }
void setUnmapSize(uint64_t unmapSize) { this->unmapSize = unmapSize; }
Expand All @@ -87,7 +85,6 @@ class BufferObject {
drm_i915_gem_exec_object2 *execObjectsStorage;

int handle; // i915 gem object handle
bool isSoftpin;
bool isReused;

//Tiling
Expand All @@ -97,9 +94,9 @@ class BufferObject {
MOCKABLE_VIRTUAL void fillExecObject(drm_i915_gem_exec_object2 &execObject, uint32_t drmContextId);
void processRelocs(int &idx, uint32_t drmContextId);

uint64_t offset64; // last-seen GPU offset
size_t size;
void *address; // GPU side virtual address
uint64_t gpuAddress = 0llu;

uint64_t size;
void *lockedAddress; // CPU side virtual address

bool isAllocated = false;
Expand Down
26 changes: 9 additions & 17 deletions runtime/os_interface/linux/drm_memory_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ uint32_t DrmMemoryManager::unreference(OCLRT::BufferObject *bo, bool synchronous

if (r == 1) {
auto unmapSize = bo->peekUnmapSize();
auto address = bo->isAllocated || unmapSize > 0 ? bo->address : nullptr;
auto address = bo->isAllocated || unmapSize > 0 ? reinterpret_cast<void *>(bo->gpuAddress) : nullptr;
auto allocatorType = bo->peekAllocationType();

if (bo->isReused) {
Expand Down Expand Up @@ -206,8 +206,7 @@ OCLRT::BufferObject *DrmMemoryManager::allocUserptr(uintptr_t address, size_t si
return nullptr;
}
res->size = size;
res->address = reinterpret_cast<void *>(address);
res->softPin(address);
res->gpuAddress = address;

return res;
}
Expand Down Expand Up @@ -279,8 +278,7 @@ DrmAllocation *DrmMemoryManager::allocateGraphicsMemoryForNonSvmHostPtr(size_t s

bo->isAllocated = false;
bo->setUnmapSize(alignedSize);
bo->address = reinterpret_cast<void *>(gpuVirtualAddress);
bo->softPin((uint64_t)bo->address);
bo->gpuAddress = gpuVirtualAddress;
bo->setAllocationType(allocType);

auto allocation = new DrmAllocation(bo, alignedPtr, gpuVirtualAddress, size, MemoryPool::System4KBPages, false);
Expand Down Expand Up @@ -319,8 +317,7 @@ GraphicsAllocation *DrmMemoryManager::allocateGraphicsMemoryForImageImpl(const A
return nullptr;
}
bo->size = allocationData.imgInfo->size;
bo->address = reinterpret_cast<void *>(gpuRange);
bo->softPin(gpuRange);
bo->gpuAddress = gpuRange;

auto ret2 = bo->setTiling(I915_TILING_Y, static_cast<uint32_t>(allocationData.imgInfo->rowPitch));
DEBUG_BREAK_IF(ret2 != true);
Expand Down Expand Up @@ -358,9 +355,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio

bo->isAllocated = false;
bo->setUnmapSize(realAllocationSize);
bo->address = reinterpret_cast<void *>(gpuVirtualAddress);
uintptr_t offset = (uintptr_t)bo->address;
bo->softPin((uint64_t)offset);
bo->gpuAddress = gpuVirtualAddress;
bo->setAllocationType(allocatorType);
auto drmAllocation = new DrmAllocation(bo, const_cast<void *>(allocationData.hostPtr), static_cast<uint64_t>(ptrOffset(gpuVirtualAddress, inputPointerOffset)),
allocationSize, MemoryPool::System4KBPagesWith32BitGpuAddressing, false);
Expand Down Expand Up @@ -409,8 +404,7 @@ DrmAllocation *DrmMemoryManager::allocate32BitGraphicsMemoryImpl(const Allocatio
DrmAllocation *drmAllocation = nullptr;
if (limitedRangeAllocation) {
// softpin to the GPU address, res if it uses limitedRangeAllocation
bo->address = reinterpret_cast<void *>(res);
bo->softPin(res);
bo->gpuAddress = res;
drmAllocation = new DrmAllocation(bo, ptrAlloc, res, alignedAllocationSize,
MemoryPool::System4KBPagesWith32BitGpuAddressing, false);
} else {
Expand Down Expand Up @@ -451,8 +445,7 @@ BufferObject *DrmMemoryManager::createSharedBufferObject(int boHandle, size_t si
}

bo->size = size;
bo->address = reinterpret_cast<void *>(gpuRange);
bo->softPin(gpuRange);
bo->gpuAddress = gpuRange;
bo->setUnmapSize(size);
bo->setAllocationType(storageType);
return bo;
Expand Down Expand Up @@ -490,7 +483,7 @@ GraphicsAllocation *DrmMemoryManager::createGraphicsAllocationFromSharedHandle(o

lock.unlock();

auto drmAllocation = new DrmAllocation(bo, bo->address, bo->size, handle, MemoryPool::SystemCpuInaccessible, false);
auto drmAllocation = new DrmAllocation(bo, reinterpret_cast<void *>(bo->gpuAddress), bo->size, handle, MemoryPool::SystemCpuInaccessible, false);

if (requireSpecificBitness && this->force32bitAllocations) {
drmAllocation->is32BitAllocation = true;
Expand All @@ -517,8 +510,7 @@ GraphicsAllocation *DrmMemoryManager::createPaddedAllocation(GraphicsAllocation
if (!bo) {
return nullptr;
}
bo->setAddress(reinterpret_cast<void *>(gpuRange));
bo->softPin(gpuRange);
bo->gpuAddress = gpuRange;
bo->setUnmapSize(sizeWithPadding);
bo->setAllocationType(storageType);
return new DrmAllocation(bo, (void *)srcPtr, (uint64_t)ptrOffset(gpuRange, offset), sizeWithPadding,
Expand Down
12 changes: 6 additions & 6 deletions unit_tests/os_interface/linux/drm_buffer_object_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ TEST_F(DrmBufferObjectTest, givenAddressThatWhenSizeIsAddedCrosses32BitBoundaryW
drm_i915_gem_exec_object2 execObject;

memset(&execObject, 0, sizeof(execObject));
bo->setAddress((void *)(((uint64_t)1u << 32) - 0x1000u));
bo->setAddress(((uint64_t)1u << 32) - 0x1000u);
bo->setSize(0x1000);
bo->fillExecObject(execObject, 1);
//base address + size > size of 32bit address space
Expand All @@ -113,7 +113,7 @@ TEST_F(DrmBufferObjectTest, givenAddressThatWhenSizeIsAddedWithin32BitBoundaryWh
drm_i915_gem_exec_object2 execObject;

memset(&execObject, 0, sizeof(execObject));
bo->setAddress((void *)(((uint64_t)1u << 32) - 0x1000u));
bo->setAddress(((uint64_t)1u << 32) - 0x1000u);
bo->setSize(0xFFF);
bo->fillExecObject(execObject, 1);
//base address + size < size of 32bit address space
Expand All @@ -130,7 +130,7 @@ TEST_F(DrmBufferObjectTest, onPinIoctlFailed) {
std::unique_ptr<BufferObject> boToPin(new TestedBufferObject(this->mock));
ASSERT_NE(nullptr, boToPin.get());

bo->setAddress(buff.get());
bo->setAddress(reinterpret_cast<uint64_t>(buff.get()));
BufferObject *boArray[1] = {boToPin.get()};
auto ret = bo->pin(boArray, 1, 1);
EXPECT_EQ(EINVAL, ret);
Expand All @@ -151,7 +151,7 @@ TEST(DrmBufferObjectSimpleTest, givenInvalidBoWhenPinIsCalledThenErrorIsReturned
std::unique_ptr<BufferObject> boToPin(new TestedBufferObject(mock.get()));
ASSERT_NE(nullptr, boToPin.get());

bo->setAddress(buff.get());
bo->setAddress(reinterpret_cast<uint64_t>(buff.get()));
mock->errnoValue = EFAULT;

BufferObject *boArray[1] = {boToPin.get()};
Expand Down Expand Up @@ -179,7 +179,7 @@ TEST(DrmBufferObjectSimpleTest, givenArrayOfBosWhenPinnedThenAllBosArePinned) {

BufferObject *array[3] = {boToPin.get(), boToPin2.get(), boToPin3.get()};

bo->setAddress(buff.get());
bo->setAddress(reinterpret_cast<uint64_t>(buff.get()));
auto ret = bo->pin(array, 3, 1);
EXPECT_EQ(mock->ioctl_res, ret);
uint32_t bb_end = 0x05000000;
Expand All @@ -191,5 +191,5 @@ TEST(DrmBufferObjectSimpleTest, givenArrayOfBosWhenPinnedThenAllBosArePinned) {
EXPECT_NE(nullptr, boToPin2->execObjectPointerFilled);
EXPECT_NE(nullptr, boToPin3->execObjectPointerFilled);

bo->setAddress(nullptr);
bo->setAddress(0llu);
}
1 change: 0 additions & 1 deletion unit_tests/os_interface/linux/drm_command_stream_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,6 @@ class DrmCommandStreamEnhancedFixture

protected:
MockBufferObject(Drm *drm, size_t size) : BufferObject(drm, 1, false) {
this->isSoftpin = true;
this->size = alignUp(size, 4096);
}
};
Expand Down
14 changes: 7 additions & 7 deletions unit_tests/os_interface/linux/drm_memory_manager_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ TEST_F(DrmMemoryManagerTest, Allocate_HostPtr) {
auto bo = alloc->getBO();
ASSERT_NE(nullptr, bo);
EXPECT_FALSE(bo->peekIsAllocated());
EXPECT_EQ(ptr, bo->peekAddress());
EXPECT_EQ(ptr, reinterpret_cast<void *>(bo->peekAddress()));
EXPECT_EQ(Sharing::nonSharedResource, alloc->peekSharedHandle());
memoryManager->freeGraphicsMemory(alloc);
::alignedFree(ptr);
Expand All @@ -490,7 +490,7 @@ TEST_F(DrmMemoryManagerTest, Allocate_HostPtr_Nullptr) {
auto bo = alloc->getBO();
ASSERT_NE(nullptr, bo);
EXPECT_FALSE(bo->peekIsAllocated());
EXPECT_EQ(ptr, bo->peekAddress());
EXPECT_EQ(ptr, reinterpret_cast<void *>(bo->peekAddress()));

memoryManager->freeGraphicsMemory(alloc);
::alignedFree(ptr);
Expand All @@ -514,7 +514,7 @@ TEST_F(DrmMemoryManagerTest, Allocate_HostPtr_MisAligned) {
auto bo = alloc->getBO();
ASSERT_NE(nullptr, bo);
EXPECT_FALSE(bo->peekIsAllocated());
EXPECT_EQ(ptrT, bo->peekAddress());
EXPECT_EQ(ptrT, reinterpret_cast<void *>(bo->peekAddress()));

memoryManager->freeGraphicsMemory(alloc);
::alignedFree(ptrT);
Expand Down Expand Up @@ -747,7 +747,7 @@ TEST_F(DrmMemoryManagerTest, GivenMisalignedHostPtrAndMultiplePagesSizeWhenAsked
for (int i = 0; i < maxFragmentsCount; i++) {
ASSERT_NE(nullptr, graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo);
EXPECT_EQ(reqs.AllocationFragments[i].allocationSize, graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo->peekSize());
EXPECT_EQ(reqs.AllocationFragments[i].allocationPtr, graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo->peekAddress());
EXPECT_EQ(reqs.AllocationFragments[i].allocationPtr, reinterpret_cast<void *>(graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo->peekAddress()));
EXPECT_FALSE(graphicsAllocation->fragmentsStorage.fragmentStorageData[i].osHandleStorage->bo->peekIsAllocated());
}
memoryManager->freeGraphicsMemory(graphicsAllocation);
Expand Down Expand Up @@ -1561,7 +1561,7 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerAndOsHandleWhenCreateIsCalledT
auto bo = drmAllocation->getBO();
EXPECT_EQ(bo->peekHandle(), (int)this->mock->outputHandle);
EXPECT_EQ(bo->peekUnmapSize(), size);
EXPECT_NE(nullptr, bo->peekAddress());
EXPECT_NE(0llu, bo->peekAddress());
EXPECT_TRUE(bo->peekIsAllocated());
EXPECT_EQ(1u, bo->getRefCount());
EXPECT_EQ(size, bo->peekSize());
Expand Down Expand Up @@ -1625,7 +1625,7 @@ TEST_F(DrmMemoryManagerTest, givenDrmMemoryManagerAndThreeOsHandlesWhenReuseCrea
bo = drmAllocation->getBO();
EXPECT_EQ(bo->peekHandle(), (int)this->mock->outputHandle);
EXPECT_EQ(bo->peekUnmapSize(), size);
EXPECT_NE(nullptr, bo->peekAddress());
EXPECT_NE(0llu, bo->peekAddress());
EXPECT_TRUE(bo->peekIsAllocated());
EXPECT_EQ(expectedRefCount, bo->getRefCount());
EXPECT_EQ(size, bo->peekSize());
Expand Down Expand Up @@ -2041,7 +2041,7 @@ TEST_F(DrmMemoryManagerTest, givenSharedAllocationWithSmallerThenRealSizeWhenCre
auto bo = drmAllocation->getBO();
EXPECT_EQ(bo->peekHandle(), (int)this->mock->outputHandle);
EXPECT_EQ(bo->peekUnmapSize(), realSize);
EXPECT_NE(nullptr, bo->peekAddress());
EXPECT_NE(0llu, bo->peekAddress());
EXPECT_TRUE(bo->peekIsAllocated());
EXPECT_EQ(1u, bo->getRefCount());
EXPECT_EQ(realSize, bo->peekSize());
Expand Down

0 comments on commit 45a0cee

Please sign in to comment.