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