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
|
/*
* Copyright (C) 2018-2022 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include "shared/source/helpers/aligned_memory.h"
#include "shared/source/helpers/constants.h"
#include "shared/source/helpers/debug_helpers.h"
#include "shared/source/memory_manager/memory_banks.h"
#include <atomic>
#include <mutex>
namespace NEO {
class PhysicalAddressAllocator {
public:
PhysicalAddressAllocator() {
mainAllocator.store(initialPageAddress);
}
virtual ~PhysicalAddressAllocator() = default;
uint64_t reserve4kPage(uint32_t memoryBank) {
return reservePage(memoryBank, MemoryConstants::pageSize, MemoryConstants::pageSize);
}
uint64_t reserve64kPage(uint32_t memoryBank) {
return reservePage(memoryBank, MemoryConstants::pageSize64k, MemoryConstants::pageSize64k);
}
virtual uint64_t reservePage(uint32_t memoryBank, size_t pageSize, size_t alignement) {
UNRECOVERABLE_IF(memoryBank != MemoryBanks::MainBank);
std::unique_lock<std::mutex> lock(pageReserveMutex);
auto currentAddress = mainAllocator.load();
auto alignmentSize = alignUp(currentAddress, alignement) - currentAddress;
mainAllocator += alignmentSize;
return mainAllocator.fetch_add(pageSize);
}
protected:
std::atomic<uint64_t> mainAllocator;
std::mutex pageReserveMutex;
const uint64_t initialPageAddress = 0x1000;
};
template <typename GfxFamily>
class PhysicalAddressAllocatorHw : public PhysicalAddressAllocator {
public:
PhysicalAddressAllocatorHw(uint64_t bankSize, uint32_t numOfBanks) : memoryBankSize(bankSize), numberOfBanks(numOfBanks) {
if (numberOfBanks > 0) {
bankAllocators = new std::atomic<uint64_t>[numberOfBanks];
bankAllocators[0].store(initialPageAddress);
for (uint32_t i = 1; i < numberOfBanks; i++) {
bankAllocators[i].store(i * memoryBankSize);
}
}
}
~PhysicalAddressAllocatorHw() override {
if (bankAllocators) {
delete[] bankAllocators;
}
}
uint64_t reservePage(uint32_t memoryBank, size_t pageSize, size_t alignement) override {
std::unique_lock<std::mutex> lock(pageReserveMutex);
if (memoryBank == MemoryBanks::MainBank || numberOfBanks == 0) {
auto currentAddress = mainAllocator.load();
auto alignmentSize = alignUp(currentAddress, alignement) - currentAddress;
mainAllocator += alignmentSize;
return mainAllocator.fetch_add(pageSize);
}
UNRECOVERABLE_IF(memoryBank > numberOfBanks);
auto index = memoryBank - MemoryBanks::getBankForLocalMemory(0);
auto currentAddress = bankAllocators[index].load();
auto alignmentSize = alignUp(currentAddress, alignement) - currentAddress;
bankAllocators[index] += alignmentSize;
auto address = bankAllocators[index].fetch_add(pageSize);
UNRECOVERABLE_IF(address > ((index + 1) * memoryBankSize));
return address;
}
uint64_t getBankSize() { return memoryBankSize; }
uint32_t getNumberOfBanks() { return numberOfBanks; }
protected:
std::atomic<uint64_t> *bankAllocators = nullptr;
uint64_t memoryBankSize = 0;
uint32_t numberOfBanks = 0;
};
} // namespace NEO
|