1 /*
2  * Copyright (C) 2020-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #include "level_zero/core/source/driver/driver_handle_imp.h"
9 
10 #include "shared/source/debug_settings/debug_settings_manager.h"
11 #include "shared/source/device/device.h"
12 #include "shared/source/helpers/string.h"
13 #include "shared/source/memory_manager/memory_manager.h"
14 #include "shared/source/os_interface/os_library.h"
15 
16 #include "level_zero/core/source/context/context_imp.h"
17 #include "level_zero/core/source/debugger/debugger_l0.h"
18 #include "level_zero/core/source/device/device_imp.h"
19 #include "level_zero/core/source/driver/driver_imp.h"
20 #include "level_zero/core/source/driver/host_pointer_manager.h"
21 
22 #include "driver_version_l0.h"
23 
24 #include <cstdlib>
25 #include <cstring>
26 #include <ctime>
27 #include <vector>
28 
29 namespace L0 {
30 
31 struct DriverHandleImp *GlobalDriver;
32 
33 DriverHandleImp::DriverHandleImp() = default;
34 
createContext(const ze_context_desc_t * desc,uint32_t numDevices,ze_device_handle_t * phDevices,ze_context_handle_t * phContext)35 ze_result_t DriverHandleImp::createContext(const ze_context_desc_t *desc,
36                                            uint32_t numDevices,
37                                            ze_device_handle_t *phDevices,
38                                            ze_context_handle_t *phContext) {
39     ContextImp *context = new ContextImp(this);
40     if (nullptr == context) {
41         return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
42     }
43 
44     *phContext = context->toHandle();
45 
46     if (numDevices == 0) {
47         for (auto device : this->devices) {
48             context->addDeviceAndSubDevices(device);
49         }
50     } else {
51         for (uint32_t i = 0; i < numDevices; i++) {
52             context->addDeviceAndSubDevices(Device::fromHandle(phDevices[i]));
53         }
54     }
55 
56     for (auto devicePair : context->getDevices()) {
57         auto neoDevice = devicePair.second->getNEODevice();
58         context->rootDeviceIndices.insert(neoDevice->getRootDeviceIndex());
59         context->deviceBitfields.insert({neoDevice->getRootDeviceIndex(),
60                                          neoDevice->getDeviceBitfield()});
61     }
62 
63     return ZE_RESULT_SUCCESS;
64 }
65 
getMemoryManager()66 NEO::MemoryManager *DriverHandleImp::getMemoryManager() {
67     return this->memoryManager;
68 }
69 
setMemoryManager(NEO::MemoryManager * memoryManager)70 void DriverHandleImp::setMemoryManager(NEO::MemoryManager *memoryManager) {
71     this->memoryManager = memoryManager;
72 }
73 
getSvmAllocsManager()74 NEO::SVMAllocsManager *DriverHandleImp::getSvmAllocsManager() {
75     return this->svmAllocsManager;
76 }
77 
getApiVersion(ze_api_version_t * version)78 ze_result_t DriverHandleImp::getApiVersion(ze_api_version_t *version) {
79     *version = ZE_API_VERSION_1_2;
80     return ZE_RESULT_SUCCESS;
81 }
82 
getProperties(ze_driver_properties_t * properties)83 ze_result_t DriverHandleImp::getProperties(ze_driver_properties_t *properties) {
84     uint32_t versionMajor = static_cast<uint32_t>(strtoul(L0_PROJECT_VERSION_MAJOR, NULL, 10));
85     uint32_t versionMinor = static_cast<uint32_t>(strtoul(L0_PROJECT_VERSION_MINOR, NULL, 10));
86     uint32_t versionBuild = static_cast<uint32_t>(strtoul(NEO_VERSION_BUILD, NULL, 10));
87 
88     properties->driverVersion = ((versionMajor << 24) & 0xFF000000) |
89                                 ((versionMinor << 16) & 0x00FF0000) |
90                                 (versionBuild & 0x0000FFFF);
91 
92     uint64_t uniqueId = (properties->driverVersion) | (uuidTimestamp & 0xFFFFFFFF00000000);
93     memcpy_s(properties->uuid.id, sizeof(uniqueId), &uniqueId, sizeof(uniqueId));
94 
95     return ZE_RESULT_SUCCESS;
96 }
97 
getIPCProperties(ze_driver_ipc_properties_t * pIPCProperties)98 ze_result_t DriverHandleImp::getIPCProperties(ze_driver_ipc_properties_t *pIPCProperties) {
99     pIPCProperties->flags = ZE_IPC_PROPERTY_FLAG_MEMORY;
100 
101     return ZE_RESULT_SUCCESS;
102 }
103 
getExtensionFunctionAddress(const char * pFuncName,void ** pfunc)104 ze_result_t DriverHandleImp::getExtensionFunctionAddress(const char *pFuncName, void **pfunc) {
105     auto funcAddr = extensionFunctionsLookupMap.find(std::string(pFuncName));
106     if (funcAddr != extensionFunctionsLookupMap.end()) {
107         *pfunc = funcAddr->second;
108         return ZE_RESULT_SUCCESS;
109     }
110     return ZE_RESULT_ERROR_INVALID_ARGUMENT;
111 }
112 
getExtensionProperties(uint32_t * pCount,ze_driver_extension_properties_t * pExtensionProperties)113 ze_result_t DriverHandleImp::getExtensionProperties(uint32_t *pCount,
114                                                     ze_driver_extension_properties_t *pExtensionProperties) {
115     if (nullptr == pExtensionProperties) {
116         *pCount = static_cast<uint32_t>(this->extensionsSupported.size());
117         return ZE_RESULT_SUCCESS;
118     }
119 
120     *pCount = std::min(static_cast<uint32_t>(this->extensionsSupported.size()), *pCount);
121 
122     for (uint32_t i = 0; i < *pCount; i++) {
123         auto extension = this->extensionsSupported[i];
124         strncpy_s(pExtensionProperties[i].name, ZE_MAX_EXTENSION_NAME,
125                   extension.first.c_str(), extension.first.length() + 1);
126         pExtensionProperties[i].version = extension.second;
127     }
128 
129     return ZE_RESULT_SUCCESS;
130 }
131 
~DriverHandleImp()132 DriverHandleImp::~DriverHandleImp() {
133     for (auto &device : this->devices) {
134         delete device;
135     }
136     if (this->svmAllocsManager) {
137         delete this->svmAllocsManager;
138         this->svmAllocsManager = nullptr;
139     }
140 }
141 
updateRootDeviceBitFields(std::unique_ptr<NEO::Device> & neoDevice)142 void DriverHandleImp::updateRootDeviceBitFields(std::unique_ptr<NEO::Device> &neoDevice) {
143     const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
144     auto entry = this->deviceBitfields.find(rootDeviceIndex);
145     entry->second = neoDevice->getDeviceBitfield();
146 }
147 
enableRootDeviceDebugger(std::unique_ptr<NEO::Device> & neoDevice)148 void DriverHandleImp::enableRootDeviceDebugger(std::unique_ptr<NEO::Device> &neoDevice) {
149     const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
150     auto rootDeviceEnvironment = neoDevice->getExecutionEnvironment()->rootDeviceEnvironments[rootDeviceIndex].get();
151 
152     if (enableProgramDebugging) {
153         if (neoDevice->getDebugger() != nullptr) {
154             NEO::printDebugString(NEO::DebugManager.flags.PrintDebugMessages.get(), stderr,
155                                   "%s", "Source Level Debugger cannot be used with Environment Variable enabling program debugging.\n");
156             UNRECOVERABLE_IF(neoDevice->getDebugger() != nullptr && enableProgramDebugging);
157         }
158         rootDeviceEnvironment->getMutableHardwareInfo()->capabilityTable.fusedEuEnabled = false;
159 
160         rootDeviceEnvironment->debugger = DebuggerL0::create(neoDevice.get());
161     }
162 }
163 
initialize(std::vector<std::unique_ptr<NEO::Device>> neoDevices)164 ze_result_t DriverHandleImp::initialize(std::vector<std::unique_ptr<NEO::Device>> neoDevices) {
165     bool multiOsContextDriver = false;
166     for (auto &neoDevice : neoDevices) {
167         ze_result_t returnValue = ZE_RESULT_SUCCESS;
168         if (!neoDevice->getHardwareInfo().capabilityTable.levelZeroSupported) {
169             continue;
170         }
171 
172         if (this->memoryManager == nullptr) {
173             this->memoryManager = neoDevice->getMemoryManager();
174             if (this->memoryManager == nullptr) {
175                 return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
176             }
177         }
178 
179         const auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
180 
181         enableRootDeviceDebugger(neoDevice);
182 
183         this->rootDeviceIndices.insert(rootDeviceIndex);
184         this->deviceBitfields.insert({rootDeviceIndex, neoDevice->getDeviceBitfield()});
185 
186         auto pNeoDevice = neoDevice.release();
187 
188         auto device = Device::create(this, pNeoDevice, false, &returnValue);
189         this->devices.push_back(device);
190 
191         multiOsContextDriver |= device->isImplicitScalingCapable();
192         if (returnValue != ZE_RESULT_SUCCESS) {
193             return returnValue;
194         }
195     }
196 
197     if (this->devices.size() == 0) {
198         return ZE_RESULT_ERROR_UNINITIALIZED;
199     }
200 
201     this->svmAllocsManager = new NEO::SVMAllocsManager(memoryManager, multiOsContextDriver);
202     if (this->svmAllocsManager == nullptr) {
203         return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
204     }
205 
206     this->numDevices = static_cast<uint32_t>(this->devices.size());
207 
208     extensionFunctionsLookupMap = getExtensionFunctionsLookupMap();
209 
210     uuidTimestamp = static_cast<uint64_t>(std::chrono::system_clock::now().time_since_epoch().count());
211 
212     if (NEO::DebugManager.flags.EnableHostPointerImport.get() != 0) {
213         createHostPointerManager();
214     }
215 
216     return ZE_RESULT_SUCCESS;
217 }
218 
create(std::vector<std::unique_ptr<NEO::Device>> devices,const L0EnvVariables & envVariables,ze_result_t * returnValue)219 DriverHandle *DriverHandle::create(std::vector<std::unique_ptr<NEO::Device>> devices, const L0EnvVariables &envVariables, ze_result_t *returnValue) {
220     DriverHandleImp *driverHandle = new DriverHandleImp;
221     UNRECOVERABLE_IF(nullptr == driverHandle);
222 
223     driverHandle->enableProgramDebugging = envVariables.programDebugging;
224     driverHandle->enableSysman = envVariables.sysman;
225     driverHandle->enablePciIdDeviceOrder = envVariables.pciIdDeviceOrder;
226     ze_result_t res = driverHandle->initialize(std::move(devices));
227     if (res != ZE_RESULT_SUCCESS) {
228         delete driverHandle;
229         *returnValue = res;
230         return nullptr;
231     }
232 
233     GlobalDriver = driverHandle;
234 
235     driverHandle->getMemoryManager()->setForceNonSvmForExternalHostPtr(true);
236 
237     return driverHandle;
238 }
239 
getDevice(uint32_t * pCount,ze_device_handle_t * phDevices)240 ze_result_t DriverHandleImp::getDevice(uint32_t *pCount, ze_device_handle_t *phDevices) {
241     if (*pCount == 0) {
242         *pCount = this->numDevices;
243         return ZE_RESULT_SUCCESS;
244     }
245 
246     if (phDevices == nullptr) {
247         return ZE_RESULT_ERROR_INVALID_NULL_HANDLE;
248     }
249 
250     for (uint32_t i = 0; i < *pCount; i++) {
251         phDevices[i] = this->devices[i];
252     }
253 
254     return ZE_RESULT_SUCCESS;
255 }
256 
findAllocationDataForRange(const void * buffer,size_t size,NEO::SvmAllocationData ** allocData)257 bool DriverHandleImp::findAllocationDataForRange(const void *buffer,
258                                                  size_t size,
259                                                  NEO::SvmAllocationData **allocData) {
260 
261     size_t offset = 0;
262     if (size > 0) {
263         offset = size - 1;
264     }
265 
266     // Make sure the host buffer does not overlap any existing allocation
267     const char *baseAddress = reinterpret_cast<const char *>(buffer);
268     NEO::SvmAllocationData *beginAllocData = svmAllocsManager->getSVMAlloc(baseAddress);
269     NEO::SvmAllocationData *endAllocData = svmAllocsManager->getSVMAlloc(baseAddress + offset);
270 
271     if (allocData) {
272         if (beginAllocData) {
273             *allocData = beginAllocData;
274         } else {
275             *allocData = endAllocData;
276         }
277     }
278 
279     // Return true if the whole range requested is covered by the same allocation
280     if (beginAllocData && endAllocData &&
281         (beginAllocData->gpuAllocations.getDefaultGraphicsAllocation() == endAllocData->gpuAllocations.getDefaultGraphicsAllocation())) {
282         return true;
283     }
284     return false;
285 }
286 
findAllocationsWithinRange(const void * buffer,size_t size,bool * allocationRangeCovered)287 std::vector<NEO::SvmAllocationData *> DriverHandleImp::findAllocationsWithinRange(const void *buffer,
288                                                                                   size_t size,
289                                                                                   bool *allocationRangeCovered) {
290     std::vector<NEO::SvmAllocationData *> allocDataArray;
291     const char *baseAddress = reinterpret_cast<const char *>(buffer);
292     // Check if the host buffer overlaps any existing allocation
293     NEO::SvmAllocationData *beginAllocData = svmAllocsManager->getSVMAlloc(baseAddress);
294     NEO::SvmAllocationData *endAllocData = svmAllocsManager->getSVMAlloc(baseAddress + size - 1);
295 
296     // Add the allocation that matches the beginning address
297     if (beginAllocData) {
298         allocDataArray.push_back(beginAllocData);
299     }
300     // Add the allocation that matches the end address range if there was no beginning allocation
301     // or the beginning allocation does not match the ending allocation
302     if (endAllocData) {
303         if ((beginAllocData && (beginAllocData->gpuAllocations.getDefaultGraphicsAllocation() != endAllocData->gpuAllocations.getDefaultGraphicsAllocation())) ||
304             !beginAllocData) {
305             allocDataArray.push_back(endAllocData);
306         }
307     }
308 
309     // Return true if the whole range requested is covered by the same allocation
310     if (beginAllocData && endAllocData &&
311         (beginAllocData->gpuAllocations.getDefaultGraphicsAllocation() == endAllocData->gpuAllocations.getDefaultGraphicsAllocation())) {
312         *allocationRangeCovered = true;
313     } else {
314         *allocationRangeCovered = false;
315     }
316     return allocDataArray;
317 }
318 
createHostPointerManager()319 void DriverHandleImp::createHostPointerManager() {
320     hostPointerManager = std::make_unique<HostPointerManager>(getMemoryManager());
321 }
322 
importExternalPointer(void * ptr,size_t size)323 ze_result_t DriverHandleImp::importExternalPointer(void *ptr, size_t size) {
324     if (hostPointerManager.get() != nullptr) {
325         auto ret = hostPointerManager->createHostPointerMultiAllocation(this->devices,
326                                                                         ptr,
327                                                                         size);
328         return ret;
329     }
330 
331     return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
332 }
333 
releaseImportedPointer(void * ptr)334 ze_result_t DriverHandleImp::releaseImportedPointer(void *ptr) {
335     if (hostPointerManager.get() != nullptr) {
336         bool ret = hostPointerManager->freeHostPointerAllocation(ptr);
337         return ret ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_INVALID_ARGUMENT;
338     }
339     return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
340 }
341 
getHostPointerBaseAddress(void * ptr,void ** baseAddress)342 ze_result_t DriverHandleImp::getHostPointerBaseAddress(void *ptr, void **baseAddress) {
343     if (hostPointerManager.get() != nullptr) {
344         auto hostPointerData = hostPointerManager->getHostPointerAllocation(ptr);
345         if (hostPointerData != nullptr) {
346             if (baseAddress != nullptr) {
347                 *baseAddress = hostPointerData->basePtr;
348             }
349             return ZE_RESULT_SUCCESS;
350         }
351         return ZE_RESULT_ERROR_INVALID_ARGUMENT;
352     }
353     return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
354 }
355 
findHostPointerAllocation(void * ptr,size_t size,uint32_t rootDeviceIndex)356 NEO::GraphicsAllocation *DriverHandleImp::findHostPointerAllocation(void *ptr, size_t size, uint32_t rootDeviceIndex) {
357     if (hostPointerManager.get() != nullptr) {
358         HostPointerData *hostData = hostPointerManager->getHostPointerAllocation(ptr);
359         if (hostData != nullptr) {
360             size_t foundEndSize = reinterpret_cast<size_t>(hostData->basePtr) + hostData->size;
361             size_t inputEndSize = reinterpret_cast<size_t>(ptr) + size;
362             if (foundEndSize >= inputEndSize) {
363                 return hostData->hostPtrAllocations.getGraphicsAllocation(rootDeviceIndex);
364             }
365             return nullptr;
366         }
367 
368         if (NEO::DebugManager.flags.ForceHostPointerImport.get() == 1) {
369             importExternalPointer(ptr, size);
370             return hostPointerManager->getHostPointerAllocation(ptr)->hostPtrAllocations.getGraphicsAllocation(rootDeviceIndex);
371         }
372         return nullptr;
373     }
374 
375     return nullptr;
376 }
377 
getDriverSystemMemoryAllocation(void * ptr,size_t size,uint32_t rootDeviceIndex,uintptr_t * gpuAddress)378 NEO::GraphicsAllocation *DriverHandleImp::getDriverSystemMemoryAllocation(void *ptr,
379                                                                           size_t size,
380                                                                           uint32_t rootDeviceIndex,
381                                                                           uintptr_t *gpuAddress) {
382     NEO::SvmAllocationData *allocData = nullptr;
383     bool allocFound = findAllocationDataForRange(ptr, size, &allocData);
384     if (allocFound) {
385         if (gpuAddress != nullptr) {
386             *gpuAddress = reinterpret_cast<uintptr_t>(ptr);
387         }
388         return allocData->gpuAllocations.getGraphicsAllocation(rootDeviceIndex);
389     }
390     auto allocation = findHostPointerAllocation(ptr, size, rootDeviceIndex);
391     if (allocation != nullptr) {
392         if (gpuAddress != nullptr) {
393             uintptr_t offset = reinterpret_cast<uintptr_t>(ptr) -
394                                reinterpret_cast<uintptr_t>(allocation->getUnderlyingBuffer());
395             *gpuAddress = static_cast<uintptr_t>(allocation->getGpuAddress()) + offset;
396         }
397     }
398     return allocation;
399 }
400 
isRemoteResourceNeeded(void * ptr,NEO::GraphicsAllocation * alloc,NEO::SvmAllocationData * allocData,Device * device)401 bool DriverHandleImp::isRemoteResourceNeeded(void *ptr, NEO::GraphicsAllocation *alloc, NEO::SvmAllocationData *allocData, Device *device) {
402     return (alloc == nullptr || (allocData && ((allocData->gpuAllocations.getGraphicsAllocations().size() - 1) < device->getRootDeviceIndex())));
403 }
404 
importFdHandle(ze_device_handle_t hDevice,ze_ipc_memory_flags_t flags,uint64_t handle,NEO::GraphicsAllocation ** pAlloc)405 void *DriverHandleImp::importFdHandle(ze_device_handle_t hDevice, ze_ipc_memory_flags_t flags, uint64_t handle, NEO::GraphicsAllocation **pAlloc) {
406     auto neoDevice = Device::fromHandle(hDevice)->getNEODevice();
407     NEO::osHandle osHandle = static_cast<NEO::osHandle>(handle);
408     NEO::AllocationProperties unifiedMemoryProperties{neoDevice->getRootDeviceIndex(),
409                                                       MemoryConstants::pageSize,
410                                                       NEO::GraphicsAllocation::AllocationType::BUFFER,
411                                                       neoDevice->getDeviceBitfield()};
412     unifiedMemoryProperties.subDevicesBitfield = neoDevice->getDeviceBitfield();
413     NEO::GraphicsAllocation *alloc =
414         this->getMemoryManager()->createGraphicsAllocationFromSharedHandle(osHandle,
415                                                                            unifiedMemoryProperties,
416                                                                            false,
417                                                                            false);
418     if (alloc == nullptr) {
419         return nullptr;
420     }
421 
422     NEO::SvmAllocationData allocData(neoDevice->getRootDeviceIndex());
423     allocData.gpuAllocations.addAllocation(alloc);
424     allocData.cpuAllocation = nullptr;
425     allocData.size = alloc->getUnderlyingBufferSize();
426     allocData.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY;
427     allocData.device = neoDevice;
428     if (flags & ZE_DEVICE_MEM_ALLOC_FLAG_BIAS_UNCACHED) {
429         allocData.allocationFlagsProperty.flags.locallyUncachedResource = 1;
430     }
431 
432     if (flags & ZE_IPC_MEMORY_FLAG_BIAS_UNCACHED) {
433         allocData.allocationFlagsProperty.flags.locallyUncachedResource = 1;
434     }
435 
436     this->getSvmAllocsManager()->insertSVMAlloc(allocData);
437 
438     if (pAlloc) {
439         *pAlloc = alloc;
440     }
441 
442     return reinterpret_cast<void *>(alloc->getGpuAddress());
443 }
444 
getPeerAllocation(Device * device,NEO::SvmAllocationData * allocData,void * basePtr,uintptr_t * peerGpuAddress)445 NEO::GraphicsAllocation *DriverHandleImp::getPeerAllocation(Device *device,
446                                                             NEO::SvmAllocationData *allocData,
447                                                             void *basePtr,
448                                                             uintptr_t *peerGpuAddress) {
449     DeviceImp *deviceImp = static_cast<DeviceImp *>(device);
450     NEO::GraphicsAllocation *alloc = nullptr;
451 
452     NEO::SvmAllocationData *peerAllocData = nullptr;
453     void *peerPtr = nullptr;
454 
455     std::unique_lock<NEO::SpinLock> lock(deviceImp->peerAllocationsMutex);
456 
457     auto iter = deviceImp->peerAllocations.allocations.find(basePtr);
458     if (iter != deviceImp->peerAllocations.allocations.end()) {
459         peerAllocData = &iter->second;
460         alloc = peerAllocData->gpuAllocations.getDefaultGraphicsAllocation();
461         UNRECOVERABLE_IF(alloc == nullptr);
462         peerPtr = reinterpret_cast<void *>(alloc->getGpuAddress());
463     } else {
464         alloc = allocData->gpuAllocations.getDefaultGraphicsAllocation();
465         UNRECOVERABLE_IF(alloc == nullptr);
466         uint64_t handle = alloc->peekInternalHandle(this->getMemoryManager());
467         ze_ipc_memory_flags_t flags = {};
468 
469         peerPtr = this->importFdHandle(device, flags, handle, &alloc);
470         if (peerPtr == nullptr) {
471             return nullptr;
472         }
473 
474         peerAllocData = this->getSvmAllocsManager()->getSVMAlloc(peerPtr);
475         deviceImp->peerAllocations.allocations.insert(std::make_pair(basePtr, *peerAllocData));
476     }
477 
478     if (peerGpuAddress) {
479         *peerGpuAddress = reinterpret_cast<uintptr_t>(peerPtr);
480     }
481 
482     return alloc;
483 }
484 
importNTHandle(ze_device_handle_t hDevice,void * handle)485 void *DriverHandleImp::importNTHandle(ze_device_handle_t hDevice, void *handle) {
486     auto neoDevice = Device::fromHandle(hDevice)->getNEODevice();
487 
488     auto alloc = this->getMemoryManager()->createGraphicsAllocationFromNTHandle(handle, neoDevice->getRootDeviceIndex(), NEO::GraphicsAllocation::AllocationType::SHARED_BUFFER);
489 
490     if (alloc == nullptr) {
491         return nullptr;
492     }
493 
494     NEO::SvmAllocationData allocData(neoDevice->getRootDeviceIndex());
495     allocData.gpuAllocations.addAllocation(alloc);
496     allocData.cpuAllocation = nullptr;
497     allocData.size = alloc->getUnderlyingBufferSize();
498     allocData.memoryType = InternalMemoryType::DEVICE_UNIFIED_MEMORY;
499     allocData.device = neoDevice;
500 
501     this->getSvmAllocsManager()->insertSVMAlloc(allocData);
502 
503     return reinterpret_cast<void *>(alloc->getGpuAddress());
504 }
505 
checkMemoryAccessFromDevice(Device * device,const void * ptr)506 ze_result_t DriverHandleImp::checkMemoryAccessFromDevice(Device *device, const void *ptr) {
507     auto allocation = svmAllocsManager->getSVMAlloc(ptr);
508     if (allocation == nullptr) {
509         return ZE_RESULT_ERROR_INVALID_ARGUMENT;
510     }
511 
512     if (allocation->memoryType == InternalMemoryType::HOST_UNIFIED_MEMORY ||
513         allocation->memoryType == InternalMemoryType::SHARED_UNIFIED_MEMORY)
514         return ZE_RESULT_SUCCESS;
515 
516     if (allocation->gpuAllocations.getGraphicsAllocation(device->getRootDeviceIndex()) != nullptr) {
517         return ZE_RESULT_SUCCESS;
518     }
519 
520     return ZE_RESULT_ERROR_INVALID_ARGUMENT;
521 }
522 
523 } // namespace L0
524