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