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