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

Generated by: LCOV version 2.0-1