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 "sqe_mgr.h"
12 : #include <chrono>
13 : #include "stl_util.h"
14 : #include "exception_util.h"
15 : #include "null_ptr_exception.h"
16 : #include "internal_exception.h"
17 : #include "string_util.h"
18 : #include "drv_api_exception.h"
19 :
20 : namespace Hccl {
21 :
22 : constexpr u64 COMMIT_TIMEOUT = 30;
23 :
24 4 : HcclResult SqeMgr::Begin(u32 sqId)
25 : {
26 12 : HCCL_INFO("SqeMgr::%s start, sqId %u", __func__, sqId);
27 4 : if (Contain(sqInfos, sqId)) {
28 3 : if (sqInfos[sqId]->sqeCnt != 0) {
29 : // called Begin twice in the same round
30 3 : HCCL_ERROR("sqInfos[sqId]->sqeCnt != 0");
31 1 : return HcclResult::HCCL_E_INTERNAL;
32 : }
33 2 : return HcclResult::HCCL_SUCCESS;
34 : }
35 1 : std::unique_ptr<SqInfo> sqInfo = std::make_unique<SqInfo>();
36 1 : sqInfo->sqeCnt = 0;
37 1 : sqInfo->sqDepth = QuerySqDepth(sqId);
38 1 : sqInfo->sqTail = QuerySqTail(sqId);
39 1 : sqInfo->sqHead = QuerySqHead(sqId);
40 1 : sqInfo->sqBaseAddr = QuerySqBaseAddr(sqId);
41 1 : s32 ret = memset_s(sqInfo->sqeBuffer, sizeof(sqInfo->sqeBuffer), 0, sizeof(sqInfo->sqeBuffer));
42 1 : if (ret != EOK) {
43 0 : std::string formatStr = StringFormat("SqeMgr::%s memcpy_s failed. errorno[%d]", __func__, ret);
44 0 : HCCL_ERROR("%s", formatStr.c_str());
45 0 : THROW<InternalException>(formatStr);
46 0 : }
47 :
48 1 : sqInfos[sqId] = std::move(sqInfo);
49 1 : if (sqInfos[sqId] == nullptr) {
50 0 : HCCL_ERROR("SqeMgr::Begin sqInfos[sqId] == nullptr, sq_id=%u", sqId);
51 0 : return HcclResult::HCCL_E_INTERNAL;
52 : }
53 3 : HCCL_INFO("SqeMgr::%s end", __func__);
54 1 : return HcclResult::HCCL_SUCCESS;
55 1 : }
56 :
57 7 : HcclResult SqeMgr::Add(u32 sqId, HcclSqe* sqe)
58 : {
59 7 : if (sqe == nullptr) {
60 6 : HCCL_ERROR("SqeMgr::Add HcclSqe *sqe is nullptr");
61 2 : return HcclResult::HCCL_E_PTR;
62 : }
63 15 : HCCL_INFO("SqeMgr::%s start sq_id=%u", __func__, sqId);
64 5 : if (!Contain(sqInfos, sqId)) {
65 0 : HCCL_ERROR("no available buffer found for sq_id=%u", sqId);
66 0 : return HcclResult::HCCL_E_NOT_FOUND;
67 : }
68 :
69 5 : SqInfo* sqInfo = sqInfos[sqId].get();
70 5 : if (sqInfo == nullptr) {
71 3 : HCCL_ERROR("SqeMgr::add failed sqInfo is null sq_id=%u", sqId);
72 1 : return HcclResult::HCCL_E_NOT_FOUND;
73 : }
74 :
75 4 : void* nextBufferAddr = static_cast<u8*>(sqInfo->sqeBuffer) + sqInfo->sqeCnt * AC_SQE_SIZE;
76 :
77 12 : HCCL_INFO("SqeMgr::%s sqe->GetSqe() %llu", __func__, sqe->GetSqe());
78 :
79 4 : AddSqeToBuffer(nextBufferAddr, reinterpret_cast<void*>(sqe->GetSqe()));
80 4 : sqInfo->sqeCnt++;
81 12 : HCCL_INFO("SqeMgr::%s end sqInfo->sqeCnt[%u]", __func__, sqInfo->sqeCnt);
82 4 : return HcclResult::HCCL_SUCCESS;
83 : }
84 :
85 3 : HcclResult SqeMgr::Commit(u32 sqId)
86 : {
87 9 : HCCL_INFO("SqeMgr::%s start sq_id=%u", __func__, sqId);
88 3 : if (!Contain(sqInfos, sqId)) {
89 3 : HCCL_ERROR("no available buffer found for sq_id=%u", sqId);
90 1 : return HcclResult::HCCL_E_NOT_FOUND;
91 : }
92 :
93 2 : SqInfo* sqInfo = sqInfos[sqId].get();
94 2 : CHECK_NULLPTR(sqInfo, "[SqeMgr::Commit] sqInfo is nullptr!");
95 2 : u32 availableSpace = GetTailToHeadDist(sqId, sqInfo->sqHead, sqInfo->sqTail);
96 2 : auto startTime = std::chrono::steady_clock::now();
97 2 : auto timeout = std::chrono::seconds(COMMIT_TIMEOUT);
98 6 : HCCL_INFO("SqeMgr::%s start", __func__);
99 2 : while (availableSpace <= sqInfo->sqeCnt) {
100 0 : HCCL_INFO(
101 : "SqeMgr::%s while loop availableSpace %u <= sqInfo->sqeCnt %u", __func__, availableSpace, sqInfo->sqeCnt);
102 0 : if ((std::chrono::steady_clock::now() - startTime) >= timeout) {
103 0 : HCCL_ERROR("Rtsq full, timeout 30s. cur head: %u, sqId: %u", sqInfo->sqHead, sqId);
104 0 : return HcclResult::HCCL_E_TIMEOUT;
105 : }
106 0 : sqInfo->sqHead = QuerySqHead(sqId);
107 0 : availableSpace = GetTailToHeadDist(sqId, sqInfo->sqHead, sqInfo->sqTail);
108 : }
109 2 : u32 depthLeft = sqInfo->sqDepth - sqInfo->sqTail;
110 2 : if (sqInfo->sqeCnt <= depthLeft) { // 没有回绕
111 3 : HCCL_INFO(
112 : "SqeMgr::%s copy sqe from sqe buffer, cur tail: %u, size: %u", __func__, sqInfo->sqTail, sqInfo->sqeCnt);
113 2 : int ret = memcpy_s(
114 1 : reinterpret_cast<u8*>(sqInfo->sqBaseAddr) + sqInfo->sqTail * AC_SQE_SIZE, sqInfo->sqeCnt * AC_SQE_SIZE,
115 1 : sqInfo->sqeBuffer, sqInfo->sqeCnt * AC_SQE_SIZE);
116 1 : if (ret != 0) {
117 0 : THROW<InternalException>(StringFormat("SqeMgr::%s sqe memcpy_s failed, ret = %d", __func__, ret));
118 : }
119 : } else {
120 3 : HCCL_INFO(
121 : "SqeMgr::%s copy sqe twice, cnt: %u, tail: %u, depth remain: %u", __func__, sqInfo->sqeCnt, sqInfo->sqTail,
122 : depthLeft);
123 : // 先拷贝rtsq里剩余空间大小
124 2 : int ret = memcpy_s(
125 1 : reinterpret_cast<u8*>(sqInfo->sqBaseAddr) + sqInfo->sqTail * AC_SQE_SIZE, depthLeft * AC_SQE_SIZE,
126 1 : sqInfo->sqeBuffer, depthLeft * AC_SQE_SIZE);
127 1 : if (ret != 0) {
128 0 : THROW<InternalException>(
129 0 : StringFormat("SqeMgr::%s rtsq remaining space memcpy_s failed, ret = %d", __func__, ret));
130 : }
131 : // 拷贝剩余sqe
132 2 : ret = memcpy_s(
133 1 : reinterpret_cast<u8*>(sqInfo->sqBaseAddr), sqInfo->sqHead * AC_SQE_SIZE,
134 1 : sqInfo->sqeBuffer + depthLeft * AC_SQE_SIZE, (sqInfo->sqeCnt - depthLeft) * AC_SQE_SIZE);
135 1 : if (ret != 0) {
136 0 : THROW<InternalException>(StringFormat("SqeMgr::%s remaining sqe memcpy_s failed, ret = %d", __func__, ret));
137 : }
138 : }
139 :
140 : // set new tail position
141 2 : u32 newTail = static_cast<u32>((static_cast<u64>(sqInfo->sqTail) + sqInfo->sqeCnt) % sqInfo->sqDepth);
142 2 : ConfigSqTail(sqId, newTail);
143 2 : sqInfo->sqTail = newTail;
144 : // clear sqe buffer
145 2 : auto sRet = memset_s(sqInfo->sqeBuffer, sizeof(sqInfo->sqeBuffer), 0, sqInfo->sqeCnt * AC_SQE_SIZE);
146 2 : if (sRet != 0) {
147 0 : THROW<InternalException>(StringFormat("SqeMgr::%s remaining sqe memcpy_s failed, ret = %d", __func__, sRet));
148 : }
149 2 : sqInfo->sqeCnt = 0;
150 6 : HCCL_INFO("SqeMgr::%s end", __func__);
151 2 : return HcclResult::HCCL_SUCCESS;
152 : }
153 :
154 1 : u32 SqeMgr::QuerySqHead(u32 sqId)
155 : {
156 3 : HCCL_INFO("SqeMgr::%s", __func__);
157 1 : return QuerySqStatusByType(sqId, DRV_SQCQ_PROP_SQ_HEAD);
158 : }
159 :
160 1 : u32 SqeMgr::QuerySqTail(u32 sqId)
161 : {
162 3 : HCCL_INFO("SqeMgr::%s", __func__);
163 1 : return QuerySqStatusByType(sqId, DRV_SQCQ_PROP_SQ_TAIL);
164 : }
165 :
166 1 : u32 SqeMgr::QuerySqDepth(u32 sqId)
167 : {
168 3 : HCCL_INFO("SqeMgr::%s", __func__);
169 1 : return QuerySqStatusByType(sqId, DRV_SQCQ_PROP_SQ_DEPTH);
170 : }
171 :
172 2 : u64 SqeMgr::QuerySqBaseAddr(u32 sqId)
173 : {
174 : halSqCqQueryInfo queryInfo;
175 2 : queryInfo.tsId = 0;
176 2 : queryInfo.sqId = sqId;
177 2 : queryInfo.cqId = 0;
178 2 : queryInfo.type = DRV_NORMAL_TYPE;
179 2 : queryInfo.prop = DRV_SQCQ_PROP_SQ_BASE;
180 :
181 6 : HCCL_INFO("SqeMgr::%s begin", __func__);
182 2 : drvError_t ret = halSqCqQuery(devPhyId, &queryInfo);
183 2 : if (ret != 0) {
184 : std::string formatStr
185 1 : = StringFormat("SqeMgr::%s call halSqCqQuery failed, devPhyId %d, ret %d", __func__, devPhyId, ret);
186 3 : HCCL_ERROR("%s", formatStr.c_str());
187 1 : THROW<DrvApiException>(formatStr);
188 1 : }
189 3 : HCCL_INFO("SqeMgr::%s end", __func__);
190 :
191 1 : return ((static_cast<u64>(queryInfo.value[1])) << UINT32_BIT_NUM) | queryInfo.value[0];
192 : }
193 :
194 4 : u32 SqeMgr::QuerySqStatusByType(u32 sqId, drvSqCqPropType_t type) const
195 : {
196 : halSqCqQueryInfo queryInfo;
197 4 : queryInfo.tsId = 0;
198 4 : queryInfo.sqId = sqId;
199 4 : queryInfo.cqId = 0;
200 4 : queryInfo.type = DRV_NORMAL_TYPE;
201 4 : queryInfo.prop = type;
202 12 : HCCL_INFO("%s::halSqCqQuery begin, type %d sqiId %u", __func__, type, sqId);
203 4 : drvError_t ret = halSqCqQuery(devPhyId, &queryInfo);
204 4 : if (ret != 0) {
205 : std::string formatStr
206 1 : = StringFormat("SqeMgr::%s call halSqCqQuery failed, devPhyId %d, ret %d", __func__, devPhyId, ret);
207 3 : HCCL_ERROR("%s", formatStr.c_str());
208 1 : THROW<DrvApiException>(formatStr);
209 1 : }
210 9 : HCCL_INFO("%s::halSqCqQuery end", __func__);
211 :
212 3 : return queryInfo.value[0];
213 : }
214 :
215 1 : void SqeMgr::ConfigSqTail(u32 sqId, u32 value)
216 : {
217 3 : HCCL_INFO("SqeMgr::%s sqId %u, value %u", __func__, sqId, value);
218 1 : ConfigSqStatusByType(sqId, DRV_SQCQ_PROP_SQ_TAIL, value);
219 1 : }
220 :
221 1 : void SqeMgr::ConfigSqStatusByType(u32 sqId, drvSqCqPropType_t type, u32 value) const
222 : {
223 : halSqCqConfigInfo configInfo;
224 1 : configInfo.tsId = 0;
225 1 : configInfo.sqId = sqId;
226 1 : configInfo.cqId = 0;
227 1 : configInfo.type = DRV_NORMAL_TYPE;
228 1 : configInfo.prop = type;
229 1 : configInfo.value[0] = value;
230 :
231 3 : HCCL_INFO("SqeMgr::%s start", __func__);
232 1 : drvError_t ret = halSqCqConfig(devPhyId, &configInfo);
233 1 : if (ret != 0) {
234 3 : HCCL_ERROR("call halSqCqConfig failed, ret %d", ret);
235 : }
236 3 : HCCL_INFO("SqeMgr::%s end", __func__);
237 1 : }
238 :
239 2 : u32 SqeMgr::GetTailToHeadDist(u32 sqId, u32 head, u32 tail)
240 : {
241 2 : if (head == tail) {
242 1 : return sqInfos[sqId]->sqDepth;
243 : }
244 1 : return (tail < head) ? head - tail : sqInfos[sqId]->sqDepth - (tail - head);
245 : }
246 :
247 5 : void SqeMgr::AddSqeToBuffer(void* bufferAddr, void* sqeAddr) const
248 : {
249 5 : if (bufferAddr == nullptr || sqeAddr == nullptr) {
250 1 : std::string formatStr = StringFormat("SqeMgr::%s bufferAddr[%u], sqeAddr[%u]", __func__, bufferAddr, sqeAddr);
251 3 : HCCL_ERROR("%s", formatStr.c_str());
252 1 : THROW<NullPtrException>(formatStr);
253 1 : }
254 4 : s32 ret = memcpy_s(bufferAddr, AC_SQE_SIZE, sqeAddr, AC_SQE_SIZE);
255 4 : if (ret != 0) {
256 0 : std::string formatStr = StringFormat("SqeMgr::%s memcpy_s failed, ret = %d", __func__, ret);
257 0 : HCCL_ERROR("%s", formatStr.c_str());
258 0 : THROW<InternalException>(formatStr);
259 0 : }
260 4 : }
261 :
262 89 : SqeMgr::SqeMgr(u32 devPhysicalId) : devPhyId(devPhysicalId) {}
263 :
264 : } // namespace Hccl
|