Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : /*!
12 : * \file kernel_event.h
13 : * \brief
14 : */
15 : #ifndef ASCENDC_KERNEL_EVENT_IMPL_H
16 : #define ASCENDC_KERNEL_EVENT_IMPL_H
17 :
18 : #include "kernel_macros.h"
19 : #include "kernel_log.h"
20 : #if defined(ASCENDC_CPU_DEBUG) && ASCENDC_CPU_DEBUG == 1
21 : #include <cstdint>
22 : #include "stub_def.h"
23 : #include "stub_fun.h"
24 : #endif
25 :
26 : namespace AscendC {
27 : enum class TPosition : uint8_t {
28 : GM,
29 : A1,
30 : A2,
31 : B1,
32 : B2,
33 : C1,
34 : C2,
35 : CO1,
36 : CO2,
37 : VECIN,
38 : VECOUT,
39 : VECCALC,
40 : LCM = VECCALC,
41 : SPM,
42 : SHM = SPM,
43 : TSCM,
44 : C2PIPE2GM,
45 : C2PIPE2LOCAL,
46 : MAX,
47 : };
48 :
49 : using QuePosition = TPosition;
50 : enum class Hardware : uint8_t { GM, UB, L1, L0A, L0B, L0C, BIAS, FIXBUF, MAX };
51 :
52 : enum class HardEvent : uint8_t {
53 : // src_dst
54 : MTE2_MTE1,
55 : MTE1_MTE2,
56 : MTE1_M,
57 : M_MTE1,
58 : MTE2_V,
59 : V_MTE2,
60 : MTE3_V,
61 : V_MTE3,
62 : M_V,
63 : V_M,
64 : V_V,
65 : MTE3_MTE1,
66 : MTE1_MTE3,
67 : MTE1_V,
68 : MTE2_M,
69 : M_MTE2,
70 : V_MTE1,
71 : M_FIX,
72 : FIX_M,
73 : MTE3_MTE2,
74 : MTE2_MTE3,
75 : S_V,
76 : V_S,
77 : S_MTE2,
78 : MTE2_S,
79 : S_MTE3,
80 : MTE3_S,
81 : MTE2_FIX,
82 : FIX_MTE2,
83 : FIX_S,
84 : M_S,
85 : FIX_MTE3,
86 : MTE1_FIX,
87 : FIX_MTE1,
88 : FIX_FIX,
89 : #if defined(__NPU_ARCH__) && \
90 : ((__NPU_ARCH__ == 2103) || (__NPU_ARCH__ == 3003) || (__NPU_ARCH__ == 3103) || (__NPU_ARCH__ == 3113))
91 : M_MTE3,
92 : MTE3_M,
93 : #endif
94 : MAX,
95 : };
96 :
97 : enum class HardEventAic : uint8_t {
98 : // src_dst
99 : MTE2_MTE1,
100 : MTE1_MTE2,
101 : MTE1_M,
102 : M_MTE1,
103 : MTE3_MTE1,
104 : MTE1_MTE3,
105 : MTE2_M,
106 : M_MTE2,
107 : M_FIX,
108 : FIX_M,
109 : MTE3_MTE2,
110 : MTE2_MTE3,
111 : S_MTE2,
112 : MTE2_S,
113 : S_MTE3,
114 : MTE3_S,
115 : MTE2_FIX,
116 : FIX_MTE2,
117 : FIX_S,
118 : M_S,
119 : FIX_MTE3,
120 : MTE1_FIX,
121 : FIX_MTE1,
122 : FIX_FIX,
123 : #if defined(__NPU_ARCH__) && \
124 : ((__NPU_ARCH__ == 2103) || (__NPU_ARCH__ == 3003) || (__NPU_ARCH__ == 3103) || (__NPU_ARCH__ == 3113))
125 : M_MTE3,
126 : MTE3_M,
127 : #endif
128 : MAX,
129 : };
130 :
131 : enum class HardEventAiv : uint8_t {
132 : // src_dst
133 : MTE2_V,
134 : V_MTE2,
135 : MTE3_V,
136 : V_MTE3,
137 : V_V,
138 : MTE3_MTE2,
139 : MTE2_MTE3,
140 : S_V,
141 : V_S,
142 : S_MTE2,
143 : MTE2_S,
144 : S_MTE3,
145 : MTE3_S,
146 : MAX,
147 : };
148 :
149 : enum class MemoryT : uint8_t { L1 = 0, L0A, L0B, L0C, UB, BIAS };
150 :
151 : enum class MemDsbT : uint8_t { ALL = 0, DDR, UB, SEQ };
152 :
153 : #if defined(__NPU_ARCH__) && (__NPU_ARCH__ != 1001) && (__NPU_ARCH__ != 2002)
154 : constexpr int32_t PIPE_NUM = 7;
155 : constexpr pipe_t SUPPORTED_PIPE[PIPE_NUM] = {PIPE_S, PIPE_V, PIPE_M, PIPE_MTE1, PIPE_MTE2, PIPE_MTE3, PIPE_FIX};
156 : #else
157 : constexpr int32_t PIPE_NUM = 6;
158 : constexpr pipe_t SUPPORTED_PIPE[PIPE_NUM] = {PIPE_S, PIPE_V, PIPE_M, PIPE_MTE1, PIPE_MTE2, PIPE_MTE3};
159 : #endif
160 :
161 : #if defined(__NPU_ARCH__)
162 : template <pipe_t pipe>
163 : __aicore__ inline constexpr bool IsSplitVectorPipe()
164 : {
165 : return pipe == PIPE_S || pipe == PIPE_V || pipe == PIPE_MTE2 || pipe == PIPE_MTE3;
166 : }
167 :
168 : template <pipe_t pipe>
169 : __aicore__ inline constexpr bool IsSplitCubePipe()
170 : {
171 : #if defined(__NPU_ARCH__) && (__NPU_ARCH__ == 1001 || __NPU_ARCH__ == 2002)
172 : return pipe == PIPE_S || pipe == PIPE_MTE1 || pipe == PIPE_MTE2 || pipe == PIPE_MTE3 || pipe == PIPE_M;
173 : #elif defined(__NPU_ARCH__) && (__NPU_ARCH__ == 3510)
174 : return pipe == PIPE_S || pipe == PIPE_MTE1 || pipe == PIPE_MTE2 || pipe == PIPE_FIX || pipe == PIPE_M;
175 : #else
176 : return pipe == PIPE_S || pipe == PIPE_MTE1 || pipe == PIPE_MTE2 || pipe == PIPE_MTE3 || pipe == PIPE_FIX ||
177 : pipe == PIPE_M;
178 : #endif
179 : }
180 :
181 : template <pipe_t pipe>
182 : __aicore__ inline void PipeBarrierInternal()
183 : {
184 : #if defined(__NPU_ARCH__) && (__NPU_ARCH__ == 3510)
185 : if constexpr (IsSplitVectorPipe<pipe>() || pipe == PIPE_ALL) {
186 : if ASCEND_IS_AIV {
187 : pipe_barrier(pipe);
188 : }
189 : }
190 : if constexpr (IsSplitCubePipe<pipe>() || pipe == PIPE_ALL) {
191 : if ASCEND_IS_AIC {
192 : pipe_barrier(pipe);
193 : }
194 : }
195 : #else
196 : pipe_barrier(pipe);
197 : #endif
198 : }
199 :
200 : template <pipe_t srcPipe, pipe_t dstPipe>
201 : __aicore__ inline void SetFlagInternal(event_t evt)
202 : {
203 : #if defined(__NPU_ARCH__) && (__NPU_ARCH__ == 3510)
204 : if constexpr (IsSplitVectorPipe<srcPipe>() && IsSplitVectorPipe<dstPipe>()) {
205 : if ASCEND_IS_AIV {
206 : set_flag(srcPipe, dstPipe, evt);
207 : }
208 : }
209 : if constexpr (IsSplitCubePipe<srcPipe>() && IsSplitCubePipe<dstPipe>()) {
210 : if ASCEND_IS_AIC {
211 : set_flag(srcPipe, dstPipe, evt);
212 : }
213 : }
214 : #else
215 : set_flag(srcPipe, dstPipe, evt);
216 : #endif
217 : }
218 :
219 : template <pipe_t srcPipe, pipe_t dstPipe>
220 : __aicore__ inline void WaitFlagInternal(event_t evt)
221 : {
222 : #if defined(__NPU_ARCH__) && (__NPU_ARCH__ == 3510)
223 : if constexpr (IsSplitVectorPipe<srcPipe>() && IsSplitVectorPipe<dstPipe>()) {
224 : if ASCEND_IS_AIV {
225 : wait_flag(srcPipe, dstPipe, evt);
226 : }
227 : }
228 : if constexpr (IsSplitCubePipe<srcPipe>() && IsSplitCubePipe<dstPipe>()) {
229 : if ASCEND_IS_AIC {
230 : wait_flag(srcPipe, dstPipe, evt);
231 : }
232 : }
233 : #else
234 : wait_flag(srcPipe, dstPipe, evt);
235 : #endif
236 : (void)evt;
237 : }
238 : #endif
239 :
240 3084 : __aicore__ constexpr Hardware GetPhyType(TPosition pos)
241 : {
242 3084 : ASSERT(pos != TPosition::MAX);
243 3084 : Hardware hard = Hardware::UB;
244 3084 : if (pos == TPosition::GM) {
245 4 : hard = Hardware::GM;
246 3080 : } else if (pos == TPosition::A1) {
247 332 : hard = Hardware::L1;
248 2748 : } else if (pos == TPosition::A2) {
249 64 : hard = Hardware::L0A;
250 2684 : } else if (pos == TPosition::B1) {
251 60 : hard = Hardware::L1;
252 2624 : } else if (pos == TPosition::B2) {
253 36 : hard = Hardware::L0B;
254 : #if defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 1001) || (__NPU_ARCH__ == 2002))
255 1294 : } else if (pos == TPosition::C1) {
256 0 : hard = Hardware::UB;
257 1294 : } else if (pos == TPosition::C2) {
258 0 : hard = Hardware::L0C;
259 1294 : } else if (pos == TPosition::CO2) {
260 0 : hard = Hardware::UB;
261 : #elif defined(__NPU_ARCH__) && (__NPU_ARCH__ == 2103)
262 : } else if (pos == TPosition::C1) {
263 : hard = Hardware::L1;
264 : } else if (pos == TPosition::C2) {
265 : hard = Hardware::BIAS;
266 : } else if (pos == TPosition::CO2) {
267 : hard = Hardware::L1;
268 : } else if (pos == TPosition::C2PIPE2GM) {
269 : hard = Hardware::FIXBUF;
270 : #elif (__NPU_ARCH__ == 2201)
271 1294 : } else if (pos == TPosition::C1) {
272 0 : hard = Hardware::L1;
273 1294 : } else if (pos == TPosition::C2) {
274 0 : hard = Hardware::BIAS;
275 1294 : } else if (pos == TPosition::CO2) {
276 0 : hard = Hardware::GM;
277 1294 : } else if (pos == TPosition::C2PIPE2GM) {
278 0 : hard = Hardware::FIXBUF;
279 : #elif (__NPU_ARCH__ == 3002)
280 0 : } else if (pos == TPosition::C1) {
281 0 : hard = Hardware::L1;
282 0 : } else if (pos == TPosition::C2) {
283 0 : hard = Hardware::BIAS;
284 0 : } else if (pos == TPosition::C2PIPE2GM) {
285 0 : hard = Hardware::FIXBUF;
286 : #elif (__NPU_ARCH__ == 3102)
287 : } else if (pos == TPosition::C1) {
288 : hard = Hardware::L1;
289 : } else if (pos == TPosition::C2) {
290 : hard = Hardware::BIAS;
291 : #elif defined(__NPU_ARCH__) && (__NPU_ARCH__ == 3103)
292 : } else if (pos == TPosition::C1) {
293 : hard = Hardware::L1;
294 : } else if (pos == TPosition::C2) {
295 : hard = Hardware::BIAS;
296 : } else if (pos == TPosition::CO2) {
297 : hard = Hardware::GM;
298 : } else if (pos == TPosition::C2PIPE2GM) {
299 : hard = Hardware::FIXBUF;
300 : #elif (__NPU_ARCH__ == 5102)
301 : } else if (pos == TPosition::C1) {
302 : hard = Hardware::L1;
303 : } else if (pos == TPosition::C2) {
304 : hard = Hardware::BIAS;
305 : } else if (pos == TPosition::CO2) {
306 : hard = Hardware::GM;
307 : #elif defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 3510) || (__NPU_ARCH__ == 3113))
308 : } else if (pos == TPosition::C1) {
309 : hard = Hardware::L1;
310 : } else if (pos == TPosition::C2) {
311 : hard = Hardware::BIAS;
312 : } else if (pos == TPosition::CO2) {
313 : hard = Hardware::GM;
314 : } else if (pos == TPosition::C2PIPE2GM) {
315 : hard = Hardware::FIXBUF;
316 : #endif
317 2588 : } else if (pos == TPosition::CO1) {
318 84 : hard = Hardware::L0C;
319 2504 : } else if (pos == TPosition::SHM) {
320 0 : hard = Hardware::L1;
321 2504 : } else if (pos == TPosition::TSCM) {
322 0 : hard = Hardware::L1;
323 : }
324 3084 : return hard;
325 : }
326 :
327 : __aicore__ constexpr TPosition GetPosition(TPosition srcPos, TPosition dstPos)
328 : {
329 : // unsupported data stream
330 : ASSERT(!((srcPos == TPosition::CO2) && (dstPos == TPosition::SHM)));
331 : ASSERT(!((srcPos == TPosition::VECOUT) && (dstPos == TPosition::SHM)));
332 : #if defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 1001) || (__NPU_ARCH__ == 2002))
333 : if (dstPos == TPosition::GM || ((dstPos == TPosition::CO2) && (srcPos == TPosition::CO1))) {
334 : return srcPos;
335 : }
336 : #elif defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 2201) || (__NPU_ARCH__ == 3002) || (__NPU_ARCH__ == 3102) || \
337 : (__NPU_ARCH__ == 3510) || (__NPU_ARCH__ == 5102) || (__NPU_ARCH__ == 2103) || \
338 : (__NPU_ARCH__ == 3003) || (__NPU_ARCH__ == 3103) || (__NPU_ARCH__ == 3113))
339 : if ((dstPos == TPosition::GM) || (dstPos == TPosition::CO2)) {
340 : return srcPos;
341 : }
342 : #endif
343 : return dstPos;
344 : }
345 :
346 : #if defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 3510) || (__NPU_ARCH__ == 5102))
347 : #ifdef ASCENDC_CPU_DEBUG
348 : class BufIdTracker {
349 : public:
350 : struct BufState {
351 : pipe_t pipe;
352 : bool mode;
353 : bool released;
354 : };
355 :
356 : static BufIdTracker& GetInstance()
357 : {
358 : static BufIdTracker tracker;
359 : return tracker;
360 : }
361 :
362 : bool AddBufIdEvent(uint8_t bufId, pipe_t pipe, bool mode, bool released);
363 : bool GetState();
364 : void Reset();
365 :
366 : private:
367 : BufIdTracker() {}
368 : bool UpdateState(uint8_t bufId, pipe_t pipe, bool mode, bool released);
369 :
370 : std::map<uint8_t, BufState> bufIdMapAiv_;
371 : std::map<uint8_t, BufState> bufIdMapAic_;
372 :
373 : bool aivState_ = true;
374 : bool aicState_ = true;
375 : };
376 : #endif
377 :
378 : using TBufId = uint8_t;
379 : constexpr TBufId MAX_TBUFID = (static_cast<TBufId>(31));
380 : #endif
381 :
382 : } // namespace AscendC
383 :
384 : #endif // ASCENDC_KERNEL_EVENT_IMPL_H
|