Align mmaped bo address properly

Change-Id: I010f6619821ad715bb6f0e9640be19943a45abd8
Signed-off-by: Lukasz Jobczyk <lukasz.jobczyk@intel.com>
This commit is contained in:
Lukasz Jobczyk
2020-10-12 15:11:40 +02:00
committed by sys_ocldev
parent eb8f5fa301
commit 8892ee3f1f
6 changed files with 25 additions and 12 deletions

View File

@@ -65,6 +65,8 @@ class DrmAllocation : public GraphicsAllocation {
void *getMmapPtr() { return this->mmapPtr; }
void setMmapPtr(void *ptr) { this->mmapPtr = ptr; }
size_t getMmapSize() { return this->mmapSize; }
void setMmapSize(size_t size) { this->mmapSize = size; }
void makeBOsResident(OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind);
void bindBO(BufferObject *bo, OsContext *osContext, uint32_t vmHandleId, std::vector<BufferObject *> *bufferObjects, bool bind);
@@ -77,5 +79,6 @@ class DrmAllocation : public GraphicsAllocation {
StackVec<uint32_t, 1> registeredBoBindHandles;
void *mmapPtr = nullptr;
size_t mmapSize = 0u;
};
} // namespace NEO

View File

@@ -669,7 +669,7 @@ void DrmMemoryManager::freeGraphicsMemoryImpl(GraphicsAllocation *gfxAllocation)
this->unregisterAllocation(gfxAllocation);
if (drmAlloc->getMmapPtr()) {
this->munmapFunction(drmAlloc->getMmapPtr(), gfxAllocation->getUnderlyingBufferSize());
this->munmapFunction(drmAlloc->getMmapPtr(), drmAlloc->getMmapSize());
}
for (auto &engine : this->registeredEngines) {

View File

@@ -73,9 +73,16 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
}
if (useBooMmap) {
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(this->createBufferObjectInMemoryRegion(&this->getDrm(allocationData.rootDeviceIndex), 0u, alignedSVMSize, 0u, maxOsContextCount));
auto totalSizeToAlloc = alignedSVMSize + alignment;
auto cpuPointer = this->mmapFunction(0, totalSizeToAlloc, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
auto cpuBasePointer = cpuPointer;
cpuPointer = alignUp(cpuPointer, alignment);
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(this->createBufferObjectInMemoryRegion(&this->getDrm(allocationData.rootDeviceIndex), reinterpret_cast<uintptr_t>(cpuPointer), alignedSVMSize, 0u, maxOsContextCount));
if (!bo) {
this->munmapFunction(cpuBasePointer, totalSizeToAlloc);
return nullptr;
}
@@ -85,23 +92,18 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
auto ret = this->getDrm(allocationData.rootDeviceIndex).ioctl(DRM_IOCTL_I915_GEM_MMAP_OFFSET, &gemMmap);
if (ret != 0) {
this->munmapFunction(cpuBasePointer, totalSizeToAlloc);
return nullptr;
}
void *cpuPointer = reinterpret_cast<void *>(this->mmapFunction(0, alignedSVMSize, PROT_READ | PROT_WRITE, MAP_SHARED, getDrm(allocationData.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(gemMmap.offset)));
auto cpuBasePointer = cpuPointer;
cpuPointer = alignUp(cpuPointer, alignment);
auto offset = ptrDiff(cpuPointer, cpuBasePointer);
bo->gpuAddress = reinterpret_cast<uintptr_t>(cpuPointer);
this->mmapFunction(cpuPointer, alignedSVMSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, getDrm(allocationData.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(gemMmap.offset));
obtainGpuAddress(allocationData, bo.get(), gpuAddress);
emitPinningRequest(bo.get(), allocationData);
auto allocation = new DrmAllocation(allocationData.rootDeviceIndex, allocationData.type, bo.get(), cpuPointer, bo->gpuAddress, alignedSVMSize, MemoryPool::System4KBPages);
allocation->setMmapPtr(cpuBasePointer);
allocation->setAllocationOffset(offset);
allocation->setMmapSize(totalSizeToAlloc);
allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuAddress), alignedSVMSize);
bo.release();

View File

@@ -17,6 +17,9 @@ extern off_t lseekReturn;
extern std::atomic<int> lseekCalledCount;
inline void *mmapMock(void *addr, size_t length, int prot, int flags, int fd, off_t offset) noexcept {
if (addr) {
return addr;
}
void *ptr = nullptr;
if (length > 0) {
ptr = alignedMalloc(length, MemoryConstants::pageSize64k);