Skip to content

Commit 2e9925c

Browse files
Extend createMultiHostAllocation, allocWithAlignment
Signed-off-by: Daniel Chabrowski [email protected] Related-To: NEO-6591
1 parent e0c892e commit 2e9925c

File tree

5 files changed

+136
-75
lines changed

5 files changed

+136
-75
lines changed

shared/source/os_interface/linux/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ set(NEO_CORE_OS_INTERFACE_LINUX
3939
${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_operations_handler_bind.h
4040
${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_operations_handler_default.cpp
4141
${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_operations_handler_default.h
42-
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}drm_memory_manager_create_multi_host_allocation.cpp
42+
${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_manager_create_multi_host_allocation.cpp
4343
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}drm_memory_manager_local_memory.cpp
44-
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}drm_memory_operations_handler_create.cpp
44+
${CMAKE_CURRENT_SOURCE_DIR}/drm_memory_operations_handler_create.cpp
4545
${CMAKE_CURRENT_SOURCE_DIR}${BRANCH_DIR_SUFFIX}drm_query.cpp
4646
${CMAKE_CURRENT_SOURCE_DIR}/hw_info_config_drm.cpp
4747
${CMAKE_CURRENT_SOURCE_DIR}/hw_device_id.h

shared/source/os_interface/linux/drm_memory_manager.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,4 +1538,82 @@ void DrmMemoryManager::waitOnCompletionFence(GraphicsAllocation *allocation) {
15381538
}
15391539
}
15401540

1541+
DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &allocationData, size_t size, size_t alignment, size_t alignedSize, uint64_t gpuAddress) {
1542+
bool useBooMmap = this->getDrm(allocationData.rootDeviceIndex).getMemoryInfo() && allocationData.useMmapObject;
1543+
1544+
if (DebugManager.flags.EnableBOMmapCreate.get() != -1) {
1545+
useBooMmap = DebugManager.flags.EnableBOMmapCreate.get();
1546+
}
1547+
1548+
if (useBooMmap) {
1549+
auto totalSizeToAlloc = alignedSize + alignment;
1550+
auto cpuPointer = this->mmapFunction(0, totalSizeToAlloc, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1551+
1552+
auto cpuBasePointer = cpuPointer;
1553+
cpuPointer = alignUp(cpuPointer, alignment);
1554+
1555+
auto pointerDiff = ptrDiff(cpuPointer, cpuBasePointer);
1556+
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(this->createBufferObjectInMemoryRegion(&this->getDrm(allocationData.rootDeviceIndex), reinterpret_cast<uintptr_t>(cpuPointer), alignedSize, 0u, maxOsContextCount));
1557+
1558+
if (!bo) {
1559+
this->munmapFunction(cpuBasePointer, totalSizeToAlloc);
1560+
return nullptr;
1561+
}
1562+
1563+
uint64_t offset = 0;
1564+
if (!retrieveMmapOffsetForBufferObject(allocationData.rootDeviceIndex, *bo, I915_MMAP_OFFSET_WB, offset)) {
1565+
this->munmapFunction(cpuPointer, size);
1566+
return nullptr;
1567+
}
1568+
1569+
[[maybe_unused]] auto retPtr = this->mmapFunction(cpuPointer, alignedSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, getDrm(allocationData.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(offset));
1570+
DEBUG_BREAK_IF(retPtr != cpuPointer);
1571+
1572+
obtainGpuAddress(allocationData, bo.get(), gpuAddress);
1573+
emitPinningRequest(bo.get(), allocationData);
1574+
1575+
auto allocation = std::make_unique<DrmAllocation>(allocationData.rootDeviceIndex, allocationData.type, bo.get(), cpuPointer, bo->peekAddress(), alignedSize, MemoryPool::System4KBPages);
1576+
allocation->setMmapPtr(cpuPointer);
1577+
allocation->setMmapSize(alignedSize);
1578+
if (pointerDiff != 0) {
1579+
allocation->registerMemoryToUnmap(cpuBasePointer, pointerDiff, this->munmapFunction);
1580+
}
1581+
[[maybe_unused]] int retCode = this->munmapFunction(ptrOffset(cpuPointer, alignedSize), alignment - pointerDiff);
1582+
DEBUG_BREAK_IF(retCode != 0);
1583+
allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuAddress), alignedSize);
1584+
if (!allocation->setCacheRegion(&this->getDrm(allocationData.rootDeviceIndex), static_cast<CacheRegion>(allocationData.cacheRegion))) {
1585+
if (pointerDiff == 0) {
1586+
allocation->registerMemoryToUnmap(cpuBasePointer, totalSizeToAlloc, this->munmapFunction);
1587+
}
1588+
return nullptr;
1589+
}
1590+
1591+
bo.release();
1592+
1593+
return allocation.release();
1594+
} else {
1595+
return createAllocWithAlignmentFromUserptr(allocationData, size, alignment, alignedSize, gpuAddress);
1596+
}
1597+
}
1598+
1599+
void *DrmMemoryManager::lockResourceInLocalMemoryImpl(BufferObject *bo) {
1600+
if (bo == nullptr) {
1601+
return nullptr;
1602+
}
1603+
1604+
auto rootDeviceIndex = this->getRootDeviceIndex(bo->peekDrm());
1605+
1606+
uint64_t offset = 0;
1607+
if (!retrieveMmapOffsetForBufferObject(rootDeviceIndex, *bo, I915_MMAP_OFFSET_WC, offset)) {
1608+
return nullptr;
1609+
}
1610+
1611+
auto addr = mmapFunction(nullptr, bo->peekSize(), PROT_WRITE | PROT_READ, MAP_SHARED, getDrm(rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(offset));
1612+
DEBUG_BREAK_IF(addr == MAP_FAILED);
1613+
1614+
bo->setLockedAddress(addr);
1615+
1616+
return bo->peekLockedAddress();
1617+
}
1618+
15411619
} // namespace NEO

shared/source/os_interface/linux/drm_memory_manager_create_multi_host_allocation.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,56 @@
55
*
66
*/
77

8+
#include "shared/source/gmm_helper/gmm.h"
9+
#include "shared/source/gmm_helper/gmm_helper.h"
10+
#include "shared/source/memory_manager/memory_pool.h"
811
#include "shared/source/os_interface/linux/drm_memory_manager.h"
912

1013
namespace NEO {
14+
1115
DrmAllocation *DrmMemoryManager::createMultiHostAllocation(const AllocationData &allocationData) {
12-
return allocateGraphicsMemoryWithAlignmentImpl(allocationData);
16+
if (!isAligned<MemoryConstants::pageSize>(allocationData.size)) {
17+
return nullptr;
18+
}
19+
20+
auto numTiles = allocationData.storageInfo.getNumBanks();
21+
auto sizePerTile = allocationData.size;
22+
auto hostSizeToAllocate = numTiles * sizePerTile;
23+
24+
auto cpuBasePointer = alignedMallocWrapper(hostSizeToAllocate, MemoryConstants::pageSize);
25+
if (!cpuBasePointer) {
26+
return nullptr;
27+
}
28+
29+
zeroCpuMemoryIfRequested(allocationData, cpuBasePointer, hostSizeToAllocate);
30+
31+
auto gpuAddress = acquireGpuRange(sizePerTile, allocationData.rootDeviceIndex, HeapIndex::HEAP_STANDARD);
32+
auto allocation = new DrmAllocation(allocationData.rootDeviceIndex, numTiles, allocationData.type,
33+
nullptr /*bo*/, cpuBasePointer, gpuAddress, sizePerTile, MemoryPool::System4KBPages);
34+
35+
allocation->storageInfo = allocationData.storageInfo;
36+
allocation->setFlushL3Required(true);
37+
allocation->setUncacheable(true);
38+
allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuAddress), sizePerTile);
39+
allocation->setDriverAllocatedCpuPtr(cpuBasePointer);
40+
41+
for (auto tile = 0u, currentBank = 0u; tile < numTiles; ++tile, ++currentBank) {
42+
while (!allocationData.storageInfo.memoryBanks.test(currentBank)) {
43+
++currentBank;
44+
}
45+
46+
auto boHostPtr = static_cast<uint8_t *>(cpuBasePointer) + tile * sizePerTile;
47+
auto bo = allocUserptr(reinterpret_cast<uintptr_t>(boHostPtr), sizePerTile, 0, allocationData.rootDeviceIndex);
48+
if (!bo) {
49+
freeGraphicsMemoryImpl(allocation);
50+
return nullptr;
51+
}
52+
53+
bo->setAddress(gpuAddress);
54+
allocation->getBufferObjectToModify(currentBank) = bo;
55+
}
56+
57+
return allocation;
1358
}
59+
1460
} // namespace NEO

shared/source/os_interface/linux/drm_memory_manager_local_memory.cpp

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -30,80 +30,8 @@ DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandl
3030
handle, MemoryPool::SystemCpuInaccessible);
3131
}
3232

33-
DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &allocationData, size_t size, size_t alignment, size_t alignedSize, uint64_t gpuAddress) {
34-
bool useBooMmap = this->getDrm(allocationData.rootDeviceIndex).getMemoryInfo() && allocationData.useMmapObject;
35-
36-
if (DebugManager.flags.EnableBOMmapCreate.get() != -1) {
37-
useBooMmap = DebugManager.flags.EnableBOMmapCreate.get();
38-
}
39-
40-
if (useBooMmap) {
41-
auto totalSizeToAlloc = alignedSize + alignment;
42-
auto cpuPointer = this->mmapFunction(0, totalSizeToAlloc, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
43-
44-
auto cpuBasePointer = cpuPointer;
45-
cpuPointer = alignUp(cpuPointer, alignment);
46-
47-
auto pointerDiff = ptrDiff(cpuPointer, cpuBasePointer);
48-
std::unique_ptr<BufferObject, BufferObject::Deleter> bo(this->createBufferObjectInMemoryRegion(&this->getDrm(allocationData.rootDeviceIndex), reinterpret_cast<uintptr_t>(cpuPointer), alignedSize, 0u, maxOsContextCount));
49-
50-
if (!bo) {
51-
this->munmapFunction(cpuBasePointer, totalSizeToAlloc);
52-
return nullptr;
53-
}
54-
55-
uint64_t offset = 0;
56-
if (!retrieveMmapOffsetForBufferObject(allocationData.rootDeviceIndex, *bo, I915_MMAP_OFFSET_WB, offset)) {
57-
this->munmapFunction(cpuPointer, size);
58-
return nullptr;
59-
}
60-
61-
[[maybe_unused]] auto retPtr = this->mmapFunction(cpuPointer, alignedSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, getDrm(allocationData.rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(offset));
62-
DEBUG_BREAK_IF(retPtr != cpuPointer);
63-
64-
obtainGpuAddress(allocationData, bo.get(), gpuAddress);
65-
emitPinningRequest(bo.get(), allocationData);
66-
67-
auto allocation = new DrmAllocation(allocationData.rootDeviceIndex, allocationData.type, bo.get(), cpuPointer, bo->peekAddress(), alignedSize, MemoryPool::System4KBPages);
68-
allocation->setMmapPtr(cpuPointer);
69-
allocation->setMmapSize(alignedSize);
70-
if (pointerDiff != 0) {
71-
allocation->registerMemoryToUnmap(cpuBasePointer, pointerDiff, this->munmapFunction);
72-
}
73-
[[maybe_unused]] int retCode = this->munmapFunction(ptrOffset(cpuPointer, alignedSize), alignment - pointerDiff);
74-
DEBUG_BREAK_IF(retCode != 0);
75-
allocation->setReservedAddressRange(reinterpret_cast<void *>(gpuAddress), alignedSize);
76-
77-
bo.release();
78-
79-
return allocation;
80-
} else {
81-
return createAllocWithAlignmentFromUserptr(allocationData, size, alignment, alignedSize, gpuAddress);
82-
}
83-
}
84-
8533
GraphicsAllocation *DrmMemoryManager::createSharedUnifiedMemoryAllocation(const AllocationData &allocationData) {
8634
return nullptr;
8735
}
8836

89-
void *DrmMemoryManager::lockResourceInLocalMemoryImpl(BufferObject *bo) {
90-
if (bo == nullptr) {
91-
return nullptr;
92-
}
93-
94-
auto rootDeviceIndex = this->getRootDeviceIndex(bo->peekDrm());
95-
96-
uint64_t offset = 0;
97-
if (!retrieveMmapOffsetForBufferObject(rootDeviceIndex, *bo, I915_MMAP_OFFSET_WC, offset)) {
98-
return nullptr;
99-
}
100-
101-
auto addr = mmapFunction(nullptr, bo->peekSize(), PROT_WRITE | PROT_READ, MAP_SHARED, getDrm(rootDeviceIndex).getFileDescriptor(), static_cast<off_t>(offset));
102-
DEBUG_BREAK_IF(addr == MAP_FAILED);
103-
104-
bo->setLockedAddress(addr);
105-
106-
return bo->peekLockedAddress();
107-
}
108-
10937
} // namespace NEO

shared/source/os_interface/linux/drm_memory_operations_handler_create.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55
*
66
*/
77

8+
#include "shared/source/execution_environment/root_device_environment.h"
9+
#include "shared/source/os_interface/linux/drm_memory_operations_handler_bind.h"
810
#include "shared/source/os_interface/linux/drm_memory_operations_handler_default.h"
11+
#include "shared/source/os_interface/linux/drm_neo.h"
912

1013
namespace NEO {
1114

1215
std::unique_ptr<DrmMemoryOperationsHandler> DrmMemoryOperationsHandler::create(Drm &drm, uint32_t rootDeviceIndex) {
16+
bool useVmBind = drm.isVmBindAvailable();
17+
18+
if (useVmBind) {
19+
return std::make_unique<DrmMemoryOperationsHandlerBind>(drm.getRootDeviceEnvironment(), rootDeviceIndex);
20+
}
21+
1322
return std::make_unique<DrmMemoryOperationsHandlerDefault>();
1423
}
1524

0 commit comments

Comments
 (0)