LCOV - code coverage report
Current view: top level - base_comm/resources/ccu/ccu_device/ccu_comp - ccu_comp.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 67.9 % 736 500
Test Date: 2026-08-04 10:52:23 Functions: 75.8 % 62 47

            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_comp.h"
      12              : 
      13              : #include <random>
      14              : 
      15              : #include "hccl_common.h"
      16              : #include "rdma_handle_manager.h"
      17              : 
      18              : #include "eid_info_mgr.h"
      19              : #include "ccu_res_specs.h"
      20              : #include "ccu_channel_ctx_mgr_v1.h"
      21              : #include "ccu_channel_ctx_mgr_v2.h"
      22              : 
      23              : #include "exception_handler.h"
      24              : #include "adapter_rts_common.h"
      25              : #include "env_config.h"
      26              : #include "orion_adapter_hccp.h"
      27              : #include "hcomm_adapter_hccp.h"
      28              : 
      29              : namespace hcomm {
      30              : 
      31              : constexpr TpProtocol LOOP_JETTY_PROTOCOL = TpProtocol::RTP; // 环回使用RTP避免被环境link down阻塞
      32              : constexpr uint8_t CCU_MAX_MISSION_NUM = 16;
      33              : 
      34              : // 设置为0,分配数量由channelCtxMgr决定,v1 默认1个
      35              : constexpr uint32_t LOOP_CHANNEL_USE_JETTY  = 0;
      36              : constexpr uint32_t LOOP_CHANNEL_USE_SQSIZE_V1 = 16;
      37              : constexpr uint32_t LOOP_CHANNEL_USE_SQSIZE_V2 = 32;
      38              : 
      39              : // 环回获取TP信息超时等待10s
      40              : constexpr uint32_t LOOP_CHANNEL_WAIT_TIMEOUT_MS = 10000;
      41              : 
      42              : // 环境是ARM+X86时,配置 die0 的 MS 交织粒度为 1<<7 = 128
      43              : constexpr uint32_t MSID_CONFIG_ARMX86_MAINBOARD = 7;
      44              : // 设计支持的最大IOdie数量
      45              : constexpr uint8_t MAX_CCU_IODIE_NUM = 2;
      46              : // 清理CKE批量申请大小
      47              : constexpr u32 MAX_CKE_DATA_ARRAY_SIZE = 8;
      48              : 
      49         4508 : CcuComponent &CcuComponent::GetInstance(const int32_t deviceLogicId)
      50              : {
      51         4706 :     static CcuComponent ccuComponent[MAX_MODULE_DEVICE_NUM + 1];
      52         4508 :     int32_t devLogicId = deviceLogicId;
      53         4508 :     if (devLogicId < 0 || static_cast<uint32_t>(devLogicId) >= MAX_MODULE_DEVICE_NUM) {
      54            0 :         HCCL_WARNING("[CcuComponent][%s] use the backup device, devLogicId[%d] should be "
      55              :             "less than %u.", __func__, devLogicId, MAX_MODULE_DEVICE_NUM);
      56            0 :         devLogicId = MAX_MODULE_DEVICE_NUM; // 使用备份设备
      57              :     }
      58              : 
      59         4508 :     ccuComponent[devLogicId].devLogicId_ = devLogicId;
      60         4508 :     return ccuComponent[devLogicId];
      61              : }
      62              : 
      63          145 : HcclResult CcuComponent::Init()
      64              : {
      65          145 :     std::lock_guard<std::mutex> _lock(innerMutex_);
      66              : 
      67          145 :     if (initFlag_) {
      68           59 :         return HcclResult::HCCL_SUCCESS;
      69              :     }
      70              : 
      71           86 :     CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<uint32_t>(devLogicId_), devPhyId_));
      72           86 :     CHK_RET(CheckDiesEnable());
      73           85 :     CHK_RET(CreateCcuRmaBuffer());
      74           85 :     CHK_RET(CreateResourceManagers());
      75           85 :     CHK_RET(CreateLoopChannels());
      76           85 :     CHK_RET(ConfigMsIdToken());
      77           85 :     initFlag_ = true;
      78           85 :     return HcclResult::HCCL_SUCCESS;
      79          145 : }
      80              : 
      81          371 : HcclResult CcuComponent::Deinit()
      82              : {
      83          371 :     std::lock_guard<std::mutex> _lock(innerMutex_);
      84          371 :     CHK_RET(ReleaseJettyRes());
      85              : 
      86          368 :     loopFeCommAddrMap_.clear();
      87          368 :     ccuRmaBufferMap_.clear();
      88              :     
      89         1104 :     for (uint8_t dieId = 0; dieId < CCU_MAX_IODIE_NUM; dieId++) {
      90          736 :         channelCtxMgrs_[dieId] = nullptr;
      91          736 :         resAllocators_[dieId] = nullptr;
      92          736 :         loopChannelIds_[dieId] = INVAILD_LOOP_CHANNEL_ID;
      93              :     }
      94              : 
      95          368 :     initFlag_ = false;
      96          368 :     return HcclResult::HCCL_SUCCESS;
      97          371 : }
      98              : 
      99          207 : CcuComponent::~CcuComponent()
     100              : {
     101          207 :     (void)Deinit();
     102          207 : }
     103              : 
     104           86 : static std::array<bool, CCU_MAX_IODIE_NUM> GetDieDrvEnableFlags(const int32_t devLogicId)
     105              : {
     106              :     // 根据资源规格的记录驱动可用的die
     107           86 :     std::array<bool, CCU_MAX_IODIE_NUM> dieDrvEnableFlags{false, false}; 
     108           86 :     const auto &ccuResSpecs = CcuResSpecifications::GetInstance(devLogicId);
     109          258 :     for (uint8_t dieId = 0; dieId < CCU_MAX_IODIE_NUM; dieId++) {
     110          172 :         (void)ccuResSpecs.GetDieEnableFlag(dieId, dieDrvEnableFlags[dieId]);
     111          172 :         if (!dieDrvEnableFlags[dieId]) { // 调用接口失败时不会改变dieEnableFlags[i]
     112            2 :             HCCL_WARNING("[CcuComponent][%s] devLogicId[%d], dieId[%u] driver is not usable.",
     113              :                 __func__, devLogicId, dieId);
     114              :         }
     115              :     }
     116              : 
     117           86 :     return dieDrvEnableFlags;
     118              : }
     119              : 
     120           86 : HcclResult CcuComponent::CheckDiesEnable()
     121              : {
     122           86 :     ccuVersion_ = CcuResSpecifications::GetInstance(devLogicId_).GetCcuVersion();
     123           86 :     HCCL_INFO("[CcuComponent][%s] ccu version[%s], devLogicId[%d].",
     124              :         __func__, ccuVersion_.Describe().c_str(), devLogicId_);
     125              : 
     126           86 :     const auto &dieDrvEnableFlags = GetDieDrvEnableFlags(devLogicId_);
     127              :     // 内部检查驱动可用的die上是否配置eid,内部更新die是否可用的标记
     128           86 :     CHK_RET(ChooseLoopEids(dieDrvEnableFlags));
     129              : 
     130           86 :     bool allDieDisable = true;
     131          258 :     for (uint8_t dieId = 0; dieId < CCU_MAX_IODIE_NUM; dieId++) {
     132          172 :         allDieDisable = allDieDisable && !dieEnableFlags_[dieId];
     133              :     }
     134              : 
     135           86 :     if (allDieDisable) {
     136            1 :         HCCL_ERROR("[CcuComponent][%s] failed, because all dies are "
     137              :             "disabled, devLogicId[%d].", __func__, devLogicId_);
     138            1 :         return HcclResult::HCCL_E_UNAVAIL;
     139              :     }
     140              : 
     141           85 :     return HcclResult::HCCL_SUCCESS;
     142              : }
     143              : 
     144          170 : static HcclResult FindOneUsableEid(const int32_t devLogicId, const uint32_t devPhyId,
     145              :     const uint8_t dieId, uint32_t &feId, CommAddr &commAddr)
     146              : {
     147              :     // 如果无法查询设备是否为uboe设备,报错退出
     148          170 :     CHK_RET(HccpGetUboeFlagEnable(devPhyId));
     149              : 
     150          170 :     std::vector<DevEidInfo> eidInfos;
     151          170 :     auto ret = EidInfoMgr::GetInstance(devPhyId).GetEidInfos(eidInfos);
     152          170 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     153              :         HCCL_WARNING("[CcuComponent][%s] failed, devLogicId[%d], dieId[%u].",
     154              :             __func__, devLogicId, dieId),
     155              :         ret);
     156              : 
     157          170 :     std::string name;
     158          170 :     bool findFlag = false;
     159              :     // 当前结论,除仅包含UBOE的FE外
     160              :     // 其他eid均支持源与目标eid一致时应用环回
     161              :     // 故当前版本选择首个可用eid即可
     162              :     EXCEPTION_HANDLE_BEGIN
     163          170 :     auto &rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
     164          680 :     for (auto &eidInfo : eidInfos) {
     165              :         // 如果是UBOE设备或非本die,则跳过
     166          510 :         if (HccpCheckUboeSupported(eidInfo.devFeature) || (eidInfo.dieId != dieId)) {
     167          335 :             continue;
     168              :         }
     169              : 
     170          255 :         Hccl::IpAddress ipAddr{};
     171          255 :         CHK_RET(CommAddrToIpAddress(eidInfo.commAddr, ipAddr));
     172          255 :         const auto rdmaHandle = rdmaHandleMgr.GetByIp(devPhyId, ipAddr);
     173          255 :         CHK_PTR_NULL(rdmaHandle);
     174          255 :         const bool rtpEnable = rdmaHandleMgr.GetRtpEnable(rdmaHandle);
     175          255 :         if (!rtpEnable) {
     176              :             // 遍历端口可能较多,避免刷屏不打印
     177           80 :             continue;
     178              :         }
     179              : 
     180          175 :         feId = eidInfo.funcId;
     181          175 :         commAddr = eidInfo.commAddr;
     182          175 :         name = eidInfo.name;
     183          175 :         findFlag = true;
     184              :     }
     185            0 :     EXCEPTION_HANDLE_END
     186              : 
     187          170 :     if (!findFlag) {
     188            0 :         HCCL_WARNING("[CcuComponent][%s] dieId[%u] doesn't have usable func ID, "
     189              :             "devLogicId[%d].", __func__, dieId, devLogicId);
     190            0 :         return HcclResult::HCCL_E_INTERNAL;
     191              :     }
     192              : 
     193          170 :     Hccl::IpAddress ipAddr{};
     194          170 :     CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
     195          170 :     HCCL_INFO("[CcuComponent][%s] dieId[%u] choose: name[%s] feId[%u] ipAddr[%s], "
     196              :         "devLogicId[%d].", __func__, dieId, name.c_str(), feId,
     197              :         ipAddr.Describe().c_str(), devLogicId);
     198              : 
     199          170 :     return HcclResult::HCCL_SUCCESS;
     200          170 : }
     201              : 
     202           86 : HcclResult CcuComponent::ChooseLoopEids(const std::array<bool, CCU_MAX_IODIE_NUM> &dieDrvEnableFlags)
     203              : {
     204          258 :     for (uint8_t dieId = 0; dieId < CCU_MAX_IODIE_NUM; dieId++) {
     205          172 :         if (!dieDrvEnableFlags[dieId]) {
     206            2 :             dieEnableFlags_[dieId] = false;
     207            2 :             continue;
     208              :         }
     209              : 
     210          170 :         uint32_t feId = 0;
     211          170 :         CommAddr commAddr{};
     212          170 :         if (FindOneUsableEid(devLogicId_, devPhyId_, dieId, feId, commAddr) != HcclResult::HCCL_SUCCESS) {
     213            0 :             dieEnableFlags_[dieId] = false;
     214            0 :             HCCL_WARNING("[CcuComponent][%s] failed to find feId eid, but passed, "
     215              :                 "devLogicId[%d], dieId[%u].", __func__, devLogicId_, dieId);
     216            0 :             continue;
     217              :         }
     218              : 
     219          170 :         loopFeCommAddrMap_[dieId] = {feId, commAddr};
     220          170 :         dieEnableFlags_[dieId] = true;
     221          170 :         HCCL_RUN_INFO("[CcuComponent][%s] devLogicId[%d] die[%u] is usable.",
     222              :             __func__, devLogicId_, dieId);
     223              :     }
     224           86 :     return HcclResult::HCCL_SUCCESS;
     225              : }
     226              : 
     227          340 : HcclResult CcuComponent::GetLoopFeIpByDieId(const uint8_t dieId, uint32_t &feId,
     228              :     CommAddr &commAddr)
     229              : {
     230          340 :     const auto &dieIter = loopFeCommAddrMap_.find(dieId);
     231          340 :     CHK_PRT_RET(dieIter == loopFeCommAddrMap_.end(),
     232              :         HCCL_WARNING("[CcuComponent][%s] failed but passed, "
     233              :             "dieId[%u] doesn't have usable loop feId, devLogicId[%d].",
     234              :             __func__, dieId, devLogicId_),
     235              :         HcclResult::HCCL_E_NOT_FOUND);
     236              : 
     237          340 :     const auto &feIdCommAddr = dieIter->second;
     238          340 :     feId = feIdCommAddr.first;
     239          340 :     commAddr = feIdCommAddr.second;
     240              : 
     241          340 :     return HcclResult::HCCL_SUCCESS;
     242              : }
     243              : 
     244           85 : HcclResult CcuComponent::CreateCcuRmaBuffer()
     245              : {
     246           85 :     auto &rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
     247           85 :     auto &ccuResSpecs = CcuResSpecifications::GetInstance(devLogicId_);
     248          255 :     for (uint8_t dieId = 0; dieId < CCU_MAX_IODIE_NUM; dieId++) {
     249          170 :         if (!dieEnableFlags_[dieId]) {
     250            0 :             continue;
     251              :         }
     252              : 
     253          170 :         uint32_t feId = 0;
     254          170 :         CommAddr commAddr{};
     255          170 :         if (GetLoopFeIpByDieId(dieId, feId, commAddr) != HcclResult::HCCL_SUCCESS) {
     256            0 :             continue;
     257              :         }
     258              : 
     259          170 :         uint64_t ccuResAddr = 0;
     260          170 :         (void)ccuResSpecs.GetResourceAddr(dieId, ccuResAddr);
     261          170 :         if (ccuResAddr == 0) {
     262            0 :             HCCL_WARNING("[CcuComponent][%s] failed, ccu resource space address[0] is invalid, "
     263              :                 "devLogicId[%d] dieId[%u]", __func__, devLogicId_, dieId);
     264            0 :             continue;
     265              :         }
     266              : 
     267              :         // 申请rdmaHandle可能抛异常
     268              :         EXCEPTION_HANDLE_BEGIN
     269          170 :         Hccl::IpAddress ipAddr{};
     270          170 :         CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
     271          170 :         const CtxHandle ctxHandle = static_cast<CtxHandle>(rdmaHandleMgr.GetByIp(devPhyId_, ipAddr));
     272          170 :         CHK_PTR_NULL(ctxHandle);
     273          170 :         const auto ccuBuffer = std::make_shared<Hccl::Buffer>(ccuResAddr, CCU_RESOURCE_SIZE);
     274          170 :         ccuRmaBufferMap_.emplace(dieId,
     275          340 :             std::make_unique<Hccl::LocalUbRmaBuffer>(ccuBuffer, ctxHandle));
     276              : 
     277          170 :         EXCEPTION_HANDLE_END
     278              :     }
     279              : 
     280           85 :     return HcclResult::HCCL_SUCCESS;
     281              : }
     282              : 
     283          170 : static HcclResult CreateChannelCtxMgrByVersion(const CcuVersion version,
     284              :     const uint32_t devLogicId, const uint8_t dieId, const uint32_t devPhyId,
     285              :     std::unique_ptr<CcuChannelCtxMgr>& channelCtxMgr)
     286              : {
     287          170 :     switch (version) {
     288          142 :         case CcuVersion::CCU_V1:
     289          142 :             channelCtxMgr.reset(
     290          142 :                 new (std::nothrow) CcuChannelCtxMgrV1(devLogicId, dieId, devPhyId));
     291          142 :             break;
     292           28 :          case CcuVersion::CCU_V2:
     293           28 :             channelCtxMgr.reset(
     294           28 :                 new (std::nothrow) CcuChannelCtxMgrV2(devLogicId, dieId, devPhyId));
     295           28 :             break;
     296            0 :         default:
     297            0 :             HCCL_ERROR("[CcuComponent][%s] failed, ccu driver version[%s] is not expected, "
     298              :                 "devLogicId[%d] dieId[%u].", __func__, version.Describe().c_str(),
     299              :                 devLogicId, dieId);
     300            0 :             return HcclResult::HCCL_E_NOT_SUPPORT;
     301              :     }
     302          170 :     CHK_PTR_NULL(channelCtxMgr);
     303          170 :     return HcclResult::HCCL_SUCCESS;
     304              : }
     305              : 
     306           85 : HcclResult CcuComponent::CreateResourceManagers()
     307              : {
     308          255 :     for (uint8_t dieId = 0; dieId < CCU_MAX_IODIE_NUM; dieId++) {
     309          170 :         if (!dieEnableFlags_[dieId]) {
     310            0 :             continue;
     311              :         }
     312              : 
     313          170 :         std::unique_ptr<CcuChannelCtxMgr> channelCtxMgrPtr = nullptr;
     314          170 :         CHK_RET(CreateChannelCtxMgrByVersion(ccuVersion_, devLogicId_,
     315              :             dieId, devPhyId_, channelCtxMgrPtr));
     316          170 :         CHK_RET(channelCtxMgrPtr->Init());
     317              : 
     318          170 :         std::unique_ptr<CcuResAllocator> resAllocatorPtr = nullptr;
     319          170 :         resAllocatorPtr.reset(new (std::nothrow) CcuResAllocator(devLogicId_, dieId));
     320          170 :         CHK_PTR_NULL(resAllocatorPtr);
     321          170 :         CHK_RET(resAllocatorPtr->Init());
     322              : 
     323          170 :         channelCtxMgrs_[dieId] = std::move(channelCtxMgrPtr);
     324          170 :         resAllocators_[dieId] = std::move(resAllocatorPtr);
     325          170 :     }
     326           85 :     return HcclResult::HCCL_SUCCESS;
     327              : }
     328              : 
     329           85 : HcclResult CcuComponent::CreateLoopChannels()
     330              : {
     331          255 :     for (uint8_t dieId = 0; dieId < CCU_MAX_IODIE_NUM; dieId++) {
     332          170 :         loopChannelIds_[dieId] = INVAILD_LOOP_CHANNEL_ID;
     333              :         // 失败抛异常处理,jetty资源跟随数据结构析构释放
     334          170 :         auto ret = CreateLoopChannel(dieId, loopChannelIds_[dieId]);
     335          170 :         CHK_PRT_RET(ret,
     336              :            HCCL_ERROR("[CcuComponent][%s] failed, devLogicId[%d], dieId[%u].",
     337              :             __func__, devLogicId_, dieId),
     338              :             ret);
     339              : 
     340          170 :         if (loopChannelIds_[dieId] == INVAILD_LOOP_CHANNEL_ID) {
     341            0 :             HCCL_RUN_WARNING("[CcuComponent][%s] failed but passed, loop channel id[%u], "
     342              :                 "devLogicId[%d], dieId[%u].", __func__, loopChannelIds_[dieId],
     343              :                 devLogicId_, dieId);
     344            0 :             continue;
     345              :         }
     346              : 
     347          170 :         HCCL_RUN_INFO("[CcuComponent][%s] succeed, loop channel id[%u], "
     348              :             "devLogicId[%d], dieId[%u].", __func__, loopChannelIds_[dieId],
     349              :             devLogicId_, dieId);
     350              :     }
     351              : 
     352           85 :     return HcclResult::HCCL_SUCCESS;
     353              : }
     354              : 
     355          170 : HcclResult CcuComponent::CreateLoopChannel(const uint8_t dieId, uint32_t &channelId)
     356              : {
     357          170 :     if (!dieEnableFlags_[dieId]) {
     358            0 :         HCCL_WARNING("CcuComponent][%s] passed, dieId[%u] is not enable, "
     359              :             "devLogicId[%d].", __func__, dieId, devLogicId_);
     360            0 :         return HcclResult::HCCL_SUCCESS;
     361              :     }
     362              : 
     363              :     // 对于单p或单die场景,可能设备或die不会配置eid,按成功处理不阻塞用例
     364          170 :     uint32_t feId = 0;
     365          170 :     CommAddr commAddr{};
     366          170 :     if (GetLoopFeIpByDieId(dieId, feId, commAddr) != HcclResult::HCCL_SUCCESS) {
     367            0 :         channelId = INVAILD_LOOP_CHANNEL_ID;
     368            0 :         HCCL_WARNING("[CcuComponent][%s] failed but passed, dieId[%u] doesn't have loop feId, "
     369              :             "devLogicId[%d].", __func__, dieId, devLogicId_);
     370            0 :         return HcclResult::HCCL_SUCCESS;
     371              :     }
     372          170 :     const uint32_t loopChannelSqsize = (ccuVersion_ == CcuVersion::CCU_V1 ?
     373          170 :         LOOP_CHANNEL_USE_SQSIZE_V1 : LOOP_CHANNEL_USE_SQSIZE_V2);
     374          170 :     std::vector<ChannelInfo> channelInfos; // 按jetty组分配
     375          170 :     const ChannelPara channelPara{feId, LOOP_CHANNEL_USE_JETTY, loopChannelSqsize};
     376          170 :     auto ret = channelCtxMgrs_[dieId]->Alloc(channelPara, channelInfos);
     377          170 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     378              :         HCCL_WARNING("[CcuComponent][%s] failed to alloc channel, "
     379              :             "devLogicId[%d], dieId[%u].", __func__, devLogicId_, dieId),
     380              :         ret);
     381              : 
     382          170 :     const auto &channelInfo = channelInfos[0]; // 环回只使用1个channel
     383          170 :     ret = CreateAndImportLoopJettys(dieId, commAddr, channelInfo.jettyInfos);
     384          170 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     385              :         HCCL_WARNING("[CcuComponent][%s] failed to create or import loop jettys, "
     386              :             "devLogicId[%d], dieId[%u].", __func__, devLogicId_, dieId),
     387              :         ret);
     388              : 
     389          170 :     ret = ConfigLoopChannel(dieId, commAddr, channelInfo);
     390          170 :     CHK_PRT_RET(ret != HCCL_SUCCESS,
     391              :         HCCL_WARNING("[CcuComponent][%s] failed to config the loop channel, "
     392              :             "devLogicId[%d], dieId[%u].", __func__, devLogicId_, dieId),
     393              :         ret);
     394              : 
     395          170 :     channelId = channelInfo.channelId;
     396          170 :     return HcclResult::HCCL_SUCCESS;
     397          170 : }
     398              : 
     399          172 : JettyImportCfg GetJettyImportCfg(const TpInfo &tpInfo, const uint32_t &psn)
     400              : {
     401          172 :     const TpHandle tpHandle = tpInfo.tpHandle;
     402          172 :     HCCL_INFO("[CcuComponent][%s] loop channel use tp handle[%llu] psn[%u].",
     403              :         __func__, tpHandle, psn);
     404              : 
     405          172 :     JettyImportCfg cfg = {};
     406          172 :     cfg.localTpHandle = tpHandle;
     407          172 :     cfg.remoteTpHandle = tpHandle;
     408          172 :     cfg.localPsn = psn;
     409          172 :     cfg.remotePsn = psn;
     410          172 :     cfg.protocol = LOOP_JETTY_PROTOCOL;
     411          172 :     return cfg;
     412              : }
     413              : 
     414          172 : HcclResult CcuComponent::CreateAndImportLoopJettys(const uint8_t dieId,
     415              :     const CommAddr &commAddr, const std::vector<JettyInfo> &jettyInfos)
     416              : {
     417          172 :     Hccl::IpAddress ipAddr{};
     418          172 :     CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
     419              : 
     420          172 :     Hccl::CqCreateInfo cqInfo{0};
     421          172 :     auto &rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
     422          172 :     const auto ctxHandle = static_cast<CtxHandle>(rdmaHandleMgr.GetByIp(devPhyId_, ipAddr));
     423          172 :     const auto _jfcHandle = rdmaHandleMgr.GetJfcHandle(ctxHandle, cqInfo, Hccl::HrtUbJfcMode::CCU_POLL);
     424          172 :     const JfcHandle jfcHandle = reinterpret_cast<JfcHandle>(_jfcHandle);
     425              : 
     426          172 :     const auto &rmaBufferIter = ccuRmaBufferMap_.find(dieId);
     427          172 :     CHK_PRT_RET(rmaBufferIter == ccuRmaBufferMap_.end(),
     428              :         HCCL_RUN_WARNING("[CcuComponent][%s] failed, ccu rma buffer of die[%u] is not existed, "
     429              :             "devLogicId[%d].", __func__, dieId, devLogicId_),
     430              :         HcclResult::HCCL_E_NOT_FOUND);
     431              : 
     432          172 :     const auto &ccuRmaBuffer = rmaBufferIter->second;
     433          172 :     const auto ccuBufTokenValue = ccuRmaBuffer->GetTokenValue();
     434              :     
     435          172 :     auto &createdVec = createdOutParamMap_[dieId];
     436          172 :     auto &importedVec = importedOutParamMap_[dieId];
     437              : 
     438          172 :     TpInfo loopTpInfo{};
     439          172 :     CHK_RET(GetLoopTpInfo(dieId, commAddr, loopTpInfo));
     440          344 :     const uint32_t loopJettyQos = loopTpInfo.hasMappedJettyPriority
     441          172 :         ? (loopTpInfo.mappedJettyPriority & 0xFU)
     442              :         : EnvConfig::UB_QOS_DEFAULT;
     443              : 
     444          172 :     TpAttrInfo tpAttrInfo{};
     445          172 :     CHK_RET(GetLoopTpAttr(dieId, commAddr, tpAttrInfo));
     446          172 :     const uint8_t errTimeout = TpMgr::CalcTaTimeout(tpAttrInfo);
     447              : 
     448          344 :     for (const auto &jettyInfo : jettyInfos) {
     449          172 :         const auto jettyMode = jettyInfo.jettyType == CcuJettyType::CCUM_CACHED_JETTY ?
     450          172 :             HrtJettyMode::CCU_CCUM_CACHE : HrtJettyMode::CCU_TA_CACHE;
     451              :         HrtRaUbCreateJettyParam req{jfcHandle, jfcHandle, ccuBufTokenValue,
     452          172 :             0, jettyMode, jettyInfo.taJettyId, jettyInfo.sqBufVa,
     453          172 :             jettyInfo.sqBufSize, jettyInfo.wqeBBStartId, jettyInfo.sqDepth,
     454          172 :             errTimeout};
     455          172 :         req.qos = loopJettyQos;
     456              : 
     457          172 :         HrtRaUbJettyCreatedOutParam createdOutParam{};
     458          172 :         CHK_RET(HccpUbCreateJetty(ctxHandle, req, createdOutParam));
     459          172 :         createdVec.emplace_back(createdOutParam);
     460              : 
     461          172 :         const auto psn = GetNewPsn();
     462          172 :         const auto &jettyImportCfg = GetJettyImportCfg(loopTpInfo, psn);
     463              : 
     464          172 :         HrtRaUbJettyImportedOutParam importedOutParam{};
     465          172 :         CHK_RET(HccpUbTpImportJetty(ctxHandle, createdOutParam.key,
     466              :             createdOutParam.keySize, ccuBufTokenValue, jettyImportCfg, importedOutParam));
     467          172 :         importedVec.emplace_back(std::make_pair(ctxHandle, importedOutParam));
     468              :     }
     469              : 
     470          172 :     return HcclResult::HCCL_SUCCESS;
     471              : }
     472              : 
     473          341 : static GetTpInfoParam MakeLoopGetTpInfoParam(const CommAddr &commAddr)
     474              : {
     475          341 :     GetTpInfoParam param;
     476          341 :     param.locAddr = commAddr;
     477          341 :     param.rmtAddr = commAddr;
     478          341 :     param.tpProtocol = LOOP_JETTY_PROTOCOL;
     479          341 :     param.qos = 0U; // CCU 环回与通信域 hcclQos 解耦;SL 仅由 RaGetTpAttr.slBitmap + loopFirstTpLowestSl 决定
     480          341 :     param.slLevelCount = 0;
     481          341 :     param.loopFirstTpLowestSl = true;
     482          341 :     param.ccuLoopbackGetTpInfo = true;
     483          341 :     return param;
     484              : }
     485              : 
     486          171 : static HcclResult RequestNewLoopTpInfo(const uint32_t devPhyId,
     487              :     const CommAddr &commAddr, TpInfo &tpInfo)
     488              : {
     489          171 :     constexpr auto timeout = std::chrono::milliseconds(LOOP_CHANNEL_WAIT_TIMEOUT_MS);
     490          171 :     const auto startTime = std::chrono::steady_clock::now();
     491              : 
     492          171 :     auto &tpMgr = TpMgr::GetInstance(devPhyId);
     493          171 :     const GetTpInfoParam &tpParam = MakeLoopGetTpInfoParam(commAddr);
     494          171 :     HcclResult ret = HcclResult::HCCL_SUCCESS;
     495              :     do {
     496          507 :         if ((std::chrono::steady_clock::now() - startTime) >= timeout) {
     497            0 :             HCCL_ERROR("[CcuComponent][%s] failed, get tp info "
     498              :                 "timeout[%d ms], devPhyId[%u].", __func__, timeout, devPhyId);
     499            0 :             return HcclResult::HCCL_E_TIMEOUT;
     500              :         }
     501              : 
     502          507 :         ret = tpMgr.GetTpInfo(tpParam, tpInfo);
     503          507 :     } while (ret == HcclResult::HCCL_E_AGAIN);
     504              : 
     505          171 :     CHK_RET(ret); // 非重试属于异常情况
     506          171 :     return HcclResult::HCCL_SUCCESS;
     507              : }
     508              : 
     509          173 : HcclResult CcuComponent::GetLoopTpInfo(const uint8_t dieId,
     510              :     const CommAddr &commAddr, TpInfo &tpInfo)
     511              : {
     512          173 :     const auto &srcIter = tpInfoMap_.find(dieId);
     513              :     // 优先使用已经创建过的tpHandle
     514          173 :     if (srcIter == tpInfoMap_.end()) {
     515          171 :         TpInfo newTpInfo{};
     516          171 :         CHK_RET(RequestNewLoopTpInfo(devPhyId_, commAddr, newTpInfo));
     517          171 :         tpInfoMap_[dieId] = std::move(newTpInfo);
     518              :     }
     519              : 
     520          173 :     tpInfo = tpInfoMap_[dieId];
     521          173 :     return HcclResult::HCCL_SUCCESS;
     522              : }
     523              : 
     524          170 : static HcclResult RequestNewLoopTpAttr(const uint32_t devPhyId, CtxHandle ctxHandle,
     525              :     const TpHandle tpHandle, TpAttrInfo &tpAttrInfo)
     526              : {
     527          170 :     constexpr auto timeout = std::chrono::milliseconds(LOOP_CHANNEL_WAIT_TIMEOUT_MS);
     528          170 :     const auto startTime = std::chrono::steady_clock::now();
     529              : 
     530          170 :     auto &tpMgr = TpMgr::GetInstance(devPhyId);
     531          170 :     constexpr uint32_t TP_ATTR_BITMAP = 0;
     532          170 :     const GetTpAttrParam tpAttrParam = {tpHandle, TP_ATTR_BITMAP};
     533          170 :     HcclResult ret = HcclResult::HCCL_SUCCESS;
     534              :     do {
     535          254 :         if ((std::chrono::steady_clock::now() - startTime) >= timeout) {
     536            0 :             HCCL_ERROR("[CcuComponent][%s] failed, get tp attr "
     537              :                 "timeout[%d ms], devPhyId[%d].", __func__, timeout, devPhyId);
     538            0 :             return HcclResult::HCCL_E_TIMEOUT;
     539              :         }
     540              : 
     541          254 :         ret = tpMgr.GetTpAttr(tpAttrParam, tpAttrInfo, ctxHandle);
     542          254 :     } while (ret == HcclResult::HCCL_E_AGAIN);
     543              : 
     544          170 :     CHK_RET(ret);
     545          170 :     return HcclResult::HCCL_SUCCESS;
     546              : }
     547              : 
     548          174 : HcclResult CcuComponent::GetLoopTpAttr(const uint8_t dieId,
     549              :     const CommAddr &commAddr, TpAttrInfo &tpAttrInfo)
     550              : {
     551          174 :     const auto &srcIter = tpAttrInfoMap_.find(dieId);
     552          174 :     if (srcIter == tpAttrInfoMap_.end()) {
     553          171 :         const auto &tpInfoIter = tpInfoMap_.find(dieId);
     554          171 :         CHK_PRT_RET(tpInfoIter == tpInfoMap_.end(),
     555              :             HCCL_ERROR("[CcuComponent][%s] failed, tpInfo not found for dieId[%u], "
     556              :                 "devLogicId[%d].", __func__, dieId, devLogicId_),
     557              :             HcclResult::HCCL_E_NOT_FOUND);
     558              : 
     559          170 :         Hccl::IpAddress ipAddr{};
     560          170 :         CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
     561          170 :         auto &rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
     562          170 :         const CtxHandle ctxHandle = static_cast<CtxHandle>(rdmaHandleMgr.GetByIp(devPhyId_, ipAddr));
     563              : 
     564          170 :         TpAttrInfo newTpAttrInfo{};
     565          170 :         CHK_RET(RequestNewLoopTpAttr(devPhyId_, ctxHandle, tpInfoIter->second.tpHandle, newTpAttrInfo));
     566          170 :         tpAttrInfoMap_[dieId] = std::move(newTpAttrInfo);
     567              :     }
     568              : 
     569          173 :     tpAttrInfo = tpAttrInfoMap_[dieId];
     570          173 :     return HcclResult::HCCL_SUCCESS;
     571              : }
     572              : 
     573          172 : inline uint32_t GenerateRandomNum()
     574              : {
     575          172 :     uint32_t randNum = std::rand();
     576          172 :     return randNum;
     577              : }
     578              : 
     579          172 : uint32_t CcuComponent::GetNewPsn()
     580              : {
     581          172 :     return GenerateRandomNum();
     582              : }
     583              : 
     584          170 : HcclResult CcuComponent::ConfigLoopChannel(const uint8_t dieId, const CommAddr &commAddr,
     585              :     const ChannelInfo &channelInfo)
     586              : {
     587          170 :     const uint32_t dstDieId = 1 - dieId; // 当前仅存在最多两个die
     588              :     // 当前环回复用支持die内die间,当两个die均启用时应配置对die,否则为本die
     589          170 :     auto rmaBufferIter = ccuRmaBufferMap_.find(dstDieId);
     590          170 :     if (rmaBufferIter == ccuRmaBufferMap_.end()) {
     591            0 :         rmaBufferIter = ccuRmaBufferMap_.find(dieId);
     592              :     }
     593              : 
     594          170 :     CHK_PRT_RET(rmaBufferIter == ccuRmaBufferMap_.end(),
     595              :         HCCL_WARNING("[CcuComponent][%s] failed, ccu rma buffer of die[%u] is not existed, "
     596              :             "devLogicId[%d].", __func__, dieId, devLogicId_),
     597              :         HcclResult::HCCL_E_NOT_FOUND);
     598              : 
     599          170 :     const auto &ccuRmaBuffer = rmaBufferIter->second;
     600          170 :     const auto ccuBufTokenValue = ccuRmaBuffer->GetTokenValue();
     601              : 
     602          170 :     Hccl::IpAddress ipAddr{};
     603          170 :     CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
     604              : 
     605          170 :     ChannelCfg cfg{};
     606          170 :     cfg.channelId = channelInfo.channelId;
     607          170 :     CHK_RET(IpAddressToReverseHcclEid(ipAddr, cfg.remoteEid));
     608          170 :     cfg.tpn       = importedOutParamMap_[dieId][0].second.tpn; // 环回仅1个对端
     609          170 :     cfg.remoteCcuVa   = ccuRmaBuffer->GetBuf()->GetAddr();
     610          170 :     cfg.memTokenId    = ccuRmaBuffer->GetTokenId();
     611          170 :     cfg.memTokenValue = ccuBufTokenValue;
     612              : 
     613          170 :     const auto &jettyInfos = channelInfo.jettyInfos;
     614          170 :     const auto &createdVec = createdOutParamMap_[dieId];
     615          170 :     const uint32_t jettyNum = jettyInfos.size();
     616          340 :     for (uint32_t i = 0; i < jettyNum; i++) {
     617          170 :         cfg.jettyCfgs.emplace_back(JettyCfg{
     618          170 :             jettyInfos[i].jettyCtxId,
     619          170 :             createdVec[i].dbVa,
     620          170 :             createdVec[i].dbTokenId,
     621              :             ccuBufTokenValue
     622              :         });
     623              :     }
     624              : 
     625          170 :     return channelCtxMgrs_[dieId]->Config(cfg);
     626          170 : }
     627              : 
     628           85 : HcclResult CcuComponent::ConfigMsIdToken()
     629              : {
     630           85 :     const auto serveMode = CcuResSpecifications::GetInstance(devLogicId_).GetServeMode();
     631           85 :     CustomChannelInfoIn  inBuff{};
     632           85 :     CustomChannelInfoOut outBuff{};
     633          255 :     for (uint8_t dieId = 0; dieId < CCU_MAX_IODIE_NUM; dieId++) {
     634          170 :         const auto &dieIter = ccuRmaBufferMap_.find(dieId);
     635          170 :         if (dieIter == ccuRmaBufferMap_.end()) {
     636            0 :             HCCL_WARNING("[CcuComponent][%s] failed but passed, ccu rma buffer of die[%u] "
     637              :                 "is not existed, devLogicId[%d].", __func__, dieId, devLogicId_);
     638            0 :             continue;
     639              :         }
     640          170 :         const auto &ccuRmaBuffer = dieIter->second;
     641          170 :         const uint32_t tokenId = ccuRmaBuffer->GetTokenId();
     642          170 :         const uint32_t tokenValue = ccuRmaBuffer->GetTokenValue();
     643          170 :         uint32_t msId = 0;
     644              :         // 非A+X, 非die 0,采用默认交织粒度
     645          170 :         if (serveMode == ServeMode::ARMX86 && dieId == 0) {
     646            0 :             msId = MSID_CONFIG_ARMX86_MAINBOARD;
     647              :         } else {
     648          170 :             CHK_RET(CcuResSpecifications::GetInstance(devLogicId_).GetMsId(dieId, msId));
     649              :         }
     650              : 
     651          170 :         inBuff.op                    = CcuOpcodeType::CCU_U_OP_SET_MSID_TOKEN;
     652          170 :         inBuff.offsetStartIdx        = 0;
     653          170 :         inBuff.data.dataInfo.udieIdx = dieId;
     654          170 :         inBuff.data.dataInfo.dataArray[0].baseinfo.msId       = msId;
     655          170 :         inBuff.data.dataInfo.dataArray[0].baseinfo.tokenId    = tokenId;
     656          170 :         inBuff.data.dataInfo.dataArray[0].baseinfo.tokenValue = tokenValue;
     657              : 
     658          170 :         auto ret = HccpRaTlvCcuCustomChannel(devLogicId_,
     659              :         static_cast<void *>(&inBuff), static_cast<void *>(&outBuff));
     660          170 :         if (ret != HCCL_SUCCESS) {
     661            0 :             HCCL_ERROR("[CcuComponent][%s] failed to call ccu driver, "
     662              :                 "devLogicId[%d] dieId[%d] op[%s] ret[%d].", __func__, devLogicId_, dieId,
     663              :                 "SET_MSID_TOKEN", ret);
     664            0 :             return ret;
     665              :         }
     666              : 
     667          170 :         HCCL_INFO("[CcuComponent][%s] config MS ID token success, dieId[%u], msid[%u]",
     668              :             __func__, dieId, msId);
     669              :     }
     670              : 
     671           85 :     return HcclResult::HCCL_SUCCESS;
     672              : }
     673              : 
     674           15 : HcclResult CcuComponent::GetCcuResourceSpaceBufInfo(const uint8_t dieId, uint64_t &addr,
     675              :     uint64_t &size) const
     676              : {
     677           15 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     678              : 
     679           15 :     auto res = ccuRmaBufferMap_.find(dieId);
     680           15 :     CHK_PRT_RET(res == ccuRmaBufferMap_.end(),
     681              :         HCCL_WARNING("[CcuComponent][%s] failed, ccu rma buffer of die[%u] is not existed, "
     682              :             "devLogicId[%d].", __func__, dieId, devLogicId_),
     683              :         HcclResult::HCCL_E_NOT_FOUND);
     684              : 
     685           15 :     const auto rawBuffer = res->second->GetBuf();
     686           15 :     addr = static_cast<uint64_t>(rawBuffer->GetAddr());
     687           15 :     size = static_cast<uint64_t>(rawBuffer->GetSize());
     688           15 :     return HcclResult::HCCL_SUCCESS;
     689              : }
     690              : 
     691          169 : HcclResult CcuComponent::GetCcuResourceSpaceTokenInfo(const uint8_t dieId, uint64_t &tokenId,
     692              :     uint64_t &tokenValue) const
     693              : {
     694          169 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     695              : 
     696          169 :     auto res = ccuRmaBufferMap_.find(dieId);
     697          169 :     CHK_PRT_RET(res == ccuRmaBufferMap_.end(),
     698              :         HCCL_WARNING("[CcuComponent][%s] failed, ccu rma buffer of die[%u] is not existed, "
     699              :             "devLogicId[%d].", __func__, dieId, devLogicId_),
     700              :         HcclResult::HCCL_E_NOT_FOUND);
     701              : 
     702          169 :     const auto &ccuRmaBuffer = res->second;
     703          169 :     tokenId = static_cast<uint64_t>(ccuRmaBuffer->GetTokenId());
     704          169 :     tokenValue = static_cast<uint64_t>(ccuRmaBuffer->GetTokenValue());
     705          169 :     return HcclResult::HCCL_SUCCESS;
     706              : }
     707              : 
     708           15 : HcclResult CcuComponent::AllocChannels(const uint8_t dieId, const ChannelPara &channelPara,
     709              :     std::vector<ChannelInfo> &channelInfos)
     710              : {
     711           15 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     712              : 
     713           15 :     CHK_PTR_NULL(channelCtxMgrs_[dieId]);
     714           15 :     auto ret = channelCtxMgrs_[dieId]->Alloc(channelPara, channelInfos);
     715           15 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     716              :         HCCL_WARNING("[CcuComponent][%s] failed, feId[%u], devLogicId[%d], dieId[%u].",
     717              :             __func__, channelPara.feId, devLogicId_, dieId),
     718              :         ret);
     719              : 
     720           15 :     return HcclResult::HCCL_SUCCESS;
     721              : }
     722              : 
     723            0 : HcclResult CcuComponent::ConfigChannel(const uint8_t dieId, const ChannelCfg &cfg)
     724              : {
     725            0 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     726              : 
     727            0 :     uint32_t channelId = cfg.channelId;
     728            0 :     CHK_PRT_RET(channelId == loopChannelIds_[dieId],
     729              :         HCCL_WARNING("[CcuComponent][%s] failed, refused to config loop channel[%u], "
     730              :             "devLogicId[%d], dieId[%u].", __func__, channelId, devLogicId_, dieId),
     731              :         HcclResult::HCCL_E_PARA);
     732              : 
     733            0 :     CHK_PTR_NULL(channelCtxMgrs_[dieId]);
     734            0 :     auto ret = channelCtxMgrs_[dieId]->Config(cfg);
     735            0 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     736              :         HCCL_WARNING("[CcuComponent][%s] failed, channelId[%u], devLogicId[%d], dieId[%u].",
     737              :             __func__, channelId, devLogicId_, dieId),
     738              :         ret);
     739              : 
     740            0 :     return HcclResult::HCCL_SUCCESS;
     741              : }
     742              : 
     743           29 : HcclResult CcuComponent::ReleaseChannel(const uint8_t dieId, const uint32_t channelId)
     744              : {
     745           29 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     746           29 :     CHK_PRT_RET(channelId == loopChannelIds_[dieId],
     747              :         HCCL_WARNING("[CcuComponent][%s] failed, refused to release loop channel[%u], "
     748              :             "devLogicId[%d], dieId[%u].", __func__, channelId, devLogicId_, dieId),
     749              :         HcclResult::HCCL_E_PARA);
     750              : 
     751           29 :     CHK_PTR_NULL(channelCtxMgrs_[dieId]);
     752           29 :     auto ret = channelCtxMgrs_[dieId]->Release(channelId);
     753           29 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     754              :         HCCL_WARNING("[CcuComponent][%s] failed, channelId[%u], devLogicId[%d], dieId[%u].",
     755              :             __func__, channelId, devLogicId_, dieId),
     756              :         ret);
     757              : 
     758           29 :     return HcclResult::HCCL_SUCCESS;
     759              : }
     760              : 
     761          308 : HcclResult CcuComponent::GetLoopChannelId(const uint8_t srcDieId, const uint8_t dstDieId,
     762              :     uint32_t &channelId) const
     763              : {
     764          308 :     channelId = INVAILD_LOOP_CHANNEL_ID; // 允许die未启用时查询环回channelId
     765              : 
     766          308 :     CHK_RET(CheckDieValid(__func__, devLogicId_, srcDieId, {true, true}));
     767          308 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dstDieId, {true, true}));
     768              : 
     769              :     // 特殊处理die未启用场景
     770          308 :     CHK_PRT_RET(!dieEnableFlags_[srcDieId] || !dieEnableFlags_[dstDieId],
     771              :         HCCL_WARNING("[CcuComponent][%s] passed, srcDie[%u] or dstDie[%u] is not enable,"
     772              :             "devLogicId[%d].", __func__, srcDieId, dstDieId, devLogicId_),
     773              :         HcclResult::HCCL_SUCCESS);
     774              : 
     775              :     // 当前环回channel每个die占用1个,不区分die内die间
     776          308 :     CHK_PRT_RET(loopChannelIds_[srcDieId] == INVAILD_LOOP_CHANNEL_ID,
     777              :         HCCL_ERROR("[CcuComponent][%s] failed, invalid loop channel id, "
     778              :             "devLogicId[%d], srcDieId[%u].", __func__, devLogicId_, srcDieId),
     779              :         HcclResult::HCCL_E_INTERNAL);
     780              : 
     781          308 :     channelId = loopChannelIds_[srcDieId];
     782          308 :     return HcclResult::HCCL_SUCCESS;
     783              : }
     784              : 
     785         1378 : HcclResult CcuComponent::AllocRes(const uint8_t dieId, const ResType resType, const uint32_t num,
     786              :     const bool consecutive, std::vector<ResInfo> &resInfos)
     787              : {
     788         1378 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     789              : 
     790         1378 :     CHK_PTR_NULL(resAllocators_[dieId]);
     791         1378 :     auto ret = resAllocators_[dieId]->Alloc(resType, num, consecutive, resInfos);
     792         1378 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     793              :         HCCL_WARNING("[CcuComponent][%s] failed, resType[%s], num[%u], devLogicId[%d], dieId[%u].",
     794              :             __func__, resType.Describe().c_str(), num, devLogicId_, dieId),
     795              :         ret);
     796              : 
     797         1378 :     return HcclResult::HCCL_SUCCESS;
     798              : }
     799              : 
     800            0 : HcclResult CcuComponent::ReleaseRes(const uint8_t dieId, const ResType resType, const uint32_t startId,
     801              :     const uint32_t num)
     802              : {
     803            0 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     804              : 
     805            0 :     CHK_PTR_NULL(resAllocators_[dieId]);
     806            0 :     auto ret = resAllocators_[dieId]->Release(resType, startId, num);
     807            0 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     808              :         HCCL_WARNING("[CcuComponent][%s] failed, resType[%s], startId[%u], num[%u], "
     809              :             "devLogicId[%d], dieId[%u].", __func__, resType.Describe().c_str(),
     810              :             startId, num, devLogicId_, dieId),
     811              :         ret);
     812              : 
     813            0 :     return HcclResult::HCCL_SUCCESS;
     814              : }
     815              : 
     816            0 : uint32_t CcuComponent::GetInsConsecutiveRemainSize(const uint8_t dieId) const
     817              : {
     818            0 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     819            0 :     if (resAllocators_[dieId] == nullptr) return 0;
     820            0 :     return resAllocators_[dieId]->GetConsecutiveRemainSize(ResType::INS);
     821              : }
     822              : 
     823           33 : HcclResult CcuComponent::AllocIns(const uint8_t dieId, const uint32_t num, ResInfo &insInfo)
     824              : {
     825           33 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     826              : 
     827           33 :     CHK_PTR_NULL(resAllocators_[dieId]);
     828           33 :     std::vector<ResInfo> resInfos;
     829           33 :     auto ret = resAllocators_[dieId]->Alloc(ResType::INS, num, true, resInfos);
     830           33 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     831              :         HCCL_WARNING("[CcuComponent][%s] failed, num[%u], devLogicId[%d], dieId[%u].",
     832              :             __func__, num, devLogicId_, dieId),
     833              :         ret);
     834              : 
     835           33 :     insInfo = resInfos[0]; // 申请连续资源只会有一份
     836           33 :     return HcclResult::HCCL_SUCCESS;
     837           33 : }
     838              : 
     839           33 : HcclResult CcuComponent::ReleaseIns(const uint8_t dieId, const ResInfo &insInfo)
     840              : {
     841           33 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     842              : 
     843           33 :     CHK_PTR_NULL(resAllocators_[dieId]);
     844           33 :     auto ret = resAllocators_[dieId]->Release(ResType::INS, insInfo.startId, insInfo.num);
     845           33 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     846              :         HCCL_WARNING("[CcuComponent][%s] failed, resInfo[%s], devLogicId[%d], dieId[%u].",
     847              :             __func__, insInfo.Describe().c_str(), devLogicId_, dieId),
     848              :         ret);
     849              : 
     850           33 :     return HcclResult::HCCL_SUCCESS;
     851              : }
     852              : 
     853           15 : HcclResult CcuComponent::AllocCke(const uint8_t dieId, const uint32_t num, std::vector<ResInfo> &ckeInfos)
     854              : {
     855           15 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     856              : 
     857           15 :     CHK_PTR_NULL(resAllocators_[dieId]);
     858           15 :     auto ret = resAllocators_[dieId]->Alloc(ResType::CKE, num, false, ckeInfos);
     859           15 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     860              :         HCCL_WARNING("[CcuComponent][%s] failed, num[%u], devLogicId[%d], dieId[%u].",
     861              :             __func__, num, devLogicId_, dieId),
     862              :         ret);
     863              : 
     864           15 :     return HcclResult::HCCL_SUCCESS;
     865              : }
     866              : 
     867           15 : HcclResult CcuComponent::ReleaseCke(const uint8_t dieId, const std::vector<ResInfo> &ckeInfos)
     868              : {
     869           15 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     870              : 
     871           15 :     CHK_PTR_NULL(resAllocators_[dieId]);
     872           30 :     for (auto &ckeInfo : ckeInfos) {
     873           15 :         auto ret = resAllocators_[dieId]->Release(ResType::CKE, ckeInfo.startId, ckeInfo.num);
     874           15 :         CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     875              :             HCCL_WARNING("[CcuComponent][%s] failed, resInfo[%s], devLogicId[%d], dieId[%u].",
     876              :                 __func__, ckeInfo.Describe().c_str(), devLogicId_, dieId),
     877              :             ret);
     878              :     }
     879              : 
     880           15 :     return HcclResult::HCCL_SUCCESS;
     881              : }
     882              : 
     883           15 : HcclResult CcuComponent::AllocXn(const uint8_t dieId, const uint32_t num, std::vector<ResInfo> &xnInfos)
     884              : {
     885           15 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     886              : 
     887           15 :     CHK_PTR_NULL(resAllocators_[dieId]);
     888           15 :     auto ret = resAllocators_[dieId]->Alloc(ResType::XN, num, false, xnInfos);
     889           15 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     890              :         HCCL_WARNING("[CcuComponent][%s] failed, num[%u], devLogicId[%d], dieId[%u].",
     891              :             __func__, num, devLogicId_, dieId),
     892              :         ret);
     893              : 
     894           15 :     return HcclResult::HCCL_SUCCESS;
     895              : }
     896              : 
     897           15 : HcclResult CcuComponent::ReleaseXn(const uint8_t dieId, const std::vector<ResInfo> &xnInfos)
     898              : {
     899           15 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
     900              : 
     901           15 :     CHK_PTR_NULL(resAllocators_[dieId]);
     902           30 :     for (auto &xnInfo : xnInfos) {
     903           15 :         auto ret = resAllocators_[dieId]->Release(ResType::XN, xnInfo.startId, xnInfo.num);
     904           15 :         CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     905              :             HCCL_WARNING("[CcuComponent][%s] failed, resInfo[%s], devLogicId[%d], dieId[%u].",
     906              :                 __func__, xnInfo.Describe().c_str(), devLogicId_, dieId),
     907              :             ret);
     908              :     }
     909              : 
     910           15 :     return HcclResult::HCCL_SUCCESS;
     911              : }
     912              : 
     913              : constexpr u32 WISH_COUNT_XN_NUM = 511;
     914              : constexpr u32 TOTAL_COUNT_XN_NUM = 1;
     915              : 
     916            0 : HcclResult CcuComponent::SetSplitUnit(uint8_t dieId, uint32_t splitPktUnit) const
     917              : {
     918            0 :     CHK_PRT_RET(dieId >= MAX_CCU_IODIE_NUM,
     919              :         HCCL_ERROR("[CcuComponent][%s] failed, dieId[%u] is invalid, shoudle be in [0-%u), devLogicId[%d].",
     920              :             __func__, dieId, MAX_CCU_IODIE_NUM, devLogicId_),
     921              :         HcclResult::HCCL_E_PARA);
     922              : 
     923            0 :     CustomChannelInfoIn  inBuff{};
     924            0 :     CustomChannelInfoOut outBuff{};
     925              : 
     926            0 :     inBuff.op                          = CcuOpcodeType::CCU_U_OP_SET_TIF_SPLIT_SIZE;
     927            0 :     inBuff.data.dataInfo.udieIdx       = dieId;
     928            0 :     inBuff.data.dataInfo.dataArraySize = 1;
     929            0 :     inBuff.data.dataInfo.dataLen       = sizeof(CcuDataTypeUnion) * inBuff.data.dataInfo.dataArraySize;
     930              : 
     931            0 :     inBuff.data.dataInfo.dataArray[0].tifSplitSize.splitPktUnit = splitPktUnit & 0b1;
     932            0 :     inBuff.data.dataInfo.dataArray[0].tifSplitSize.tpSplitSize  = 0x2;    // 0x2:TP模式的拆包size为4KB
     933            0 :     inBuff.data.dataInfo.dataArray[0].tifSplitSize.ctpSplitSize = 0x1;    // 0x1:CTP模式的拆包size为4KB
     934              : 
     935            0 :     auto ret = HccpRaTlvCcuCustomChannel(devLogicId_,
     936              :         static_cast<void *>(&inBuff), static_cast<void *>(&outBuff));
     937            0 :     if (ret != 0) {
     938            0 :         HCCL_ERROR("[CcuComponent][%s] failed to call ccu driver, "
     939              :             "devPhyId[%u] dieId[%d] op[%s].", __func__, devPhyId_, dieId,
     940              :             "CCU_U_OP_SET_TIF_SPLIT_SIZE");
     941            0 :         return HcclResult::HCCL_E_NETWORK;
     942              :     }
     943            0 :     return HcclResult::HCCL_SUCCESS;
     944              : }
     945              : 
     946            0 : HcclResult CcuComponent::GetAvailableTotalCntXnIndex(uint32_t& index) const
     947              : {
     948            0 :     for (uint32_t i = 0; i < CCU_V2_RESOURCE_TOTAL_CNT_XNS_NUM; ++i) {
     949            0 :         if (!usedTotalCntXnFlags_[i]) {
     950            0 :             index = i;
     951            0 :             return HcclResult::HCCL_SUCCESS;
     952              :         }
     953              :     }
     954              : 
     955            0 :     HCCL_ERROR("[CcuComponent][%s] failed, no available TotalCnt Xns.", __func__);
     956            0 :     return HcclResult::HCCL_E_UNAVAIL;
     957              : }
     958              : 
     959            0 : HcclResult CcuComponent::SetTotalCntXn(uint8_t dieId, uint32_t fromId, uint32_t toId, uint32_t totalId, uint32_t index)
     960              : {
     961            0 :     CHK_PRT_RET(fromId > toId,
     962              :         HCCL_ERROR("[CcuComponent][%s] failed, fromId or toId invalid, fromId[%u] > toId[%u].", __func__, fromId, toId),
     963              :         HcclResult::HCCL_E_PARA);
     964              : 
     965            0 :     CHK_PRT_RET(fromId <= totalId && totalId <= toId,
     966              :         HCCL_ERROR("[CcuComponent][%s] failed, totalId[%u] invalid, should not be in [fromId[%u], toId[%u]].",
     967              :             __func__, totalId, fromId, toId),
     968              :         HcclResult::HCCL_E_PARA);
     969              : 
     970            0 :     HcclResult ret = SetTotalCntXnProcess(dieId, index, fromId, toId, totalId);
     971            0 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     972              :         HCCL_ERROR("[CcuComponent][%s] failed, dieId[%u], index[%u], devLogicId[%d].",
     973              :             __func__, dieId, index, devLogicId_),
     974              :         ret);
     975              : 
     976            0 :     usedTotalCntXnFlags_[index] = true;
     977            0 :     return HcclResult::HCCL_SUCCESS;
     978              : }
     979              : 
     980            0 : HcclResult CcuComponent::ResetTotalCntXn(uint8_t dieId, uint32_t index)
     981              : {
     982            0 :     if (index >= CCU_V2_RESOURCE_TOTAL_CNT_XNS_NUM || !usedTotalCntXnFlags_[index]) {
     983            0 :         return HcclResult::HCCL_SUCCESS;
     984              :     }
     985              : 
     986              :     static constexpr uint32_t fromIdDefault = 0xFFFF;   // from默认值
     987              :     static constexpr uint32_t toIdDefault   = 0x0000;   // to默认值
     988              :     static constexpr uint32_t totalIdDefault[CCU_V2_RESOURCE_TOTAL_CNT_XNS_NUM] {0x3FFC, 0x3FFD, 0x3FFE, 0x3FFF};   // total默认值
     989              : 
     990            0 :     auto ret = SetTotalCntXnProcess(dieId, index, fromIdDefault, toIdDefault, totalIdDefault[index]);
     991            0 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
     992              :         HCCL_ERROR("[CcuComponent][%s] failed, dieId[%u], index[%u], devLogicId[%d].",
     993              :             __func__, dieId, index, devLogicId_),
     994              :         ret);
     995              : 
     996            0 :     usedTotalCntXnFlags_[index] = false;
     997            0 :     return HcclResult::HCCL_SUCCESS;
     998              : }
     999              : 
    1000            0 : HcclResult CcuComponent::SetTotalCntXnProcess(uint8_t dieId, uint32_t index, uint32_t fromId, uint32_t toId, uint32_t totalId) const
    1001              : {
    1002            0 :     CHK_PRT_RET(dieId >= MAX_CCU_IODIE_NUM,
    1003              :         HCCL_ERROR("[CcuComponent][%s] failed, dieId[%u] is invalid, shoudle be in [0-%u), devLogicId[%d].",
    1004              :             __func__, dieId, MAX_CCU_IODIE_NUM, devLogicId_),
    1005              :         HcclResult::HCCL_E_PARA);
    1006              : 
    1007            0 :     CHK_PRT_RET(index >= CCU_V2_RESOURCE_TOTAL_CNT_XNS_NUM,
    1008              :         HCCL_ERROR("[CcuComponent][%s] failed, index[%u] is invalid, shoudle be in [0-%u), devLogicId[%d].",
    1009              :             __func__, index, CCU_V2_RESOURCE_TOTAL_CNT_XNS_NUM, devLogicId_),
    1010              :         HcclResult::HCCL_E_PARA);
    1011              : 
    1012            0 :     CustomChannelInfoIn  inBuff{};
    1013            0 :     CustomChannelInfoOut outBuff{};
    1014              : 
    1015            0 :     inBuff.op                          = CcuOpcodeType::CCU_U_OP_SET_XN_TOTAL_CNT;
    1016            0 :     inBuff.data.dataInfo.udieIdx       = dieId;
    1017            0 :     inBuff.data.dataInfo.dataArraySize = 1;
    1018            0 :     inBuff.data.dataInfo.dataLen       = sizeof(CcuDataTypeUnion) * inBuff.data.dataInfo.dataArraySize;
    1019              : 
    1020            0 :     inBuff.data.dataInfo.dataArray[0].xnTotalCnt.cntIndex     = index & 0b11;    // range: [0, 3]
    1021            0 :     inBuff.data.dataInfo.dataArray[0].xnTotalCnt.flagFromAddr = fromId;
    1022            0 :     inBuff.data.dataInfo.dataArray[0].xnTotalCnt.flagToAddr   = toId;
    1023            0 :     inBuff.data.dataInfo.dataArray[0].xnTotalCnt.totalAddr    = totalId;
    1024            0 :     auto ret = HccpRaTlvCcuCustomChannel(devLogicId_,
    1025              :         static_cast<void *>(&inBuff), static_cast<void *>(&outBuff));
    1026            0 :     if (ret != 0) {
    1027            0 :         HCCL_ERROR("[CcuComponent][%s] failed to call ccu driver, "
    1028              :             "devPhyId[%u] dieId[%d] op[%s].", __func__, devPhyId_, dieId,
    1029              :             "CCU_U_OP_SET_XN_TOTAL_CNT");
    1030            0 :         return HcclResult::HCCL_E_NETWORK;
    1031              :     }
    1032              : 
    1033            0 :     return HcclResult::HCCL_SUCCESS;
    1034              : }
    1035              : 
    1036            0 : HcclResult CcuComponent::ConfirmCntXns(const uint8_t dieId, const std::string &resGroupTag,
    1037              :     const ResInfo &cntXnInfos)
    1038              : {
    1039            0 :     struct CntXnBlock cntXnBlock;
    1040            0 :     uint32_t totalCntXnId = cntXnInfos.startId + cntXnInfos.num - TOTAL_COUNT_XN_NUM;
    1041            0 :     uint32_t wishCntXnIdBegin = cntXnInfos.startId;
    1042            0 :     uint32_t wishCntXnIdEnd = totalCntXnId - 1;
    1043            0 :     uint32_t blockIdx = CCU_V2_RESOURCE_TOTAL_CNT_XNS_NUM;  // invalid value
    1044              : 
    1045            0 :     HCCL_INFO("Set TotalCntXn, wishCntXnIdBegin[%u] wishCntXnIdEnd[%u] totalCntXnId[%u]",
    1046              :         wishCntXnIdBegin, wishCntXnIdEnd, totalCntXnId);
    1047              : 
    1048            0 :     auto ret = GetAvailableTotalCntXnIndex(blockIdx);
    1049            0 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
    1050              :         HCCL_ERROR("[CcuComponent][%s] failed, no available TotalCnt Xns, dieId[%u], devLogicId[%d].",
    1051              :             __func__, dieId, devLogicId_),
    1052              :         ret);
    1053            0 :     CHK_RET(SetTotalCntXn(dieId, wishCntXnIdBegin, wishCntXnIdEnd, totalCntXnId, blockIdx));
    1054            0 :     HCCL_INFO("Set TotalCntXn success, index[%u]", blockIdx);
    1055              : 
    1056            0 :     ret = SetSplitUnit(dieId, 0);  //0表示stomic store add value的单位是byte。1表示以包为单位
    1057            0 :     if (ret != HcclResult::HCCL_SUCCESS) {
    1058            0 :         HCCL_ERROR("[CcuComponent][%s] SetSplitUnit failed, dieId[%u], devLogicId[%d].",
    1059              :         __func__, dieId, devLogicId_);
    1060            0 :         CHK_RET(ResetTotalCntXn(dieId, blockIdx));
    1061            0 :         return ret;
    1062              :     }
    1063              : 
    1064            0 :     for (u32 idx = wishCntXnIdBegin; idx <= wishCntXnIdEnd; idx++) {
    1065            0 :         cntXnBlock.wishCntXns.push(idx);
    1066              :     }
    1067            0 :     cntXnBlock.resInfo = cntXnInfos;
    1068            0 :     cntXnBlock.totalCntXn = totalCntXnId;
    1069            0 :     cntXnBlock.blockIdx = blockIdx;
    1070            0 :     cntXnBlocks_[dieId].insert(std::make_pair(resGroupTag, cntXnBlock));
    1071            0 :     return HcclResult::HCCL_SUCCESS;
    1072            0 : }
    1073              : 
    1074            0 : HcclResult CcuComponent::AllocWishCntXn(const uint8_t dieId, const std::string &resGroupTag,
    1075              :     uint32_t &wishCntXn)
    1076              : {
    1077            0 :     CHK_PRT_RET((ccuVersion_ != CcuVersion::CCU_V2),
    1078              :         HCCL_ERROR("[CcuComponent][%s] failed, ccuVersion[%d] does not support this interface.",
    1079              :             __func__, ccuVersion_), HCCL_E_NOT_SUPPORT);
    1080            0 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
    1081              : 
    1082            0 :     std::unique_lock<std::mutex> lock(cntXnBlockMutex_);
    1083            0 :     auto &cntXnBlocks = cntXnBlocks_[dieId];
    1084            0 :     auto iter = cntXnBlocks.find(resGroupTag);
    1085            0 :     if (iter != cntXnBlocks.end()) {
    1086            0 :         CHK_PRT_RET((iter->second.wishCntXns.size() == 0),
    1087              :             HCCL_ERROR("[CcuComponent][%s] failed, wishCntXn is not enough, resGroupTag[%s], devLogicId[%d], "
    1088              :                 "dieId[%u].", __func__, resGroupTag.c_str(), devLogicId_, dieId), HCCL_E_UNAVAIL);
    1089              :     } else {
    1090            0 :         CHK_PRT_RET((cntXnBlocks.size() == CCU_V2_RESOURCE_TOTAL_CNT_XNS_NUM),
    1091              :             HCCL_ERROR("[CcuComponent][%s] failed, cntXnBlock is not enough, resGroupTag[%s], "
    1092              :                 "devLogicId[%d], dieId[%u].", __func__, resGroupTag.c_str(), devLogicId_, dieId), HCCL_E_UNAVAIL);
    1093            0 :         ResInfo countXnInfo;
    1094              :         // 申请511 + 1个cntXn,前511个为wishCntXn,最后一个为totalCntXn
    1095            0 :         auto ret = resAllocators_[dieId]->AllocCountXn(WISH_COUNT_XN_NUM + TOTAL_COUNT_XN_NUM, countXnInfo);
    1096            0 :         CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
    1097              :             HCCL_ERROR("[CcuComponent][%s] failed, num[%u], resGroupTag[%s], devLogicId[%d], dieId[%u].",
    1098              :                 __func__, (WISH_COUNT_XN_NUM + TOTAL_COUNT_XN_NUM), resGroupTag.c_str(), devLogicId_, dieId), ret);
    1099              :         // 配置cntXn
    1100            0 :         ret = ConfirmCntXns(dieId, resGroupTag, countXnInfo);
    1101            0 :         if (ret != HcclResult::HCCL_SUCCESS) {
    1102            0 :             HCCL_ERROR("[CcuComponent][%s] failed[%d] to confirm cnt xns, "
    1103              :                 "try to release new allocated cnt xns, dieId[%u] resGroupTag[%s].",
    1104              :                 __func__, ret, dieId, resGroupTag.c_str());
    1105            0 :             CHK_RET(resAllocators_[dieId]->ReleaseCountXn(countXnInfo.startId, countXnInfo.num));
    1106            0 :             return ret;
    1107              :         }
    1108              :     }
    1109            0 :     auto &xnBlock = cntXnBlocks_[dieId][resGroupTag];
    1110            0 :     HCCL_INFO("resGroupTag[%s]stack size[%u]", resGroupTag.c_str(), xnBlock.wishCntXns.size());
    1111            0 :     wishCntXn = xnBlock.wishCntXns.top();
    1112            0 :     xnBlock.wishCntXns.pop();
    1113            0 :     uint32_t totalCntXn = xnBlock.totalCntXn;
    1114            0 :     HCCL_INFO("[CcuComponent][%s] success, resGroupTag[%s], devLogicId[%d], dieId[%u], wishCntXn[%u], totalCntXn[%u].",
    1115              :         __func__, resGroupTag.c_str(), devLogicId_, dieId, wishCntXn, totalCntXn);
    1116              : 
    1117            0 :     return HcclResult::HCCL_SUCCESS;
    1118            0 : }
    1119              : 
    1120            0 : HcclResult CcuComponent::ReleaseWishCntXn(const uint8_t dieId, const std::string &resGroupTag, uint32_t wishCntXn)
    1121              : {
    1122            0 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
    1123              : 
    1124            0 :     std::unique_lock<std::mutex> lock(cntXnBlockMutex_);
    1125            0 :     if (cntXnBlocks_[dieId].find(resGroupTag) == cntXnBlocks_[dieId].end()) {
    1126            0 :         HCCL_ERROR("[CcuComponent][%s] failed, resGroupTag[%s] is not found, devLogicId[%d], dieId[%u].",
    1127              :             __func__, resGroupTag.c_str(), devLogicId_, dieId);
    1128            0 :         return HCCL_E_NOT_FOUND;
    1129              :     }
    1130              : 
    1131            0 :     auto &xnBlock = cntXnBlocks_[dieId][resGroupTag];
    1132            0 :     xnBlock.wishCntXns.push(wishCntXn);
    1133            0 :     if (xnBlock.wishCntXns.size() != WISH_COUNT_XN_NUM) {
    1134            0 :         HCCL_INFO("[CcuComponent][%s] success, resGroupTag[%s], devLogicId[%d], dieId[%u], wishCntXn[%u], available "
    1135              :             "wishCntXn num[%u].",
    1136              :             __func__, resGroupTag.c_str(), devLogicId_, dieId, wishCntXn, xnBlock.wishCntXns.size());
    1137            0 :         return HCCL_SUCCESS;
    1138              :     }
    1139              : 
    1140              :     // 所有wishCnt都已经release,释放资源
    1141            0 :     CHK_RET(ResetTotalCntXn(dieId, xnBlock.blockIdx));
    1142              : 
    1143            0 :     auto ret = resAllocators_[dieId]->ReleaseCountXn(xnBlock.resInfo.startId,
    1144              :         xnBlock.resInfo.num);
    1145            0 :     CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
    1146              :         HCCL_ERROR("[CcuComponent][%s] failed, resGroupTag[%s], resInfo[%s], devLogicId[%d], dieId[%u].",
    1147              :             __func__, resGroupTag.c_str(), xnBlock.resInfo.Describe().c_str(), devLogicId_, dieId),
    1148              :         ret);
    1149            0 :     cntXnBlocks_[dieId].erase(resGroupTag);
    1150              : 
    1151            0 :     return HcclResult::HCCL_SUCCESS;
    1152            0 : }
    1153              : 
    1154            0 : HcclResult CcuComponent::GetCntXnBlock(const uint8_t dieId, const std::string &resGroupTag,
    1155              :     std::pair<uint32_t, uint32_t> &cntXnPair)
    1156              : {
    1157            0 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
    1158              : 
    1159            0 :     std::unique_lock<std::mutex> lock(cntXnBlockMutex_);
    1160            0 :     auto iter = cntXnBlocks_[dieId].find(resGroupTag);
    1161            0 :     if (iter == cntXnBlocks_[dieId].end()) {
    1162            0 :         HCCL_ERROR("[CcuComponent][%s] failed, resGroupTag[%s] is not found, devLogicId[%d], dieId[%u].",
    1163              :             __func__, resGroupTag.c_str(), devLogicId_, dieId);
    1164            0 :         return HCCL_E_NOT_FOUND;
    1165              :     }
    1166              : 
    1167            0 :     cntXnPair = std::make_pair(iter->second.resInfo.startId,
    1168            0 :         iter->second.totalCntXn);
    1169              : 
    1170            0 :     return HcclResult::HCCL_SUCCESS;
    1171            0 : }
    1172              : 
    1173            0 : HcclResult CcuComponent::GetTotalCntXn(const uint8_t dieId,
    1174              :     const std::string &resGroupTag, uint32_t &totalCntXn)
    1175              : {
    1176            0 :     CHK_RET(CheckDieValid(__func__, devLogicId_, dieId, dieEnableFlags_));
    1177              : 
    1178            0 :     std::unique_lock<std::mutex> lock(cntXnBlockMutex_);
    1179            0 :     auto iter = cntXnBlocks_[dieId].find(resGroupTag);
    1180            0 :     if (iter == cntXnBlocks_[dieId].end()) {
    1181            0 :         HCCL_ERROR("[CcuComponent][%s] failed, resGroupTag[%s] is not found, devLogicId[%d], dieId[%u].",
    1182              :             __func__, resGroupTag.c_str(), devLogicId_, dieId);
    1183            0 :         return HCCL_E_NOT_FOUND;
    1184              :     }
    1185              : 
    1186            0 :     totalCntXn = iter->second.totalCntXn;
    1187              : 
    1188            0 :     return HcclResult::HCCL_SUCCESS;
    1189            0 : }
    1190              : 
    1191         2851 : const std::array<bool, CCU_MAX_IODIE_NUM> &CcuComponent::GetDieEnableFlags() const
    1192              : {
    1193         2851 :     return dieEnableFlags_;
    1194              : }
    1195              : 
    1196          371 : HcclResult CcuComponent::ReleaseJettyRes()
    1197              : {
    1198          371 :     CHK_RET(UnimportAllJettys());
    1199          371 :     CHK_RET(ReleaseAllTpInfos());
    1200          368 :     CHK_RET(DestroyAllJettys());
    1201              :     // HrtRaUbLocalMemReg 跟随 LocalUbRmaBuffer 析构时释放
    1202              :     // 环回channel不需要手动释放,channelCtxMgr跟随CcuComponent释放
    1203          368 :     return HcclResult::HCCL_SUCCESS;
    1204              : }
    1205              : 
    1206          371 : HcclResult CcuComponent::UnimportAllJettys()
    1207              : {
    1208          543 :     for (auto &importedVec : importedOutParamMap_) {
    1209          344 :         for (auto &paramPair : importedVec.second) {
    1210          172 :             const auto ctxHandle = paramPair.first;
    1211          172 :             const auto remoteJettyHandle = paramPair.second.handle;
    1212          172 :             if (!ctxHandle || !remoteJettyHandle) {
    1213          172 :                 continue;
    1214              :             }
    1215            0 :             int32_t ret = RaCtxQpUnimport(ctxHandle, remoteJettyHandle);
    1216            0 :             if (ret != 0) {
    1217            0 :                 HCCL_ERROR("[CcuComponent][%s] failed, ctxHandle[%p] "
    1218              :                     "remoteJettyHandle[%p], devLogicId[%d].", __func__,
    1219              :                     ctxHandle, remoteJettyHandle, devLogicId_);
    1220              :             }
    1221            0 :             paramPair.second.handle = 0; // 清理handle,避免重复释放
    1222              :         }
    1223              :     }
    1224          371 :     importedOutParamMap_.clear();
    1225          371 :     return HcclResult::HCCL_SUCCESS;
    1226              : }
    1227              : 
    1228          371 : HcclResult CcuComponent::ReleaseAllTpInfos()
    1229              : {
    1230          544 :     for (auto &item : tpAttrInfoMap_) {
    1231          173 :         const auto &dieId = item.first;
    1232          173 :         const auto &tpAttrInfo = item.second;
    1233          173 :         const auto &tpInfoIter = tpInfoMap_.find(dieId);
    1234          173 :         if (tpInfoIter != tpInfoMap_.end() && tpInfoIter->second.tpHandle != 0) {
    1235          172 :             (void)TpMgr::GetInstance(devPhyId_).ReleaseTpAttr(tpInfoIter->second.tpHandle, tpAttrInfo);
    1236              :         }
    1237              :     }
    1238          371 :     tpAttrInfoMap_.clear();
    1239          541 :     for (auto &item : tpInfoMap_) {
    1240          173 :         const auto &dieId = item.first;
    1241          173 :         const auto &tpInfo = item.second;
    1242          173 :         if (!tpInfo.tpHandle) {
    1243            0 :             continue;
    1244              :         }
    1245              : 
    1246          173 :         const auto &dieIdIter = loopFeCommAddrMap_.find(dieId);
    1247          173 :         if (dieIdIter == loopFeCommAddrMap_.end()) {
    1248            3 :             HCCL_ERROR("[CcuComponent][%s] failed, dieId[%u] loop comm address"
    1249              :                 " is not found, devLogicId[%d].", __func__,
    1250              :                 static_cast<uint32_t>(dieId), devLogicId_);
    1251            3 :             return HcclResult::HCCL_E_NOT_FOUND;
    1252              :         }
    1253          170 :         const auto &commAddr = dieIdIter->second.second;
    1254          170 :         const GetTpInfoParam &tpParam = MakeLoopGetTpInfoParam(commAddr);
    1255          170 :         (void)TpMgr::GetInstance(devPhyId_).ReleaseTpInfo(tpParam, tpInfo);
    1256          170 :         item.second.tpHandle = 0; // 清理handle,避免重复释放
    1257              :     }
    1258          368 :     tpInfoMap_.clear();
    1259          368 :     return HcclResult::HCCL_SUCCESS;
    1260              : }
    1261              : 
    1262          368 : HcclResult CcuComponent::DestroyAllJettys()
    1263              : {
    1264          538 :     for (auto &createdVec : createdOutParamMap_) {
    1265          340 :         for (auto &param : createdVec.second) {
    1266          170 :             const auto jettyHandle = param.handle;
    1267          170 :             if (!jettyHandle) {
    1268          170 :                 continue;
    1269              :             }
    1270            0 :             int32_t ret = RaCtxQpDestroy(jettyHandle);
    1271            0 :             if (ret != 0) {
    1272            0 :                 HCCL_ERROR("[CcuComponent][%s] failed, jettyHandle[%p], "
    1273              :                     "devLogicId[%d].", __func__, jettyHandle, devLogicId_);
    1274              :             }
    1275            0 :             param.handle = 0; // 清理handle,避免重复释放
    1276              :         }
    1277              :     }
    1278          368 :     createdOutParamMap_.clear();
    1279          368 :     return HcclResult::HCCL_SUCCESS;
    1280              : }
    1281              : 
    1282            3 : HcclResult CcuComponent::SetProcess(CcuOpcodeType opCode) const
    1283              : {
    1284            3 :     CustomChannelInfoIn  inBuff;
    1285            3 :     CustomChannelInfoOut outBuff;
    1286              : 
    1287            3 :     inBuff.op = opCode;
    1288            9 :     for (uint8_t dieId = 0; dieId < MAX_CCU_IODIE_NUM; dieId++) {
    1289            6 :         if (!dieEnableFlags_[dieId]) {
    1290            6 :             HCCL_WARNING("[%s]devLogicId[%d], dieId[%u] is not enable, skip." , __func__, devLogicId_, dieId);
    1291            6 :             continue;
    1292              :         }
    1293            0 :         HCCL_INFO("[%s]devLogicId[%d], dieId[%u] start.", __func__, devLogicId_, dieId);
    1294            0 :         inBuff.data.dataInfo.udieIdx = dieId;
    1295            0 :         auto ret = HccpRaTlvCcuCustomChannel(devLogicId_,
    1296              :             static_cast<void *>(&inBuff), static_cast<void *>(&outBuff));
    1297            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    1298              :             HCCL_ERROR("[%s] failed to call ccu driver, devLogicId[%d] dieId[%u] op[%u] ret[%d].",
    1299              :                 __func__, devLogicId_, dieId, static_cast<uint32_t>(opCode), ret),
    1300              :             ret);
    1301              :     }
    1302            3 :     return HcclResult::HCCL_SUCCESS;
    1303              : }
    1304              : 
    1305            1 : HcclResult CcuComponent::CleanTaskKillState() const
    1306              : {
    1307            1 :     CHK_RET(SetProcess(CcuOpcodeType::CCU_U_OP_CLEAN_TASKKILL_STATE));
    1308            1 :     return HcclResult::HCCL_SUCCESS;
    1309              : }
    1310              : 
    1311            2 : HcclResult CcuComponent::SetTaskKill()
    1312              : {
    1313            2 :     std::lock_guard<std::mutex> _lock(taskKillMutex_);// 加锁,确保线程安全
    1314              :     
    1315              :     // 初始化状态下,设置任务kill状态
    1316            2 :     if (status == CcuTaskKillStatus::INVALID) {
    1317            1 :         status = CcuTaskKillStatus::INIT;
    1318              :     }
    1319              : 
    1320            2 :     if (status == CcuTaskKillStatus::TASK_KILL) {
    1321            1 :         HCCL_INFO("No need to set task kill, state = %u, devLogicId = %u", status, devLogicId_);
    1322            1 :         return HcclResult::HCCL_SUCCESS;
    1323              :     }
    1324              : 
    1325            1 :     if (status != CcuTaskKillStatus::INIT) {
    1326            0 :         HCCL_ERROR("[CcuComponent][%s] failed, cannot be invoked in the current state, "
    1327              :             "state = %u, devLogicId = %d.", __func__, status, devLogicId_);
    1328            0 :         return HcclResult::HCCL_E_INTERNAL;
    1329              :     }
    1330              : 
    1331            1 :     CHK_RET(SetProcess(CcuOpcodeType::CCU_U_OP_SET_TASKKILL));
    1332            1 :     status = CcuTaskKillStatus::TASK_KILL;
    1333            1 :     HCCL_INFO("[CcuComponent][%s] success, state = %u, devLogicId = %d.", __func__, status, devLogicId_);
    1334            1 :     return HcclResult::HCCL_SUCCESS;
    1335            2 : }
    1336              : 
    1337            1 : HcclResult CcuComponent::SetTaskKillDone()
    1338              : {
    1339            1 :     std::lock_guard<std::mutex> _lock(taskKillMutex_);// 加锁,确保线程安全
    1340            1 :     if (status == CcuTaskKillStatus::INVALID) {
    1341            0 :         HCCL_ERROR("[CcuComponent][%s] failed, cannot be invoked in the current state, "
    1342              :             "state = %u, devLogicId = %d.", __func__, status, devLogicId_);
    1343            0 :         return HcclResult::HCCL_E_INTERNAL;
    1344              :     }
    1345              : 
    1346            1 :     if (status == CcuTaskKillStatus::INIT) {
    1347            0 :         HCCL_INFO("No need to set task kill done, state = %u, devLogicId = %u", status, devLogicId_);
    1348            0 :         return HcclResult::HCCL_SUCCESS;
    1349              :     }
    1350              : 
    1351            1 :     if (status != CcuTaskKillStatus::TASK_KILL) {
    1352            0 :         HCCL_ERROR("[CcuComponent][%s] failed, cannot be invoked in the current state, "
    1353              :             "state = %u, devLogicId = %d.", __func__, status, devLogicId_);
    1354            0 :         return HcclResult::HCCL_E_INTERNAL;
    1355              :     }
    1356              : 
    1357            1 :     CHK_RET(SetProcess(CcuOpcodeType::CCU_U_OP_CLEAN_TASKKILL_STATE));
    1358            1 :     status = CcuTaskKillStatus::INIT;
    1359            1 :     HCCL_INFO("[CcuComponent][%s] success, state = %u, devLogicId = %d", __func__, status, devLogicId_);
    1360            1 :     return HcclResult::HCCL_SUCCESS;
    1361            1 : }
    1362              : 
    1363            0 : HcclResult CcuComponent::CcuSetTaskKillDone(const int32_t deviceLogicId)
    1364              : {
    1365            0 :     HCCL_INFO("[CcuSetTaskKillDone] Input params: deviceLogicId[%d]", deviceLogicId);
    1366              :     // 入参校验拦截
    1367            0 :     CHK_PRT_RET((deviceLogicId < 0 || static_cast<u32>(deviceLogicId) >= MAX_MODULE_DEVICE_NUM),
    1368              :         HCCL_ERROR("[CcuSetTaskKillDone]deviceLogicId[%d] error, MAX_MODULE_DEVICE_NUM[%u]", deviceLogicId, MAX_MODULE_DEVICE_NUM),
    1369              :             HcclResult::HCCL_E_PARA);
    1370            0 :     return CcuComponent::GetInstance(deviceLogicId).SetTaskKillDone();
    1371              : }
    1372              : 
    1373            0 : HcclResult CcuComponent::CcuCleanTaskKillState(const int32_t deviceLogicId)
    1374              : {
    1375            0 :     HCCL_INFO("[CcuCleanTaskKillState] Input params: deviceLogicId[%d]", deviceLogicId);
    1376              :     // 入参校验拦截
    1377            0 :     CHK_PRT_RET((deviceLogicId < 0 || static_cast<u32>(deviceLogicId) >= MAX_MODULE_DEVICE_NUM),
    1378              :         HCCL_ERROR("[CcuCleanTaskKillState]deviceLogicId[%d] error, MAX_MODULE_DEVICE_NUM[%u]", deviceLogicId, MAX_MODULE_DEVICE_NUM),
    1379              :             HcclResult::HCCL_E_PARA);
    1380            0 :     return CcuComponent::GetInstance(deviceLogicId).CleanTaskKillState();
    1381              : }
    1382              : 
    1383              : // 以下接口用于n秒快恢与TaskException
    1384            2 : HcclResult CcuComponent::CleanDieCkes(const uint8_t dieId) const
    1385              : {
    1386            2 :     CHK_PRT_RET(dieId >= MAX_CCU_IODIE_NUM,
    1387              :         HCCL_WARNING("[%s] failed, dieId[%u] is invalid, should be in [0-%u), devLogicId[%d].",
    1388              :         __func__, dieId, MAX_CCU_IODIE_NUM, devLogicId_), HcclResult::HCCL_E_PARA);
    1389              : 
    1390            1 :     if (!dieEnableFlags_[dieId]) {
    1391            1 :         HCCL_INFO("[%s] dieId[%u] is not enable, skip", __func__, dieId);
    1392            1 :         return HcclResult::HCCL_SUCCESS;
    1393              :     }
    1394              : 
    1395            0 :     CustomChannelInfoIn  inBuff{};
    1396            0 :     CustomChannelInfoOut outBuff{};
    1397              : 
    1398              :     // 设置操作码和数据
    1399            0 :     uint32_t ckeNum = 0;
    1400            0 :     CHK_RET(CcuResSpecifications::GetInstance(devLogicId_).GetCkeNum(dieId, ckeNum));
    1401            0 :     HCCL_INFO("[CcuComponent][CleanAllCke]Nsrecovery devLogicId[%d], dieId[%u] ckeNum[%u].",
    1402              :         devLogicId_, dieId, ckeNum);
    1403              :     
    1404            0 :     inBuff.op                          = CcuOpcodeType::CCU_U_OP_SET_CKE;
    1405            0 :     inBuff.data.dataInfo.udieIdx       = dieId;
    1406              :     // 接口限制,目前方案每次最多清理8个cke,超过8个时分多次清理
    1407            0 :     for (uint32_t startIdx = 0; startIdx < ckeNum; startIdx += MAX_CKE_DATA_ARRAY_SIZE) {
    1408            0 :         inBuff.data.dataInfo.dataArraySize = std::min(ckeNum - startIdx, MAX_CKE_DATA_ARRAY_SIZE);
    1409            0 :         inBuff.data.dataInfo.dataLen       = sizeof(CcuDataByte8) * inBuff.data.dataInfo.dataArraySize;
    1410            0 :         inBuff.offsetStartIdx              = startIdx;
    1411            0 :         auto ret = HccpRaTlvCcuCustomChannel(devLogicId_,
    1412              :             static_cast<void *>(&inBuff), static_cast<void *>(&outBuff));
    1413            0 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
    1414              :             HCCL_ERROR("[%s] failed to call ccu driver, devLogicId[%d] dieId[%u] op[%s] ret[%d].",
    1415              :                 __func__, devLogicId_, dieId, "SET_CKE", ret),
    1416              :             ret);
    1417              :     }
    1418              : 
    1419            0 :     return HcclResult::HCCL_SUCCESS;
    1420              : }
    1421              : 
    1422              : }; // namespace hcomm
        

Generated by: LCOV version 2.0-1