Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2021 Marvell.
3 : : */
4 : :
5 : : #ifndef __CNXK_TIM_EVDEV_H__
6 : : #define __CNXK_TIM_EVDEV_H__
7 : :
8 : : #include <stddef.h>
9 : : #include <stdint.h>
10 : : #include <stdlib.h>
11 : : #include <string.h>
12 : :
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>
18 : :
19 : : #include "hw/tim.h"
20 : :
21 : : #include "roc_model.h"
22 : : #include "roc_tim.h"
23 : :
24 : : #define NSECPERSEC 1E9
25 : : #define USECPERSEC 1E6
26 : : #define TICK2NSEC(__tck, __freq) (((__tck)*NSECPERSEC) / (__freq))
27 : :
28 : : #define CNXK_TIM_EVDEV_NAME cnxk_tim_eventdev
29 : : #define CNXK_TIM_MAX_BUCKETS (0xFFFFF)
30 : : #define CNXK_TIM_RING_DEF_CHUNK_SZ (1024)
31 : : #define CNXK_TIM_CHUNK_ALIGNMENT (16)
32 : : #define CNXK_TIM_MAX_BURST (16)
33 : : #define CNXK_TIM_NB_CHUNK_SLOTS(sz) (((sz) / CNXK_TIM_CHUNK_ALIGNMENT) - 1)
34 : : #define CNXK_TIM_MIN_CHUNK_SLOTS (0x1)
35 : : #define CNXK_TIM_MAX_CHUNK_SLOTS (0x1FFE)
36 : : #define CNXK_TIM_MAX_POOL_CACHE_SZ (16)
37 : :
38 : : #define CN9K_TIM_MIN_TMO_TKS (256)
39 : :
40 : : #define CNXK_TIM_DISABLE_NPA "tim_disable_npa"
41 : : #define CNXK_TIM_CHNK_SLOTS "tim_chnk_slots"
42 : : #define CNXK_TIM_STATS_ENA "tim_stats_ena"
43 : : #define CNXK_TIM_RINGS_LMT "tim_rings_lmt"
44 : : #define CNXK_TIM_RING_CTL "tim_ring_ctl"
45 : : #define CNXK_TIM_EXT_CLK "tim_eclk_freq"
46 : :
47 : : #define CNXK_TIM_SP 0x1
48 : : #define CNXK_TIM_MP 0x2
49 : : #define CNXK_TIM_ENA_FB 0x10
50 : : #define CNXK_TIM_ENA_DFB 0x20
51 : : #define CNXK_TIM_ENA_STATS 0x40
52 : :
53 : : #define TIM_BUCKET_W1_S_CHUNK_REMAINDER (48)
54 : : #define TIM_BUCKET_W1_M_CHUNK_REMAINDER \
55 : : ((1ULL << (64 - TIM_BUCKET_W1_S_CHUNK_REMAINDER)) - 1)
56 : : #define TIM_BUCKET_W1_S_LOCK (40)
57 : : #define TIM_BUCKET_W1_M_LOCK \
58 : : ((1ULL << (TIM_BUCKET_W1_S_CHUNK_REMAINDER - TIM_BUCKET_W1_S_LOCK)) - 1)
59 : : #define TIM_BUCKET_W1_S_RSVD (35)
60 : : #define TIM_BUCKET_W1_S_BSK (34)
61 : : #define TIM_BUCKET_W1_M_BSK \
62 : : ((1ULL << (TIM_BUCKET_W1_S_RSVD - TIM_BUCKET_W1_S_BSK)) - 1)
63 : : #define TIM_BUCKET_W1_S_HBT (33)
64 : : #define TIM_BUCKET_W1_M_HBT \
65 : : ((1ULL << (TIM_BUCKET_W1_S_BSK - TIM_BUCKET_W1_S_HBT)) - 1)
66 : : #define TIM_BUCKET_W1_S_SBT (32)
67 : : #define TIM_BUCKET_W1_M_SBT \
68 : : ((1ULL << (TIM_BUCKET_W1_S_HBT - TIM_BUCKET_W1_S_SBT)) - 1)
69 : : #define TIM_BUCKET_W1_S_NUM_ENTRIES (0)
70 : : #define TIM_BUCKET_W1_M_NUM_ENTRIES \
71 : : ((1ULL << (TIM_BUCKET_W1_S_SBT - TIM_BUCKET_W1_S_NUM_ENTRIES)) - 1)
72 : :
73 : : #define TIM_BUCKET_SEMA (TIM_BUCKET_CHUNK_REMAIN)
74 : :
75 : : #define TIM_BUCKET_CHUNK_REMAIN \
76 : : (TIM_BUCKET_W1_M_CHUNK_REMAINDER << TIM_BUCKET_W1_S_CHUNK_REMAINDER)
77 : :
78 : : #define TIM_BUCKET_LOCK (TIM_BUCKET_W1_M_LOCK << TIM_BUCKET_W1_S_LOCK)
79 : :
80 : : #define TIM_BUCKET_SEMA_WLOCK \
81 : : (TIM_BUCKET_CHUNK_REMAIN | (1ull << TIM_BUCKET_W1_S_LOCK))
82 : :
83 : : typedef void (*cnxk_sso_set_priv_mem_t)(const struct rte_eventdev *event_dev,
84 : : void *lookup_mem);
85 : :
86 : : struct cnxk_tim_ctl {
87 : : uint16_t ring;
88 : : uint16_t chunk_slots;
89 : : uint16_t disable_npa;
90 : : uint16_t enable_stats;
91 : : };
92 : :
93 : : struct cnxk_tim_evdev {
94 : : struct roc_tim tim;
95 : : struct rte_eventdev *event_dev;
96 : : uint16_t nb_rings;
97 : : uint32_t chunk_sz;
98 : : /* Dev args */
99 : : uint8_t disable_npa;
100 : : uint32_t chunk_slots;
101 : : uint32_t min_ring_cnt;
102 : : uint8_t enable_stats;
103 : : uint16_t ring_ctl_cnt;
104 : : uint64_t ext_clk_freq[ROC_TIM_CLK_SRC_INVALID];
105 : : struct cnxk_tim_ctl *ring_ctl_data;
106 : : };
107 : :
108 : : struct cnxk_tim_bkt {
109 : : uint64_t first_chunk;
110 : : union {
111 : : uint64_t w1;
112 : : struct {
113 : : uint32_t nb_entry;
114 : : uint8_t sbt : 1;
115 : : uint8_t hbt : 1;
116 : : uint8_t bsk : 1;
117 : : uint8_t rsvd : 5;
118 : : uint8_t lock;
119 : : int16_t chunk_remainder;
120 : : };
121 : : };
122 : : uint64_t current_chunk;
123 : : uint64_t pad;
124 : : };
125 : :
126 : : struct cnxk_tim_ring {
127 : : uint16_t nb_chunk_slots;
128 : : uint32_t nb_bkts;
129 : : uintptr_t tbase;
130 : : uint64_t (*tick_fn)(uint64_t tbase);
131 : : uint64_t ring_start_cyc;
132 : : struct cnxk_tim_bkt *bkt;
133 : : struct rte_mempool *chunk_pool;
134 : : struct rte_reciprocal_u64 fast_div;
135 : : struct rte_reciprocal_u64 fast_bkt;
136 : : uint64_t tck_int;
137 : : uint64_t arm_cnt;
138 : : uintptr_t base;
139 : : uint8_t prod_type_sp;
140 : : uint8_t enable_stats;
141 : : uint8_t disable_npa;
142 : : uint8_t ena_dfb;
143 : : uint8_t ena_periodic;
144 : : uint16_t ring_id;
145 : : uint32_t aura;
146 : : uint64_t nb_timers;
147 : : uint64_t tck_nsec;
148 : : uint64_t max_tout;
149 : : uint64_t nb_chunks;
150 : : uint64_t chunk_sz;
151 : : enum roc_tim_clk_src clk_src;
152 : : } __rte_cache_aligned;
153 : :
154 : : struct cnxk_tim_ent {
155 : : uint64_t w0;
156 : : uint64_t wqe;
157 : : };
158 : :
159 : : static inline struct cnxk_tim_evdev *
160 : : cnxk_tim_priv_get(void)
161 : : {
162 : : const struct rte_memzone *mz;
163 : :
164 : 0 : mz = rte_memzone_lookup(RTE_STR(CNXK_TIM_EVDEV_NAME));
165 [ # # # # : 0 : if (mz == NULL)
# # # # #
# # # ]
166 : : return NULL;
167 : :
168 [ # # # # : 0 : return mz->addr;
# # # # #
# # # ]
169 : : }
170 : :
171 : : static inline double
172 : : cnxk_tim_ns_per_tck(uint64_t freq)
173 : : {
174 : 0 : return (double)NSECPERSEC / freq;
175 : : }
176 : :
177 : : #ifdef RTE_ARCH_ARM64
178 : : static inline uint64_t
179 : : cnxk_tim_cntvct(uint64_t base __rte_unused)
180 : : {
181 : : uint64_t tsc;
182 : :
183 : : asm volatile("mrs %0, CNTVCT_EL0" : "=r"(tsc)::"memory");
184 : : return tsc;
185 : : }
186 : :
187 : : static inline uint64_t
188 : : cnxk_tim_cntfrq(void)
189 : : {
190 : : uint64_t freq;
191 : :
192 : : asm volatile("mrs %0, cntfrq_el0" : "=r"(freq));
193 : : return freq;
194 : : }
195 : : #else
196 : : static inline uint64_t
197 : 0 : cnxk_tim_cntvct(uint64_t base __rte_unused)
198 : : {
199 : 0 : return 0;
200 : : }
201 : :
202 : : static inline uint64_t
203 : : cnxk_tim_cntfrq(void)
204 : : {
205 : : return 0;
206 : : }
207 : : #endif
208 : :
209 : : static inline uint64_t
210 : 0 : cnxk_tim_tick_read(uint64_t tick_base)
211 : : {
212 : 0 : return plt_read64(tick_base);
213 : : }
214 : :
215 : : static inline enum roc_tim_clk_src
216 : : cnxk_tim_convert_clk_src(enum rte_event_timer_adapter_clk_src clk_src)
217 : : {
218 : : switch (clk_src) {
219 : : case RTE_EVENT_TIMER_ADAPTER_CPU_CLK:
220 : : return ROC_TIM_CLK_SRC_GTI;
221 : : case RTE_EVENT_TIMER_ADAPTER_EXT_CLK0:
222 : : return ROC_TIM_CLK_SRC_10NS;
223 : : case RTE_EVENT_TIMER_ADAPTER_EXT_CLK1:
224 : : return ROC_TIM_CLK_SRC_GPIO;
225 : : case RTE_EVENT_TIMER_ADAPTER_EXT_CLK2:
226 : : return ROC_TIM_CLK_SRC_PTP;
227 : : case RTE_EVENT_TIMER_ADAPTER_EXT_CLK3:
228 : : return roc_model_constant_is_cn9k() ? ROC_TIM_CLK_SRC_INVALID :
229 : : ROC_TIM_CLK_SRC_SYNCE;
230 : : default:
231 : : return ROC_TIM_CLK_SRC_INVALID;
232 : : }
233 : : }
234 : :
235 : : static inline uintptr_t
236 : 0 : cnxk_tim_get_tick_base(enum roc_tim_clk_src clk_src, uintptr_t base)
237 : : {
238 [ # # # # : 0 : switch (clk_src) {
# # # ]
239 : 0 : case ROC_TIM_CLK_SRC_GTI:
240 : 0 : return base + TIM_LF_FR_RN_GTI;
241 : 0 : case ROC_TIM_CLK_SRC_GPIO:
242 : 0 : return base + TIM_LF_FR_RN_GPIOS;
243 : 0 : case ROC_TIM_CLK_SRC_10NS:
244 : 0 : return base + TIM_LF_FR_RN_TENNS;
245 : 0 : case ROC_TIM_CLK_SRC_PTP:
246 : 0 : return base + TIM_LF_FR_RN_PTP;
247 : 0 : case ROC_TIM_CLK_SRC_SYNCE:
248 : 0 : return base + TIM_LF_FR_RN_SYNCE;
249 : 0 : case ROC_TIM_CLK_SRC_BTS:
250 : 0 : return base + TIM_LF_FR_RN_BTS;
251 : : default:
252 : : return ROC_TIM_CLK_SRC_INVALID;
253 : : }
254 : : }
255 : :
256 : : static inline int
257 : : cnxk_tim_get_clk_freq(struct cnxk_tim_evdev *dev, enum roc_tim_clk_src clk_src,
258 : : uint64_t *freq)
259 : : {
260 : : if (freq == NULL)
261 : : return -EINVAL;
262 : :
263 : : PLT_SET_USED(dev);
264 [ # # # # ]: 0 : switch (clk_src) {
265 : : case ROC_TIM_CLK_SRC_GTI:
266 : : *freq = cnxk_tim_cntfrq();
267 : : break;
268 : : case ROC_TIM_CLK_SRC_10NS:
269 : : *freq = 1E8;
270 : : break;
271 : 0 : case ROC_TIM_CLK_SRC_GPIO:
272 : : case ROC_TIM_CLK_SRC_PTP:
273 : : case ROC_TIM_CLK_SRC_SYNCE:
274 : 0 : *freq = dev->ext_clk_freq[clk_src];
275 : : break;
276 : : default:
277 : : return -EINVAL;
278 : : }
279 : :
280 : : return 0;
281 : : }
282 : :
283 : : #define TIM_ARM_FASTPATH_MODES \
284 : : FP(sp, 0, 0, 0, CNXK_TIM_ENA_DFB | CNXK_TIM_SP) \
285 : : FP(mp, 0, 0, 1, CNXK_TIM_ENA_DFB | CNXK_TIM_MP) \
286 : : FP(fb_sp, 0, 1, 0, CNXK_TIM_ENA_FB | CNXK_TIM_SP) \
287 : : FP(fb_mp, 0, 1, 1, CNXK_TIM_ENA_FB | CNXK_TIM_MP) \
288 : : FP(stats_sp, 1, 0, 0, \
289 : : CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_DFB | CNXK_TIM_SP) \
290 : : FP(stats_mp, 1, 0, 1, \
291 : : CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_DFB | CNXK_TIM_MP) \
292 : : FP(stats_fb_sp, 1, 1, 0, \
293 : : CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_FB | CNXK_TIM_SP) \
294 : : FP(stats_fb_mp, 1, 1, 1, \
295 : : CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_FB | CNXK_TIM_MP)
296 : :
297 : : #define TIM_ARM_TMO_FASTPATH_MODES \
298 : : FP(dfb, 0, 0, CNXK_TIM_ENA_DFB) \
299 : : FP(fb, 0, 1, CNXK_TIM_ENA_FB) \
300 : : FP(stats_dfb, 1, 0, CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_DFB) \
301 : : FP(stats_fb, 1, 1, CNXK_TIM_ENA_STATS | CNXK_TIM_ENA_FB)
302 : :
303 : : #define FP(_name, _f3, _f2, _f1, flags) \
304 : : uint16_t cnxk_tim_arm_burst_##_name( \
305 : : const struct rte_event_timer_adapter *adptr, \
306 : : struct rte_event_timer **tim, const uint16_t nb_timers);
307 : : TIM_ARM_FASTPATH_MODES
308 : : #undef FP
309 : :
310 : : #define FP(_name, _f2, _f1, flags) \
311 : : uint16_t cnxk_tim_arm_tmo_tick_burst_##_name( \
312 : : const struct rte_event_timer_adapter *adptr, \
313 : : struct rte_event_timer **tim, const uint64_t timeout_tick, \
314 : : const uint16_t nb_timers);
315 : : TIM_ARM_TMO_FASTPATH_MODES
316 : : #undef FP
317 : :
318 : : uint16_t
319 : : cnxk_tim_timer_cancel_burst(const struct rte_event_timer_adapter *adptr,
320 : : struct rte_event_timer **tim,
321 : : const uint16_t nb_timers);
322 : :
323 : : int cnxk_tim_remaining_ticks_get(const struct rte_event_timer_adapter *adapter,
324 : : const struct rte_event_timer *evtim, uint64_t *ticks_remaining);
325 : :
326 : : int cnxk_tim_caps_get(const struct rte_eventdev *dev, uint64_t flags,
327 : : uint32_t *caps,
328 : : const struct event_timer_adapter_ops **ops,
329 : : cnxk_sso_set_priv_mem_t priv_mem_fn);
330 : :
331 : : void cnxk_tim_init(struct roc_sso *sso);
332 : : void cnxk_tim_fini(void);
333 : :
334 : : #endif /* __CNXK_TIM_EVDEV_H__ */
|