Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2021 Marvell.
3 : : */
4 : :
5 : : #include <math.h>
6 : :
7 : : #include "roc_api.h"
8 : :
9 : : #include "cnxk_eventdev.h"
10 : : #include "cnxk_tim_evdev.h"
11 : :
12 : : static struct event_timer_adapter_ops cnxk_tim_ops;
13 : : static cnxk_sso_set_priv_mem_t sso_set_priv_mem_fn;
14 : :
15 : : static void
16 : 0 : cnxk_tim_chnk_pool_mem_free(struct rte_mempool_memhdr *memhdr, void *opaque)
17 : : {
18 : : RTE_SET_USED(memhdr);
19 : 0 : rte_free(opaque);
20 : 0 : }
21 : :
22 : : /* CN20K needs 256B aligned chunk buffers (cn20k-tim 14.2), other models use ROC_ALIGN. */
23 : : static inline size_t
24 : : cnxk_tim_chunk_buf_align(void)
25 : : {
26 [ # # ]: 0 : return roc_model_is_cn20k() ? CNXK_TIM_CN20K_CHUNK_BUF_ALIGN : ROC_ALIGN;
27 : : }
28 : :
29 : : /* Populate the pool from a single aligned block, like the SSO XAQ pool. */
30 : : static int
31 : 0 : cnxk_tim_chnk_pool_populate_aligned(struct rte_mempool *mp, size_t align)
32 : : {
33 : : size_t stride, len;
34 : : void *mem;
35 : : int rc;
36 : :
37 : 0 : stride = (size_t)mp->header_size + mp->elt_size + mp->trailer_size;
38 : 0 : len = stride * mp->size;
39 : :
40 : 0 : mem = rte_zmalloc_socket("cnxk_tim_chunks", len, align, mp->socket_id);
41 [ # # ]: 0 : if (mem == NULL)
42 : : return -ENOMEM;
43 : :
44 : 0 : rc = rte_mempool_populate_iova(mp, mem, rte_malloc_virt2iova(mem), len,
45 : : cnxk_tim_chnk_pool_mem_free, mem);
46 [ # # ]: 0 : if (rc <= 0) {
47 : : /* The pool only takes ownership of the memory when objects were added. */
48 : 0 : rte_free(mem);
49 [ # # ]: 0 : return rc < 0 ? rc : -ENOMEM;
50 : : }
51 : :
52 : : return 0;
53 : : }
54 : :
55 : : static int
56 : 0 : cnxk_tim_chnk_pool_create(struct cnxk_tim_ring *tim_ring,
57 : : struct rte_event_timer_adapter_conf *rcfg)
58 : : {
59 : : unsigned int mp_flags = 0;
60 : : struct rte_mempool *mp;
61 : : unsigned int cache_sz;
62 : : char pool_name[25];
63 : : size_t align, sz;
64 : : const char *ops;
65 : : int rc;
66 : :
67 : : /* Create chunk pool. */
68 [ # # ]: 0 : if (rcfg->flags & RTE_EVENT_TIMER_ADAPTER_F_SP_PUT) {
69 : : mp_flags = RTE_MEMPOOL_F_SP_PUT | RTE_MEMPOOL_F_SC_GET;
70 : 0 : plt_tim_dbg("Using single producer mode");
71 : 0 : tim_ring->prod_type_sp = true;
72 : : }
73 : :
74 : : /* One aligned block backs the pool, so objects need not be page contiguous. */
75 [ # # ]: 0 : if (tim_ring->disable_npa)
76 : 0 : mp_flags |= RTE_MEMPOOL_F_NO_IOVA_CONTIG;
77 : :
78 : 0 : snprintf(pool_name, sizeof(pool_name), "cnxk_tim_chunk_pool%d",
79 : 0 : tim_ring->ring_id);
80 : :
81 : : cache_sz = CNXK_TIM_MAX_POOL_CACHE_SZ;
82 : 0 : tim_ring->nb_chunks += (cache_sz * rte_lcore_count());
83 : :
84 : 0 : tim_ring->chunk_pool = rte_mempool_create_empty(pool_name, tim_ring->nb_chunks,
85 : 0 : tim_ring->chunk_sz, cache_sz, 0,
86 : 0 : rte_socket_id(), mp_flags);
87 [ # # ]: 0 : if (tim_ring->chunk_pool == NULL) {
88 : 0 : plt_err("Unable to create chunkpool.");
89 : 0 : return -ENOMEM;
90 : : }
91 : :
92 : : /* Align the header and pad the trailer so every chunk buffer is aligned. */
93 : : mp = tim_ring->chunk_pool;
94 : : align = cnxk_tim_chunk_buf_align();
95 : 0 : mp->header_size = RTE_ALIGN_CEIL(mp->header_size, align);
96 : 0 : sz = (size_t)mp->header_size + mp->elt_size + mp->trailer_size;
97 : 0 : mp->trailer_size += RTE_ALIGN_CEIL(sz, align) - sz;
98 : :
99 [ # # ]: 0 : if (!tim_ring->disable_npa)
100 : 0 : ops = rte_mbuf_platform_mempool_ops();
101 [ # # ]: 0 : else if (mp_flags & RTE_MEMPOOL_F_SP_PUT)
102 : : ops = "ring_sp_sc";
103 : : else
104 : : ops = "ring_mp_mc";
105 : :
106 : 0 : rc = rte_mempool_set_ops_byname(tim_ring->chunk_pool, ops, NULL);
107 [ # # ]: 0 : if (rc < 0) {
108 : 0 : plt_err("Unable to set chunkpool ops");
109 : 0 : goto free;
110 : : }
111 : :
112 [ # # ]: 0 : if (tim_ring->disable_npa) {
113 : 0 : rc = cnxk_tim_chnk_pool_populate_aligned(tim_ring->chunk_pool, align);
114 : : } else {
115 : 0 : rc = rte_mempool_populate_default(tim_ring->chunk_pool);
116 : : }
117 [ # # ]: 0 : if (rc < 0) {
118 : 0 : plt_err("Unable to populate chunkpool.");
119 : 0 : goto free;
120 : : }
121 : :
122 [ # # ]: 0 : if (!tim_ring->disable_npa) {
123 : 0 : tim_ring->aura = roc_npa_aura_handle_to_aura(tim_ring->chunk_pool->pool_id);
124 : 0 : tim_ring->ena_dfb = tim_ring->ena_periodic ? 1 : 0;
125 : : } else {
126 : 0 : tim_ring->ena_dfb = 1;
127 : : }
128 : :
129 : : return 0;
130 : :
131 : 0 : free:
132 : 0 : rte_mempool_free(tim_ring->chunk_pool);
133 : 0 : return rc;
134 : : }
135 : :
136 : : static inline bool
137 : : cnxk_tim_hwwqe_enabled(const struct cnxk_tim_evdev *dev)
138 : : {
139 [ # # # # : 0 : return dev->tim.feat.hwwqe && dev->tim.feat.hwwqe_ver != TIM_HWWQE_VER_0;
# # # # #
# # # ]
140 : : }
141 : :
142 : : static int
143 : 0 : cnxk_tim_enable_hwwqe(struct cnxk_tim_evdev *dev, struct cnxk_tim_ring *tim_ring)
144 : : {
145 : : struct roc_tim_hwwqe_cfg hwwqe_cfg;
146 : :
147 : : memset(&hwwqe_cfg, 0, sizeof(hwwqe_cfg));
148 : 0 : hwwqe_cfg.hwwqe_ena = 1;
149 : : hwwqe_cfg.grp_ena = 0;
150 : : hwwqe_cfg.flw_ctrl_ena = 0;
151 : 0 : hwwqe_cfg.result_offset = CNXK_TIM_HWWQE_RES_OFFSET_B;
152 : :
153 : 0 : tim_ring->lmt_base = dev->tim.roc_sso->lmt_base;
154 : 0 : return roc_tim_lf_config_hwwqe(&dev->tim, tim_ring->ring_id, &hwwqe_cfg);
155 : : }
156 : :
157 : : static void
158 : 0 : cnxk_tim_set_fp_ops(struct cnxk_tim_ring *tim_ring)
159 : : {
160 : : struct cnxk_tim_evdev *dev = cnxk_tim_priv_get();
161 : 0 : uint8_t prod_flag = !tim_ring->prod_type_sp;
162 : :
163 : : /* [STATS] [DFB/FB] [SP][MP]*/
164 : 0 : const rte_event_timer_arm_burst_t arm_burst[2][2][2] = {
165 : : #define FP(_name, _f3, _f2, _f1, flags) \
166 : : [_f3][_f2][_f1] = cnxk_tim_arm_burst_##_name,
167 : : TIM_ARM_FASTPATH_MODES
168 : : #undef FP
169 : : };
170 : :
171 : 0 : const rte_event_timer_arm_tmo_tick_burst_t arm_tmo_burst[2][2] = {
172 : : #define FP(_name, _f2, _f1, flags) \
173 : : [_f2][_f1] = cnxk_tim_arm_tmo_tick_burst_##_name,
174 : : TIM_ARM_TMO_FASTPATH_MODES
175 : : #undef FP
176 : : };
177 : :
178 [ # # ]: 0 : if (dev == NULL)
179 : 0 : return;
180 : :
181 [ # # ]: 0 : if (cnxk_tim_hwwqe_enabled(dev)) {
182 : 0 : cnxk_tim_ops.arm_burst = cnxk_tim_arm_burst_hwwqe;
183 : 0 : cnxk_tim_ops.arm_tmo_tick_burst = cnxk_tim_arm_tmo_burst_hwwqe;
184 : 0 : cnxk_tim_ops.cancel_burst = cnxk_tim_timer_cancel_burst_hwwqe;
185 : 0 : return;
186 : : }
187 : :
188 : 0 : cnxk_tim_ops.arm_burst =
189 : 0 : arm_burst[tim_ring->enable_stats][tim_ring->ena_dfb][prod_flag];
190 : 0 : cnxk_tim_ops.arm_tmo_tick_burst =
191 : 0 : arm_tmo_burst[tim_ring->enable_stats][tim_ring->ena_dfb];
192 : 0 : cnxk_tim_ops.cancel_burst = cnxk_tim_timer_cancel_burst;
193 : : }
194 : :
195 : : static void
196 : 0 : cnxk_tim_ring_info_get(const struct rte_event_timer_adapter *adptr,
197 : : struct rte_event_timer_adapter_info *adptr_info)
198 : : {
199 : 0 : struct cnxk_tim_ring *tim_ring = adptr->data->adapter_priv;
200 : :
201 : 0 : adptr_info->max_tmo_ns = tim_ring->max_tout;
202 : 0 : adptr_info->min_resolution_ns = tim_ring->ena_periodic ?
203 [ # # ]: 0 : tim_ring->max_tout :
204 : : tim_ring->tck_nsec;
205 : : rte_memcpy(&adptr_info->conf, &adptr->data->conf,
206 : : sizeof(struct rte_event_timer_adapter_conf));
207 : 0 : }
208 : :
209 : : static int
210 : 0 : cnxk_tim_ring_create(struct rte_event_timer_adapter *adptr)
211 : : {
212 : 0 : struct rte_event_timer_adapter_conf *rcfg = &adptr->data->conf;
213 : : struct cnxk_tim_evdev *dev = cnxk_tim_priv_get();
214 : : uint64_t min_intvl_ns, min_intvl_cyc;
215 : : struct cnxk_tim_ring *tim_ring;
216 : : enum roc_tim_clk_src clk_src;
217 : : uint64_t clk_freq = 0;
218 : : int i, rc;
219 : :
220 [ # # ]: 0 : if (dev == NULL)
221 : : return -ENODEV;
222 : :
223 [ # # ]: 0 : if (adptr->data->id >= dev->nb_rings)
224 : : return -ENODEV;
225 : :
226 : 0 : tim_ring = rte_zmalloc("cnxk_tim_prv", sizeof(struct cnxk_tim_ring), 0);
227 [ # # ]: 0 : if (tim_ring == NULL)
228 : : return -ENOMEM;
229 : :
230 : 0 : rc = roc_tim_lf_alloc(&dev->tim, adptr->data->id, NULL);
231 [ # # ]: 0 : if (rc < 0) {
232 : 0 : plt_err("Failed to create timer ring");
233 : 0 : goto tim_ring_free;
234 : : }
235 : :
236 [ # # ]: 0 : clk_src = cnxk_tim_convert_clk_src(rcfg->clk_src);
237 [ # # ]: 0 : if (clk_src == ROC_TIM_CLK_SRC_INVALID) {
238 : 0 : plt_err("Invalid clock source");
239 : 0 : goto tim_hw_free;
240 : : }
241 : :
242 : : rc = cnxk_tim_get_clk_freq(dev, clk_src, &clk_freq);
243 : : if (rc < 0) {
244 : 0 : plt_err("Failed to get clock frequency");
245 : 0 : goto tim_hw_free;
246 : : }
247 : :
248 : 0 : rc = roc_tim_lf_interval(&dev->tim, clk_src, clk_freq, &min_intvl_ns,
249 : : &min_intvl_cyc);
250 [ # # ]: 0 : if (rc < 0) {
251 : 0 : plt_err("Failed to get min interval details");
252 : 0 : goto tim_hw_free;
253 : : }
254 : :
255 [ # # ]: 0 : if (rcfg->flags & RTE_EVENT_TIMER_ADAPTER_F_PERIODIC) {
256 : : /* Use 2 buckets to avoid contention */
257 : 0 : rcfg->timer_tick_ns /= 2;
258 : 0 : tim_ring->ena_periodic = 1;
259 : : }
260 : :
261 [ # # ]: 0 : if (rcfg->timer_tick_ns < min_intvl_ns) {
262 [ # # ]: 0 : if (rcfg->flags & RTE_EVENT_TIMER_ADAPTER_F_ADJUST_RES) {
263 : 0 : rcfg->timer_tick_ns = min_intvl_ns;
264 : : } else {
265 : : rc = -ERANGE;
266 : 0 : goto tim_hw_free;
267 : : }
268 : : }
269 : :
270 [ # # ]: 0 : if (tim_ring->ena_periodic)
271 : 0 : rcfg->max_tmo_ns = rcfg->timer_tick_ns * 2;
272 : :
273 [ # # ]: 0 : if (rcfg->timer_tick_ns > rcfg->max_tmo_ns) {
274 : 0 : plt_err("Max timeout to too high");
275 : : rc = -ERANGE;
276 : 0 : goto tim_hw_free;
277 : : }
278 : :
279 : 0 : tim_ring->tck_int = round((double)rcfg->timer_tick_ns /
280 : : cnxk_tim_ns_per_tck(clk_freq));
281 : 0 : tim_ring->tck_nsec =
282 : 0 : ceil(tim_ring->tck_int * cnxk_tim_ns_per_tck(clk_freq));
283 : :
284 : 0 : tim_ring->ring_id = adptr->data->id;
285 : 0 : tim_ring->clk_src = clk_src;
286 : 0 : tim_ring->max_tout = rcfg->max_tmo_ns;
287 : 0 : tim_ring->nb_bkts = (tim_ring->max_tout / tim_ring->tck_nsec);
288 : 0 : tim_ring->nb_timers = rcfg->nb_timers;
289 : 0 : tim_ring->chunk_sz = dev->chunk_sz;
290 : 0 : tim_ring->disable_npa = dev->disable_npa;
291 : 0 : tim_ring->enable_stats = dev->enable_stats;
292 : 0 : tim_ring->base = roc_tim_lf_base_get(&dev->tim, tim_ring->ring_id);
293 [ # # ]: 0 : tim_ring->tbase = cnxk_tim_get_tick_base(clk_src, tim_ring->base);
294 : :
295 [ # # # # ]: 0 : if (roc_model_is_cn9k() && (tim_ring->clk_src == ROC_TIM_CLK_SRC_GTI))
296 : 0 : tim_ring->tick_fn = cnxk_tim_cntvct;
297 : : else
298 : 0 : tim_ring->tick_fn = cnxk_tim_tick_read;
299 : :
300 [ # # ]: 0 : for (i = 0; i < dev->ring_ctl_cnt; i++) {
301 : 0 : struct cnxk_tim_ctl *ring_ctl = &dev->ring_ctl_data[i];
302 : :
303 [ # # ]: 0 : if (ring_ctl->ring == tim_ring->ring_id) {
304 [ # # ]: 0 : if (ring_ctl->chunk_slots >= CNXK_TIM_MIN_CHUNK_SLOTS &&
305 : : ring_ctl->chunk_slots <= CNXK_TIM_MAX_CHUNK_SLOTS)
306 : 0 : tim_ring->chunk_sz = ((uint32_t)ring_ctl->chunk_slots + 1) *
307 : : CNXK_TIM_CHUNK_ALIGNMENT;
308 : 0 : tim_ring->enable_stats = ring_ctl->enable_stats;
309 : 0 : tim_ring->disable_npa = ring_ctl->disable_npa;
310 : : }
311 : : }
312 : :
313 [ # # ]: 0 : if (roc_model_is_cn20k()) {
314 : 0 : tim_ring->chunk_sz =
315 : 0 : RTE_ALIGN_CEIL(tim_ring->chunk_sz, CNXK_TIM_CN20K_CHUNK_BUF_ALIGN);
316 : : /* Clamp to the largest 256B aligned size within the slot limit. */
317 [ # # ]: 0 : if (CNXK_TIM_NB_CHUNK_SLOTS(tim_ring->chunk_sz) > CNXK_TIM_MAX_CHUNK_SLOTS)
318 : 0 : tim_ring->chunk_sz = RTE_ALIGN_FLOOR(
319 : : (CNXK_TIM_MAX_CHUNK_SLOTS + 1) * CNXK_TIM_CHUNK_ALIGNMENT,
320 : : CNXK_TIM_CN20K_CHUNK_BUF_ALIGN);
321 : : }
322 : :
323 [ # # # # ]: 0 : if (!cnxk_tim_hwwqe_enabled(dev) && tim_ring->disable_npa) {
324 : : /* Each bucket needs at least one chunk. */
325 : 0 : tim_ring->nb_chunks = RTE_MAX(RTE_ALIGN_MUL_CEIL(tim_ring->nb_timers,
326 : : CNXK_TIM_NB_CHUNK_SLOTS(tim_ring->chunk_sz)) /
327 : : CNXK_TIM_NB_CHUNK_SLOTS(tim_ring->chunk_sz), 1UL);
328 : 0 : tim_ring->nb_chunks = tim_ring->nb_chunks * tim_ring->nb_bkts;
329 : : } else {
330 : 0 : tim_ring->disable_npa = 0;
331 : 0 : tim_ring->nb_chunks = tim_ring->nb_timers;
332 : : }
333 : :
334 : 0 : tim_ring->nb_chunk_slots = CNXK_TIM_NB_CHUNK_SLOTS(tim_ring->chunk_sz);
335 : : /* Create buckets. */
336 : 0 : tim_ring->bkt =
337 : 0 : rte_zmalloc("cnxk_tim_bucket",
338 : 0 : (tim_ring->nb_bkts) * sizeof(struct cnxk_tim_bkt),
339 : : RTE_CACHE_LINE_SIZE);
340 [ # # ]: 0 : if (tim_ring->bkt == NULL)
341 : 0 : goto tim_hw_free;
342 : :
343 : 0 : rc = cnxk_tim_chnk_pool_create(tim_ring, rcfg);
344 [ # # ]: 0 : if (rc < 0)
345 : 0 : goto tim_bkt_free;
346 : :
347 : 0 : rc = roc_tim_lf_config(&dev->tim, tim_ring->ring_id, clk_src,
348 : 0 : tim_ring->ena_periodic, tim_ring->ena_dfb, 0,
349 : 0 : tim_ring->nb_bkts, tim_ring->chunk_sz,
350 : : tim_ring->tck_int, tim_ring->tck_nsec, clk_freq);
351 [ # # ]: 0 : if (rc < 0) {
352 : 0 : plt_err("Failed to configure timer ring");
353 : 0 : goto tim_chnk_free;
354 : : }
355 : :
356 [ # # ]: 0 : if (cnxk_tim_hwwqe_enabled(dev)) {
357 : 0 : rc = cnxk_tim_enable_hwwqe(dev, tim_ring);
358 [ # # ]: 0 : if (rc < 0) {
359 : 0 : plt_err("Failed to enable hwwqe");
360 : 0 : goto tim_chnk_free;
361 : : }
362 : : }
363 : :
364 : 0 : plt_write64((uint64_t)tim_ring->bkt, tim_ring->base + TIM_LF_RING_BASE);
365 : 0 : plt_write64(tim_ring->aura, tim_ring->base + TIM_LF_RING_AURA);
366 : :
367 : : /* Set fastpath ops. */
368 : 0 : cnxk_tim_set_fp_ops(tim_ring);
369 : :
370 : : /* Update SSO xae count. */
371 : 0 : cnxk_sso_updt_xae_cnt(cnxk_sso_pmd_priv(dev->event_dev), tim_ring,
372 : : RTE_EVENT_TYPE_TIMER);
373 : 0 : cnxk_sso_xae_reconfigure(dev->event_dev);
374 : 0 : sso_set_priv_mem_fn(dev->event_dev, NULL);
375 : :
376 : 0 : plt_tim_dbg(
377 : : "Total memory used %" PRIu64 "MB",
378 : : (uint64_t)(((tim_ring->nb_chunks * tim_ring->chunk_sz) +
379 : : (tim_ring->nb_bkts * sizeof(struct cnxk_tim_bkt))) /
380 : : BIT_ULL(20)));
381 : :
382 : 0 : adptr->data->adapter_priv = tim_ring;
383 : 0 : return rc;
384 : :
385 : 0 : tim_chnk_free:
386 : 0 : rte_mempool_free(tim_ring->chunk_pool);
387 : 0 : tim_bkt_free:
388 : 0 : rte_free(tim_ring->bkt);
389 : 0 : tim_hw_free:
390 : 0 : roc_tim_lf_free(&dev->tim, tim_ring->ring_id);
391 : 0 : tim_ring_free:
392 : 0 : rte_free(tim_ring);
393 : 0 : return rc;
394 : : }
395 : :
396 : : static int
397 : 0 : cnxk_tim_ring_free(struct rte_event_timer_adapter *adptr)
398 : : {
399 : 0 : struct cnxk_tim_ring *tim_ring = adptr->data->adapter_priv;
400 : : struct cnxk_tim_evdev *dev = cnxk_tim_priv_get();
401 : :
402 [ # # ]: 0 : if (dev == NULL)
403 : : return -ENODEV;
404 : :
405 : 0 : roc_tim_lf_free(&dev->tim, tim_ring->ring_id);
406 : 0 : rte_free(tim_ring->bkt);
407 : 0 : rte_mempool_free(tim_ring->chunk_pool);
408 : 0 : rte_free(tim_ring);
409 : :
410 : 0 : return 0;
411 : : }
412 : :
413 : : static int
414 : 0 : cnxk_tim_ring_start(const struct rte_event_timer_adapter *adptr)
415 : : {
416 : 0 : struct cnxk_tim_ring *tim_ring = adptr->data->adapter_priv;
417 : : struct cnxk_tim_evdev *dev = cnxk_tim_priv_get();
418 : : int rc;
419 : :
420 [ # # ]: 0 : if (dev == NULL)
421 : : return -ENODEV;
422 : :
423 : 0 : rc = roc_tim_lf_enable(&dev->tim, tim_ring->ring_id,
424 : : &tim_ring->ring_start_cyc, NULL);
425 [ # # ]: 0 : if (rc < 0)
426 : : return rc;
427 : :
428 : 0 : tim_ring->fast_div = rte_reciprocal_value_u64(tim_ring->tck_int);
429 : 0 : tim_ring->fast_bkt = rte_reciprocal_value_u64(tim_ring->nb_bkts);
430 : :
431 [ # # # # ]: 0 : if (roc_model_is_cn9k() && (tim_ring->clk_src == ROC_TIM_CLK_SRC_GTI)) {
432 : : uint64_t start_diff;
433 : :
434 : 0 : start_diff = cnxk_tim_cntvct(tim_ring->tbase) -
435 : : cnxk_tim_tick_read(tim_ring->tbase);
436 : 0 : tim_ring->ring_start_cyc += start_diff;
437 : : }
438 : : return rc;
439 : : }
440 : :
441 : : static int
442 : 0 : cnxk_tim_ring_stop(const struct rte_event_timer_adapter *adptr)
443 : : {
444 : 0 : struct cnxk_tim_ring *tim_ring = adptr->data->adapter_priv;
445 : : struct cnxk_tim_evdev *dev = cnxk_tim_priv_get();
446 : : int rc;
447 : :
448 [ # # ]: 0 : if (dev == NULL)
449 : : return -ENODEV;
450 : :
451 : 0 : rc = roc_tim_lf_disable(&dev->tim, tim_ring->ring_id);
452 [ # # ]: 0 : if (rc < 0)
453 : 0 : plt_err("Failed to disable timer ring");
454 : :
455 : : return rc;
456 : : }
457 : :
458 : : static int
459 : 0 : cnxk_tim_stats_get(const struct rte_event_timer_adapter *adapter,
460 : : struct rte_event_timer_adapter_stats *stats)
461 : : {
462 : 0 : struct cnxk_tim_ring *tim_ring = adapter->data->adapter_priv;
463 : 0 : uint64_t bkt_cyc =
464 : 0 : tim_ring->tick_fn(tim_ring->tbase) - tim_ring->ring_start_cyc;
465 : :
466 : 0 : stats->evtim_exp_count =
467 : 0 : rte_atomic_load_explicit(&tim_ring->arm_cnt, rte_memory_order_relaxed);
468 : 0 : stats->ev_enq_count = stats->evtim_exp_count;
469 : 0 : stats->adapter_tick_count =
470 : : rte_reciprocal_divide_u64(bkt_cyc, &tim_ring->fast_div);
471 : 0 : return 0;
472 : : }
473 : :
474 : : static int
475 : 0 : cnxk_tim_stats_reset(const struct rte_event_timer_adapter *adapter)
476 : : {
477 : 0 : struct cnxk_tim_ring *tim_ring = adapter->data->adapter_priv;
478 : :
479 : 0 : rte_atomic_store_explicit(&tim_ring->arm_cnt, 0, rte_memory_order_relaxed);
480 : 0 : return 0;
481 : : }
482 : :
483 : : int
484 : 0 : cnxk_tim_caps_get(const struct rte_eventdev *evdev, uint64_t flags,
485 : : uint32_t *caps, const struct event_timer_adapter_ops **ops,
486 : : cnxk_sso_set_priv_mem_t priv_mem_fn)
487 : : {
488 : : struct cnxk_tim_evdev *dev = cnxk_tim_priv_get();
489 : : struct cnxk_tim_ring *tim_ring;
490 : :
491 : : RTE_SET_USED(flags);
492 : :
493 [ # # ]: 0 : if (dev == NULL)
494 : : return -ENODEV;
495 : :
496 : 0 : cnxk_tim_ops.init = cnxk_tim_ring_create;
497 : 0 : cnxk_tim_ops.uninit = cnxk_tim_ring_free;
498 : 0 : cnxk_tim_ops.start = cnxk_tim_ring_start;
499 : 0 : cnxk_tim_ops.stop = cnxk_tim_ring_stop;
500 : 0 : cnxk_tim_ops.get_info = cnxk_tim_ring_info_get;
501 : 0 : cnxk_tim_ops.remaining_ticks_get = cnxk_tim_remaining_ticks_get;
502 : 0 : sso_set_priv_mem_fn = priv_mem_fn;
503 : :
504 [ # # ]: 0 : if (dev->enable_stats) {
505 : 0 : cnxk_tim_ops.stats_get = cnxk_tim_stats_get;
506 : 0 : cnxk_tim_ops.stats_reset = cnxk_tim_stats_reset;
507 : : }
508 : :
509 : : /* Store evdev pointer for later use. */
510 : 0 : dev->event_dev = (struct rte_eventdev *)(uintptr_t)evdev;
511 : 0 : *caps = RTE_EVENT_TIMER_ADAPTER_CAP_INTERNAL_PORT |
512 : : RTE_EVENT_TIMER_ADAPTER_CAP_PERIODIC;
513 : :
514 : 0 : tim_ring = ((struct rte_event_timer_adapter_data
515 : : *)((char *)caps - offsetof(struct rte_event_timer_adapter_data, caps)))
516 : : ->adapter_priv;
517 [ # # # # ]: 0 : if (tim_ring != NULL && rte_eal_process_type() == RTE_PROC_SECONDARY)
518 : 0 : cnxk_tim_set_fp_ops(tim_ring);
519 : 0 : *ops = &cnxk_tim_ops;
520 : :
521 : 0 : return 0;
522 : : }
523 : :
524 : : static void
525 : 0 : cnxk_tim_parse_ring_param(char *value, void *opaque)
526 : : {
527 : : struct cnxk_tim_evdev *dev = opaque;
528 : 0 : struct cnxk_tim_ctl ring_ctl = {0};
529 : 0 : char *tok = strtok(value, "-");
530 : : struct cnxk_tim_ctl *old_ptr;
531 : : uint16_t *val;
532 : :
533 : : val = (uint16_t *)&ring_ctl;
534 : :
535 [ # # ]: 0 : if (!strlen(value))
536 : 0 : return;
537 : :
538 [ # # # # ]: 0 : while (tok != NULL && val < (&ring_ctl.enable_stats + 1)) {
539 : 0 : *val = atoi(tok);
540 : 0 : tok = strtok(NULL, "-");
541 : 0 : val++;
542 : : }
543 : :
544 [ # # # # ]: 0 : if (tok != NULL || val != (&ring_ctl.enable_stats + 1)) {
545 : 0 : plt_err("Invalid ring param expected [ring-chunk_sz-disable_npa-enable_stats]");
546 : 0 : return;
547 : : }
548 : :
549 : 0 : dev->ring_ctl_cnt++;
550 : 0 : old_ptr = dev->ring_ctl_data;
551 : 0 : dev->ring_ctl_data =
552 : 0 : rte_realloc(dev->ring_ctl_data,
553 : 0 : sizeof(struct cnxk_tim_ctl) * dev->ring_ctl_cnt, 0);
554 [ # # ]: 0 : if (dev->ring_ctl_data == NULL) {
555 : 0 : dev->ring_ctl_data = old_ptr;
556 : 0 : dev->ring_ctl_cnt--;
557 : 0 : return;
558 : : }
559 : :
560 : 0 : dev->ring_ctl_data[dev->ring_ctl_cnt - 1] = ring_ctl;
561 : : }
562 : :
563 : : static void
564 : 0 : cnxk_tim_parse_ring_ctl_list(const char *value, void *opaque)
565 : : {
566 : 0 : char *s = strdup(value);
567 : : char *start = NULL;
568 : : char *end = NULL;
569 : : char *f = s;
570 : :
571 [ # # # # ]: 0 : if (s == NULL || !strlen(s))
572 : 0 : goto free;
573 : :
574 [ # # ]: 0 : while (*s) {
575 [ # # ]: 0 : if (*s == '[')
576 : : start = s;
577 [ # # ]: 0 : else if (*s == ']')
578 : : end = s;
579 : : else
580 : 0 : continue;
581 : :
582 [ # # # # ]: 0 : if (start && end && start < end) {
583 : 0 : *end = 0;
584 : 0 : cnxk_tim_parse_ring_param(start + 1, opaque);
585 : : start = end;
586 : : s = end;
587 : : }
588 : 0 : s++;
589 : : }
590 : :
591 : 0 : free:
592 : 0 : free(f);
593 : 0 : }
594 : :
595 : : static int
596 : 0 : cnxk_tim_parse_kvargs_dict(const char *key, const char *value, void *opaque)
597 : : {
598 : : RTE_SET_USED(key);
599 : :
600 : : /* Dict format [ring-chunk_sz-disable_npa-enable_stats] use '-' as ','
601 : : * isn't allowed. 0 represents default.
602 : : */
603 : 0 : cnxk_tim_parse_ring_ctl_list(value, opaque);
604 : :
605 : 0 : return 0;
606 : : }
607 : :
608 : : static void
609 : 0 : cnxk_tim_parse_clk_list(const char *value, void *opaque)
610 : : {
611 : 0 : enum roc_tim_clk_src src[] = {ROC_TIM_CLK_SRC_GPIO, ROC_TIM_CLK_SRC_PTP,
612 : : ROC_TIM_CLK_SRC_SYNCE,
613 : : ROC_TIM_CLK_SRC_INVALID};
614 : : struct cnxk_tim_evdev *dev = opaque;
615 : 0 : char *str = strdup(value);
616 : : char *tok;
617 : : int i = 0;
618 : :
619 [ # # # # ]: 0 : if (str == NULL || !strlen(str))
620 : 0 : goto free;
621 : :
622 : 0 : tok = strtok(str, "-");
623 [ # # # # ]: 0 : while (tok != NULL && src[i] != ROC_TIM_CLK_SRC_INVALID) {
624 : 0 : dev->ext_clk_freq[src[i]] = strtoull(tok, NULL, 10);
625 : 0 : tok = strtok(NULL, "-");
626 : 0 : i++;
627 : : }
628 : :
629 : 0 : free:
630 : 0 : free(str);
631 : 0 : }
632 : :
633 : : static int
634 : 0 : cnxk_tim_parse_kvargs_dsv(const char *key, const char *value, void *opaque)
635 : : {
636 : : RTE_SET_USED(key);
637 : :
638 : : /* DSV format GPIO-PTP-SYNCE-BTS use '-' as ','
639 : : * isn't allowed. 0 represents default.
640 : : */
641 : 0 : cnxk_tim_parse_clk_list(value, opaque);
642 : :
643 : 0 : return 0;
644 : : }
645 : :
646 : : static void
647 : 0 : cnxk_tim_parse_devargs(struct rte_devargs *devargs, struct cnxk_tim_evdev *dev)
648 : : {
649 : : struct rte_kvargs *kvlist;
650 : :
651 [ # # ]: 0 : if (devargs == NULL)
652 : : return;
653 : :
654 : 0 : kvlist = rte_kvargs_parse(devargs->args, NULL);
655 [ # # ]: 0 : if (kvlist == NULL)
656 : : return;
657 : :
658 : 0 : rte_kvargs_process(kvlist, CNXK_TIM_DISABLE_NPA, &parse_kvargs_flag,
659 : 0 : &dev->disable_npa);
660 : 0 : rte_kvargs_process(kvlist, CNXK_TIM_CHNK_SLOTS, &parse_kvargs_value,
661 : 0 : &dev->chunk_slots);
662 : 0 : rte_kvargs_process(kvlist, CNXK_TIM_STATS_ENA, &parse_kvargs_flag,
663 : 0 : &dev->enable_stats);
664 : 0 : rte_kvargs_process(kvlist, CNXK_TIM_RINGS_LMT, &parse_kvargs_value,
665 : 0 : &dev->min_ring_cnt);
666 : 0 : rte_kvargs_process(kvlist, CNXK_TIM_RING_CTL,
667 : : &cnxk_tim_parse_kvargs_dict, &dev);
668 : 0 : rte_kvargs_process(kvlist, CNXK_TIM_EXT_CLK, &cnxk_tim_parse_kvargs_dsv,
669 : : dev);
670 : :
671 : 0 : rte_kvargs_free(kvlist);
672 : : }
673 : :
674 : : void
675 : 0 : cnxk_tim_init(struct roc_sso *sso)
676 : : {
677 : : const struct rte_memzone *mz;
678 : : struct cnxk_tim_evdev *dev;
679 : : int rc;
680 : :
681 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
682 : : return;
683 : :
684 : 0 : mz = rte_memzone_reserve(RTE_STR(CNXK_TIM_EVDEV_NAME),
685 : : sizeof(struct cnxk_tim_evdev), 0, 0);
686 [ # # ]: 0 : if (mz == NULL) {
687 : 0 : plt_tim_dbg("Unable to allocate memory for TIM Event device");
688 : 0 : return;
689 : : }
690 : 0 : dev = mz->addr;
691 : :
692 : 0 : cnxk_tim_parse_devargs(sso->pci_dev->device.devargs, dev);
693 : :
694 : 0 : dev->tim.roc_sso = sso;
695 : 0 : dev->tim.nb_lfs = dev->min_ring_cnt;
696 : 0 : rc = roc_tim_init(&dev->tim);
697 [ # # ]: 0 : if (rc < 0) {
698 : 0 : plt_err("Failed to initialize roc tim resources");
699 : 0 : rte_memzone_free(mz);
700 : 0 : return;
701 : : }
702 : 0 : dev->nb_rings = rc;
703 : :
704 [ # # ]: 0 : if (dev->chunk_slots && dev->chunk_slots <= CNXK_TIM_MAX_CHUNK_SLOTS &&
705 : : dev->chunk_slots >= CNXK_TIM_MIN_CHUNK_SLOTS) {
706 : 0 : dev->chunk_sz =
707 : 0 : (dev->chunk_slots + 1) * CNXK_TIM_CHUNK_ALIGNMENT;
708 : : } else {
709 : 0 : dev->chunk_sz = CNXK_TIM_RING_DEF_CHUNK_SZ;
710 : : }
711 : : }
712 : :
713 : : void
714 : 0 : cnxk_tim_fini(void)
715 : : {
716 : : struct cnxk_tim_evdev *dev = cnxk_tim_priv_get();
717 : :
718 [ # # # # ]: 0 : if (dev == NULL || rte_eal_process_type() != RTE_PROC_PRIMARY)
719 : 0 : return;
720 : :
721 : 0 : roc_tim_fini(&dev->tim);
722 : 0 : rte_memzone_free(rte_memzone_lookup(RTE_STR(CNXK_TIM_EVDEV_NAME)));
723 : : }
|