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_pfe_mgr.h"
12 :
13 : #include "hccp_ctx.h"
14 :
15 : #include "ccu_res_specs.h"
16 : #include "ccu_pfe_cfg_mgr.h"
17 : #include "hcomm_adapter_hccp.h"
18 :
19 : namespace hcomm {
20 :
21 170 : inline PfeCtx BuildPfeCtx(const uint8_t dieId, const PfeJettyCtxCfg &cfg)
22 : {
23 170 : struct PfeCtx ctx = {0};
24 170 : ctx.startJettyId = cfg.startTaJettyId;
25 170 : ctx.jettyNum = cfg.size - 1; // PFE给CCU分配的Jetty个数,配置硬件时需减1
26 170 : ctx.startLocalJettyCtxId = cfg.startJettyCtxId;
27 :
28 170 : HCCL_RUN_INFO("[CcuPfeMgr][%s]: dieId[%u] feId[%u], startJettyId[%u], jettyNum(-1)[%u], "
29 : "startLocalJettyCtxId[%u], pfe ctx size[%u]", __func__, dieId, cfg.feId,
30 : ctx.startJettyId, ctx.jettyNum, ctx.startLocalJettyCtxId, sizeof(PfeCtx));
31 170 : return ctx;
32 : }
33 :
34 170 : inline PfeJettyStrategy BuildStrategy(const PfeJettyCtxCfg &cfg)
35 : {
36 170 : struct PfeJettyStrategy pfeJettyStrategy = {0};
37 170 : pfeJettyStrategy.feId = cfg.feId;
38 170 : pfeJettyStrategy.pfeId = cfg.feId;
39 170 : pfeJettyStrategy.size = cfg.size;
40 170 : pfeJettyStrategy.startTaJettyId = cfg.startTaJettyId;
41 170 : pfeJettyStrategy.startLocalJettyCtxId = cfg.startJettyCtxId;
42 170 : return pfeJettyStrategy;
43 : }
44 :
45 170 : static HcclResult ConfigPfeTable(const int32_t devLogicId, const uint32_t devPhyId, const uint8_t dieId, const uint32_t feId,
46 : const uint32_t pfeReservedNum, const PfeCtx &pfeCtx)
47 : {
48 170 : if (UNLIKELY(feId > UINT32_MAX - static_cast<uint32_t>(dieId) * pfeReservedNum)) {
49 0 : HCCL_ERROR("[CcuPfeMgr][%s] failed, feId[%u] is greater than expected, "
50 : "pfeReservedNum[%u], will exceed the range of uint32_t, devLogicId[%d] devPhyId[%u], "
51 : "dieId[%u].", __func__, feId, pfeReservedNum, devLogicId, devPhyId, dieId);
52 0 : return HcclResult::HCCL_E_INTERNAL;
53 : }
54 : // die1 使用后半部分pfe表项,故根据pfe预留数量偏移
55 170 : const uint32_t pfeTableOffset = static_cast<uint32_t>(dieId) * pfeReservedNum + feId;
56 :
57 170 : CustomChannelInfoIn inBuff{};
58 170 : CustomChannelInfoOut outBuff{};
59 170 : inBuff.op = CcuOpcodeType::CCU_U_OP_SET_PFE;
60 170 : inBuff.data.dataInfo.udieIdx = static_cast<uint32_t>(dieId);
61 170 : inBuff.data.dataInfo.dataArraySize = 1;
62 170 : inBuff.data.dataInfo.dataLen = sizeof(struct PfeCtx); // 单个8B
63 170 : inBuff.offsetStartIdx = pfeTableOffset;
64 :
65 170 : (void)memcpy_s(inBuff.data.dataInfo.dataArray, inBuff.data.dataInfo.dataLen, &pfeCtx,
66 170 : inBuff.data.dataInfo.dataLen);
67 :
68 170 : auto ret = HccpRaTlvCcuCustomChannel(devLogicId,
69 : static_cast<void *>(&inBuff), static_cast<void *>(&outBuff));
70 170 : if (ret != HCCL_SUCCESS) {
71 0 : HCCL_ERROR("[CcuPfeMgr][%s] failed to call ccu driver, "
72 : "devLogicId[%d] devPhyId[%u] dieId[%d] op[%s] ret[%d].", __func__, devLogicId, devPhyId, dieId,
73 : "SET_PFE", ret);
74 0 : return ret;
75 : }
76 :
77 170 : return HcclResult::HCCL_SUCCESS;
78 : }
79 :
80 193 : HcclResult CcuPfeMgr::Init()
81 : {
82 : std::vector<PfeJettyCtxCfg> cfgs =
83 193 : CcuPfeCfgMgr::GetInstance(devLogicId_).GetPfeJettyCtxCfg(dieId_);
84 193 : if (UNLIKELY(cfgs.empty())) { // 此处不中断流程,后续jettyCtx分配时会因无pfe配置报错停止
85 23 : HCCL_WARNING("[CcuJettyCtxMgr] config pfe table passed, pfe cfgs size is 0, "
86 : "devLogicId[%d], dieId[%u].", devLogicId_, dieId_);
87 23 : return HcclResult::HCCL_SUCCESS;
88 : }
89 :
90 170 : uint32_t pfeReservedNum = 0;
91 170 : (void)CcuResSpecifications::GetInstance(devLogicId_).GetPfeReservedNum(dieId_, pfeReservedNum);
92 170 : if (UNLIKELY(pfeReservedNum == 0)) { // 此处不中断流程,后续jettyCtx分配时会因无pfe配置报错停止
93 0 : HCCL_WARNING("[CcuPfeMgr] config pfe table passed, pfe reserved num is 0, "
94 : "devLogicId[%d], dieId[%u].", devLogicId_, dieId_);
95 0 : return HcclResult::HCCL_SUCCESS;
96 : }
97 :
98 340 : for (const auto &cfg : cfgs) {
99 : // 分配策略保证cfg中feId均为ccu可用设备
100 170 : const uint32_t feId = cfg.feId;
101 170 : const auto &iter = pfeJettyMap_.find(feId);
102 170 : if (iter != pfeJettyMap_.end()) {
103 0 : continue; // 跳过已配置pfe
104 : }
105 170 : pfeJettyMap_[feId] = BuildStrategy(cfg);
106 :
107 170 : const auto &pfeCtx = BuildPfeCtx(dieId_, cfg);
108 170 : CHK_RET(ConfigPfeTable(devLogicId_, devPhyId_, dieId_, feId, pfeReservedNum, pfeCtx));
109 : }
110 :
111 170 : return HcclResult::HCCL_SUCCESS;
112 193 : }
113 :
114 186 : HcclResult CcuPfeMgr::GetPfeStrategy(uint32_t feId, PfeJettyStrategy &pfeJettyStrategy) const
115 : {
116 186 : const auto &iter = pfeJettyMap_.find(feId);
117 186 : if (iter == pfeJettyMap_.end()) {
118 2 : HCCL_ERROR("[CcuPfeMgr][%s] failed, feId[%u] is not found, "
119 : "devLogicId[%d], dieId[%u].", __func__, feId, devLogicId_, dieId_);
120 2 : return HCCL_E_NOT_FOUND;
121 : }
122 :
123 184 : pfeJettyStrategy = iter->second;
124 184 : HCCL_RUN_INFO("[CcuPfeMgr][%s] dieId[%u] feId[%u] pfeId[%u] size[%u] "
125 : "startTaJettyId[%u] startLocalJettyCtxId[%u]", __func__, dieId_,
126 : pfeJettyStrategy.feId, pfeJettyStrategy.pfeId, pfeJettyStrategy.size,
127 : pfeJettyStrategy.startTaJettyId, pfeJettyStrategy.startLocalJettyCtxId);
128 :
129 184 : return HCCL_SUCCESS;
130 : }
131 :
132 : }; // Hccl
|