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 <thread>
12 : #include <adapter_hal.h>
13 : #include "dlhal_function.h"
14 : #include "log.h"
15 : #include "sal_pub.h"
16 : #include "aicpu_schedule/aicpu_mc2_maintenance_thread.h"
17 :
18 : using namespace hccl;
19 :
20 : int (*g_grpIdCallback)(int tag, int* grpId, int* devId) = nullptr;
21 : #ifdef __cplusplus
22 : extern "C" {
23 : #endif
24 :
25 : constexpr u32 MEMORY_PAGE_SIZE = 4096;
26 : constexpr u32 PRE_FETCH_THREADS_NUM = 24;
27 : constexpr u32 PRE_FETCH_MEMORY_THRESHOLD = 268435456; // 考虑到多线程预访问也有开销,只有在内存大于256MB时才启动预访问
28 :
29 : drvError_t __attribute__((weak))
30 : halResourceIdRestore(struct drvResIdKey* info); // custom进程notify资源同步,在调用halResourceIdCheck前调用
31 :
32 0 : HcclResult HcclSetGrpIdCallback(int (*grpIdCallback)(int tag, int* grpId, int* devId))
33 : {
34 0 : if (grpIdCallback == nullptr) {
35 0 : HCCL_ERROR("para grpIdCallback is nullptr");
36 0 : return HCCL_E_PARA;
37 : }
38 :
39 0 : g_grpIdCallback = grpIdCallback;
40 0 : HCCL_DEBUG("New grpIdCallback was set, grpIdCallback[%p]", grpIdCallback);
41 0 : return HCCL_SUCCESS;
42 : }
43 :
44 : #ifdef __cplusplus
45 : }
46 : #endif
47 :
48 0 : s32 hrtGetgrpId(int& groupId, int& devId)
49 : {
50 0 : if (g_grpIdCallback == nullptr) {
51 0 : groupId = HCCL_ESCHED_GROUP_ID;
52 0 : devId = HCCL_RESERVED_DEV_ID;
53 :
54 0 : HCCL_DEBUG("g_grpIdCallback is nullptr, event.grp_id[%d] = HCCL_ESCHED_GROUP_ID", groupId);
55 : } else {
56 0 : HCCL_DEBUG("g_grpIdCallback is not nullptr");
57 0 : s32 ret = g_grpIdCallback(0, &groupId, &devId);
58 0 : if (ret != 0) {
59 0 : HCCL_ERROR("g_grpIdCallback failed, ret[%d]", ret);
60 0 : return ret;
61 : }
62 :
63 0 : HCCL_DEBUG("g_grpIdCallback is not nullptr, g_grpIdCallback(0) event.grp_id[%d], devId[%d]", groupId, devId);
64 : }
65 :
66 0 : return 0;
67 : }
68 :
69 0 : HcclResult hrtHalSubmitEvent(u32 devId, u32 eventId, u32 groupId)
70 : {
71 0 : struct event_summary event = {};
72 0 : event.pid = SalGetPid();
73 0 : event.grp_id = groupId;
74 0 : event.event_id = static_cast<EVENT_ID>(eventId);
75 0 : event.subevent_id = 0;
76 0 : event.msg_len = 0;
77 0 : event.msg = nullptr;
78 0 : event.dst_engine = ACPU_DEVICE;
79 0 : event.policy = ONLY;
80 0 : for (u32 i = 0; i < EVENT_SUMMARY_RSV; i++) {
81 0 : event.rsv[i] = 0;
82 : }
83 :
84 0 : HCCL_DEBUG("SubmitEvent: event id:%u, pid:%d, grp id:%u, dev id:%u", eventId, event.pid, event.grp_id, devId);
85 :
86 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalEschedSubmitEvent(devId, &event);
87 0 : CHK_PRT_RET(
88 : ret != DRV_ERROR_NONE,
89 : HCCL_ERROR(
90 : "[Submit][Event]errNo[0x%016llx] halEschedSubmitEvent fail,"
91 : "return[%d], para: deviceLogicId[%u] group[%u] eventId[%u].",
92 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devId, event.grp_id, eventId),
93 : HCCL_E_DRV);
94 0 : return HCCL_SUCCESS;
95 : }
96 :
97 0 : HcclResult hrtHalEschedAttachDevice(unsigned int devId)
98 : {
99 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalEschedAttachDevice(devId);
100 0 : CHK_PRT_RET(
101 : ret != DRV_ERROR_NONE,
102 : HCCL_ERROR(
103 : "[Attach][Device]errNo[0x%016llx] hrtHalEschedAttachDevice fail,"
104 : "return[%d], para: deviceLogicId[%u].",
105 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devId),
106 : HCCL_E_DRV);
107 0 : return HCCL_SUCCESS;
108 : }
109 :
110 0 : HcclResult hrtHalEschedDettachDevice(unsigned int devId)
111 : {
112 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalEschedDettachDevice(devId);
113 0 : CHK_PRT_RET(
114 : ret != DRV_ERROR_NONE,
115 : HCCL_ERROR(
116 : "[Detach][Device]errNo[0x%016llx] hrtHalEschedDettachDevice fail,"
117 : "return[%d], para: deviceLogicId[%u].",
118 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devId),
119 : HCCL_E_DRV);
120 0 : return HCCL_SUCCESS;
121 : }
122 :
123 0 : HcclResult hrtHalGetAPIVersion(int& apiVersion)
124 : {
125 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalGetAPIVersion(&apiVersion);
126 0 : CHK_PRT_RET(
127 : ret != DRV_ERROR_NONE,
128 : HCCL_ERROR(
129 : "[Get][APIVersion]errNo[0x%016llx] dlHalGetAPIVersion fail,"
130 : "return[%d], para: apiVersion[%d].",
131 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, apiVersion),
132 : HCCL_E_DRV);
133 0 : return HCCL_SUCCESS;
134 : }
135 :
136 0 : HcclResult hrtHalEschedCreateGrp(unsigned int devId, unsigned int grpId, GROUP_TYPE type)
137 : {
138 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalEschedCreateGrp(devId, grpId, type);
139 0 : CHK_PRT_RET(
140 : ret == DRV_ERROR_GROUP_EXIST,
141 : HCCL_INFO("hal event group is exist, devId[%u] grpId[%u] group type[%u]", devId, grpId, type), HCCL_SUCCESS);
142 0 : CHK_PRT_RET(
143 : ret != DRV_ERROR_NONE,
144 : HCCL_ERROR(
145 : "[hrtHalEschedCreateGrp]errNo[0x%016llx] hrtHalEschedCreateGrp fail,"
146 : "return[%d], para: devId[%u].",
147 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devId),
148 : HCCL_E_DRV);
149 0 : return HCCL_SUCCESS;
150 : }
151 :
152 0 : HcclResult hrtHalEschedCreateGrpEx(
153 : [[maybe_unused]] unsigned int devId, [[maybe_unused]] unsigned int* grpId, [[maybe_unused]] unsigned int threadNum,
154 : [[maybe_unused]] GROUP_TYPE type)
155 : {
156 0 : return HCCL_SUCCESS;
157 : }
158 :
159 0 : HcclResult hrtHalEschedSubscribeEvent(
160 : [[maybe_unused]] unsigned int devId, [[maybe_unused]] unsigned int grpId, [[maybe_unused]] unsigned int threadId,
161 : [[maybe_unused]] unsigned long long eventBitmap)
162 : {
163 0 : return HCCL_SUCCESS;
164 : }
165 :
166 0 : HcclResult hrtHalEschedWaitEvent(
167 : [[maybe_unused]] unsigned int devId, [[maybe_unused]] unsigned int grpId, [[maybe_unused]] unsigned int threadId,
168 : [[maybe_unused]] int timeout, [[maybe_unused]] struct event_info* event)
169 : {
170 0 : return HCCL_SUCCESS;
171 : }
172 :
173 0 : HcclResult hrtHalEschedRegisterAckFunc(
174 : unsigned int eventId, void (*ackFunc)(unsigned int devId, unsigned int subeventId, u8* msg, unsigned int msgLen))
175 : {
176 0 : CHK_PTR_NULL(ackFunc);
177 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalEschedRegisterAckFunc(HCCL_ESCHED_GROUP_ID, eventId, ackFunc);
178 0 : CHK_PRT_RET(
179 : ret != DRV_ERROR_NONE,
180 : HCCL_ERROR(
181 : "errNo[0x%016llx] hrtHalEschedRegisterAckFunc fail,"
182 : "return[%d], para: eventId[%u].",
183 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, eventId),
184 : HCCL_E_DRV);
185 0 : return HCCL_SUCCESS;
186 : }
187 :
188 0 : HcclResult hrtHalEschedRegisterAckFuncWithGrpid(
189 : unsigned int grpid, unsigned int eventId,
190 : void (*ackFunc)(unsigned int devId, unsigned int subeventId, u8* msg, unsigned int msgLen))
191 : {
192 0 : CHK_PTR_NULL(ackFunc);
193 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalEschedRegisterAckFunc(grpid, eventId, ackFunc);
194 0 : CHK_PRT_RET(
195 : ret != DRV_ERROR_NONE,
196 : HCCL_ERROR(
197 : "errNo[0x%016llx] hrtHalEschedRegisterAckFunc fail,"
198 : "return[%d], para: eventId[%u].",
199 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, eventId),
200 : HCCL_E_DRV);
201 0 : return HCCL_SUCCESS;
202 : }
203 :
204 0 : HcclResult hrtHalDrvOpenIpcNotify([[maybe_unused]] const char* name, [[maybe_unused]] struct drvIpcNotifyInfo* notify)
205 : {
206 0 : return HCCL_SUCCESS;
207 : }
208 :
209 0 : HcclResult hrtHalDrvCloseIpcNotify([[maybe_unused]] const char* name, [[maybe_unused]] struct drvIpcNotifyInfo* notify)
210 : {
211 0 : return HCCL_SUCCESS;
212 : }
213 :
214 0 : HcclResult hrtHalDrvIpcNotifyRecord([[maybe_unused]] const char* name) { return HCCL_SUCCESS; }
215 :
216 11 : HcclResult HrtHalDrvQueryProcessHostPid(
217 : int pid, unsigned int* chipId, unsigned int* vfid, unsigned int* hostPid, unsigned int* cpType)
218 : {
219 11 : CHK_PTR_NULL(hostPid);
220 : // 和底软确认,chipId、vfid、hostPid、cpType不需要校验空指针,如果传入空指针表示当前不获取该值
221 11 : drvError_t ret = DlHalFunction::GetInstance().dlHalDrvQueryProcessHostPid(pid, chipId, vfid, hostPid, cpType);
222 11 : CHK_PRT_RET(
223 : ret != DRV_ERROR_NONE,
224 : HCCL_ERROR(
225 : "errNo[0x%016llx] HrtHalDrvQueryProcessHostPid fail,"
226 : "return[%d], para: pid[%d].",
227 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, pid),
228 : HCCL_E_DRV);
229 11 : HCCL_INFO("HrtHalDrvQueryProcessHostPid pid[%d] hostPid[%u]", pid, *hostPid);
230 11 : return HCCL_SUCCESS;
231 : }
232 :
233 0 : HcclResult hrtDrvMemCpy(void* dst, uint64_t destMax, const void* src, uint64_t count)
234 : {
235 : // 参数有效性检查
236 0 : CHK_PTR_NULL(dst);
237 0 : CHK_PTR_NULL(src);
238 :
239 0 : uint64_t dstAddr = reinterpret_cast<uintptr_t>(dst);
240 0 : uint64_t srcAddr = reinterpret_cast<uintptr_t>(const_cast<void*>(src));
241 0 : drvError_t ret = DlHalFunction::GetInstance().dlDrvMemCpy(dstAddr, destMax, srcAddr, count);
242 0 : CHK_PRT_RET(
243 : ret != DRV_ERROR_NONE,
244 : HCCL_ERROR(
245 : "errNo[0x%016llx] hrtDrvMemCpy fail,"
246 : "return[%d].",
247 : HCCL_ERROR_CODE(HCCL_E_DRV), ret),
248 : HCCL_E_DRV);
249 0 : return HCCL_SUCCESS;
250 : }
251 :
252 1805 : HcclResult hrtDrvGetDevNum(uint32_t* num_dev)
253 : {
254 : // 参数有效性检查
255 1805 : CHK_PTR_NULL(num_dev);
256 1805 : drvError_t ret = DlHalFunction::GetInstance().dlHalDrvGetDevNum(num_dev);
257 1805 : if (ret == DRV_ERROR_NOT_SUPPORT) {
258 : // 通用服务器不支持该接口,默认num_dev为0,返回成功
259 0 : *num_dev = 0;
260 0 : return HCCL_SUCCESS;
261 : }
262 1805 : CHK_PRT_RET(
263 : ret != DRV_ERROR_NONE,
264 : HCCL_ERROR(
265 : "errNo[0x%016llx] hrtDrvGetDevNum fail,"
266 : "return[%d].",
267 : HCCL_ERROR_CODE(HCCL_E_DRV), ret),
268 : HCCL_E_DRV);
269 1805 : return HCCL_SUCCESS;
270 : }
271 :
272 529 : HcclResult hrtHalGetDeviceInfo(uint32_t devId, int32_t moduleType, int32_t infoType, int64_t* value)
273 : {
274 : // 参数有效性检查
275 529 : CHK_PTR_NULL(value);
276 529 : if (UNLIKELY(!hccl::DlHalFunction::GetInstance().DlHalFunctionIsInit())) {
277 0 : CHK_RET(DlHalFunction::GetInstance().DlHalFunctionInit());
278 : }
279 529 : HCCL_INFO("Entry-hrtHalGetDeviceInfo");
280 529 : drvError_t ret = DlHalFunction::GetInstance().dlHalGetDeviceInfo(devId, moduleType, infoType, value);
281 529 : CHK_PRT_RET(
282 : ret == DRV_ERROR_NOT_SUPPORT,
283 : HCCL_WARNING(
284 : "hrtHalGetDeviceInfo not support"
285 : "return[%d].",
286 : HCCL_ERROR_CODE(DRV_ERROR_NOT_SUPPORT), ret),
287 : HCCL_E_NOT_SUPPORT);
288 529 : CHK_PRT_RET(
289 : ret != DRV_ERROR_NONE,
290 : HCCL_ERROR(
291 : "errNo[0x%016llx] hrtHalGetDeviceInfo fail,"
292 : "return[%d].",
293 : HCCL_ERROR_CODE(HCCL_E_DRV), ret),
294 : HCCL_E_DRV);
295 529 : return HCCL_SUCCESS;
296 : }
297 :
298 0 : void hrtDrvDeviceGetBareTgid(s32& pid)
299 : {
300 0 : pid = DlHalFunction::GetInstance().dlDrvDeviceGetBareTgid();
301 :
302 0 : return;
303 : }
304 :
305 0 : HcclResult hrtHalGrpQuery(GroupQueryCmdType cmd, void* inBuff, unsigned int inLen, void* outBuff, unsigned int* outLen)
306 : {
307 0 : CHK_PTR_NULL(inBuff);
308 0 : CHK_PTR_NULL(outBuff);
309 0 : CHK_PTR_NULL(outLen);
310 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalGrpQuery(cmd, inBuff, inLen, outBuff, outLen);
311 0 : CHK_PRT_RET(ret == DRV_ERROR_NOT_SUPPORT, HCCL_WARNING("dlHalGrpQuery is not support."), HCCL_SUCCESS);
312 0 : CHK_PRT_RET(
313 : ret != DRV_ERROR_NONE,
314 : HCCL_ERROR(
315 : "errNo[0x%016llx] hrtHalGrpQuery fail,"
316 : "return[%d], para: cmd[%d].",
317 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, cmd),
318 : HCCL_E_DRV);
319 0 : return HCCL_SUCCESS;
320 : }
321 :
322 0 : HcclResult hrtEschedQueryInfo(
323 : [[maybe_unused]] unsigned int devId, [[maybe_unused]] ESCHED_QUERY_TYPE type,
324 : [[maybe_unused]] struct esched_input_info* inPut, [[maybe_unused]] struct esched_output_info* outPut)
325 : {
326 0 : return HCCL_SUCCESS;
327 : }
328 :
329 826 : HcclResult hrtDrvGetPlatformInfo(uint32_t* info)
330 : {
331 : // 参数有效性检查
332 826 : CHK_PTR_NULL(info);
333 826 : CHK_SMART_PTR_NULL(DlHalFunction::GetInstance().dlHalDrvGetPlatformInfo);
334 826 : HCCL_INFO("Entry-hrtDrvGetPlatformInfo");
335 826 : drvError_t ret = DlHalFunction::GetInstance().dlHalDrvGetPlatformInfo(info);
336 826 : CHK_PRT_RET(
337 : ret != DRV_ERROR_NONE,
338 : HCCL_ERROR(
339 : "errNo[0x%016llx] hrtDrvGetPlatformInfo fail,"
340 : "return[%d].",
341 : HCCL_ERROR_CODE(HCCL_E_DRV), ret),
342 : HCCL_E_DRV);
343 826 : return HCCL_SUCCESS;
344 : }
345 :
346 530 : HcclResult hrtHalGetChipInfo(uint32_t devId, std::string& chipName)
347 : {
348 : // 参数有效性检查
349 530 : CHK_SMART_PTR_NULL(DlHalFunction::GetInstance().dlHalGetChipInfo);
350 530 : halChipInfo chipInfo = {};
351 530 : drvError_t ret = DlHalFunction::GetInstance().dlHalGetChipInfo(devId, &chipInfo);
352 530 : CHK_PRT_RET(
353 : ret != DRV_ERROR_NONE,
354 : HCCL_ERROR(
355 : "errNo[0x%016llx] hrtHalGetChipInfo fail,"
356 : "return[%d].",
357 : HCCL_ERROR_CODE(HCCL_E_DRV), ret),
358 : HCCL_E_DRV);
359 : chipName
360 1590 : = std::string(reinterpret_cast<char*>(chipInfo.type)) + std::string(reinterpret_cast<char*>(chipInfo.name));
361 530 : HCCL_INFO("hrtHalGetChipInfo succ chipName[%s]", chipName.c_str());
362 530 : return HCCL_SUCCESS;
363 : }
364 :
365 0 : HcclResult hrtHalBindCgroup(BIND_CGROUP_TYPE bindType)
366 : {
367 0 : if (DlHalFunction::GetInstance().dlHalBindCgroup == nullptr) {
368 0 : HCCL_ERROR("dlHalBindCgroup is nullptr, can not use dlHalBindCgroup");
369 0 : return HCCL_E_DRV;
370 : }
371 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalBindCgroup(bindType);
372 0 : CHK_PRT_RET(
373 : ret != DRV_ERROR_NONE,
374 : HCCL_ERROR(
375 : "errNo[0x%016llx] hrtHalBindCgroup fail,"
376 : "return[%d].",
377 : HCCL_ERROR_CODE(HCCL_E_DRV), ret),
378 : HCCL_E_DRV);
379 0 : return HCCL_SUCCESS;
380 : }
381 :
382 0 : HcclResult hrtDrvDeviceGetPhyIdByIndex([[maybe_unused]] u32 deviceLogicId, [[maybe_unused]] u32& devicePhyId)
383 : {
384 0 : return HCCL_SUCCESS;
385 : }
386 :
387 0 : void MemoryPreFetchImpl(u64 start, u64 end, u32 pageSize)
388 : {
389 : // 多线程并发对待映射的内存进行逐页表的预访问
390 : volatile uint64_t* ptr;
391 0 : for (u64 i = start; i < end; i += pageSize) {
392 0 : ptr = reinterpret_cast<volatile uint64_t*>(i);
393 0 : *ptr;
394 : }
395 0 : }
396 :
397 0 : HcclResult MemoryPreFetch(u64 size, void* hostPtr)
398 : {
399 0 : if (size >= PRE_FETCH_MEMORY_THRESHOLD) {
400 0 : std::vector<std::unique_ptr<std::thread>> threads(PRE_FETCH_THREADS_NUM);
401 0 : u64 chunkSize = size / PRE_FETCH_THREADS_NUM;
402 0 : u64 hostPtrUpperLimit = size + reinterpret_cast<uintptr_t>(hostPtr);
403 0 : for (unsigned int i = 0; i < PRE_FETCH_THREADS_NUM; ++i) {
404 0 : u64 start = i * chunkSize + reinterpret_cast<uintptr_t>(hostPtr);
405 0 : u64 end = (start + chunkSize < hostPtrUpperLimit) ? start + chunkSize : hostPtrUpperLimit;
406 0 : threads[i].reset(new (std::nothrow) std::thread(&MemoryPreFetchImpl, start, end, MEMORY_PAGE_SIZE));
407 0 : CHK_PRT_RET(
408 : !threads[i], HCCL_ERROR("[MemoryPreFetch]threads[%d] threads reset failed.", i), HCCL_E_INTERNAL);
409 : }
410 :
411 0 : for (auto& thread : threads) {
412 0 : thread->join();
413 : }
414 0 : }
415 0 : return HCCL_SUCCESS;
416 : }
417 :
418 954 : HcclResult hrtHalHostRegister(void* hostPtr, u64 size, u32 flag, u32 devid, void*& devPtr)
419 : {
420 954 : CHK_PTR_NULL(hostPtr);
421 954 : if (flag == HOST_MEM_MAP_DEV_PCIE_TH || flag == HOST_MEM_MAP_DEV) {
422 0 : CHK_RET(MemoryPreFetch(size, hostPtr));
423 : }
424 :
425 954 : if (DlHalFunction::GetInstance().dlHalHostRegister == nullptr) {
426 0 : HCCL_ERROR("dlHalHostRegister is nullptr, can not use dlHalHostRegister");
427 0 : return HCCL_E_DRV;
428 : }
429 :
430 954 : drvError_t ret = DlHalFunction::GetInstance().dlHalHostRegister(hostPtr, size, flag, devid, &devPtr);
431 954 : CHK_PRT_RET(
432 : ret != DRV_ERROR_NONE,
433 : HCCL_ERROR(
434 : "[hrtHalHostRegister]errNo[0x%016llx]"
435 : "hrtHalHostRegister fail, return[%d], para: size[%llu] flag[%u] devid[%u].",
436 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, size, flag, devid),
437 : HCCL_E_DRV);
438 954 : return HCCL_SUCCESS;
439 : }
440 :
441 965 : HcclResult hrtHalHostUnregister(void* hostPtr, u32 devid)
442 : {
443 965 : CHK_PTR_NULL(hostPtr);
444 965 : if (DlHalFunction::GetInstance().dlHalHostUnregister == nullptr) {
445 0 : HCCL_ERROR("dlHalHostUnregister is nullptr, can not use dlHalHostUnregister");
446 0 : return HCCL_E_DRV;
447 : }
448 :
449 964 : drvError_t ret = DlHalFunction::GetInstance().dlHalHostUnregister(hostPtr, devid);
450 963 : CHK_PRT_RET(
451 : ret != DRV_ERROR_NONE,
452 : HCCL_ERROR(
453 : "[hrtHalHostUnregister]errNo[0x%016llx]"
454 : "hrtHalHostUnregister fail, return[%d], para: devid[%u].",
455 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devid),
456 : HCCL_E_DRV);
457 :
458 963 : return HCCL_SUCCESS;
459 : }
460 :
461 0 : HcclResult hrtHalHostUnregisterEx(void* hostPtr, u32 devid, u32 flag)
462 : {
463 0 : CHK_PTR_NULL(hostPtr);
464 0 : if (DlHalFunction::GetInstance().dlHalHostUnregisterEx == nullptr) {
465 0 : HCCL_ERROR("dlHalHostUnregisterEx is nullptr, can not use dlHalHostUnregisterEx");
466 0 : return HCCL_E_DRV;
467 : }
468 :
469 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalHostUnregisterEx(hostPtr, devid, flag);
470 0 : CHK_PRT_RET(
471 : ret != DRV_ERROR_NONE,
472 : HCCL_ERROR(
473 : "[hrtHalHostUnregisterEx]errNo[0x%016llx]"
474 : "hrtHalHostUnregisterEx fail, return[%d], para: devid[%u].",
475 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devid),
476 : HCCL_E_DRV);
477 :
478 0 : return HCCL_SUCCESS;
479 : }
480 :
481 1120 : HcclResult hrtHalMemCtl(int type, void* input, size_t inputSize, void* output, size_t* outputSize)
482 : {
483 1120 : CHK_PTR_NULL(input);
484 1120 : CHK_PTR_NULL(output);
485 1120 : CHK_PTR_NULL(outputSize);
486 1120 : if (DlHalFunction::GetInstance().dlHalMemCtl == nullptr) {
487 0 : HCCL_ERROR("dlHalMemCtl is nullptr, can not use dlHalMemCtl");
488 0 : return HCCL_E_DRV;
489 : }
490 :
491 1120 : drvError_t ret = DlHalFunction::GetInstance().dlHalMemCtl(type, input, inputSize, output, outputSize);
492 1120 : CHK_PRT_RET(
493 : ret != DRV_ERROR_NONE,
494 : HCCL_ERROR(
495 : "[dlHalMemCtl]errNo[0x%016llx]"
496 : "dlHalMemCtl fail, return[%d], para: type[%u].",
497 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, type),
498 : HCCL_E_DRV);
499 :
500 1120 : return HCCL_SUCCESS;
501 : }
502 :
503 0 : HcclResult hrtHalSensorNodeRegister([[maybe_unused]] uint32_t devId, [[maybe_unused]] uint64_t* handle)
504 : {
505 : #ifdef CCL_KERNEL
506 0 : CHK_PTR_NULL(handle);
507 0 : if (UNLIKELY(!hccl::DlHalFunction::GetInstance().DlHalFunctionIsInit())) {
508 0 : CHK_RET(DlHalFunction::GetInstance().DlHalFunctionInit());
509 : }
510 0 : if (DlHalFunction::GetInstance().dlHalSensorNodeRegister == nullptr) {
511 0 : HCCL_ERROR("dlHalSensorNodeRegister is nullptr, can not use dlHalSensorNodeRegister");
512 0 : return HCCL_E_DRV;
513 : }
514 :
515 0 : constexpr uint32_t HCCL_ASSERT_EVENT_MASK = 0xE00; // 当前仅使能 bit 9-B
516 0 : constexpr uint32_t HCCL_DEASSERT_EVENT_MASK = 0x0; // 0x09/0x0A/0x0B 使能为通知事件
517 : // 构造 Sensor Node 注册参数
518 0 : halSensorNodeCfg cfg = {};
519 0 : cfg.NodeType = HAL_DMS_DEV_TYPE_HCCP;
520 0 : cfg.SensorType = SAFTY_STATE_SENSOR_TYTPE;
521 0 : cfg.AssertEventMask = HCCL_ASSERT_EVENT_MASK;
522 0 : cfg.DeassertEventMask = HCCL_DEASSERT_EVENT_MASK;
523 :
524 0 : auto const sRet = snprintf_s(cfg.name, sizeof(cfg.name), sizeof(cfg.name) - 1U, "hccl_%d", getpid());
525 0 : if (sRet <= 0) {
526 0 : HCCL_ERROR("[CannErrorReporter][ConstructSensorNodeCfg] sprintf_s name err, ret[%d].", sRet);
527 0 : return HCCL_E_INTERNAL;
528 : }
529 :
530 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalSensorNodeRegister(devId, &cfg, handle);
531 0 : CHK_PRT_RET(
532 : ret != DRV_ERROR_NONE,
533 : HCCL_ERROR(
534 : "[hrtHalSensorNodeRegister]errNo[0x%016llx]"
535 : "hrtHalSensorNodeRegister fail, return[%d], para: devid[%u], nodeName[%s], nodeType[%u], sensorType[%u].",
536 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devId, cfg.name, cfg.NodeType, cfg.SensorType),
537 : HCCL_E_DRV);
538 :
539 0 : return HCCL_SUCCESS;
540 : #else
541 : HCCL_ERROR("[halSensorNodeRegister]Does not support this interface.");
542 : return HCCL_E_NOT_SUPPORT;
543 : #endif
544 : }
545 :
546 0 : HcclResult hrtHalSensorNodeUnregister([[maybe_unused]] uint32_t devId, [[maybe_unused]] uint64_t handle)
547 : {
548 : #ifdef CCL_KERNEL
549 0 : if (DlHalFunction::GetInstance().dlHalSensorNodeUnregister == nullptr) {
550 0 : HCCL_ERROR("dlHalSensorNodeUnregister is nullptr, can not use dlHalSensorNodeUnregister");
551 0 : return HCCL_E_DRV;
552 : }
553 :
554 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalSensorNodeUnregister(devId, handle);
555 0 : CHK_PRT_RET(
556 : ret != DRV_ERROR_NONE,
557 : HCCL_ERROR(
558 : "[hrtHalSensorNodeUnregister]errNo[0x%016llx]"
559 : "hrtHalSensorNodeUnregister fail, return[%d], para: devid[%u], handle[%llu].",
560 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devId, handle),
561 : HCCL_E_DRV);
562 :
563 0 : return HCCL_SUCCESS;
564 : #else
565 : HCCL_ERROR("[halSensorNodeUnregister]Does not support this interface.");
566 : return HCCL_E_NOT_SUPPORT;
567 : #endif
568 : }
569 0 : halGeneralEventType_t TranslateEventType(HcclGeneralEventType assertion)
570 : {
571 0 : switch (assertion) {
572 0 : case HcclGeneralEventType::HCCL_GENERAL_EVENT_TYPE_RESUME:
573 0 : return GENERAL_EVENT_TYPE_RESUME;
574 :
575 0 : case HcclGeneralEventType::HCCL_GENERAL_EVENT_TYPE_OCCUR:
576 0 : return GENERAL_EVENT_TYPE_OCCUR;
577 :
578 0 : case HcclGeneralEventType::HCCL_GENERAL_EVENT_TYPE_ONE_TIME:
579 0 : return GENERAL_EVENT_TYPE_ONE_TIME;
580 :
581 0 : case HcclGeneralEventType::HCCL_GENERAL_EVENT_TYPE_MAX:
582 0 : return GENERAL_EVENT_TYPE_MAX;
583 :
584 0 : default: {
585 0 : HCCL_ERROR("[TranslateEventType]Not support the General Event type[%d].", assertion);
586 0 : return GENERAL_EVENT_TYPE_MAX;
587 : }
588 : }
589 : }
590 :
591 0 : HcclResult hrtHalSensorNodeUpdateState(
592 : [[maybe_unused]] uint32_t devId, [[maybe_unused]] uint64_t handle, [[maybe_unused]] int val,
593 : [[maybe_unused]] HcclGeneralEventType assertion)
594 : {
595 : #ifdef CCL_KERNEL
596 0 : if (DlHalFunction::GetInstance().dlHalSensorNodeUpdateState == nullptr) {
597 0 : HCCL_ERROR("dlHalSensorNodeUpdateState is nullptr, can not use dlHalSensorNodeUpdateState");
598 0 : return HCCL_E_DRV;
599 : }
600 :
601 0 : halGeneralEventType_t type = TranslateEventType(assertion);
602 0 : drvError_t ret = DlHalFunction::GetInstance().dlHalSensorNodeUpdateState(devId, handle, val, type);
603 0 : CHK_PRT_RET(
604 : ret != DRV_ERROR_NONE,
605 : HCCL_RUN_WARNING(
606 : "[hrtHalSensorNodeUpdateState]errNo[0x%016llx]"
607 : "hrtHalSensorNodeUpdateState fail, return[%d], para: devid[%u], handle[%llu], val[%d], assertion[%u].",
608 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, devId, handle, val, assertion),
609 : HCCL_E_DRV);
610 :
611 0 : return HCCL_SUCCESS;
612 : #else
613 : HCCL_ERROR("[halSensorNodeUpdateState]Does not support this interface.");
614 : return HCCL_E_NOT_SUPPORT;
615 : #endif
616 : }
617 :
618 530 : HcclResult hrtHalGetDeviceType(const uint32_t devId, DevType& devType)
619 : {
620 530 : if (UNLIKELY(!hccl::DlHalFunction::GetInstance().DlHalFunctionIsInit())) {
621 0 : CHK_RET(DlHalFunction::GetInstance().DlHalFunctionInit());
622 : }
623 :
624 530 : std::string chipName;
625 530 : HcclResult ret = hrtHalGetChipInfo(devId, chipName);
626 530 : if (ret != HCCL_SUCCESS) {
627 0 : HCCL_ERROR("hrtHalGetChipInfo failed, ret[%d], devId[%u]", ret, devId);
628 0 : devType = DevType::DEV_TYPE_COUNT;
629 0 : return ret;
630 : }
631 530 : HCCL_INFO("[Get][DeviceType]Chip name[%s].", chipName.c_str());
632 :
633 530 : if (chipName.find("Ascend950") != std::string::npos) {
634 0 : devType = DevType::DEV_TYPE_950;
635 0 : return HCCL_SUCCESS;
636 : }
637 :
638 1060 : if (chipName.find("Ascend910_96") != std::string::npos || chipName.find("Ascend960") != std::string::npos
639 1060 : || chipName.find("ascend960") != std::string::npos) {
640 0 : devType = DevType::DEV_TYPE_960;
641 0 : return HCCL_SUCCESS;
642 : }
643 :
644 530 : auto iter = SOC_VER_CONVERT.find(chipName);
645 530 : if (iter == SOC_VER_CONVERT.end()) {
646 0 : HCCL_ERROR(
647 : "[Get][DeviceType]errNo[0x%016llx] hrtHalGetChipInfo get illegal chipver, chipName[%s].",
648 : HCCL_ERROR_CODE(HCCL_E_DRV), chipName.c_str());
649 0 : devType = DevType::DEV_TYPE_COUNT;
650 0 : return HCCL_E_DRV;
651 : }
652 530 : devType = iter->second;
653 :
654 530 : HCCL_INFO("[Get][DeviceType]deviceId[%u] get devType[%d] success!", devId, devType);
655 530 : return HCCL_SUCCESS;
656 530 : }
657 :
658 1 : HcclResult GetRunSideIsDevice(bool& isDeviceSide)
659 : {
660 : static bool deviceSide = false;
661 : static bool init = false;
662 1 : if (UNLIKELY(!init)) {
663 1 : if (UNLIKELY(!hccl::DlHalFunction::GetInstance().DlHalFunctionIsInit())) {
664 0 : CHK_RET(DlHalFunction::GetInstance().DlHalFunctionInit());
665 : }
666 1 : u32 info = 0;
667 1 : CHK_RET(hrtDrvGetPlatformInfo(&info));
668 1 : deviceSide = info == 0 ? true : false;
669 1 : init = true;
670 : }
671 :
672 1 : isDeviceSide = deviceSide;
673 1 : return HCCL_SUCCESS;
674 : }
675 :
676 22 : HcclResult CheckRunSideIsDevice()
677 : {
678 : static bool isDeviceSide = false;
679 : static bool init = false;
680 22 : if (UNLIKELY(!init)) {
681 1 : CHK_RET(GetRunSideIsDevice(isDeviceSide));
682 1 : init = true;
683 : }
684 22 : if (UNLIKELY(!isDeviceSide)) {
685 0 : HCCL_ERROR("currently running on host");
686 0 : return HCCL_E_INTERNAL;
687 : }
688 22 : return HCCL_SUCCESS;
689 : }
690 : extern "C" {
691 : drvError_t __attribute__((weak)) drvGetLocalDevIDByHostDevID(uint32_t host_dev_id, uint32_t* local_dev_id);
692 : drvError_t __attribute__((weak)) drvMemSmmuQuery(DVdevice device, uint32_t* SSID);
693 : int32_t __attribute__((weak)) StartMC2MaintenanceThread(mc2Funcs f1, void* p1, mc2Funcs f2, void* p2);
694 : int32_t __attribute__((weak)) AicpuCreateCtrlThread(uint32_t type, mc2Funcs f1, void* p1, mc2Funcs f2, void* p2);
695 : };
696 :
697 189 : HcclResult hrtDrvGetLocalDevIDByHostDevID(u32 hostUdevid, u32* localDevid)
698 : {
699 189 : CHK_PTR_NULL(drvGetLocalDevIDByHostDevID);
700 189 : CHK_PTR_NULL(localDevid);
701 :
702 189 : drvError_t ret = drvGetLocalDevIDByHostDevID(hostUdevid, localDevid);
703 189 : CHK_PRT_RET(
704 : ret != DRV_ERROR_NONE,
705 : HCCL_ERROR(
706 : "[hrtDrvGetLocalDevIDByHostDevID]errNo[0x%016llx]"
707 : "hrtDrvGetLocalDevIDByHostDevID fail, return[%d], para: hostUdevid[%u] localDevid[%p].",
708 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, hostUdevid, localDevid),
709 : HCCL_E_DRV);
710 189 : return HCCL_SUCCESS;
711 : }
712 :
713 0 : HcclResult hrtDrvMemSmmuQuery(uint32_t localDevid, uint32_t* SSID)
714 : {
715 0 : CHK_PTR_NULL(drvMemSmmuQuery);
716 0 : CHK_PTR_NULL(SSID);
717 :
718 0 : drvError_t ret = drvMemSmmuQuery(localDevid, SSID);
719 0 : CHK_PRT_RET(
720 : ret != DRV_ERROR_NONE,
721 : HCCL_ERROR(
722 : "[hrtDrvMemSmmuQuery]errNo[0x%016llx]"
723 : "hrtDrvMemSmmuQuery fail, return[%d], para: localDevid[%u] SSID[%p].",
724 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, localDevid, SSID),
725 : HCCL_E_DRV);
726 0 : return HCCL_SUCCESS;
727 : }
728 :
729 779 : bool IsSupportStartMC2MaintenanceThread()
730 : {
731 779 : return (StartMC2MaintenanceThread == nullptr && AicpuCreateCtrlThread == nullptr) ? false : true;
732 : }
733 :
734 140 : HcclResult hrtHalStartMC2MaintenanceThread(mc2Funcs f1, void* p1, mc2Funcs f2, void* p2)
735 : {
736 140 : CHK_PTR_NULL(f1);
737 140 : CHK_PTR_NULL(p1);
738 140 : CHK_PTR_NULL(f2);
739 140 : CHK_PTR_NULL(p2);
740 :
741 140 : int ret = 0;
742 :
743 140 : if (AicpuCreateCtrlThread != nullptr) {
744 140 : HCCL_INFO("[hrtHalStartMC2MaintenanceThread] AicpuCreateCtrlThread");
745 140 : ret = AicpuCreateCtrlThread(THREAD_TYPE_HCOM, f1, p1, f2, p2);
746 140 : CHK_PRT_RET(
747 : (ret != 0 && ret != AICPU_SCHEDULE_THREAD_ALREADY_EXISTS),
748 : HCCL_ERROR(
749 : "[AicpuCreateCtrlThread]errNo[0x%016llx]"
750 : "AicpuCreateCtrlThread fail, return[%d], para: mc2Funcs f1[%p] p1[%p], mc2Funcs f2[%p] p2[%p].",
751 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, f1, p1, f2, p2),
752 : HCCL_E_DRV);
753 0 : } else if (StartMC2MaintenanceThread != nullptr) {
754 0 : HCCL_INFO("[hrtHalStartMC2MaintenanceThread] StartMC2MaintenanceThread");
755 0 : ret = StartMC2MaintenanceThread(f1, p1, f2, p2);
756 0 : CHK_PRT_RET(
757 : (ret != 0 && ret != AICPU_SCHEDULE_THREAD_ALREADY_EXISTS),
758 : HCCL_ERROR(
759 : "[StartMC2MaintenanceThread]errNo[0x%016llx]"
760 : "StartMC2MaintenanceThread fail, return[%d], para: mc2Funcs f1[%p] p1[%p], mc2Funcs f2[%p] p2[%p].",
761 : HCCL_ERROR_CODE(HCCL_E_DRV), ret, f1, p1, f2, p2),
762 : HCCL_E_DRV);
763 : } else {
764 0 : HCCL_ERROR(
765 : "[hrtHalStartMC2MaintenanceThread] AicpuCreateCtrlThread and StartMC2MaintenanceThread are both nullptr");
766 0 : return HCCL_E_DRV;
767 : }
768 140 : return HCCL_SUCCESS;
769 : }
770 :
771 4809 : HcclResult hrtHalResourceIdRestore(u32 devId, u32 tsId, drvIdType_t resType, u32 resId, u32 flag)
772 : {
773 : // 兼容老的12包,找不到符号时不报错,返回HCCL_E_NOT_SUPPORT由上层处理是否报错
774 4809 : CHK_PRT_RET(
775 : halResourceIdRestore == nullptr,
776 : HCCL_WARNING("hrtHalResourceIdRestore not support, halResourceIdRestore is nullptr"), HCCL_E_NOT_SUPPORT);
777 :
778 : drvResIdKey resInfo;
779 4809 : resInfo.ruDevId = devId;
780 4809 : resInfo.tsId = tsId;
781 4809 : resInfo.resType = resType;
782 4809 : resInfo.resId = resId;
783 4809 : resInfo.flag = flag;
784 :
785 4809 : int checkResult = halResourceIdRestore(&resInfo);
786 4809 : if (checkResult != 0) {
787 0 : HCCL_ERROR(
788 : "[drv api]res restore failed, result:%d, resType:%d, resId:%u, tsId:%u, ruDevId:%u, flag:%u", checkResult,
789 : resInfo.resType, resInfo.resId, resInfo.tsId, resInfo.ruDevId, resInfo.flag);
790 0 : return HCCL_E_DRV;
791 : }
792 :
793 4809 : HCCL_INFO(
794 : "res restore success, resType:%d, resId:%u, tsId:%u, ruDevId:%u, flag:%u", resInfo.resType, resInfo.resId,
795 : resInfo.tsId, resInfo.ruDevId, resInfo.flag);
796 4809 : return HCCL_SUCCESS;
797 : }
|