aboutsummaryrefslogtreecommitdiffstats
path: root/src/vlib/dma/dma.h
blob: eaeba8b13b7796eef8d6c8f8cacbd6c17a182c62 (plain)
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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright (c) 2022 Cisco Systems, Inc.
 */

#ifndef included_vlib_dma_h
#define included_vlib_dma_h
#include <vlib/vlib.h>

#define dma_log_debug(f, ...)                                                 \
  vlib_log (VLIB_LOG_LEVEL_DEBUG, dma_log.class, "%s: " f, __func__,          \
	    ##__VA_ARGS__)

#define dma_log_info(f, ...)                                                  \
  vlib_log (VLIB_LOG_LEVEL_INFO, dma_log.class, "%s: " f, __func__,           \
	    ##__VA_ARGS__)

struct vlib_dma_batch;
struct vlib_dma_config_data;

typedef int (vlib_dma_config_add_fn) (vlib_main_t *vm,
				      struct vlib_dma_config_data *cfg);
typedef void (vlib_dma_config_del_fn) (vlib_main_t *vm,
				       struct vlib_dma_config_data *cfg);
typedef struct vlib_dma_batch *(vlib_dma_batch_new_fn) (
  vlib_main_t *vm, struct vlib_dma_config_data *);
typedef int (vlib_dma_batch_submit_fn) (vlib_main_t *vm,
					struct vlib_dma_batch *b);
typedef void (vlib_dma_batch_callback_fn) (vlib_main_t *vm,
					   struct vlib_dma_batch *b);
typedef struct
{
  union
  {
    struct
    {
      u32 barrier_before_last : 1;
      u32 sw_fallback : 1;
    };
    u32 features;
  };
  u16 max_transfers;
  u32 max_transfer_size;
  vlib_dma_batch_callback_fn *callback_fn;
} vlib_dma_config_t;

typedef struct vlib_dma_batch
{
  vlib_dma_batch_submit_fn *submit_fn;
  vlib_dma_batch_callback_fn *callback_fn;
  uword cookie;
  u16 src_ptr_off;
  u16 dst_ptr_off;
  u16 size_off;
  u16 stride;
  u16 n_enq;
} vlib_dma_batch_t;

typedef struct
{
  char *name;
  vlib_dma_config_add_fn *config_add_fn;
  vlib_dma_config_del_fn *config_del_fn;
  format_function_t *info_fn;
} vlib_dma_backend_t;

typedef struct vlib_dma_config_data
{
  vlib_dma_config_t cfg;
  vlib_dma_batch_new_fn *batch_new_fn;
  uword private_data;
  u32 backend_index;
  u32 config_index;
} vlib_dma_config_data_t;

typedef struct
{
  vlib_dma_backend_t *backends;
  vlib_dma_config_data_t *configs;
} vlib_dma_main_t;

extern vlib_dma_main_t vlib_dma_main;

clib_error_t *vlib_dma_register_backend (vlib_main_t *vm,
					 vlib_dma_backend_t *b);

int vlib_dma_config_add (vlib_main_t *vm, vlib_dma_config_t *b);
void vlib_dma_config_del (vlib_main_t *vm, u32 config_index);
u8 *vlib_dma_config_info (u8 *s, va_list *args);

static_always_inline vlib_dma_batch_t *
vlib_dma_batch_new (vlib_main_t *vm, u32 config_index)
{
  vlib_dma_main_t *dm = &vlib_dma_main;
  vlib_dma_config_data_t *cd = pool_elt_at_index (dm->configs, config_index);

  return cd->batch_new_fn (vm, cd);
}

static_always_inline void
vlib_dma_batch_set_cookie (vlib_main_t *vm, vlib_dma_batch_t *batch,
			   uword cookie)
{
  batch->cookie = cookie;
}

static_always_inline uword
vlib_dma_batch_get_cookie (vlib_main_t *vm, vlib_dma_batch_t *batch)
{
  return batch->cookie;
}

static_always_inline void
vlib_dma_batch_add (vlib_main_t *vm, vlib_dma_batch_t *batch, void *dst,
		    void *src, u32 size)
{
  u8 *p = (u8 *) batch + batch->n_enq * batch->stride;

  *((void **) (p + batch->dst_ptr_off)) = dst;
  *((void **) (p + batch->src_ptr_off)) = src;
  *((u32 *) (p + batch->size_off)) = size;

  batch->n_enq++;
}

static_always_inline void
vlib_dma_batch_submit (vlib_main_t *vm, vlib_dma_batch_t *batch)
{
  batch->submit_fn (vm, batch);
}

#endif