2018-01-03 17:54:31 +08:00
|
|
|
/*
|
2020-01-17 22:47:31 +08:00
|
|
|
* Copyright (C) 2018-2020 Intel Corporation
|
2018-01-03 17:54:31 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2018-01-03 17:54:31 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/command_stream/command_stream_receiver.h"
|
|
|
|
#include "shared/source/memory_manager/surface.h"
|
|
|
|
#include "shared/source/utilities/spinlock.h"
|
2020-02-24 17:22:30 +08:00
|
|
|
|
2020-03-20 18:15:25 +08:00
|
|
|
#include "opencl/source/cl_device/cl_device.h"
|
2020-02-23 05:50:57 +08:00
|
|
|
#include "opencl/source/command_queue/command_queue.h"
|
|
|
|
#include "opencl/source/context/context.h"
|
|
|
|
#include "opencl/source/gtpin/gtpin_defs.h"
|
|
|
|
#include "opencl/source/gtpin/gtpin_hw_helper.h"
|
|
|
|
#include "opencl/source/gtpin/gtpin_notify.h"
|
|
|
|
#include "opencl/source/kernel/kernel.h"
|
|
|
|
#include "opencl/source/mem_obj/buffer.h"
|
|
|
|
#include "opencl/source/program/program.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
|
|
|
#include "CL/cl.h"
|
|
|
|
#include "ocl_igc_shared/gtpin/gtpin_ocl_interface.h"
|
|
|
|
|
2018-02-08 23:00:20 +08:00
|
|
|
#include <deque>
|
|
|
|
#include <vector>
|
2018-01-03 17:54:31 +08:00
|
|
|
|
|
|
|
using namespace gtpin;
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2018-02-08 23:00:20 +08:00
|
|
|
|
2018-01-12 22:00:36 +08:00
|
|
|
extern gtpin::ocl::gtpin_events_t GTPinCallbacks;
|
2018-01-03 17:54:31 +08:00
|
|
|
|
2018-08-06 19:19:32 +08:00
|
|
|
igc_init_t *pIgcInit = nullptr;
|
2018-02-08 23:00:20 +08:00
|
|
|
std::atomic<int> sequenceCount(1);
|
|
|
|
CommandQueue *pCmdQueueForFlushTask = nullptr;
|
|
|
|
std::deque<gtpinkexec_t> kernelExecQueue;
|
2018-10-16 21:53:39 +08:00
|
|
|
SpinLock kernelExecQueueLock;
|
2018-01-03 17:54:31 +08:00
|
|
|
|
|
|
|
void gtpinNotifyContextCreate(cl_context context) {
|
|
|
|
if (isGTPinInitialized) {
|
|
|
|
platform_info_t gtpinPlatformInfo;
|
2020-01-17 22:47:31 +08:00
|
|
|
auto pContext = castToObjectOrAbort<Context>(context);
|
|
|
|
auto pDevice = pContext->getDevice(0);
|
|
|
|
UNRECOVERABLE_IF(pDevice == nullptr);
|
2019-05-08 22:00:24 +08:00
|
|
|
GFXCORE_FAMILY genFamily = pDevice->getHardwareInfo().platform.eRenderCoreFamily;
|
2018-01-03 17:54:31 +08:00
|
|
|
GTPinHwHelper >pinHelper = GTPinHwHelper::get(genFamily);
|
|
|
|
gtpinPlatformInfo.gen_version = (gtpin::GTPIN_GEN_VERSION)gtpinHelper.getGenVersion();
|
2019-05-08 22:00:24 +08:00
|
|
|
gtpinPlatformInfo.device_id = static_cast<uint32_t>(pDevice->getHardwareInfo().platform.usDeviceID);
|
2018-08-06 19:19:32 +08:00
|
|
|
(*GTPinCallbacks.onContextCreate)((context_handle_t)context, >pinPlatformInfo, &pIgcInit);
|
2018-01-03 17:54:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gtpinNotifyContextDestroy(cl_context context) {
|
|
|
|
if (isGTPinInitialized) {
|
|
|
|
(*GTPinCallbacks.onContextDestroy)((context_handle_t)context);
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 20:26:46 +08:00
|
|
|
|
|
|
|
void gtpinNotifyKernelCreate(cl_kernel kernel) {
|
2020-01-12 01:25:26 +08:00
|
|
|
if (nullptr == kernel) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-24 20:26:46 +08:00
|
|
|
if (isGTPinInitialized) {
|
2018-02-08 23:00:20 +08:00
|
|
|
auto pKernel = castToObjectOrAbort<Kernel>(kernel);
|
|
|
|
size_t gtpinBTI = pKernel->getNumberOfBindingTableStates();
|
|
|
|
// Enlarge local copy of SSH by 1 SS
|
2020-01-17 22:47:31 +08:00
|
|
|
auto &device = pKernel->getDevice();
|
|
|
|
GFXCORE_FAMILY genFamily = device.getHardwareInfo().platform.eRenderCoreFamily;
|
2018-02-08 23:00:20 +08:00
|
|
|
GTPinHwHelper >pinHelper = GTPinHwHelper::get(genFamily);
|
2020-08-27 22:11:44 +08:00
|
|
|
if (pKernel->isParentKernel || !gtpinHelper.addSurfaceState(pKernel)) {
|
2018-02-08 23:00:20 +08:00
|
|
|
// Kernel with no SSH or Kernel EM, not supported
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (pKernel->isKernelHeapSubstituted()) {
|
|
|
|
// ISA for this kernel was already substituted
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Notify GT-Pin that new kernel was created
|
2018-01-24 20:26:46 +08:00
|
|
|
Context *pContext = &(pKernel->getContext());
|
|
|
|
cl_context context = (cl_context)pContext;
|
2018-02-08 23:00:20 +08:00
|
|
|
auto &kernelInfo = pKernel->getKernelInfo();
|
2018-08-06 19:19:32 +08:00
|
|
|
instrument_params_in_t paramsIn = {};
|
|
|
|
|
2018-01-24 20:26:46 +08:00
|
|
|
paramsIn.kernel_type = GTPIN_KERNEL_TYPE_CS;
|
2018-02-08 23:00:20 +08:00
|
|
|
paramsIn.simd = (GTPIN_SIMD_WIDTH)kernelInfo.getMaxSimdSize();
|
2018-01-24 20:26:46 +08:00
|
|
|
paramsIn.orig_kernel_binary = (uint8_t *)pKernel->getKernelHeap();
|
|
|
|
paramsIn.orig_kernel_size = static_cast<uint32_t>(pKernel->getKernelHeapSize());
|
|
|
|
paramsIn.buffer_type = GTPIN_BUFFER_BINDFULL;
|
2018-02-08 23:00:20 +08:00
|
|
|
paramsIn.buffer_desc.BTI = static_cast<uint32_t>(gtpinBTI);
|
2020-05-26 15:36:04 +08:00
|
|
|
paramsIn.igc_hash_id = kernelInfo.shaderHashCode;
|
2020-10-20 02:36:05 +08:00
|
|
|
paramsIn.kernel_name = (char *)kernelInfo.kernelDescriptor.kernelMetadata.kernelName.c_str();
|
2018-11-23 17:32:56 +08:00
|
|
|
paramsIn.igc_info = kernelInfo.igcInfoForGtpin;
|
2018-08-06 19:19:32 +08:00
|
|
|
paramsIn.debug_data = pKernel->getProgram()->getDebugData();
|
|
|
|
paramsIn.debug_data_size = static_cast<uint32_t>(pKernel->getProgram()->getDebugDataSize());
|
2018-01-24 20:26:46 +08:00
|
|
|
instrument_params_out_t paramsOut = {0};
|
|
|
|
(*GTPinCallbacks.onKernelCreate)((context_handle_t)(cl_context)context, ¶msIn, ¶msOut);
|
2018-02-08 23:00:20 +08:00
|
|
|
// Substitute ISA of created kernel with instrumented code
|
2018-01-24 20:26:46 +08:00
|
|
|
pKernel->substituteKernelHeap(paramsOut.inst_kernel_binary, paramsOut.inst_kernel_size);
|
|
|
|
pKernel->setKernelId(paramsOut.kernel_id);
|
|
|
|
}
|
|
|
|
}
|
2018-02-08 23:00:20 +08:00
|
|
|
|
|
|
|
void gtpinNotifyKernelSubmit(cl_kernel kernel, void *pCmdQueue) {
|
|
|
|
if (isGTPinInitialized) {
|
|
|
|
auto pKernel = castToObjectOrAbort<Kernel>(kernel);
|
2020-08-27 22:11:44 +08:00
|
|
|
if (pKernel->isParentKernel || pKernel->getSurfaceStateHeapSize() == 0) {
|
2018-02-08 23:00:20 +08:00
|
|
|
// Kernel with no SSH, not supported
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Context *pContext = &(pKernel->getContext());
|
|
|
|
cl_context context = (cl_context)pContext;
|
|
|
|
uint64_t kernelId = pKernel->getKernelId();
|
|
|
|
command_buffer_handle_t commandBuffer = (command_buffer_handle_t)((uintptr_t)(sequenceCount++));
|
|
|
|
uint32_t kernelOffset = 0;
|
|
|
|
resource_handle_t resource = 0;
|
|
|
|
// Notify GT-Pin that abstract "command buffer" was created
|
|
|
|
(*GTPinCallbacks.onCommandBufferCreate)((context_handle_t)context, commandBuffer);
|
|
|
|
// Notify GT-Pin that kernel was submited for execution
|
|
|
|
(*GTPinCallbacks.onKernelSubmit)(commandBuffer, kernelId, &kernelOffset, &resource);
|
|
|
|
// Create new record in Kernel Execution Queue describing submited kernel
|
2018-06-04 15:09:04 +08:00
|
|
|
pKernel->setStartOffset(kernelOffset);
|
2018-02-08 23:00:20 +08:00
|
|
|
gtpinkexec_t kExec;
|
|
|
|
kExec.pKernel = pKernel;
|
|
|
|
kExec.gtpinResource = (cl_mem)resource;
|
|
|
|
kExec.commandBuffer = commandBuffer;
|
|
|
|
kExec.pCommandQueue = (CommandQueue *)pCmdQueue;
|
2018-10-16 21:53:39 +08:00
|
|
|
std::unique_lock<SpinLock> lock{kernelExecQueueLock};
|
2018-02-08 23:00:20 +08:00
|
|
|
kernelExecQueue.push_back(kExec);
|
2018-10-16 21:53:39 +08:00
|
|
|
lock.unlock();
|
2018-02-08 23:00:20 +08:00
|
|
|
// Patch SSH[gtpinBTI] with GT-Pin resource
|
2018-05-28 14:32:47 +08:00
|
|
|
if (!resource) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-17 22:47:31 +08:00
|
|
|
auto &device = pKernel->getDevice();
|
|
|
|
GFXCORE_FAMILY genFamily = device.getHardwareInfo().platform.eRenderCoreFamily;
|
2018-02-08 23:00:20 +08:00
|
|
|
GTPinHwHelper >pinHelper = GTPinHwHelper::get(genFamily);
|
|
|
|
size_t gtpinBTI = pKernel->getNumberOfBindingTableStates() - 1;
|
|
|
|
void *pSurfaceState = gtpinHelper.getSurfaceState(pKernel, gtpinBTI);
|
|
|
|
cl_mem buffer = (cl_mem)resource;
|
|
|
|
auto pBuffer = castToObjectOrAbort<Buffer>(buffer);
|
2020-06-30 23:42:12 +08:00
|
|
|
pBuffer->setArgStateful(pSurfaceState, false, false, false, false, device.getDevice());
|
2018-02-08 23:00:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gtpinNotifyPreFlushTask(void *pCmdQueue) {
|
|
|
|
if (isGTPinInitialized) {
|
|
|
|
pCmdQueueForFlushTask = (CommandQueue *)pCmdQueue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gtpinNotifyFlushTask(uint32_t flushedTaskCount) {
|
|
|
|
if (isGTPinInitialized) {
|
2018-10-16 21:53:39 +08:00
|
|
|
std::unique_lock<SpinLock> lock{kernelExecQueueLock};
|
2018-02-08 23:00:20 +08:00
|
|
|
size_t numElems = kernelExecQueue.size();
|
|
|
|
for (size_t n = 0; n < numElems; n++) {
|
|
|
|
if ((kernelExecQueue[n].pCommandQueue == pCmdQueueForFlushTask) && !kernelExecQueue[n].isTaskCountValid) {
|
|
|
|
// Update record in Kernel Execution Queue with kernel's TC
|
|
|
|
kernelExecQueue[n].isTaskCountValid = true;
|
|
|
|
kernelExecQueue[n].taskCount = flushedTaskCount;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pCmdQueueForFlushTask = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gtpinNotifyTaskCompletion(uint32_t completedTaskCount) {
|
|
|
|
if (isGTPinInitialized) {
|
2018-10-16 21:53:39 +08:00
|
|
|
std::unique_lock<SpinLock> lock{kernelExecQueueLock};
|
2018-02-08 23:00:20 +08:00
|
|
|
size_t numElems = kernelExecQueue.size();
|
|
|
|
for (size_t n = 0; n < numElems;) {
|
|
|
|
if (kernelExecQueue[n].isTaskCountValid && (kernelExecQueue[n].taskCount <= completedTaskCount)) {
|
|
|
|
// Notify GT-Pin that execution of "command buffer" was completed
|
|
|
|
(*GTPinCallbacks.onCommandBufferComplete)(kernelExecQueue[n].commandBuffer);
|
|
|
|
// Remove kernel's record from Kernel Execution Queue
|
|
|
|
kernelExecQueue.erase(kernelExecQueue.begin() + n);
|
|
|
|
numElems--;
|
|
|
|
} else {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gtpinNotifyMakeResident(void *pKernel, void *pCSR) {
|
|
|
|
if (isGTPinInitialized) {
|
2018-10-16 21:53:39 +08:00
|
|
|
std::unique_lock<SpinLock> lock{kernelExecQueueLock};
|
2018-02-08 23:00:20 +08:00
|
|
|
size_t numElems = kernelExecQueue.size();
|
|
|
|
for (size_t n = 0; n < numElems; n++) {
|
2018-07-18 16:56:11 +08:00
|
|
|
if ((kernelExecQueue[n].pKernel == pKernel) && !kernelExecQueue[n].isResourceResident && kernelExecQueue[n].gtpinResource) {
|
2018-02-08 23:00:20 +08:00
|
|
|
// It's time for kernel to make resident its GT-Pin resource
|
|
|
|
CommandStreamReceiver *pCommandStreamReceiver = reinterpret_cast<CommandStreamReceiver *>(pCSR);
|
|
|
|
cl_mem gtpinBuffer = kernelExecQueue[n].gtpinResource;
|
|
|
|
auto pBuffer = castToObjectOrAbort<Buffer>(gtpinBuffer);
|
2020-06-02 19:38:13 +08:00
|
|
|
GraphicsAllocation *pGfxAlloc = pBuffer->getGraphicsAllocation(pCommandStreamReceiver->getRootDeviceIndex());
|
2018-02-08 23:00:20 +08:00
|
|
|
pCommandStreamReceiver->makeResident(*pGfxAlloc);
|
|
|
|
kernelExecQueue[n].isResourceResident = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gtpinNotifyUpdateResidencyList(void *pKernel, void *pResVec) {
|
|
|
|
if (isGTPinInitialized) {
|
2018-10-16 21:53:39 +08:00
|
|
|
std::unique_lock<SpinLock> lock{kernelExecQueueLock};
|
2018-02-08 23:00:20 +08:00
|
|
|
size_t numElems = kernelExecQueue.size();
|
|
|
|
for (size_t n = 0; n < numElems; n++) {
|
2018-07-18 16:56:11 +08:00
|
|
|
if ((kernelExecQueue[n].pKernel == pKernel) && !kernelExecQueue[n].isResourceResident && kernelExecQueue[n].gtpinResource) {
|
2018-02-08 23:00:20 +08:00
|
|
|
// It's time for kernel to update its residency list with its GT-Pin resource
|
|
|
|
std::vector<Surface *> *pResidencyVector = (std::vector<Surface *> *)pResVec;
|
|
|
|
cl_mem gtpinBuffer = kernelExecQueue[n].gtpinResource;
|
|
|
|
auto pBuffer = castToObjectOrAbort<Buffer>(gtpinBuffer);
|
2020-06-02 19:38:13 +08:00
|
|
|
auto rootDeviceIndex = kernelExecQueue[n].pCommandQueue->getDevice().getRootDeviceIndex();
|
|
|
|
GraphicsAllocation *pGfxAlloc = pBuffer->getGraphicsAllocation(rootDeviceIndex);
|
2018-02-08 23:00:20 +08:00
|
|
|
GeneralSurface *pSurface = new GeneralSurface(pGfxAlloc);
|
|
|
|
pResidencyVector->push_back(pSurface);
|
|
|
|
kernelExecQueue[n].isResourceResident = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gtpinNotifyPlatformShutdown() {
|
|
|
|
if (isGTPinInitialized) {
|
|
|
|
// Clear Kernel Execution Queue
|
|
|
|
kernelExecQueue.clear();
|
|
|
|
}
|
|
|
|
}
|
2018-08-06 19:19:32 +08:00
|
|
|
void *gtpinGetIgcInit() {
|
|
|
|
return pIgcInit;
|
|
|
|
}
|
2020-03-23 20:41:48 +08:00
|
|
|
void gtpinSetIgcInit(void *pIgcInitPtr) {
|
|
|
|
pIgcInit = static_cast<igc_init_t *>(pIgcInitPtr);
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|