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
|
/*
* Copyright (C) 2020-2025 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "shared/source/helpers/non_copyable_or_moveable.h"
#include "shared/source/memory_manager/gfx_partition.h"
#include "shared/source/memory_manager/memory_manager.h"
#include "shared/source/os_interface/sys_calls_common.h"
#include "shared/source/utilities/stackvec.h"
#include "level_zero/core/source/context/context.h"
#include "level_zero/core/source/driver/driver_handle_imp.h"
#include <map>
namespace NEO {
class GraphicsAllocation;
enum class HeapIndex : uint32_t;
struct SvmAllocationData;
struct VirtualMemoryReservation;
} // namespace NEO
namespace L0 {
struct StructuresLookupTable;
struct DriverHandleImp;
struct Device;
struct IpcCounterBasedEventData;
class ContextExt;
struct DriverHandle;
ContextExt *createContextExt(DriverHandle *driverHandle);
void destroyContextExt(ContextExt *ctxExt);
struct ContextSettings {
bool enablePidfdOrSockets = true;
bool enableSvmHeapReservation = true;
};
struct ContextImp : Context, NEO::NonCopyableAndNonMovableClass {
ContextImp(DriverHandle *driverHandle);
~ContextImp() override;
ze_result_t destroy() override;
ze_result_t getStatus() override;
DriverHandle *getDriverHandle() override;
ze_result_t allocHostMem(const ze_host_mem_alloc_desc_t *hostDesc,
size_t size,
size_t alignment,
void **ptr) override;
ze_result_t allocDeviceMem(ze_device_handle_t hDevice,
const ze_device_mem_alloc_desc_t *deviceDesc,
size_t size,
size_t alignment, void **ptr) override;
ze_result_t 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) override;
ze_result_t freeMem(const void *ptr) override;
ze_result_t freeMem(const void *ptr, bool blocking) override;
ze_result_t freeMemExt(const ze_memory_free_ext_desc_t *pMemFreeDesc,
void *ptr) override;
ze_result_t registerMemoryFreeCallback(zex_memory_free_callback_ext_desc_t *pfnCallbackDesc, void *ptr) override;
ze_result_t makeMemoryResident(ze_device_handle_t hDevice,
void *ptr,
size_t size) override;
ze_result_t evictMemory(ze_device_handle_t hDevice,
void *ptr,
size_t size) override;
ze_result_t makeImageResident(ze_device_handle_t hDevice, ze_image_handle_t hImage) override;
ze_result_t evictImage(ze_device_handle_t hDevice, ze_image_handle_t hImage) override;
ze_result_t getMemAddressRange(const void *ptr,
void **pBase,
size_t *pSize) override;
ze_result_t closeIpcMemHandle(const void *ptr) override;
ze_result_t putIpcMemHandle(ze_ipc_mem_handle_t ipcHandle) override;
ze_result_t getIpcMemHandle(const void *ptr,
ze_ipc_mem_handle_t *pIpcHandle) override;
ze_result_t openIpcMemHandle(ze_device_handle_t hDevice,
const ze_ipc_mem_handle_t &handle,
ze_ipc_memory_flags_t flags,
void **ptr) override;
ze_result_t getIpcHandleFromFd(uint64_t handle, ze_ipc_mem_handle_t *pIpcHandle) override;
ze_result_t getFdFromIpcHandle(ze_ipc_mem_handle_t ipcHandle, uint64_t *pHandle) override;
ze_result_t lockMemory(ze_device_handle_t hDevice, void *ptr, size_t size) override;
ze_result_t
getIpcMemHandles(
const void *ptr,
uint32_t *numIpcHandles,
ze_ipc_mem_handle_t *pIpcHandles) override;
ze_result_t
openIpcMemHandles(
ze_device_handle_t hDevice,
uint32_t numIpcHandles,
ze_ipc_mem_handle_t *pIpcHandles,
ze_ipc_memory_flags_t flags,
void **pptr) override;
ze_result_t getMemAllocProperties(const void *ptr,
ze_memory_allocation_properties_t *pMemAllocProperties,
ze_device_handle_t *phDevice) override;
ze_result_t getImageAllocProperties(Image *image,
ze_image_allocation_ext_properties_t *pAllocProperties) override;
ze_result_t setAtomicAccessAttribute(ze_device_handle_t hDevice, const void *ptr, size_t size, ze_memory_atomic_attr_exp_flags_t attr) override;
ze_result_t getAtomicAccessAttribute(ze_device_handle_t hDevice, const void *ptr, size_t size, ze_memory_atomic_attr_exp_flags_t *pAttr) override;
ze_result_t createModule(ze_device_handle_t hDevice,
const ze_module_desc_t *desc,
ze_module_handle_t *phModule,
ze_module_build_log_handle_t *phBuildLog) override;
ze_result_t createSampler(ze_device_handle_t hDevice,
const ze_sampler_desc_t *pDesc,
ze_sampler_handle_t *phSampler) override;
ze_result_t createCommandQueue(ze_device_handle_t hDevice,
const ze_command_queue_desc_t *desc,
ze_command_queue_handle_t *commandQueue) override;
ze_result_t createCommandList(ze_device_handle_t hDevice,
const ze_command_list_desc_t *desc,
ze_command_list_handle_t *commandList) override;
ze_result_t createCommandListImmediate(ze_device_handle_t hDevice,
const ze_command_queue_desc_t *desc,
ze_command_list_handle_t *commandList) override;
ze_result_t activateMetricGroups(zet_device_handle_t hDevice,
uint32_t count,
zet_metric_group_handle_t *phMetricGroups) override;
ze_result_t reserveVirtualMem(const void *pStart,
size_t size,
void **pptr) override;
ze_result_t freeVirtualMem(const void *ptr,
size_t size) override;
ze_result_t queryVirtualMemPageSizeWithStartAddress(ze_device_handle_t hDevice,
const void *pStart,
size_t size,
size_t *pagesize) override;
ze_result_t queryVirtualMemPageSize(ze_device_handle_t hDevice,
size_t size,
size_t *pagesize) override;
ze_result_t createPhysicalMem(ze_device_handle_t hDevice,
ze_physical_mem_desc_t *desc,
ze_physical_mem_handle_t *phPhysicalMemory) override;
ze_result_t destroyPhysicalMem(ze_physical_mem_handle_t hPhysicalMemory) override;
ze_result_t mapVirtualMem(const void *ptr,
size_t size,
ze_physical_mem_handle_t hPhysicalMemory,
size_t offset,
ze_memory_access_attribute_t access) override;
ze_result_t unMapVirtualMem(const void *ptr,
size_t size) override;
ze_result_t setVirtualMemAccessAttribute(const void *ptr,
size_t size,
ze_memory_access_attribute_t access) override;
ze_result_t getVirtualMemAccessAttribute(const void *ptr,
size_t size,
ze_memory_access_attribute_t *access,
size_t *outSize) override;
ze_result_t openEventPoolIpcHandle(const ze_ipc_event_pool_handle_t &ipcEventPoolHandle,
ze_event_pool_handle_t *eventPoolHandle) override;
ze_result_t openCounterBasedIpcHandle(const IpcCounterBasedEventData &ipcData, ze_event_handle_t *phEvent);
ze_result_t createEventPool(const ze_event_pool_desc_t *desc,
uint32_t numDevices,
ze_device_handle_t *phDevices,
ze_event_pool_handle_t *phEventPool) override;
ze_result_t createImage(ze_device_handle_t hDevice,
const ze_image_desc_t *desc,
ze_image_handle_t *phImage) override;
ze_result_t getVirtualAddressSpaceIpcHandle(ze_device_handle_t hDevice,
ze_ipc_mem_handle_t *pIpcHandle) override;
ze_result_t putVirtualAddressSpaceIpcHandle(ze_ipc_mem_handle_t ipcHandle) override;
std::map<uint32_t, ze_device_handle_t> &getDevices() {
return devices;
}
void freePeerAllocations(const void *ptr, bool blocking, Device *device);
ze_result_t handleAllocationExtensions(NEO::GraphicsAllocation *alloc, ze_memory_type_t type,
void *pNext, struct DriverHandleImp *driverHandle);
RootDeviceIndicesContainer rootDeviceIndices;
std::map<uint32_t, NEO::DeviceBitfield> deviceBitfields;
ContextSettings contextSettings;
bool isDeviceDefinedForThisContext(Device *inDevice);
bool isShareableMemory(const void *exportDesc, bool exportableMemory, NEO::Device *neoDevice, bool shareableWithoutNTHandle) override;
void *getMemHandlePtr(ze_device_handle_t hDevice, uint64_t handle, NEO::AllocationType allocationType, unsigned int processId, ze_ipc_memory_flags_t flags) override;
void getDataFromIpcHandle(ze_device_handle_t hDevice, const ze_ipc_mem_handle_t ipcHandle, uint64_t &handle, uint8_t &type, unsigned int &processId, uint64_t &poolOffset) override;
bool isOpaqueHandleSupported(IpcHandleType *handleType) override;
void initDeviceHandles(uint32_t numDevices, ze_device_handle_t *deviceHandles) {
this->numDevices = numDevices;
if (numDevices > 0) {
this->deviceHandles.assign(deviceHandles, deviceHandles + numDevices);
}
}
void addDeviceHandle(ze_device_handle_t deviceHandle) {
this->deviceHandles.push_back(deviceHandle);
this->numDevices = static_cast<uint32_t>(this->deviceHandles.size());
}
NEO::VirtualMemoryReservation *findSupportedVirtualReservation(const void *ptr, size_t size);
ze_result_t checkMemSizeLimit(Device *inDevice, size_t size, bool relaxedSizeAllowed, void **ptr);
ze_result_t getPitchFor2dImage(
ze_device_handle_t hDevice,
size_t imageWidth,
size_t imageHeight,
unsigned int elementSizeInBytes,
size_t *rowPitch) override;
ContextExt *getContextExt() override {
return contextExt;
}
uint32_t getNumDevices() const {
return numDevices;
}
protected:
ze_result_t getIpcMemHandlesImpl(const void *ptr, uint32_t *numIpcHandles, ze_ipc_mem_handle_t *pIpcHandles);
template <typename IpcDataT>
void setIPCHandleData(NEO::GraphicsAllocation *graphicsAllocation, uint64_t handle, IpcDataT &ipcData, uint64_t ptrAddress, uint8_t type, NEO::UsmMemAllocPool *usmPool, IpcHandleType handleType) {
std::map<uint64_t, IpcHandleTracking *>::iterator ipcHandleIterator;
ipcData = {};
if constexpr (std::is_same_v<IpcDataT, IpcMemoryData>) {
ipcData.handle = handle;
ipcData.type = type;
}
if constexpr (std::is_same_v<IpcDataT, IpcOpaqueMemoryData>) {
ipcData.memoryType = type;
ipcData.processId = NEO::SysCalls::getCurrentProcessId();
ipcData.type = handleType;
if (handleType == IpcHandleType::ntHandle) {
ipcData.handle.reserved = handle;
} else if (handleType == IpcHandleType::fdHandle) {
// For fdHandle, we store the handle as an int
ipcData.handle.fd = static_cast<int>(handle);
}
}
if (usmPool) {
ipcData.poolOffset = usmPool->getOffsetInPool(addrToPtr(ptrAddress));
ptrAddress = usmPool->getPoolAddress();
}
auto lock = this->driverHandle->lockIPCHandleMap();
ipcHandleIterator = this->driverHandle->getIPCHandleMap().find(handle);
if (ipcHandleIterator != this->driverHandle->getIPCHandleMap().end()) {
ipcHandleIterator->second->refcnt += 1;
} else {
IpcHandleTracking *handleTracking = new IpcHandleTracking;
handleTracking->alloc = graphicsAllocation;
handleTracking->refcnt = 1;
handleTracking->ptr = ptrAddress;
handleTracking->handle = handle;
if constexpr (std::is_same_v<IpcDataT, IpcMemoryData>) {
handleTracking->ipcData = ipcData;
} else {
handleTracking->opaqueData = ipcData;
handleTracking->opaqueIpcHandle = true;
}
this->driverHandle->getIPCHandleMap().insert(std::pair<uint64_t, IpcHandleTracking *>(handle, handleTracking));
}
}
bool isAllocationSuitableForCompression(const StructuresLookupTable &structuresLookupTable, Device &device, size_t allocSize);
size_t getPageAlignedSizeRequired(size_t size, NEO::HeapIndex *heapRequired, size_t *pageSizeRequired) {
return getPageAlignedSizeRequired(nullptr, size, heapRequired, pageSizeRequired);
}
size_t getPageAlignedSizeRequired(const void *pStart, size_t size, NEO::HeapIndex *heapRequired, size_t *pageSizeRequired);
NEO::UsmMemAllocPool *getUsmPoolOwningPtr(const void *ptr, NEO::SvmAllocationData *svmData);
bool tryFreeViaPooling(const void *ptr, NEO::SvmAllocationData *svmData, NEO::UsmMemAllocPool *usmPool);
std::map<uint32_t, ze_device_handle_t> devices;
std::vector<ze_device_handle_t> deviceHandles;
DriverHandleImp *driverHandle = nullptr;
uint32_t numDevices = 0;
ContextExt *contextExt = nullptr;
};
static_assert(NEO::NonCopyableAndNonMovable<ContextImp>);
} // namespace L0
|