Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2017 Cavium, Inc
3 : : */
4 : :
5 : : #include <eventdev_pmd.h>
6 : :
7 : : #include <rte_common.h>
8 : : #include <rte_branch_prediction.h>
9 : :
10 : : #include "timvf_evdev.h"
11 : :
12 : : static inline int16_t
13 : : timr_bkt_fetch_rem(uint64_t w1)
14 : : {
15 : 0 : return (w1 >> TIM_BUCKET_W1_S_CHUNK_REMAINDER) &
16 : : TIM_BUCKET_W1_M_CHUNK_REMAINDER;
17 : : }
18 : :
19 : : static inline int16_t
20 : : timr_bkt_get_rem(struct tim_mem_bucket *bktp)
21 : : {
22 : : return rte_atomic_load_explicit(&bktp->chunk_remainder,
23 : : rte_memory_order_acquire);
24 : : }
25 : :
26 : : static inline void
27 : : timr_bkt_set_rem(struct tim_mem_bucket *bktp, uint16_t v)
28 : : {
29 : 0 : rte_atomic_store_explicit(&bktp->chunk_remainder, v,
30 : : rte_memory_order_release);
31 : 0 : }
32 : :
33 : : static inline void
34 : : timr_bkt_sub_rem(struct tim_mem_bucket *bktp, uint16_t v)
35 : : {
36 : 0 : rte_atomic_fetch_sub_explicit(&bktp->chunk_remainder, v,
37 : : rte_memory_order_release);
38 : : }
39 : :
40 : : static inline uint8_t
41 : : timr_bkt_get_sbt(uint64_t w1)
42 : : {
43 : : return (w1 >> TIM_BUCKET_W1_S_SBT) & TIM_BUCKET_W1_M_SBT;
44 : : }
45 : :
46 : : static inline uint64_t
47 : : timr_bkt_set_sbt(struct tim_mem_bucket *bktp)
48 : : {
49 : : const uint64_t v = TIM_BUCKET_W1_M_SBT << TIM_BUCKET_W1_S_SBT;
50 : : return rte_atomic_fetch_or_explicit(&bktp->w1, v, rte_memory_order_acq_rel);
51 : : }
52 : :
53 : : static inline uint64_t
54 : : timr_bkt_clr_sbt(struct tim_mem_bucket *bktp)
55 : : {
56 : : const uint64_t v = ~(TIM_BUCKET_W1_M_SBT << TIM_BUCKET_W1_S_SBT);
57 : : return rte_atomic_fetch_and_explicit(&bktp->w1, v, rte_memory_order_acq_rel);
58 : : }
59 : :
60 : : static inline uint8_t
61 : : timr_bkt_get_shbt(uint64_t w1)
62 : : {
63 : 0 : return ((w1 >> TIM_BUCKET_W1_S_HBT) & TIM_BUCKET_W1_M_HBT) |
64 : 0 : ((w1 >> TIM_BUCKET_W1_S_SBT) & TIM_BUCKET_W1_M_SBT);
65 : : }
66 : :
67 : : static inline uint8_t
68 : : timr_bkt_get_hbt(uint64_t w1)
69 : : {
70 : 0 : return (w1 >> TIM_BUCKET_W1_S_HBT) & TIM_BUCKET_W1_M_HBT;
71 : : }
72 : :
73 : : static inline uint8_t
74 : : timr_bkt_get_bsk(uint64_t w1)
75 : : {
76 : : return (w1 >> TIM_BUCKET_W1_S_BSK) & TIM_BUCKET_W1_M_BSK;
77 : : }
78 : :
79 : : static inline uint64_t
80 : : timr_bkt_clr_bsk(struct tim_mem_bucket *bktp)
81 : : {
82 : : /*Clear everything except lock. */
83 : : const uint64_t v = TIM_BUCKET_W1_M_LOCK << TIM_BUCKET_W1_S_LOCK;
84 : : return rte_atomic_fetch_and_explicit(&bktp->w1, v, rte_memory_order_acq_rel);
85 : : }
86 : :
87 : : static inline uint64_t
88 : : timr_bkt_fetch_sema_lock(struct tim_mem_bucket *bktp)
89 : : {
90 : 0 : return rte_atomic_fetch_add_explicit(&bktp->w1, TIM_BUCKET_SEMA_WLOCK,
91 : : rte_memory_order_acq_rel);
92 : : }
93 : :
94 : : static inline uint64_t
95 : : timr_bkt_fetch_sema(struct tim_mem_bucket *bktp)
96 : : {
97 : 0 : return rte_atomic_fetch_add_explicit(&bktp->w1, TIM_BUCKET_SEMA,
98 : : rte_memory_order_relaxed);
99 : : }
100 : :
101 : : static inline uint64_t
102 : : timr_bkt_inc_lock(struct tim_mem_bucket *bktp)
103 : : {
104 : : const uint64_t v = 1ull << TIM_BUCKET_W1_S_LOCK;
105 : 0 : return rte_atomic_fetch_add_explicit(&bktp->w1, v, rte_memory_order_acq_rel);
106 : : }
107 : :
108 : : static inline void
109 : : timr_bkt_dec_lock(struct tim_mem_bucket *bktp)
110 : : {
111 : 0 : rte_atomic_fetch_add_explicit(&bktp->lock, 0xff, rte_memory_order_acq_rel);
112 : : }
113 : :
114 : : static inline uint32_t
115 : : timr_bkt_get_nent(uint64_t w1)
116 : : {
117 : 0 : return (w1 >> TIM_BUCKET_W1_S_NUM_ENTRIES) &
118 : : TIM_BUCKET_W1_M_NUM_ENTRIES;
119 : : }
120 : :
121 : : static inline void
122 : : timr_bkt_inc_nent(struct tim_mem_bucket *bktp)
123 : : {
124 : 0 : rte_atomic_fetch_add_explicit(&bktp->nb_entry, 1, rte_memory_order_relaxed);
125 : : }
126 : :
127 : : static inline void
128 : : timr_bkt_add_nent(struct tim_mem_bucket *bktp, uint32_t v)
129 : : {
130 : 0 : rte_atomic_fetch_add_explicit(&bktp->nb_entry, v, rte_memory_order_relaxed);
131 : 0 : }
132 : :
133 : : static inline uint64_t
134 : : timr_bkt_clr_nent(struct tim_mem_bucket *bktp)
135 : : {
136 : : const uint64_t v = ~(TIM_BUCKET_W1_M_NUM_ENTRIES <<
137 : : TIM_BUCKET_W1_S_NUM_ENTRIES);
138 : : return rte_atomic_fetch_and_explicit(&bktp->w1, v, rte_memory_order_acq_rel) & v;
139 : : }
140 : :
141 : : static inline struct tim_mem_entry *
142 : 0 : timr_clr_bkt(struct timvf_ring * const timr, struct tim_mem_bucket * const bkt)
143 : : {
144 : : struct tim_mem_entry *chunk;
145 : : struct tim_mem_entry *pnext;
146 : 0 : chunk = ((struct tim_mem_entry *)(uintptr_t)bkt->first_chunk);
147 : 0 : chunk = (struct tim_mem_entry *)(uintptr_t)(chunk + nb_chunk_slots)->w0;
148 : :
149 [ # # ]: 0 : while (chunk) {
150 : 0 : pnext = (struct tim_mem_entry *)(uintptr_t)
151 : 0 : ((chunk + nb_chunk_slots)->w0);
152 [ # # ]: 0 : rte_mempool_put(timr->chunk_pool, chunk);
153 : : chunk = pnext;
154 : : }
155 : 0 : return (struct tim_mem_entry *)(uintptr_t)bkt->first_chunk;
156 : : }
157 : :
158 : : static inline int
159 : 0 : timvf_rem_entry(struct rte_event_timer *tim)
160 : : {
161 : : uint64_t lock_sema;
162 : : struct tim_mem_entry *entry;
163 : : struct tim_mem_bucket *bkt;
164 [ # # ]: 0 : if (tim->impl_opaque[1] == 0 ||
165 [ # # ]: 0 : tim->impl_opaque[0] == 0)
166 : : return -ENOENT;
167 : :
168 : 0 : entry = (struct tim_mem_entry *)(uintptr_t)tim->impl_opaque[0];
169 [ # # ]: 0 : if (entry->wqe != tim->ev.u64) {
170 : 0 : tim->impl_opaque[1] = tim->impl_opaque[0] = 0;
171 : 0 : return -ENOENT;
172 : : }
173 : 0 : bkt = (struct tim_mem_bucket *)(uintptr_t)tim->impl_opaque[1];
174 : : lock_sema = timr_bkt_inc_lock(bkt);
175 [ # # ]: 0 : if (timr_bkt_get_shbt(lock_sema)
176 [ # # ]: 0 : || !timr_bkt_get_nent(lock_sema)) {
177 : : timr_bkt_dec_lock(bkt);
178 : 0 : tim->impl_opaque[1] = tim->impl_opaque[0] = 0;
179 : 0 : return -ENOENT;
180 : : }
181 : :
182 : 0 : entry->w0 = entry->wqe = 0;
183 : : timr_bkt_dec_lock(bkt);
184 : :
185 : 0 : tim->state = RTE_EVENT_TIMER_CANCELED;
186 : 0 : tim->impl_opaque[1] = tim->impl_opaque[0] = 0;
187 : 0 : return 0;
188 : : }
189 : :
190 : : static inline struct tim_mem_entry *
191 : 0 : timvf_refill_chunk_generic(struct tim_mem_bucket * const bkt,
192 : : struct timvf_ring * const timr)
193 : : {
194 : : struct tim_mem_entry *chunk;
195 : :
196 [ # # # # ]: 0 : if (bkt->nb_entry || !bkt->first_chunk) {
197 [ # # # # ]: 0 : if (unlikely(rte_mempool_get(timr->chunk_pool,
198 : : (void **)&chunk))) {
199 : : return NULL;
200 : : }
201 [ # # ]: 0 : if (bkt->nb_entry) {
202 : 0 : *(uint64_t *)(((struct tim_mem_entry *)(uintptr_t)
203 : 0 : bkt->current_chunk) +
204 : 0 : nb_chunk_slots) =
205 : 0 : (uintptr_t) chunk;
206 : : } else {
207 : 0 : bkt->first_chunk = (uintptr_t) chunk;
208 : : }
209 : : } else {
210 : 0 : chunk = timr_clr_bkt(timr, bkt);
211 : 0 : bkt->first_chunk = (uintptr_t)chunk;
212 : : }
213 : 0 : *(uint64_t *)(chunk + nb_chunk_slots) = 0;
214 : :
215 : 0 : return chunk;
216 : : }
217 : :
218 : : static inline struct tim_mem_entry *
219 : 0 : timvf_refill_chunk_fpa(struct tim_mem_bucket * const bkt,
220 : : struct timvf_ring * const timr)
221 : : {
222 : : struct tim_mem_entry *chunk;
223 : :
224 [ # # # # ]: 0 : if (unlikely(rte_mempool_get(timr->chunk_pool, (void **)&chunk)))
225 : : return NULL;
226 : :
227 : 0 : *(uint64_t *)(chunk + nb_chunk_slots) = 0;
228 [ # # ]: 0 : if (bkt->nb_entry) {
229 : 0 : *(uint64_t *)(((struct tim_mem_entry *)(uintptr_t)
230 : 0 : bkt->current_chunk) +
231 : 0 : nb_chunk_slots) =
232 : 0 : (uintptr_t) chunk;
233 : : } else {
234 : 0 : bkt->first_chunk = (uintptr_t) chunk;
235 : : }
236 : :
237 : : return chunk;
238 : : }
239 : :
240 : : static inline struct tim_mem_bucket *
241 : 0 : timvf_get_target_bucket(struct timvf_ring * const timr, const uint32_t rel_bkt)
242 : : {
243 : 0 : const uint64_t bkt_cyc = rte_rdtsc() - timr->ring_start_cyc;
244 : 0 : const uint32_t bucket = rte_reciprocal_divide_u64(bkt_cyc,
245 : 0 : &timr->fast_div) + rel_bkt;
246 : 0 : const uint32_t tbkt_id = timr->get_target_bkt(bucket,
247 : : timr->nb_bkts);
248 : 0 : return &timr->bkt[tbkt_id];
249 : : }
250 : :
251 : : /* Single producer functions. */
252 : : static inline int
253 : 0 : timvf_add_entry_sp(struct timvf_ring * const timr, const uint32_t rel_bkt,
254 : : struct rte_event_timer * const tim,
255 : : const struct tim_mem_entry * const pent)
256 : : {
257 : : int16_t rem;
258 : : uint64_t lock_sema;
259 : : struct tim_mem_bucket *bkt;
260 : : struct tim_mem_entry *chunk;
261 : :
262 : :
263 : 0 : bkt = timvf_get_target_bucket(timr, rel_bkt);
264 : 0 : __retry:
265 : : /*Get Bucket sema*/
266 : : lock_sema = timr_bkt_fetch_sema(bkt);
267 : : /* Bucket related checks. */
268 [ # # ]: 0 : if (unlikely(timr_bkt_get_hbt(lock_sema)))
269 : 0 : goto __retry;
270 : :
271 : : /* Insert the work. */
272 : : rem = timr_bkt_fetch_rem(lock_sema);
273 : :
274 [ # # ]: 0 : if (!rem) {
275 : 0 : chunk = timr->refill_chunk(bkt, timr);
276 [ # # ]: 0 : if (unlikely(chunk == NULL)) {
277 : : timr_bkt_set_rem(bkt, 0);
278 : 0 : tim->impl_opaque[0] = tim->impl_opaque[1] = 0;
279 : 0 : tim->state = RTE_EVENT_TIMER_ERROR;
280 : 0 : return -ENOMEM;
281 : : }
282 : 0 : bkt->current_chunk = (uintptr_t) chunk;
283 : : timr_bkt_set_rem(bkt, nb_chunk_slots - 1);
284 : : } else {
285 : 0 : chunk = (struct tim_mem_entry *)(uintptr_t)bkt->current_chunk;
286 : 0 : chunk += nb_chunk_slots - rem;
287 : : }
288 : : /* Copy work entry. */
289 : 0 : *chunk = *pent;
290 : : timr_bkt_inc_nent(bkt);
291 : :
292 : 0 : tim->impl_opaque[0] = (uintptr_t)chunk;
293 : 0 : tim->impl_opaque[1] = (uintptr_t)bkt;
294 : 0 : tim->state = RTE_EVENT_TIMER_ARMED;
295 : 0 : return 0;
296 : : }
297 : :
298 : : /* Multi producer functions. */
299 : : static inline int
300 : 0 : timvf_add_entry_mp(struct timvf_ring * const timr, const uint32_t rel_bkt,
301 : : struct rte_event_timer * const tim,
302 : : const struct tim_mem_entry * const pent)
303 : : {
304 : : int16_t rem;
305 : : uint64_t lock_sema;
306 : : struct tim_mem_bucket *bkt;
307 : : struct tim_mem_entry *chunk;
308 : :
309 : : __retry:
310 : 0 : bkt = timvf_get_target_bucket(timr, rel_bkt);
311 : : /* Bucket related checks. */
312 : : /*Get Bucket sema*/
313 : : lock_sema = timr_bkt_fetch_sema_lock(bkt);
314 [ # # ]: 0 : if (unlikely(timr_bkt_get_shbt(lock_sema))) {
315 : : timr_bkt_dec_lock(bkt);
316 : 0 : goto __retry;
317 : : }
318 : :
319 : : rem = timr_bkt_fetch_rem(lock_sema);
320 : :
321 [ # # ]: 0 : if (rem < 0) {
322 : : /* goto diff bucket. */
323 : : timr_bkt_dec_lock(bkt);
324 : 0 : goto __retry;
325 [ # # ]: 0 : } else if (!rem) {
326 : : /*Only one thread can be here*/
327 : 0 : chunk = timr->refill_chunk(bkt, timr);
328 [ # # ]: 0 : if (unlikely(chunk == NULL)) {
329 : : timr_bkt_set_rem(bkt, 0);
330 : : timr_bkt_dec_lock(bkt);
331 : 0 : tim->impl_opaque[0] = tim->impl_opaque[1] = 0;
332 : 0 : tim->state = RTE_EVENT_TIMER_ERROR;
333 : 0 : return -ENOMEM;
334 : : }
335 : 0 : bkt->current_chunk = (uintptr_t) chunk;
336 : : timr_bkt_set_rem(bkt, nb_chunk_slots - 1);
337 : : } else {
338 : 0 : chunk = (struct tim_mem_entry *)(uintptr_t)bkt->current_chunk;
339 : 0 : chunk += nb_chunk_slots - rem;
340 : : }
341 : : /* Copy work entry. */
342 : 0 : *chunk = *pent;
343 : : timr_bkt_inc_nent(bkt);
344 : : timr_bkt_dec_lock(bkt);
345 : :
346 : 0 : tim->impl_opaque[0] = (uintptr_t)chunk;
347 : 0 : tim->impl_opaque[1] = (uintptr_t)bkt;
348 : 0 : tim->state = RTE_EVENT_TIMER_ARMED;
349 : 0 : return 0;
350 : : }
351 : :
352 : : static inline uint16_t
353 : : timvf_cpy_wrk(uint16_t index, uint16_t cpy_lmt,
354 : : struct tim_mem_entry *chunk,
355 : : struct rte_event_timer ** const tim,
356 : : const struct tim_mem_entry * const ents,
357 : : const struct tim_mem_bucket * const bkt)
358 : : {
359 [ # # # # : 0 : for (; index < cpy_lmt; index++) {
# # ]
360 : 0 : *chunk = *(ents + index);
361 : 0 : tim[index]->impl_opaque[0] = (uintptr_t)chunk++;
362 : 0 : tim[index]->impl_opaque[1] = (uintptr_t)bkt;
363 : 0 : tim[index]->state = RTE_EVENT_TIMER_ARMED;
364 : : }
365 : :
366 : : return index;
367 : : }
368 : :
369 : : /* Burst mode functions */
370 : : static inline int
371 : 0 : timvf_add_entry_brst(struct timvf_ring * const timr, const uint16_t rel_bkt,
372 : : struct rte_event_timer ** const tim,
373 : : const struct tim_mem_entry *ents,
374 : : const uint16_t nb_timers)
375 : : {
376 : : int16_t rem;
377 : : int16_t crem;
378 : : uint8_t lock_cnt;
379 : : uint16_t index = 0;
380 : : uint16_t chunk_remainder;
381 : : uint64_t lock_sema;
382 : : struct tim_mem_bucket *bkt;
383 : : struct tim_mem_entry *chunk;
384 : :
385 : : __retry:
386 : 0 : bkt = timvf_get_target_bucket(timr, rel_bkt);
387 : :
388 : : /* Only one thread beyond this. */
389 : : lock_sema = timr_bkt_inc_lock(bkt);
390 : 0 : lock_cnt = (uint8_t)
391 : 0 : ((lock_sema >> TIM_BUCKET_W1_S_LOCK) & TIM_BUCKET_W1_M_LOCK);
392 : :
393 [ # # ]: 0 : if (lock_cnt) {
394 : : timr_bkt_dec_lock(bkt);
395 : 0 : goto __retry;
396 : : }
397 : :
398 : : /* Bucket related checks. */
399 [ # # ]: 0 : if (unlikely(timr_bkt_get_hbt(lock_sema))) {
400 : : timr_bkt_dec_lock(bkt);
401 : 0 : goto __retry;
402 : : }
403 : :
404 : 0 : chunk_remainder = timr_bkt_fetch_rem(lock_sema);
405 : 0 : rem = chunk_remainder - nb_timers;
406 [ # # ]: 0 : if (rem < 0) {
407 : 0 : crem = nb_chunk_slots - chunk_remainder;
408 [ # # ]: 0 : if (chunk_remainder && crem) {
409 : 0 : chunk = ((struct tim_mem_entry *)
410 : 0 : (uintptr_t)bkt->current_chunk) + crem;
411 : :
412 : 0 : index = timvf_cpy_wrk(index, chunk_remainder,
413 : : chunk, tim, ents, bkt);
414 : : timr_bkt_sub_rem(bkt, chunk_remainder);
415 : 0 : timr_bkt_add_nent(bkt, chunk_remainder);
416 : : }
417 : 0 : rem = nb_timers - chunk_remainder;
418 : : ents = ents + chunk_remainder;
419 : :
420 : 0 : chunk = timr->refill_chunk(bkt, timr);
421 [ # # ]: 0 : if (unlikely(chunk == NULL)) {
422 : : timr_bkt_dec_lock(bkt);
423 : 0 : rte_errno = ENOMEM;
424 : 0 : tim[index]->state = RTE_EVENT_TIMER_ERROR;
425 : 0 : return crem;
426 : : }
427 : 0 : *(uint64_t *)(chunk + nb_chunk_slots) = 0;
428 : 0 : bkt->current_chunk = (uintptr_t) chunk;
429 : :
430 : : index = timvf_cpy_wrk(index, nb_timers, chunk, tim, ents, bkt);
431 : 0 : timr_bkt_set_rem(bkt, nb_chunk_slots - rem);
432 : 0 : timr_bkt_add_nent(bkt, rem);
433 : : } else {
434 : 0 : chunk = (struct tim_mem_entry *)(uintptr_t)bkt->current_chunk;
435 : 0 : chunk += (nb_chunk_slots - chunk_remainder);
436 : :
437 : 0 : index = timvf_cpy_wrk(index, nb_timers,
438 : : chunk, tim, ents, bkt);
439 : : timr_bkt_sub_rem(bkt, nb_timers);
440 : 0 : timr_bkt_add_nent(bkt, nb_timers);
441 : : }
442 : :
443 : : timr_bkt_dec_lock(bkt);
444 : 0 : return nb_timers;
445 : : }
|