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 "ts_aicpu_msg_info_adapter.h"
11 : #include "aicpusd_event_manager.h"
12 : #include "aicpusd_event_process.h"
13 : #include "aicpusd_msg_send.h"
14 : namespace AicpuSchedule {
15 27 : TsAicpuMsgInfoAdapter::TsAicpuMsgInfoAdapter(const TsAicpuMsgInfo& msgInfo)
16 27 : : TsMsgAdapter(msgInfo.pid, msgInfo.cmd_type, msgInfo.vf_id, msgInfo.tid, msgInfo.ts_id),
17 27 : aicpuMsgInfo_(msgInfo),
18 27 : invalidMsgInfo_(false)
19 : {
20 27 : aicpusd_info("Adapter initialized: message_format=msg_info.");
21 27 : }
22 :
23 2 : TsAicpuMsgInfoAdapter::TsAicpuMsgInfoAdapter() : aicpuMsgInfo_({}), invalidMsgInfo_(true)
24 : {
25 2 : aicpusd_info("Adapter initialized: message_format=msg_info valid=false.");
26 2 : }
27 :
28 4 : bool TsAicpuMsgInfoAdapter::IsAdapterInvalidParameter() const { return invalidMsgInfo_; }
29 :
30 : // dump
31 3 : void TsAicpuMsgInfoAdapter::GetAicpuDataDumpInfo(AicpuDataDumpInfo& info)
32 : {
33 3 : switch (static_cast<uint32_t>(cmdType_)) {
34 1 : case TS_AICPU_DEBUG_DATADUMP_REPORT: {
35 1 : TsToAicpuDebugDataDumpMsg tmpInfo = aicpuMsgInfo_.u.ts_to_aicpu_debug_datadump;
36 1 : aicpusd_info(
37 : "Dump request(debug): dump_task=%u debug_dump_task=%u dump_stream=%u "
38 : "is_model=%u dump_type=%u reserved=%u.",
39 : tmpInfo.dump_task_id, tmpInfo.debug_dump_task_id, tmpInfo.dump_stream_id, tmpInfo.is_model,
40 : tmpInfo.dump_type, tmpInfo.rsv);
41 1 : info.is_debug = true;
42 1 : info.dump_task_id = tmpInfo.dump_task_id;
43 1 : info.dump_stream_id = INVALID_VALUE16;
44 1 : info.debug_dump_task_id = tmpInfo.debug_dump_task_id;
45 1 : info.debug_dump_stream_id = INVALID_VALUE16;
46 1 : info.is_model = (tmpInfo.is_model == 1);
47 1 : break;
48 : }
49 1 : case TS_AICPU_NORMAL_DATADUMP_REPORT: {
50 1 : TsToAicpuNormalDataDumpMsg tmpInfo = aicpuMsgInfo_.u.ts_to_aicpu_normal_datadump;
51 1 : aicpusd_info(
52 : "Dump request(normal): dump_task=%u dump_stream=%u is_model=%u dump_type=%u reserved=%u.",
53 : tmpInfo.dump_task_id, tmpInfo.dump_stream_id, tmpInfo.is_model, tmpInfo.dump_type, tmpInfo.rsv);
54 1 : info.is_debug = false;
55 1 : info.dump_task_id = tmpInfo.dump_task_id;
56 1 : info.dump_stream_id = INVALID_VALUE16;
57 1 : info.debug_dump_task_id = INVALID_VALUE32;
58 1 : info.debug_dump_stream_id = INVALID_VALUE16;
59 1 : info.is_model = (tmpInfo.is_model == 1);
60 1 : break;
61 : }
62 1 : default:
63 1 : aicpusd_err("Unsupported message type: message_type=%u", cmdType_);
64 1 : break;
65 : }
66 3 : info.file_name_stream_id = info.dump_stream_id;
67 3 : info.file_name_task_id = info.dump_task_id;
68 3 : return;
69 : }
70 :
71 1 : bool TsAicpuMsgInfoAdapter::IsOpMappingDumpTaskInfoVaild(const AicpuOpMappingDumpTaskInfo& info) const
72 : {
73 : (void)info;
74 1 : return true;
75 : }
76 :
77 1 : void TsAicpuMsgInfoAdapter::GetAicpuDumpTaskInfo(
78 : AicpuOpMappingDumpTaskInfo& opmappingInfo, AicpuDumpTaskInfo& dumpTaskInfo)
79 : {
80 1 : aicpusd_info(
81 : "Dump task mapping: proto_task=%u proto_stream=%u mapped_task=%u mapped_stream=%u",
82 : opmappingInfo.proto_info_task_id, opmappingInfo.proto_info_stream_id, opmappingInfo.task_id,
83 : opmappingInfo.stream_id);
84 1 : dumpTaskInfo.task_id = opmappingInfo.proto_info_task_id;
85 1 : dumpTaskInfo.stream_id = INVALID_VALUE16;
86 1 : dumpTaskInfo.context_id = INVALID_VALUE16;
87 1 : dumpTaskInfo.thread_id = INVALID_VALUE16;
88 1 : }
89 :
90 1 : void TsAicpuMsgInfoAdapter::GetAicpuDataDumpInfoLoad(AicpuDataDumpInfoLoad& info)
91 : {
92 1 : TsToAicpuDataDumpInfoloadMsg tmpinfo = aicpuMsgInfo_.u.ts_to_aicpu_datadump_info_load;
93 1 : aicpusd_info(
94 : "Dump load request: address=%p length=%u task=%u stream=%u.", tmpinfo.dumpinfoPtr, tmpinfo.length,
95 : tmpinfo.task_id, tmpinfo.stream_id);
96 1 : info.dumpinfoPtr = tmpinfo.dumpinfoPtr;
97 1 : info.length = tmpinfo.length;
98 1 : info.task_id = tmpinfo.task_id;
99 1 : info.stream_id = tmpinfo.stream_id;
100 1 : }
101 3 : int32_t TsAicpuMsgInfoAdapter::AicpuDumpResponseToTs(const int32_t result)
102 : {
103 3 : TsAicpuMsgInfo msgInfo{};
104 3 : msgInfo.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
105 3 : msgInfo.vf_id = static_cast<uint8_t>(AicpuDrvManager::GetInstance().GetVfId());
106 3 : msgInfo.tid = tid_;
107 3 : msgInfo.ts_id = tsId_;
108 3 : msgInfo.cmd_type = cmdType_;
109 3 : msgInfo.u.aicpu_resp.result_code = static_cast<uint16_t>(result);
110 3 : switch (static_cast<uint32_t>(cmdType_)) {
111 1 : case TS_AICPU_NORMAL_DATADUMP_REPORT: {
112 1 : TsToAicpuNormalDataDumpMsg tmpInfo = aicpuMsgInfo_.u.ts_to_aicpu_normal_datadump;
113 1 : msgInfo.u.aicpu_resp.task_id = tmpInfo.dump_task_id;
114 1 : msgInfo.u.aicpu_resp.stream_id = tmpInfo.dump_stream_id;
115 1 : msgInfo.u.aicpu_resp.reserved = tmpInfo.dump_type;
116 1 : break;
117 : }
118 1 : case TS_AICPU_DEBUG_DATADUMP_REPORT: {
119 1 : TsToAicpuDebugDataDumpMsg tmpInfo = aicpuMsgInfo_.u.ts_to_aicpu_debug_datadump;
120 1 : msgInfo.u.aicpu_resp.task_id = tmpInfo.debug_dump_task_id;
121 1 : msgInfo.u.aicpu_resp.stream_id = tmpInfo.dump_stream_id;
122 1 : msgInfo.u.aicpu_resp.reserved = tmpInfo.dump_type;
123 1 : break;
124 : }
125 1 : default: {
126 1 : aicpusd_err("Unsupported message type: message_type=%u", cmdType_);
127 1 : return AICPU_SCHEDULE_ERROR_INNER_ERROR;
128 : }
129 : }
130 2 : aicpusd_info(
131 : "Dump response: message_type=%u result=%u task=%u stream=%u", cmdType_, msgInfo.u.aicpu_resp.result_code,
132 : msgInfo.u.aicpu_resp.task_id, msgInfo.u.aicpu_resp.stream_id);
133 2 : return ResponseToTs(
134 4 : msgInfo, 0U, AicpuDrvManager::GetInstance().GetDeviceId(), static_cast<uint32_t>(msgInfo.ts_id));
135 : }
136 1 : int32_t TsAicpuMsgInfoAdapter::AicpuDataDumpLoadResponseToTs(const int32_t result)
137 : {
138 1 : TsAicpuMsgInfo msgInfo{};
139 1 : msgInfo.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
140 1 : msgInfo.vf_id = static_cast<uint8_t>(AicpuDrvManager::GetInstance().GetVfId());
141 1 : msgInfo.tid = tid_;
142 1 : msgInfo.ts_id = tsId_;
143 1 : msgInfo.cmd_type = cmdType_;
144 1 : msgInfo.u.aicpu_resp.result_code = static_cast<uint16_t>(result);
145 1 : msgInfo.u.aicpu_resp.stream_id = aicpuMsgInfo_.u.ts_to_aicpu_datadump_info_load.stream_id;
146 1 : msgInfo.u.aicpu_resp.task_id = aicpuMsgInfo_.u.ts_to_aicpu_datadump_info_load.task_id;
147 1 : aicpusd_info(
148 : "Dump load response: message_type=%u result=%u stream=%u task=%u.", msgInfo.cmd_type, result,
149 : msgInfo.u.aicpu_resp.stream_id, msgInfo.u.aicpu_resp.task_id);
150 2 : return ResponseToTs(msgInfo, 0U, AicpuDrvManager::GetInstance().GetDeviceId(), msgInfo.ts_id);
151 : }
152 :
153 : // model operator
154 1 : void TsAicpuMsgInfoAdapter::GetAicpuModelOperateInfo(AicpuModelOperateInfo& info)
155 : {
156 1 : TsAicpuModelOperateMsg tmpInfo = aicpuMsgInfo_.u.aicpu_model_operate;
157 1 : aicpusd_info(
158 : "Model operation request: argument=%p operation=%u model=%u.", tmpInfo.arg_ptr, tmpInfo.cmd_type,
159 : tmpInfo.model_id);
160 1 : info.arg_ptr = tmpInfo.arg_ptr;
161 1 : info.cmd_type = tmpInfo.cmd_type;
162 1 : info.model_id = tmpInfo.model_id;
163 1 : info.stream_id = tmpInfo.stream_id;
164 1 : info.task_id = INVALID_VALUE16;
165 1 : info.reserved[0] = tmpInfo.reserved[0];
166 2 : return;
167 : }
168 :
169 1 : int32_t TsAicpuMsgInfoAdapter::AicpuModelOperateResponseToTs(const int32_t result, const uint32_t subEvent)
170 : {
171 1 : TsAicpuMsgInfo aicpuMsgInfo = {};
172 1 : aicpuMsgInfo.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
173 1 : aicpuMsgInfo.cmd_type = cmdType_;
174 1 : aicpuMsgInfo.vf_id = vfId_;
175 1 : aicpuMsgInfo.tid = tid_;
176 1 : aicpuMsgInfo.ts_id = tsId_;
177 1 : aicpuMsgInfo.u.aicpu_resp.result_code = static_cast<uint16_t>(result);
178 1 : aicpuMsgInfo.u.aicpu_resp.stream_id = INVALID_VALUE16;
179 1 : aicpuMsgInfo.u.aicpu_resp.task_id = INVALID_VALUE32;
180 1 : aicpuMsgInfo.u.aicpu_resp.reserved = 0U;
181 1 : aicpusd_info(
182 : "Model operation response: message_type=%u result=%u stream=%u task=%u", cmdType_, result,
183 : aicpuMsgInfo.u.aicpu_resp.stream_id, aicpuMsgInfo.u.aicpu_resp.task_id);
184 1 : hwts_response_t hwtsResp = {};
185 1 : hwtsResp.result = static_cast<uint32_t>(result);
186 1 : hwtsResp.status =
187 1 : (result == AICPU_SCHEDULE_OK) ? static_cast<uint32_t>(TASK_SUCC) : static_cast<uint32_t>(TASK_FAIL);
188 1 : hwtsResp.msg = PtrToPtr<TsAicpuMsgInfo, char_t>(&aicpuMsgInfo);
189 1 : hwtsResp.len = static_cast<int32_t>(sizeof(TsAicpuMsgInfo));
190 2 : return ResponseToTs(hwtsResp, AicpuDrvManager::GetInstance().GetDeviceId(), EVENT_TS_CTRL_MSG, subEvent);
191 : }
192 :
193 1 : void TsAicpuMsgInfoAdapter::AicpuActiveStreamSetMsg(ActiveStreamInfo& info)
194 : {
195 1 : TsAicpuMsgInfo msgInfo = {};
196 1 : msgInfo.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
197 1 : msgInfo.cmd_type = TS_AICPU_ACTIVE_STREAM;
198 1 : msgInfo.vf_id = static_cast<uint8_t>(AicpuDrvManager::GetInstance().GetVfId());
199 1 : msgInfo.tid = 0U; // no need tid
200 1 : msgInfo.ts_id = info.ts_id;
201 1 : msgInfo.u.aicpu_active_stream.aicpu_stamp = info.aicpu_stamp;
202 1 : msgInfo.u.aicpu_active_stream.stream_id = info.stream_id;
203 1 : aicpusd_info(
204 : "Active stream update: message_type=%u stream=%u stamp=%u.", msgInfo.cmd_type,
205 : msgInfo.u.aicpu_active_stream.stream_id, msgInfo.u.aicpu_active_stream.aicpu_stamp);
206 1 : AicpuMsgSend::SetTsDevSendMsgAsync(info.device_id, static_cast<uint32_t>(info.ts_id), msgInfo, info.handle_id);
207 1 : }
208 :
209 : // version set
210 1 : void TsAicpuMsgInfoAdapter::GetAicpuMsgVersionInfo(AicpuMsgVersionInfo& info)
211 : {
212 : (void)info;
213 1 : aicpusd_warn("Version update is not supported in current message format.");
214 1 : return;
215 : }
216 :
217 1 : int32_t TsAicpuMsgInfoAdapter::AicpuMsgVersionResponseToTs(const int32_t result)
218 : {
219 : (void)result;
220 1 : return AICPU_SCHEDULE_OK;
221 : }
222 :
223 : // task report
224 1 : void TsAicpuMsgInfoAdapter::GetAicpuTaskReportInfo(AicpuTaskReportInfo& info)
225 : {
226 1 : TsToAicpuTaskReportMsg tmpInfo = aicpuMsgInfo_.u.ts_to_aicpu_task_report;
227 1 : aicpusd_info(
228 : "Task report request: task=%u model=%u result=%u stream=%u", tmpInfo.task_id, tmpInfo.model_id,
229 : tmpInfo.result_code, tmpInfo.stream_id);
230 1 : info.task_id = tmpInfo.task_id;
231 1 : info.model_id = tmpInfo.model_id;
232 1 : info.result_code = tmpInfo.result_code;
233 1 : info.stream_id = tmpInfo.stream_id;
234 1 : }
235 :
236 1 : int32_t TsAicpuMsgInfoAdapter::ErrorMsgResponseToTs(ErrMsgRspInfo& rspInfo)
237 : {
238 1 : TsAicpuMsgInfo msgInfo{};
239 1 : msgInfo.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
240 1 : msgInfo.cmd_type = cmdType_;
241 1 : msgInfo.vf_id = static_cast<uint8_t>(AicpuDrvManager::GetInstance().GetVfId());
242 1 : msgInfo.tid = 0U; // no need tid
243 1 : msgInfo.ts_id = static_cast<uint8_t>(rspInfo.ts_id);
244 1 : msgInfo.u.aicpu_resp.result_code = rspInfo.err_code;
245 1 : msgInfo.u.aicpu_resp.stream_id = static_cast<uint16_t>(rspInfo.stream_id);
246 1 : msgInfo.u.aicpu_resp.task_id = rspInfo.task_id;
247 1 : aicpusd_info(
248 : "Error response: message_type=%u result=%u response_stream=%u response_task=%u", msgInfo.cmd_type,
249 : rspInfo.err_code, rspInfo.stream_id, rspInfo.task_id);
250 1 : return ResponseToTs(
251 3 : msgInfo, rspInfo.model_id, AicpuDrvManager::GetInstance().GetDeviceId(), static_cast<uint32_t>(msgInfo.ts_id));
252 : }
253 :
254 : // pid notice
255 1 : int32_t TsAicpuMsgInfoAdapter::AicpuNoticeTsPidResponse(const uint32_t deviceId) const
256 : {
257 1 : TsAicpuSqe aicpuSqe = {};
258 1 : aicpuSqe.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid()); // host pid
259 1 : aicpuSqe.cmd_type = AICPU_NOTICE_TS_PID;
260 1 : aicpuSqe.vf_id = static_cast<uint8_t>(AicpuDrvManager::GetInstance().GetVfId());
261 1 : aicpusd_info("Notice TS pid response: device_id=%u pid=%u vf=%u.", deviceId, aicpuSqe.pid, aicpuSqe.vf_id);
262 2 : return ResponseToTs(aicpuSqe, 0U, deviceId, 0U);
263 : }
264 :
265 : // time out
266 1 : void TsAicpuMsgInfoAdapter::GetAicpuTimeOutConfigInfo(AicpuTimeOutConfigInfo& info)
267 : {
268 1 : aicpusd_info("Get timeout config.");
269 1 : info.i = aicpuMsgInfo_.u.ts_to_aicpu_timeout_cfg;
270 1 : return;
271 : }
272 :
273 1 : int32_t TsAicpuMsgInfoAdapter::AicpuTimeOutConfigResponseToTs(const int32_t result)
274 : {
275 : TsAicpuMsgInfo msgInfo;
276 1 : msgInfo.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
277 1 : msgInfo.cmd_type = cmdType_;
278 1 : msgInfo.vf_id = static_cast<uint8_t>(AicpuDrvManager::GetInstance().GetVfId());
279 1 : msgInfo.tid = tid_;
280 1 : msgInfo.ts_id = tsId_;
281 1 : msgInfo.u.aicpu_resp.result_code = static_cast<uint16_t>(result);
282 1 : msgInfo.u.aicpu_resp.stream_id = INVALID_VALUE16;
283 1 : msgInfo.u.aicpu_resp.task_id = INVALID_VALUE32;
284 1 : aicpusd_info(
285 : "Timeout config response: message_type=%u result=%u stream=%u task=%u.", msgInfo.cmd_type,
286 : msgInfo.u.aicpu_resp.result_code, msgInfo.u.aicpu_resp.stream_id, msgInfo.u.aicpu_resp.task_id);
287 2 : return ResponseToTs(msgInfo, 0U, AicpuDrvManager::GetInstance().GetDeviceId(), msgInfo.ts_id);
288 : }
289 :
290 : // info load
291 1 : void TsAicpuMsgInfoAdapter::GetAicpuInfoLoad(AicpuInfoLoad& info)
292 : {
293 1 : TsToAicpuInfoLoadMsg tmpinfo = aicpuMsgInfo_.u.ts_to_aicpu_info_load;
294 1 : aicpusd_info(
295 : "Info load request: address=%p length=%u stream=%u task=%u", tmpinfo.aicpu_info_ptr, tmpinfo.length,
296 : tmpinfo.stream_id, tmpinfo.task_id);
297 1 : info.aicpuInfoPtr = tmpinfo.aicpu_info_ptr;
298 1 : info.length = tmpinfo.length;
299 1 : info.stream_id = tmpinfo.stream_id;
300 1 : info.task_id = tmpinfo.task_id;
301 1 : }
302 :
303 1 : int32_t TsAicpuMsgInfoAdapter::AicpuInfoLoadResponseToTs(const int32_t result)
304 : {
305 1 : TsAicpuMsgInfo msgInfo{};
306 1 : TsToAicpuInfoLoadMsg tmpInfo = aicpuMsgInfo_.u.ts_to_aicpu_info_load;
307 1 : msgInfo.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
308 1 : msgInfo.tid = tid_;
309 1 : msgInfo.ts_id = tsId_;
310 1 : msgInfo.vf_id = static_cast<uint8_t>(AicpuDrvManager::GetInstance().GetVfId());
311 1 : msgInfo.cmd_type = cmdType_;
312 1 : msgInfo.u.aicpu_resp.stream_id = tmpInfo.stream_id;
313 1 : msgInfo.u.aicpu_resp.task_id = tmpInfo.task_id;
314 1 : msgInfo.u.aicpu_resp.result_code = static_cast<uint16_t>(result);
315 1 : msgInfo.u.aicpu_resp.reserved = 0U;
316 1 : aicpusd_info(
317 : "Info load response: message_type=%u result=%u stream=%u task=%u.", msgInfo.cmd_type, result, tmpInfo.stream_id,
318 : tmpInfo.task_id);
319 1 : return ResponseToTs(
320 3 : msgInfo, 0U, AicpuDrvManager::GetInstance().GetDeviceId(), static_cast<uint32_t>(msgInfo.ts_id));
321 : }
322 :
323 : // err report
324 1 : void TsAicpuMsgInfoAdapter::GetAicErrReportInfo(AicErrReportInfo& info)
325 : {
326 1 : aicpusd_info("Get AIC error report.");
327 1 : info.u.aicErrorMsg = aicpuMsgInfo_.u.aic_err_msg;
328 1 : return;
329 : }
330 :
331 : // record
332 2 : int32_t TsAicpuMsgInfoAdapter::AicpuRecordResponseToTs(AicpuRecordInfo& info)
333 : {
334 : TsAicpuMsgInfo msgInfo;
335 2 : msgInfo.pid = static_cast<uint32_t>(AicpuDrvManager::GetInstance().GetHostPid());
336 2 : msgInfo.cmd_type = TS_AICPU_RECORD;
337 2 : msgInfo.vf_id = static_cast<uint8_t>(AicpuDrvManager::GetInstance().GetVfId());
338 2 : msgInfo.tid = 0U; // notify is no need tid
339 2 : msgInfo.ts_id = info.ts_id;
340 2 : msgInfo.u.aicpu_record.record_id = info.record_id;
341 2 : msgInfo.u.aicpu_record.record_type = info.record_type;
342 2 : msgInfo.u.aicpu_record.ret_code = info.ret_code;
343 2 : msgInfo.u.aicpu_record.fault_task_id = info.fault_task_id;
344 2 : uint32_t handleId = info.ret_code == 0 ? MSG_EVENT_SUB_EVENTID_RECORD : 0U;
345 2 : aicpusd_info(
346 : "Record response: pid=%u message_type=%u vf=%u tid=%u ts=%u record_type=%u "
347 : "record_id=%u fault_task=%u result=%u handle=%u",
348 : msgInfo.pid, msgInfo.cmd_type, msgInfo.vf_id, msgInfo.tid, msgInfo.ts_id, msgInfo.u.aicpu_record.record_type,
349 : msgInfo.u.aicpu_record.record_id, msgInfo.u.aicpu_record.fault_task_id, msgInfo.u.aicpu_record.ret_code,
350 : handleId);
351 4 : return ResponseToTs(msgInfo, handleId, info.dev_id, static_cast<uint32_t>(msgInfo.ts_id));
352 : }
353 : } // namespace AicpuSchedule
|