vlib: introduce DMA infrastructure
[vpp.git] / src / vlib / dma / dma.c
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright (c) 2022 Cisco Systems, Inc.
3  */
4
5 #include <vlib/vlib.h>
6 #include <vlib/log.h>
7 #include <vlib/dma/dma.h>
8
9 VLIB_REGISTER_LOG_CLASS (dma_log) = {
10   .class_name = "dma",
11 };
12
13 vlib_dma_main_t vlib_dma_main = {};
14
15 clib_error_t *
16 vlib_dma_register_backend (vlib_main_t *vm, vlib_dma_backend_t *b)
17 {
18   vlib_dma_main_t *dm = &vlib_dma_main;
19   vec_add1 (dm->backends, *b);
20   dma_log_info ("backend '%s' registered", b->name);
21   return 0;
22 }
23
24 int
25 vlib_dma_config_add (vlib_main_t *vm, vlib_dma_config_t *c)
26 {
27   vlib_dma_main_t *dm = &vlib_dma_main;
28   vlib_dma_backend_t *b;
29   vlib_dma_config_data_t *cd;
30
31   pool_get_zero (dm->configs, cd);
32   cd->config_index = cd - dm->configs;
33
34   clib_memcpy (&cd->cfg, c, sizeof (vlib_dma_config_t));
35
36   vec_foreach (b, dm->backends)
37     {
38       dma_log_info ("calling '%s' config_add_fn", b->name);
39       if (b->config_add_fn (vm, cd))
40         {
41           dma_log_info ("config %u added into backend %s", cd - dm->configs,
42                         b->name);
43           cd->backend_index = b - dm->backends;
44           return cd - dm->configs;
45         }
46     }
47
48   pool_put (dm->configs, cd);
49   return -1;
50 }
51
52 void
53 vlib_dma_config_del (vlib_main_t *vm, u32 config_index)
54 {
55   vlib_dma_main_t *dm = &vlib_dma_main;
56   vlib_dma_config_data_t *cd = pool_elt_at_index (dm->configs, config_index);
57   vlib_dma_backend_t *b = vec_elt_at_index (dm->backends, cd->backend_index);
58
59   if (b->config_del_fn)
60     b->config_del_fn (vm, cd);
61
62   pool_put (dm->configs, cd);
63   dma_log_info ("config %u deleted from backend %s", config_index, b->name);
64 }
65
66 u8 *
67 vlib_dma_config_info (u8 *s, va_list *args)
68 {
69   vlib_dma_main_t *dm = &vlib_dma_main;
70   int config_index = va_arg (*args, int);
71   u32 len = pool_elts (dm->configs);
72   if (config_index >= len)
73     return format (s, "%s", "not found");
74   vlib_dma_config_data_t *cd = pool_elt_at_index (dm->configs, config_index);
75
76   vlib_dma_backend_t *b = vec_elt_at_index (dm->backends, cd->backend_index);
77
78   if (b->info_fn)
79     return b->info_fn (s, args);
80
81   return 0;
82 }