Repository navigation
Expand file tree
/
Copy pathwd_bmm.c
More file actions
1227 lines (1023 loc) · 28.7 KB
/
Copy pathwd_bmm.c
File metadata and controls
1227 lines (1023 loc) · 28.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: Apache-2.0 */
/*
* Copyright 2025 Huawei Technologies Co.,Ltd. All rights reserved.
*/
/* Block Memory Management (lib): Adapted for SVA mode */
#define _GNU_SOURCE
#include <dirent.h>
#include <numa.h>
#include <sched.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/queue.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "wd_internal.h"
#include "wd_bmm.h"
#include "uacce.h"
#include "wd.h"
#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define UACCE_DEV_IOMMU (1<<7)
#define TAG_FREE 0x12345678 /* block is free */
#define TAG_USED 0x87654321 /* block is busy */
#define MAX_ALIGN_SIZE 0x1000 /* 4KB */
#define MAX_BLOCK_SIZE 0x10000000 /* 256MB */
#define BLK_BALANCE_SZ 0x100000ul
#define NUM_TIMES(x) (87 * (x) / 100)
#define BYTE_SIZE 8
#define BIT_SHIFT 3
struct wd_ss_region {
unsigned long long pa;
void *va;
size_t size;
TAILQ_ENTRY(wd_ss_region) next;
};
TAILQ_HEAD(wd_ss_region_list, wd_ss_region);
struct ctx_info {
int fd;
int iommu_type;
void *ss_va;
size_t ss_mm_size;
struct wd_ss_region_list ss_list;
struct wd_ss_region_list *head;
unsigned long qfrs_offset[UACCE_QFRT_MAX];
};
struct wd_blk_hd {
unsigned int blk_tag;
unsigned int blk_num;
void *blk_dma;
void *blk;
};
struct wd_blkpool {
pthread_spinlock_t pool_lock;
unsigned int free_blk_num;
unsigned int alloc_failures;
struct ctx_info *cinfo;
struct wd_blk_hd *blk_array; // memory blk array
unsigned int total_blocks; // total blk numbers
unsigned char *free_bitmap; // free blk bitmap, 0 mean unused
unsigned int bitmap_size; // bitmap's memory size
void *usr_mem_start;
void *act_start;
unsigned int act_hd_sz;
unsigned int act_blk_sz;
unsigned long act_mem_sz;
unsigned int dev_id;
struct wd_mempool_setup setup;
/* SVA mode for Hugepage */
bool sva_mode;
handle_t hp_mempool;
handle_t hp_blkpool;
};
struct mem_ctx_node {
char alg_name[CRYPTO_MAX_ALG_NAME];
handle_t h_ctx;
int numa_id;
bool used;
TAILQ_ENTRY(mem_ctx_node) list_node;
};
static TAILQ_HEAD(, mem_ctx_node) g_mem_ctx_list = TAILQ_HEAD_INITIALIZER(g_mem_ctx_list);
static pthread_mutex_t g_mem_ctx_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* Fork child handler for the global ctx pool.
*
* After fork() the child is single-threaded; the inherited mutex state is
* unreliable (the holding thread, if any, does not exist in the child).
* POSIX allows reinitializing a statically-initialized mutex in the child
* fork handler, so do that unconditionally and skip lock/unlock below.
*
* Discard all inherited ctx nodes. Each node's h_ctx points to a wd_ctx_h
* whose fd and mmap regions are shared with the parent (fd via dup,
* MAP_SHARED mmap via shared physical pages). Using these ctx in the child
* would cause parent/child to contend on the same hardware queue. Free only
* the node shells (COW copies, safe to free in the child). Do NOT call
* wd_release_ctx: closing fd or munmapping regions in the child could
* interfere with the parent's shared resources. The kernel reclaims child
* fds and VMAs on exit.
*/
static void wd_bmm_atfork_child(void)
{
struct mem_ctx_node *node;
pthread_mutex_init(&g_mem_ctx_mutex, NULL);
while ((node = TAILQ_FIRST(&g_mem_ctx_list)) != NULL) {
TAILQ_REMOVE(&g_mem_ctx_list, node, list_node);
free(node);
}
TAILQ_INIT(&g_mem_ctx_list);
}
/*
* Register the atfork handler once when libwd is loaded, before any user
* code (including fork) runs. The constructor attribute ensures single
* registration regardless of how many alg init calls happen later.
*/
static void __attribute__((constructor)) wd_bmm_ctor(void)
{
pthread_atfork(NULL, NULL, wd_bmm_atfork_child);
}
handle_t wd_find_ctx(const char *alg_name)
{
struct mem_ctx_node *close_node = NULL;
struct mem_ctx_node *node;
int min_distance = 0xFFFF;
handle_t h_ctx = 0;
unsigned int nid;
int numa_dis;
if (!alg_name) {
WD_ERR("Invalid: alg_name is NULL!\n");
return 0;
}
/* Under default conditions in a VM, the node value is 0 */
if (getcpu(NULL, &nid) || nid == (unsigned int)NUMA_NO_NODE) {
WD_ERR("invalid: failed to get numa node for memory pool!\n");
return 0;
}
pthread_mutex_lock(&g_mem_ctx_mutex);
TAILQ_FOREACH(node, &g_mem_ctx_list, list_node) {
if (node->used == false && strstr(node->alg_name, alg_name)) {
if (node->numa_id == (int)nid) {
h_ctx = node->h_ctx;
node->used = true;
break;
}
/* Query the queue with the shortest NUMA distance */
numa_dis = numa_distance((int)nid, node->numa_id);
if (numa_dis < min_distance) {
min_distance = numa_dis;
close_node = node;
}
}
}
/* If no ctx matching the NUMA ID, use the shortest distance instead ctx */
if (!h_ctx && close_node) {
h_ctx = close_node->h_ctx;
close_node->used = true;
}
pthread_mutex_unlock(&g_mem_ctx_mutex);
if (!h_ctx)
WD_ERR("Failed to find mem ctx for alg: %s\n", alg_name);
return h_ctx;
}
void wd_remove_ctx_list(handle_t h_ctx)
{
struct mem_ctx_node *node;
if (!h_ctx)
return;
pthread_mutex_lock(&g_mem_ctx_mutex);
TAILQ_FOREACH(node, &g_mem_ctx_list, list_node) {
if (node->h_ctx == h_ctx) {
TAILQ_REMOVE(&g_mem_ctx_list, node, list_node);
free(node);
break;
}
}
pthread_mutex_unlock(&g_mem_ctx_mutex);
}
static inline bool wd_ctx_is_hw(handle_t h_ctx)
{
return h_ctx && (*(__u8 *)h_ctx == UADK_ALG_HW);
}
int wd_insert_ctx_list(handle_t h_ctx, char *alg_name)
{
struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
struct mem_ctx_node *new_node;
int numa_id;
if (!alg_name || !h_ctx) {
WD_ERR("Invalid: input params is NULL!\n");
return -WD_EINVAL;
}
/* A simple and efficient method to check the queue type */
if (!wd_ctx_is_hw(h_ctx)) {
WD_INFO("Notes: this ctx not HW ctx.\n");
return 0;
}
numa_id = ctx->dev->numa_id;
new_node = malloc(sizeof(struct mem_ctx_node));
if (new_node) {
pthread_mutex_lock(&g_mem_ctx_mutex);
strncpy(new_node->alg_name, alg_name, CRYPTO_MAX_ALG_NAME - 1);
new_node->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
new_node->numa_id = numa_id;
new_node->h_ctx = h_ctx;
new_node->used = false;
TAILQ_INSERT_TAIL(&g_mem_ctx_list, new_node, list_node);
pthread_mutex_unlock(&g_mem_ctx_mutex);
return 0;
}
return -WD_ENOMEM;
}
static void wd_free_slice(struct ctx_info *cinfo)
{
struct wd_ss_region *rgn;
while (true) {
rgn = TAILQ_FIRST(&cinfo->ss_list);
if (!rgn)
break;
TAILQ_REMOVE(&cinfo->ss_list, rgn, next);
free(rgn);
}
}
static void wd_add_slice(struct ctx_info *cinfo, struct wd_ss_region *rgn)
{
struct wd_ss_region *rg;
rg = TAILQ_LAST(&cinfo->ss_list, wd_ss_region_list);
if (rg) {
if (rg->pa + rg->size == rgn->pa) {
rg->size += rgn->size;
free(rgn);
return;
}
}
TAILQ_INSERT_TAIL(&cinfo->ss_list, rgn, next);
}
static void wd_show_ss_slices(struct ctx_info *cinfo)
{
struct wd_ss_region *rgn;
int i = 0;
TAILQ_FOREACH(rgn, cinfo->head, next) {
WD_ERR("slice-%d:size = 0x%lx\n", i, rgn->size);
i++;
}
}
static void bitmap_set_bit(unsigned char *bitmap, unsigned int bit_index)
{
if (!bitmap)
return;
bitmap[bit_index >> BIT_SHIFT] |= (1 << (bit_index % BYTE_SIZE));
}
static void bitmap_clear_bit(unsigned char *bitmap, unsigned int bit_index)
{
if (!bitmap)
return;
bitmap[bit_index >> BIT_SHIFT] &= ~(1 << (bit_index % BYTE_SIZE));
}
static bool bitmap_test_bit(const unsigned char *bitmap, unsigned int bit_index)
{
if (!bitmap)
return false;
/* bit is 1, it indicates that the block has already been used and is not free */
if ((bitmap[bit_index >> BIT_SHIFT] >> (bit_index % BYTE_SIZE)) & 0x1)
return false;
return true;
}
static int wd_parse_dev_id(char *dev_name)
{
char *last_dash;
char *endptr;
int dev_id;
if (!dev_name)
return -WD_EINVAL;
/* Find the last '-' in the string. */
last_dash = strrchr(dev_name, '-');
if (!last_dash || *(last_dash + 1) == '\0')
return -WD_EINVAL;
/* Parse the following number */
dev_id = strtol(last_dash + 1, &endptr, DECIMAL_NUMBER);
/* Check whether it is truly all digits */
if (*endptr != '\0' || dev_id < 0)
return -WD_EINVAL;
return dev_id;
}
/*----------------------------------SVA Hugepage memory pool---------------------------------*/
static void *wd_hugepage_pool_create(handle_t h_ctx, struct wd_mempool_setup *setup)
{
struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
struct wd_blkpool *pool = NULL;
size_t total_size;
int numa_id, ret;
pool = calloc(1, sizeof(*pool));
if (!pool) {
WD_ERR("failed to malloc pool.\n");
return NULL;
}
pool->sva_mode = true;
memcpy(&pool->setup, setup, sizeof(pool->setup));
total_size = setup->block_size * setup->block_num;
numa_id = ctx->dev->numa_id;
ret = wd_parse_dev_id(ctx->dev_path);
if (ret < 0) {
WD_ERR("failed to parse device id.\n");
goto error;
}
pool->dev_id = ret;
/* Create hugepage memory pool */
pool->hp_mempool = wd_mempool_create(total_size, numa_id);
if (WD_IS_ERR(pool->hp_mempool)) {
WD_ERR("failed to create hugepage mempool.\n");
goto error;
}
/* Create memory blocks */
pool->hp_blkpool = wd_blockpool_create(pool->hp_mempool,
setup->block_size,
setup->block_num);
if (WD_IS_ERR(pool->hp_blkpool)) {
WD_ERR("failed to create hugepage blockpool.\n");
wd_mempool_destroy(pool->hp_mempool);
goto error;
}
pool->free_blk_num = setup->block_num;
pool->act_blk_sz = setup->block_size;
return pool;
error:
free(pool);
return NULL;
}
static void wd_hugepage_pool_destroy(struct wd_blkpool *p)
{
if (p->hp_blkpool) {
wd_blockpool_destroy(p->hp_blkpool);
p->hp_blkpool = 0;
}
if (p->hp_mempool) {
wd_mempool_destroy(p->hp_mempool);
p->hp_mempool = 0;
}
free(p);
}
static void *wd_hugepage_blk_alloc(struct wd_blkpool *p, size_t size)
{
if (size > p->act_blk_sz) {
WD_ERR("request size %zu > block size %u\n", size, p->act_blk_sz);
return NULL;
}
void *addr = wd_block_alloc(p->hp_blkpool);
if (!addr) {
p->alloc_failures++;
WD_ERR("failed to alloc block from hugepage pool.\n");
return NULL;
}
__atomic_fetch_sub(&p->free_blk_num, 1, __ATOMIC_RELAXED);
return addr;
}
static void wd_hugepage_blk_free(struct wd_blkpool *p, void *buf)
{
/* The function call ensures that buf is not null */
wd_block_free(p->hp_blkpool, buf);
__atomic_fetch_add(&p->free_blk_num, 1, __ATOMIC_RELAXED);
}
/*----------------------------------No-SVA kernel memory pool--------------------------------*/
static void *wd_mmap_qfr(struct ctx_info *cinfo, enum uacce_qfrt qfrt, size_t size)
{
off_t off;
off = qfrt * getpagesize();
return mmap(0, size, PROT_READ | PROT_WRITE,
MAP_SHARED, cinfo->fd, off);
}
static void wd_unmap_reserve_mem(void *addr, size_t size)
{
int ret;
if (!addr)
return;
ret = munmap(addr, size);
if (ret)
WD_ERR("wd qfr unmap failed!\n");
}
static void *wd_map_reserve_mem(struct wd_blkpool *pool, size_t size)
{
struct ctx_info *cinfo = pool->cinfo;
struct wd_ss_region *rgn;
unsigned long info;
size_t tmp = size;
unsigned long i = 0;
void *ptr;
int ret = 1;
if (!cinfo) {
WD_ERR("ctx queue information is NULL!\n");
return NULL;
}
/* Make sure memory map granularity size align */
if (!cinfo->iommu_type)
tmp = ALIGN(tmp, UACCE_GRAN_SIZE);
ptr = wd_mmap_qfr(cinfo, UACCE_QFRT_SS, tmp);
if (ptr == MAP_FAILED) {
WD_ERR("wd drv mmap fail!(err = %d)\n", errno);
return NULL;
}
cinfo->ss_va = ptr;
cinfo->ss_mm_size = tmp;
tmp = 0;
while (ret > 0) {
info = i;
ret = ioctl(cinfo->fd, UACCE_CMD_GET_SS_DMA, &info);
if (ret < 0) {
wd_show_ss_slices(cinfo);
WD_ERR("get DMA fail!\n");
goto err_out;
}
rgn = malloc(sizeof(*rgn));
if (!rgn) {
WD_ERR("alloc ss region fail!\n");
goto err_out;
}
memset(rgn, 0, sizeof(*rgn));
if (cinfo->iommu_type)
rgn->size = cinfo->ss_mm_size;
else
rgn->size = (info & UACCE_GRAN_NUM_MASK) <<
UACCE_GRAN_SHIFT;
rgn->pa = info & (~UACCE_GRAN_NUM_MASK);
rgn->va = ptr + tmp;
tmp += rgn->size;
wd_add_slice(cinfo, rgn);
i++;
}
return ptr;
err_out:
wd_free_slice(cinfo);
wd_unmap_reserve_mem(cinfo->ss_va, cinfo->ss_mm_size);
return NULL;
}
static int wd_pool_params_check(struct wd_mempool_setup *setup)
{
if (!setup->block_num || !setup->block_size ||
setup->block_size > MAX_BLOCK_SIZE) {
WD_ERR("Invalid: block_size or block_num(%x, %u)!\n",
setup->block_size, setup->block_num);
return -WD_EINVAL;
}
/* Check parameters, and align_size must be 2^N */
if (setup->align_size <= 0x1 || setup->align_size > MAX_ALIGN_SIZE ||
(setup->align_size & (setup->align_size - 0x1))) {
WD_ERR("Invalid align_size.\n");
return -WD_EINVAL;
}
return WD_SUCCESS;
}
static int wd_ctx_info_init(struct wd_ctx_h *ctx, struct wd_blkpool *p)
{
struct ctx_info *cinfo;
cinfo = calloc(1, sizeof(struct ctx_info));
if (!cinfo) {
WD_ERR("failed to alloc ctx info memory.\n");
return -WD_ENOMEM;
}
cinfo->fd = ctx->fd;
cinfo->iommu_type = (unsigned int)ctx->dev->flags & UACCE_DEV_IOMMU;
cinfo->head = &cinfo->ss_list;
TAILQ_INIT(&cinfo->ss_list);
(void)memcpy(cinfo->qfrs_offset, ctx->qfrs_offs,
sizeof(cinfo->qfrs_offset));
p->cinfo = (void *)cinfo;
return 0;
}
static int wd_pool_pre_layout(handle_t h_ctx,
struct wd_blkpool *p,
struct wd_mempool_setup *sp)
{
struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
struct ctx_info *cinfo = NULL;
unsigned int asz;
int ret;
if (!ctx && !sp->ops.alloc) {
WD_ERR("ctx is NULL!\n");
return -WD_EINVAL;
}
if (!sp->ops.alloc) {
ret = wd_ctx_info_init(ctx, p);
if (ret) {
WD_ERR("failed to init ctx info.\n");
return ret;
}
cinfo = p->cinfo;
}
ret = wd_pool_params_check(sp);
if (ret) {
free(p->cinfo);
p->cinfo = NULL;
return ret;
}
asz = sp->align_size;
/* Get actual value by align */
p->act_hd_sz = ALIGN(sizeof(struct wd_blk_hd), asz);
p->act_blk_sz = ALIGN(sp->block_size, asz);
p->act_mem_sz = (p->act_hd_sz + p->act_blk_sz) *
(unsigned long)sp->block_num + asz;
/*
* When we use WD reserve memory and the blk_sz is larger than 1M,
* in order to ensure the mem_pool to be success,
* ensure that the allocated memory is an integer multiple of 1M.
*/
if (!sp->ops.alloc && (cinfo && !cinfo->iommu_type))
p->act_mem_sz = ((p->act_mem_sz + BLK_BALANCE_SZ - 1) & ~(BLK_BALANCE_SZ - 1)) << 1;
return WD_SUCCESS;
}
/**
* wd_iova_map - Map virtual address to physical address
* @cinfo: context information
* @va: virtual address to map
* @sz: size of the mapping (not used in current implementation)
*
* When IOMMU is enabled, the PA is actually an IOVA; userspace still sees it
* as consistent and contiguous with the VA.
* When IOMMU is disabled, the PA refers to the kernel's physical address, which
* must be physically contiguous to be allocated by the kernel.
* Therefore, the PA address can be obtained from the offset of the VA.
*
*/
static void *wd_iova_map(struct ctx_info *cinfo, void *va, size_t sz)
{
struct wd_ss_region *rgn;
unsigned long offset;
void *dma_addr;
if (!cinfo || !va) {
WD_ERR("wd iova map: parameter err!\n");
return NULL;
}
/* Search through all memory regions to find where va belongs */
TAILQ_FOREACH(rgn, cinfo->head, next) {
if (rgn->va <= va && va < rgn->va + rgn->size) {
/* Calculate offset within the region */
offset = (uintptr_t)va - (uintptr_t)rgn->va;
/* Add base physical address of the region */
dma_addr = (void *)((uintptr_t)rgn->pa + offset);
return dma_addr;
}
}
WD_ERR("wd iova map: va not found in any region\n");
return NULL;
}
/**
* wd_iova_unmap - Unmap physical address (no-op in non-IOMMU mode)
* @cinfo: context information
* @va: virtual address
* @dma: physical address
* @sz: size of the mapping (not used in current implementation)
*
* In non-IOMMU mode, this function does nothing as there's no need to unmap.
* In IOMMU mode, this would typically involve unmapping the DMA address.
*/
static void wd_iova_unmap(struct ctx_info *cinfo, void *va, void *dma, size_t sz)
{
/* For no-iommu, dma-unmap doing nothing */
}
static void wd_pool_uninit(struct wd_blkpool *p)
{
struct ctx_info *cinfo = p->cinfo;
struct wd_blk_hd *fhd = NULL;
unsigned long block_size;
unsigned int i;
block_size = (unsigned long)p->act_hd_sz + p->act_blk_sz;
/* Clean up the allocated resources. */
for (i = 0; i < p->total_blocks; i++) {
/* Release the previously allocated blocks. */
fhd = &p->blk_array[i];
wd_iova_unmap(cinfo, fhd->blk, fhd->blk_dma, block_size);
}
free(p->free_bitmap);
p->free_bitmap = NULL;
free(p->blk_array);
p->blk_array = NULL;
}
static int wd_pool_init(struct wd_blkpool *p)
{
struct ctx_info *cinfo = p->cinfo;
__u32 blk_size = p->setup.block_size;
void *dma_start, *dma_end, *va;
struct wd_blk_hd *fhd = NULL;
struct wd_blk_hd *hd = NULL;
unsigned int i, j, act_num;
unsigned long block_size;
unsigned int dma_num = 0;
p->act_start = (void *)ALIGN((uintptr_t)p->usr_mem_start,
p->setup.align_size);
/* Calculate the actual number of allocatable blocks */
block_size = (unsigned long)(p->act_hd_sz + p->act_blk_sz);
if (block_size == 0) {
WD_ERR("Invalid block size with header.\n");
return -WD_EINVAL;
}
act_num = p->act_mem_sz / block_size;
if (!act_num) {
WD_ERR("Invalid memory size.\n");
return -WD_EINVAL;
}
/* Allocate block array */
p->blk_array = (struct wd_blk_hd *)calloc(act_num, sizeof(struct wd_blk_hd));
if (!p->blk_array) {
WD_ERR("Failed to allocate block array.\n");
return -WD_ENOMEM;
}
/* Allocate bitmap */
p->total_blocks = act_num;
p->bitmap_size = (act_num + BYTE_SIZE - 1) >> BIT_SHIFT;
p->free_bitmap = (unsigned char *)calloc(1, p->bitmap_size);
if (!p->free_bitmap) {
WD_ERR("Failed to allocate free bitmap.\n");
goto bitmap_error;
}
/* Initialize all blocks. */
for (i = 0; i < act_num; i++) {
/* Calculate the virtual address of the current block. */
va = (void *)((uintptr_t)p->act_start + block_size * i);
/* Get the physical address. */
dma_start = wd_iova_map(cinfo, va, 0);
dma_end = wd_iova_map(cinfo, va + blk_size - 1, 0);
if (!dma_start || !dma_end) {
WD_ERR("wd_iova_map err.\n");
/* Clean up the allocated resources. */
goto init_blk_error;
}
/* Check whether the physical addresses are contiguous. */
if ((uintptr_t)dma_end - (uintptr_t)dma_start != blk_size - 1) {
/* If OS kernel is not open SMMU, need to check dma address */
WD_INFO("wd dma address not continuous.\n");
/* Mark as unavailable, bit value is 1. */
bitmap_set_bit(p->free_bitmap, i);
continue;
}
/* Initialize the block. */
hd = &p->blk_array[i];
hd->blk_dma = dma_start;
hd->blk = va;
hd->blk_tag = TAG_FREE;
hd->blk_num = 0;
dma_num++;
}
/*
* if dma_num <= (1 / 1.15) * user's block_num, we think the pool
* is created with failure.
*/
if (dma_num <= NUM_TIMES(p->setup.block_num)) {
WD_ERR("dma_num = %u, not enough.\n", dma_num);
goto init_blk_error;
}
p->free_blk_num = dma_num;
p->setup.block_num = dma_num;
return WD_SUCCESS;
init_blk_error:
/* Clean up the allocated resources. */
for (j = 0; j < i; j++) {
/* Release the previously allocated blocks. */
fhd = &p->blk_array[j];
wd_iova_unmap(cinfo, fhd->blk, fhd->blk_dma, block_size);
}
free(p->free_bitmap);
bitmap_error:
free(p->blk_array);
return -WD_ENOMEM;
}
static int usr_pool_init(struct wd_blkpool *p)
{
struct wd_mempool_setup *sp = &p->setup;
__u32 blk_size = sp->block_size;
struct wd_blk_hd *hd = NULL;
__u32 i;
p->act_start = (void *)ALIGN((uintptr_t)p->usr_mem_start,
sp->align_size);
for (i = 0; i < sp->block_num; i++) {
hd = (void *)((uintptr_t)p->act_start + (p->act_hd_sz + p->act_blk_sz) * i);
hd->blk = (void *)((uintptr_t)hd + p->act_hd_sz);
hd->blk_dma = sp->ops.iova_map(sp->ops.usr, hd->blk, blk_size);
if (!hd->blk_dma) {
WD_ERR("failed to map usr blk.\n");
return -WD_ENOMEM;
}
hd->blk_tag = TAG_FREE;
}
p->free_blk_num = sp->block_num;
return WD_SUCCESS;
}
static int wd_mempool_init(handle_t h_ctx, struct wd_blkpool *pool,
struct wd_mempool_setup *setup)
{
struct wd_ctx_h *ctx = (struct wd_ctx_h *)h_ctx;
struct ctx_info *cinfo = pool->cinfo;
void *addr = NULL;
int ret;
/* Use user's memory, and its ops alloc function */
if (setup->ops.alloc && setup->ops.free && setup->ops.iova_map) {
addr = setup->ops.alloc(setup->ops.usr, pool->act_mem_sz);
if (!addr) {
WD_ERR("failed to allocate memory in user pool.\n");
return -WD_EINVAL;
}
pool->usr_mem_start = addr;
if (usr_pool_init(pool)) {
WD_ERR("failed to initialize user pool.\n");
setup->ops.free(setup->ops.usr, addr);
return -WD_EINVAL;
}
} else {
/* Use wd to reserve memory */
addr = wd_map_reserve_mem(pool, pool->act_mem_sz);
if (!addr) {
WD_ERR("wd pool failed to reserve memory.\n");
return -WD_ENOMEM;
}
pool->usr_mem_start = addr;
if (wd_pool_init(pool)) {
WD_ERR("failed to initialize wd pool.\n");
goto err_out;
}
setup->block_num = pool->setup.block_num;
}
ret = wd_parse_dev_id(ctx->dev_path);
if (ret < 0) {
wd_pool_uninit(pool);
goto err_out;
}
pool->dev_id = ret;
return WD_SUCCESS;
err_out:
if (pool->cinfo) {
wd_free_slice(cinfo);
wd_unmap_reserve_mem(cinfo->ss_va, cinfo->ss_mm_size);
pool->cinfo = NULL;
}
return -WD_EINVAL;
}
void *wd_mempool_alloc(handle_t h_ctx, struct wd_mempool_setup *setup)
{
struct wd_blkpool *pool = NULL;
int ret;
if (!setup || !h_ctx) {
WD_ERR("Input param is NULL!\n");
return NULL;
}
ret = wd_is_sva(h_ctx);
if (ret < 0) {
WD_ERR("failed to check device ctx!\n");
return NULL;
} else if (ret == UACCE_DEV_SVA) {
WD_INFO("the device is SVA mode!\n");
return wd_hugepage_pool_create(h_ctx, setup);
}
pool = calloc(1, sizeof(*pool));
if (!pool) {
WD_ERR("failed to malloc pool.\n");
return NULL;
}
ret = pthread_spin_init(&pool->pool_lock, PTHREAD_PROCESS_PRIVATE);
if (ret)
goto err_pool_alloc;
memcpy(&pool->setup, setup, sizeof(pool->setup));
pool->sva_mode = false;
ret = wd_pool_pre_layout(h_ctx, pool, setup);
if (ret)
goto err_pool_layout;
ret = wd_mempool_init(h_ctx, pool, setup);
if (ret)
goto err_pool_init;
return pool;
err_pool_init:
if (pool->cinfo) {
free(pool->cinfo);
pool->cinfo = NULL;
}
err_pool_layout:
pthread_spin_destroy(&pool->pool_lock);
err_pool_alloc:
free(pool);
return NULL;
}
void wd_mempool_free(handle_t h_ctx, void *pool)
{
struct wd_mempool_setup *setup;
struct wd_blkpool *p = pool;
if (!p || !h_ctx) {
WD_ERR("pool destroy err, pool or ctx is NULL.\n");
return;
}
if (p->sva_mode) {
wd_hugepage_pool_destroy(p);
return;
}
setup = &p->setup;
if (p->free_blk_num != setup->block_num) {
WD_ERR("Can not destroy blk pool, as it's in use.\n");
return;
}
if (setup->ops.free)
setup->ops.free(setup->ops.usr, p->usr_mem_start);
if (p->cinfo) {
/* Free block array memory */
if (p->blk_array)
free(p->blk_array);
if (p->free_bitmap)
free(p->free_bitmap);
wd_free_slice(p->cinfo);
wd_unmap_reserve_mem(p->cinfo->ss_va, p->cinfo->ss_mm_size);
free(p->cinfo);
p->cinfo = NULL;
}
pthread_spin_destroy(&p->pool_lock);
free(p);
}
void wd_mem_free(void *pool, void *buf)
{
struct wd_blkpool *p = pool;
struct wd_blk_hd *current_hd;
struct wd_blk_hd *hd;
unsigned int current_idx;
unsigned int blk_idx;
unsigned long offset;
unsigned int i, num;
unsigned long sz;
if (unlikely(!p || !buf)) {
WD_ERR("free blk parameters err!\n");
return;
}
if (p->sva_mode) {
wd_hugepage_blk_free(p, buf);
return;
}
sz = p->act_hd_sz + p->act_blk_sz;
if (!sz) {
WD_ERR("memory pool blk size is zero!\n");
return;
}
if ((uintptr_t)buf < (uintptr_t)p->act_start) {
WD_ERR("free block addr is error.\n");
return;
}
/* Calculate the block index. */
offset = (unsigned long)((uintptr_t)buf - (uintptr_t)p->act_start);
blk_idx = offset / sz;
/* Check if the index is valid. */