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 : #include "sqe_build_a5.h"
11 : #include "communicator_impl_lite_manager.h"
12 :
13 : namespace Hccl {
14 :
15 2 : u32 GetKernelExecTimeoutFromEnvConfig()
16 : {
17 2 : const u32 envTimeout = CommunicatorImplLiteMgr::GetInstance().GetEnvConfig().hcclExecTimeout;
18 2 : return envTimeout;
19 : }
20 :
21 : namespace {
22 1 : void ConstructLHWI(const Rt91095StarsCondIsaRegister_t dstReg, const u64 immd, Rt91095StarsCondOpLHWI_t& opLHWI)
23 : {
24 1 : opLHWI.opCode = static_cast<uint32_t>(Hccl::Rt91095StarsCondIsaOpCode_t::RT_91095_STARS_COND_ISA_OP_CODE_LWI);
25 1 : opLHWI.func3 = static_cast<uint32_t>(Rt91095StarsCondIsaLwiFunc3_t::RT_91095_STARS_COND_ISA_LWI_FUNC3_LHWI);
26 1 : opLHWI.rd = static_cast<uint32_t>(dstReg);
27 1 : opLHWI.immd = static_cast<uint32_t>((immd >> 49U) & 0x7FFFU); // High15-immd[63:49]
28 1 : }
29 :
30 1 : void ConstructLLWI(const Rt91095StarsCondIsaRegister_t dstReg, const u64 immd, Rt91095StarsCondOpLLWI_t& opLLWI)
31 : {
32 1 : opLLWI.opCode = static_cast<uint32_t>(Rt91095StarsCondIsaOpCode_t::RT_91095_STARS_COND_ISA_OP_CODE_LWI);
33 1 : opLLWI.func3 = static_cast<uint32_t>(Rt91095StarsCondIsaLwiFunc3_t::RT_91095_STARS_COND_ISA_LWI_FUNC3_LLWI);
34 1 : opLLWI.rd = static_cast<uint32_t>(dstReg);
35 1 : opLLWI.immdHigh = static_cast<uint32_t>((immd >> 32U) & 0x1FFFFU); // Low49-immd[48:32]
36 1 : opLLWI.immdLow = static_cast<uint32_t>(immd & 0xFFFFFFFFU); // Low49-immd[31:0]
37 1 : }
38 :
39 3 : void ConstructLoadImm(
40 : const Rt91095StarsCondIsaRegister_t dstReg, const u64 addr, const Rt91095StarsCondIsaLoadImmFunc3_t func3,
41 : Rt91095StarsCondOpLoadImm_t& loadImm)
42 : {
43 3 : loadImm.opCode = static_cast<uint32_t>(Rt91095StarsCondIsaOpCode_t::RT_91095_STARS_COND_ISA_OP_CODE_LOAD_IMM);
44 3 : loadImm.rd = static_cast<uint32_t>(dstReg);
45 3 : loadImm.func3 = static_cast<uint32_t>(func3);
46 3 : loadImm.immdAddrHigh = static_cast<uint32_t>((addr >> 32U) & 0X1FFFFU); // bit[48:32]
47 3 : loadImm.immdAddrLow = static_cast<uint32_t>(addr & 0xFFFFFFFFU); // bit[31:0]
48 3 : }
49 :
50 1 : void ConstructBranch(
51 : const Rt91095StarsCondIsaRegister_t rs1Reg, const Rt91095StarsCondIsaRegister_t rs2Reg,
52 : const Rt91095StarsCondIsaBranchFunc3_t func3, const uint8_t instrOffset, Rt91095StarsCondOpBranch_t& opBranch)
53 : {
54 1 : opBranch.opCode = static_cast<uint32_t>(Rt91095StarsCondIsaOpCode_t::RT_91095_STARS_COND_ISA_OP_CODE_BRANCH);
55 1 : opBranch.func3 = static_cast<uint32_t>(func3);
56 1 : opBranch.rs1 = static_cast<uint32_t>(rs1Reg);
57 1 : opBranch.rs2 = static_cast<uint32_t>(rs2Reg);
58 1 : opBranch.jumpInstrOffset = instrOffset & 0xFU; // Jump-immd[3:0]
59 1 : }
60 :
61 1 : void ConstructStore(
62 : const Rt91095StarsCondIsaRegister_t addrReg, const Rt91095StarsCondIsaRegister_t valReg,
63 : const uint16_t immdOffset, const Rt91095StarsCondIsaStoreFunc3_t func3, Rt91095StarsCondOpStore_t& opStore)
64 : {
65 1 : opStore.opCode = static_cast<uint32_t>(Rt91095StarsCondIsaOpCode_t::RT_91095_STARS_COND_ISA_OP_CODE_STORE);
66 1 : opStore.immdLow = static_cast<uint8_t>(immdOffset & 0x1FU); // S-immd[4:0]
67 1 : opStore.func3 = static_cast<uint32_t>(func3);
68 1 : opStore.rs1 = static_cast<uint32_t>(addrReg);
69 1 : opStore.rs2 = static_cast<uint32_t>(valReg);
70 1 : opStore.immdHigh = static_cast<uint8_t>((immdOffset & 0xFE0U) >> 5U); // S-immd[11:5]
71 1 : }
72 :
73 13 : void ConstructNop(Rt91095StarsCondOpNop_t& nop)
74 : {
75 13 : nop.opCode = static_cast<uint32_t>(Rt91095StarsCondIsaOpCode_t::RT_91095_STARS_COND_ISA_OP_CODE_NOP);
76 13 : nop.rd = static_cast<uint32_t>(Rt91095StarsCondIsaRegister_t::RT_91095_STARS_COND_ISA_REGISTER_R0);
77 13 : nop.func3 = static_cast<uint32_t>(Rt91095StarsCondIsaOpImmFunc3_t::RT_91095_STARS_COND_ISA_OP_IMM_FUNC3_NOP);
78 13 : nop.rs1 = static_cast<uint32_t>(Rt91095StarsCondIsaRegister_t::RT_91095_STARS_COND_ISA_REGISTER_R0);
79 13 : nop.immd = 0U;
80 13 : }
81 : } // namespace
82 :
83 1 : void BuildA5SqeCCoreNotifyWait(u32 streamId, u32 taskId, u64 waitAddr, u64 actAddr, bool last, uint8_t* const sqeIn)
84 : {
85 1 : Rt91095StarsCCoreSqeNotifyWait* sqe = (Rt91095StarsCCoreSqeNotifyWait*)sqeIn;
86 1 : sqe->header.type = static_cast<uint8_t>(Rt91095StarsSqeType::RT_91095_SQE_TYPE_COND);
87 1 : sqe->header.rtStreamId = static_cast<uint16_t>(taskId);
88 1 : sqe->header.taskId = static_cast<uint16_t>(taskId >> LOW_BITS);
89 :
90 1 : sqe->kernelCredit = RT_STARS_DEFAULT_KERNEL_CREDIT;
91 1 : sqe->csc = 1U;
92 :
93 1 : constexpr Rt91095StarsCondIsaRegister_t r0 = Rt91095StarsCondIsaRegister_t::RT_91095_STARS_COND_ISA_REGISTER_R0;
94 1 : constexpr Rt91095StarsCondIsaRegister_t r1 = Rt91095StarsCondIsaRegister_t::RT_91095_STARS_COND_ISA_REGISTER_R1;
95 1 : constexpr Rt91095StarsCondIsaRegister_t r2 = Rt91095StarsCondIsaRegister_t::RT_91095_STARS_COND_ISA_REGISTER_R2;
96 1 : constexpr Rt91095StarsCondIsaRegister_t r3 = Rt91095StarsCondIsaRegister_t::RT_91095_STARS_COND_ISA_REGISTER_R3;
97 :
98 : // load current Turn to r3
99 1 : ConstructLoadImm(
100 1 : r3, actAddr, Rt91095StarsCondIsaLoadImmFunc3_t::RT_91095_STARS_COND_ISA_LOAD_IMM_FUNC3_LHU, sqe->ldrImm1);
101 :
102 : // load sendcnt to r2
103 1 : ConstructLoadImm(
104 1 : r2, waitAddr, Rt91095StarsCondIsaLoadImmFunc3_t::RT_91095_STARS_COND_ISA_LOAD_IMM_FUNC3_LHU, sqe->ldrImm2);
105 1 : uint8_t loadInstrOff
106 : = (offsetof(Rt91095StarsCCoreSqeNotifyWait, ldrImm2) - offsetof(Rt91095StarsCCoreSqeNotifyWait, ldrImm1));
107 1 : loadInstrOff = loadInstrOff / sizeof(uint32_t);
108 :
109 : // r2(sendCnt) < r3(curTurn),goto reload r2
110 1 : ConstructBranch(
111 1 : r2, r3, Rt91095StarsCondIsaBranchFunc3_t::RT_91095_STARS_COND_ISA_BRANCH_FUNC3_BLTU, loadInstrOff, sqe->beq);
112 :
113 1 : if (last) {
114 : // load sendcount addr to r1
115 0 : ConstructLLWI(r1, waitAddr, sqe->clear.llwi1);
116 0 : ConstructLHWI(r1, waitAddr, sqe->clear.lhwi1);
117 : // the last turn clear sendCnt, r0(0) value store to r1(sendCnt),
118 0 : ConstructStore(
119 0 : r1, r0, 0U, Rt91095StarsCondIsaStoreFunc3_t::RT_91095_STARS_COND_ISA_STORE_FUNC3_SH, sqe->clear.sw);
120 0 : for (Rt91095StarsCondOpNop_t& nop : sqe->clear.nop) {
121 0 : ConstructNop(nop);
122 : }
123 : } else {
124 8 : for (Rt91095StarsCondOpNop_t& nop : sqe->nop) {
125 7 : ConstructNop(nop);
126 : }
127 : }
128 :
129 3 : HCCL_INFO(
130 : "[SQE]CCoreWait: waitAddr=%llu, actAddr=%llu, last=%u, streamId=%u, taskId=%u, "
131 : "ISA=%08x %08x %08x %08x %08x %08x %08x",
132 : waitAddr, actAddr, last, streamId, taskId, sqe->ldrImm1, sqe->ldrImm2, sqe->beq, sqe->clear.llwi1,
133 : sqe->clear.lhwi1, sqe->clear.sw, sqe->clear.nop[0]);
134 1 : }
135 :
136 1 : void BuildA5SqeCCoreNotifyRecord(u32 streamId, u32 taskId, u64 writeAddr, u64 valueAddr, uint8_t* const sqeIn)
137 : {
138 1 : Rt91095StarsCCoreSqeNotifyRecord* sqe = (Rt91095StarsCCoreSqeNotifyRecord*)sqeIn;
139 1 : sqe->header.type = static_cast<uint8_t>(Rt91095StarsSqeType::RT_91095_SQE_TYPE_COND);
140 1 : sqe->header.rtStreamId = static_cast<uint16_t>(taskId);
141 1 : sqe->header.taskId = static_cast<uint16_t>(taskId >> LOW_BITS);
142 :
143 1 : sqe->kernelCredit = RT_STARS_DEFAULT_KERNEL_CREDIT;
144 1 : sqe->csc = 1U;
145 :
146 1 : constexpr Rt91095StarsCondIsaRegister_t r1 = Rt91095StarsCondIsaRegister_t::RT_91095_STARS_COND_ISA_REGISTER_R1;
147 1 : constexpr Rt91095StarsCondIsaRegister_t r2 = Rt91095StarsCondIsaRegister_t::RT_91095_STARS_COND_ISA_REGISTER_R2;
148 :
149 1 : ConstructLoadImm(
150 1 : r1, valueAddr, Rt91095StarsCondIsaLoadImmFunc3_t::RT_91095_STARS_COND_ISA_LOAD_IMM_FUNC3_LHU, sqe->ldrImm);
151 1 : ConstructLLWI(r2, writeAddr, sqe->llwi1);
152 1 : ConstructLHWI(r2, writeAddr, sqe->lhwi1);
153 :
154 1 : ConstructStore(r2, r1, 0U, Rt91095StarsCondIsaStoreFunc3_t::RT_91095_STARS_COND_ISA_STORE_FUNC3_SH, sqe->sw);
155 7 : for (Rt91095StarsCondOpNop_t& nop : sqe->nop) {
156 6 : ConstructNop(nop);
157 : }
158 :
159 3 : HCCL_INFO(
160 : "[SQE]CCoreWrite: writeAddr=%p, valueAddr=%p, streamId=%u, taskId=%u, "
161 : "ISA=%08x %08x %08x %08x %08x",
162 : writeAddr, valueAddr, streamId, taskId, sqe->ldrImm, sqe->llwi1, sqe->lhwi1, sqe->sw, sqe->nop[0]);
163 1 : }
164 :
165 : // 写64bit值的形式来完成敲DB
166 6 : void BuildA5SqeRdmaDbSend(u32 streamId, u32 taskId, u64 dbAddr, u64 dbValue, uint8_t* const sqeIn)
167 : {
168 : (void)streamId;
169 6 : Rt91095StarsWriteValueSqe* sqe = reinterpret_cast<Rt91095StarsWriteValueSqe*>(sqeIn);
170 6 : SetSqeHeaderTaskFields(sqe, taskId);
171 6 : sqe->header.type = static_cast<uint8_t>(Rt91095StarsSqeType::RT_91095_SQE_TYPE_WRITE_VALUE);
172 :
173 6 : sqe->kernelCredit = RT_STARS_DEFAULT_KERNEL_CREDIT;
174 6 : sqe->header.rtStreamId = streamId;
175 6 : sqe->header.taskId = taskId;
176 :
177 6 : sqe->writeAddrLow = dbAddr & MASK_32_BIT;
178 6 : sqe->writeAddrHigh = (dbAddr >> UINT32_BIT_NUM) & MASK_17_BIT;
179 :
180 6 : sqe->awsize = RtStarsWriteValueSizeType::RT_STARS_WRITE_VALUE_SIZE_TYPE_64BIT; // writeValue 为 8 byte
181 6 : sqe->writeValuePart[0] = static_cast<uint32_t>(dbValue & MASK_32_BIT); // low 32 bit
182 6 : sqe->writeValuePart[1] = static_cast<uint32_t>((dbValue >> UINT32_BIT_NUM) & MASK_32_BIT); // high 32 bit
183 :
184 6 : sqe->va = 1U;
185 6 : }
186 :
187 : } // namespace Hccl
|