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 <unistd.h>
12 : #include <stdlib.h>
13 : #include <sys/types.h>
14 : #include <netinet/in.h>
15 : #include <arpa/inet.h>
16 : #include <dlfcn.h>
17 : #include "urma_opcode.h"
18 : #include "user_log.h"
19 : #include "dl_hal_function.h"
20 : #include "dl_urma_function.h"
21 : #include "dl_net_function.h"
22 : #include "dl_ccu_function.h"
23 : #include "ra_rs_err.h"
24 : #include "rs_inner.h"
25 : #include "rs_ctx_inner.h"
26 : #include "rs_ub_jfc.h"
27 :
28 : struct ExtJfcAttr {
29 : urma_jfc_t *jfc;
30 : unsigned int jfcId;
31 : unsigned long long cqeBaseAddrVa;
32 : };
33 :
34 0 : STATIC int RsInitJfcAttr(struct RsCtxJfcCb *jfcCb, urma_jfc_cfg_t *jfcCfg, struct ExtJfcAttr *jfcAttr)
35 : {
36 0 : unsigned int dieId = 0;
37 0 : int ret = 0;
38 :
39 0 : ret = RsUrmaAllocJfc(jfcCb->devCb->urmaCtx, jfcCfg, &jfcAttr->jfc);
40 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_urma_alloc_jfc failed, ret:%d errno:%d", ret, errno), -EOPENSRC);
41 :
42 0 : if (jfcCb->jfcType == JFC_MODE_USER_CTL_NORMAL) {
43 0 : return 0;
44 : }
45 :
46 0 : if (jfcCb->jfcType == JFC_MODE_CCU_POLL) {
47 0 : if (!jfcCb->ccuExCfg.valid || jfcCb->ccuExCfg.cqeFlag == 0) {
48 0 : dieId = jfcCb->devCb->devAttr.ub.dieId;
49 : } else {
50 0 : dieId = (jfcCb->devCb->devAttr.ub.dieId == 0) ? 1 : 0;
51 : }
52 0 : ret = RsCcuGetCqeBaseAddr(dieId, &jfcAttr->cqeBaseAddrVa);
53 0 : if (ret != 0 || jfcAttr->cqeBaseAddrVa == 0) {
54 0 : hccp_err("rs_ccu_get_cqe_base_addr failed, ret:%d, dieId:%u", ret, dieId);
55 0 : ret = -EOPENSRC;
56 0 : goto free_jfc;
57 : }
58 : } else {
59 0 : ret = RsNetGetCqeBaseAddr(jfcCb->devCb->devAttr.ub.dieId, &jfcAttr->cqeBaseAddrVa);
60 0 : if (ret != 0 || jfcAttr->cqeBaseAddrVa == 0) {
61 0 : hccp_err("rs_net_get_cqe_base_addr failed, ret:%d, dieId:%u", ret, jfcCb->devCb->devAttr.ub.dieId);
62 0 : ret = -EOPENSRC;
63 0 : goto free_jfc;
64 : }
65 : }
66 :
67 0 : ret = RsNetAllocJfcId(jfcCb->devCb->urmaDev->name, jfcCb->jfcType, &jfcAttr->jfcId);
68 0 : if (ret != 0) {
69 0 : hccp_err("rs_net_alloc_jfc_id failed, ret:%d", ret);
70 0 : goto free_jfc;
71 : }
72 :
73 0 : return 0;
74 :
75 0 : free_jfc:
76 0 : (void)RsUrmaFreeJfc(jfcAttr->jfc);
77 0 : return ret;
78 : }
79 :
80 0 : STATIC void RsDeinitJfcAttr(struct RsCtxJfcCb *jfcCb, urma_jfc_cfg_t *jfcCfg, struct ExtJfcAttr *jfcAttr)
81 : {
82 : (void)jfcCfg;
83 0 : (void)RsUrmaFreeJfc(jfcAttr->jfc);
84 0 : if (jfcCb->jfcType == JFC_MODE_USER_CTL_NORMAL) {
85 0 : return;
86 : }
87 0 : (void)RsNetFreeJfcId(jfcCb->devCb->urmaDev->name, jfcCb->jfcType, jfcAttr->jfcId);
88 : }
89 :
90 0 : STATIC int RsSetJfcOpt(struct RsCtxJfcCb *jfcCb, struct ExtJfcAttr *jfcAttr)
91 : {
92 0 : int ret = 0;
93 :
94 0 : if (jfcCb->jfcType == JFC_MODE_USER_CTL_NORMAL) {
95 0 : return ret;
96 : }
97 :
98 0 : ret = RsUrmaSetJfcOpt(jfcAttr->jfc, URMA_JFC_ID, (void *)&jfcAttr->jfcId, sizeof(uint32_t));
99 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_urma_set_jfc_opt URMA_JFC_ID failed, ret:%d, errno:%d", ret, errno),
100 : -EOPENSRC);
101 :
102 0 : ret = RsUrmaSetJfcOpt(jfcAttr->jfc, URMA_JFC_CQE_BASE_ADDR, (void *)&jfcAttr->cqeBaseAddrVa, sizeof(uint64_t));
103 0 : CHK_PRT_RETURN(ret != 0,
104 : hccp_err("rs_urma_set_jfc_opt URMA_JFC_CQE_BASE_ADDR failed, ret:%d, errno:%d", ret, errno), -EOPENSRC);
105 :
106 0 : return 0;
107 : }
108 :
109 0 : STATIC int RsJfcResAddrMunmap(struct RsCtxJfcCb *jfcCb, struct UdmaVaInfo *vaInfo)
110 : {
111 0 : struct res_map_info_in resInfoIn = {0};
112 0 : int ret = 0;
113 :
114 0 : resInfoIn.res_id = RsGenerateMmapResId(jfcCb->jfcId, jfcCb->devCb->devAttr.ub.dieId,
115 0 : jfcCb->devCb->devAttr.ub.funcId);
116 0 : resInfoIn.target_proc_type = PROCESS_CP1;
117 0 : resInfoIn.res_type = (enum res_addr_type)vaInfo->resType;
118 0 : resInfoIn.priv_len = sizeof(struct UdmaVaInfo);
119 0 : resInfoIn.priv = (void *)vaInfo;
120 0 : ret = DlHalResAddrUnmapV2(jfcCb->devCb->rscb->logicId, &resInfoIn);
121 0 : CHK_PRT_RETURN(ret != 0,
122 : hccp_err("DlHalResAddrUnmapV2 failed, res_type:%d ret:%d, errno:%d", resInfoIn.res_type, ret, errno), ret);
123 :
124 0 : return ret;
125 : }
126 :
127 0 : STATIC int RsJfcResAddrMmap(struct RsCtxJfcCb *jfcCb, struct UdmaVaInfo *vaInfo, struct res_map_info_out *resInfoOut)
128 : {
129 0 : struct res_map_info_in resInfoIn = {0};
130 0 : int ret = 0;
131 :
132 0 : resInfoIn.res_id = RsGenerateMmapResId(jfcCb->jfcId, jfcCb->devCb->devAttr.ub.dieId,
133 0 : jfcCb->devCb->devAttr.ub.funcId);
134 0 : resInfoIn.target_proc_type = PROCESS_CP1;
135 0 : resInfoIn.res_type = (enum res_addr_type)vaInfo->resType;
136 0 : resInfoIn.priv_len = sizeof(struct UdmaVaInfo);
137 0 : resInfoIn.priv = (void *)vaInfo;
138 0 : ret = DlHalResAddrMapV2(jfcCb->devCb->rscb->logicId, &resInfoIn, resInfoOut);
139 0 : CHK_PRT_RETURN(ret != 0,
140 : hccp_err("DlHalResAddrMapV2 failed, res_type:%d ret:%d, errno:%d", resInfoIn.res_type, ret, errno), ret);
141 :
142 0 : return ret;
143 : }
144 :
145 0 : STATIC void RsMunmapJfcVa(struct RsCtxJfcCb *jfcCb)
146 : {
147 0 : struct UdmaVaInfo vaInfo = {0};
148 :
149 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
150 0 : return;
151 : }
152 :
153 0 : vaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_JFC;
154 0 : vaInfo.va = jfcCb->bufAddr;
155 0 : vaInfo.len = WQE_BB_SIZE * jfcCb->depth;
156 0 : vaInfo.pid = getpid();
157 0 : (void)RsJfcResAddrMunmap(jfcCb, &vaInfo);
158 :
159 0 : vaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_DB;
160 0 : vaInfo.va = jfcCb->swdbAddr;
161 0 : vaInfo.len = sizeof(uint64_t);
162 0 : vaInfo.pid = getpid();
163 0 : (void)RsJfcResAddrMunmap(jfcCb, &vaInfo);
164 : }
165 :
166 0 : STATIC int RsMmapJfcVa(struct RsCtxJfcCb *jfcCb)
167 : {
168 0 : struct res_map_info_out resInfoOut = {0};
169 0 : struct UdmaVaInfo jfcVaInfo = {0};
170 0 : struct UdmaVaInfo dbVaInfo = {0};
171 0 : int retTmp = 0;
172 0 : int ret = 0;
173 :
174 0 : jfcVaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_JFC;
175 0 : jfcVaInfo.va = jfcCb->bufAddr;
176 0 : jfcVaInfo.len = WQE_BB_SIZE * jfcCb->depth;
177 0 : jfcVaInfo.pid = getpid();
178 0 : ret = RsJfcResAddrMmap(jfcCb, &jfcVaInfo, &resInfoOut);
179 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_jfc_res_addr_mmap failed, res_type:%u ret:%d", jfcVaInfo.resType, ret), ret);
180 0 : jfcCb->bufAddr = resInfoOut.va;
181 :
182 0 : dbVaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_DB;
183 0 : dbVaInfo.va = jfcCb->swdbAddr;
184 0 : dbVaInfo.len = sizeof(uint64_t);
185 0 : dbVaInfo.pid = getpid();
186 0 : ret = RsJfcResAddrMmap(jfcCb, &dbVaInfo, &resInfoOut);
187 0 : if (ret != 0) {
188 0 : hccp_err("rs_jfc_res_addr_mmap failed, res_type:%u ret:%d", dbVaInfo.resType, ret);
189 0 : goto munmap_jfc_buff_va;
190 : }
191 :
192 0 : jfcCb->swdbAddr = resInfoOut.va;
193 0 : return ret;
194 :
195 0 : munmap_jfc_buff_va:
196 0 : jfcVaInfo.va = jfcCb->bufAddr;
197 0 : retTmp = RsJfcResAddrMunmap(jfcCb, &jfcVaInfo);
198 0 : CHK_PRT_RETURN(retTmp != 0,
199 : hccp_err("rs_jfc_res_addr_munmap failed, res_type:%u ret:%d", jfcVaInfo.resType, retTmp), retTmp);
200 0 : return ret;
201 : }
202 :
203 0 : STATIC int RsGetJfcOpt(struct RsCtxJfcCb *jfcCb, urma_jfc_t *jfc)
204 : {
205 0 : uint64_t cqBuffVa = 0, dbVa = 0;
206 0 : int ret = 0;
207 :
208 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
209 0 : return ret;
210 : }
211 :
212 0 : ret = RsUrmaGetJfcOpt(jfc, URMA_JFC_CQE_BASE_ADDR, &cqBuffVa, sizeof(uint64_t));
213 0 : CHK_PRT_RETURN(ret != 0,
214 : hccp_err("rs_urma_get_jfc_opt URMA_JFC_CQE_BASE_ADDR failed, ret:%d, errno:%d", ret, errno), -EOPENSRC);
215 :
216 0 : ret = RsUrmaGetJfcOpt(jfc, URMA_JFC_DB_ADDR, &dbVa, sizeof(uint64_t));
217 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_urma_get_jfc_opt URMA_JFC_DB_ADDR failed, ret:%d, errno:%d", ret, errno),
218 : -EOPENSRC);
219 :
220 0 : jfcCb->bufAddr = cqBuffVa;
221 0 : jfcCb->swdbAddr = dbVa;
222 :
223 0 : ret = RsMmapJfcVa(jfcCb);
224 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_mmap_jfc_va failed, ret:%d", ret), ret);
225 :
226 0 : return ret;
227 : }
228 :
229 0 : int RsUbCtxJfcCreateExt(struct RsCtxJfcCb *jfcCb, urma_jfc_cfg_t *jfcCfg, urma_jfc_t **jfc)
230 : {
231 0 : struct ExtJfcAttr jfcAttr = {0};
232 0 : int ret = 0;
233 :
234 0 : ret = RsInitJfcAttr(jfcCb, jfcCfg, &jfcAttr);
235 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_init_jfc_attr failed, ret:%d", ret), ret);
236 :
237 0 : ret = RsSetJfcOpt(jfcCb, &jfcAttr);
238 0 : if (ret != 0) {
239 0 : hccp_err("rs_set_jfc_attr failed, ret:%d", ret);
240 0 : goto deinit_attr;
241 : }
242 :
243 0 : ret = RsUrmaActiveJfc(jfcAttr.jfc);
244 0 : if (ret != 0) {
245 0 : hccp_err("rs_urma_active_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcAttr.jfc->jfc_id.id, ret, errno);
246 0 : ret = -EOPENSRC;
247 0 : goto deinit_attr;
248 : }
249 0 : jfcCb->jfcId = jfcAttr.jfc->jfc_id.id;
250 :
251 0 : ret = RsGetJfcOpt(jfcCb, jfcAttr.jfc);
252 0 : if (ret != 0) {
253 0 : hccp_err("rs_get_jfc_opt failed, jfcId:%u, ret:%d, errno:%d", jfcAttr.jfc->jfc_id.id, ret, errno);
254 0 : goto deactive_jfc;
255 : }
256 :
257 0 : *jfc = jfcAttr.jfc;
258 0 : return 0;
259 :
260 0 : deactive_jfc:
261 0 : (void)RsUrmaDeactiveJfc(jfcAttr.jfc);
262 0 : deinit_attr:
263 0 : (void)RsDeinitJfcAttr(jfcCb, jfcCfg, &jfcAttr);
264 0 : *jfc = NULL;
265 0 : return ret;
266 : }
267 :
268 0 : int RsUbDeleteJfcExt(struct RsUbDevCb *devCb, struct RsCtxJfcCb *jfcCb)
269 : {
270 0 : urma_jfc_t *jfc = (urma_jfc_t *)(uintptr_t)(jfcCb->jfcAddr);
271 0 : unsigned int jfcId = jfc->jfc_id.id;
272 0 : int ret = 0;
273 :
274 0 : RsMunmapJfcVa(jfcCb);
275 :
276 0 : ret = RsUrmaDeactiveJfc(jfc);
277 0 : if (ret != 0) {
278 0 : hccp_err("rs_urma_deactive_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcId, ret, errno);
279 0 : ret = -EOPENSRC;
280 : }
281 :
282 0 : ret = RsUrmaFreeJfc(jfc);
283 0 : if (ret != 0) {
284 0 : hccp_err("rs_urma_free_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcId, ret, errno);
285 0 : ret = -EOPENSRC;
286 : }
287 :
288 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
289 0 : ret = RsNetFreeJfcId(devCb->urmaDev->name, jfcCb->jfcType, jfcId);
290 0 : if (ret != 0) {
291 0 : hccp_err("rs_net_free_jfc_id failed, jfcId:%u, ret:%d", jfcId, ret);
292 : }
293 : }
294 :
295 0 : return ret;
296 : }
|