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 : #include "ccu_channel_ctx_mgr_v1.h"
12 :
13 : #include <vector>
14 : #include <string>
15 :
16 : #include "ccu_res_specs.h"
17 : #include "hcomm_adapter_hccp.h"
18 :
19 : namespace hcomm {
20 :
21 : constexpr uint32_t CCU_V1_CHANNEL_DEFAULT_JETTY_NUM = 1;
22 :
23 176 : HcclResult CcuChannelCtxMgrV1::Init()
24 : {
25 176 : uint32_t strategy = 0; // 获取失败或为0场景,分配将按资源不足操作
26 176 : (void)CcuResSpecifications::GetInstance(devLogicId_).GetChannelNum(dieId_, strategy);
27 176 : channelResInfos_.resize(strategy);
28 176 : CHK_RET(jettyCtxMgr_.Init());
29 176 : return HcclResult::HCCL_SUCCESS;
30 : }
31 :
32 189 : static HcclResult FindFreeChannelId(std::vector<ChannelResInfo>& channelResInfos, uint32_t& channelId)
33 : {
34 : // ccu v1每次都分配新的channel与jettyCtx
35 : // 故直接选择首个可用channel即可
36 189 : const uint32_t channelNum = channelResInfos.size();
37 202 : for (uint32_t i = 0; i < channelNum; i++) {
38 202 : if (!channelResInfos[i].allocated) {
39 189 : channelId = i;
40 189 : return HcclResult::HCCL_SUCCESS;
41 : }
42 : }
43 0 : return HcclResult::HCCL_E_UNAVAIL;
44 : }
45 :
46 189 : HcclResult CcuChannelCtxMgrV1::Alloc(const ChannelPara& channelPara, std::vector<ChannelInfo>& channelInfos)
47 : {
48 189 : const uint32_t feId = channelPara.feId;
49 189 : uint32_t jettyNum = channelPara.jettyNum;
50 189 : if (jettyNum == 0) {
51 189 : jettyNum = CCU_V1_CHANNEL_DEFAULT_JETTY_NUM;
52 189 : HCCL_INFO(
53 : "[CcuJettyCtxMgrV1][%s] jettyNum is 0, reset to default[%u], "
54 : "feId[%u], devLogicId[%d], dieId[%u].",
55 : __func__, jettyNum, feId, devLogicId_, dieId_);
56 : }
57 :
58 189 : std::lock_guard<std::mutex> lock(innerMutex_);
59 189 : uint32_t channelId = 0;
60 189 : auto ret = FindFreeChannelId(channelResInfos_, channelId);
61 189 : CHK_PRT_RET(
62 : ret == HcclResult::HCCL_E_UNAVAIL,
63 : HCCL_WARNING(
64 : "[CcuChannelCtxMgrV1][%s] failed to find free channel, channel strategy[%zu], "
65 : "left resources are not enough, feId[%u], devLogicId[%d], dieId[%u].",
66 : __func__, channelResInfos_.size(), feId, devLogicId_, dieId_),
67 : ret);
68 189 : CHK_RET(ret);
69 :
70 189 : ChannelInfo channelInfo = {};
71 189 : ret = jettyCtxMgr_.Alloc(feId, jettyNum, channelPara.sqSize, channelInfo.jettyInfos);
72 189 : CHK_PRT_RET(
73 : ret == HcclResult::HCCL_E_UNAVAIL,
74 : HCCL_WARNING(
75 : "[CcuChannelCtxMgrV1][%s] failed to allocate jetty contexts to channelId[%u], "
76 : "left resources are not enough, feId[%u], devLogicId[%d], dieId[%u].",
77 : __func__, channelId, feId, devLogicId_, dieId_),
78 : ret);
79 189 : CHK_RET(ret);
80 :
81 189 : channelInfo.channelId = channelId;
82 189 : channelInfo.dieId = dieId_;
83 189 : channelResInfos_[channelId].feId = feId;
84 189 : channelResInfos_[channelId].channelInfo = channelInfo;
85 189 : channelResInfos_[channelId].allocated = true;
86 189 : DumpChannelResInfo(feId, channelInfo);
87 :
88 189 : channelInfos.clear(); // ccu v1每次仅分配1个channel,不同channel不复用jettyCtx
89 189 : channelInfos.emplace_back(std::move(channelInfo));
90 189 : return ret;
91 189 : }
92 :
93 : static ChannelCtxDataV1
94 176 : BuildChannelCtxDataV1(const ChannelCfg& cfg, const uint32_t feId, const uint8_t dieId, const uint16_t startTaJettyId)
95 : {
96 176 : ChannelCtxDataV1 data = {};
97 176 : (void)memcpy_s(&data.eidRaw[0], URMA_EID_LEN, &cfg.remoteEid, URMA_EID_LEN);
98 :
99 176 : data.vtpLow = cfg.tpn & MASK_VTP_LOW;
100 176 : data.vtpHigh = ((cfg.tpn & MASK_VTP) >> Hccl::SHIFT_16BITS) & MASK_VTP_HIGH;
101 :
102 176 : data.srcPfeId = static_cast<uint16_t>(feId);
103 :
104 176 : data.startJettyIdLow = startTaJettyId & MASK_START_JETTY_ID_LOW;
105 176 : data.startJettyIdHigh = (startTaJettyId >> Hccl::SHIFT_4BITS) & MASK_START_JETTY_ID_HIGH;
106 :
107 : // 写入硬件减 1,cfgs的数量一定小于jetty规格数,不会超过uint8_t范围
108 176 : uint8_t jettyNum = static_cast<uint8_t>(cfg.jettyCfgs.size()) - 1;
109 176 : data.jettyNumLow = jettyNum & MASK_JETTY_NUM_LOW;
110 176 : data.jettyNumHigh = (jettyNum >> Hccl::SHIFT_4BITS) & MASK_JETTY_NUM_HIGH;
111 :
112 176 : data.ioDieId = static_cast<uint16_t>(dieId);
113 :
114 176 : data.dstTokenIdLow = cfg.memTokenId & MASK_TOKEN_ID_LOW;
115 176 : data.dstTokenIdHigh = (cfg.memTokenId >> Hccl::SHIFT_12BITS) & MASK_TOKEN_ID_HIGH;
116 :
117 176 : data.dstTokenValueLow = cfg.memTokenValue & MASK_TOKEN_VALUE_LOW;
118 176 : data.dstTokenValueMiddle = (cfg.memTokenValue >> Hccl::SHIFT_8BITS) & MASK_TOKEN_VALUE_MID;
119 176 : data.dstTokenValueHigh = (cfg.memTokenValue >> Hccl::SHIFT_24BITS) & MASK_TOKEN_VALUE_HIGH;
120 :
121 176 : uint64_t dstVa = (cfg.remoteCcuVa >> REMOTE_CCU_VA_RIGHT_SHIFT_NUM);
122 176 : data.dstVaLow = dstVa & MASK_VA_LOW;
123 176 : data.dstVaMiddle = (dstVa >> Hccl::SHIFT_8BITS) & MASK_VA_MID;
124 176 : data.dstVaHigh = (dstVa >> Hccl::SHIFT_24BITS) & MASK_VA_HIGH;
125 176 : data.dstVaHigher = (dstVa >> Hccl::SHIFT_40BITS) & MASK_VA_HIGHER;
126 176 : data.dstTokenValueValid = TOKEN_VALUE_VALID;
127 176 : return data;
128 : }
129 :
130 189 : static void DumpChannelCtxDataV1(const struct ChannelCtxDataV1& data)
131 : {
132 189 : if (IsEidEmpty(data.eidRaw)) {
133 13 : return;
134 : }
135 176 : std::string dstEidInfo = "eidRaw: ";
136 2816 : for (uint32_t i = 0; i < URMA_EID_LEN - 1; i++) {
137 2640 : dstEidInfo += Hccl::StringFormat("0x%02x, ", data.eidRaw[i]);
138 : }
139 176 : dstEidInfo += Hccl::StringFormat("0x%02x", data.eidRaw[URMA_EID_LEN - 1]);
140 176 : HCCL_INFO("%s.", dstEidInfo.c_str());
141 :
142 176 : HCCL_INFO(
143 : "vtpLow: 0x%04x, vtpHigh: 0x%04x, srcPfeId: 0x%04x, "
144 : "startJettyIdLow: 0x%04x, startJettyIdHigh: 0x%04x, "
145 : "JettyNumLow: 0x%04x, JettyNumHigh: 0x%04x, ioDieId: 0x%04x, ",
146 : data.vtpLow, data.vtpHigh, data.srcPfeId, data.startJettyIdLow, data.startJettyIdHigh, data.jettyNumLow,
147 : data.jettyNumHigh, data.ioDieId);
148 :
149 176 : HCCL_INFO(
150 : "dstVaLow: 0x%04x, dstVaMiddle: 0x%04x, "
151 : "dstVaHigh: 0x%04x, dstVaHigher: 0x%04x, dstTokenValueValid: 0x%04x",
152 : data.dstVaLow, data.dstVaMiddle, data.dstVaHigh, data.dstVaHigher, data.dstTokenValueValid);
153 176 : }
154 :
155 189 : static HcclResult ConfigChannelCtxDataV1(
156 : const int32_t devLogicId, const uint32_t devPhyId, const uint8_t dieId, const uint32_t channelId,
157 : const ChannelCtxDataV1& channelCtxData)
158 : {
159 189 : CustomChannelInfoIn inBuff{};
160 189 : CustomChannelInfoOut outBuff{};
161 :
162 189 : constexpr uint32_t dataArraySize = 1; // 每次配置1个Channel
163 189 : inBuff.op = CcuOpcodeType::CCU_U_OP_SET_CHANNEL;
164 189 : inBuff.data.dataInfo.udieIdx = dieId;
165 189 : inBuff.data.dataInfo.dataArraySize = dataArraySize;
166 189 : inBuff.data.dataInfo.dataLen = sizeof(struct ChannelCtxDataV1) * dataArraySize;
167 189 : inBuff.offsetStartIdx = channelId;
168 :
169 189 : HCCL_INFO(
170 : "[CcuChannelCtxMgrV1][%s] set data to ccu driver, devLogicId[%d] devPhyId[%u], "
171 : "ioDie[%u], idx[%u], size[%u].",
172 : __func__, devLogicId, devPhyId, dieId, channelId, sizeof(struct ChannelCtxDataV1));
173 189 : DumpChannelCtxDataV1(channelCtxData);
174 :
175 189 : (void)memcpy_s(
176 : inBuff.data.dataInfo.dataArray, sizeof(struct ChannelCtxDataV1), &channelCtxData,
177 : sizeof(struct ChannelCtxDataV1));
178 :
179 189 : auto ret = HccpRaTlvCcuCustomChannel(devLogicId, static_cast<void*>(&inBuff), static_cast<void*>(&outBuff));
180 189 : if (ret != HCCL_SUCCESS) {
181 0 : HCCL_ERROR(
182 : "[CcuChannelCtxMgrV1][%s] failed to call ccu driver, "
183 : "devLogicId[%d] devPhyId[%u] dieId[%d] op[%s] ret[%d].",
184 : __func__, devLogicId, devPhyId, dieId, "SET_CHANNEL", ret);
185 0 : return ret;
186 : }
187 :
188 189 : return HcclResult::HCCL_SUCCESS;
189 : }
190 :
191 176 : HcclResult CcuChannelCtxMgrV1::Config(const ChannelCfg& channelCfg)
192 : {
193 176 : std::lock_guard<std::mutex> lock(innerMutex_);
194 176 : const uint32_t channelId = channelCfg.channelId;
195 176 : if (!CheckIfChannelAllocated(channelId)) {
196 0 : return HcclResult::HCCL_E_PARA; // 日志已在判断处处理
197 : };
198 :
199 176 : const auto& channelResInfo = channelResInfos_[channelId];
200 176 : const uint32_t feId = channelResInfo.feId;
201 176 : const std::vector<JettyInfo>& jettyInfos = channelResInfo.channelInfo.jettyInfos;
202 176 : auto ret = jettyCtxMgr_.Config(feId, jettyInfos, channelCfg.jettyCfgs);
203 176 : if (ret != HcclResult::HCCL_SUCCESS) {
204 0 : HCCL_ERROR(
205 : "[CcuChannelCtxMgrV1][%s] failed to config jetty contexts of channelId[%u], "
206 : "feId[%u], devLogicId[%d], dieId[%u].",
207 : __func__, channelId, feId, devLogicId_, dieId_);
208 0 : return ret;
209 : }
210 : // 因jettyCtx连续,从起始jettyCtx配置
211 176 : const uint16_t startTaJettyId = jettyInfos[0].taJettyId;
212 176 : const ChannelCtxDataV1& data = BuildChannelCtxDataV1(channelCfg, feId, dieId_, startTaJettyId);
213 176 : CHK_RET(ConfigChannelCtxDataV1(devLogicId_, devPhyId_, dieId_, channelId, data));
214 176 : return HcclResult::HCCL_SUCCESS;
215 176 : }
216 :
217 13 : HcclResult CcuChannelCtxMgrV1::Release(const uint32_t channelId)
218 : {
219 13 : std::lock_guard<std::mutex> lock(innerMutex_);
220 13 : if (!CheckIfChannelAllocated(channelId)) {
221 0 : return HcclResult::HCCL_E_PARA; // 日志已在判断处处理
222 : };
223 :
224 13 : const auto& channelResInfo = channelResInfos_[channelId];
225 13 : auto ret = jettyCtxMgr_.Release(channelResInfo.feId, channelResInfo.channelInfo.jettyInfos);
226 13 : CHK_PRT_RET(
227 : ret != HcclResult::HCCL_SUCCESS,
228 : HCCL_WARNING(
229 : "[CcuChannelCtxMgrV1][%s] failed to release jetty contexts "
230 : "of channelId[%u], feId[%u], devLogicId[%d], dieId[%u].",
231 : __func__, channelId, channelResInfos_[channelId].feId, devLogicId_, dieId_),
232 : ret);
233 : // 重置并配置Channel表,避免错误复用
234 13 : channelResInfos_[channelId] = ChannelResInfo{};
235 13 : ChannelCtxDataV1 data = {};
236 13 : CHK_RET(ConfigChannelCtxDataV1(devLogicId_, devPhyId_, dieId_, channelId, data));
237 13 : return HcclResult::HCCL_SUCCESS;
238 13 : }
239 :
240 : }; // namespace hcomm
|