1010#include " shared/source/command_container/implicit_scaling.h"
1111#include " shared/source/command_stream/command_stream_receiver.h"
1212#include " shared/source/execution_environment/root_device_environment.h"
13- #include " shared/source/helpers/aligned_memory .h"
13+ #include " shared/source/helpers/basic_math .h"
1414#include " shared/source/helpers/gfx_core_helper.h"
1515#include " shared/source/helpers/ptr_math.h"
1616#include " shared/source/memory_manager/allocation_properties.h"
1717#include " shared/source/memory_manager/memory_operations_handler.h"
1818#include " shared/source/memory_manager/unified_memory_manager.h"
19- #include " shared/source/utilities/cpu_info.h"
2019
2120#include " level_zero/api/driver_experimental/public/zex_memory.h"
2221#include " level_zero/core/source/cmdlist/cmdlist.h"
@@ -1048,59 +1047,21 @@ NEO::VirtualMemoryReservation *ContextImp::findSupportedVirtualReservation(const
10481047ze_result_t ContextImp::reserveVirtualMem (const void *pStart,
10491048 size_t size,
10501049 void **pptr) {
1051- uint64_t maxCpuVa = 0 ;
1052- if (this ->driverHandle ->getMemoryManager ()->peek32bit ()) {
1053- maxCpuVa = maxNBitValue (32 );
1054- } else {
1055- maxCpuVa = NEO::CpuInfo::getInstance ().getVirtualAddressSize () == 57u ? maxNBitValue (56 ) : maxNBitValue (47 );
1056- }
1057- bool reserveOnSvmHeap = pStart == nullptr ;
1058- if (castToUint64 (pStart) <= maxCpuVa) {
1059- reserveOnSvmHeap = true ;
1060- }
1061-
1062- NEO::AddressRange addressRange{};
1063- uint32_t reservedOnRootDeviceIndex = 0 ;
1064- uint64_t reservationBase = 0 ;
1065- size_t reservationTotalSize = 0 ;
1066-
1067- if (reserveOnSvmHeap) {
1068- if (alignUp (size, MemoryConstants::pageSize) != size) {
1069- return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
1070- }
1071- reservationTotalSize = size + MemoryConstants::pageSize2M;
1072- addressRange = this ->driverHandle ->getMemoryManager ()->reserveCpuAddressWithZeroBaseRetry (castToUint64 (pStart), reservationTotalSize);
1073- if (addressRange.address == 0 ) {
1074- return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
1075- }
1076- DEBUG_BREAK_IF (addressRange.address + reservationTotalSize > maxCpuVa);
1077- reservationBase = addressRange.address ;
1078- addressRange.address = alignUp (addressRange.address , MemoryConstants::pageSize2M);
1079- addressRange.size = size;
1080- } else {
1081- NEO::HeapIndex heap;
1082- size_t pageSize;
1083- if ((getPageAlignedSizeRequired (size, &heap, &pageSize) != size)) {
1084- return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
1085- }
1086- addressRange = this ->driverHandle ->getMemoryManager ()->reserveGpuAddressOnHeap (castToUint64 (pStart), size, this ->driverHandle ->rootDeviceIndices , &reservedOnRootDeviceIndex, heap, pageSize);
1087- if (addressRange.address == 0 ) {
1088- return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;
1089- }
1090- reservationBase = addressRange.address ;
1091- reservationTotalSize = addressRange.size ;
1050+ NEO::HeapIndex heap;
1051+ size_t pageSize;
1052+ if ((getPageAlignedSizeRequired (size, &heap, &pageSize) != size)) {
1053+ return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
10921054 }
1093-
10941055 NEO::VirtualMemoryReservation *virtualMemoryReservation = new NEO::VirtualMemoryReservation;
1095- virtualMemoryReservation->virtualAddressRange = addressRange;
1096- virtualMemoryReservation->isSvmReservation = reserveOnSvmHeap;
1097- virtualMemoryReservation->rootDeviceIndex = reservedOnRootDeviceIndex;
1056+ virtualMemoryReservation->virtualAddressRange = this ->driverHandle ->getMemoryManager ()->reserveGpuAddressOnHeap (reinterpret_cast <uint64_t >(pStart), size, this ->driverHandle ->rootDeviceIndices , &virtualMemoryReservation->rootDeviceIndex , heap, pageSize);
1057+ if (virtualMemoryReservation->virtualAddressRange .address == 0 ) {
1058+ delete virtualMemoryReservation;
1059+ return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;
1060+ }
10981061 virtualMemoryReservation->flags .readWrite = false ;
10991062 virtualMemoryReservation->flags .readOnly = false ;
11001063 virtualMemoryReservation->flags .noAccess = true ;
11011064 virtualMemoryReservation->reservationSize = size;
1102- virtualMemoryReservation->reservationBase = reservationBase;
1103- virtualMemoryReservation->reservationTotalSize = reservationTotalSize;
11041065 auto lock = this ->driverHandle ->getMemoryManager ()->lockVirtualMemoryReservationMap ();
11051066 this ->driverHandle ->getMemoryManager ()->getVirtualMemoryReservationMap ().insert (std::pair<void *, NEO::VirtualMemoryReservation *>(reinterpret_cast <void *>(virtualMemoryReservation->virtualAddressRange .address ), virtualMemoryReservation));
11061067 *pptr = reinterpret_cast <void *>(virtualMemoryReservation->virtualAddressRange .address );
@@ -1121,12 +1082,7 @@ ze_result_t ContextImp::freeVirtualMem(const void *ptr,
11211082 if (virtualMemoryReservation->reservationSize != size) {
11221083 return ZE_RESULT_ERROR_INVALID_ARGUMENT;
11231084 }
1124- NEO::AddressRange addressRange{virtualMemoryReservation->reservationBase , virtualMemoryReservation->reservationTotalSize };
1125- if (virtualMemoryReservation->isSvmReservation ) {
1126- this ->driverHandle ->getMemoryManager ()->freeCpuAddress (addressRange);
1127- } else {
1128- this ->driverHandle ->getMemoryManager ()->freeGpuAddress (addressRange, virtualMemoryReservation->rootDeviceIndex );
1129- }
1085+ this ->driverHandle ->getMemoryManager ()->freeGpuAddress (virtualMemoryReservation->virtualAddressRange , virtualMemoryReservation->rootDeviceIndex );
11301086 delete virtualMemoryReservation;
11311087 this ->driverHandle ->getMemoryManager ()->getVirtualMemoryReservationMap ().erase (it);
11321088 virtualMemoryReservation = nullptr ;
0 commit comments