event/octeontx2: add TIM bucket operations
[dpdk.git] / drivers / event / octeontx2 / otx2_tim_worker.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #ifndef __OTX2_TIM_WORKER_H__
6 #define __OTX2_TIM_WORKER_H__
7
8 #include "otx2_tim_evdev.h"
9
10 static inline int16_t
11 tim_bkt_fetch_rem(uint64_t w1)
12 {
13         return (w1 >> TIM_BUCKET_W1_S_CHUNK_REMAINDER) &
14                 TIM_BUCKET_W1_M_CHUNK_REMAINDER;
15 }
16
17 static inline int16_t
18 tim_bkt_get_rem(struct otx2_tim_bkt *bktp)
19 {
20         return __atomic_load_n(&bktp->chunk_remainder, __ATOMIC_ACQUIRE);
21 }
22
23 static inline void
24 tim_bkt_set_rem(struct otx2_tim_bkt *bktp, uint16_t v)
25 {
26         __atomic_store_n(&bktp->chunk_remainder, v, __ATOMIC_RELAXED);
27 }
28
29 static inline void
30 tim_bkt_sub_rem(struct otx2_tim_bkt *bktp, uint16_t v)
31 {
32         __atomic_fetch_sub(&bktp->chunk_remainder, v, __ATOMIC_RELAXED);
33 }
34
35 static inline uint8_t
36 tim_bkt_get_hbt(uint64_t w1)
37 {
38         return (w1 >> TIM_BUCKET_W1_S_HBT) & TIM_BUCKET_W1_M_HBT;
39 }
40
41 static inline uint8_t
42 tim_bkt_get_bsk(uint64_t w1)
43 {
44         return (w1 >> TIM_BUCKET_W1_S_BSK) & TIM_BUCKET_W1_M_BSK;
45 }
46
47 static inline uint64_t
48 tim_bkt_clr_bsk(struct otx2_tim_bkt *bktp)
49 {
50         /* Clear everything except lock. */
51         const uint64_t v = TIM_BUCKET_W1_M_LOCK << TIM_BUCKET_W1_S_LOCK;
52
53         return __atomic_fetch_and(&bktp->w1, v, __ATOMIC_ACQ_REL);
54 }
55
56 static inline uint64_t
57 tim_bkt_fetch_sema_lock(struct otx2_tim_bkt *bktp)
58 {
59         return __atomic_fetch_add(&bktp->w1, TIM_BUCKET_SEMA_WLOCK,
60                         __ATOMIC_ACQUIRE);
61 }
62
63 static inline uint64_t
64 tim_bkt_fetch_sema(struct otx2_tim_bkt *bktp)
65 {
66         return __atomic_fetch_add(&bktp->w1, TIM_BUCKET_SEMA, __ATOMIC_RELAXED);
67 }
68
69 static inline uint64_t
70 tim_bkt_inc_lock(struct otx2_tim_bkt *bktp)
71 {
72         const uint64_t v = 1ull << TIM_BUCKET_W1_S_LOCK;
73
74         return __atomic_fetch_add(&bktp->w1, v, __ATOMIC_ACQUIRE);
75 }
76
77 static inline void
78 tim_bkt_dec_lock(struct otx2_tim_bkt *bktp)
79 {
80         __atomic_add_fetch(&bktp->lock, 0xff, __ATOMIC_RELEASE);
81 }
82
83 static inline uint32_t
84 tim_bkt_get_nent(uint64_t w1)
85 {
86         return (w1 >> TIM_BUCKET_W1_S_NUM_ENTRIES) &
87                 TIM_BUCKET_W1_M_NUM_ENTRIES;
88 }
89
90 static inline void
91 tim_bkt_inc_nent(struct otx2_tim_bkt *bktp)
92 {
93         __atomic_add_fetch(&bktp->nb_entry, 1, __ATOMIC_RELAXED);
94 }
95
96 static inline void
97 tim_bkt_add_nent(struct otx2_tim_bkt *bktp, uint32_t v)
98 {
99         __atomic_add_fetch(&bktp->nb_entry, v, __ATOMIC_RELAXED);
100 }
101
102 static inline uint64_t
103 tim_bkt_clr_nent(struct otx2_tim_bkt *bktp)
104 {
105         const uint64_t v = ~(TIM_BUCKET_W1_M_NUM_ENTRIES <<
106                         TIM_BUCKET_W1_S_NUM_ENTRIES);
107
108         return __atomic_and_fetch(&bktp->w1, v, __ATOMIC_ACQ_REL);
109 }
110
111 #endif /* __OTX2_TIM_WORKER_H__ */