Repository navigation
Expand file tree
/
Copy pathwd_udma.c
More file actions
535 lines (448 loc) · 12.7 KB
/
Copy pathwd_udma.c
File metadata and controls
535 lines (448 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// SPDX-License-Identifier: Apache-2.0
/*
* Copyright 2025 Huawei Technologies Co.,Ltd. All rights reserved.
*/
#include <stdlib.h>
#include <pthread.h>
#include <limits.h>
#include "include/drv/wd_udma_drv.h"
#include "wd_udma.h"
struct wd_udma_sess {
const char *alg_name;
wd_dev_mask_t *dev_mask;
void *priv;
void *sched_key;
};
static struct wd_udma_setting {
enum wd_status status;
struct wd_ctx_config_internal config;
struct wd_sched sched;
struct wd_async_msg_pool pool;
void *dlhandle;
void *dlh_list;
} wd_udma_setting;
static struct wd_init_attrs wd_udma_init_attrs;
static void wd_udma_close_driver(void)
{
#ifndef WD_STATIC_DRV
wd_dlclose_drv(wd_udma_setting.dlh_list);
wd_udma_setting.dlh_list = NULL;
#else
hisi_udma_remove();
#endif
}
static int wd_udma_open_driver(void)
{
#ifndef WD_STATIC_DRV
/*
* Driver lib file path could set by env param.
* then open tham by wd_dlopen_drv()
* use NULL means dynamic query path
*/
wd_udma_setting.dlh_list = wd_dlopen_drv(NULL);
if (!wd_udma_setting.dlh_list) {
WD_ERR("fail to open driver lib files.\n");
return -WD_EINVAL;
}
#else
hisi_udma_probe();
#endif
return WD_SUCCESS;
}
void wd_udma_free_sess(handle_t sess)
{
struct wd_udma_sess *sess_t = (struct wd_udma_sess *)sess;
if (!sess_t) {
WD_ERR("invalid: free udma sess param NULL!\n");
return;
}
if (sess_t->sched_key) {
if (wd_udma_setting.sched.sched_uninit)
wd_udma_setting.sched.sched_uninit(
wd_udma_setting.sched.h_sched_ctx,
(handle_t)sess_t->sched_key);
else
free(sess_t->sched_key);
}
free(sess_t);
}
handle_t wd_udma_alloc_sess(struct wd_udma_sess_setup *setup)
{
struct wd_sched_params params = {0};
struct wd_udma_sess *sess;
int ret;
if (!setup) {
WD_ERR("invalid: alloc udma sess setup NULL!\n");
return (handle_t)0;
}
sess = calloc(1, sizeof(struct wd_udma_sess));
if (!sess)
return (handle_t)0;
sess->alg_name = "udma";
ret = wd_drv_alg_support(sess->alg_name, &wd_udma_setting.config);
if (!ret) {
WD_ERR("failed to support udma algorithm: %s!\n", sess->alg_name);
goto free_sess;
}
/* Some simple scheduler don't need scheduling parameters */
sess->sched_key = (void *)wd_udma_setting.sched.sched_init(
wd_udma_setting.sched.h_sched_ctx, setup->sched_param);
if (WD_IS_ERR(sess->sched_key)) {
WD_ERR("failed to init session schedule key!\n");
goto free_sess;
}
params.alg_name = sess->alg_name;
params.ctxs = wd_udma_setting.config.ctxs;
wd_udma_setting.sched.set_param(
wd_udma_setting.sched.h_sched_ctx,
sess->sched_key, ¶ms);
return (handle_t)sess;
free_sess:
free(sess);
return (handle_t)0;
}
static int wd_udma_addr_check(struct wd_data_addr *data_addr)
{
if (unlikely(!data_addr->addr)) {
WD_ERR("invalid: udma addr is NULL!\n");
return -WD_EINVAL;
}
if (unlikely(!data_addr->data_size ||
data_addr->data_size > data_addr->addr_size)) {
WD_ERR("invalid: udma size is error, data_size %lu, addr_size is %lu!\n",
data_addr->data_size, data_addr->addr_size);
return -WD_EINVAL;
}
return WD_SUCCESS;
}
static int wd_udma_param_check(struct wd_udma_sess *sess,
struct wd_udma_req *req)
{
struct wd_data_addr *src, *dst;
int i, ret;
if (unlikely(!sess || !req)) {
WD_ERR("invalid: input param NULL!\n");
return -WD_EINVAL;
}
if (unlikely(req->addr_num <= 0)) {
WD_ERR("invalid: addr num is error %d!\n", req->addr_num);
return -WD_EINVAL;
}
src = req->src;
dst = req->dst;
if (unlikely(req->op_type >= WD_UDMA_OP_MAX)) {
WD_ERR("invalid: op_type is error %u!\n", req->op_type);
return -WD_EINVAL;
} else if (unlikely(req->op_type == WD_UDMA_MEMCPY && (!src || !dst))) {
WD_ERR("invalid: memcpy src or dst is NULL!\n");
return -WD_EINVAL;
} else if (unlikely(req->op_type == WD_UDMA_MEMSET &&
((!src && !dst) || (src && dst)))) {
WD_ERR("invalid: memset src and dst is error!\n");
return -WD_EINVAL;
}
if (req->op_type == WD_UDMA_MEMSET)
dst = !req->src ? req->dst : req->src;
for (i = 0; i < req->addr_num; i++) {
if (req->op_type == WD_UDMA_MEMCPY) {
ret = wd_udma_addr_check(&src[i]);
if (unlikely(ret)) {
WD_ERR("invalid: udma memcpy src addr is error!\n");
return -WD_EINVAL;
}
ret = wd_udma_addr_check(&dst[i]);
if (unlikely(ret)) {
WD_ERR("invalid: udma memcpy dst addr is error!\n");
return -WD_EINVAL;
}
if (unlikely(dst[i].data_size != src[i].data_size)) {
WD_ERR("invalid: udma memcpy data_size is error!\n"
"src %lu, dst %lu!\n",
dst[i].data_size, src[i].data_size);
return -WD_EINVAL;
}
} else {
ret = wd_udma_addr_check(&dst[i]);
if (unlikely(ret)) {
WD_ERR("invalid: udma memset addr is error!\n");
return -WD_EINVAL;
}
}
}
return WD_SUCCESS;
}
static void fill_udma_msg(struct wd_udma_msg *msg, struct wd_udma_req *req)
{
msg->result = WD_EINVAL;
memcpy(&msg->req, req, sizeof(*req));
msg->op_type = req->op_type;
msg->addr_num = req->addr_num;
msg->value = req->value;
if (req->op_type == WD_UDMA_MEMSET) {
msg->dst = !req->src ? req->dst : req->src;
} else {
msg->src = req->src;
msg->dst = req->dst;
}
}
int wd_do_udma_sync(handle_t h_sess, struct wd_udma_req *req)
{
struct wd_ctx_config_internal *config = &wd_udma_setting.config;
handle_t h_sched_ctx = wd_udma_setting.sched.h_sched_ctx;
struct wd_udma_sess *sess_t = (struct wd_udma_sess *)h_sess;
struct wd_msg_handle msg_handle;
struct wd_ctx_internal *ctx;
struct wd_udma_msg msg = {0};
__u32 idx;
int ret;
ret = wd_udma_param_check(sess_t, req);
if (unlikely(ret))
return ret;
idx = wd_udma_setting.sched.pick_next_ctx(h_sched_ctx,
sess_t->sched_key,
CTX_MODE_SYNC);
ret = wd_check_ctx(config, CTX_MODE_SYNC, idx);
if (unlikely(ret))
return ret;
wd_dfx_msg_cnt(config, WD_CTX_CNT_NUM, idx);
ctx = config->ctxs + idx;
fill_udma_msg(&msg, req);
msg_handle.send = ctx->drv->send;
msg_handle.recv = ctx->drv->recv;
pthread_spin_lock(&ctx->lock);
ret = wd_handle_msg_sync(&msg_handle, ctx->ctx,
&msg, NULL, wd_udma_setting.config.epoll_en);
pthread_spin_unlock(&ctx->lock);
if (unlikely(ret))
return ret;
req->status = msg.result;
return GET_NEGATIVE(msg.result);
}
int wd_do_udma_async(handle_t sess, struct wd_udma_req *req)
{
struct wd_ctx_config_internal *config = &wd_udma_setting.config;
handle_t h_sched_ctx = wd_udma_setting.sched.h_sched_ctx;
struct wd_udma_sess *sess_t = (struct wd_udma_sess *)sess;
struct wd_udma_msg *msg = NULL;
struct wd_ctx_internal *ctx;
int ret, mid;
__u32 idx;
ret = wd_udma_param_check(sess_t, req);
if (unlikely(ret))
return ret;
if (unlikely(!req->cb)) {
WD_ERR("invalid: udma input req cb is NULL!\n");
return -WD_EINVAL;
}
idx = wd_udma_setting.sched.pick_next_ctx(h_sched_ctx,
sess_t->sched_key,
CTX_MODE_ASYNC);
ret = wd_check_ctx(config, CTX_MODE_ASYNC, idx);
if (unlikely(ret))
return ret;
ctx = config->ctxs + idx;
mid = wd_get_msg_from_pool(&wd_udma_setting.pool, idx, (void **)&msg);
if (unlikely(mid < 0)) {
WD_ERR("failed to get msg from pool!\n");
return mid;
}
fill_udma_msg(msg, req);
msg->tag = mid;
ret = ctx->drv->send(ctx->ctx, msg);
if (unlikely(ret)) {
if (ret != -WD_EBUSY)
WD_ERR("failed to send udma BD, hw is err!\n");
goto fail_with_msg;
}
wd_dfx_msg_cnt(config, WD_CTX_CNT_NUM, idx);
return WD_SUCCESS;
fail_with_msg:
wd_put_msg_to_pool(&wd_udma_setting.pool, idx, mid);
return ret;
}
static int wd_udma_poll_ctx(__u32 idx, __u32 expt, __u32 *count)
{
struct wd_ctx_config_internal *config = &wd_udma_setting.config;
struct wd_udma_msg rcv_msg = {0};
struct wd_ctx_internal *ctx;
struct wd_udma_req *req;
struct wd_udma_msg *msg;
__u32 rcv_cnt = 0;
__u32 tmp = expt;
int ret;
*count = 0;
ret = wd_check_ctx(config, CTX_MODE_ASYNC, idx);
if (ret)
return ret;
ctx = config->ctxs + idx;
do {
ret = ctx->drv->recv(ctx->ctx, &rcv_msg);
if (ret == -WD_EAGAIN) {
return ret;
} else if (unlikely(ret)) {
WD_ERR("failed to async recv, ret = %d!\n", ret);
*count = rcv_cnt;
return ret;
}
rcv_cnt++;
msg = wd_find_msg_in_pool(&wd_udma_setting.pool, idx, rcv_msg.tag);
if (!msg) {
WD_ERR("failed to find udma msg!\n");
return -WD_EINVAL;
}
msg->req.status = rcv_msg.result;
req = &msg->req;
req->cb(req);
wd_put_msg_to_pool(&wd_udma_setting.pool, idx, rcv_msg.tag);
*count = rcv_cnt;
} while (--tmp);
return ret;
}
int wd_udma_poll(__u32 expt, __u32 *count)
{
handle_t h_sched_ctx = wd_udma_setting.sched.h_sched_ctx;
if (unlikely(!count || !expt)) {
WD_ERR("invalid: udma poll count is NULL or expt is 0!\n");
return -WD_EINVAL;
}
return wd_udma_setting.sched.poll_policy(h_sched_ctx, expt, count);
}
static bool wd_udma_atfork_registered = false;
static void wd_udma_clear_status(void)
{
wd_alg_clear_init(&wd_udma_setting.status);
}
static void wd_udma_alg_uninit(void)
{
/* Uninit async request pool */
wd_uninit_async_request_pool(&wd_udma_setting.pool);
/* Unset config, sched, driver */
wd_clear_sched(&wd_udma_setting.sched);
}
void wd_udma_uninit(void)
{
enum wd_status status;
wd_alg_get_init(&wd_udma_setting.status, &status);
if (status != WD_INIT)
return;
wd_alg_uninit_driver(&wd_udma_setting.config);
wd_ctx_unbind_drivers(&wd_udma_setting.config);
wd_udma_setting.config.drv_array = NULL;
wd_udma_setting.config.drv_count = 0;
wd_udma_alg_uninit();
wd_alg_attrs_uninit(&wd_udma_init_attrs);
wd_udma_close_driver();
wd_alg_clear_init(&wd_udma_setting.status);
}
static int wd_udma_alg_init(struct wd_ctx_config *config, struct wd_sched *sched)
{
int ret;
ret = wd_set_epoll_en("WD_UDMA_EPOLL_EN", &wd_udma_setting.config.epoll_en);
if (ret < 0)
return ret;
wd_udma_setting.config.alg_name = "udma";
ret = wd_init_ctx_config(&wd_udma_setting.config, config);
if (ret < 0)
return ret;
ret = wd_init_sched(&wd_udma_setting.sched, sched);
if (ret < 0)
goto out_clear_ctx_config;
/* Allocate async pool for every ctx */
ret = wd_init_async_request_pool(&wd_udma_setting.pool, config, WD_POOL_MAX_ENTRIES,
sizeof(struct wd_udma_msg));
if (ret < 0)
goto out_clear_sched;
return WD_SUCCESS;
out_clear_sched:
wd_clear_sched(&wd_udma_setting.sched);
out_clear_ctx_config:
wd_clear_ctx_config(&wd_udma_setting.config);
return ret;
}
int wd_udma_init(const char *alg, __u32 sched_type, int task_type,
struct wd_ctx_params *ctx_params)
{
struct wd_ctx_nums udma_ctx_num[WD_UDMA_OP_MAX] = {0};
struct wd_ctx_params udma_ctx_params = {0};
int state, ret = -WD_EINVAL;
int try_cnt = 0;
if (!wd_udma_atfork_registered) {
if (pthread_atfork(NULL, NULL, wd_udma_clear_status) == 0)
wd_udma_atfork_registered = true;
}
state = wd_alg_try_init(&wd_udma_setting.status);
if (state)
return state;
if (!alg || sched_type >= SCHED_POLICY_BUTT ||
task_type < 0 || task_type >= TASK_MAX_TYPE) {
WD_ERR("invalid: input param is wrong!\n");
goto out_clear_init;
}
if (strcmp(alg, "udma")) {
WD_ERR("invalid: the alg %s not support!\n", alg);
goto out_clear_init;
}
state = wd_udma_open_driver();
if (state)
goto out_clear_init;
while (ret) {
if (try_cnt++ >= WD_INIT2_MAX_RETRY) {
WD_ERR("failed to init2 after %d retries.\n",
WD_INIT2_MAX_RETRY);
goto out_driver;
}
memset(&wd_udma_setting.config, 0, sizeof(struct wd_ctx_config_internal));
udma_ctx_params.ctx_set_num = udma_ctx_num;
ret = wd_ctx_param_init(&udma_ctx_params, ctx_params,
alg, WD_UDMA_TYPE, WD_UDMA_OP_MAX);
if (ret) {
if (ret == -WD_EAGAIN)
continue;
goto out_driver;
}
(void)strcpy(wd_udma_init_attrs.alg, alg);
wd_udma_init_attrs.sched_type = sched_type;
wd_udma_init_attrs.task_type = task_type;
wd_udma_init_attrs.ctx_params = &udma_ctx_params;
wd_udma_init_attrs.alg_init = wd_udma_alg_init;
wd_udma_init_attrs.alg_poll_ctx = wd_udma_poll_ctx;
ret = wd_alg_attrs_init(&wd_udma_init_attrs);
if (ret) {
if (ret == -WD_ENODEV) {
wd_ctx_param_uninit(&udma_ctx_params);
continue;
}
WD_ERR("failed to init alg attrs!\n");
goto out_params_uninit;
}
}
ret = wd_ctx_bind_drivers(&wd_udma_setting.config,
wd_udma_init_attrs.ctx_config_internal,
WD_TYPE_V2);
if (ret) {
WD_ERR("failed to bind driver for udma!\n");
goto out_common_uninit;
}
ret = wd_alg_init_driver(&wd_udma_setting.config);
if (ret)
goto out_unbind_drivers;
wd_alg_set_init(&wd_udma_setting.status);
wd_ctx_param_uninit(&udma_ctx_params);
return WD_SUCCESS;
out_unbind_drivers:
wd_ctx_unbind_drivers(&wd_udma_setting.config);
out_common_uninit:
wd_udma_alg_uninit();
wd_alg_attrs_uninit(&wd_udma_init_attrs);
out_params_uninit:
wd_ctx_param_uninit(&udma_ctx_params);
out_driver:
wd_udma_close_driver();
out_clear_init:
wd_alg_clear_init(&wd_udma_setting.status);
return ret;
}
struct wd_udma_msg *wd_udma_get_msg(__u32 idx, __u32 tag)
{
return wd_find_msg_in_pool(&wd_udma_setting.pool, idx, tag);
}