2018-09-11 20:45:43 +08:00
|
|
|
/*
|
2023-12-04 22:23:15 +08:00
|
|
|
* Copyright (C) 2018-2023 Intel Corporation
|
2018-09-11 20:45:43 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2018-09-11 20:45:43 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/helpers/aligned_memory.h"
|
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/helpers/debug_helpers.h"
|
2020-10-15 01:22:01 +08:00
|
|
|
#include "shared/source/memory_manager/memory_banks.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2018-09-11 20:45:43 +08:00
|
|
|
#include <atomic>
|
2018-09-29 05:36:11 +08:00
|
|
|
#include <mutex>
|
2018-09-11 20:45:43 +08:00
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2018-09-11 20:45:43 +08:00
|
|
|
|
|
|
|
class PhysicalAddressAllocator {
|
|
|
|
public:
|
|
|
|
PhysicalAddressAllocator() {
|
2018-09-21 04:54:19 +08:00
|
|
|
mainAllocator.store(initialPageAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~PhysicalAddressAllocator() = default;
|
2018-09-11 20:45:43 +08:00
|
|
|
|
2018-09-29 05:36:11 +08:00
|
|
|
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) {
|
2023-12-04 22:23:15 +08:00
|
|
|
UNRECOVERABLE_IF(memoryBank != MemoryBanks::mainBank);
|
2018-09-29 05:36:11 +08:00
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(pageReserveMutex);
|
|
|
|
|
|
|
|
auto currentAddress = mainAllocator.load();
|
|
|
|
auto alignmentSize = alignUp(currentAddress, alignement) - currentAddress;
|
|
|
|
mainAllocator += alignmentSize;
|
|
|
|
return mainAllocator.fetch_add(pageSize);
|
2018-09-11 20:45:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-09-21 04:54:19 +08:00
|
|
|
std::atomic<uint64_t> mainAllocator;
|
2018-09-29 05:36:11 +08:00
|
|
|
std::mutex pageReserveMutex;
|
2018-09-11 20:45:43 +08:00
|
|
|
const uint64_t initialPageAddress = 0x1000;
|
|
|
|
};
|
|
|
|
|
2021-04-24 00:43:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 01:40:30 +08:00
|
|
|
~PhysicalAddressAllocatorHw() override {
|
2021-04-24 00:43:48 +08:00
|
|
|
if (bankAllocators) {
|
2022-05-10 01:40:30 +08:00
|
|
|
delete[] bankAllocators;
|
2021-04-24 00:43:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t reservePage(uint32_t memoryBank, size_t pageSize, size_t alignement) override {
|
|
|
|
std::unique_lock<std::mutex> lock(pageReserveMutex);
|
|
|
|
|
2023-12-04 22:23:15 +08:00
|
|
|
if (memoryBank == MemoryBanks::mainBank || numberOfBanks == 0) {
|
2021-04-24 00:43:48 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|