Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "npu_driver.hpp"
11 : #include "driver/ascend_hal.h"
12 : #include "driver/ascend_inpackage_hal.h"
13 : #include "errcode_manage.hpp"
14 : #include "error_message_manage.hpp"
15 : #include "rt_log.h"
16 :
17 : namespace cce {
18 : namespace runtime {
19 :
20 3 : rtError_t NpuDriver::MbufInit(rtMemBuffCfg_t* const cfg)
21 : {
22 3 : RT_LOG(RT_LOG_INFO, "init device mem buff.");
23 :
24 3 : COND_RETURN_WARN(&halBuffInit == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halBuffInit does not exist.");
25 : drvError_t drvRet;
26 3 : if (cfg != nullptr) {
27 2 : BuffCfg bufCfg = {};
28 130 : for (int32_t i = 0; i < RT_MEM_BUFF_MAX_CFG_NUM; ++i) {
29 128 : bufCfg.cfg[i].cfg_id = cfg->cfg[i].cfgId;
30 128 : bufCfg.cfg[i].total_size = cfg->cfg[i].totalSize;
31 128 : bufCfg.cfg[i].blk_size = cfg->cfg[i].blkSize;
32 128 : bufCfg.cfg[i].max_buf_size = cfg->cfg[i].maxBufSize;
33 128 : bufCfg.cfg[i].page_type = cfg->cfg[i].pageType;
34 :
35 128 : bufCfg.cfg[i].elasticEnable = cfg->cfg[i].elasticEnable;
36 128 : bufCfg.cfg[i].elasticRate = cfg->cfg[i].elasticRate;
37 128 : bufCfg.cfg[i].elasticRateMax = cfg->cfg[i].elasticRateMax;
38 128 : bufCfg.cfg[i].elasticHighLevel = cfg->cfg[i].elasticHighLevel;
39 128 : bufCfg.cfg[i].elasticLowLevel = cfg->cfg[i].elasticLowLevel;
40 : }
41 2 : drvRet = static_cast<drvError_t>(halBuffInit(&bufCfg));
42 : } else {
43 1 : drvRet = static_cast<drvError_t>(halBuffInit(nullptr));
44 : }
45 :
46 3 : COND_RETURN_WARN(drvRet == DRV_ERROR_REPEATED_INIT, RT_GET_DRV_ERRCODE(drvRet), "repeated init"); // special state
47 3 : if (drvRet != DRV_ERROR_NONE) {
48 1 : DRV_ERROR_PROCESS(drvRet, "Call driver api halBuffInit failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
49 1 : return RT_GET_DRV_ERRCODE(drvRet);
50 : }
51 2 : return RT_ERROR_NONE;
52 : }
53 :
54 2 : rtError_t NpuDriver::MbufAlloc(rtMbufPtr_t* const mbufPtr, const uint64_t size)
55 : {
56 2 : RT_LOG(RT_LOG_INFO, "alloc mbuf, size=%" PRIu64, size);
57 2 : COND_RETURN_WARN(&halMbufAlloc == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufAlloc does not exist.");
58 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufAlloc(size, RtPtrToPtr<Mbuf**>(mbufPtr)));
59 2 : if (drvRet != DRV_ERROR_NONE) {
60 1 : DRV_ERROR_PROCESS(
61 : drvRet, "Call driver api halMbufAlloc failed, drvRetCode=%d, size=%" PRIu64 "(bytes).",
62 : static_cast<int32_t>(drvRet), size);
63 1 : return RT_GET_DRV_ERRCODE(drvRet);
64 : }
65 1 : return RT_ERROR_NONE;
66 : }
67 :
68 5 : rtError_t NpuDriver::MbufAllocEx(
69 : rtMbufPtr_t* const mbufPtr, const uint64_t size, const uint64_t flag, const int32_t grpId)
70 : {
71 5 : RT_LOG(RT_LOG_INFO, "alloc mbuf, size=%" PRIu64, size);
72 5 : if (flag == NORMAL_MEM) {
73 2 : COND_RETURN_WARN(
74 : &halMbufAlloc == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufAlloc does not exist.");
75 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufAlloc(size, RtPtrToPtr<Mbuf**>(mbufPtr)));
76 2 : if (drvRet != DRV_ERROR_NONE) {
77 1 : DRV_ERROR_PROCESS(
78 : drvRet, "Call driver api halMbufAlloc failed, drvRetCode=%d, size=%" PRIu64 "(bytes).",
79 : static_cast<int32_t>(drvRet), size);
80 1 : return RT_GET_DRV_ERRCODE(drvRet);
81 : }
82 3 : } else if (flag == DVPP_MEM) {
83 2 : COND_RETURN_WARN(
84 : &halMbufAllocEx == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufAlloc does not exist.");
85 2 : constexpr uint32_t align = 128U; // dvpp default 128-bit alignment
86 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufAllocEx(
87 : size, align, static_cast<unsigned long>(BUFF_SP_DVPP | BUFF_SP_HUGEPAGE_PRIOR), grpId,
88 2 : RtPtrToPtr<Mbuf**>(mbufPtr)));
89 2 : if (drvRet != DRV_ERROR_NONE) {
90 1 : DRV_ERROR_PROCESS(
91 : drvRet, "Call driver api halMbufAllocEx failed, drvRetCode=%d, size=%" PRIu64 "(bytes).",
92 : static_cast<int32_t>(drvRet), size);
93 1 : return RT_GET_DRV_ERRCODE(drvRet);
94 : }
95 : } else {
96 : RT_LOG_OUTER_MSG_INVALID_PARAM(flag, "[0, 1]");
97 1 : return RT_ERROR_INVALID_VALUE;
98 : }
99 2 : return RT_ERROR_NONE;
100 : }
101 :
102 2 : rtError_t NpuDriver::MbufFree(rtMbufPtr_t const memBuf)
103 : {
104 2 : RT_LOG(RT_LOG_INFO, "free mbuf");
105 2 : COND_RETURN_WARN(&halMbufFree == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufFree does not exist.");
106 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufFree(RtPtrToPtr<Mbuf*>(memBuf)));
107 2 : if (drvRet != DRV_ERROR_NONE) {
108 1 : DRV_ERROR_PROCESS(drvRet, "Call driver api halMbufFree failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
109 1 : return RT_GET_DRV_ERRCODE(drvRet);
110 : }
111 1 : return RT_ERROR_NONE;
112 : }
113 :
114 2 : rtError_t NpuDriver::MbufBuild(void* const buff, const uint64_t size, rtMbufPtr_t* mbufPtr)
115 : {
116 2 : RT_LOG(RT_LOG_INFO, "use buff to alloc mbuf, size=%" PRIu64, size);
117 2 : COND_RETURN_WARN(&halMbufBuild == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufBuild does not exist.");
118 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufBuild(buff, size, RtPtrToPtr<Mbuf**>(mbufPtr)));
119 2 : if (drvRet != DRV_ERROR_NONE) {
120 1 : DRV_ERROR_PROCESS(
121 : drvRet, "Call driver api halMbufBuild failed, drvRetCode=%d, size=%" PRIu64 "(bytes).",
122 : static_cast<int32_t>(drvRet), size);
123 1 : return RT_GET_DRV_ERRCODE(drvRet);
124 : }
125 :
126 1 : return RT_ERROR_NONE;
127 : }
128 :
129 2 : rtError_t NpuDriver::MbufUnBuild(const rtMbufPtr_t mbufPtr, void** const buff, uint64_t* const size)
130 : {
131 2 : RT_LOG(RT_LOG_INFO, "unBuild the head of mbufPtr, and free the head");
132 2 : COND_RETURN_WARN(
133 : &halMbufUnBuild == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufUnBuild does not exist.");
134 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufUnBuild(RtPtrToPtr<Mbuf*>(mbufPtr), buff, size));
135 2 : if (drvRet != DRV_ERROR_NONE) {
136 1 : DRV_ERROR_PROCESS(
137 : drvRet, "Call driver api halMbufUnBuild failed, drvRetCode=%d, size=%" PRIu64 "(bytes).",
138 : static_cast<int32_t>(drvRet), (*size));
139 1 : return RT_GET_DRV_ERRCODE(drvRet);
140 : }
141 :
142 1 : return RT_ERROR_NONE;
143 : }
144 :
145 2 : rtError_t NpuDriver::MbufGet(const rtMbufPtr_t mbufPtr, void* const buff, const uint64_t size)
146 : {
147 2 : RT_LOG(RT_LOG_INFO, "get mbufPtr");
148 2 : COND_RETURN_WARN(&halBuffGet == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halBuffGet does not exist.");
149 2 : const drvError_t drvRet = static_cast<drvError_t>(halBuffGet(RtPtrToPtr<Mbuf*>(mbufPtr), buff, size));
150 2 : if (drvRet != DRV_ERROR_NONE) {
151 1 : return RT_GET_DRV_ERRCODE(drvRet);
152 : }
153 :
154 1 : return RT_ERROR_NONE;
155 : }
156 :
157 1 : rtError_t NpuDriver::MbufPut(const rtMbufPtr_t mbufPtr, void* const buff)
158 : {
159 1 : RT_LOG(RT_LOG_INFO, "put mbufPtr");
160 1 : COND_RETURN_WARN(&halBuffPut == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halBuffPut does not exist.");
161 1 : halBuffPut(RtPtrToPtr<Mbuf*>(mbufPtr), buff);
162 :
163 1 : return RT_ERROR_NONE;
164 : }
165 :
166 2 : rtError_t NpuDriver::MbufSetDataLen(const rtMbufPtr_t mbufPtr, const uint64_t len)
167 : {
168 2 : RT_LOG(RT_LOG_INFO, "set mbuf data len.");
169 :
170 2 : if (&halMbufSetDataLen == nullptr) {
171 0 : return RT_GET_DRV_ERRCODE(DRV_ERROR_NOT_SUPPORT);
172 : }
173 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufSetDataLen(RtPtrToPtr<Mbuf*>(mbufPtr), len));
174 2 : if (drvRet != DRV_ERROR_NONE) {
175 1 : DRV_ERROR_PROCESS(
176 : drvRet, "Call driver api halMbufSetDataLen failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
177 1 : return RT_GET_DRV_ERRCODE(drvRet);
178 : }
179 1 : return RT_ERROR_NONE;
180 : }
181 :
182 2 : rtError_t NpuDriver::MbufGetDataLen(const rtMbufPtr_t mbufPtr, uint64_t* len)
183 : {
184 2 : RT_LOG(RT_LOG_INFO, "get mbuf data len.");
185 2 : COND_RETURN_WARN(
186 : &halMbufGetDataLen == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufGetDataLen does not exist.");
187 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufGetDataLen(RtPtrToPtr<Mbuf*>(mbufPtr), len));
188 2 : if (drvRet != DRV_ERROR_NONE) {
189 1 : DRV_ERROR_PROCESS(
190 : drvRet, "Call driver api halMbufGetDataLen failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
191 1 : return RT_GET_DRV_ERRCODE(drvRet);
192 : }
193 1 : return RT_ERROR_NONE;
194 : }
195 :
196 2 : rtError_t NpuDriver::MbufGetBuffSize(const rtMbufPtr_t memBuf, uint64_t* const totalSize)
197 : {
198 2 : RT_LOG(RT_LOG_INFO, "get size address from mbuf, device_id=0.");
199 2 : COND_RETURN_WARN(
200 : &halMbufGetBuffSize == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufGetBuffSize does not exist.");
201 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufGetBuffSize(RtPtrToPtr<Mbuf*>(memBuf), totalSize));
202 2 : if (drvRet != DRV_ERROR_NONE) {
203 1 : DRV_ERROR_PROCESS(
204 : drvRet, "Call driver api halMbufGetBuffSize failed, drvRetCode=%d, drvDevId=0.",
205 : static_cast<int32_t>(drvRet));
206 1 : return RT_GET_DRV_ERRCODE(drvRet);
207 : }
208 1 : return RT_ERROR_NONE;
209 : }
210 :
211 2 : rtError_t NpuDriver::MbufGetBuffAddr(const rtMbufPtr_t memBuf, void** const buf)
212 : {
213 2 : RT_LOG(RT_LOG_INFO, "get buff address from mbuf, device_id=0.");
214 2 : COND_RETURN_WARN(
215 : &halMbufGetBuffAddr == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufGetBuffAddr does not exist.");
216 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufGetBuffAddr(RtPtrToPtr<Mbuf*>(memBuf), buf));
217 2 : if (drvRet != DRV_ERROR_NONE) {
218 1 : DRV_ERROR_PROCESS(
219 : drvRet, "Call driver api halMbufGetBuffAddr failed, drvRetCode=%d, drvDevId=0.",
220 : static_cast<int32_t>(drvRet));
221 1 : return RT_GET_DRV_ERRCODE(drvRet);
222 : }
223 1 : return RT_ERROR_NONE;
224 : }
225 :
226 1 : rtError_t NpuDriver::MbufGetPrivInfo(const rtMbufPtr_t memBuf, void** const priv, uint64_t* const size)
227 : {
228 1 : RT_LOG(RT_LOG_INFO, "get private info from mbuf.");
229 1 : COND_RETURN_WARN(
230 : &halMbufGetPrivInfo == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufGetPrivInfo does not exist.");
231 1 : uint32_t privSize = 0U;
232 1 : const drvError_t drvRet = static_cast<drvError_t>(halMbufGetPrivInfo(RtPtrToPtr<Mbuf*>(memBuf), priv, &privSize));
233 1 : if (drvRet != DRV_ERROR_NONE) {
234 0 : DRV_ERROR_PROCESS(
235 : drvRet, "Call driver api halMbufGetPrivInfo failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
236 0 : return RT_GET_DRV_ERRCODE(drvRet);
237 : }
238 1 : *size = privSize;
239 1 : return RT_ERROR_NONE;
240 : }
241 :
242 1 : rtError_t NpuDriver::MbufCopyBufRef(const rtMbufPtr_t mbufPtr, rtMbufPtr_t* const newMbufPtr)
243 : {
244 1 : RT_LOG(RT_LOG_INFO, "copy buf ref.");
245 1 : COND_RETURN_WARN(
246 : &halMbufCopyRef == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufCopyRef does not exist.");
247 : const drvError_t drvRet =
248 1 : static_cast<drvError_t>(halMbufCopyRef(RtPtrToPtr<Mbuf*>(mbufPtr), RtPtrToPtr<Mbuf**>(newMbufPtr)));
249 1 : if (drvRet != DRV_ERROR_NONE) {
250 0 : DRV_ERROR_PROCESS(
251 : drvRet, "Call driver api halMbufCopyRef failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
252 0 : return RT_GET_DRV_ERRCODE(drvRet);
253 : }
254 1 : return RT_ERROR_NONE;
255 : }
256 :
257 2 : rtError_t NpuDriver::MbufChainAppend(const rtMbufPtr_t memBufChainHead, rtMbufPtr_t memBuf)
258 : {
259 2 : RT_LOG(RT_LOG_INFO, "append mbuf chain.");
260 2 : COND_RETURN_WARN(
261 : &halMbufChainAppend == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufChainAppend does not exist.");
262 : const drvError_t drvRet =
263 2 : static_cast<drvError_t>(halMbufChainAppend(RtPtrToPtr<Mbuf*>(memBufChainHead), RtPtrToPtr<Mbuf*>(memBuf)));
264 2 : if (drvRet != DRV_ERROR_NONE) {
265 1 : DRV_ERROR_PROCESS(
266 : drvRet, "Call driver api halMbufChainAppend failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
267 1 : return RT_GET_DRV_ERRCODE(drvRet);
268 : }
269 1 : return RT_ERROR_NONE;
270 : }
271 :
272 2 : rtError_t NpuDriver::MbufChainGetMbuf(
273 : rtMbufPtr_t const memBufChainHead, const uint32_t index, rtMbufPtr_t* const memBuf)
274 : {
275 2 : RT_LOG(RT_LOG_INFO, "get mbuf chain mbuf index is %u.", index);
276 2 : COND_RETURN_WARN(
277 : &halMbufChainGetMbuf == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT, "[drv api] halMbufChainGetMbuf does not exist.");
278 : const drvError_t drvRet = static_cast<drvError_t>(
279 2 : halMbufChainGetMbuf(RtPtrToPtr<Mbuf*>(memBufChainHead), index, RtPtrToPtr<Mbuf**>(memBuf)));
280 2 : if (drvRet != DRV_ERROR_NONE) {
281 1 : DRV_ERROR_PROCESS(
282 : drvRet, "Call driver api halMbufChainGetMbuf failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
283 1 : return RT_GET_DRV_ERRCODE(drvRet);
284 : }
285 1 : return RT_ERROR_NONE;
286 : }
287 :
288 2 : rtError_t NpuDriver::MbufChainGetMbufNum(const rtMbufPtr_t memBufChainHead, uint32_t* num)
289 : {
290 2 : RT_LOG(RT_LOG_INFO, "get mbuf chain num.");
291 2 : COND_RETURN_WARN(
292 : &halMbufChainGetMbufNum == nullptr, RT_ERROR_FEATURE_NOT_SUPPORT,
293 : "[drv api] halMbufChainGetMbufNum does not exist.");
294 2 : const drvError_t drvRet = static_cast<drvError_t>(halMbufChainGetMbufNum(RtPtrToPtr<Mbuf*>(memBufChainHead), num));
295 2 : if (drvRet != DRV_ERROR_NONE) {
296 1 : DRV_ERROR_PROCESS(
297 : drvRet, "Call driver api halMbufChainGetMbufNum failed, drvRetCode=%d.", static_cast<int32_t>(drvRet));
298 1 : return RT_GET_DRV_ERRCODE(drvRet);
299 : }
300 1 : return RT_ERROR_NONE;
301 : }
302 :
303 : } // namespace runtime
304 : } // namespace cce
|