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: 68.5 % 733 502
Test Date: 2026-07-28 12:11:00 Functions: 78.7 % 61 48

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

Generated by: LCOV version 2.0-1