2020-03-06 18:09:57 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019-2020 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-03-19 13:21:57 +08:00
|
|
|
#include "level_zero/core/source/fence/fence.h"
|
2020-03-06 18:09:57 +08:00
|
|
|
|
|
|
|
#include "shared/source/command_stream/command_stream_receiver.h"
|
2020-04-02 17:28:38 +08:00
|
|
|
#include "shared/source/helpers/constants.h"
|
2020-03-06 18:09:57 +08:00
|
|
|
#include "shared/source/memory_manager/memory_manager.h"
|
|
|
|
#include "shared/source/utilities/cpuintrinsics.h"
|
|
|
|
|
2020-03-07 07:52:16 +08:00
|
|
|
#include "hw_helpers.h"
|
|
|
|
|
2020-03-06 18:09:57 +08:00
|
|
|
namespace L0 {
|
|
|
|
|
|
|
|
Fence *Fence::create(CommandQueueImp *cmdQueue, const ze_fence_desc_t *desc) {
|
|
|
|
auto fence = new FenceImp(cmdQueue);
|
|
|
|
UNRECOVERABLE_IF(fence == nullptr);
|
|
|
|
|
|
|
|
fence->initialize();
|
|
|
|
|
|
|
|
return fence;
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:25:29 +08:00
|
|
|
FenceImp::~FenceImp() {
|
|
|
|
cmdQueue->getDevice()->getDriverHandle()->getMemoryManager()->freeGraphicsMemory(allocation);
|
|
|
|
allocation = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ze_result_t FenceImp::queryStatus() {
|
|
|
|
auto csr = cmdQueue->getCsr();
|
|
|
|
if (csr) {
|
|
|
|
csr->downloadAllocations();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto hostAddr = static_cast<uint64_t *>(allocation->getUnderlyingBuffer());
|
|
|
|
return *hostAddr == Fence::STATE_CLEARED ? ZE_RESULT_NOT_READY : ZE_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2020-03-06 18:09:57 +08:00
|
|
|
bool FenceImp::initialize() {
|
|
|
|
NEO::AllocationProperties properties(
|
2020-02-26 22:20:19 +08:00
|
|
|
cmdQueue->getDevice()->getRootDeviceIndex(), MemoryConstants::cacheLineSize, NEO::GraphicsAllocation::AllocationType::BUFFER_HOST_MEMORY);
|
|
|
|
properties.alignment = MemoryConstants::cacheLineSize;
|
2020-03-06 18:09:57 +08:00
|
|
|
allocation = cmdQueue->getDevice()->getDriverHandle()->getMemoryManager()->allocateGraphicsMemoryWithProperties(properties);
|
|
|
|
UNRECOVERABLE_IF(allocation == nullptr);
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ze_result_t FenceImp::reset() {
|
|
|
|
auto hostAddress = static_cast<uint64_t *>(allocation->getUnderlyingBuffer());
|
|
|
|
*(hostAddress) = Fence::STATE_CLEARED;
|
|
|
|
|
|
|
|
NEO::CpuIntrinsics::clFlush(hostAddress);
|
|
|
|
|
|
|
|
return ZE_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ze_result_t FenceImp::hostSynchronize(uint32_t timeout) {
|
|
|
|
std::chrono::high_resolution_clock::time_point time1, time2;
|
|
|
|
int64_t timeDiff = 0;
|
|
|
|
ze_result_t ret = ZE_RESULT_NOT_READY;
|
|
|
|
|
|
|
|
if (cmdQueue->getCsr()->getType() == NEO::CommandStreamReceiverType::CSR_AUB) {
|
|
|
|
return ZE_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout == 0) {
|
|
|
|
return queryStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
time1 = std::chrono::high_resolution_clock::now();
|
|
|
|
while (timeDiff < timeout) {
|
|
|
|
ret = queryStatus();
|
|
|
|
if (ret == ZE_RESULT_SUCCESS) {
|
|
|
|
return ZE_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::this_thread::yield();
|
|
|
|
NEO::CpuIntrinsics::pause();
|
|
|
|
|
|
|
|
if (timeout == std::numeric_limits<uint32_t>::max()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
time2 = std::chrono::high_resolution_clock::now();
|
|
|
|
timeDiff = std::chrono::duration_cast<std::chrono::nanoseconds>(time2 - time1).count();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace L0
|