1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2021 Marvell.
5 #ifndef __CNXK_TIM_EVDEV_H__
6 #define __CNXK_TIM_EVDEV_H__
13 #include <eventdev_pmd_pci.h>
14 #include <rte_event_timer_adapter.h>
15 #include <rte_malloc.h>
16 #include <rte_memzone.h>
17 #include <rte_reciprocal.h>
21 #define NSECPERSEC 1E9
22 #define USECPERSEC 1E6
23 #define TICK2NSEC(__tck, __freq) (((__tck)*NSECPERSEC) / (__freq))
25 #define CNXK_TIM_EVDEV_NAME cnxk_tim_eventdev
26 #define CNXK_TIM_MAX_BUCKETS (0xFFFFF)
27 #define CNXK_TIM_RING_DEF_CHUNK_SZ (4096)
28 #define CNXK_TIM_CHUNK_ALIGNMENT (16)
29 #define CNXK_TIM_MAX_BURST \
30 (RTE_CACHE_LINE_SIZE / CNXK_TIM_CHUNK_ALIGNMENT)
31 #define CNXK_TIM_NB_CHUNK_SLOTS(sz) (((sz) / CNXK_TIM_CHUNK_ALIGNMENT) - 1)
32 #define CNXK_TIM_MIN_CHUNK_SLOTS (0x1)
33 #define CNXK_TIM_MAX_CHUNK_SLOTS (0x1FFE)
34 #define CNXK_TIM_MAX_POOL_CACHE_SZ (128)
36 #define CN9K_TIM_MIN_TMO_TKS (256)
38 #define CNXK_TIM_DISABLE_NPA "tim_disable_npa"
39 #define CNXK_TIM_CHNK_SLOTS "tim_chnk_slots"
40 #define CNXK_TIM_STATS_ENA "tim_stats_ena"
41 #define CNXK_TIM_RINGS_LMT "tim_rings_lmt"
42 #define CNXK_TIM_RING_CTL "tim_ring_ctl"
44 #define CNXK_TIM_SP 0x1
45 #define CNXK_TIM_MP 0x2
46 #define CNXK_TIM_ENA_FB 0x10
47 #define CNXK_TIM_ENA_DFB 0x20
48 #define CNXK_TIM_ENA_STATS 0x40
50 #define TIM_BUCKET_W1_S_CHUNK_REMAINDER (48)
51 #define TIM_BUCKET_W1_M_CHUNK_REMAINDER \
52 ((1ULL << (64 - TIM_BUCKET_W1_S_CHUNK_REMAINDER)) - 1)
53 #define TIM_BUCKET_W1_S_LOCK (40)
54 #define TIM_BUCKET_W1_M_LOCK \
55 ((1ULL << (TIM_BUCKET_W1_S_CHUNK_REMAINDER - TIM_BUCKET_W1_S_LOCK)) - 1)
56 #define TIM_BUCKET_W1_S_RSVD (35)
57 #define TIM_BUCKET_W1_S_BSK (34)
58 #define TIM_BUCKET_W1_M_BSK \
59 ((1ULL << (TIM_BUCKET_W1_S_RSVD - TIM_BUCKET_W1_S_BSK)) - 1)
60 #define TIM_BUCKET_W1_S_HBT (33)
61 #define TIM_BUCKET_W1_M_HBT \
62 ((1ULL << (TIM_BUCKET_W1_S_BSK - TIM_BUCKET_W1_S_HBT)) - 1)
63 #define TIM_BUCKET_W1_S_SBT (32)
64 #define TIM_BUCKET_W1_M_SBT \
65 ((1ULL << (TIM_BUCKET_W1_S_HBT - TIM_BUCKET_W1_S_SBT)) - 1)
66 #define TIM_BUCKET_W1_S_NUM_ENTRIES (0)
67 #define TIM_BUCKET_W1_M_NUM_ENTRIES \
68 ((1ULL << (TIM_BUCKET_W1_S_SBT - TIM_BUCKET_W1_S_NUM_ENTRIES)) - 1)
70 #define TIM_BUCKET_SEMA (TIM_BUCKET_CHUNK_REMAIN)
72 #define TIM_BUCKET_CHUNK_REMAIN \
73 (TIM_BUCKET_W1_M_CHUNK_REMAINDER << TIM_BUCKET_W1_S_CHUNK_REMAINDER)
75 #define TIM_BUCKET_LOCK (TIM_BUCKET_W1_M_LOCK << TIM_BUCKET_W1_S_LOCK)
77 #define TIM_BUCKET_SEMA_WLOCK \
78 (TIM_BUCKET_CHUNK_REMAIN | (1ull << TIM_BUCKET_W1_S_LOCK))
84 uint16_t enable_stats;
87 struct cnxk_tim_evdev {
89 struct rte_eventdev *event_dev;
95 uint32_t min_ring_cnt;
97 uint16_t ring_ctl_cnt;
98 struct cnxk_tim_ctl *ring_ctl_data;
101 enum cnxk_tim_clk_src {
102 CNXK_TIM_CLK_SRC_10NS = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
103 CNXK_TIM_CLK_SRC_GPIO = RTE_EVENT_TIMER_ADAPTER_EXT_CLK0,
104 CNXK_TIM_CLK_SRC_GTI = RTE_EVENT_TIMER_ADAPTER_EXT_CLK1,
105 CNXK_TIM_CLK_SRC_PTP = RTE_EVENT_TIMER_ADAPTER_EXT_CLK2,
108 struct cnxk_tim_bkt {
109 uint64_t first_chunk;
119 int16_t chunk_remainder;
122 uint64_t current_chunk;
126 struct cnxk_tim_ring {
128 uint16_t nb_chunk_slots;
130 uint64_t last_updt_cyc;
131 uint64_t ring_start_cyc;
134 struct cnxk_tim_bkt *bkt;
135 struct rte_mempool *chunk_pool;
136 struct rte_reciprocal_u64 fast_div;
137 struct rte_reciprocal_u64 fast_bkt;
139 uint8_t prod_type_sp;
140 uint8_t enable_stats;
150 enum cnxk_tim_clk_src clk_src;
151 } __rte_cache_aligned;
153 struct cnxk_tim_ent {
158 static inline struct cnxk_tim_evdev *
159 cnxk_tim_priv_get(void)
161 const struct rte_memzone *mz;
163 mz = rte_memzone_lookup(RTE_STR(CNXK_TIM_EVDEV_NAME));
170 static inline uint64_t
171 cnxk_tim_min_tmo_ticks(uint64_t freq)
173 if (roc_model_runtime_is_cn9k())
174 return CN9K_TIM_MIN_TMO_TKS;
175 else /* CN10K min tick is of 1us */
176 return freq / USECPERSEC;
179 static inline uint64_t
180 cnxk_tim_min_resolution_ns(uint64_t freq)
182 return NSECPERSEC / freq;
185 static inline enum roc_tim_clk_src
186 cnxk_tim_convert_clk_src(enum cnxk_tim_clk_src clk_src)
189 case RTE_EVENT_TIMER_ADAPTER_CPU_CLK:
190 return roc_model_runtime_is_cn9k() ? ROC_TIM_CLK_SRC_10NS :
193 return ROC_TIM_CLK_SRC_INVALID;
197 #ifdef RTE_ARCH_ARM64
198 static inline uint64_t
199 cnxk_tim_cntvct(void)
203 asm volatile("mrs %0, cntvct_el0" : "=r"(tsc));
207 static inline uint64_t
208 cnxk_tim_cntfrq(void)
212 asm volatile("mrs %0, cntfrq_el0" : "=r"(freq));
216 static inline uint64_t
217 cnxk_tim_cntvct(void)
222 static inline uint64_t
223 cnxk_tim_cntfrq(void)
229 #define TIM_ARM_FASTPATH_MODES \
230 FP(sp, 0, 0, 0, CNXK_TIM_ENA_DFB | CNXK_TIM_SP) \
231 FP(mp, 0, 0, 1, CNXK_TIM_ENA_DFB | CNXK_TIM_MP) \
232 FP(fb_sp, 0, 1, 0, CNXK_TIM_ENA_FB | CNXK_TIM_SP) \
233 FP(fb_mp, 0, 1, 1, CNXK_TIM_ENA_FB | CNXK_TIM_MP) \
234 FP(stats_sp, 1, 0, 0, \
235 CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_DFB | CNXK_TIM_SP) \
236 FP(stats_mp, 1, 0, 1, \
237 CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_DFB | CNXK_TIM_MP) \
238 FP(stats_fb_sp, 1, 1, 0, \
239 CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_FB | CNXK_TIM_SP) \
240 FP(stats_fb_mp, 1, 1, 1, \
241 CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_FB | CNXK_TIM_MP)
243 #define TIM_ARM_TMO_FASTPATH_MODES \
244 FP(dfb, 0, 0, CNXK_TIM_ENA_DFB) \
245 FP(fb, 0, 1, CNXK_TIM_ENA_FB) \
246 FP(stats_dfb, 1, 0, CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_DFB) \
247 FP(stats_fb, 1, 1, CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_FB)
249 #define FP(_name, _f3, _f2, _f1, flags) \
250 uint16_t cnxk_tim_arm_burst_##_name( \
251 const struct rte_event_timer_adapter *adptr, \
252 struct rte_event_timer **tim, const uint16_t nb_timers);
253 TIM_ARM_FASTPATH_MODES
256 #define FP(_name, _f2, _f1, flags) \
257 uint16_t cnxk_tim_arm_tmo_tick_burst_##_name( \
258 const struct rte_event_timer_adapter *adptr, \
259 struct rte_event_timer **tim, const uint64_t timeout_tick, \
260 const uint16_t nb_timers);
261 TIM_ARM_TMO_FASTPATH_MODES
265 cnxk_tim_timer_cancel_burst(const struct rte_event_timer_adapter *adptr,
266 struct rte_event_timer **tim,
267 const uint16_t nb_timers);
269 int cnxk_tim_caps_get(const struct rte_eventdev *dev, uint64_t flags,
271 const struct event_timer_adapter_ops **ops);
273 void cnxk_tim_init(struct roc_sso *sso);
274 void cnxk_tim_fini(void);
276 #endif /* __CNXK_TIM_EVDEV_H__ */