2019-03-01 23:14:28 +08:00
|
|
|
/*
|
2023-01-13 23:18:40 +08:00
|
|
|
* Copyright (C) 2019-2023 Intel Corporation
|
2019-03-01 23:14:28 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-04-02 17:28:38 +08:00
|
|
|
#include "shared/source/helpers/constants.h"
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/os_interface/os_memory.h"
|
2019-03-01 23:14:28 +08:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2023-01-13 23:18:40 +08:00
|
|
|
class HeapAllocator;
|
2019-03-01 23:14:28 +08:00
|
|
|
|
|
|
|
enum class HeapIndex : uint32_t {
|
|
|
|
HEAP_INTERNAL_DEVICE_MEMORY = 0u,
|
|
|
|
HEAP_INTERNAL = 1u,
|
|
|
|
HEAP_EXTERNAL_DEVICE_MEMORY = 2u,
|
|
|
|
HEAP_EXTERNAL = 3u,
|
|
|
|
HEAP_STANDARD,
|
|
|
|
HEAP_STANDARD64KB,
|
2021-03-01 20:11:41 +08:00
|
|
|
HEAP_STANDARD2MB,
|
2019-03-01 23:14:28 +08:00
|
|
|
HEAP_SVM,
|
2020-01-08 19:36:09 +08:00
|
|
|
HEAP_EXTENDED,
|
2020-09-14 21:14:11 +08:00
|
|
|
HEAP_EXTERNAL_FRONT_WINDOW,
|
|
|
|
HEAP_EXTERNAL_DEVICE_FRONT_WINDOW,
|
2020-10-14 15:50:07 +08:00
|
|
|
HEAP_INTERNAL_FRONT_WINDOW,
|
|
|
|
HEAP_INTERNAL_DEVICE_FRONT_WINDOW,
|
2023-04-27 23:43:49 +08:00
|
|
|
HEAP_EXTENDED_HOST,
|
2019-03-01 23:14:28 +08:00
|
|
|
|
|
|
|
// Please put new heap indexes above this line
|
|
|
|
TOTAL_HEAPS
|
|
|
|
};
|
|
|
|
|
|
|
|
class GfxPartition {
|
|
|
|
public:
|
2023-03-22 22:32:27 +08:00
|
|
|
GfxPartition(OSMemory::ReservedCpuAddressRange &reservedCpuAddressRangeForHeapSvm);
|
2019-07-29 23:50:46 +08:00
|
|
|
MOCKABLE_VIRTUAL ~GfxPartition();
|
2019-03-01 23:14:28 +08:00
|
|
|
|
2023-04-28 21:24:48 +08:00
|
|
|
MOCKABLE_VIRTUAL bool init(uint64_t gpuAddressSpace, size_t cpuAddressRangeSizeToReserve, uint32_t rootDeviceIndex, size_t numRootDevices, bool useExternalFrontWindowPool, uint64_t systemMemorySize);
|
2019-03-01 23:14:28 +08:00
|
|
|
|
|
|
|
void heapInit(HeapIndex heapIndex, uint64_t base, uint64_t size) {
|
2021-01-21 20:52:48 +08:00
|
|
|
getHeap(heapIndex).init(base, size, MemoryConstants::pageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void heapInitWithAllocationAlignment(HeapIndex heapIndex, uint64_t base, uint64_t size, size_t allocationAlignment) {
|
|
|
|
getHeap(heapIndex).init(base, size, allocationAlignment);
|
2019-03-01 23:14:28 +08:00
|
|
|
}
|
|
|
|
|
2020-09-14 21:14:11 +08:00
|
|
|
void heapInitExternalWithFrontWindow(HeapIndex heapIndex, uint64_t base, uint64_t size) {
|
|
|
|
getHeap(heapIndex).initExternalWithFrontWindow(base, size);
|
|
|
|
}
|
|
|
|
|
2020-10-14 15:50:07 +08:00
|
|
|
void heapInitWithFrontWindow(HeapIndex heapIndex, uint64_t base, uint64_t size, uint64_t frontWindowSize) {
|
|
|
|
getHeap(heapIndex).initWithFrontWindow(base, size, frontWindowSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void heapInitFrontWindow(HeapIndex heapIndex, uint64_t base, uint64_t size) {
|
|
|
|
getHeap(heapIndex).initFrontWindow(base, size);
|
|
|
|
}
|
|
|
|
|
2021-03-26 04:30:21 +08:00
|
|
|
MOCKABLE_VIRTUAL uint64_t heapAllocate(HeapIndex heapIndex, size_t &size) {
|
2019-03-01 23:14:28 +08:00
|
|
|
return getHeap(heapIndex).allocate(size);
|
|
|
|
}
|
|
|
|
|
2022-08-29 21:30:21 +08:00
|
|
|
MOCKABLE_VIRTUAL uint64_t heapAllocateWithCustomAlignment(HeapIndex heapIndex, size_t &size, size_t alignment) {
|
2021-05-11 17:23:27 +08:00
|
|
|
return getHeap(heapIndex).allocateWithCustomAlignment(size, alignment);
|
|
|
|
}
|
|
|
|
|
2021-03-26 04:30:21 +08:00
|
|
|
MOCKABLE_VIRTUAL void heapFree(HeapIndex heapIndex, uint64_t ptr, size_t size) {
|
2019-03-01 23:14:28 +08:00
|
|
|
getHeap(heapIndex).free(ptr, size);
|
|
|
|
}
|
|
|
|
|
2019-07-29 23:50:46 +08:00
|
|
|
MOCKABLE_VIRTUAL void freeGpuAddressRange(uint64_t ptr, size_t size);
|
2019-04-18 21:30:47 +08:00
|
|
|
|
2019-03-01 23:14:28 +08:00
|
|
|
uint64_t getHeapBase(HeapIndex heapIndex) {
|
|
|
|
return getHeap(heapIndex).getBase();
|
|
|
|
}
|
2019-04-04 20:22:13 +08:00
|
|
|
|
2019-03-18 17:06:01 +08:00
|
|
|
uint64_t getHeapLimit(HeapIndex heapIndex) {
|
2019-04-18 21:30:47 +08:00
|
|
|
return getHeap(heapIndex).getLimit();
|
2019-03-18 17:06:01 +08:00
|
|
|
}
|
2019-03-01 23:14:28 +08:00
|
|
|
|
2022-12-16 00:01:37 +08:00
|
|
|
uint64_t getHeapMinimalAddress(HeapIndex heapIndex);
|
2019-04-15 21:20:51 +08:00
|
|
|
|
2019-04-18 21:30:47 +08:00
|
|
|
bool isLimitedRange() { return getHeap(HeapIndex::HEAP_SVM).getSize() == 0ull; }
|
|
|
|
|
2021-03-24 19:37:13 +08:00
|
|
|
static constexpr uint64_t heapGranularity = MemoryConstants::pageSize64k;
|
|
|
|
static constexpr uint64_t heapGranularity2MB = 2 * MemoryConstants::megaByte;
|
2020-10-14 15:50:07 +08:00
|
|
|
static constexpr size_t externalFrontWindowPoolSize = 16 * MemoryConstants::megaByte;
|
|
|
|
static constexpr size_t internalFrontWindowPoolSize = 1 * MemoryConstants::megaByte;
|
2019-04-04 20:22:13 +08:00
|
|
|
|
2019-03-01 23:14:28 +08:00
|
|
|
static const std::array<HeapIndex, 4> heap32Names;
|
2021-04-20 02:58:10 +08:00
|
|
|
static const std::array<HeapIndex, 8> heapNonSvmNames;
|
2019-03-01 23:14:28 +08:00
|
|
|
|
|
|
|
protected:
|
2023-04-28 21:24:48 +08:00
|
|
|
bool initAdditionalRange(uint32_t cpuAddressWidth, uint64_t gpuAddressSpace, uint64_t &gfxBase, uint64_t &gfxTop, uint32_t rootDeviceIndex, size_t numRootDevices, uint64_t systemMemorySize);
|
2019-09-10 20:59:45 +08:00
|
|
|
|
2019-03-01 23:14:28 +08:00
|
|
|
class Heap {
|
|
|
|
public:
|
|
|
|
Heap() = default;
|
2021-01-21 20:52:48 +08:00
|
|
|
void init(uint64_t base, uint64_t size, size_t allocationAlignment);
|
2020-09-14 21:14:11 +08:00
|
|
|
void initExternalWithFrontWindow(uint64_t base, uint64_t size);
|
2020-10-14 15:50:07 +08:00
|
|
|
void initWithFrontWindow(uint64_t base, uint64_t size, uint64_t frontWindowSize);
|
|
|
|
void initFrontWindow(uint64_t base, uint64_t size);
|
2019-03-01 23:14:28 +08:00
|
|
|
uint64_t getBase() const { return base; }
|
|
|
|
uint64_t getSize() const { return size; }
|
2020-01-08 19:36:09 +08:00
|
|
|
uint64_t getLimit() const { return size ? base + size - 1 : 0; }
|
2023-01-13 23:18:40 +08: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 23:14:28 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
uint64_t base = 0, size = 0;
|
|
|
|
std::unique_ptr<HeapAllocator> alloc;
|
|
|
|
};
|
|
|
|
|
|
|
|
Heap &getHeap(HeapIndex heapIndex) {
|
2019-04-18 21:30:47 +08:00
|
|
|
return heaps[static_cast<uint32_t>(heapIndex)];
|
2019-03-01 23:14:28 +08:00
|
|
|
}
|
|
|
|
|
2019-04-18 21:30:47 +08:00
|
|
|
std::array<Heap, static_cast<uint32_t>(HeapIndex::TOTAL_HEAPS)> heaps;
|
|
|
|
|
2023-03-22 22:32:27 +08:00
|
|
|
OSMemory::ReservedCpuAddressRange &reservedCpuAddressRangeForHeapSvm;
|
|
|
|
OSMemory::ReservedCpuAddressRange reservedCpuAddressRangeForHeapExtended{};
|
2019-07-30 22:46:58 +08:00
|
|
|
std::unique_ptr<OSMemory> osMemory;
|
2019-03-01 23:14:28 +08:00
|
|
|
};
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|