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