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_HTS_ELEM_PVT_H_
11 #define _RTE_RING_HTS_ELEM_PVT_H_
14 * @file rte_ring_hts_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 head/tail sync (HTS) ring mode.
18 * For more information please refer to <rte_ring_hts.h>.
22 * @internal update tail with new value.
24 static __rte_always_inline void
25 __rte_ring_hts_update_tail(struct rte_ring_hts_headtail *ht, uint32_t old_tail,
26 uint32_t num, uint32_t enqueue)
30 RTE_SET_USED(enqueue);
32 tail = old_tail + num;
33 __atomic_store_n(&ht->ht.pos.tail, tail, __ATOMIC_RELEASE);
37 * @internal waits till tail will become equal to head.
38 * Means no writer/reader is active for that ring.
39 * Suppose to work as serialization point.
41 static __rte_always_inline void
42 __rte_ring_hts_head_wait(const struct rte_ring_hts_headtail *ht,
43 union __rte_ring_hts_pos *p)
45 while (p->pos.head != p->pos.tail) {
47 p->raw = __atomic_load_n(&ht->ht.raw, __ATOMIC_ACQUIRE);
52 * @internal This function updates the producer head for enqueue
54 static __rte_always_inline unsigned int
55 __rte_ring_hts_move_prod_head(struct rte_ring *r, unsigned int num,
56 enum rte_ring_queue_behavior behavior, uint32_t *old_head,
57 uint32_t *free_entries)
60 union __rte_ring_hts_pos np, op;
62 const uint32_t capacity = r->capacity;
64 op.raw = __atomic_load_n(&r->hts_prod.ht.raw, __ATOMIC_ACQUIRE);
67 /* Reset n to the initial burst count */
71 * wait for tail to be equal to head,
72 * make sure that we read prod head/tail *before*
75 __rte_ring_hts_head_wait(&r->hts_prod, &op);
78 * The subtraction is done between two unsigned 32bits value
79 * (the result is always modulo 32 bits even if we have
80 * *old_head > cons_tail). So 'free_entries' is always between 0
81 * and capacity (which is < size).
83 *free_entries = capacity + r->cons.tail - op.pos.head;
85 /* check that we have enough room in ring */
86 if (unlikely(n > *free_entries))
87 n = (behavior == RTE_RING_QUEUE_FIXED) ?
93 np.pos.tail = op.pos.tail;
94 np.pos.head = op.pos.head + n;
97 * this CAS(ACQUIRE, ACQUIRE) serves as a hoist barrier to prevent:
98 * - OOO reads of cons tail value
99 * - OOO copy of elems from the ring
101 } while (__atomic_compare_exchange_n(&r->hts_prod.ht.raw,
103 0, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE) == 0);
105 *old_head = op.pos.head;
110 * @internal This function updates the consumer head for dequeue
112 static __rte_always_inline unsigned int
113 __rte_ring_hts_move_cons_head(struct rte_ring *r, unsigned int num,
114 enum rte_ring_queue_behavior behavior, uint32_t *old_head,
118 union __rte_ring_hts_pos np, op;
120 op.raw = __atomic_load_n(&r->hts_cons.ht.raw, __ATOMIC_ACQUIRE);
122 /* move cons.head atomically */
124 /* Restore n as it may change every loop */
128 * wait for tail to be equal to head,
129 * make sure that we read cons head/tail *before*
132 __rte_ring_hts_head_wait(&r->hts_cons, &op);
134 /* The subtraction is done between two unsigned 32bits value
135 * (the result is always modulo 32 bits even if we have
136 * cons_head > prod_tail). So 'entries' is always between 0
139 *entries = r->prod.tail - op.pos.head;
141 /* Set the actual entries for dequeue */
143 n = (behavior == RTE_RING_QUEUE_FIXED) ? 0 : *entries;
145 if (unlikely(n == 0))
148 np.pos.tail = op.pos.tail;
149 np.pos.head = op.pos.head + n;
152 * this CAS(ACQUIRE, ACQUIRE) serves as a hoist barrier to prevent:
153 * - OOO reads of prod tail value
154 * - OOO copy of elems from the ring
156 } while (__atomic_compare_exchange_n(&r->hts_cons.ht.raw,
158 0, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE) == 0);
160 *old_head = op.pos.head;
165 * @internal Enqueue several objects on the HTS ring.
168 * A pointer to the ring structure.
170 * A pointer to a table of objects.
172 * The size of ring element, in bytes. It must be a multiple of 4.
173 * This must be the same value used while creating the ring. Otherwise
174 * the results are undefined.
176 * The number of objects to add in the ring from the obj_table.
178 * RTE_RING_QUEUE_FIXED: Enqueue a fixed number of items from a ring
179 * RTE_RING_QUEUE_VARIABLE: Enqueue as many items as possible from ring
181 * returns the amount of space after the enqueue operation has finished
183 * Actual number of objects enqueued.
184 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
186 static __rte_always_inline unsigned int
187 __rte_ring_do_hts_enqueue_elem(struct rte_ring *r, const void *obj_table,
188 uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
189 uint32_t *free_space)
193 n = __rte_ring_hts_move_prod_head(r, n, behavior, &head, &free);
196 __rte_ring_enqueue_elems(r, head, obj_table, esize, n);
197 __rte_ring_hts_update_tail(&r->hts_prod, head, n, 1);
200 if (free_space != NULL)
201 *free_space = free - n;
206 * @internal Dequeue several objects from the HTS ring.
209 * A pointer to the ring structure.
211 * A pointer to a table of objects.
213 * The size of ring element, in bytes. It must be a multiple of 4.
214 * This must be the same value used while creating the ring. Otherwise
215 * the results are undefined.
217 * The number of objects to pull from the ring.
219 * RTE_RING_QUEUE_FIXED: Dequeue a fixed number of items from a ring
220 * RTE_RING_QUEUE_VARIABLE: Dequeue as many items as possible from ring
222 * returns the number of remaining ring entries after the dequeue has finished
224 * - Actual number of objects dequeued.
225 * If behavior == RTE_RING_QUEUE_FIXED, this will be 0 or n only.
227 static __rte_always_inline unsigned int
228 __rte_ring_do_hts_dequeue_elem(struct rte_ring *r, void *obj_table,
229 uint32_t esize, uint32_t n, enum rte_ring_queue_behavior behavior,
232 uint32_t entries, head;
234 n = __rte_ring_hts_move_cons_head(r, n, behavior, &head, &entries);
237 __rte_ring_dequeue_elems(r, head, obj_table, esize, n);
238 __rte_ring_hts_update_tail(&r->hts_cons, head, n, 0);
241 if (available != NULL)
242 *available = entries - n;
246 #endif /* _RTE_RING_HTS_ELEM_PVT_H_ */