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 "dlhal_function.h"
12 :
13 : #include <string>
14 : #include <map>
15 : #include "log.h"
16 : #include "hccl_dl.h"
17 :
18 : namespace hccl {
19 15457 : DlHalFunction &DlHalFunction::GetInstance()
20 : {
21 15457 : static DlHalFunction hcclDlHalFunction;
22 15457 : return hcclDlHalFunction;
23 : }
24 :
25 19 : DlHalFunction::DlHalFunction() : handle_(nullptr)
26 : {
27 19 : }
28 :
29 19 : DlHalFunction::~DlHalFunction()
30 : {
31 19 : if (handle_ != nullptr) {
32 19 : (void)HcclDlclose(handle_);
33 19 : handle_ = nullptr;
34 : }
35 19 : }
36 :
37 3930 : HcclResult DlHalFunction::DlHalFunctionEschedInit()
38 : {
39 3930 : dlHalEschedSubmitEvent = (drvError_t(*)(unsigned int, struct event_summary *))HcclDlsym(handle_,
40 3930 : "halEschedSubmitEvent");
41 3930 : CHK_SMART_PTR_NULL(dlHalEschedSubmitEvent);
42 :
43 3930 : dlHalEschedAttachDevice = (drvError_t(*)(unsigned int))HcclDlsym(handle_,
44 3930 : "halEschedAttachDevice");
45 3930 : CHK_SMART_PTR_NULL(dlHalEschedAttachDevice);
46 :
47 3930 : dlHalGetAPIVersion = (drvError_t(*)(int *))HcclDlsym(handle_, "halGetAPIVersion");
48 3930 : CHK_SMART_PTR_NULL(dlHalGetAPIVersion);
49 :
50 3930 : dlHalEschedDettachDevice = (drvError_t(*)(unsigned int))HcclDlsym(handle_, "halEschedDettachDevice");
51 3930 : CHK_SMART_PTR_NULL(dlHalEschedDettachDevice);
52 :
53 3930 : dlHalEschedCreateGrp = (drvError_t(*)(unsigned int, unsigned int, GROUP_TYPE))HcclDlsym(handle_,
54 3930 : "halEschedCreateGrp");
55 3930 : CHK_SMART_PTR_NULL(dlHalEschedCreateGrp);
56 :
57 0 : dlHalEschedRegisterAckFunc = (drvError_t(*)(unsigned int, unsigned int,
58 : void (*ackFunc)(unsigned int, unsigned int, u8 *, unsigned int)))
59 3930 : HcclDlsym(handle_, "halEschedRegisterAckFunc");
60 3930 : CHK_SMART_PTR_NULL(dlHalEschedRegisterAckFunc);
61 :
62 0 : dlHalDrvQueryProcessHostPid = (drvError_t(*)(int, unsigned int *, unsigned int *,
63 3930 : unsigned int *, unsigned int *))HcclDlsym(handle_, "drvQueryProcessHostPid");
64 3930 : CHK_SMART_PTR_NULL(dlHalDrvQueryProcessHostPid);
65 :
66 0 : dlDrvMemCpy = (DVresult(*)(unsigned long long dst, unsigned long long destMax,
67 3930 : unsigned long long src, unsigned long long count))HcclDlsym(handle_, "drvMemcpy");
68 3930 : CHK_SMART_PTR_NULL(dlDrvMemCpy);
69 :
70 3930 : dlHalDrvGetDevNum = (drvError_t(*)(uint32_t *))HcclDlsym(handle_, "drvGetDevNum");
71 3930 : CHK_SMART_PTR_NULL(dlHalDrvGetDevNum);
72 :
73 3930 : dlDrvDeviceGetBareTgid = (pid_t(*)(void))HcclDlsym(handle_, "drvDeviceGetBareTgid");
74 3930 : CHK_SMART_PTR_NULL(dlDrvDeviceGetBareTgid);
75 :
76 3930 : dlHalGetDeviceInfo = (drvError_t(*)(uint32_t, int32_t, int32_t, int64_t *))HcclDlsym(handle_, "halGetDeviceInfo");
77 3930 : CHK_SMART_PTR_NULL(dlHalGetDeviceInfo);
78 :
79 0 : dlHalGrpQuery = (drvError_t(*)(GroupQueryCmdType, void *, unsigned int, void *, unsigned int *))
80 3930 : HcclDlsym(handle_, "halGrpQuery");
81 3930 : CHK_SMART_PTR_NULL(dlHalGrpQuery);
82 :
83 3930 : dlHalDrvGetPlatformInfo = (drvError_t(*)(uint32_t *))HcclDlsym(handle_, "drvGetPlatformInfo");
84 3930 : CHK_SMART_PTR_NULL(dlHalDrvGetPlatformInfo);
85 :
86 3930 : dlHalGetChipInfo = (drvError_t(*)(uint32_t, halChipInfo *))HcclDlsym(handle_, "halGetChipInfo");
87 3930 : if (dlHalGetChipInfo == nullptr) {
88 0 : HCCL_WARNING("dlHalGetChipInfo is nullptr, can not use halGetChipInfo");
89 : }
90 :
91 3930 : dlHalBindCgroup = (drvError_t(*)(BIND_CGROUP_TYPE))HcclDlsym(handle_, "halBindCgroup");
92 3930 : if (dlHalBindCgroup == nullptr) {
93 0 : HCCL_WARNING("dlHalBindCgroup is nullptr, can not use dlHalBindCgroup");
94 : }
95 :
96 0 : dlHalHostRegister = (drvError_t(*)(void *, uint64_t, uint32_t, uint32_t, void **))
97 3930 : HcclDlsym(handle_, "halHostRegister");
98 3930 : if (dlHalHostRegister == nullptr) {
99 0 : HCCL_WARNING("dlHalHostRegister is nullptr, can not use dlHalHostRegister");
100 : }
101 :
102 3930 : dlHalHostUnregister = (drvError_t(*)(void *, uint32_t))HcclDlsym(handle_, "halHostUnregister");
103 3930 : if (dlHalHostUnregister == nullptr) {
104 0 : HCCL_WARNING("dlHalHostUnregister is nullptr, can not use dlHalHostUnregister");
105 : }
106 :
107 3930 : dlHalHostUnregisterEx = (drvError_t(*)(void *, uint32_t, uint32_t))HcclDlsym(handle_, "halHostUnregisterEx");
108 3930 : if (dlHalHostUnregisterEx == nullptr) {
109 0 : HCCL_WARNING("dlHalHostUnregisterEx is nullptr, can not use dlHalHostUnregisterEx");
110 : }
111 :
112 3930 : dlHalMemCtl = (drvError_t(*)(int, void*, size_t, void*, size_t*))HcclDlsym(handle_, "halMemCtl");
113 3930 : CHK_SMART_PTR_NULL(dlHalMemCtl);
114 : #ifdef CCL_KERNEL
115 3930 : dlHalSensorNodeRegister = (drvError_t(*)(uint32_t, struct halSensorNodeCfg *, uint64_t *))HcclDlsym(handle_,
116 3930 : "halSensorNodeRegister");
117 3930 : CHK_SMART_PTR_NULL(dlHalSensorNodeRegister);
118 :
119 3930 : dlHalSensorNodeUnregister = (drvError_t(*)(uint32_t, uint64_t))HcclDlsym(handle_, "halSensorNodeUnregister");
120 3930 : CHK_SMART_PTR_NULL(dlHalSensorNodeUnregister);
121 :
122 3930 : dlHalSensorNodeUpdateState = (drvError_t(*)(uint32_t, uint64_t, int, halGeneralEventType_t))HcclDlsym(handle_,
123 3930 : "halSensorNodeUpdateState");
124 3930 : CHK_SMART_PTR_NULL(dlHalSensorNodeUpdateState);
125 : #endif
126 3930 : return HCCL_SUCCESS;
127 : }
128 :
129 3930 : HcclResult DlHalFunction::DlHalFunctionInit()
130 : {
131 3930 : std::lock_guard<std::mutex> lock(handleMutex_);
132 3930 : if (handle_ == nullptr) {
133 20 : handle_ = HcclDlopen("libascend_hal.so", RTLD_NOW);
134 20 : const char* errMsg = dlerror();
135 20 : CHK_PRT_RET(handle_ == nullptr, HCCL_ERROR("dlopen [%s] failed, %s", "libascend_hal.so",\
136 : (errMsg == nullptr) ? "please check the file exist or permission denied." : errMsg),\
137 : HCCL_E_OPEN_FILE_FAILURE);
138 : }
139 :
140 3930 : CHK_RET(DlHalFunctionEschedInit());
141 3930 : return HCCL_SUCCESS;
142 3930 : }
143 : }
|