1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2010-2020 Intel Corporation
4 * Copyright (c) 2007-2009 Kip Macy kmacy@freebsd.org
6 * Derived from FreeBSD's bufring.h
7 * Used as BSD-3 Licensed with permission from Kip Macy.
10 #ifndef _RTE_RING_RTS_ELEM_PVT_H_
11 #define _RTE_RING_RTS_ELEM_PVT_H_
14 * @file rte_ring_rts_elem_pvt.h
15 * It is not recommended to include this file directly,
16 * include <rte_ring.h> instead.
17 * Contains internal helper functions for Relaxed Tail Sync (RTS) ring mode.
18 * For more information please refer to <rte_ring_rts.h>.
22 * @internal This function updates tail values.
24 static __rte_always_inline void
25 __rte_ring_rts_update_tail(struct rte_ring_rts_headtail *ht)
27 union __rte_ring_rts_poscnt h, ot, nt;
30 * If there are other enqueues/dequeues in progress that
31 * might preceded us, then don't update tail with new value.
34 ot.raw = __atomic_load_n(&ht->tail.raw, __ATOMIC_ACQUIRE);
37 /* on 32-bit systems we have to do atomic read here */
38 h.raw = __atomic_load_n(&ht->head.raw, __ATOMIC_RELAXED);
41 if (++nt.val.cnt == h.val.cnt)
42 nt.val.pos = h.val.pos;
44 } while (__atomic_compare_exchange_n(&ht->tail.raw, &ot.raw, nt.raw,
45 0, __ATOMIC_RELEASE, __ATOMIC_ACQUIRE) == 0);
49 * @internal This function waits till head/tail distance wouldn't
50 * exceed pre-defined max value.
52 static __rte_always_inline void
53 __rte_ring_rts_head_wait(const struct rte_ring_rts_headtail *ht,
54 union __rte_ring_rts_poscnt *h)
60 while (h->val.pos - ht->tail.val.pos > max) {
62 h->raw = __atomic_load_n(&ht->head.raw, __ATOMIC_ACQUIRE);
67 * @internal This function updates the producer head for enqueue.
69 static __rte_always_inline uint32_t
70 __rte_ring_rts_move_prod_head(struct rte_ring *r, uint32_t num,
71 enum rte_ring_queue_behavior behavior, uint32_t *old_head,
72 uint32_t *free_entries)
75 union __rte_ring_rts_poscnt nh, oh;
77 const uint32_t capacity = r->capacity;
79 oh.raw = __atomic_load_n(&r->rts_prod.head.raw, __ATOMIC_ACQUIRE);
82 /* Reset n to the initial burst count */
86 * wait for prod head/tail distance,
87 * make sure that we read prod head *before*
90 __rte_ring_rts_head_wait(&r->rts_prod, &oh);
93 * The subtraction is done between two unsigned 32bits value
94 * (the result is always modulo 32 bits even if we have
95 * *old_head > cons_tail). So 'free_entries' is always between 0
96 * and capacity (which is < size).
98 *free_entries = capacity + r->cons.tail - oh.val.pos;
100 /* check that we have enough room in ring */
101 if (unlikely(n > *free_entries))
102 n = (behavior == RTE_RING_QUEUE_FIXED) ?
108 nh.val.pos = oh.val.pos + n;
109 nh.val.cnt = oh.val.cnt + 1;
112 * this CAS(ACQUIRE, ACQUIRE) serves as a hoist barrier to prevent:
113 * - OOO reads of cons tail value
114 * - OOO copy of elems to the ring
116 } while (__atomic_compare_exchange_n(&r->rts_prod.head.raw,
118 0, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE) == 0);
120 *old_head = oh.val.pos;
125 * @internal This function updates the consumer head for dequeue
127 static __rte_always_inline unsigned int
128 __rte_ring_rts_move_cons_head(struct rte_ring *r, uint32_t num,
129 enum rte_ring_queue_behavior behavior, uint32_t *old_head,
133 union __rte_ring_rts_poscnt nh, oh;
135 oh.raw = __atomic_load_n(&r->rts_cons.head.raw, __ATOMIC_ACQUIRE);
137 /* move cons.head atomically */
139 /* Restore n as it may change every loop */
143 * wait for cons head/tail distance,
144 * make sure that we read cons head *before*
147 __rte_ring_rts_head_wait(&r->rts_cons, &oh);
149 /* The subtraction is done between two unsigned 32bits value
150 * (the result is always modulo 32 bits even if we have
151 * cons_head > prod_tail). So 'entries' is always between 0
154 *entries = r->prod.tail - oh.val.pos;
156 /* Set the actual entries for dequeue */
158 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
160 if (unlikely(n == 0))
163 nh.val.pos = oh.val.pos + n;
164 nh.val.cnt = oh.val.cnt + 1;
167 * this CAS(ACQUIRE, ACQUIRE) serves as a hoist barrier to prevent:
168 * - OOO reads of prod tail value
169 * - OOO copy of elems from the ring
171 } while (__atomic_compare_exchange_n(&r->rts_cons.head.raw,
173 0, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE) == 0);
175 *old_head = oh.val.pos;
180 * @internal Enqueue several objects on the RTS ring.
183 * A pointer to the ring structure.
185 * A pointer to a table of objects.
187 * The size of ring element, in bytes. It must be a multiple of 4.
188 * This must be the same value used while creating the ring. Otherwise
189 * the results are undefined.
191 * The number of objects to add in the ring from the obj_table.
193 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
194 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
196 * returns the amount of space after the enqueue operation has finished
198 * Actual number of objects enqueued.
199 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
201 static __rte_always_inline unsigned int
202 __rte_ring_do_rts_enqueue_elem(struct rte_ring *r, const void *obj_table,
203 uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
204 uint32_t *free_space)
208 n = __rte_ring_rts_move_prod_head(r, n, behavior, &head, &free);
211 __rte_ring_enqueue_elems(r, head, obj_table, esize, n);
212 __rte_ring_rts_update_tail(&r->rts_prod);
215 if (free_space != NULL)
216 *free_space = free - n;
221 * @internal Dequeue several objects from the RTS ring.
224 * A pointer to the ring structure.
226 * A pointer to a table of objects.
228 * The size of ring element, in bytes. It must be a multiple of 4.
229 * This must be the same value used while creating the ring. Otherwise
230 * the results are undefined.
232 * The number of objects to pull from the ring.
234 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
235 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
237 * returns the number of remaining ring entries after the dequeue has finished
239 * - Actual number of objects dequeued.
240 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
242 static __rte_always_inline unsigned int
243 __rte_ring_do_rts_dequeue_elem(struct rte_ring *r, void *obj_table,
244 uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
247 uint32_t entries, head;
249 n = __rte_ring_rts_move_cons_head(r, n, behavior, &head, &entries);
252 __rte_ring_dequeue_elems(r, head, obj_table, esize, n);
253 __rte_ring_rts_update_tail(&r->rts_cons);
256 if (available != NULL)
257 *available = entries - n;
261 #endif /* _RTE_RING_RTS_ELEM_PVT_H_ */