Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 Cisco and/or its affiliates. |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 3 | * Copyright (c) 2019 Arm Limited |
| 4 | * Copyright (c) 2010-2017 Intel Corporation and/or its affiliates. |
| 5 | * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org |
| 6 | * Inspired from DPDK rte_ring.h (SPSC only) (derived from freebsd bufring.h). |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at: |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | #ifndef __included_ssvm_fifo_h__ |
| 20 | #define __included_ssvm_fifo_h__ |
| 21 | |
| 22 | #include <vppinfra/clib.h> |
| 23 | #include <vppinfra/vec.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 24 | #include <vppinfra/pool.h> |
| 25 | #include <vppinfra/format.h> |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 26 | #include <svm/fifo_types.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 27 | |
Florin Coras | 326b81e | 2018-10-04 19:03:05 -0700 | [diff] [blame] | 28 | #define OOO_SEGMENT_INVALID_INDEX ((u32)~0) |
| 29 | #define SVM_FIFO_INVALID_SESSION_INDEX ((u32)~0) |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 30 | #define SVM_FIFO_INVALID_INDEX ((u32)~0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 31 | |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 32 | typedef enum svm_fifo_deq_ntf_ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 33 | { |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 34 | SVM_FIFO_NO_DEQ_NOTIF = 0, /**< No notification requested */ |
| 35 | SVM_FIFO_WANT_DEQ_NOTIF = 1, /**< Notify on dequeue */ |
| 36 | SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL = 2, /**< Notify on transition from full */ |
Michal Kalderon | fb07958 | 2021-12-21 04:41:53 -0800 | [diff] [blame^] | 37 | SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY = 4, /**< Notify on transition to empty */ |
| 38 | SVM_FIFO_WANT_DEQ_NOTIF_IF_LEQ_THRESH = 5, /**< Notify on transition to less |
| 39 | than or equal threshold */ |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 40 | } svm_fifo_deq_ntf_t; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 41 | |
Florin Coras | 2309e7a | 2019-04-18 18:58:10 -0700 | [diff] [blame] | 42 | typedef enum svm_fifo_flag_ |
| 43 | { |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 44 | SVM_FIFO_F_LL_TRACKED = 1 << 0, |
Florin Coras | 2309e7a | 2019-04-18 18:58:10 -0700 | [diff] [blame] | 45 | } svm_fifo_flag_t; |
| 46 | |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 47 | typedef enum |
| 48 | { |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 49 | SVM_FIFO_EFULL = -2, |
| 50 | SVM_FIFO_EEMPTY = -3, |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 51 | SVM_FIFO_EGROW = -4, |
Florin Coras | 7fb0fe1 | 2018-04-09 09:24:52 -0700 | [diff] [blame] | 52 | } svm_fifo_err_t; |
| 53 | |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 54 | typedef struct svm_fifo_seg_ |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 55 | { |
| 56 | u8 *data; |
| 57 | u32 len; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 58 | } svm_fifo_seg_t; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 59 | |
Florin Coras | 3eb5062 | 2017-07-13 01:24:57 -0400 | [diff] [blame] | 60 | #if SVM_FIFO_TRACE |
| 61 | #define svm_fifo_trace_add(_f, _s, _l, _t) \ |
| 62 | { \ |
| 63 | svm_fifo_trace_elem_t *trace_elt; \ |
| 64 | vec_add2(_f->trace, trace_elt, 1); \ |
| 65 | trace_elt->offset = _s; \ |
| 66 | trace_elt->len = _l; \ |
| 67 | trace_elt->action = _t; \ |
| 68 | } |
| 69 | #else |
| 70 | #define svm_fifo_trace_add(_f, _s, _l, _t) |
| 71 | #endif |
| 72 | |
| 73 | u8 *svm_fifo_dump_trace (u8 * s, svm_fifo_t * f); |
| 74 | u8 *svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose); |
| 75 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Load head and tail optimized for consumer |
| 78 | * |
| 79 | * Internal function. |
| 80 | */ |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 81 | static inline void |
| 82 | f_load_head_tail_cons (svm_fifo_t * f, u32 * head, u32 * tail) |
| 83 | { |
| 84 | /* load-relaxed: consumer owned index */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 85 | *head = f->shr->head; |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 86 | /* load-acq: consumer foreign index (paired with store-rel in producer) */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 87 | *tail = clib_atomic_load_acq_n (&f->shr->tail); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 88 | } |
| 89 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 90 | /** Load head and tail optimized for producer |
| 91 | * |
| 92 | * Internal function |
| 93 | */ |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 94 | static inline void |
| 95 | f_load_head_tail_prod (svm_fifo_t * f, u32 * head, u32 * tail) |
| 96 | { |
| 97 | /* load relaxed: producer owned index */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 98 | *tail = f->shr->tail; |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 99 | /* load-acq: producer foreign index (paired with store-rel in consumer) */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 100 | *head = clib_atomic_load_acq_n (&f->shr->head); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 101 | } |
| 102 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 103 | /** |
| 104 | * Load head and tail independent of producer/consumer role |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 105 | * |
| 106 | * Internal function. |
| 107 | */ |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 108 | static inline void |
| 109 | f_load_head_tail_all_acq (svm_fifo_t * f, u32 * head, u32 * tail) |
| 110 | { |
| 111 | /* load-acq : consumer foreign index (paired with store-rel) */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 112 | *tail = clib_atomic_load_acq_n (&f->shr->tail); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 113 | /* load-acq : producer foriegn index (paired with store-rel) */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 114 | *head = clib_atomic_load_acq_n (&f->shr->head); |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 117 | /** |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 118 | * Fifo current size, i.e., number of bytes enqueued |
| 119 | * |
| 120 | * Internal function. |
| 121 | */ |
| 122 | static inline u32 |
| 123 | f_cursize (svm_fifo_t * f, u32 head, u32 tail) |
| 124 | { |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 125 | return tail - head; |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Fifo free bytes, i.e., number of free bytes |
| 130 | * |
| 131 | * Internal function |
| 132 | */ |
| 133 | static inline u32 |
| 134 | f_free_count (svm_fifo_t * f, u32 head, u32 tail) |
| 135 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 136 | return (f->shr->size - f_cursize (f, head, tail)); |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 139 | always_inline u32 |
| 140 | f_chunk_end (svm_fifo_chunk_t * c) |
| 141 | { |
| 142 | return c->start_byte + c->length; |
| 143 | } |
| 144 | |
| 145 | always_inline int |
| 146 | f_pos_lt (u32 a, u32 b) |
| 147 | { |
| 148 | return ((i32) (a - b) < 0); |
| 149 | } |
| 150 | |
| 151 | always_inline int |
| 152 | f_pos_leq (u32 a, u32 b) |
| 153 | { |
| 154 | return ((i32) (a - b) <= 0); |
| 155 | } |
| 156 | |
| 157 | always_inline int |
| 158 | f_pos_gt (u32 a, u32 b) |
| 159 | { |
| 160 | return ((i32) (a - b) > 0); |
| 161 | } |
| 162 | |
| 163 | always_inline int |
| 164 | f_pos_geq (u32 a, u32 b) |
| 165 | { |
| 166 | return ((i32) (a - b) >= 0); |
| 167 | } |
| 168 | |
| 169 | always_inline u8 |
| 170 | f_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos) |
| 171 | { |
| 172 | return (f_pos_geq (pos, c->start_byte) |
| 173 | && f_pos_lt (pos, c->start_byte + c->length)); |
| 174 | } |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 175 | |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 176 | always_inline svm_fifo_chunk_t * |
| 177 | f_start_cptr (svm_fifo_t *f) |
| 178 | { |
| 179 | return fs_chunk_ptr (f->fs_hdr, f->shr->start_chunk); |
| 180 | } |
| 181 | |
| 182 | always_inline svm_fifo_chunk_t * |
| 183 | f_end_cptr (svm_fifo_t *f) |
| 184 | { |
| 185 | return fs_chunk_ptr (f->fs_hdr, f->shr->end_chunk); |
| 186 | } |
| 187 | |
| 188 | always_inline svm_fifo_chunk_t * |
| 189 | f_head_cptr (svm_fifo_t *f) |
| 190 | { |
| 191 | return fs_chunk_ptr (f->fs_hdr, f->shr->head_chunk); |
| 192 | } |
| 193 | |
| 194 | always_inline svm_fifo_chunk_t * |
| 195 | f_tail_cptr (svm_fifo_t *f) |
| 196 | { |
| 197 | return fs_chunk_ptr (f->fs_hdr, f->shr->tail_chunk); |
| 198 | } |
| 199 | |
| 200 | always_inline svm_fifo_chunk_t * |
Florin Coras | 17672aa | 2020-12-29 16:55:32 -0800 | [diff] [blame] | 201 | f_cptr (svm_fifo_t *f, fs_sptr_t cp) |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 202 | { |
| 203 | return fs_chunk_ptr (f->fs_hdr, cp); |
| 204 | } |
| 205 | |
Florin Coras | 17672aa | 2020-12-29 16:55:32 -0800 | [diff] [blame] | 206 | always_inline fs_sptr_t |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 207 | f_csptr (svm_fifo_t *f, svm_fifo_chunk_t *c) |
| 208 | { |
| 209 | return fs_chunk_sptr (f->fs_hdr, c); |
| 210 | } |
| 211 | |
| 212 | always_inline void |
Florin Coras | 17672aa | 2020-12-29 16:55:32 -0800 | [diff] [blame] | 213 | f_csptr_link (svm_fifo_t *f, fs_sptr_t cp, svm_fifo_chunk_t *c) |
Florin Coras | af58882 | 2020-12-09 12:51:13 -0800 | [diff] [blame] | 214 | { |
| 215 | fs_chunk_ptr (f->fs_hdr, cp)->next = fs_chunk_sptr (f->fs_hdr, c); |
| 216 | } |
| 217 | |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 218 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 219 | * Create fifo of requested size |
Florin Coras | 41c9e04 | 2018-09-11 00:10:41 -0700 | [diff] [blame] | 220 | * |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 221 | * Allocates fifo on current heap. |
| 222 | * |
| 223 | * @param size data size in bytes for fifo to be allocated. Will be |
| 224 | * rounded to the next highest power-of-two value. |
| 225 | * @return pointer to new fifo |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 226 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 227 | svm_fifo_t *svm_fifo_alloc (u32 size); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 228 | /** |
| 229 | * Initialize fifo |
| 230 | * |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 231 | * @param f fifo |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 232 | * @param size size for fifo |
| 233 | */ |
Florin Coras | 0a84680 | 2019-04-09 18:29:14 -0700 | [diff] [blame] | 234 | void svm_fifo_init (svm_fifo_t * f, u32 size); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Allocate a fifo chunk on heap |
| 237 | * |
| 238 | * If the chunk is allocated on a fifo segment, this should be called |
| 239 | * with the segment's heap pushed. |
| 240 | * |
| 241 | * @param size chunk size in bytes. Will be rounded to the next highest |
| 242 | * power-of-two |
| 243 | * @return new chunk or 0 if alloc failed |
| 244 | */ |
| 245 | svm_fifo_chunk_t *svm_fifo_chunk_alloc (u32 size); |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 246 | /** |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 247 | * Ensure the whole fifo size is writeable |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 248 | * |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 249 | * Allocates enough chunks to cover the whole fifo size. |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 250 | * |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 251 | * @param f fifo |
Florin Coras | 4375fa3 | 2019-04-19 15:56:00 -0700 | [diff] [blame] | 252 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 253 | int svm_fifo_fill_chunk_list (svm_fifo_t * f); |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 254 | /** |
Florin Coras | 40a5da8 | 2020-12-18 09:19:18 -0800 | [diff] [blame] | 255 | * Provision and return chunks for number of bytes requested |
| 256 | * |
| 257 | * Allocates enough chunks to cover the bytes requested and returns them |
| 258 | * in the fifo segment array. The number of bytes provisioned may be less |
| 259 | * than requested if not enough segments were provided. |
| 260 | * |
| 261 | * @param f fifo |
| 262 | * @param fs array of fifo segments |
| 263 | * @param n_segs length of fifo segments array |
| 264 | * @param len number of bytes to preallocate |
| 265 | * @return number of fifo segments provisioned or error |
| 266 | */ |
| 267 | int svm_fifo_provision_chunks (svm_fifo_t *f, svm_fifo_seg_t *fs, u32 n_segs, |
| 268 | u32 len); |
| 269 | /** |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 270 | * Initialize rbtrees used for ooo lookups |
| 271 | * |
| 272 | * @param f fifo |
| 273 | * @param ooo_type type of ooo operation (0 enqueue, 1 dequeue) |
| 274 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 275 | void svm_fifo_init_ooo_lookup (svm_fifo_t * f, u8 ooo_type); |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 276 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 277 | * Free fifo and associated state |
| 278 | * |
| 279 | * @param f fifo |
| 280 | */ |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 281 | void svm_fifo_free (svm_fifo_t * f); |
Florin Coras | b095a3c | 2019-04-25 12:58:46 -0700 | [diff] [blame] | 282 | /** |
| 283 | * Cleanup fifo chunk lookup rb tree |
| 284 | * |
| 285 | * The rb tree is allocated in segment heap so this should be called |
| 286 | * with it pushed. |
| 287 | * |
| 288 | * @param f fifo to cleanup |
| 289 | */ |
| 290 | void svm_fifo_free_chunk_lookup (svm_fifo_t * f); |
| 291 | /** |
| 292 | * Cleanup fifo ooo data |
| 293 | * |
| 294 | * The ooo data is allocated in producer process memory. The fifo |
| 295 | * segment heap should not be pushed. |
| 296 | * |
| 297 | * @param f fifo to cleanup |
| 298 | */ |
| 299 | void svm_fifo_free_ooo_data (svm_fifo_t * f); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 300 | /** |
| 301 | * Init fifo head and tail |
| 302 | * |
| 303 | * @param f fifo |
| 304 | * @param head head value that will be matched to a chunk |
| 305 | * @param tail tail value that will be matched to a chunk |
| 306 | */ |
| 307 | void svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail); |
| 308 | /** |
| 309 | * Clone fifo |
| 310 | * |
| 311 | * Clones single/default chunk fifo. It does not work for fifos with |
| 312 | * multiple chunks. |
| 313 | */ |
| 314 | void svm_fifo_clone (svm_fifo_t * df, svm_fifo_t * sf); |
| 315 | /** |
| 316 | * Enqueue data to fifo |
| 317 | * |
| 318 | * Data is enqueued and tail pointer is updated atomically. If the new data |
| 319 | * enqueued partly overlaps or "touches" an out-of-order segment, said segment |
| 320 | * is "consumed" and the number of bytes returned is appropriately updated. |
| 321 | * |
| 322 | * @param f fifo |
| 323 | * @param len length of data to copy |
| 324 | * @param src buffer from where to copy the data |
| 325 | * @return number of contiguous bytes that can be consumed or error |
| 326 | */ |
| 327 | int svm_fifo_enqueue (svm_fifo_t * f, u32 len, const u8 * src); |
| 328 | /** |
| 329 | * Enqueue data to fifo with offset |
| 330 | * |
| 331 | * Data is enqueued without updating tail pointer. Instead, an out-of-order |
| 332 | * list of segments is generated and maintained. Fifo takes care of coalescing |
| 333 | * contiguous or overlapping segments. |
| 334 | * |
| 335 | * @param f fifo |
| 336 | * @param offset offset at which to copy the data |
| 337 | * @param len len of data to copy |
| 338 | * @param src buffer from where to copy the data |
| 339 | * @return 0 if enqueue was successful, error otherwise |
| 340 | */ |
| 341 | int svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, |
| 342 | u8 * src); |
Florin Coras | a7570b0 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 343 | |
| 344 | /** |
| 345 | * Advance tail pointer |
| 346 | * |
| 347 | * Useful for moving tail pointer after external enqueue. |
| 348 | * |
| 349 | * @param f fifo |
| 350 | * @param len number of bytes to add to tail |
| 351 | */ |
| 352 | void svm_fifo_enqueue_nocopy (svm_fifo_t * f, u32 len); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 353 | /** |
Florin Coras | c95cfa2 | 2020-11-24 08:41:17 -0800 | [diff] [blame] | 354 | * Enqueue array of @ref svm_fifo_seg_t in order |
| 355 | * |
| 356 | * @param f fifo |
| 357 | * @param segs array of segments to enqueue |
| 358 | * @param n_segs number of segments |
| 359 | * @param allow_partial if set partial enqueues are allowed |
| 360 | * @return len if enqueue was successful, error otherwise |
| 361 | */ |
| 362 | int svm_fifo_enqueue_segments (svm_fifo_t * f, const svm_fifo_seg_t segs[], |
| 363 | u32 n_segs, u8 allow_partial); |
| 364 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 365 | * Overwrite fifo head with new data |
| 366 | * |
| 367 | * This should be typically used by dgram transport protocols that need |
Florin Coras | 97d39e3 | 2020-09-04 08:57:27 -0700 | [diff] [blame] | 368 | * to update the dgram header after dequeuing a chunk of data. It assumes |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 369 | * that the dgram header is at most spread over two chunks. |
| 370 | * |
| 371 | * @param f fifo |
| 372 | * @param src src of new data |
| 373 | * @param len length of new data |
| 374 | */ |
| 375 | void svm_fifo_overwrite_head (svm_fifo_t * f, u8 * src, u32 len); |
| 376 | /** |
| 377 | * Dequeue data from fifo |
| 378 | * |
| 379 | * Data is dequeued to consumer provided buffer and head is atomically |
Florin Coras | 97d39e3 | 2020-09-04 08:57:27 -0700 | [diff] [blame] | 380 | * updated. This should not be used in combination with ooo lookups. If |
| 381 | * ooo peeking of data is needed in combination with dequeuing use @ref |
| 382 | * svm_fifo_dequeue_drop. |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 383 | * |
| 384 | * @param f fifo |
| 385 | * @param len length of data to dequeue |
| 386 | * @param dst buffer to where to dequeue the data |
| 387 | * @return number of bytes dequeued or error |
| 388 | */ |
| 389 | int svm_fifo_dequeue (svm_fifo_t * f, u32 len, u8 * dst); |
| 390 | /** |
| 391 | * Peek data from fifo |
| 392 | * |
| 393 | * Data is copied from requested offset into provided dst buffer. Head is |
| 394 | * not updated. |
| 395 | * |
| 396 | * @param f fifo |
| 397 | * @param offset offset from which to copy the data |
| 398 | * @param len length of data to copy |
| 399 | * @param dst buffer to where to dequeue the data |
| 400 | * @return number of bytes peeked |
| 401 | */ |
| 402 | int svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst); |
| 403 | /** |
| 404 | * Dequeue and drop bytes from fifo |
| 405 | * |
| 406 | * Advances fifo head by requested amount of bytes. |
| 407 | * |
| 408 | * @param f fifo |
| 409 | * @param len number of bytes to drop |
| 410 | * @return number of bytes dropped |
| 411 | */ |
| 412 | int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 len); |
| 413 | /** |
| 414 | * Dequeue and drop all bytes from fifo |
| 415 | * |
| 416 | * Advances head to tail position. |
| 417 | * |
| 418 | * @param f fifo |
| 419 | */ |
Florin Coras | 25579b4 | 2018-06-06 17:55:02 -0700 | [diff] [blame] | 420 | void svm_fifo_dequeue_drop_all (svm_fifo_t * f); |
Florin Coras | d68faf8 | 2020-09-25 15:18:13 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Get pointers to fifo chunks data in @ref svm_fifo_seg_t array |
| 423 | * |
| 424 | * Populates fifo segment array with pointers to fifo chunk data and lengths. |
| 425 | * Because this returns pointers to data, it must be paired with |
| 426 | * @ref svm_fifo_dequeue_drop to actually release the fifo chunks after the |
| 427 | * data is consumed. |
| 428 | * |
| 429 | * @param f fifo |
Florin Coras | d1cc38d | 2020-10-11 11:05:04 -0700 | [diff] [blame] | 430 | * @param offset offset from where to retrieve segments |
Florin Coras | d68faf8 | 2020-09-25 15:18:13 -0700 | [diff] [blame] | 431 | * @param fs array of fifo segments allocated by caller |
| 432 | * @param n_segs number of fifo segments in array |
| 433 | * @param max_bytes max bytes to be mapped to fifo segments |
| 434 | * @return number of bytes in fifo segments or SVM_FIFO_EEMPTY |
| 435 | */ |
Florin Coras | d1cc38d | 2020-10-11 11:05:04 -0700 | [diff] [blame] | 436 | int svm_fifo_segments (svm_fifo_t * f, u32 offset, svm_fifo_seg_t * fs, |
| 437 | u32 n_segs, u32 max_bytes); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 438 | /** |
| 439 | * Add io events subscriber to list |
| 440 | * |
| 441 | * @param f fifo |
| 442 | * @param sub subscriber opaque index (typically app worker index) |
| 443 | */ |
| 444 | void svm_fifo_add_subscriber (svm_fifo_t * f, u8 sub); |
| 445 | /** |
| 446 | * Remove io events subscriber form list |
| 447 | * |
| 448 | * @param f fifo |
| 449 | * @param sub subscriber index to be removed |
| 450 | */ |
Florin Coras | 72b0428 | 2019-01-14 17:23:11 -0800 | [diff] [blame] | 451 | void svm_fifo_del_subscriber (svm_fifo_t * f, u8 subscriber); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 452 | /** |
| 453 | * Number of out-of-order segments for fifo |
| 454 | * |
| 455 | * @param f fifo |
| 456 | * @return number of out of order segments |
| 457 | */ |
| 458 | u32 svm_fifo_n_ooo_segments (svm_fifo_t * f); |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 459 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 460 | * First out-of-order segment for fifo |
| 461 | * |
| 462 | * @param f fifo |
| 463 | * @return first out-of-order segment for fifo |
| 464 | */ |
| 465 | ooo_segment_t *svm_fifo_first_ooo_segment (svm_fifo_t * f); |
Florin Coras | eaacce4 | 2019-07-02 13:07:37 -0700 | [diff] [blame] | 466 | /** |
| 467 | * Check if fifo is sane. Debug only. |
| 468 | * |
| 469 | * @param f fifo |
| 470 | * @return 1 if sane, 0 otherwise |
| 471 | */ |
| 472 | u8 svm_fifo_is_sane (svm_fifo_t * f); |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 473 | /** |
| 474 | * Number of chunks linked into the fifo |
| 475 | * |
| 476 | * @param f fifo |
| 477 | * @return number of chunks in fifo linked list |
| 478 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 479 | u32 svm_fifo_n_chunks (svm_fifo_t * f); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 480 | format_function_t format_svm_fifo; |
| 481 | |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 482 | /** |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 483 | * Fifo max bytes to dequeue optimized for consumer |
| 484 | * |
| 485 | * @param f fifo |
| 486 | * @return max number of bytes that can be dequeued |
| 487 | */ |
| 488 | static inline u32 |
| 489 | svm_fifo_max_dequeue_cons (svm_fifo_t * f) |
| 490 | { |
| 491 | u32 tail, head; |
| 492 | f_load_head_tail_cons (f, &head, &tail); |
| 493 | return f_cursize (f, head, tail); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Fifo max bytes to dequeue optimized for producer |
| 498 | * |
| 499 | * @param f fifo |
| 500 | * @return max number of bytes that can be dequeued |
| 501 | */ |
| 502 | static inline u32 |
| 503 | svm_fifo_max_dequeue_prod (svm_fifo_t * f) |
| 504 | { |
| 505 | u32 tail, head; |
| 506 | f_load_head_tail_prod (f, &head, &tail); |
| 507 | return f_cursize (f, head, tail); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Fifo max bytes to dequeue |
| 512 | * |
| 513 | * Note: use producer or consumer specific functions for performance: |
| 514 | * @ref svm_fifo_max_dequeue_cons (svm_fifo_t *f) |
| 515 | * @ref svm_fifo_max_dequeue_prod (svm_fifo_t *f) |
| 516 | */ |
| 517 | static inline u32 |
| 518 | svm_fifo_max_dequeue (svm_fifo_t * f) |
| 519 | { |
| 520 | u32 tail, head; |
| 521 | f_load_head_tail_all_acq (f, &head, &tail); |
| 522 | return f_cursize (f, head, tail); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Check if fifo is full optimized for producer |
| 527 | * |
| 528 | * @param f fifo |
| 529 | * @return 1 if fifo is full 0 otherwise |
| 530 | */ |
| 531 | static inline int |
| 532 | svm_fifo_is_full_prod (svm_fifo_t * f) |
| 533 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 534 | return (svm_fifo_max_dequeue_prod (f) == f->shr->size); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* Check if fifo is full. |
| 538 | * |
| 539 | * Note: use producer or consumer specific functions for performance. |
| 540 | * @ref svm_fifo_is_full_prod (svm_fifo_t * f) |
| 541 | * add cons version if needed |
| 542 | */ |
| 543 | static inline int |
| 544 | svm_fifo_is_full (svm_fifo_t * f) |
| 545 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 546 | return (svm_fifo_max_dequeue (f) == f->shr->size); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Check if fifo is empty optimized for consumer |
| 551 | * |
| 552 | * @param f fifo |
| 553 | * @return 1 if fifo is empty 0 otherwise |
| 554 | */ |
| 555 | static inline int |
| 556 | svm_fifo_is_empty_cons (svm_fifo_t * f) |
| 557 | { |
| 558 | return (svm_fifo_max_dequeue_cons (f) == 0); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Check if fifo is empty optimized for producer |
| 563 | * |
| 564 | * @param f fifo |
| 565 | * @return 1 if fifo is empty 0 otherwise |
| 566 | */ |
| 567 | static inline int |
| 568 | svm_fifo_is_empty_prod (svm_fifo_t * f) |
| 569 | { |
| 570 | return (svm_fifo_max_dequeue_prod (f) == 0); |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Check if fifo is empty |
| 575 | * |
| 576 | * Note: use producer or consumer specific functions for perfomance. |
| 577 | * @ref svm_fifo_is_empty_cons (svm_fifo_t * f) |
| 578 | * @ref svm_fifo_is_empty_prod (svm_fifo_t * f) |
| 579 | */ |
| 580 | static inline int |
| 581 | svm_fifo_is_empty (svm_fifo_t * f) |
| 582 | { |
| 583 | return (svm_fifo_max_dequeue (f) == 0); |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Check if fifo is wrapped |
| 588 | * |
| 589 | * @param f fifo |
| 590 | * @return 1 if 'normalized' head is ahead of tail |
| 591 | */ |
| 592 | static inline u8 |
| 593 | svm_fifo_is_wrapped (svm_fifo_t * f) |
| 594 | { |
| 595 | u32 head, tail; |
| 596 | f_load_head_tail_all_acq (f, &head, &tail); |
Florin Coras | 29a59c3 | 2019-05-02 12:52:19 -0700 | [diff] [blame] | 597 | return head > tail; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Maximum number of bytes that can be enqueued into fifo |
| 602 | * |
| 603 | * Optimized for producer |
| 604 | * |
| 605 | * @param f fifo |
| 606 | * @return max number of bytes that can be enqueued into fifo |
| 607 | */ |
| 608 | static inline u32 |
| 609 | svm_fifo_max_enqueue_prod (svm_fifo_t * f) |
| 610 | { |
| 611 | u32 head, tail; |
| 612 | f_load_head_tail_prod (f, &head, &tail); |
| 613 | return f_free_count (f, head, tail); |
| 614 | } |
| 615 | |
| 616 | /* Maximum number of bytes that can be enqueued into fifo |
| 617 | * |
| 618 | * Note: use producer or consumer specific functions for performance. |
| 619 | * @ref svm_fifo_max_enqueue_prod (svm_fifo_t *f) |
| 620 | * add consumer specific version if needed. |
| 621 | */ |
| 622 | static inline u32 |
| 623 | svm_fifo_max_enqueue (svm_fifo_t * f) |
| 624 | { |
| 625 | u32 head, tail; |
| 626 | f_load_head_tail_all_acq (f, &head, &tail); |
| 627 | return f_free_count (f, head, tail); |
| 628 | } |
| 629 | |
| 630 | /** |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 631 | * Max contiguous chunk of data that can be read. |
| 632 | * |
| 633 | * Should only be called by consumers. |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 634 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 635 | u32 svm_fifo_max_read_chunk (svm_fifo_t * f); |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 636 | |
| 637 | /** |
| 638 | * Max contiguous chunk of data that can be written |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 639 | * |
| 640 | * Should only be called by producers |
Florin Coras | 58d36f0 | 2018-03-09 13:05:53 -0800 | [diff] [blame] | 641 | */ |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 642 | u32 svm_fifo_max_write_chunk (svm_fifo_t * f); |
| 643 | |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 644 | /** |
Florin Coras | 9e61d9a | 2020-02-05 21:13:18 +0000 | [diff] [blame] | 645 | * Fifo number of subscribers getter |
| 646 | * |
| 647 | * @param f fifo |
| 648 | * @return number of subscribers |
| 649 | */ |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 650 | static inline u8 |
| 651 | svm_fifo_n_subscribers (svm_fifo_t * f) |
| 652 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 653 | return f->shr->n_subscribers; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
| 657 | * Check if fifo has out-of-order data |
| 658 | * |
| 659 | * @param f fifo |
| 660 | * @return 1 if fifo has ooo data, 0 otherwise |
| 661 | */ |
| 662 | static inline u8 |
| 663 | svm_fifo_has_ooo_data (svm_fifo_t * f) |
| 664 | { |
| 665 | return f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX; |
| 666 | } |
| 667 | |
| 668 | static inline ooo_segment_t * |
| 669 | svm_fifo_newest_ooo_segment (svm_fifo_t * f) |
| 670 | { |
| 671 | if (f->ooos_newest == OOO_SEGMENT_INVALID_INDEX) |
| 672 | return 0; |
| 673 | return pool_elt_at_index (f->ooo_segments, f->ooos_newest); |
| 674 | } |
| 675 | |
| 676 | static inline void |
| 677 | svm_fifo_newest_ooo_segment_reset (svm_fifo_t * f) |
| 678 | { |
| 679 | f->ooos_newest = OOO_SEGMENT_INVALID_INDEX; |
| 680 | } |
| 681 | |
| 682 | static inline u32 |
| 683 | ooo_segment_offset_prod (svm_fifo_t * f, ooo_segment_t * s) |
| 684 | { |
| 685 | u32 tail; |
| 686 | /* load-relaxed: producer owned index */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 687 | tail = f->shr->tail; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 688 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 689 | return (s->start - tail); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static inline u32 |
| 693 | ooo_segment_length (svm_fifo_t * f, ooo_segment_t * s) |
| 694 | { |
| 695 | return s->length; |
| 696 | } |
| 697 | |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 698 | static inline u32 |
| 699 | svm_fifo_size (svm_fifo_t * f) |
| 700 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 701 | return f->shr->size; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static inline void |
| 705 | svm_fifo_set_size (svm_fifo_t * f, u32 size) |
| 706 | { |
Florin Coras | 4b8011e | 2020-12-07 19:38:23 -0800 | [diff] [blame] | 707 | if (size > (1 << f->fs_hdr->max_log2_fifo_size)) |
Florin Coras | 0e6199d | 2020-04-17 20:15:22 +0000 | [diff] [blame] | 708 | return; |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 709 | fsh_virtual_mem_update (f->fs_hdr, f->shr->slice_index, |
| 710 | (int) f->shr->size - size); |
| 711 | f->shr->size = size; |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 712 | } |
| 713 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 714 | /** |
| 715 | * Check if fifo has io event |
| 716 | * |
| 717 | * @param f fifo |
| 718 | * @return 1 if fifo has event, 0 otherwise |
| 719 | */ |
| 720 | static inline int |
| 721 | svm_fifo_has_event (svm_fifo_t * f) |
| 722 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 723 | return f->shr->has_event; |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Set fifo event flag. |
| 728 | * |
| 729 | * Forces release semantics. |
| 730 | * |
| 731 | * @param f fifo |
| 732 | * @return 1 if flag was not set, 0 otherwise |
| 733 | */ |
| 734 | always_inline u8 |
| 735 | svm_fifo_set_event (svm_fifo_t * f) |
| 736 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 737 | return !clib_atomic_swap_rel_n (&f->shr->has_event, 1); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Unset fifo event flag. |
| 742 | * |
| 743 | * Forces acquire semantics |
| 744 | * |
| 745 | * @param f fifo |
| 746 | */ |
| 747 | always_inline void |
| 748 | svm_fifo_unset_event (svm_fifo_t * f) |
| 749 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 750 | clib_atomic_swap_acq_n (&f->shr->has_event, 0); |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 754 | * Set specific want notification flag |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 755 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 756 | * For list of flags see @ref svm_fifo_deq_ntf_t |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 757 | * |
| 758 | * @param f fifo |
| 759 | * @param ntf_type type of notification requested |
| 760 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 761 | static inline void |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 762 | svm_fifo_add_want_deq_ntf (svm_fifo_t * f, u8 ntf_type) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 763 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 764 | f->shr->want_deq_ntf |= ntf_type; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 765 | } |
| 766 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 767 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 768 | * Clear specific want notification flag |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 769 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 770 | * For list of flags see @ref svm_fifo_ntf_t |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 771 | * |
| 772 | * @param f fifo |
| 773 | * @param ntf_type type of notification to be cleared |
| 774 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 775 | static inline void |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 776 | svm_fifo_del_want_deq_ntf (svm_fifo_t * f, u8 ntf_type) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 777 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 778 | f->shr->want_deq_ntf &= ~ntf_type; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 781 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 782 | * Clear the want notification flag and set has notification |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 783 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 784 | * Should be used after enqueuing an event. This clears the |
| 785 | * SVM_FIFO_WANT_NOTIF flag but it does not clear |
| 786 | * SVM_FIFO_WANT_NOTIF_IF_FULL. If the latter was set, has_ntf is |
| 787 | * set to avoid enqueueing events for for all dequeue operations until |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 788 | * it is manually cleared. |
| 789 | * |
| 790 | * @param f fifo |
| 791 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 792 | static inline void |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 793 | svm_fifo_clear_deq_ntf (svm_fifo_t * f) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 794 | { |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 795 | /* Set the flag if want_notif_if_full was the only ntf requested */ |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 796 | f->shr->has_deq_ntf = |
| 797 | f->shr->want_deq_ntf == SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL; |
Michal Kalderon | fb07958 | 2021-12-21 04:41:53 -0800 | [diff] [blame^] | 798 | svm_fifo_del_want_deq_ntf (f, SVM_FIFO_WANT_DEQ_NOTIF | |
| 799 | SVM_FIFO_WANT_DEQ_NOTIF_IF_LEQ_THRESH); |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 800 | } |
| 801 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 802 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 803 | * Clear has notification flag |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 804 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 805 | * The fifo generates only one event per SVM_FIFO_WANT_NOTIF_IF_FULL |
| 806 | * request and sets has_ntf. To received new events the flag must be |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 807 | * cleared using this function. |
| 808 | * |
| 809 | * @param f fifo |
| 810 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 811 | static inline void |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 812 | svm_fifo_reset_has_deq_ntf (svm_fifo_t * f) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 813 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 814 | f->shr->has_deq_ntf = 0; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 815 | } |
| 816 | |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 817 | /** |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 818 | * Check if fifo needs dequeue notification |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 819 | * |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 820 | * Determines based on notification request flags and state of the fifo if |
| 821 | * an event should be generated. |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 822 | * |
| 823 | * @param f fifo |
| 824 | * @param n_last_deq number of bytes last dequeued |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 825 | * @return 1 if event should be generated, 0 otherwise |
Florin Coras | 87b15ce | 2019-04-28 21:16:30 -0700 | [diff] [blame] | 826 | */ |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 827 | static inline u8 |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 828 | svm_fifo_needs_deq_ntf (svm_fifo_t * f, u32 n_last_deq) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 829 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 830 | u8 want_ntf = f->shr->want_deq_ntf; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 831 | |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 832 | if (PREDICT_TRUE (want_ntf == SVM_FIFO_NO_DEQ_NOTIF)) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 833 | return 0; |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 834 | else if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 835 | return 1; |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 836 | if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_FULL) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 837 | { |
Sirshak Das | 28aa539 | 2019-02-05 01:33:33 -0600 | [diff] [blame] | 838 | u32 max_deq = svm_fifo_max_dequeue_cons (f); |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 839 | u32 size = f->shr->size; |
| 840 | if (!f->shr->has_deq_ntf && max_deq < size && |
| 841 | max_deq + n_last_deq >= size) |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 842 | return 1; |
Nathan Skrzypczak | e82a7ad | 2019-05-22 18:41:50 +0200 | [diff] [blame] | 843 | } |
Florin Coras | 2d379d8 | 2019-06-28 12:45:12 -0700 | [diff] [blame] | 844 | if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_EMPTY) |
Nathan Skrzypczak | e82a7ad | 2019-05-22 18:41:50 +0200 | [diff] [blame] | 845 | { |
Florin Coras | c547e91 | 2020-12-08 17:50:45 -0800 | [diff] [blame] | 846 | if (!f->shr->has_deq_ntf && svm_fifo_is_empty (f)) |
Nathan Skrzypczak | e82a7ad | 2019-05-22 18:41:50 +0200 | [diff] [blame] | 847 | return 1; |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 848 | } |
Michal Kalderon | fb07958 | 2021-12-21 04:41:53 -0800 | [diff] [blame^] | 849 | if (want_ntf & SVM_FIFO_WANT_DEQ_NOTIF_IF_LEQ_THRESH) |
| 850 | { |
| 851 | if (!f->shr->has_deq_ntf && |
| 852 | (svm_fifo_max_dequeue (f) <= f->shr->deq_thresh)) |
| 853 | return 1; |
| 854 | } |
Florin Coras | 1bcad5c | 2019-01-09 20:04:38 -0800 | [diff] [blame] | 855 | return 0; |
| 856 | } |
| 857 | |
Michal Kalderon | fb07958 | 2021-12-21 04:41:53 -0800 | [diff] [blame^] | 858 | /** |
| 859 | * Set the fifo dequeue threshold which will be used for notifications. |
| 860 | * |
| 861 | * Note: If not set, by default threshold is zero, equivalent to |
| 862 | * empty. |
| 863 | */ |
| 864 | static inline void |
| 865 | svm_fifo_set_deq_thresh (svm_fifo_t *f, u32 thresh) |
| 866 | { |
| 867 | f->shr->deq_thresh = thresh; |
| 868 | } |
| 869 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 870 | #endif /* __included_ssvm_fifo_h__ */ |
| 871 | |
| 872 | /* |
| 873 | * fd.io coding-style-patch-verification: ON |
| 874 | * |
| 875 | * Local Variables: |
| 876 | * eval: (c-set-style "gnu") |
| 877 | * End: |
| 878 | */ |