@@ -229,8 +229,30 @@ ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
229229}
230230
231231ze_result_t ContextImp::freeMem (const void *ptr) {
232- DEBUG_BREAK_IF (nullptr == this ->driverHandle );
233- return this ->driverHandle ->freeMem (ptr);
232+ auto allocation = this ->driverHandle ->svmAllocsManager ->getSVMAlloc (ptr);
233+ if (allocation == nullptr ) {
234+ return ZE_RESULT_ERROR_INVALID_ARGUMENT;
235+ }
236+
237+ for (auto pairDevice : this ->devices ) {
238+ DeviceImp *deviceImp = static_cast <DeviceImp *>(pairDevice.second );
239+
240+ std::unique_lock<NEO::SpinLock> lock (deviceImp->peerAllocationsMutex );
241+
242+ auto iter = deviceImp->peerAllocations .allocations .find (ptr);
243+ if (iter != deviceImp->peerAllocations .allocations .end ()) {
244+ auto peerAllocData = &iter->second ;
245+ auto peerAlloc = peerAllocData->gpuAllocations .getDefaultGraphicsAllocation ();
246+ auto peerPtr = reinterpret_cast <void *>(peerAlloc->getGpuAddress ());
247+ this ->driverHandle ->svmAllocsManager ->freeSVMAlloc (peerPtr);
248+ }
249+ }
250+
251+ this ->driverHandle ->svmAllocsManager ->freeSVMAlloc (const_cast <void *>(ptr));
252+ if (this ->driverHandle ->svmAllocsManager ->getSvmMapOperation (ptr)) {
253+ this ->driverHandle ->svmAllocsManager ->removeSvmMapOperation (ptr);
254+ }
255+ return ZE_RESULT_SUCCESS;
234256}
235257
236258ze_result_t ContextImp::makeMemoryResident (ze_device_handle_t hDevice, void *ptr, size_t size) {
@@ -304,42 +326,102 @@ ze_result_t ContextImp::evictImage(ze_device_handle_t hDevice, ze_image_handle_t
304326ze_result_t ContextImp::getMemAddressRange (const void *ptr,
305327 void **pBase,
306328 size_t *pSize) {
307- DEBUG_BREAK_IF (nullptr == this ->driverHandle );
308- return this ->driverHandle ->getMemAddressRange (ptr,
309- pBase,
310- pSize);
329+ NEO::SvmAllocationData *allocData = this ->driverHandle ->svmAllocsManager ->getSVMAlloc (ptr);
330+ if (allocData) {
331+ NEO::GraphicsAllocation *alloc;
332+ alloc = allocData->gpuAllocations .getDefaultGraphicsAllocation ();
333+ if (pBase) {
334+ uint64_t *allocBase = reinterpret_cast <uint64_t *>(pBase);
335+ *allocBase = alloc->getGpuAddress ();
336+ }
337+
338+ if (pSize) {
339+ *pSize = alloc->getUnderlyingBufferSize ();
340+ }
341+
342+ return ZE_RESULT_SUCCESS;
343+ }
344+ DEBUG_BREAK_IF (true );
345+ return ZE_RESULT_ERROR_UNKNOWN;
311346}
312347
313348ze_result_t ContextImp::closeIpcMemHandle (const void *ptr) {
314- DEBUG_BREAK_IF (nullptr == this ->driverHandle );
315- return this ->driverHandle ->closeIpcMemHandle (ptr);
349+ return this ->freeMem (ptr);
316350}
317351
318352ze_result_t ContextImp::getIpcMemHandle (const void *ptr,
319353 ze_ipc_mem_handle_t *pIpcHandle) {
320- DEBUG_BREAK_IF (nullptr == this ->driverHandle );
321- return this ->driverHandle ->getIpcMemHandle (ptr,
322- pIpcHandle);
354+ NEO::SvmAllocationData *allocData = this ->driverHandle ->svmAllocsManager ->getSVMAlloc (ptr);
355+ if (allocData) {
356+ uint64_t handle = allocData->gpuAllocations .getDefaultGraphicsAllocation ()->peekInternalHandle (this ->driverHandle ->getMemoryManager ());
357+ memcpy_s (reinterpret_cast <void *>(pIpcHandle->data ),
358+ sizeof (ze_ipc_mem_handle_t ),
359+ &handle,
360+ sizeof (handle));
361+
362+ return ZE_RESULT_SUCCESS;
363+ }
364+ return ZE_RESULT_ERROR_INVALID_ARGUMENT;
323365}
324366
325367ze_result_t ContextImp::openIpcMemHandle (ze_device_handle_t hDevice,
326- ze_ipc_mem_handle_t handle ,
368+ ze_ipc_mem_handle_t pIpcHandle ,
327369 ze_ipc_memory_flags_t flags,
328370 void **ptr) {
329- DEBUG_BREAK_IF (nullptr == this ->driverHandle );
330- return this ->driverHandle ->openIpcMemHandle (hDevice,
331- handle,
332- flags,
333- ptr);
371+ uint64_t handle = 0u ;
372+ memcpy_s (&handle,
373+ sizeof (handle),
374+ reinterpret_cast <void *>(pIpcHandle.data ),
375+ sizeof (handle));
376+
377+ *ptr = this ->driverHandle ->importFdHandle (hDevice, flags, handle, nullptr );
378+ if (nullptr == *ptr) {
379+ return ZE_RESULT_ERROR_INVALID_ARGUMENT;
380+ }
381+
382+ return ZE_RESULT_SUCCESS;
334383}
335384
336385ze_result_t ContextImp::getMemAllocProperties (const void *ptr,
337386 ze_memory_allocation_properties_t *pMemAllocProperties,
338387 ze_device_handle_t *phDevice) {
339- DEBUG_BREAK_IF (nullptr == this ->driverHandle );
340- return this ->driverHandle ->getMemAllocProperties (ptr,
341- pMemAllocProperties,
342- phDevice);
388+ auto alloc = driverHandle->svmAllocsManager ->getSVMAlloc (ptr);
389+ if (nullptr == alloc) {
390+ pMemAllocProperties->type = ZE_MEMORY_TYPE_UNKNOWN;
391+ return ZE_RESULT_SUCCESS;
392+ }
393+
394+ pMemAllocProperties->type = Context::parseUSMType (alloc->memoryType );
395+ pMemAllocProperties->id = alloc->gpuAllocations .getDefaultGraphicsAllocation ()->getGpuAddress ();
396+
397+ if (phDevice != nullptr ) {
398+ if (alloc->device == nullptr ) {
399+ *phDevice = nullptr ;
400+ } else {
401+ auto device = static_cast <NEO::Device *>(alloc->device )->getSpecializedDevice <DeviceImp>();
402+ DEBUG_BREAK_IF (device == nullptr );
403+ *phDevice = device->toHandle ();
404+ }
405+ }
406+
407+ if (pMemAllocProperties->pNext ) {
408+ ze_base_properties_t *extendedProperties =
409+ reinterpret_cast <ze_base_properties_t *>(pMemAllocProperties->pNext );
410+ if (extendedProperties->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_FD) {
411+ ze_external_memory_export_fd_t *extendedMemoryExportProperties =
412+ reinterpret_cast <ze_external_memory_export_fd_t *>(extendedProperties);
413+ if (extendedMemoryExportProperties->flags & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_FD) {
414+ return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
415+ }
416+ if (pMemAllocProperties->type != ZE_MEMORY_TYPE_DEVICE) {
417+ return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
418+ }
419+ uint64_t handle = alloc->gpuAllocations .getDefaultGraphicsAllocation ()->peekInternalHandle (this ->driverHandle ->getMemoryManager ());
420+ extendedMemoryExportProperties->fd = static_cast <int >(handle);
421+ }
422+ }
423+
424+ return ZE_RESULT_SUCCESS;
343425}
344426
345427ze_result_t ContextImp::createModule (ze_device_handle_t hDevice,
0 commit comments