2021-11-26 23:46:48 +08:00
|
|
|
/*
|
2023-12-07 03:30:05 +08:00
|
|
|
* Copyright (C) 2021-2024 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"
|
2022-06-15 00:47:02 +08:00
|
|
|
|
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);
|
2021-12-29 19:14:26 +08:00
|
|
|
std::copy_if(drmQueryRegions.begin(), drmQueryRegions.end(), std::back_inserter(localMemoryRegions),
|
2022-07-25 19:50:32 +08:00
|
|
|
[&](const MemoryRegion &memoryRegionInfo) {
|
|
|
|
return (memoryRegionInfo.region.memoryClass == memoryClassDevice);
|
2021-12-29 19:14:26 +08:00
|
|
|
});
|
2023-12-07 03:30:05 +08:00
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return this->drm.getIoctlHelper()->createGemExt(memClassInstances, allocSize, handle, patIndex, vmId, pairHandle, isChunked, numOfChunks, mode, memPolicyNodeMask);
|
|
|
|
} else {
|
|
|
|
return this->drm.getIoctlHelper()->createGemExt(memClassInstances, allocSize, handle, patIndex, vmId, pairHandle, isChunked, numOfChunks, std::nullopt, std::nullopt);
|
|
|
|
}
|
2022-04-27 19:21:53 +08:00
|
|
|
}
|
|
|
|
|
2024-04-15 19:09:00 +08:00
|
|
|
uint32_t MemoryInfo::getTileIndex(uint32_t memoryBank) const {
|
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>();
|
|
|
|
|
2022-04-27 19:21:53 +08:00
|
|
|
auto tileIndex = Math::log2(memoryBank);
|
2022-12-20 10:54:11 +08:00
|
|
|
tileIndex = gfxCoreHelper.isBankOverrideRequired(hwInfo, productHelper) ? 0 : tileIndex;
|
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
|
|
|
}
|
|
|
|
return tileIndex;
|
2021-11-26 23:46:48 +08:00
|
|
|
}
|
|
|
|
|
2021-12-17 21:26:04 +08:00
|
|
|
MemoryClassInstance MemoryInfo::getMemoryRegionClassAndInstance(uint32_t memoryBank, 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)) {
|
|
|
|
memoryBank = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getMemoryRegion(memoryBank).region;
|
|
|
|
}
|
|
|
|
|
2024-04-15 19:09:00 +08:00
|
|
|
const MemoryRegion &MemoryInfo::getMemoryRegion(uint32_t memoryBank) const {
|
2023-05-30 19:44:37 +08:00
|
|
|
if (memoryBank == 0) {
|
|
|
|
return systemMemoryRegion;
|
2021-11-26 23:46:48 +08:00
|
|
|
}
|
|
|
|
|
2022-12-20 10:54:11 +08:00
|
|
|
auto index = getTileIndex(memoryBank);
|
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-01-19 01:43:09 +08:00
|
|
|
int MemoryInfo::createGemExtWithSingleRegion(uint32_t 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()) {
|
2023-11-30 16:32:25 +08:00
|
|
|
if (memoryBanks != 0 && debugManager.flags.EnablePrivateBO.get()) {
|
2022-12-20 10:54:11 +08:00
|
|
|
auto tileIndex = getTileIndex(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-01-19 01:43:09 +08:00
|
|
|
int MemoryInfo::createGemExtWithMultipleRegions(uint32_t 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-01-19 01:43:09 +08:00
|
|
|
int MemoryInfo::createGemExtWithMultipleRegions(uint32_t 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();
|
|
|
|
auto banks = std::bitset<4>(memoryBanks);
|
|
|
|
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++;
|
|
|
|
}
|
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
|