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