1 /*
2  * Copyright (C) 2020-2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #pragma once
9 
10 #include "shared/source/kernel/dispatch_kernel_encoder_interface.h"
11 #include "shared/source/kernel/kernel_descriptor.h"
12 #include "shared/source/memory_manager/graphics_allocation.h"
13 #include "shared/source/unified_memory/unified_memory.h"
14 
15 #include <level_zero/ze_api.h>
16 #include <level_zero/zet_api.h>
17 
18 #include <memory>
19 #include <vector>
20 
21 struct _ze_kernel_handle_t {};
22 
23 namespace NEO {
24 class Device;
25 struct KernelInfo;
26 class MemoryManager;
27 } // namespace NEO
28 
29 namespace L0 {
30 struct Device;
31 struct Module;
32 
33 struct KernelImmutableData {
34     KernelImmutableData(L0::Device *l0device = nullptr);
35     virtual ~KernelImmutableData();
36 
37     void initialize(NEO::KernelInfo *kernelInfo, Device *device,
38                     uint32_t computeUnitsUsedForSratch,
39                     NEO::GraphicsAllocation *globalConstBuffer, NEO::GraphicsAllocation *globalVarBuffer, bool internalKernel);
40 
getResidencyContainerKernelImmutableData41     const std::vector<NEO::GraphicsAllocation *> &getResidencyContainer() const {
42         return residencyContainer;
43     }
44 
getResidencyContainerKernelImmutableData45     std::vector<NEO::GraphicsAllocation *> &getResidencyContainer() {
46         return residencyContainer;
47     }
48 
49     uint32_t getIsaSize() const;
getIsaGraphicsAllocationKernelImmutableData50     NEO::GraphicsAllocation *getIsaGraphicsAllocation() const { return isaGraphicsAllocation.get(); }
51 
getCrossThreadDataTemplateKernelImmutableData52     const uint8_t *getCrossThreadDataTemplate() const { return crossThreadDataTemplate.get(); }
53 
getSurfaceStateHeapSizeKernelImmutableData54     uint32_t getSurfaceStateHeapSize() const { return surfaceStateHeapSize; }
getSurfaceStateHeapTemplateKernelImmutableData55     const uint8_t *getSurfaceStateHeapTemplate() const { return surfaceStateHeapTemplate.get(); }
56 
getDynamicStateHeapDataSizeKernelImmutableData57     uint32_t getDynamicStateHeapDataSize() const { return dynamicStateHeapSize; }
getDynamicStateHeapTemplateKernelImmutableData58     const uint8_t *getDynamicStateHeapTemplate() const { return dynamicStateHeapTemplate.get(); }
59 
getDescriptorKernelImmutableData60     const NEO::KernelDescriptor &getDescriptor() const { return *kernelDescriptor; }
61 
getDeviceKernelImmutableData62     Device *getDevice() { return this->device; }
63 
getKernelInfoKernelImmutableData64     const NEO::KernelInfo *getKernelInfo() const { return kernelInfo; }
65 
setIsaCopiedToAllocationKernelImmutableData66     void setIsaCopiedToAllocation() {
67         isaCopiedToAllocation = true;
68     }
69 
isIsaCopiedToAllocationKernelImmutableData70     bool isIsaCopiedToAllocation() const {
71         return isaCopiedToAllocation;
72     }
73 
74   protected:
75     MOCKABLE_VIRTUAL void createRelocatedDebugData(NEO::GraphicsAllocation *globalConstBuffer,
76                                                    NEO::GraphicsAllocation *globalVarBuffer);
77 
78     Device *device = nullptr;
79     NEO::KernelInfo *kernelInfo = nullptr;
80     NEO::KernelDescriptor *kernelDescriptor = nullptr;
81     std::unique_ptr<NEO::GraphicsAllocation> isaGraphicsAllocation = nullptr;
82 
83     uint32_t crossThreadDataSize = 0;
84     std::unique_ptr<uint8_t[]> crossThreadDataTemplate = nullptr;
85 
86     uint32_t surfaceStateHeapSize = 0;
87     std::unique_ptr<uint8_t[]> surfaceStateHeapTemplate = nullptr;
88 
89     uint32_t dynamicStateHeapSize = 0;
90     std::unique_ptr<uint8_t[]> dynamicStateHeapTemplate = nullptr;
91 
92     std::vector<NEO::GraphicsAllocation *> residencyContainer;
93 
94     bool isaCopiedToAllocation = false;
95 };
96 
97 struct Kernel : _ze_kernel_handle_t, virtual NEO::DispatchKernelEncoderI {
98     template <typename Type>
99     struct Allocator {
allocateKernel::Allocator100         static Kernel *allocate(Module *module) { return new Type(module); }
101     };
102 
103     static Kernel *create(uint32_t productFamily, Module *module,
104                           const ze_kernel_desc_t *desc, ze_result_t *ret);
105 
106     ~Kernel() override = default;
107 
108     virtual ze_result_t destroy() = 0;
109     virtual ze_result_t setIndirectAccess(ze_kernel_indirect_access_flags_t flags) = 0;
110     virtual ze_result_t getIndirectAccess(ze_kernel_indirect_access_flags_t *flags) = 0;
111     virtual ze_result_t getSourceAttributes(uint32_t *pSize, char **pString) = 0;
112     virtual ze_result_t getProperties(ze_kernel_properties_t *pKernelProperties) = 0;
113     virtual ze_result_t setArgumentValue(uint32_t argIndex, size_t argSize, const void *pArgValue) = 0;
114     virtual void setGroupCount(uint32_t groupCountX, uint32_t groupCountY, uint32_t groupCountZ) = 0;
115 
116     virtual ze_result_t setArgBufferWithAlloc(uint32_t argIndex, uintptr_t argVal, NEO::GraphicsAllocation *allocation) = 0;
117     virtual ze_result_t setArgRedescribedImage(uint32_t argIndex, ze_image_handle_t argVal) = 0;
118     virtual ze_result_t setGroupSize(uint32_t groupSizeX, uint32_t groupSizeY,
119                                      uint32_t groupSizeZ) = 0;
120     virtual ze_result_t suggestGroupSize(uint32_t globalSizeX, uint32_t globalSizeY,
121                                          uint32_t globalSizeZ, uint32_t *groupSizeX,
122                                          uint32_t *groupSizeY, uint32_t *groupSizeZ) = 0;
123     virtual ze_result_t getKernelName(size_t *pSize, char *pName) = 0;
124 
125     virtual uint32_t *getGlobalOffsets() = 0;
126     virtual ze_result_t setGlobalOffsetExp(uint32_t offsetX, uint32_t offsetY, uint32_t offsetZ) = 0;
127     virtual void patchGlobalOffset() = 0;
128 
129     virtual ze_result_t suggestMaxCooperativeGroupCount(uint32_t *totalGroupCount, NEO::EngineGroupType engineGroupType,
130                                                         bool isEngineInstanced) = 0;
131     virtual ze_result_t setCacheConfig(ze_cache_config_flags_t flags) = 0;
132 
133     virtual ze_result_t getProfileInfo(zet_profile_properties_t *pProfileProperties) = 0;
134 
135     virtual const KernelImmutableData *getImmutableData() const = 0;
136 
137     virtual const std::vector<NEO::GraphicsAllocation *> &getResidencyContainer() const = 0;
138 
139     virtual UnifiedMemoryControls getUnifiedMemoryControls() const = 0;
140     virtual bool hasIndirectAllocationsAllowed() const = 0;
141 
142     virtual NEO::GraphicsAllocation *getPrintfBufferAllocation() = 0;
143     virtual void printPrintfOutput() = 0;
144 
145     virtual bool usesSyncBuffer() = 0;
146     virtual void patchSyncBuffer(NEO::GraphicsAllocation *gfxAllocation, size_t bufferOffset) = 0;
147 
148     virtual NEO::GraphicsAllocation *allocatePrivateMemoryGraphicsAllocation() = 0;
149     virtual void patchCrossthreadDataWithPrivateAllocation(NEO::GraphicsAllocation *privateAllocation) = 0;
150 
151     virtual NEO::GraphicsAllocation *getPrivateMemoryGraphicsAllocation() = 0;
152 
153     virtual ze_result_t setSchedulingHintExp(ze_scheduling_hint_exp_desc_t *pHint) = 0;
154 
155     Kernel() = default;
156     Kernel(const Kernel &) = delete;
157     Kernel(Kernel &&) = delete;
158     Kernel &operator=(const Kernel &) = delete;
159     Kernel &operator=(Kernel &&) = delete;
160 
fromHandleKernel161     static Kernel *fromHandle(ze_kernel_handle_t handle) { return static_cast<Kernel *>(handle); }
162 
toHandleKernel163     inline ze_kernel_handle_t toHandle() { return this; }
164 };
165 
166 using KernelAllocatorFn = Kernel *(*)(Module *module);
167 extern KernelAllocatorFn kernelFactory[];
168 
169 template <uint32_t productFamily, typename KernelType>
170 struct KernelPopulateFactory {
KernelPopulateFactoryKernelPopulateFactory171     KernelPopulateFactory() {
172         kernelFactory[productFamily] = KernelType::template Allocator<KernelType>::allocate;
173     }
174 };
175 
176 } // namespace L0
177