2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2018-01-08 22:58:02 +08:00
|
|
|
* Copyright (c) 2018, Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-01-08 22:58:02 +08:00
|
|
|
#include "runtime/built_ins/built_ins.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/command_stream/command_stream_receiver.h"
|
2018-01-08 22:58:02 +08:00
|
|
|
#include "runtime/command_stream/preemption.h"
|
|
|
|
#include "runtime/device/device.h"
|
2018-02-08 23:00:20 +08:00
|
|
|
#include "runtime/gtpin/gtpin_notify.h"
|
2017-12-21 07:45:38 +08:00
|
|
|
#include "runtime/memory_manager/memory_manager.h"
|
|
|
|
#include "runtime/helpers/cache_policy.h"
|
|
|
|
#include "runtime/os_interface/os_interface.h"
|
|
|
|
#include "runtime/event/event.h"
|
|
|
|
#include "runtime/event/event_builder.h"
|
|
|
|
|
|
|
|
namespace OCLRT {
|
|
|
|
// Global table of CommandStreamReceiver factories for HW and tests
|
|
|
|
CommandStreamReceiverCreateFunc commandStreamReceiverFactory[2 * IGFX_MAX_CORE] = {};
|
|
|
|
|
|
|
|
CommandStreamReceiver::CommandStreamReceiver() {
|
|
|
|
latestSentStatelessMocsConfig = CacheSettings::unknownMocs;
|
|
|
|
submissionAggregator.reset(new SubmissionAggregator());
|
|
|
|
if (DebugManager.flags.CsrDispatchMode.get()) {
|
|
|
|
this->dispatchMode = (DispatchMode)DebugManager.flags.CsrDispatchMode.get();
|
|
|
|
}
|
|
|
|
flushStamp.reset(new FlushStampTracker(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandStreamReceiver::~CommandStreamReceiver() {
|
|
|
|
cleanupResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandStreamReceiver::makeResident(GraphicsAllocation &gfxAllocation) {
|
|
|
|
auto submissionTaskCount = this->taskCount + 1;
|
|
|
|
if (gfxAllocation.residencyTaskCount < (int)submissionTaskCount) {
|
|
|
|
getMemoryManager()->pushAllocationForResidency(&gfxAllocation);
|
|
|
|
gfxAllocation.taskCount = submissionTaskCount;
|
|
|
|
if (gfxAllocation.residencyTaskCount == ObjectNotResident) {
|
|
|
|
this->totalMemoryUsed += gfxAllocation.getUnderlyingBufferSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gfxAllocation.residencyTaskCount = submissionTaskCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandStreamReceiver::processEviction() {
|
|
|
|
getMemoryManager()->clearEvictionAllocations();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandStreamReceiver::makeNonResident(GraphicsAllocation &gfxAllocation) {
|
|
|
|
if (gfxAllocation.residencyTaskCount != ObjectNotResident) {
|
|
|
|
makeCoherent(gfxAllocation.getUnderlyingBuffer(), gfxAllocation.getUnderlyingBufferSize());
|
|
|
|
getMemoryManager()->pushAllocationForEviction(&gfxAllocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxAllocation.residencyTaskCount = ObjectNotResident;
|
|
|
|
}
|
|
|
|
|
2018-01-11 05:03:23 +08:00
|
|
|
void CommandStreamReceiver::makeSurfacePackNonResident(ResidencyContainer *allocationsForResidency) {
|
|
|
|
auto &residencyAllocations = allocationsForResidency ? *allocationsForResidency : this->getMemoryManager()->getResidencyAllocations();
|
|
|
|
for (auto &surface : residencyAllocations) {
|
2017-12-21 07:45:38 +08:00
|
|
|
this->makeNonResident(*surface);
|
|
|
|
}
|
2018-01-11 05:03:23 +08:00
|
|
|
if (allocationsForResidency) {
|
|
|
|
residencyAllocations.clear();
|
|
|
|
} else {
|
|
|
|
this->getMemoryManager()->clearResidencyAllocations();
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
this->processEviction();
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphicsAllocation *CommandStreamReceiver::createAllocationAndHandleResidency(const void *address, size_t size, bool addToDefferedDeleteList) {
|
|
|
|
GraphicsAllocation *graphicsAllocation = getMemoryManager()->allocateGraphicsMemory(size, address);
|
|
|
|
makeResident(*graphicsAllocation);
|
|
|
|
if (addToDefferedDeleteList) {
|
|
|
|
getMemoryManager()->storeAllocation(std::unique_ptr<GraphicsAllocation>(graphicsAllocation), TEMPORARY_ALLOCATION);
|
|
|
|
}
|
|
|
|
if (!graphicsAllocation->isL3Capable()) {
|
|
|
|
disableL3Cache = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return graphicsAllocation;
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:52:58 +08:00
|
|
|
void CommandStreamReceiver::makeResidentHostPtrAllocation(GraphicsAllocation *gfxAllocation) {
|
|
|
|
makeResident(*gfxAllocation);
|
|
|
|
if (!gfxAllocation->isL3Capable()) {
|
|
|
|
setDisableL3Cache(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 23:41:45 +08:00
|
|
|
void CommandStreamReceiver::waitForTaskCountAndCleanAllocationList(uint32_t requiredTaskCount, uint32_t allocationType) {
|
2017-12-21 07:45:38 +08:00
|
|
|
|
|
|
|
auto address = getTagAddress();
|
|
|
|
if (address && requiredTaskCount != (unsigned int)-1) {
|
|
|
|
while (*address < requiredTaskCount)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMemoryManager()->cleanAllocationList(requiredTaskCount, allocationType);
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryManager *CommandStreamReceiver::getMemoryManager() {
|
|
|
|
return memoryManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandStreamReceiver::setMemoryManager(MemoryManager *mm) {
|
|
|
|
memoryManager = mm;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinearStream &CommandStreamReceiver::getCS(size_t minRequiredSize) {
|
|
|
|
auto memoryManager = this->getMemoryManager();
|
|
|
|
DEBUG_BREAK_IF(nullptr == memoryManager);
|
|
|
|
|
|
|
|
if (commandStream.getAvailableSpace() < minRequiredSize) {
|
|
|
|
// Make sure we have enough room for a MI_BATCH_BUFFER_END and any padding.
|
|
|
|
// Currently reserving 64bytes (cacheline) which should be more than enough.
|
|
|
|
static const size_t sizeForSubmission = MemoryConstants::cacheLineSize;
|
|
|
|
minRequiredSize += sizeForSubmission;
|
|
|
|
// If not, allocate a new block. allocate full pages
|
|
|
|
minRequiredSize = alignUp(minRequiredSize, MemoryConstants::pageSize);
|
|
|
|
|
|
|
|
auto requiredSize = minRequiredSize + CSRequirements::csOverfetchSize;
|
|
|
|
|
|
|
|
auto allocation = memoryManager->obtainReusableAllocation(requiredSize).release();
|
|
|
|
if (!allocation) {
|
|
|
|
allocation = memoryManager->allocateGraphicsMemory(requiredSize, MemoryConstants::pageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
//pass current allocation to reusable list
|
2018-03-05 18:03:38 +08:00
|
|
|
if (commandStream.getCpuBase()) {
|
2017-12-21 07:45:38 +08:00
|
|
|
memoryManager->storeAllocation(std::unique_ptr<GraphicsAllocation>(commandStream.getGraphicsAllocation()), REUSABLE_ALLOCATION);
|
|
|
|
}
|
|
|
|
|
|
|
|
commandStream.replaceBuffer(allocation->getUnderlyingBuffer(), minRequiredSize - sizeForSubmission);
|
|
|
|
commandStream.replaceGraphicsAllocation(allocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return commandStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandStreamReceiver::cleanupResources() {
|
|
|
|
auto memoryManager = this->getMemoryManager();
|
|
|
|
if (!memoryManager)
|
|
|
|
return;
|
|
|
|
|
2018-01-12 23:41:45 +08:00
|
|
|
waitForTaskCountAndCleanAllocationList(this->latestFlushedTaskCount, TEMPORARY_ALLOCATION);
|
|
|
|
waitForTaskCountAndCleanAllocationList(this->latestFlushedTaskCount, REUSABLE_ALLOCATION);
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
if (scratchAllocation) {
|
|
|
|
memoryManager->freeGraphicsMemory(scratchAllocation);
|
|
|
|
scratchAllocation = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-05 18:03:38 +08:00
|
|
|
if (commandStream.getCpuBase()) {
|
2017-12-21 07:45:38 +08:00
|
|
|
memoryManager->freeGraphicsMemory(commandStream.getGraphicsAllocation());
|
|
|
|
commandStream.replaceGraphicsAllocation(nullptr);
|
|
|
|
commandStream.replaceBuffer(nullptr, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 14:40:15 +08:00
|
|
|
bool CommandStreamReceiver::waitForCompletionWithTimeout(bool enableTimeout, int64_t timeoutMicroseconds, uint32_t taskCountToWait) {
|
2017-12-21 07:45:38 +08:00
|
|
|
std::chrono::high_resolution_clock::time_point time1, time2;
|
|
|
|
int64_t timeDiff = 0;
|
|
|
|
|
|
|
|
uint32_t latestSentTaskCount = this->latestFlushedTaskCount;
|
|
|
|
if (latestSentTaskCount < taskCountToWait) {
|
|
|
|
this->flushBatchedSubmissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
time1 = std::chrono::high_resolution_clock::now();
|
2018-02-27 14:40:15 +08:00
|
|
|
while (*getTagAddress() < taskCountToWait && timeDiff <= timeoutMicroseconds) {
|
2017-12-21 07:45:38 +08:00
|
|
|
if (enableTimeout) {
|
|
|
|
time2 = std::chrono::high_resolution_clock::now();
|
2018-02-27 14:40:15 +08:00
|
|
|
timeDiff = std::chrono::duration_cast<std::chrono::microseconds>(time2 - time1).count();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*getTagAddress() >= taskCountToWait) {
|
2018-03-23 01:46:09 +08:00
|
|
|
if (gtpinIsGTPinInitialized()) {
|
|
|
|
gtpinNotifyTaskCompletion(taskCountToWait);
|
|
|
|
}
|
2017-12-21 07:45:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandStreamReceiver::setTagAllocation(GraphicsAllocation *allocation) {
|
|
|
|
this->tagAllocation = allocation;
|
2018-01-12 23:41:45 +08:00
|
|
|
this->tagAddress = allocation ? reinterpret_cast<uint32_t *>(allocation->getUnderlyingBuffer()) : nullptr;
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CommandStreamReceiver::setRequiredScratchSize(uint32_t newRequiredScratchSize) {
|
|
|
|
if (newRequiredScratchSize > requiredScratchSize) {
|
|
|
|
requiredScratchSize = newRequiredScratchSize;
|
|
|
|
}
|
|
|
|
}
|
2018-01-08 22:58:02 +08:00
|
|
|
|
|
|
|
size_t CommandStreamReceiver::getInstructionHeapCmdStreamReceiverReservedSize() const {
|
|
|
|
return PreemptionHelper::getInstructionHeapSipKernelReservedSize(*memoryManager->device);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CommandStreamReceiver::initializeInstructionHeapCmdStreamReceiverReservedBlock(LinearStream &ih) const {
|
|
|
|
return PreemptionHelper::initializeInstructionHeapSipKernelReservedBlock(ih, *memoryManager->device);
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
} // namespace OCLRT
|