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