2021-11-26 23:46:48 +08:00
|
|
|
/*
|
2025-03-06 17:57:13 +08:00
|
|
|
* Copyright (C) 2021-2025 Intel Corporation
|
2021-11-26 23:46:48 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared/source/os_interface/linux/memory_info.h"
|
|
|
|
|
2022-05-18 03:04:23 +08:00
|
|
|
#include "shared/source/debug_settings/debug_settings_manager.h"
|
2021-11-26 23:46:48 +08:00
|
|
|
#include "shared/source/execution_environment/root_device_environment.h"
|
|
|
|
#include "shared/source/helpers/basic_math.h"
|
|
|
|
#include "shared/source/helpers/debug_helpers.h"
|
2023-02-02 00:23:01 +08:00
|
|
|
#include "shared/source/helpers/gfx_core_helper.h"
|
2022-05-25 22:39:35 +08:00
|
|
|
#include "shared/source/helpers/hw_info.h"
|
2021-11-26 23:46:48 +08:00
|
|
|
#include "shared/source/os_interface/linux/drm_neo.h"
|
2023-12-07 03:30:05 +08:00
|
|
|
#include "shared/source/os_interface/linux/numa_library.h"
|
2024-05-22 21:31:31 +08:00
|
|
|
#include "shared/source/os_interface/product_helper.h"
|
2022-06-15 00:47:02 +08:00
|
|
|
|
2025-04-10 05:32:48 +08:00
|
|
|
#include <algorithm>
|
2022-05-18 03:04:23 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2021-11-26 23:46:48 +08:00
|
|
|
namespace NEO {
|
|
|
|
|
2022-07-25 19:50:32 +08:00
|
|
|
MemoryInfo::MemoryInfo(const RegionContainer ®ionInfo, const Drm &inputDrm)
|
|
|
|
: drm(inputDrm), drmQueryRegions(regionInfo), systemMemoryRegion(drmQueryRegions[0]) {
|
|
|
|
auto ioctlHelper = drm.getIoctlHelper();
|
2023-12-13 17:05:31 +08:00
|
|
|
const auto memoryClassSystem = ioctlHelper->getDrmParamValue(DrmParam::memoryClassSystem);
|
|
|
|
const auto memoryClassDevice = ioctlHelper->getDrmParamValue(DrmParam::memoryClassDevice);
|
2022-07-25 19:50:32 +08:00
|
|
|
UNRECOVERABLE_IF(this->systemMemoryRegion.region.memoryClass != memoryClassSystem);
|
2025-04-10 05:32:48 +08:00
|
|
|
|
|
|
|
std::ranges::copy_if(drmQueryRegions, std::back_inserter(localMemoryRegions),
|
|
|
|
[memoryClassDevice](const MemoryRegion &memoryRegionInfo) {
|
|
|
|
return (memoryRegionInfo.region.memoryClass == memoryClassDevice);
|
|
|
|
});
|
|
|
|
|
|
|
|
smallBarDetected = std::ranges::any_of(localMemoryRegions,
|
|
|
|
[](const MemoryRegion ®ion) {
|
|
|
|
return (region.cpuVisibleSize && region.cpuVisibleSize < region.probedSize);
|
|
|
|
});
|
2023-12-07 03:30:05 +08:00
|
|
|
|
2024-04-26 01:29:57 +08:00
|
|
|
populateTileToLocalMemoryRegionIndexMap();
|
|
|
|
|
2024-01-30 15:12:27 +08:00
|
|
|
memPolicySupported = false;
|
|
|
|
if (debugManager.flags.EnableHostAllocationMemPolicy.get()) {
|
|
|
|
memPolicySupported = Linux::NumaLibrary::init();
|
|
|
|
}
|
2023-12-07 03:30:05 +08:00
|
|
|
memPolicyMode = debugManager.flags.OverrideHostAllocationMemPolicyMode.get();
|
2021-12-29 19:14:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemoryInfo::assignRegionsFromDistances(const std::vector<DistanceInfo> &distances) {
|
|
|
|
localMemoryRegions.clear();
|
|
|
|
|
|
|
|
uint32_t memoryRegionCounter = 1;
|
|
|
|
uint32_t tile = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < distances.size(); i++) {
|
|
|
|
if (i > 0 && distances[i].region.memoryInstance != distances[i - 1].region.memoryInstance) {
|
|
|
|
UNRECOVERABLE_IF(distances[i].distance == 0);
|
|
|
|
|
|
|
|
memoryRegionCounter++;
|
|
|
|
tile++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((distances[i].distance != 0) || (localMemoryRegions.size() == (tile + 1))) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNRECOVERABLE_IF((drmQueryRegions[memoryRegionCounter].region.memoryClass != distances[i].region.memoryClass) ||
|
|
|
|
(drmQueryRegions[memoryRegionCounter].region.memoryInstance != distances[i].region.memoryInstance));
|
|
|
|
|
|
|
|
localMemoryRegions.push_back(drmQueryRegions[memoryRegionCounter]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 01:43:09 +08:00
|
|
|
int MemoryInfo::createGemExt(const MemRegionsVec &memClassInstances, size_t allocSize, uint32_t &handle, uint64_t patIndex, std::optional<uint32_t> vmId, int32_t pairHandle, bool isChunked, uint32_t numOfChunks, bool isUSMHostAllocation) {
|
2023-12-07 03:30:05 +08:00
|
|
|
std::vector<unsigned long> memPolicyNodeMask;
|
|
|
|
int mode = -1;
|
2024-05-22 21:31:31 +08:00
|
|
|
auto &productHelper = this->drm.getRootDeviceEnvironment().getHelper<ProductHelper>();
|
|
|
|
auto isCoherent = productHelper.isCoherentAllocation(patIndex);
|
2023-12-07 03:30:05 +08:00
|
|
|
if (memPolicySupported &&
|
|
|
|
isUSMHostAllocation &&
|
2024-01-19 01:43:09 +08:00
|
|
|
Linux::NumaLibrary::getMemPolicy(&mode, memPolicyNodeMask)) {
|
2023-12-07 03:30:05 +08:00
|
|
|
if (memPolicyMode != -1) {
|
|
|
|
mode = memPolicyMode;
|
|
|
|
}
|
2024-05-22 21:31:31 +08:00
|
|
|
return this->drm.getIoctlHelper()->createGemExt(memClassInstances, allocSize, handle, patIndex, vmId, pairHandle, isChunked, numOfChunks, mode, memPolicyNodeMask, isCoherent);
|
2023-12-07 03:30:05 +08:00
|
|
|
} else {
|
2024-05-22 21:31:31 +08:00
|
|
|
return this->drm.getIoctlHelper()->createGemExt(memClassInstances, allocSize, handle, patIndex, vmId, pairHandle, isChunked, numOfChunks, std::nullopt, std::nullopt, isCoherent);
|
2023-12-07 03:30:05 +08:00
|
|
|
}
|
2022-04-27 19:21:53 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 01:29:57 +08:00
|
|
|
uint32_t MemoryInfo::getLocalMemoryRegionIndex(DeviceBitfield deviceBitfield) const {
|
|
|
|
UNRECOVERABLE_IF(deviceBitfield.count() != 1u);
|
2022-12-20 10:54:11 +08:00
|
|
|
auto &hwInfo = *this->drm.getRootDeviceEnvironment().getHardwareInfo();
|
|
|
|
auto &gfxCoreHelper = this->drm.getRootDeviceEnvironment().getHelper<GfxCoreHelper>();
|
|
|
|
auto &productHelper = this->drm.getRootDeviceEnvironment().getHelper<ProductHelper>();
|
2024-04-26 01:29:57 +08:00
|
|
|
bool bankOverrideRequired{gfxCoreHelper.isBankOverrideRequired(hwInfo, productHelper)};
|
2022-12-20 10:54:11 +08:00
|
|
|
|
2024-04-26 22:39:26 +08:00
|
|
|
uint32_t tileIndex{bankOverrideRequired ? 0u : Math::log2(static_cast<uint64_t>(deviceBitfield.to_ulong()))};
|
2023-11-30 16:32:25 +08:00
|
|
|
if (debugManager.flags.OverrideDrmRegion.get() != -1) {
|
|
|
|
tileIndex = debugManager.flags.OverrideDrmRegion.get();
|
2022-04-27 19:21:53 +08:00
|
|
|
}
|
2024-04-26 01:29:57 +08:00
|
|
|
UNRECOVERABLE_IF(tileIndex >= tileToLocalMemoryRegionIndexMap.size());
|
|
|
|
return tileToLocalMemoryRegionIndexMap[tileIndex];
|
2021-11-26 23:46:48 +08:00
|
|
|
}
|
|
|
|
|
2025-03-06 17:57:13 +08:00
|
|
|
uint64_t MemoryInfo::getLocalMemoryRegionSize(uint32_t tileIndex) const {
|
|
|
|
UNRECOVERABLE_IF(tileIndex >= tileToLocalMemoryRegionIndexMap.size());
|
|
|
|
const auto regionIndex{tileToLocalMemoryRegionIndexMap[tileIndex]};
|
|
|
|
return localMemoryRegions[regionIndex].probedSize;
|
|
|
|
}
|
|
|
|
|
2024-04-26 01:29:57 +08:00
|
|
|
MemoryClassInstance MemoryInfo::getMemoryRegionClassAndInstance(DeviceBitfield deviceBitfield, const HardwareInfo &hwInfo) {
|
2022-12-20 10:54:11 +08:00
|
|
|
|
|
|
|
auto &gfxCoreHelper = this->drm.getRootDeviceEnvironment().getHelper<GfxCoreHelper>();
|
2023-05-30 19:44:37 +08:00
|
|
|
if (!gfxCoreHelper.getEnableLocalMemory(hwInfo)) {
|
2024-04-26 01:29:57 +08:00
|
|
|
deviceBitfield = 0u;
|
2023-05-30 19:44:37 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 01:29:57 +08:00
|
|
|
return getMemoryRegion(deviceBitfield).region;
|
2023-05-30 19:44:37 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 01:29:57 +08:00
|
|
|
const MemoryRegion &MemoryInfo::getMemoryRegion(DeviceBitfield deviceBitfield) const {
|
|
|
|
if (deviceBitfield.count() == 0) {
|
2023-05-30 19:44:37 +08:00
|
|
|
return systemMemoryRegion;
|
2021-11-26 23:46:48 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 01:29:57 +08:00
|
|
|
auto index = getLocalMemoryRegionIndex(deviceBitfield);
|
2021-11-26 23:46:48 +08:00
|
|
|
|
|
|
|
UNRECOVERABLE_IF(index >= localMemoryRegions.size());
|
2023-05-30 19:44:37 +08:00
|
|
|
return localMemoryRegions[index];
|
2021-11-26 23:46:48 +08:00
|
|
|
}
|
|
|
|
|
2024-04-15 19:09:00 +08:00
|
|
|
size_t MemoryInfo::getMemoryRegionSize(uint32_t memoryBank) const {
|
2023-11-30 16:32:25 +08:00
|
|
|
if (debugManager.flags.PrintMemoryRegionSizes.get()) {
|
2021-11-26 23:46:48 +08:00
|
|
|
printRegionSizes();
|
|
|
|
}
|
2023-05-30 19:44:37 +08:00
|
|
|
return getMemoryRegion(memoryBank).probedSize;
|
2021-11-26 23:46:48 +08:00
|
|
|
}
|
|
|
|
|
2024-04-15 19:09:00 +08:00
|
|
|
void MemoryInfo::printRegionSizes() const {
|
2023-10-03 23:02:52 +08:00
|
|
|
for (auto ®ion : drmQueryRegions) {
|
2021-12-17 21:26:04 +08:00
|
|
|
std::cout << "Memory type: " << region.region.memoryClass
|
|
|
|
<< ", memory instance: " << region.region.memoryInstance
|
|
|
|
<< ", region size: " << region.probedSize << std::endl;
|
2021-11-26 23:46:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 00:35:40 +08:00
|
|
|
int MemoryInfo::createGemExtWithSingleRegion(DeviceBitfield memoryBanks, size_t allocSize, uint32_t &handle, uint64_t patIndex, int32_t pairHandle, bool isUSMHostAllocation) {
|
2022-07-25 19:50:32 +08:00
|
|
|
auto pHwInfo = this->drm.getRootDeviceEnvironment().getHardwareInfo();
|
2021-11-26 23:46:48 +08:00
|
|
|
auto regionClassAndInstance = getMemoryRegionClassAndInstance(memoryBanks, *pHwInfo);
|
2022-01-31 18:12:51 +08:00
|
|
|
MemRegionsVec region = {regionClassAndInstance};
|
2022-06-01 00:14:00 +08:00
|
|
|
std::optional<uint32_t> vmId;
|
2022-07-25 19:50:32 +08:00
|
|
|
if (!this->drm.isPerContextVMRequired()) {
|
2024-05-14 00:35:40 +08:00
|
|
|
if (memoryBanks.count() && debugManager.flags.EnablePrivateBO.get()) {
|
2024-04-26 01:29:57 +08:00
|
|
|
auto tileIndex = getLocalMemoryRegionIndex(memoryBanks);
|
2022-07-25 19:50:32 +08:00
|
|
|
vmId = this->drm.getVirtualMemoryAddressSpace(tileIndex);
|
2022-04-27 19:21:53 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-08 12:06:00 +08:00
|
|
|
uint32_t numOfChunks = 0;
|
2024-01-19 01:43:09 +08:00
|
|
|
auto ret = createGemExt(region, allocSize, handle, patIndex, vmId, pairHandle, false, numOfChunks, isUSMHostAllocation);
|
2021-11-26 23:46:48 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-05-14 00:35:40 +08:00
|
|
|
int MemoryInfo::createGemExtWithMultipleRegions(DeviceBitfield memoryBanks, size_t allocSize, uint32_t &handle, uint64_t patIndex, bool isUSMHostAllocation) {
|
2022-07-25 19:50:32 +08:00
|
|
|
auto pHwInfo = this->drm.getRootDeviceEnvironment().getHardwareInfo();
|
2022-09-20 00:38:59 +08:00
|
|
|
auto banks = std::bitset<4>(memoryBanks);
|
2022-06-28 20:36:40 +08:00
|
|
|
MemRegionsVec memRegions{};
|
|
|
|
size_t currentBank = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
while (i < banks.count()) {
|
|
|
|
if (banks.test(currentBank)) {
|
|
|
|
auto regionClassAndInstance = getMemoryRegionClassAndInstance(1u << currentBank, *pHwInfo);
|
|
|
|
memRegions.push_back(regionClassAndInstance);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
currentBank++;
|
|
|
|
}
|
2023-03-08 12:06:00 +08:00
|
|
|
uint32_t numOfChunks = 0;
|
2024-01-19 01:43:09 +08:00
|
|
|
auto ret = createGemExt(memRegions, allocSize, handle, patIndex, {}, -1, false, numOfChunks, isUSMHostAllocation);
|
2023-03-08 12:06:00 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-05-14 00:35:40 +08:00
|
|
|
int MemoryInfo::createGemExtWithMultipleRegions(DeviceBitfield memoryBanks, size_t allocSize, uint32_t &handle, uint64_t patIndex, int32_t pairHandle, bool isChunked, uint32_t numOfChunks, bool isUSMHostAllocation) {
|
2023-03-08 12:06:00 +08:00
|
|
|
auto pHwInfo = this->drm.getRootDeviceEnvironment().getHardwareInfo();
|
|
|
|
MemRegionsVec memRegions{};
|
|
|
|
size_t currentBank = 0;
|
|
|
|
size_t i = 0;
|
2024-05-14 00:35:40 +08:00
|
|
|
while (i < memoryBanks.count()) {
|
|
|
|
if (memoryBanks.test(currentBank)) {
|
2023-03-08 12:06:00 +08:00
|
|
|
auto regionClassAndInstance = getMemoryRegionClassAndInstance(1u << currentBank, *pHwInfo);
|
|
|
|
memRegions.push_back(regionClassAndInstance);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
currentBank++;
|
|
|
|
}
|
2024-01-19 01:43:09 +08:00
|
|
|
auto ret = createGemExt(memRegions, allocSize, handle, patIndex, {}, pairHandle, isChunked, numOfChunks, isUSMHostAllocation);
|
2022-06-28 20:36:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-26 23:46:48 +08:00
|
|
|
} // namespace NEO
|