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_drv_handle.h"
12 :
13 : #include "ccu_log.h"
14 :
15 : #include "hccp_tlv.h"
16 : #include "hccp_tlv_hdc_mgr.h"
17 :
18 : #include "ccu_res_specs.h"
19 : #include "ccu_pfe_cfg_mgr.h"
20 : #include "ccu_comp.h"
21 : #include "ccu_res_batch_allocator.h"
22 : #include "ccu_kernel_mgr.h"
23 :
24 :
25 : // 支持ccu新老通信域混跑临时添加
26 : #include "unified_platform/ccu/ccu_device/ccu_res_specs.h"
27 : #include "unified_platform/ccu/ccu_device/ccu_component/ccu_component.h"
28 : #include "unified_platform/ccu/ccu_device/ccu_res_batch_allocator.h"
29 : #include "unified_platform/ccu/ccu_context/ccu_context_mgr_imp.h"
30 : #include "hccp_tlv_hdc_manager.h"
31 :
32 : #include "exception_handler.h"
33 :
34 : namespace hcomm {
35 :
36 0 : inline bool CheckCcuOpenSourceEnable()
37 : {
38 : // A6 不支持legacy ccu mc2,可以完全切换至开源流程
39 0 : auto devType = DevType::DEV_TYPE_COUNT;
40 0 : (void)hrtGetDeviceType(devType);
41 0 : return devType == DevType::DEV_TYPE_960;
42 : }
43 :
44 0 : static HcclResult HccpRaTlvRequest(const TlvHandle tlvHandle,
45 : const u32 tlvModuleType, const u32 tlvCcuMsgType)
46 : {
47 0 : CHK_PTR_NULL(tlvHandle);
48 0 : struct TlvMsg sendMsg {};
49 0 : struct TlvMsg recvMsg {};
50 0 : sendMsg.type = tlvCcuMsgType;
51 :
52 0 : HCCL_INFO("[%s] tlvHandle[%p].", __func__, tlvHandle);
53 0 : constexpr u32 RA_TLV_REQUEST_UNAVAIL = 128308;
54 0 : int32_t ret = RaTlvRequest(tlvHandle, tlvModuleType, &sendMsg, &recvMsg);
55 0 : if (ret == RA_TLV_REQUEST_UNAVAIL || ret == OTHERS_ENOTSUPP) {
56 0 : HCCL_RUN_WARNING("[%s] ra tlv request UNAVAIL, tlvHandle[%p], tlvModeulType[%u], tlvCcuMsgType[%u], ret[%d].",
57 : __func__, tlvHandle, tlvModuleType, tlvCcuMsgType, ret);
58 0 : return HCCL_E_AGAIN; // 代表CCU驱动已被拉起,需要等待其他进程退出
59 : }
60 :
61 0 : if (ret != 0) {
62 0 : HCCL_ERROR("[Request][RaTlv]errNo[0x%016llx] ra tlv request fail. "
63 : "return: ret[%d], module type[%u], message type[%u]",
64 : HCCL_ERROR_CODE(HcclResult::HCCL_E_NETWORK), tlvModuleType, tlvCcuMsgType);
65 0 : return HcclResult::HCCL_E_NETWORK;
66 : }
67 :
68 0 : HCCL_INFO("tlv request success, tlv module type[%u], "
69 : "message type[%u]", tlvModuleType, tlvCcuMsgType);
70 0 : return HcclResult::HCCL_SUCCESS;
71 : }
72 :
73 0 : CcuResult CcuDrvHandle::Init()
74 : {
75 0 : HCCL_RUN_INFO("[CcuDrvHandle][%s], deviceLogicId: %d", __func__, devLogicId_);
76 0 : CCU_CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<uint32_t>(devLogicId_), devPhyId_));
77 : // 支持ccu新老通信域混跑
78 : CCU_EXCEPTION_HANDLE_BEGIN
79 : // 初始化CCU平台层能力,有时序要求
80 : // 当前走进A5通信域,暂时不需要主动拉起HDC通道
81 : /* 为了支持ccu新老通信域混跑,暂时复用原有的tlv mgr,避免重复申请资源
82 : * auto &tlvHdcMgr = HccpTlvHdcMgr::GetInstance(devPhyId_);
83 : * CHK_RET(tlvHdcMgr.Init());
84 : * tlvHandle_ = tlvHdcMgr.GetHandle();
85 : */
86 :
87 0 : tlvHandle_ = Hccl::HccpTlvHdcManager::GetInstance().GetTlvHandle(devLogicId_);
88 0 : CCU_CHK_PTR_NULL(tlvHandle_);
89 : // 拉起CCU驱动如果因其他进程占用重复拉起时,返回EAGAIN,日志检查返回值打印warning
90 0 : auto ret = HccpRaTlvRequest(tlvHandle_, TLV_MODULE_TYPE_CCU, MSG_TYPE_CCU_INIT);
91 0 : if (ret == HcclResult::HCCL_E_AGAIN) {
92 0 : HCCL_RUN_WARNING("[%s] HccpRaTlvRequest ret[%d], repeat init ccu, deviceLogicId[%d].",
93 : __func__, ret, devLogicId_);
94 0 : return CcuResult::CCU_E_DRV_BUSY;
95 : }
96 0 : if (ret != HcclResult::HCCL_SUCCESS) {
97 0 : HCCL_ERROR("[%s] failed to init ccu driver, ret[%d] is unexpected.",
98 : __func__, ret);
99 0 : return CcuResult::CCU_E_DRV_INIT_FAILED;
100 : }
101 :
102 0 : if (!CheckCcuOpenSourceEnable()) {
103 0 : Hccl::CcuResSpecifications::GetInstance(devLogicId_).Init();
104 0 : Hccl::CcuComponent::GetInstance(devLogicId_).Init();
105 0 : Hccl::CcuResBatchAllocator::GetInstance(devLogicId_).Init();
106 0 : Hccl::CtxMgrImp::GetInstance(devLogicId_).Init();
107 : } else {
108 0 : CCU_CHK_RET(CcuResSpecifications::GetInstance(devLogicId_).Init());
109 0 : CCU_CHK_RET(CcuPfeCfgMgr::GetInstance(devLogicId_).Init());
110 0 : CCU_CHK_RET(CcuComponent::GetInstance(devLogicId_).Init());
111 0 : CCU_CHK_RET(CcuResBatchAllocator::GetInstance(devLogicId_).Init());
112 : }
113 :
114 0 : CCU_CHK_RET(CcuKernelMgr::GetInstance(devLogicId_).Init());
115 :
116 0 : CCU_EXCEPTION_HANDLE_END
117 :
118 0 : return CcuResult::CCU_SUCCESS;
119 : }
120 :
121 0 : static HcclResult CcuLegacyMgrDeinit(int32_t devLogicId)
122 : {
123 : // 释放有时序要求
124 : EXCEPTION_HANDLE_BEGIN
125 0 : Hccl::CtxMgrImp::GetInstance(devLogicId).Deinit();
126 0 : Hccl::CcuResBatchAllocator::GetInstance(devLogicId).Deinit();
127 0 : Hccl::CcuComponent::GetInstance(devLogicId).Deinit();
128 0 : Hccl::CcuResSpecifications::GetInstance(devLogicId).Deinit();
129 0 : EXCEPTION_HANDLE_END
130 :
131 0 : return HcclResult::HCCL_SUCCESS;
132 : }
133 :
134 0 : CcuResult CcuDrvHandle::Deinit()
135 : {
136 : // 释放流程不打断,不抛异常,尽量尝试释放所有资源
137 : // 释放有时序要求
138 0 : HCCL_RUN_INFO("[CcuDrvHandle] start to deinit ccu driver, deviceLogicId[%d].", devLogicId_);
139 0 : (void)CcuKernelMgr::GetInstance(devLogicId_).Deinit();
140 :
141 0 : if (!CheckCcuOpenSourceEnable()) {
142 0 : (void)CcuLegacyMgrDeinit(devLogicId_);
143 : } else {
144 0 : (void)CcuResBatchAllocator::GetInstance(devLogicId_).Deinit();
145 0 : (void)CcuComponent::GetInstance(devLogicId_).Deinit();
146 0 : (void)CcuPfeCfgMgr::GetInstance(devLogicId_).Deinit();
147 0 : (void)CcuResSpecifications::GetInstance(devLogicId_).Deinit();
148 : }
149 :
150 0 : if (tlvHandle_ != 0) {
151 0 : (void)HccpRaTlvRequest(tlvHandle_, TLV_MODULE_TYPE_CCU, MSG_TYPE_CCU_UNINIT);
152 0 : tlvHandle_ = 0;
153 : }
154 :
155 0 : return CcuResult::CCU_SUCCESS;
156 : }
157 :
158 27 : CcuDrvHandle::~CcuDrvHandle()
159 : {
160 27 : (void)Deinit();
161 27 : }
162 :
163 : } // namespace hcomm
|