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 : #ifndef HCCL_CCU_COMPONENT_H
12 : #define HCCL_CCU_COMPONENT_H
13 :
14 : #include <memory>
15 : #include <vector>
16 : #include <unordered_map>
17 :
18 : #include "hccl/hccl_types.h"
19 : #include "types.h"
20 :
21 : #include "ccu_channel_mgr.h"
22 : #include "ccu_res_allocator_legacy.h"
23 : #include "ccu_device_manager.h"
24 : #include "tp_manager.h"
25 : #include "ccu_error_handler.h"
26 :
27 : namespace Hccl {
28 :
29 : class CcuComponent {
30 : public:
31 : CcuComponent(const CcuComponent& that) = delete;
32 : CcuComponent& operator=(const CcuComponent& that) = delete;
33 :
34 : static CcuComponent& GetInstance(const int32_t deviceLogicId);
35 : void Init();
36 : void Deinit();
37 :
38 : HcclResult GetCcuResourceSpaceBufInfo(const uint8_t dieId, uint64_t& addr, uint64_t& size) const;
39 : HcclResult GetCcuResourceSpaceTokenInfo(const uint8_t dieId, uint64_t& tokenId, uint64_t& tokenValue) const;
40 : HcclResult GetCcuResourceSpaceTokenInfoForLocal(const uint8_t dieId, uint64_t& tokenId, uint64_t& tokenValue) const;
41 :
42 : HcclResult
43 : AllocChannels(const uint8_t dieId, const ChannelPara& channelPara, std::vector<ChannelInfo>& channelInfos);
44 : HcclResult ConfigChannel(const uint8_t dieId, const ChannelCfg& cfg);
45 : HcclResult ReleaseChannel(const uint8_t dieId, const uint32_t channelId);
46 :
47 : HcclResult GetLoopChannelId(const uint8_t srcDieId, const uint8_t dstDieId, uint32_t& channelId) const;
48 :
49 : HcclResult AllocRes(
50 : const uint8_t dieId, const ResType resType, const uint32_t num, const bool consecutive,
51 : vector<ResInfo>& resInfos);
52 : HcclResult ReleaseRes(const uint8_t dieId, const ResType resType, const uint32_t startId, const uint32_t num);
53 :
54 : HcclResult AllocIns(const uint8_t dieId, const uint32_t num, ResInfo& insInfo);
55 : HcclResult ReleaseIns(const uint8_t dieId, const ResInfo& insInfo);
56 : uint32_t GetInsConsecutiveRemainSize(const uint8_t dieId) const;
57 : HcclResult AllocCke(const uint8_t dieId, const uint32_t num, vector<ResInfo>& ckeInfos);
58 : HcclResult ReleaseCke(const uint8_t dieId, const vector<ResInfo>& ckeInfos);
59 : HcclResult AllocXn(const uint8_t dieId, const uint32_t num, vector<ResInfo>& xnInfos);
60 : HcclResult ReleaseXn(const uint8_t dieId, const vector<ResInfo>& xnInfos);
61 :
62 : HcclResult CleanDieCkes(const uint8_t dieId) const;
63 : HcclResult SetTaskKill();
64 : HcclResult SetTaskKillDone();
65 : HcclResult CleanTaskKillState() const;
66 :
67 : const std::array<bool, MAX_CCU_IODIE_NUM>& GetDieEnableFlags() const;
68 6 : bool IsInited() const { return ifInit; }
69 :
70 : private:
71 : static constexpr uint32_t INVALID_DEV_ID = 0xFFFFFFFF;
72 : bool ifInit{false};
73 : int32_t devLogicId{static_cast<int32_t>(INVALID_DEV_ID)};
74 : uint32_t devPhyId{INVALID_DEV_ID};
75 : CcuVersion ccuVersion{CcuVersion::CCU_INVALID};
76 : std::array<bool, MAX_CCU_IODIE_NUM> dieEnableFlags{}; // 根据资源规格的记录可用的die
77 :
78 : // 记录环回设备信息,dieId, (feId, ipAddr)
79 : std::unordered_map<uint8_t, std::pair<uint32_t, IpAddress>> loopFeIpAddrMap{};
80 : // 记录CCU资源空间Buffer,避免重复内存注册
81 : std::unordered_map<uint8_t, std::unique_ptr<LocalUbRmaBuffer>> ccuRmaBufferMap{};
82 : std::unordered_map<uint8_t, std::unique_ptr<LocalUbRmaBuffer>> localCcuRmaBufferMap{};
83 : std::vector<std::unique_ptr<LocalUbRmaBuffer>> additionalCcuRmaBufferMap{};
84 : // 资源管理器
85 : std::array<std::unique_ptr<CcuChannelMgr>, MAX_CCU_IODIE_NUM> channelMgrs{};
86 : std::array<std::unique_ptr<CcuResAllocator>, MAX_CCU_IODIE_NUM> resAllocators{};
87 : // 环回channel编号
88 : std::array<uint32_t, MAX_CCU_IODIE_NUM> loopChannelIds{};
89 : // 环回jetty资源信息
90 : std::unordered_map<uint8_t, std::vector<HrtRaUbJettyCreatedOutParam>> createdOutParamMap{};
91 : using ImportOutParamPair = std::pair<RdmaHandle, HrtRaUbJettyImportedOutParam>;
92 : std::unordered_map<uint8_t, std::vector<ImportOutParamPair>> importedOutParamMap{};
93 : std::unordered_map<IpAddress, TpInfo> tpInfoMap{};
94 : std::unordered_map<IpAddress, TpAttrInfo> tpAttrInfoMap{};
95 : std::unordered_map<IpAddress, uint32_t> psnMap{};
96 :
97 : // CCU Task Kill相关状态
98 : enum class CcuTaskKillStatus : uint8_t { INIT = 0, TASK_KILL = 1, KILL_DONE = 2, CLEAN_TIF = 3, INVALID = 4 };
99 : CcuTaskKillStatus status{CcuTaskKillStatus::INVALID};
100 : std::mutex innerMutex;
101 : std::mutex taskKillMutex_;
102 :
103 83 : explicit CcuComponent() = default;
104 : ~CcuComponent();
105 :
106 : void CheckDiesEnable();
107 : void ChooseLoopEid(bool& dieDrvEnableFlag, uint8_t dieId);
108 : HcclResult GetLoopFeIpByDieId(const uint8_t dieId, uint32_t& feId, IpAddress& ipAddr);
109 : void CreateCcuRmaBuffer();
110 : void CreateResourceManagers();
111 : void CreateLoopChannels();
112 : HcclResult CreateLoopChannel(const uint8_t dieId, uint32_t& channelId);
113 : HcclResult
114 : CreateAndImportLoopJettys(const uint8_t dieId, const IpAddress& ipAddr, const vector<JettyInfo>& jettyInfos);
115 : TpInfo RequestNewTpInfo(const IpAddress& srcIpAddr, const IpAddress& dstIpAddr) const;
116 : TpInfo GetTpInfo(const IpAddress& ipAddr);
117 : TpAttrInfo GetLoopTpAttr(const IpAddress& ipAddr, const TpHandle tpHandle);
118 : uint32_t GetPsn(const IpAddress& ipAddr);
119 : HcclResult ConfigLoopChannel(const uint8_t dieId, const IpAddress& ipAddr, const ChannelInfo& channelInfo);
120 : void ConfigMsIdToken();
121 :
122 : void ReleaseJettyRes();
123 : void UnimportAllJetty();
124 : void DestroyAllJetty();
125 :
126 : void SetProcess(CcuOpcodeType opCode) const;
127 : void PrintCcuMissionStatus(int32_t devLogicId) const;
128 : };
129 :
130 : }; // namespace Hccl
131 :
132 : #endif
|