Line data Source code
1 : /**
2 : * Copyright (c) 2026 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_jetty_ctx_mgr_v1.h"
12 :
13 : #include "ccu_res_specs.h"
14 :
15 : #include "ccu_wqebb_mgr_v1.h"
16 :
17 : namespace hcomm {
18 :
19 60 : HcclResult CcuJettyCtxMgrV1::Init()
20 : {
21 : // 获取失败或为0场景,分配将按资源不足操作
22 60 : (void)CcuResSpecifications::GetInstance(devLogicId_).GetJettyNum(dieId_, jettySpecNum_);
23 : // 获取地址为0在使用处校验
24 60 : (void)CcuResSpecifications::GetInstance(devLogicId_).GetResourceAddr(dieId_, ccuResBaseVa_);
25 :
26 60 : wqeBBMgr_.reset(new (std::nothrow) CcuWqeBBMgrV1(devLogicId_, dieId_));
27 60 : CHK_PTR_NULL(wqeBBMgr_);
28 60 : CHK_RET(wqeBBMgr_->Init());
29 60 : CHK_RET(pfeMgr_.Init());
30 60 : return HcclResult::HCCL_SUCCESS;
31 : }
32 :
33 72 : HcclResult CcuJettyCtxMgrV1::Alloc(const uint32_t feId, const uint32_t jettyNum,
34 : const uint32_t sqSize, std::vector<JettyInfo> &jettyInfos)
35 : {
36 72 : JettyAllocator *allocatorHandle = nullptr;
37 72 : auto ret = GetJettyAllocator(feId, allocatorHandle);
38 72 : CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
39 : HCCL_WARNING("[CcuJettyCtxMgrV1][%s] failed, failed to get jetty allocator handle, "
40 : "devLogicId[%d], dieId[%u], feId[%u].", __func__, devLogicId_, dieId_, feId),
41 : HcclResult::HCCL_E_INTERNAL);
42 :
43 72 : std::vector<ResInfo> jettyResInfos; // jettys分配必须要求连续,故返回一个元素
44 144 : ret = allocatorHandle->idAllocator->Alloc(jettyNum, true, jettyResInfos, "ResType::Jetty");
45 72 : CHK_PRT_RET(ret != HcclResult::HCCL_SUCCESS,
46 : HCCL_WARNING("[CcuJettyCtxMgrV1][%s] failed, allocator failed to allocate, "
47 : "devLogicId[%d], dieId[%u], feId[%u].", __func__, devLogicId_, dieId_, feId),
48 : ret);
49 :
50 72 : const auto &strategy = allocatorHandle->strategy;
51 72 : const uint32_t jettyResStartId = jettyResInfos[0].startId; // 资源分配器中的索引号
52 72 : const uint32_t jettyCtxStartId = strategy.startLocalJettyCtxId + jettyResStartId;
53 72 : const uint32_t taJettyStartId = strategy.startTaJettyId + jettyResStartId;
54 72 : CHK_PRT_RET(jettyCtxStartId > jettySpecNum_ - jettyNum,
55 : HCCL_WARNING("[CcuJettyCtxMgrV1][%s] jetty resource is not enough, allocated "
56 : "jettyCtxId[%u] should be less than %u, devLogicId[%d], dieId[%u], feId[%u].",
57 : __func__, jettyCtxStartId, jettySpecNum_, devLogicId_, dieId_, feId),
58 : HcclResult::HCCL_E_UNAVAIL);
59 :
60 72 : constexpr CcuJettyType type_ = CcuJettyType::CCUM_CACHED_JETTY;
61 72 : jettyInfos.resize(jettyNum);
62 72 : ret = TryAllocWqeBBResource(sqSize, jettyCtxStartId, taJettyStartId, type_, jettyInfos);
63 72 : if (ret != HcclResult::HCCL_SUCCESS) {
64 0 : HCCL_WARNING("[CcuJettyCtxMgrV1][%s] failed, try to release temp resource, "
65 : "devLogicId[%d], dieId[%u], feId[%u].", __func__, devLogicId_, dieId_, feId);
66 0 : CHK_RET(ReleaseWqeBBResource(jettyInfos)); // jettyInfos已分配的wqeBB资源信息
67 0 : jettyInfos.clear(); // 清理wqebb资源信息
68 0 : CHK_RET(allocatorHandle->idAllocator->Release(jettyResStartId, jettyNum));
69 : }
70 72 : return ret;
71 72 : }
72 :
73 84 : HcclResult CcuJettyCtxMgrV1::GetJettyAllocator(uint32_t feId, JettyAllocator* &allocatorHandle)
74 : {
75 84 : if (!allocator_) {
76 60 : PfeJettyStrategy strategy = {};
77 60 : CHK_RET(pfeMgr_.GetPfeStrategy(feId, strategy)); // 如果strategy为0,后续按资源不足处理
78 60 : allocator_.reset(new (std::nothrow) JettyAllocator(strategy));
79 60 : CHK_PTR_NULL(allocator_);
80 60 : CHK_PTR_NULL(allocator_->idAllocator);
81 : }
82 :
83 84 : allocatorHandle = allocator_.get();
84 84 : return HcclResult::HCCL_SUCCESS;
85 : }
86 :
87 60 : HcclResult CcuJettyCtxMgrV1::Config(const uint32_t feId,
88 : const std::vector<JettyInfo> &jettyInfos,
89 : const std::vector<JettyCfg> &jettyCfgs)
90 : {
91 60 : CHK_RET(CheckIfJettyCfgsValid(jettyInfos, jettyCfgs));
92 :
93 60 : std::vector<LocalJettyCtxData> jettyCtxData;
94 60 : const uint32_t jettyNum = jettyInfos.size();
95 120 : for (size_t i = 0; i < jettyNum; i++) {
96 60 : jettyCtxData.emplace_back(BuildJettyCtxData(dieId_, feId, jettyInfos[i], jettyCfgs[i]));
97 : }
98 :
99 60 : const uint32_t startJettyCtxId = jettyInfos[0].jettyCtxId;
100 60 : CHK_RET(ConfigJettyCtxData(devLogicId_, dieId_, devPhyId_, startJettyCtxId, jettyCtxData));
101 60 : return HcclResult::HCCL_SUCCESS;
102 60 : }
103 :
104 12 : HcclResult CcuJettyCtxMgrV1::Release(const uint32_t feId, const std::vector<JettyInfo> &jettyInfos)
105 : {
106 12 : if (jettyInfos.empty()) {
107 0 : HCCL_INFO("[CcuJettyCtxMgrV1][%s] passed, jettyInfos is empty, no need to release, ",
108 : "devLogicId[%d], dieId[%u], feId[%u].", __func__, devLogicId_, dieId_, feId);
109 0 : return HcclResult::HCCL_SUCCESS;
110 : }
111 12 : JettyAllocator* allocatorHandle = nullptr;
112 12 : CHK_RET(GetJettyAllocator(feId, allocatorHandle));
113 12 : CHK_RET(ReleaseWqeBBResource(jettyInfos));
114 :
115 12 : const uint32_t jettyStartCtxId = static_cast<uint32_t>(jettyInfos[0].jettyCtxId);
116 12 : const uint32_t jettyResStartId = jettyStartCtxId - allocatorHandle->strategy.startLocalJettyCtxId;
117 12 : CHK_RET(allocatorHandle->idAllocator->Release(jettyResStartId, jettyInfos.size()));
118 12 : return HcclResult::HCCL_SUCCESS;
119 : }
120 :
121 : }; // namespace hcomm
122 :
|