2019-03-01 16:14:28 +01:00
|
|
|
/*
|
2025-08-01 23:43:17 +00:00
|
|
|
* Copyright (C) 2019-2025 Intel Corporation
|
2019-03-01 16:14:28 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2020-04-02 11:28:38 +02:00
|
|
|
#include "shared/source/helpers/constants.h"
|
2020-02-23 22:44:01 +01:00
|
|
|
#include "shared/source/os_interface/os_memory.h"
|
2019-03-01 16:14:28 +01:00
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
|
2019-03-26 11:59:46 +01:00
|
|
|
namespace NEO {
|
2023-01-13 15:18:40 +00:00
|
|
|
class HeapAllocator;
|
2019-03-01 16:14:28 +01:00
|
|
|
|
|
|
|
|
enum class HeapIndex : uint32_t {
|
2023-12-12 08:58:42 +00:00
|
|
|
heapInternalDeviceMemory = 0u,
|
|
|
|
|
heapInternal = 1u,
|
|
|
|
|
heapExternalDeviceMemory = 2u,
|
|
|
|
|
heapExternal = 3u,
|
|
|
|
|
heapStandard,
|
|
|
|
|
heapStandard64KB,
|
|
|
|
|
heapStandard2MB,
|
|
|
|
|
heapSvm,
|
|
|
|
|
heapExtended,
|
|
|
|
|
heapExternalFrontWindow,
|
|
|
|
|
heapExternalDeviceFrontWindow,
|
|
|
|
|
heapInternalFrontWindow,
|
|
|
|
|
heapInternalDeviceFrontWindow,
|
|
|
|
|
heapExtendedHost,
|
2019-03-01 16:14:28 +01:00
|
|
|
|
|
|
|
|
// Please put new heap indexes above this line
|
2023-12-12 08:58:42 +00:00
|
|
|
totalHeaps
|
2019-03-01 16:14:28 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class GfxPartition {
|
|
|
|
|
public:
|
2025-08-18 10:29:39 +00:00
|
|
|
GfxPartition(OSMemory::ReservedCpuAddressRange &reservedCpuAddressRangeForNonSvmHeaps);
|
2019-07-29 17:50:46 +02:00
|
|
|
MOCKABLE_VIRTUAL ~GfxPartition();
|
2019-03-01 16:14:28 +01:00
|
|
|
|
2024-03-28 08:46:55 +00:00
|
|
|
MOCKABLE_VIRTUAL bool init(uint64_t gpuAddressSpace, size_t cpuAddressRangeSizeToReserve, uint32_t rootDeviceIndex, size_t numRootDevices, bool useExternalFrontWindowPool, uint64_t systemMemorySize, uint64_t gfxTop);
|
2019-03-01 16:14:28 +01:00
|
|
|
|
|
|
|
|
void heapInit(HeapIndex heapIndex, uint64_t base, uint64_t size) {
|
2024-10-22 18:15:13 +00:00
|
|
|
getHeap(heapIndex).init(base, size, MemoryConstants::pageSize);
|
2021-01-21 12:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void heapInitWithAllocationAlignment(HeapIndex heapIndex, uint64_t base, uint64_t size, size_t allocationAlignment) {
|
2024-10-22 18:15:13 +00:00
|
|
|
getHeap(heapIndex).init(base, size, allocationAlignment);
|
2019-03-01 16:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
2025-08-13 00:35:28 +00:00
|
|
|
void heapInitExternalWithFrontWindow(HeapIndex heapIndex, uint64_t base, uint64_t size) {
|
|
|
|
|
getHeap(heapIndex).initExternalWithFrontWindow(base, size);
|
2020-09-14 15:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-13 00:35:28 +00:00
|
|
|
void heapInitWithFrontWindow(HeapIndex heapIndex, uint64_t base, uint64_t size, uint64_t frontWindowSize) {
|
|
|
|
|
getHeap(heapIndex).initWithFrontWindow(base, size, frontWindowSize);
|
2020-10-14 09:50:07 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-13 00:35:28 +00:00
|
|
|
void heapInitFrontWindow(HeapIndex heapIndex, uint64_t base, uint64_t size) {
|
|
|
|
|
getHeap(heapIndex).initFrontWindow(base, size);
|
2020-10-14 09:50:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-25 20:30:21 +00:00
|
|
|
MOCKABLE_VIRTUAL uint64_t heapAllocate(HeapIndex heapIndex, size_t &size) {
|
2019-03-01 16:14:28 +01:00
|
|
|
return getHeap(heapIndex).allocate(size);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 13:30:21 +00:00
|
|
|
MOCKABLE_VIRTUAL uint64_t heapAllocateWithCustomAlignment(HeapIndex heapIndex, size_t &size, size_t alignment) {
|
2021-05-11 09:23:27 +00:00
|
|
|
return getHeap(heapIndex).allocateWithCustomAlignment(size, alignment);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-25 20:30:21 +00:00
|
|
|
MOCKABLE_VIRTUAL void heapFree(HeapIndex heapIndex, uint64_t ptr, size_t size) {
|
2019-03-01 16:14:28 +01:00
|
|
|
getHeap(heapIndex).free(ptr, size);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 17:50:46 +02:00
|
|
|
MOCKABLE_VIRTUAL void freeGpuAddressRange(uint64_t ptr, size_t size);
|
2019-04-18 15:30:47 +02:00
|
|
|
|
2019-03-01 16:14:28 +01:00
|
|
|
uint64_t getHeapBase(HeapIndex heapIndex) {
|
|
|
|
|
return getHeap(heapIndex).getBase();
|
|
|
|
|
}
|
2019-04-04 14:22:13 +02:00
|
|
|
|
2019-03-18 10:06:01 +01:00
|
|
|
uint64_t getHeapLimit(HeapIndex heapIndex) {
|
2019-04-18 15:30:47 +02:00
|
|
|
return getHeap(heapIndex).getLimit();
|
2019-03-18 10:06:01 +01:00
|
|
|
}
|
2019-03-01 16:14:28 +01:00
|
|
|
|
2022-12-15 16:01:37 +00:00
|
|
|
uint64_t getHeapMinimalAddress(HeapIndex heapIndex);
|
2019-04-15 15:20:51 +02:00
|
|
|
|
2023-12-12 08:58:42 +00:00
|
|
|
bool isLimitedRange() { return getHeap(HeapIndex::heapSvm).getSize() == 0ull; }
|
2019-04-18 15:30:47 +02:00
|
|
|
|
2023-10-06 14:48:29 +00:00
|
|
|
static bool isAnyHeap32(HeapIndex heapIndex) {
|
2023-12-12 08:58:42 +00:00
|
|
|
if ((heapIndex >= HeapIndex::heapInternalDeviceMemory && heapIndex <= HeapIndex::heapExternal) ||
|
|
|
|
|
(heapIndex >= HeapIndex::heapExternalFrontWindow && heapIndex <= HeapIndex::heapInternalDeviceFrontWindow)) {
|
2023-10-06 14:48:29 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-24 11:37:13 +00:00
|
|
|
static constexpr uint64_t heapGranularity = MemoryConstants::pageSize64k;
|
|
|
|
|
static constexpr uint64_t heapGranularity2MB = 2 * MemoryConstants::megaByte;
|
2023-10-05 13:08:27 +00:00
|
|
|
static constexpr size_t externalFrontWindowPoolSize = 2 * MemoryConstants::pageSize64k;
|
2020-10-14 09:50:07 +02:00
|
|
|
static constexpr size_t internalFrontWindowPoolSize = 1 * MemoryConstants::megaByte;
|
2019-04-04 14:22:13 +02:00
|
|
|
|
2019-03-01 16:14:28 +01:00
|
|
|
static const std::array<HeapIndex, 4> heap32Names;
|
2021-04-19 18:58:10 +00:00
|
|
|
static const std::array<HeapIndex, 8> heapNonSvmNames;
|
2019-03-01 16:14:28 +01:00
|
|
|
|
|
|
|
|
protected:
|
2025-08-18 13:02:33 +00:00
|
|
|
bool initAdditionalRange(uint32_t cpuAddressWidth, uint64_t gpuAddressSpace, uint64_t &gfxBase, uint64_t &gfxTop, uint32_t rootDeviceIndex, uint64_t systemMemorySize, size_t numRootDevices);
|
2019-09-10 14:59:45 +02:00
|
|
|
|
2019-03-01 16:14:28 +01:00
|
|
|
class Heap {
|
|
|
|
|
public:
|
|
|
|
|
Heap() = default;
|
2024-10-22 18:15:13 +00:00
|
|
|
void init(uint64_t base, uint64_t size, size_t allocationAlignment);
|
2025-08-13 00:35:28 +00:00
|
|
|
void initExternalWithFrontWindow(uint64_t base, uint64_t size);
|
|
|
|
|
void initWithFrontWindow(uint64_t base, uint64_t size, uint64_t frontWindowSize);
|
|
|
|
|
void initFrontWindow(uint64_t base, uint64_t size);
|
2019-03-01 16:14:28 +01:00
|
|
|
uint64_t getBase() const { return base; }
|
|
|
|
|
uint64_t getSize() const { return size; }
|
2020-01-08 12:36:09 +01:00
|
|
|
uint64_t getLimit() const { return size ? base + size - 1 : 0; }
|
2023-01-13 15:18:40 +00:00
|
|
|
uint64_t allocate(size_t &size);
|
|
|
|
|
uint64_t allocateWithCustomAlignment(size_t &sizeToAllocate, size_t alignment);
|
|
|
|
|
void free(uint64_t ptr, size_t size);
|
2019-03-01 16:14:28 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
uint64_t base = 0, size = 0;
|
|
|
|
|
std::unique_ptr<HeapAllocator> alloc;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Heap &getHeap(HeapIndex heapIndex) {
|
2019-04-18 15:30:47 +02:00
|
|
|
return heaps[static_cast<uint32_t>(heapIndex)];
|
2019-03-01 16:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-12 08:58:42 +00:00
|
|
|
std::array<Heap, static_cast<uint32_t>(HeapIndex::totalHeaps)> heaps;
|
2019-04-18 15:30:47 +02:00
|
|
|
|
2025-08-18 10:29:39 +00:00
|
|
|
OSMemory::ReservedCpuAddressRange &reservedCpuAddressRangeForNonSvmHeaps;
|
2023-03-22 14:32:27 +00:00
|
|
|
OSMemory::ReservedCpuAddressRange reservedCpuAddressRangeForHeapExtended{};
|
2019-07-30 16:46:58 +02:00
|
|
|
std::unique_ptr<OSMemory> osMemory;
|
2019-03-01 16:14:28 +01:00
|
|
|
};
|
2019-03-26 11:59:46 +01:00
|
|
|
} // namespace NEO
|