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_C11_MEM_H_
11 #define _RTE_RING_RTS_C11_MEM_H_
14 * @file rte_ring_rts_c11_mem.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;
179 #endif /* _RTE_RING_RTS_C11_MEM_H_ */