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 "rtsq_base.h"
12 : #include "log.h"
13 : #include "drv_api_exception.h"
14 : #include "exception_util.h"
15 : #include "internal_exception.h"
16 : #include "sqe_v82.h"
17 : #include <unordered_map>
18 : namespace Hccl {
19 279 : RtsqBase::RtsqBase(u32 devPhyId, u32 streamId, u32 sqId) : devPhyId_(devPhyId), streamId_(streamId), sqId_(sqId)
20 : {
21 279 : auto ret = drvGetLocalDevIDByHostDevID(devPhyId_, &localDevId_);
22 279 : if (ret != DRV_ERROR_NONE) {
23 : std::string formatStr = StringFormat(
24 0 : "RtsqBase::%s call drvGetLocalDevIDByHostDevID failed, devPhyId %u, ret %d", __func__, devPhyId_, ret);
25 0 : THROW<DrvApiException>(formatStr);
26 0 : }
27 :
28 279 : sqHead_ = QuerySqHead();
29 279 : sqTail_ = QuerySqTail();
30 279 : sqDepth_ = QuerySqDepth();
31 279 : sqBaseAddr_ = QuerySqBaseAddr();
32 :
33 279 : if (sqDepth_ == 0) {
34 0 : THROW<InternalException>("sqDepth_ cannot be zero.");
35 : }
36 661 : HCCL_INFO("%s, %s", __func__, GetHwSqDescribe().c_str());
37 279 : }
38 :
39 1 : void RtsqBase::Reset(bool reset)
40 : {
41 1 : sqHead_ = QuerySqHead();
42 1 : sqTail_ = QuerySqTail();
43 :
44 1 : if (reset) {
45 0 : CHK_PRT_CONT(
46 : sqHead_ != 0 || sqTail_ != 0,
47 : HCCL_ERROR("RtsqBase::%s, sqHead_=%u, sqTail_=%u", __func__, sqHead_, sqTail_));
48 : }
49 :
50 1 : sqDepth_ = QuerySqDepth();
51 1 : sqBaseAddr_ = QuerySqBaseAddr();
52 1 : SetTaskIdBySqeId();
53 3 : HCCL_INFO("%s, %s", __func__, GetHwSqDescribe().c_str());
54 1 : }
55 :
56 280 : std::string RtsqBase::GetHwSqDescribe() const
57 : {
58 : return StringFormat(
59 : "devPhyId=%u, localDevId=%u, streamId=%u, sqId=%u, sqDepth=%u, sqBaseAddr=0x%llx, "
60 : "currentHead=%u, currentTail=%u, cqeStatus=%u, taskId=%u",
61 280 : devPhyId_, localDevId_, streamId_, sqId_, sqDepth_, sqBaseAddr_, QuerySqHead(), QuerySqTail(), QueryCqeStatus(),
62 280 : taskId_);
63 : }
64 :
65 529 : u32 RtsqBase::QuerySqStatusByType(drvSqCqPropType_t givenType) const
66 : {
67 : halSqCqQueryInfo queryInfo;
68 :
69 529 : queryInfo.tsId = 0;
70 529 : queryInfo.sqId = sqId_;
71 529 : queryInfo.cqId = 0;
72 529 : queryInfo.type = DRV_NORMAL_TYPE;
73 529 : queryInfo.prop = givenType;
74 529 : drvError_t ret = halSqCqQuery(localDevId_, &queryInfo);
75 529 : if (ret != 0) {
76 : std::string formatStr = StringFormat(
77 1 : "RtsqBase::%s call halSqCqQuery failed, localDevId %u, ret %d, givenType=%u", __func__, localDevId_, ret,
78 1 : givenType);
79 1 : THROW<DrvApiException>(formatStr);
80 1 : }
81 :
82 528 : return queryInfo.value[0];
83 : }
84 :
85 89 : u64 RtsqBase::QuerySqBaseAddr() const
86 : {
87 : halSqCqQueryInfo queryInfo;
88 89 : queryInfo.tsId = 0;
89 89 : queryInfo.sqId = sqId_;
90 89 : queryInfo.cqId = 0;
91 89 : queryInfo.type = DRV_NORMAL_TYPE;
92 89 : queryInfo.prop = DRV_SQCQ_PROP_SQ_BASE;
93 89 : drvError_t ret = halSqCqQuery(localDevId_, &queryInfo);
94 89 : if (ret != 0) {
95 : std::string formatStr
96 1 : = StringFormat("RtsqBase::%s call halSqCqQuery failed, localDevId %u, ret %d", __func__, localDevId_, ret);
97 1 : THROW<DrvApiException>(formatStr);
98 1 : }
99 88 : HCCL_INFO("RtsqBase::%s end", __func__);
100 :
101 : // 参照 driver API,BaseAddress为64bit,由两个32bit拼接而成,高32bit为 value[1], 低32bit为value[0]
102 88 : return ((static_cast<u64>(queryInfo.value[1])) << 32) | queryInfo.value[0];
103 : }
104 :
105 563 : u32 RtsqBase::QuerySqHead() const { return QuerySqStatusByType(drvSqCqPropType_t::DRV_SQCQ_PROP_SQ_HEAD); }
106 563 : u32 RtsqBase::QuerySqTail() const { return QuerySqStatusByType(drvSqCqPropType_t::DRV_SQCQ_PROP_SQ_TAIL); }
107 88 : u32 RtsqBase::QuerySqDepth() const { return QuerySqStatusByType(drvSqCqPropType_t::DRV_SQCQ_PROP_SQ_DEPTH); }
108 280 : u32 RtsqBase::QueryCqeStatus() const { return QuerySqStatusByType(drvSqCqPropType_t::DRV_SQCQ_PROP_SQ_CQE_STATUS); }
109 :
110 1 : void RtsqBase::ConfigSqStatusByType(drvSqCqPropType_t givenType, u32 value) const
111 : {
112 : halSqCqConfigInfo configInfo;
113 1 : configInfo.tsId = 0;
114 1 : configInfo.sqId = sqId_;
115 1 : configInfo.cqId = 0;
116 1 : configInfo.type = DRV_NORMAL_TYPE;
117 1 : configInfo.prop = givenType;
118 1 : configInfo.value[0] = value;
119 :
120 1 : drvError_t ret = halSqCqConfig(localDevId_, &configInfo);
121 1 : if (UNLIKELY(ret != 0)) {
122 : std::string formatStr
123 1 : = StringFormat("RtsqBase::%s call halSqCqConfig failed, localDevId %u, ret %d", __func__, localDevId_, ret);
124 1 : THROW<DrvApiException>(formatStr);
125 1 : }
126 0 : }
127 :
128 8 : void RtsqBase::ConfigSqTail(u32 value)
129 : {
130 24 : HCCL_INFO("RtsqBase::%s, value=%u", __func__, value);
131 8 : ConfigSqStatusByType(drvSqCqPropType_t::DRV_SQCQ_PROP_SQ_TAIL, value);
132 8 : }
133 0 : void RtsqBase::ConfigDisableToEnable(u32 value)
134 : {
135 0 : HCCL_INFO("RtsqBase::%s, value=%u", __func__, value);
136 0 : ConfigSqStatusByType(drvSqCqPropType_t::DRV_SQCQ_PROP_SQ_DISABLE_TO_ENABLE, value);
137 0 : }
138 :
139 0 : HcclResult RtsqBase::GetStreamIdAndTaskIdBySqIdx(u32 sqIdx, uint16_t& streamId, uint16_t& taskId) const
140 : {
141 0 : if (sqBaseAddr_ == 0 || sqIdx >= sqDepth_) {
142 0 : HCCL_ERROR("[%s]fail, sqBaseAddr_[0x%llu], sqIdx[%u]", __func__, sqBaseAddr_, sqIdx);
143 0 : return HCCL_E_PARA;
144 : }
145 :
146 0 : Rt91095StarsNotifySqe* sqe = (Rt91095StarsNotifySqe*)(sqBaseAddr_ + sqIdx * RTSQ_SQE_SIZE);
147 0 : streamId = sqe->header.rtStreamId;
148 0 : taskId = sqe->header.taskId;
149 0 : HCCL_INFO("[%s]sqId:%u, streamId:%u, taskId:%u", __func__, sqId_, streamId, taskId);
150 0 : return HCCL_SUCCESS;
151 : }
152 : } // namespace Hccl
|