1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
|
/*
* Copyright (C) 2020-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "level_zero/core/source/context/context_imp.h"
#include "shared/source/command_container/implicit_scaling.h"
#include "shared/source/memory_manager/memory_operations_handler.h"
#include "shared/source/memory_manager/unified_memory_manager.h"
#include "level_zero/api/driver_experimental/public/zex_memory.h"
#include "level_zero/core/source/cmdlist/cmdlist.h"
#include "level_zero/core/source/device/device_imp.h"
#include "level_zero/core/source/driver/driver_handle_imp.h"
#include "level_zero/core/source/event/event.h"
#include "level_zero/core/source/helpers/properties_parser.h"
#include "level_zero/core/source/hw_helpers/l0_hw_helper.h"
#include "level_zero/core/source/image/image.h"
#include "level_zero/core/source/memory/memory_operations_helper.h"
#include "level_zero/core/source/module/module.h"
namespace L0 {
ze_result_t ContextImp::destroy() {
delete this;
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::getStatus() {
DriverHandleImp *driverHandleImp = static_cast<DriverHandleImp *>(this->driverHandle);
for (auto device : driverHandleImp->devices) {
DeviceImp *deviceImp = static_cast<DeviceImp *>(device);
if (deviceImp->resourcesReleased) {
return ZE_RESULT_ERROR_DEVICE_LOST;
}
}
return ZE_RESULT_SUCCESS;
}
DriverHandle *ContextImp::getDriverHandle() {
return this->driverHandle;
}
ContextImp::ContextImp(DriverHandle *driverHandle) {
this->driverHandle = static_cast<DriverHandleImp *>(driverHandle);
}
ze_result_t ContextImp::allocHostMem(const ze_host_mem_alloc_desc_t *hostDesc,
size_t size,
size_t alignment,
void **ptr) {
if (NEO::DebugManager.flags.ForceExtendedUSMBufferSize.get() >= 1) {
size += (MemoryConstants::pageSize * NEO::DebugManager.flags.ForceExtendedUSMBufferSize.get());
}
bool relaxedSizeAllowed = NEO::DebugManager.flags.AllowUnrestrictedSize.get();
if (hostDesc->pNext) {
const ze_base_desc_t *extendedDesc = reinterpret_cast<const ze_base_desc_t *>(hostDesc->pNext);
if (extendedDesc->stype == ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC) {
const ze_relaxed_allocation_limits_exp_desc_t *relaxedLimitsDesc =
reinterpret_cast<const ze_relaxed_allocation_limits_exp_desc_t *>(extendedDesc);
if (!(relaxedLimitsDesc->flags & ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE)) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
relaxedSizeAllowed = true;
}
}
if (relaxedSizeAllowed == false &&
(size > this->driverHandle->devices[0]->getNEODevice()->getDeviceInfo().maxMemAllocSize)) {
*ptr = nullptr;
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
}
NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::HOST_UNIFIED_MEMORY,
this->rootDeviceIndices,
this->deviceBitfields);
if (hostDesc->flags & ZE_HOST_MEM_ALLOC_FLAG_BIAS_UNCACHED) {
unifiedMemoryProperties.allocationFlags.flags.locallyUncachedResource = 1;
}
if (hostDesc->flags & ZEX_HOST_MEM_ALLOC_FLAG_USE_HOST_PTR) {
unifiedMemoryProperties.allocationFlags.hostptr = reinterpret_cast<uintptr_t>(*ptr);
}
auto usmPtr = this->driverHandle->svmAllocsManager->createHostUnifiedMemoryAllocation(size,
unifiedMemoryProperties);
if (usmPtr == nullptr) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
*ptr = usmPtr;
return ZE_RESULT_SUCCESS;
}
bool ContextImp::isDeviceDefinedForThisContext(Device *inDevice) {
uint32_t deviceIndex = inDevice->getRootDeviceIndex();
return (this->getDevices().find(deviceIndex) != this->getDevices().end());
}
ze_result_t ContextImp::allocDeviceMem(ze_device_handle_t hDevice,
const ze_device_mem_alloc_desc_t *deviceDesc,
size_t size,
size_t alignment, void **ptr) {
if (NEO::DebugManager.flags.ForceExtendedUSMBufferSize.get() >= 1) {
size += (MemoryConstants::pageSize * NEO::DebugManager.flags.ForceExtendedUSMBufferSize.get());
}
auto device = Device::fromHandle(hDevice);
if (isDeviceDefinedForThisContext(device) == false) {
return ZE_RESULT_ERROR_DEVICE_LOST;
}
StructuresLookupTable lookupTable = {};
lookupTable.relaxedSizeAllowed = NEO::DebugManager.flags.AllowUnrestrictedSize.get();
auto parseResult = prepareL0StructuresLookupTable(lookupTable, deviceDesc->pNext);
if (parseResult != ZE_RESULT_SUCCESS) {
return parseResult;
}
auto neoDevice = device->getNEODevice();
auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
auto deviceBitfields = this->driverHandle->deviceBitfields;
deviceBitfields[rootDeviceIndex] = neoDevice->getDeviceBitfield();
if (lookupTable.isSharedHandle) {
if (lookupTable.sharedHandleType.isDMABUFHandle) {
ze_ipc_memory_flags_t flags = {};
*ptr = getMemHandlePtr(hDevice, lookupTable.sharedHandleType.fd, flags);
if (nullptr == *ptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
} else {
UNRECOVERABLE_IF(!lookupTable.sharedHandleType.isNTHandle);
*ptr = this->driverHandle->importNTHandle(hDevice, lookupTable.sharedHandleType.ntHnadle);
if (*ptr == nullptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
}
return ZE_RESULT_SUCCESS;
}
if (lookupTable.relaxedSizeAllowed == false &&
(size > neoDevice->getDeviceInfo().maxMemAllocSize)) {
*ptr = nullptr;
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
}
uint64_t globalMemSize = neoDevice->getDeviceInfo().globalMemSize;
uint32_t numSubDevices = neoDevice->getNumGenericSubDevices();
if ((!device->isImplicitScalingCapable()) && (numSubDevices > 1)) {
globalMemSize = globalMemSize / numSubDevices;
}
if (lookupTable.relaxedSizeAllowed && (size > globalMemSize)) {
*ptr = nullptr;
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
}
deviceBitfields[rootDeviceIndex] = neoDevice->getDeviceBitfield();
NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::DEVICE_UNIFIED_MEMORY, this->driverHandle->rootDeviceIndices, deviceBitfields);
unifiedMemoryProperties.allocationFlags.flags.shareable = isShareableMemory(deviceDesc->pNext, static_cast<uint32_t>(lookupTable.exportMemory), neoDevice);
unifiedMemoryProperties.device = neoDevice;
unifiedMemoryProperties.allocationFlags.flags.compressedHint = isAllocationSuitableForCompression(lookupTable, *device, size);
if (deviceDesc->flags & ZE_DEVICE_MEM_ALLOC_FLAG_BIAS_UNCACHED) {
unifiedMemoryProperties.allocationFlags.flags.locallyUncachedResource = 1;
}
if (lookupTable.rayTracingMemory == true) {
unifiedMemoryProperties.allocationFlags.flags.resource48Bit = 1;
}
void *usmPtr =
this->driverHandle->svmAllocsManager->createUnifiedMemoryAllocation(size, unifiedMemoryProperties);
if (usmPtr == nullptr) {
return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;
}
*ptr = usmPtr;
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::allocSharedMem(ze_device_handle_t hDevice,
const ze_device_mem_alloc_desc_t *deviceDesc,
const ze_host_mem_alloc_desc_t *hostDesc,
size_t size,
size_t alignment,
void **ptr) {
if (NEO::DebugManager.flags.ForceExtendedUSMBufferSize.get() >= 1) {
size += (MemoryConstants::pageSize * NEO::DebugManager.flags.ForceExtendedUSMBufferSize.get());
}
auto device = Device::fromHandle(this->devices.begin()->second);
if (hDevice != nullptr) {
device = Device::fromHandle(hDevice);
}
auto neoDevice = device->getNEODevice();
bool relaxedSizeAllowed = NEO::DebugManager.flags.AllowUnrestrictedSize.get();
bool rayTracingAllocation = false;
if (deviceDesc->pNext) {
const ze_base_desc_t *extendedDesc = reinterpret_cast<const ze_base_desc_t *>(deviceDesc->pNext);
if (extendedDesc->stype == ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC) {
const ze_relaxed_allocation_limits_exp_desc_t *relaxedLimitsDesc =
reinterpret_cast<const ze_relaxed_allocation_limits_exp_desc_t *>(extendedDesc);
if (!(relaxedLimitsDesc->flags & ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE)) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
relaxedSizeAllowed = true;
} else if (extendedDesc->stype == ZE_STRUCTURE_TYPE_RAYTRACING_MEM_ALLOC_EXT_DESC) {
rayTracingAllocation = true;
}
}
if (relaxedSizeAllowed == false &&
(size > neoDevice->getDeviceInfo().maxMemAllocSize)) {
*ptr = nullptr;
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
}
uint64_t globalMemSize = neoDevice->getDeviceInfo().globalMemSize;
uint32_t numSubDevices = neoDevice->getNumGenericSubDevices();
if ((!device->isImplicitScalingCapable()) && (numSubDevices > 1)) {
globalMemSize = globalMemSize / numSubDevices;
}
if (relaxedSizeAllowed &&
(size > globalMemSize)) {
*ptr = nullptr;
return ZE_RESULT_ERROR_UNSUPPORTED_SIZE;
}
auto deviceBitfields = this->deviceBitfields;
NEO::Device *unifiedMemoryPropertiesDevice = nullptr;
if (hDevice) {
device = Device::fromHandle(hDevice);
if (isDeviceDefinedForThisContext(device) == false) {
return ZE_RESULT_ERROR_DEVICE_LOST;
}
neoDevice = device->getNEODevice();
auto rootDeviceIndex = neoDevice->getRootDeviceIndex();
unifiedMemoryPropertiesDevice = neoDevice;
deviceBitfields[rootDeviceIndex] = neoDevice->getDeviceBitfield();
}
NEO::SVMAllocsManager::UnifiedMemoryProperties unifiedMemoryProperties(InternalMemoryType::SHARED_UNIFIED_MEMORY,
this->rootDeviceIndices,
deviceBitfields);
unifiedMemoryProperties.device = unifiedMemoryPropertiesDevice;
if (deviceDesc->flags & ZE_DEVICE_MEM_ALLOC_FLAG_BIAS_UNCACHED) {
unifiedMemoryProperties.allocationFlags.flags.locallyUncachedResource = 1;
}
if (deviceDesc->flags & ZE_DEVICE_MEM_ALLOC_FLAG_BIAS_INITIAL_PLACEMENT) {
unifiedMemoryProperties.allocationFlags.allocFlags.usmInitialPlacementGpu = 1;
}
if (hostDesc->flags & ZE_HOST_MEM_ALLOC_FLAG_BIAS_INITIAL_PLACEMENT) {
unifiedMemoryProperties.allocationFlags.allocFlags.usmInitialPlacementCpu = 1;
}
if (rayTracingAllocation) {
unifiedMemoryProperties.allocationFlags.flags.resource48Bit = 1;
}
void *usmPtr = nullptr;
if (hostDesc->flags & ZEX_HOST_MEM_ALLOC_FLAG_USE_HOST_PTR) {
unifiedMemoryProperties.allocationFlags.hostptr = reinterpret_cast<uintptr_t>(*ptr);
usmPtr = this->driverHandle->svmAllocsManager->createHostUnifiedMemoryAllocation(size,
unifiedMemoryProperties);
} else {
usmPtr =
this->driverHandle->svmAllocsManager->createSharedUnifiedMemoryAllocation(size,
unifiedMemoryProperties,
static_cast<void *>(neoDevice->getSpecializedDevice<L0::Device>()));
}
if (usmPtr == nullptr) {
return ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY;
}
*ptr = usmPtr;
return ZE_RESULT_SUCCESS;
}
void ContextImp::freePeerAllocations(const void *ptr, bool blocking, Device *device) {
DeviceImp *deviceImp = static_cast<DeviceImp *>(device);
std::unique_lock<NEO::SpinLock> lock(deviceImp->peerAllocationsMutex);
auto iter = deviceImp->peerAllocations.allocations.find(ptr);
if (iter != deviceImp->peerAllocations.allocations.end()) {
auto peerAllocData = &iter->second;
auto peerAlloc = peerAllocData->gpuAllocations.getDefaultGraphicsAllocation();
auto peerPtr = reinterpret_cast<void *>(peerAlloc->getGpuAddress());
this->driverHandle->svmAllocsManager->freeSVMAlloc(peerPtr, blocking);
deviceImp->peerAllocations.allocations.erase(iter);
}
for (auto subDevice : deviceImp->subDevices) {
this->freePeerAllocations(ptr, blocking, subDevice);
}
}
ze_result_t ContextImp::freeMem(const void *ptr) {
return this->freeMem(ptr, false);
}
ze_result_t ContextImp::freeMem(const void *ptr, bool blocking) {
auto allocation = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
if (allocation == nullptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
for (auto pairDevice : this->devices) {
this->freePeerAllocations(ptr, blocking, Device::fromHandle(pairDevice.second));
}
this->driverHandle->svmAllocsManager->freeSVMAlloc(const_cast<void *>(ptr), blocking);
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::freeMemExt(const ze_memory_free_ext_desc_t *pMemFreeDesc,
void *ptr) {
if (pMemFreeDesc->freePolicy == ZE_DRIVER_MEMORY_FREE_POLICY_EXT_FLAG_BLOCKING_FREE) {
return this->freeMem(ptr, true);
}
if (pMemFreeDesc->freePolicy == ZE_DRIVER_MEMORY_FREE_POLICY_EXT_FLAG_DEFER_FREE) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
return this->freeMem(ptr, false);
}
ze_result_t ContextImp::makeMemoryResident(ze_device_handle_t hDevice, void *ptr, size_t size) {
Device *device = L0::Device::fromHandle(hDevice);
NEO::Device *neoDevice = device->getNEODevice();
auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(
ptr,
size,
neoDevice->getRootDeviceIndex(),
nullptr);
if (allocation == nullptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
NEO::MemoryOperationsHandler *memoryOperationsIface = neoDevice->getRootDeviceEnvironment().memoryOperationsInterface.get();
auto success = memoryOperationsIface->makeResident(neoDevice, ArrayRef<NEO::GraphicsAllocation *>(&allocation, 1));
ze_result_t res = changeMemoryOperationStatusToL0ResultType(success);
if (ZE_RESULT_SUCCESS == res) {
auto allocData = device->getDriverHandle()->getSvmAllocsManager()->getSVMAlloc(ptr);
if (allocData && allocData->memoryType == InternalMemoryType::SHARED_UNIFIED_MEMORY) {
DriverHandleImp *driverHandleImp = static_cast<DriverHandleImp *>(device->getDriverHandle());
std::lock_guard<std::mutex> lock(driverHandleImp->sharedMakeResidentAllocationsLock);
driverHandleImp->sharedMakeResidentAllocations.insert({ptr, allocation});
}
}
return res;
}
ze_result_t ContextImp::evictMemory(ze_device_handle_t hDevice, void *ptr, size_t size) {
Device *device = L0::Device::fromHandle(hDevice);
NEO::Device *neoDevice = device->getNEODevice();
auto allocation = device->getDriverHandle()->getDriverSystemMemoryAllocation(
ptr,
size,
neoDevice->getRootDeviceIndex(),
nullptr);
if (allocation == nullptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
{
DriverHandleImp *driverHandleImp = static_cast<DriverHandleImp *>(device->getDriverHandle());
std::lock_guard<std::mutex> lock(driverHandleImp->sharedMakeResidentAllocationsLock);
driverHandleImp->sharedMakeResidentAllocations.erase(ptr);
}
NEO::MemoryOperationsHandler *memoryOperationsIface = neoDevice->getRootDeviceEnvironment().memoryOperationsInterface.get();
auto success = memoryOperationsIface->evict(neoDevice, *allocation);
return changeMemoryOperationStatusToL0ResultType(success);
}
ze_result_t ContextImp::makeImageResident(ze_device_handle_t hDevice, ze_image_handle_t hImage) {
auto alloc = Image::fromHandle(hImage)->getAllocation();
NEO::Device *neoDevice = L0::Device::fromHandle(hDevice)->getNEODevice();
NEO::MemoryOperationsHandler *memoryOperationsIface = neoDevice->getRootDeviceEnvironment().memoryOperationsInterface.get();
auto success = memoryOperationsIface->makeResident(neoDevice, ArrayRef<NEO::GraphicsAllocation *>(&alloc, 1));
return changeMemoryOperationStatusToL0ResultType(success);
}
ze_result_t ContextImp::evictImage(ze_device_handle_t hDevice, ze_image_handle_t hImage) {
auto alloc = Image::fromHandle(hImage)->getAllocation();
NEO::Device *neoDevice = L0::Device::fromHandle(hDevice)->getNEODevice();
NEO::MemoryOperationsHandler *memoryOperationsIface = neoDevice->getRootDeviceEnvironment().memoryOperationsInterface.get();
auto success = memoryOperationsIface->evict(neoDevice, *alloc);
return changeMemoryOperationStatusToL0ResultType(success);
}
ze_result_t ContextImp::getMemAddressRange(const void *ptr,
void **pBase,
size_t *pSize) {
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
if (allocData) {
NEO::GraphicsAllocation *alloc;
alloc = allocData->gpuAllocations.getDefaultGraphicsAllocation();
if (pBase) {
uint64_t *allocBase = reinterpret_cast<uint64_t *>(pBase);
*allocBase = alloc->getGpuAddress();
}
if (pSize) {
*pSize = allocData->size;
}
return ZE_RESULT_SUCCESS;
}
return ZE_RESULT_ERROR_UNKNOWN;
}
ze_result_t ContextImp::closeIpcMemHandle(const void *ptr) {
return this->freeMem(ptr);
}
ze_result_t ContextImp::getIpcMemHandle(const void *ptr,
ze_ipc_mem_handle_t *pIpcHandle) {
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
if (allocData) {
uint64_t handle = 0;
int ret = allocData->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->driverHandle->getMemoryManager(), handle);
if (ret < 0) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
memcpy_s(reinterpret_cast<void *>(pIpcHandle->data),
sizeof(ze_ipc_mem_handle_t),
&handle,
sizeof(handle));
return ZE_RESULT_SUCCESS;
}
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
ze_result_t ContextImp::getIpcMemHandles(const void *ptr,
uint32_t *numIpcHandles,
ze_ipc_mem_handle_t *pIpcHandles) {
NEO::SvmAllocationData *allocData = this->driverHandle->svmAllocsManager->getSVMAlloc(ptr);
if (allocData) {
auto alloc = allocData->gpuAllocations.getDefaultGraphicsAllocation();
uint32_t numHandles = alloc->getNumHandles();
UNRECOVERABLE_IF(numIpcHandles == nullptr);
if (*numIpcHandles == 0 || *numIpcHandles > numHandles) {
*numIpcHandles = numHandles;
}
if (pIpcHandles == nullptr) {
return ZE_RESULT_SUCCESS;
}
for (uint32_t i = 0; i < *numIpcHandles; i++) {
uint64_t handle = 0;
int ret = allocData->gpuAllocations.getDefaultGraphicsAllocation()->peekInternalHandle(this->driverHandle->getMemoryManager(), i, handle);
if (ret < 0) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
memcpy_s(reinterpret_cast<void *>(pIpcHandles[i].data),
sizeof(ze_ipc_mem_handle_t),
&handle,
sizeof(int));
}
return ZE_RESULT_SUCCESS;
}
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
ze_result_t ContextImp::openIpcMemHandle(ze_device_handle_t hDevice,
const ze_ipc_mem_handle_t &pIpcHandle,
ze_ipc_memory_flags_t flags,
void **ptr) {
uint64_t handle = 0u;
memcpy_s(&handle,
sizeof(handle),
pIpcHandle.data,
sizeof(handle));
*ptr = getMemHandlePtr(hDevice, handle, flags);
if (nullptr == *ptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::openIpcMemHandles(ze_device_handle_t hDevice,
uint32_t numIpcHandles,
ze_ipc_mem_handle_t *pIpcHandles,
ze_ipc_memory_flags_t flags,
void **pptr) {
std::vector<NEO::osHandle> handles;
handles.reserve(numIpcHandles);
for (uint32_t i = 0; i < numIpcHandles; i++) {
uint64_t handle = 0;
memcpy_s(&handle,
sizeof(handle),
reinterpret_cast<void *>(pIpcHandles[i].data),
sizeof(handle));
handles.push_back(static_cast<NEO::osHandle>(handle));
}
auto neoDevice = Device::fromHandle(hDevice)->getNEODevice()->getRootDevice();
*pptr = this->driverHandle->importFdHandles(neoDevice, flags, handles, nullptr);
if (nullptr == *pptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
return ZE_RESULT_SUCCESS;
}
ze_result_t EventPoolImp::closeIpcHandle() {
return this->destroy();
}
ze_result_t EventPoolImp::getIpcHandle(ze_ipc_event_pool_handle_t *pIpcHandle) {
// L0 uses a vector of ZE_MAX_IPC_HANDLE_SIZE bytes to send the IPC handle, i.e.
// char data[ZE_MAX_IPC_HANDLE_SIZE];
// First four bytes (which is of size sizeof(int)) of it contain the file descriptor
// associated with the dma-buf,
// Rest is payload to communicate extra info to the other processes.
// For the event pool, this contains:
// - the number of events the pool has.
// - the id for the device used during pool creation
if (!this->isShareableEventMemory) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
uint64_t handle = 0;
this->eventPoolAllocations->getDefaultGraphicsAllocation()->peekInternalHandle(this->context->getDriverHandle()->getMemoryManager(), handle);
memcpy_s(pIpcHandle->data, sizeof(int), &handle, sizeof(int));
memcpy_s(pIpcHandle->data + sizeof(int), sizeof(this->numEvents), &this->numEvents, sizeof(this->numEvents));
uint32_t rootDeviceIndex = this->getDevice()->getRootDeviceIndex();
memcpy_s(pIpcHandle->data + sizeof(int) + sizeof(this->numEvents),
sizeof(rootDeviceIndex), &rootDeviceIndex, sizeof(rootDeviceIndex));
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::openEventPoolIpcHandle(const ze_ipc_event_pool_handle_t &hIpc,
ze_event_pool_handle_t *phEventPool) {
uint64_t handle = 0u;
memcpy_s(&handle, sizeof(int), hIpc.data, sizeof(int));
size_t numEvents = 0;
memcpy_s(&numEvents, sizeof(numEvents), hIpc.data + sizeof(int), sizeof(numEvents));
uint32_t rootDeviceIndex = std::numeric_limits<uint32_t>::max();
memcpy_s(&rootDeviceIndex, sizeof(rootDeviceIndex),
hIpc.data + sizeof(int) + sizeof(numEvents), sizeof(rootDeviceIndex));
auto device = Device::fromHandle(this->devices.begin()->second);
auto neoDevice = device->getNEODevice();
NEO::osHandle osHandle = static_cast<NEO::osHandle>(handle);
auto &hwHelper = device->getHwHelper();
const uint32_t eventAlignment = static_cast<uint32_t>(hwHelper.getTimestampPacketAllocatorAlignment());
uint32_t eventSize = static_cast<uint32_t>(alignUp(EventPacketsCount::eventPackets * hwHelper.getSingleTimestampPacketSize(), eventAlignment));
size_t alignedSize = alignUp<size_t>(numEvents * eventSize, MemoryConstants::pageSize64k);
NEO::AllocationProperties unifiedMemoryProperties{rootDeviceIndex,
alignedSize,
NEO::AllocationType::BUFFER_HOST_MEMORY,
systemMemoryBitfield};
unifiedMemoryProperties.subDevicesBitfield = neoDevice->getDeviceBitfield();
auto memoryManager = this->getDriverHandle()->getMemoryManager();
NEO::GraphicsAllocation *alloc = memoryManager->createGraphicsAllocationFromSharedHandle(osHandle,
unifiedMemoryProperties,
false,
true,
false);
if (alloc == nullptr) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
ze_event_pool_desc_t desc = {};
auto eventPool = new EventPoolImp(&desc);
eventPool->context = this;
eventPool->eventPoolAllocations =
std::make_unique<NEO::MultiGraphicsAllocation>(static_cast<uint32_t>(this->rootDeviceIndices.size()));
eventPool->eventPoolAllocations->addAllocation(alloc);
eventPool->eventPoolPtr = reinterpret_cast<void *>(alloc->getUnderlyingBuffer());
eventPool->devices.push_back(device);
eventPool->isImportedIpcPool = true;
eventPool->setEventSize(eventSize);
eventPool->setEventAlignment(eventAlignment);
for (auto currDeviceIndex : this->rootDeviceIndices) {
if (currDeviceIndex == rootDeviceIndex) {
continue;
}
unifiedMemoryProperties.rootDeviceIndex = currDeviceIndex;
unifiedMemoryProperties.flags.isUSMHostAllocation = true;
unifiedMemoryProperties.flags.forceSystemMemory = true;
unifiedMemoryProperties.flags.allocateMemory = false;
auto graphicsAllocation = memoryManager->createGraphicsAllocationFromExistingStorage(unifiedMemoryProperties,
eventPool->eventPoolPtr,
eventPool->getAllocation());
if (!graphicsAllocation) {
for (auto gpuAllocation : eventPool->getAllocation().getGraphicsAllocations()) {
memoryManager->freeGraphicsMemory(gpuAllocation);
}
memoryManager->freeGraphicsMemory(alloc);
delete eventPool;
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
eventPool->eventPoolAllocations->addAllocation(graphicsAllocation);
}
*phEventPool = eventPool;
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::handleAllocationExtensions(NEO::GraphicsAllocation *alloc, ze_memory_type_t type, void *pNext, struct DriverHandleImp *driverHandle) {
if (pNext != nullptr) {
ze_base_properties_t *extendedProperties =
reinterpret_cast<ze_base_properties_t *>(pNext);
if (extendedProperties->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_FD) {
ze_external_memory_export_fd_t *extendedMemoryExportProperties =
reinterpret_cast<ze_external_memory_export_fd_t *>(extendedProperties);
if (extendedMemoryExportProperties->flags & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_FD) {
return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
}
if (type != ZE_MEMORY_TYPE_DEVICE) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
uint64_t handle = 0;
int ret = alloc->peekInternalHandle(driverHandle->getMemoryManager(), handle);
if (ret < 0) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
extendedMemoryExportProperties->fd = static_cast<int>(handle);
} else if (extendedProperties->stype == ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_EXPORT_WIN32) {
ze_external_memory_export_win32_handle_t *exportStructure = reinterpret_cast<ze_external_memory_export_win32_handle_t *>(extendedProperties);
if (exportStructure->flags != ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32) {
return ZE_RESULT_ERROR_UNSUPPORTED_ENUMERATION;
}
uint64_t handle = 0;
int ret = alloc->peekInternalHandle(driverHandle->getMemoryManager(), handle);
if (ret < 0) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
exportStructure->handle = reinterpret_cast<void *>(handle);
} else {
return ZE_RESULT_ERROR_INVALID_ENUMERATION;
}
}
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::getMemAllocProperties(const void *ptr,
ze_memory_allocation_properties_t *pMemAllocProperties,
ze_device_handle_t *phDevice) {
auto alloc = driverHandle->svmAllocsManager->getSVMAlloc(ptr);
if (nullptr == alloc) {
pMemAllocProperties->type = ZE_MEMORY_TYPE_UNKNOWN;
return ZE_RESULT_SUCCESS;
}
pMemAllocProperties->type = Context::parseUSMType(alloc->memoryType);
pMemAllocProperties->pageSize = alloc->pageSizeForAlignment;
pMemAllocProperties->id = alloc->getAllocId();
if (phDevice != nullptr) {
if (alloc->device == nullptr) {
*phDevice = nullptr;
} else {
auto device = static_cast<NEO::Device *>(alloc->device)->getSpecializedDevice<DeviceImp>();
DEBUG_BREAK_IF(device == nullptr);
*phDevice = device->toHandle();
}
}
return handleAllocationExtensions(alloc->gpuAllocations.getDefaultGraphicsAllocation(),
pMemAllocProperties->type,
pMemAllocProperties->pNext,
driverHandle);
}
ze_result_t ContextImp::getImageAllocProperties(Image *image, ze_image_allocation_ext_properties_t *pAllocProperties) {
NEO::GraphicsAllocation *alloc = image->getAllocation();
if (alloc == nullptr) {
return ZE_RESULT_ERROR_UNKNOWN;
}
pAllocProperties->id = 0;
return handleAllocationExtensions(alloc, ZE_MEMORY_TYPE_DEVICE, pAllocProperties->pNext, driverHandle);
}
ze_result_t ContextImp::createModule(ze_device_handle_t hDevice,
const ze_module_desc_t *desc,
ze_module_handle_t *phModule,
ze_module_build_log_handle_t *phBuildLog) {
return L0::Device::fromHandle(hDevice)->createModule(desc, phModule, phBuildLog, ModuleType::User);
}
ze_result_t ContextImp::createSampler(ze_device_handle_t hDevice,
const ze_sampler_desc_t *pDesc,
ze_sampler_handle_t *phSampler) {
return L0::Device::fromHandle(hDevice)->createSampler(pDesc, phSampler);
}
ze_result_t ContextImp::createCommandQueue(ze_device_handle_t hDevice,
const ze_command_queue_desc_t *desc,
ze_command_queue_handle_t *commandQueue) {
return L0::Device::fromHandle(hDevice)->createCommandQueue(desc, commandQueue);
}
ze_result_t ContextImp::createCommandList(ze_device_handle_t hDevice,
const ze_command_list_desc_t *desc,
ze_command_list_handle_t *commandList) {
auto ret = L0::Device::fromHandle(hDevice)->createCommandList(desc, commandList);
if (*commandList) {
L0::CommandList::fromHandle(*commandList)->hContext = this->toHandle();
}
return ret;
}
ze_result_t ContextImp::createCommandListImmediate(ze_device_handle_t hDevice,
const ze_command_queue_desc_t *desc,
ze_command_list_handle_t *commandList) {
auto ret = L0::Device::fromHandle(hDevice)->createCommandListImmediate(desc, commandList);
if (*commandList) {
L0::CommandList::fromHandle(*commandList)->hContext = this->toHandle();
}
return ret;
}
ze_result_t ContextImp::activateMetricGroups(zet_device_handle_t hDevice,
uint32_t count,
zet_metric_group_handle_t *phMetricGroups) {
return L0::Device::fromHandle(hDevice)->activateMetricGroupsDeferred(count, phMetricGroups);
}
ze_result_t ContextImp::reserveVirtualMem(const void *pStart,
size_t size,
void **pptr) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ContextImp::freeVirtualMem(const void *ptr,
size_t size) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ContextImp::queryVirtualMemPageSize(ze_device_handle_t hDevice,
size_t size,
size_t *pagesize) {
*pagesize = std::max(Math::prevPowerOfTwo(size), MemoryConstants::pageSize64k);
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::createPhysicalMem(ze_device_handle_t hDevice,
ze_physical_mem_desc_t *desc,
ze_physical_mem_handle_t *phPhysicalMemory) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ContextImp::destroyPhysicalMem(ze_physical_mem_handle_t hPhysicalMemory) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ContextImp::mapVirtualMem(const void *ptr,
size_t size,
ze_physical_mem_handle_t hPhysicalMemory,
size_t offset,
ze_memory_access_attribute_t access) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ContextImp::unMapVirtualMem(const void *ptr,
size_t size) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ContextImp::setVirtualMemAccessAttribute(const void *ptr,
size_t size,
ze_memory_access_attribute_t access) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ContextImp::getVirtualMemAccessAttribute(const void *ptr,
size_t size,
ze_memory_access_attribute_t *access,
size_t *outSize) {
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
}
ze_result_t ContextImp::createEventPool(const ze_event_pool_desc_t *desc,
uint32_t numDevices,
ze_device_handle_t *phDevices,
ze_event_pool_handle_t *phEventPool) {
ze_result_t result;
EventPool *eventPool = EventPool::create(this->driverHandle, this, numDevices, phDevices, desc, result);
if (eventPool == nullptr) {
return result;
}
*phEventPool = eventPool->toHandle();
return ZE_RESULT_SUCCESS;
}
ze_result_t ContextImp::createImage(ze_device_handle_t hDevice,
const ze_image_desc_t *desc,
ze_image_handle_t *phImage) {
return L0::Device::fromHandle(hDevice)->createImage(desc, phImage);
}
bool ContextImp::isAllocationSuitableForCompression(const StructuresLookupTable &structuresLookupTable, Device &device, size_t allocSize) {
auto &hwInfo = device.getHwInfo();
auto &hwHelper = device.getHwHelper();
auto &l0HwHelper = L0HwHelper::get(hwInfo.platform.eRenderCoreFamily);
if (!l0HwHelper.usmCompressionSupported(hwInfo) || !hwHelper.isBufferSizeSuitableForCompression(allocSize, hwInfo) || structuresLookupTable.uncompressedHint) {
return false;
}
if (l0HwHelper.forceDefaultUsmCompressionSupport()) {
return true;
}
return structuresLookupTable.compressedHint;
}
} // namespace L0
|