blob: 62d04110aa6180250431d4ac2c226ac65d0e09fe [file] [log] [blame]
Marvin Liuabd56692022-08-17 09:38:40 +08001/* SPDX-License-Identifier: Apache-2.0
2 * Copyright (c) 2022 Cisco Systems, Inc.
3 */
4
5#ifndef included_vlib_dma_h
6#define included_vlib_dma_h
7#include <vlib/vlib.h>
8
9#define dma_log_debug(f, ...) \
10 vlib_log (VLIB_LOG_LEVEL_DEBUG, dma_log.class, "%s: " f, __func__, \
11 ##__VA_ARGS__)
12
13#define dma_log_info(f, ...) \
14 vlib_log (VLIB_LOG_LEVEL_INFO, dma_log.class, "%s: " f, __func__, \
15 ##__VA_ARGS__)
16
17struct vlib_dma_batch;
18struct vlib_dma_config_data;
19
20typedef int (vlib_dma_config_add_fn) (vlib_main_t *vm,
21 struct vlib_dma_config_data *cfg);
22typedef void (vlib_dma_config_del_fn) (vlib_main_t *vm,
23 struct vlib_dma_config_data *cfg);
24typedef struct vlib_dma_batch *(vlib_dma_batch_new_fn) (
25 vlib_main_t *vm, struct vlib_dma_config_data *);
26typedef int (vlib_dma_batch_submit_fn) (vlib_main_t *vm,
27 struct vlib_dma_batch *b);
28typedef void (vlib_dma_batch_callback_fn) (vlib_main_t *vm,
29 struct vlib_dma_batch *b);
30typedef struct
31{
32 union
33 {
34 struct
35 {
36 u32 barrier_before_last : 1;
37 u32 sw_fallback : 1;
38 };
39 u32 features;
40 };
Marvin Liu738aba72023-03-14 23:43:28 +080041 u16 max_batches;
Marvin Liuabd56692022-08-17 09:38:40 +080042 u16 max_transfers;
43 u32 max_transfer_size;
44 vlib_dma_batch_callback_fn *callback_fn;
45} vlib_dma_config_t;
46
47typedef struct vlib_dma_batch
48{
49 vlib_dma_batch_submit_fn *submit_fn;
50 vlib_dma_batch_callback_fn *callback_fn;
51 uword cookie;
52 u16 src_ptr_off;
53 u16 dst_ptr_off;
54 u16 size_off;
55 u16 stride;
56 u16 n_enq;
57} vlib_dma_batch_t;
58
59typedef struct
60{
61 char *name;
62 vlib_dma_config_add_fn *config_add_fn;
63 vlib_dma_config_del_fn *config_del_fn;
64 format_function_t *info_fn;
65} vlib_dma_backend_t;
66
67typedef struct vlib_dma_config_data
68{
69 vlib_dma_config_t cfg;
70 vlib_dma_batch_new_fn *batch_new_fn;
71 uword private_data;
72 u32 backend_index;
73 u32 config_index;
74} vlib_dma_config_data_t;
75
76typedef struct
77{
78 vlib_dma_backend_t *backends;
79 vlib_dma_config_data_t *configs;
80} vlib_dma_main_t;
81
82extern vlib_dma_main_t vlib_dma_main;
83
84clib_error_t *vlib_dma_register_backend (vlib_main_t *vm,
85 vlib_dma_backend_t *b);
86
87int vlib_dma_config_add (vlib_main_t *vm, vlib_dma_config_t *b);
88void vlib_dma_config_del (vlib_main_t *vm, u32 config_index);
89u8 *vlib_dma_config_info (u8 *s, va_list *args);
90
91static_always_inline vlib_dma_batch_t *
92vlib_dma_batch_new (vlib_main_t *vm, u32 config_index)
93{
94 vlib_dma_main_t *dm = &vlib_dma_main;
95 vlib_dma_config_data_t *cd = pool_elt_at_index (dm->configs, config_index);
96
97 return cd->batch_new_fn (vm, cd);
98}
99
100static_always_inline void
101vlib_dma_batch_set_cookie (vlib_main_t *vm, vlib_dma_batch_t *batch,
102 uword cookie)
103{
104 batch->cookie = cookie;
105}
106
107static_always_inline uword
108vlib_dma_batch_get_cookie (vlib_main_t *vm, vlib_dma_batch_t *batch)
109{
110 return batch->cookie;
111}
112
113static_always_inline void
114vlib_dma_batch_add (vlib_main_t *vm, vlib_dma_batch_t *batch, void *dst,
115 void *src, u32 size)
116{
117 u8 *p = (u8 *) batch + batch->n_enq * batch->stride;
118
119 *((void **) (p + batch->dst_ptr_off)) = dst;
120 *((void **) (p + batch->src_ptr_off)) = src;
121 *((u32 *) (p + batch->size_off)) = size;
122
123 batch->n_enq++;
124}
125
126static_always_inline void
127vlib_dma_batch_submit (vlib_main_t *vm, vlib_dma_batch_t *batch)
128{
129 batch->submit_fn (vm, batch);
130}
131
132#endif