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