Marvin Liu | abd5669 | 2022-08-17 09:38:40 +0800 | [diff] [blame] | 1 | /* 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 | |
| 17 | struct vlib_dma_batch; |
| 18 | struct vlib_dma_config_data; |
| 19 | |
| 20 | typedef int (vlib_dma_config_add_fn) (vlib_main_t *vm, |
| 21 | struct vlib_dma_config_data *cfg); |
| 22 | typedef void (vlib_dma_config_del_fn) (vlib_main_t *vm, |
| 23 | struct vlib_dma_config_data *cfg); |
| 24 | typedef struct vlib_dma_batch *(vlib_dma_batch_new_fn) ( |
| 25 | vlib_main_t *vm, struct vlib_dma_config_data *); |
| 26 | typedef int (vlib_dma_batch_submit_fn) (vlib_main_t *vm, |
| 27 | struct vlib_dma_batch *b); |
| 28 | typedef void (vlib_dma_batch_callback_fn) (vlib_main_t *vm, |
| 29 | struct vlib_dma_batch *b); |
| 30 | typedef struct |
| 31 | { |
| 32 | union |
| 33 | { |
| 34 | struct |
| 35 | { |
| 36 | u32 barrier_before_last : 1; |
| 37 | u32 sw_fallback : 1; |
| 38 | }; |
| 39 | u32 features; |
| 40 | }; |
| 41 | u16 max_transfers; |
| 42 | u32 max_transfer_size; |
| 43 | vlib_dma_batch_callback_fn *callback_fn; |
| 44 | } vlib_dma_config_t; |
| 45 | |
| 46 | typedef struct vlib_dma_batch |
| 47 | { |
| 48 | vlib_dma_batch_submit_fn *submit_fn; |
| 49 | vlib_dma_batch_callback_fn *callback_fn; |
| 50 | uword cookie; |
| 51 | u16 src_ptr_off; |
| 52 | u16 dst_ptr_off; |
| 53 | u16 size_off; |
| 54 | u16 stride; |
| 55 | u16 n_enq; |
| 56 | } vlib_dma_batch_t; |
| 57 | |
| 58 | typedef struct |
| 59 | { |
| 60 | char *name; |
| 61 | vlib_dma_config_add_fn *config_add_fn; |
| 62 | vlib_dma_config_del_fn *config_del_fn; |
| 63 | format_function_t *info_fn; |
| 64 | } vlib_dma_backend_t; |
| 65 | |
| 66 | typedef struct vlib_dma_config_data |
| 67 | { |
| 68 | vlib_dma_config_t cfg; |
| 69 | vlib_dma_batch_new_fn *batch_new_fn; |
| 70 | uword private_data; |
| 71 | u32 backend_index; |
| 72 | u32 config_index; |
| 73 | } vlib_dma_config_data_t; |
| 74 | |
| 75 | typedef struct |
| 76 | { |
| 77 | vlib_dma_backend_t *backends; |
| 78 | vlib_dma_config_data_t *configs; |
| 79 | } vlib_dma_main_t; |
| 80 | |
| 81 | extern vlib_dma_main_t vlib_dma_main; |
| 82 | |
| 83 | clib_error_t *vlib_dma_register_backend (vlib_main_t *vm, |
| 84 | vlib_dma_backend_t *b); |
| 85 | |
| 86 | int vlib_dma_config_add (vlib_main_t *vm, vlib_dma_config_t *b); |
| 87 | void vlib_dma_config_del (vlib_main_t *vm, u32 config_index); |
| 88 | u8 *vlib_dma_config_info (u8 *s, va_list *args); |
| 89 | |
| 90 | static_always_inline vlib_dma_batch_t * |
| 91 | vlib_dma_batch_new (vlib_main_t *vm, u32 config_index) |
| 92 | { |
| 93 | vlib_dma_main_t *dm = &vlib_dma_main; |
| 94 | vlib_dma_config_data_t *cd = pool_elt_at_index (dm->configs, config_index); |
| 95 | |
| 96 | return cd->batch_new_fn (vm, cd); |
| 97 | } |
| 98 | |
| 99 | static_always_inline void |
| 100 | vlib_dma_batch_set_cookie (vlib_main_t *vm, vlib_dma_batch_t *batch, |
| 101 | uword cookie) |
| 102 | { |
| 103 | batch->cookie = cookie; |
| 104 | } |
| 105 | |
| 106 | static_always_inline uword |
| 107 | vlib_dma_batch_get_cookie (vlib_main_t *vm, vlib_dma_batch_t *batch) |
| 108 | { |
| 109 | return batch->cookie; |
| 110 | } |
| 111 | |
| 112 | static_always_inline void |
| 113 | vlib_dma_batch_add (vlib_main_t *vm, vlib_dma_batch_t *batch, void *dst, |
| 114 | void *src, u32 size) |
| 115 | { |
| 116 | u8 *p = (u8 *) batch + batch->n_enq * batch->stride; |
| 117 | |
| 118 | *((void **) (p + batch->dst_ptr_off)) = dst; |
| 119 | *((void **) (p + batch->src_ptr_off)) = src; |
| 120 | *((u32 *) (p + batch->size_off)) = size; |
| 121 | |
| 122 | batch->n_enq++; |
| 123 | } |
| 124 | |
| 125 | static_always_inline void |
| 126 | vlib_dma_batch_submit (vlib_main_t *vm, vlib_dma_batch_t *batch) |
| 127 | { |
| 128 | batch->submit_fn (vm, batch); |
| 129 | } |
| 130 | |
| 131 | #endif |