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