@@ -1631,15 +1631,7 @@ void *DrmMemoryManager::lockResourceImpl(GraphicsAllocation &graphicsAllocation)
16311631 return cpuPtr;
16321632 }
16331633
1634- auto rootDeviceIndex = graphicsAllocation.getRootDeviceIndex ();
1635- auto ioctlHelper = this ->getDrm (rootDeviceIndex).getIoctlHelper ();
1636-
1637- if (ioctlHelper->makeResidentBeforeLockNeeded ()) {
1638- auto memoryOperationsInterface = static_cast <DrmMemoryOperationsHandler *>(executionEnvironment.rootDeviceEnvironments [rootDeviceIndex]->memoryOperationsInterface .get ());
1639- auto graphicsAllocationPtr = &graphicsAllocation;
1640- [[maybe_unused]] auto ret = memoryOperationsInterface->makeResidentWithinOsContext (getDefaultOsContext (rootDeviceIndex), ArrayRef<NEO::GraphicsAllocation *>(&graphicsAllocationPtr, 1 ), false , false , true ) == MemoryOperationsStatus::success;
1641- DEBUG_BREAK_IF (!ret);
1642- }
1634+ makeAllocationResidentIfNeeded (&graphicsAllocation);
16431635
16441636 auto bo = static_cast <DrmAllocation &>(graphicsAllocation).getBO ();
16451637 if (graphicsAllocation.getAllocationType () == AllocationType::writeCombined) {
@@ -1698,6 +1690,16 @@ Drm &DrmMemoryManager::getDrm(uint32_t rootDeviceIndex) const {
16981690 return *this ->executionEnvironment .rootDeviceEnvironments [rootDeviceIndex]->osInterface ->getDriverModel ()->as <Drm>();
16991691}
17001692
1693+ void DrmMemoryManager::makeAllocationResidentIfNeeded (GraphicsAllocation *allocation) {
1694+ auto rootDeviceIndex = allocation->getRootDeviceIndex ();
1695+ auto ioctlHelper = this ->getDrm (rootDeviceIndex).getIoctlHelper ();
1696+ if (ioctlHelper->makeResidentBeforeLockNeeded ()) {
1697+ auto memoryOperationsInterface = executionEnvironment.rootDeviceEnvironments [rootDeviceIndex]->memoryOperationsInterface .get ();
1698+ [[maybe_unused]] auto ret = memoryOperationsInterface->makeResidentWithinOsContext (getDefaultOsContext (rootDeviceIndex), ArrayRef<NEO::GraphicsAllocation *>(&allocation, 1 ), false , false , true ) == MemoryOperationsStatus::success;
1699+ DEBUG_BREAK_IF (!ret);
1700+ }
1701+ }
1702+
17011703uint32_t DrmMemoryManager::getRootDeviceIndex (const Drm *drm) {
17021704 auto rootDeviceCount = this ->executionEnvironment .rootDeviceEnvironments .size ();
17031705
@@ -2676,12 +2678,7 @@ DrmAllocation *DrmMemoryManager::createAllocWithAlignment(const AllocationData &
26762678 auto canonizedGpuAddress = gmmHelper->canonize (bo->peekAddress ());
26772679 auto allocation = std::make_unique<DrmAllocation>(allocationData.rootDeviceIndex , 1u /* num gmms*/ , allocationData.type , bo.get (), nullptr , canonizedGpuAddress, alignedSize, memoryPool);
26782680
2679- if (ioctlHelper->makeResidentBeforeLockNeeded ()) {
2680- auto memoryOperationsInterface = static_cast <DrmMemoryOperationsHandler *>(executionEnvironment.rootDeviceEnvironments [allocationData.rootDeviceIndex ]->memoryOperationsInterface .get ());
2681- GraphicsAllocation *allocationPtr = allocation.get ();
2682- [[maybe_unused]] auto ret = memoryOperationsInterface->makeResidentWithinOsContext (getDefaultOsContext (allocationData.rootDeviceIndex ), ArrayRef<NEO::GraphicsAllocation *>(&allocationPtr, 1 ), false , false , true ) == MemoryOperationsStatus::success;
2683- DEBUG_BREAK_IF (!ret);
2684- }
2681+ makeAllocationResidentIfNeeded (allocation.get ());
26852682
26862683 [[maybe_unused]] auto retPtr = ioctlHelper->mmapFunction (*this , cpuPointer, alignedSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, drm.getFileDescriptor (), static_cast <off_t >(offset));
26872684 DEBUG_BREAK_IF (retPtr != cpuPointer);
@@ -2826,6 +2823,7 @@ GraphicsAllocation *DrmMemoryManager::createSharedUnifiedMemoryAllocation(const
28262823 auto alignSize = alignUp (remainingSize, MemoryConstants::pageSize64k);
28272824 auto remainingMemoryBanks = allocationData.storageInfo .memoryBanks ;
28282825 auto numHandles = GraphicsAllocation::getNumHandlesForKmdSharedAllocation (allocationData.storageInfo .getNumBanks ());
2826+ bool makeResidentBeforeLock = ioctlHelper->makeResidentBeforeLockNeeded ();
28292827
28302828 bool useChunking = false ;
28312829 uint32_t numOfChunks = 0 ;
@@ -2879,6 +2877,12 @@ GraphicsAllocation *DrmMemoryManager::createSharedUnifiedMemoryAllocation(const
28792877 return nullptr ;
28802878 }
28812879
2880+ if (makeResidentBeforeLock) {
2881+ bo->requireImmediateBinding (true );
2882+ [[maybe_unused]] auto ret = bo->bind (getDefaultOsContext (allocationData.rootDeviceIndex ), 0 , false );
2883+ DEBUG_BREAK_IF (ret != 0 );
2884+ }
2885+
28822886 ioctlHelper->mmapFunction (*this , currentAddress, currentSize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, drm.getFileDescriptor (), static_cast <off_t >(offset));
28832887
28842888 bo->setAddress (castToUint64 (currentAddress));
@@ -2902,6 +2906,12 @@ GraphicsAllocation *DrmMemoryManager::createSharedUnifiedMemoryAllocation(const
29022906 allocation->storageInfo = allocationData.storageInfo ;
29032907 allocation->storageInfo .isChunked = useChunking;
29042908 allocation->storageInfo .numOfChunks = numOfChunks;
2909+
2910+ if (makeResidentBeforeLock) {
2911+ auto osContext = getDefaultOsContext (allocationData.rootDeviceIndex );
2912+ allocation->updateResidencyTaskCount (GraphicsAllocation::objectAlwaysResident, osContext->getContextId ());
2913+ }
2914+
29052915 if (!allocation->setCacheRegion (&drm, static_cast <CacheRegion>(allocationData.cacheRegion ))) {
29062916 ioctlHelper->munmapFunction (*this , cpuBasePointer, totalSizeToAlloc);
29072917 for (auto bo : bos) {
@@ -3039,9 +3049,6 @@ DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandl
30393049 return nullptr ;
30403050 }
30413051
3042- [[maybe_unused]] auto retPtr = this ->mmapFunction (cpuPointer, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, drm.getFileDescriptor (), static_cast <off_t >(offset));
3043- DEBUG_BREAK_IF (retPtr != cpuPointer);
3044-
30453052 AllocationData allocationData = {};
30463053 allocationData.rootDeviceIndex = properties.rootDeviceIndex ;
30473054 allocationData.size = size;
@@ -3058,7 +3065,14 @@ DrmAllocation *DrmMemoryManager::createUSMHostAllocationFromSharedHandle(osHandl
30583065
30593066 pushSharedBufferObject (bo);
30603067
3061- auto drmAllocation = std::make_unique<DrmAllocation>(properties.rootDeviceIndex , 1u /* num gmms*/ , properties.allocationType , bo, cpuPointer, bo->peekAddress (), bo->peekSize (), memoryPool);
3068+ auto drmAllocation = std::make_unique<DrmAllocation>(properties.rootDeviceIndex , 1u /* num gmms*/ , properties.allocationType , bo, nullptr , bo->peekAddress (), bo->peekSize (), memoryPool);
3069+
3070+ makeAllocationResidentIfNeeded (drmAllocation.get ());
3071+
3072+ [[maybe_unused]] auto retPtr = this ->mmapFunction (cpuPointer, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, drm.getFileDescriptor (), static_cast <off_t >(offset));
3073+ DEBUG_BREAK_IF (retPtr != cpuPointer);
3074+
3075+ drmAllocation->setCpuPtrAndGpuAddress (cpuPointer, bo->peekAddress ());
30623076 drmAllocation->setMmapPtr (cpuPointer);
30633077 drmAllocation->setMmapSize (size);
30643078 drmAllocation->setReservedAddressRange (reinterpret_cast <void *>(cpuPointer), size);
0 commit comments