event/cnxk: add TIM bucket operations
[dpdk.git] / drivers / event / cnxk / cnxk_tim_worker.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #ifndef __CNXK_TIM_WORKER_H__
6 #define __CNXK_TIM_WORKER_H__
7
8 #include "cnxk_tim_evdev.h"
9
10 static inline uint8_t
11 cnxk_tim_bkt_fetch_lock(uint64_t w1)
12 {
13         return (w1 >> TIM_BUCKET_W1_S_LOCK) & TIM_BUCKET_W1_M_LOCK;
14 }
15
16 static inline int16_t
17 cnxk_tim_bkt_fetch_rem(uint64_t w1)
18 {
19         return (w1 >> TIM_BUCKET_W1_S_CHUNK_REMAINDER) &
20                TIM_BUCKET_W1_M_CHUNK_REMAINDER;
21 }
22
23 static inline int16_t
24 cnxk_tim_bkt_get_rem(struct cnxk_tim_bkt *bktp)
25 {
26         return __atomic_load_n(&bktp->chunk_remainder, __ATOMIC_ACQUIRE);
27 }
28
29 static inline void
30 cnxk_tim_bkt_set_rem(struct cnxk_tim_bkt *bktp, uint16_t v)
31 {
32         __atomic_store_n(&bktp->chunk_remainder, v, __ATOMIC_RELAXED);
33 }
34
35 static inline void
36 cnxk_tim_bkt_sub_rem(struct cnxk_tim_bkt *bktp, uint16_t v)
37 {
38         __atomic_fetch_sub(&bktp->chunk_remainder, v, __ATOMIC_RELAXED);
39 }
40
41 static inline uint8_t
42 cnxk_tim_bkt_get_hbt(uint64_t w1)
43 {
44         return (w1 >> TIM_BUCKET_W1_S_HBT) & TIM_BUCKET_W1_M_HBT;
45 }
46
47 static inline uint8_t
48 cnxk_tim_bkt_get_bsk(uint64_t w1)
49 {
50         return (w1 >> TIM_BUCKET_W1_S_BSK) & TIM_BUCKET_W1_M_BSK;
51 }
52
53 static inline uint64_t
54 cnxk_tim_bkt_clr_bsk(struct cnxk_tim_bkt *bktp)
55 {
56         /* Clear everything except lock. */
57         const uint64_t v = TIM_BUCKET_W1_M_LOCK << TIM_BUCKET_W1_S_LOCK;
58
59         return __atomic_fetch_and(&bktp->w1, v, __ATOMIC_ACQ_REL);
60 }
61
62 static inline uint64_t
63 cnxk_tim_bkt_fetch_sema_lock(struct cnxk_tim_bkt *bktp)
64 {
65         return __atomic_fetch_add(&bktp->w1, TIM_BUCKET_SEMA_WLOCK,
66                                   __ATOMIC_ACQUIRE);
67 }
68
69 static inline uint64_t
70 cnxk_tim_bkt_fetch_sema(struct cnxk_tim_bkt *bktp)
71 {
72         return __atomic_fetch_add(&bktp->w1, TIM_BUCKET_SEMA, __ATOMIC_RELAXED);
73 }
74
75 static inline uint64_t
76 cnxk_tim_bkt_inc_lock(struct cnxk_tim_bkt *bktp)
77 {
78         const uint64_t v = 1ull << TIM_BUCKET_W1_S_LOCK;
79
80         return __atomic_fetch_add(&bktp->w1, v, __ATOMIC_ACQUIRE);
81 }
82
83 static inline void
84 cnxk_tim_bkt_dec_lock(struct cnxk_tim_bkt *bktp)
85 {
86         __atomic_fetch_sub(&bktp->lock, 1, __ATOMIC_RELEASE);
87 }
88
89 static inline void
90 cnxk_tim_bkt_dec_lock_relaxed(struct cnxk_tim_bkt *bktp)
91 {
92         __atomic_fetch_sub(&bktp->lock, 1, __ATOMIC_RELAXED);
93 }
94
95 static inline uint32_t
96 cnxk_tim_bkt_get_nent(uint64_t w1)
97 {
98         return (w1 >> TIM_BUCKET_W1_S_NUM_ENTRIES) &
99                TIM_BUCKET_W1_M_NUM_ENTRIES;
100 }
101
102 static inline void
103 cnxk_tim_bkt_inc_nent(struct cnxk_tim_bkt *bktp)
104 {
105         __atomic_add_fetch(&bktp->nb_entry, 1, __ATOMIC_RELAXED);
106 }
107
108 static inline void
109 cnxk_tim_bkt_add_nent(struct cnxk_tim_bkt *bktp, uint32_t v)
110 {
111         __atomic_add_fetch(&bktp->nb_entry, v, __ATOMIC_RELAXED);
112 }
113
114 static inline uint64_t
115 cnxk_tim_bkt_clr_nent(struct cnxk_tim_bkt *bktp)
116 {
117         const uint64_t v =
118                 ~(TIM_BUCKET_W1_M_NUM_ENTRIES << TIM_BUCKET_W1_S_NUM_ENTRIES);
119
120         return __atomic_and_fetch(&bktp->w1, v, __ATOMIC_ACQ_REL);
121 }
122
123 #endif /* __CNXK_TIM_WORKER_H__ */