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 "hccp_dl.h"
12 : #include "hccp_tlv.h"
13 : #include "dl_ccu_function.h"
14 :
15 : void *gCcuApiHandle = NULL;
16 : #ifndef CA_CONFIG_LLT
17 : struct RsCcuOps gCcuOps;
18 : #else
19 : struct RsCcuOps gCcuOps = {
20 : .rsCcuInit = ccu_init,
21 : .rsCcuUninit = ccu_uninit,
22 : .rsCcuCustomChannel = ccu_custom_channel,
23 : .rsCcuGetCqeBaseAddr = ccu_get_cqe_base_addr,
24 : .rsCcuGetMemInfo = ccu_get_mem_info,
25 : .rsCcuTlvRequest = ccu_tlv_request,
26 : };
27 : #endif
28 :
29 1 : STATIC int RsCcuDeviceApiInit(void)
30 : {
31 : #ifndef CA_CONFIG_LLT
32 : gCcuOps.rsCcuInit = (int (*)(void)) HccpDlsym(gCcuApiHandle, "ccu_init");
33 : DL_API_RET_IS_NULL_CHECK(gCcuOps.rsCcuInit, "ccu_init");
34 :
35 : gCcuOps.rsCcuUninit = (int (*)(void)) HccpDlsym(gCcuApiHandle, "ccu_uninit");
36 : DL_API_RET_IS_NULL_CHECK(gCcuOps.rsCcuUninit, "ccu_uninit");
37 :
38 : gCcuOps.rsCcuCustomChannel = (int (*)(const struct channel_info_in *in, struct channel_info_out *out))
39 : HccpDlsym(gCcuApiHandle, "ccu_custom_channel");
40 : DL_API_RET_IS_NULL_CHECK(gCcuOps.rsCcuCustomChannel, "ccu_custom_channel");
41 :
42 : gCcuOps.rsCcuGetCqeBaseAddr = (unsigned long long (*)(unsigned int dieId))
43 : HccpDlsym(gCcuApiHandle, "ccu_get_cqe_base_addr");
44 : DL_API_RET_IS_NULL_CHECK(gCcuOps.rsCcuGetCqeBaseAddr, "ccu_get_cqe_base_addr");
45 :
46 : gCcuOps.rsCcuGetMemInfo = (int (*)(unsigned int dieId, unsigned long long memTypeBitmap,
47 : struct ccu_mem_rsp *rsp)) HccpDlsym(gCcuApiHandle, "ccu_get_mem_info");
48 : DL_API_RET_IS_NULL_CHECK(gCcuOps.rsCcuGetMemInfo, "ccu_get_mem_info");
49 :
50 : gCcuOps.rsCcuTlvRequest = (int (*)(struct ccu_tlv_request_info *tlvRequestInfo))
51 : HccpDlsym(gCcuApiHandle, "ccu_tlv_request");
52 : DL_API_RET_IS_NULL_INFO(gCcuOps.rsCcuTlvRequest, "ccu_tlv_request");
53 : #endif
54 1 : return 0;
55 : }
56 :
57 1 : STATIC int RsOpenCcuSo(void)
58 : {
59 : #ifndef CA_CONFIG_LLT
60 : if (gCcuApiHandle == NULL) {
61 : gCcuApiHandle = HccpDlopen("libccu-user-drv.so", RTLD_NOW);
62 : if (gCcuApiHandle != NULL) {
63 : return 0;
64 : }
65 : return -EINVAL;
66 : } else {
67 : hccp_run_info("ccu_api dlopen again!");
68 : }
69 : #endif
70 1 : return 0;
71 : }
72 :
73 1 : STATIC void RsCloseCcuSo(void)
74 : {
75 : #ifndef CA_CONFIG_LLT
76 : if (gCcuApiHandle != NULL) {
77 : (void)HccpDlclose(gCcuApiHandle);
78 : gCcuApiHandle = NULL;
79 : }
80 : #endif
81 1 : return;
82 : }
83 :
84 0 : int RsCcuApiInit(void)
85 : {
86 : int ret;
87 :
88 0 : ret = RsOpenCcuSo();
89 0 : CHK_PRT_RETURN(ret, hccp_err("HccpDlopen[libccu-user-drv.so] failed! ret=[%d],"\
90 : "Please check ccu driver has been installed", ret), ret);
91 :
92 0 : ret = RsCcuDeviceApiInit();
93 0 : if (ret != 0) {
94 0 : hccp_err("[rs_ccu_device_api_init]HccpDlopen failed! ret=[%d]", ret);
95 0 : RsCloseCcuSo();
96 0 : return ret;
97 : }
98 0 : return 0;
99 : }
100 :
101 0 : void RsCcuApiDeinit(void)
102 : {
103 0 : RsCloseCcuSo();
104 0 : return;
105 : }
106 :
107 0 : int RsCcuInit(void)
108 : {
109 0 : if (gCcuApiHandle == NULL || gCcuOps.rsCcuInit == NULL) {
110 : #ifndef CA_CONFIG_LLT
111 : hccp_err("g_ccu_api_handle is NULL or rs_ccu_init is NULL");
112 : return -EINVAL;
113 : #endif
114 : }
115 0 : return gCcuOps.rsCcuInit();
116 : }
117 :
118 0 : int RsCcuCustomChannel(const struct channel_info_in *in, struct channel_info_out *out)
119 : {
120 0 : if (gCcuApiHandle == NULL || gCcuOps.rsCcuCustomChannel == NULL) {
121 : #ifndef CA_CONFIG_LLT
122 : hccp_err("g_ccu_api_handle is NULL or rs_ccu_custom_channel is NULL");
123 : return -EINVAL;
124 : #endif
125 : }
126 0 : return gCcuOps.rsCcuCustomChannel(in, out);
127 : }
128 :
129 1 : int RsCcuGetCqeBaseAddr(unsigned int dieId, unsigned long long *cqeBaseAddr)
130 : {
131 1 : if (gCcuApiHandle == NULL || gCcuOps.rsCcuGetCqeBaseAddr == NULL) {
132 : #ifndef CA_CONFIG_LLT
133 : hccp_err("g_ccu_api_handle is NULL or rs_ccu_get_cqe_base_addr is NULL");
134 : return -EINVAL;
135 : #endif
136 : }
137 1 : CHK_PRT_RETURN(cqeBaseAddr == NULL, hccp_err("cqe_base_addr is null, dieId:%u", dieId), -EINVAL);
138 1 : *cqeBaseAddr = gCcuOps.rsCcuGetCqeBaseAddr(dieId);
139 1 : return 0;
140 : }
141 :
142 0 : int RsCcuUninit(void)
143 : {
144 0 : if (gCcuApiHandle == NULL || gCcuOps.rsCcuUninit == NULL) {
145 : #ifndef CA_CONFIG_LLT
146 : hccp_err("g_ccu_api_handle is NULL or rs_ccu_uninit is NULL");
147 : return -EINVAL;
148 : #endif
149 : }
150 0 : return gCcuOps.rsCcuUninit();
151 : }
152 :
153 0 : int RsCcuGetMemInfo(char *dataIn, char* dataOut, unsigned int *bufferSize)
154 : {
155 0 : struct ccu_mem_rsp *rsp = (struct ccu_mem_rsp *)dataOut;
156 0 : struct CcuMemReq *memReq = (struct CcuMemReq *)dataIn;
157 :
158 0 : if (gCcuApiHandle == NULL || gCcuOps.rsCcuGetMemInfo == NULL) {
159 : #ifndef CA_CONFIG_LLT
160 : hccp_err("g_ccu_api_handle is NULL or rsCcuGetMemInfo is NULL");
161 : return -EINVAL;
162 : #endif
163 : }
164 0 : *bufferSize = sizeof(struct ccu_mem_rsp);
165 0 : return gCcuOps.rsCcuGetMemInfo(memReq->udieIdx, memReq->memTypeBitmap, rsp);
166 : }
167 :
168 1 : bool isCcuTlvReqExist(void) {
169 1 : if (gCcuApiHandle == NULL || gCcuOps.rsCcuTlvRequest == NULL) {
170 : #ifndef CA_CONFIG_LLT
171 : hccp_info("g_ccu_api_handle is NULL or rsCcuTlvRequest is NULL");
172 : return false;
173 : #endif
174 : }
175 1 : return true;
176 : }
177 :
178 1 : int RsCcuTlvRequest(unsigned int type, char *dataIn, char *dataOut, unsigned int dataInlength, unsigned int *dataOutLength,
179 : unsigned int dataOutMaxLength) {
180 1 : int ret = 0;
181 1 : struct ccu_tlv_request_info tlvRequestInfo = {
182 : .ccuMsg = type,
183 : .dataIn = dataIn,
184 : .dataInLength = dataInlength,
185 : .dataOut = dataOut,
186 : .dataOutMaxLength = dataOutMaxLength,
187 : };
188 1 : ret = gCcuOps.rsCcuTlvRequest(&tlvRequestInfo);
189 1 : *dataOutLength = tlvRequestInfo.dataOutLength;
190 1 : return ret;
191 : }
|