|
| 1 | + |
| 2 | +/* |
| 3 | + * Copyright (c) 2021 Nordic Semiconductor ASA |
| 4 | + * |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | +#ifndef ZEPHYR_INCLUDE_SYS_MPSC_PBUF_H_ |
| 8 | +#define ZEPHYR_INCLUDE_SYS_MPSC_PBUF_H_ |
| 9 | + |
| 10 | +#include <kernel.h> |
| 11 | +#include <sys/mpsc_packet.h> |
| 12 | +#include <string.h> |
| 13 | +#include <stdint.h> |
| 14 | +#include <stdbool.h> |
| 15 | + |
| 16 | +#ifdef __cplusplus |
| 17 | +extern "C" { |
| 18 | +#endif |
| 19 | + |
| 20 | +/** |
| 21 | + * @brief Multi producer, single consumer packet buffer API |
| 22 | + * @defgroup mpsc_buf MPSC (Multi producer, single consumer) packet buffer API |
| 23 | + * @ingroup kernel_apis |
| 24 | + * @{ |
| 25 | + */ |
| 26 | + |
| 27 | +/* |
| 28 | + * Multi producer, single consumer packet buffer allows to allocate variable |
| 29 | + * length consecutive space for storing a packet. When space is allocated |
| 30 | + * it can be filled by the user (except for the first 2 bits) and when packet |
| 31 | + * is ready it is commited. It is allowed to allocate another packet before |
| 32 | + * commiting the previous one. |
| 33 | + * |
| 34 | + * If buffer is full and packet cannot be allocated then null is returned unless |
| 35 | + * overwrite mode is selected. In that mode, oldest entry are dropped (user is |
| 36 | + * notified) until allocation succeeds. It can happen that candidate for |
| 37 | + * dropping is currently being claimed. In that case, it is ommited and next |
| 38 | + * packet is dropped and claimed packet is marked as invalid when freeing. |
| 39 | + * |
| 40 | + * Reading packets is performed in two steps. First packet is claimed. Claiming |
| 41 | + * returns pointer to the packet within the buffer. Packet is freed when no |
| 42 | + * longer in use. |
| 43 | + */ |
| 44 | + |
| 45 | +/**@defgroup MPSC_PBUF_FLAGS MPSC packet buffer flags |
| 46 | + * @{ */ |
| 47 | + |
| 48 | +/** @brief Flag indicating that buffer size is power of 2. |
| 49 | + * |
| 50 | + * When buffer size is power of 2 then optimizations are applied. |
| 51 | + */ |
| 52 | +#define MPSC_PBUF_SIZE_POW2 BIT(0) |
| 53 | + |
| 54 | +/** @brief Flag indicating buffer full policy. |
| 55 | + * |
| 56 | + * If flag is set then when allocating from a full buffer oldest packets are |
| 57 | + * dropped. When flag is not set then allocation returns null. |
| 58 | + */ |
| 59 | +#define MPSC_PBUF_MODE_OVERWRITE BIT(1) |
| 60 | + |
| 61 | +/**@} */ |
| 62 | + |
| 63 | +/* Forward declaration */ |
| 64 | +struct mpsc_pbuf_buffer; |
| 65 | + |
| 66 | +/** @brief Callback prototype for getting length of a packet. |
| 67 | + * |
| 68 | + * @param packet User packet. |
| 69 | + * |
| 70 | + * @return Size of the packet in 32 bit words. |
| 71 | + */ |
| 72 | +typedef uint32_t (*mpsc_pbuf_get_wlen)(union mpsc_pbuf_generic *packet); |
| 73 | + |
| 74 | +/** @brief Callback called when packet is dropped. |
| 75 | + * |
| 76 | + * @param buffer Packet buffer. |
| 77 | + * |
| 78 | + * @param packet Packet that is being dropped. |
| 79 | + */ |
| 80 | +typedef void (*mpsc_pbuf_notify_drop)(struct mpsc_pbuf_buffer *buffer, |
| 81 | + union mpsc_pbuf_generic *packet); |
| 82 | + |
| 83 | +/** @brief MPSC packet buffer structure. */ |
| 84 | +struct mpsc_pbuf_buffer { |
| 85 | + /** Temporary write index. */ |
| 86 | + uint32_t tmp_wr_idx; |
| 87 | + |
| 88 | + /** Write index. */ |
| 89 | + uint32_t wr_idx; |
| 90 | + |
| 91 | + /** Temporary read index. */ |
| 92 | + uint32_t tmp_rd_idx; |
| 93 | + |
| 94 | + /** Read index. */ |
| 95 | + uint32_t rd_idx; |
| 96 | + |
| 97 | + /** Flags. */ |
| 98 | + uint32_t flags; |
| 99 | + |
| 100 | + /** Lock. */ |
| 101 | + struct k_spinlock lock; |
| 102 | + |
| 103 | + /** User callback called whenever packet is dropped. */ |
| 104 | + mpsc_pbuf_notify_drop notify_drop; |
| 105 | + |
| 106 | + /** Callback for getting packet length. */ |
| 107 | + mpsc_pbuf_get_wlen get_wlen; |
| 108 | + |
| 109 | + /* Buffer. */ |
| 110 | + uint32_t *buf; |
| 111 | + |
| 112 | + /* Buffer size in 32 bit words. */ |
| 113 | + uint32_t size; |
| 114 | + |
| 115 | + struct k_sem sem; |
| 116 | +}; |
| 117 | + |
| 118 | +/** @brief MPSC packet buffer configuration. */ |
| 119 | +struct mpsc_pbuf_buffer_config { |
| 120 | + /* Pointer to a memory used for storing packets. */ |
| 121 | + uint32_t *buf; |
| 122 | + |
| 123 | + /* Buffer size in 32 bit words. */ |
| 124 | + uint32_t size; |
| 125 | + |
| 126 | + /* Callbacks. */ |
| 127 | + mpsc_pbuf_notify_drop notify_drop; |
| 128 | + mpsc_pbuf_get_wlen get_wlen; |
| 129 | + |
| 130 | + /* Configuration flags. */ |
| 131 | + uint32_t flags; |
| 132 | +}; |
| 133 | + |
| 134 | +/** @brief Initnialize a packet buffer. |
| 135 | + * |
| 136 | + * @param buffer Buffer. |
| 137 | + * |
| 138 | + * @param config Configuration. |
| 139 | + */ |
| 140 | +void mpsc_pbuf_init(struct mpsc_pbuf_buffer *buffer, |
| 141 | + const struct mpsc_pbuf_buffer_config *config); |
| 142 | + |
| 143 | +/** @brief Allocate a packet. |
| 144 | + * |
| 145 | + * If a buffer is configured to overwrite mode then if there is no space to |
| 146 | + * allocated a new buffer, oldest packets are dropped. Otherwise allocation |
| 147 | + * fails and null pointer is returned. |
| 148 | + * |
| 149 | + * @param buffer Buffer. |
| 150 | + * |
| 151 | + * @param wlen Number of words to allocate. |
| 152 | + * |
| 153 | + * @param timeout Timeout. If called from thread context it will pend for given |
| 154 | + * timeout if packet cannot be allocated before dropping the oldest or |
| 155 | + * returning null. |
| 156 | + * |
| 157 | + * @return Pointer to the allocated space or null if it cannot be allocated. |
| 158 | + */ |
| 159 | +union mpsc_pbuf_generic *mpsc_pbuf_alloc(struct mpsc_pbuf_buffer *buffer, |
| 160 | + size_t wlen, k_timeout_t timeout); |
| 161 | + |
| 162 | +/** @brief Commit a packet. |
| 163 | + * |
| 164 | + * @param buffer Buffer. |
| 165 | + * |
| 166 | + * @param packet Pointer to a packet allocated by @ref mpsc_pbuf_alloc. |
| 167 | + */ |
| 168 | +void mpsc_pbuf_commit(struct mpsc_pbuf_buffer *buffer, |
| 169 | + union mpsc_pbuf_generic *packet); |
| 170 | + |
| 171 | +/** @brief Put single word packet into a buffer. |
| 172 | + * |
| 173 | + * Function is optimized for storing a packet which fit into a single word. |
| 174 | + * Note that 2 bits of that word is used by the buffer. |
| 175 | + * |
| 176 | + * @param buffer Buffer. |
| 177 | + * |
| 178 | + * @param word Packet content consisting of MPSC_PBUF_HDR with valid bit set |
| 179 | + * and data on remaining bits. |
| 180 | + */ |
| 181 | +void mpsc_pbuf_put_word(struct mpsc_pbuf_buffer *buffer, |
| 182 | + union mpsc_pbuf_generic word); |
| 183 | + |
| 184 | +/** @brief Put a packet consisting of a word and a pointer. |
| 185 | + * * |
| 186 | + * Function is optimized for storing packet consisting of a word and a pointer. |
| 187 | + * Note that 2 bits of a first word is used by the buffer. |
| 188 | + * |
| 189 | + * @param buffer Buffer. |
| 190 | + * |
| 191 | + * @param word First word of a packet consisting of MPSC_PBUF_HDR with valid |
| 192 | + * bit set and data on remaining bits. |
| 193 | + * |
| 194 | + * @param data User data. |
| 195 | + */ |
| 196 | +void mpsc_pbuf_put_word_ext(struct mpsc_pbuf_buffer *buffer, |
| 197 | + union mpsc_pbuf_generic word, void *data); |
| 198 | + |
| 199 | +/** @brief Put a packet into a buffer. |
| 200 | + * |
| 201 | + * Copy data into a buffer. |
| 202 | + * Note that 2 bits of a first word is used by the buffer. |
| 203 | + * |
| 204 | + * @param buffer Buffer. |
| 205 | + * |
| 206 | + * @param data First word of data must contain MPSC_PBUF_HDR with valid set. |
| 207 | + * |
| 208 | + * @param wlen Packet size in words. |
| 209 | + */ |
| 210 | +void mpsc_pbuf_put_data(struct mpsc_pbuf_buffer *buffer, |
| 211 | + uint32_t *data, size_t wlen); |
| 212 | + |
| 213 | +/** @brief Claim the first pending packet. |
| 214 | + * |
| 215 | + * @param buffer Buffer. |
| 216 | + */ |
| 217 | +union mpsc_pbuf_generic *mpsc_pbuf_claim(struct mpsc_pbuf_buffer *buffer); |
| 218 | + |
| 219 | +/** @brief Free a packet. |
| 220 | + * |
| 221 | + * @param buffer Buffer. |
| 222 | + * |
| 223 | + * @param packet Packet. |
| 224 | + */ |
| 225 | +void mpsc_pbuf_free(struct mpsc_pbuf_buffer *buffer, |
| 226 | + union mpsc_pbuf_generic *packet); |
| 227 | + |
| 228 | +/** @brief Check if there are any message pending. |
| 229 | + * |
| 230 | + * @param buffer Buffer. |
| 231 | + * |
| 232 | + * @retval true if pending. |
| 233 | + * @retval false if no message is pending. |
| 234 | + */ |
| 235 | +bool mpsc_pbuf_is_pending(struct mpsc_pbuf_buffer *buffer); |
| 236 | + |
| 237 | +/** |
| 238 | + * @} |
| 239 | + */ |
| 240 | + |
| 241 | +#ifdef __cplusplus |
| 242 | +} |
| 243 | +#endif |
| 244 | + |
| 245 | +#endif /* ZEPHYR_INCLUDE_SYS_MPSC_PBUF_H_ */ |
0 commit comments