event/cnxk: add options for timer chunk size and rings
[dpdk.git] / drivers / event / cnxk / cnxk_tim_evdev.h
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
18 #include "roc_api.h"
19
20 #define NSECPERSEC               1E9
21 #define USECPERSEC               1E6
22 #define TICK2NSEC(__tck, __freq) (((__tck)*NSECPERSEC) / (__freq))
23
24 #define CNXK_TIM_EVDEV_NAME         cnxk_tim_eventdev
25 #define CNXK_TIM_MAX_BUCKETS        (0xFFFFF)
26 #define CNXK_TIM_RING_DEF_CHUNK_SZ  (4096)
27 #define CNXK_TIM_CHUNK_ALIGNMENT    (16)
28 #define CNXK_TIM_MAX_BURST          \
29                         (RTE_CACHE_LINE_SIZE / CNXK_TIM_CHUNK_ALIGNMENT)
30 #define CNXK_TIM_NB_CHUNK_SLOTS(sz) (((sz) / CNXK_TIM_CHUNK_ALIGNMENT) - 1)
31 #define CNXK_TIM_MIN_CHUNK_SLOTS    (0x1)
32 #define CNXK_TIM_MAX_CHUNK_SLOTS    (0x1FFE)
33
34 #define CN9K_TIM_MIN_TMO_TKS (256)
35
36 #define CNXK_TIM_DISABLE_NPA "tim_disable_npa"
37 #define CNXK_TIM_CHNK_SLOTS  "tim_chnk_slots"
38 #define CNXK_TIM_RINGS_LMT   "tim_rings_lmt"
39
40 struct cnxk_tim_evdev {
41         struct roc_tim tim;
42         struct rte_eventdev *event_dev;
43         uint16_t nb_rings;
44         uint32_t chunk_sz;
45         /* Dev args */
46         uint8_t disable_npa;
47         uint16_t chunk_slots;
48         uint16_t min_ring_cnt;
49 };
50
51 enum cnxk_tim_clk_src {
52         CNXK_TIM_CLK_SRC_10NS = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
53         CNXK_TIM_CLK_SRC_GPIO = RTE_EVENT_TIMER_ADAPTER_EXT_CLK0,
54         CNXK_TIM_CLK_SRC_GTI = RTE_EVENT_TIMER_ADAPTER_EXT_CLK1,
55         CNXK_TIM_CLK_SRC_PTP = RTE_EVENT_TIMER_ADAPTER_EXT_CLK2,
56 };
57
58 struct cnxk_tim_bkt {
59         uint64_t first_chunk;
60         union {
61                 uint64_t w1;
62                 struct {
63                         uint32_t nb_entry;
64                         uint8_t sbt : 1;
65                         uint8_t hbt : 1;
66                         uint8_t bsk : 1;
67                         uint8_t rsvd : 5;
68                         uint8_t lock;
69                         int16_t chunk_remainder;
70                 };
71         };
72         uint64_t current_chunk;
73         uint64_t pad;
74 };
75
76 struct cnxk_tim_ring {
77         uintptr_t base;
78         uint16_t nb_chunk_slots;
79         uint32_t nb_bkts;
80         uint64_t tck_int;
81         uint64_t tot_int;
82         struct cnxk_tim_bkt *bkt;
83         struct rte_mempool *chunk_pool;
84         uint64_t arm_cnt;
85         uint8_t prod_type_sp;
86         uint8_t disable_npa;
87         uint8_t ena_dfb;
88         uint16_t ring_id;
89         uint32_t aura;
90         uint64_t nb_timers;
91         uint64_t tck_nsec;
92         uint64_t max_tout;
93         uint64_t nb_chunks;
94         uint64_t chunk_sz;
95         enum cnxk_tim_clk_src clk_src;
96 } __rte_cache_aligned;
97
98 struct cnxk_tim_ent {
99         uint64_t w0;
100         uint64_t wqe;
101 };
102
103 static inline struct cnxk_tim_evdev *
104 cnxk_tim_priv_get(void)
105 {
106         const struct rte_memzone *mz;
107
108         mz = rte_memzone_lookup(RTE_STR(CNXK_TIM_EVDEV_NAME));
109         if (mz == NULL)
110                 return NULL;
111
112         return mz->addr;
113 }
114
115 static inline uint64_t
116 cnxk_tim_min_tmo_ticks(uint64_t freq)
117 {
118         if (roc_model_runtime_is_cn9k())
119                 return CN9K_TIM_MIN_TMO_TKS;
120         else /* CN10K min tick is of 1us */
121                 return freq / USECPERSEC;
122 }
123
124 static inline uint64_t
125 cnxk_tim_min_resolution_ns(uint64_t freq)
126 {
127         return NSECPERSEC / freq;
128 }
129
130 static inline enum roc_tim_clk_src
131 cnxk_tim_convert_clk_src(enum cnxk_tim_clk_src clk_src)
132 {
133         switch (clk_src) {
134         case RTE_EVENT_TIMER_ADAPTER_CPU_CLK:
135                 return roc_model_runtime_is_cn9k() ? ROC_TIM_CLK_SRC_10NS :
136                                                            ROC_TIM_CLK_SRC_GTI;
137         default:
138                 return ROC_TIM_CLK_SRC_INVALID;
139         }
140 }
141
142 #ifdef RTE_ARCH_ARM64
143 static inline uint64_t
144 cnxk_tim_cntvct(void)
145 {
146         uint64_t tsc;
147
148         asm volatile("mrs %0, cntvct_el0" : "=r"(tsc));
149         return tsc;
150 }
151
152 static inline uint64_t
153 cnxk_tim_cntfrq(void)
154 {
155         uint64_t freq;
156
157         asm volatile("mrs %0, cntfrq_el0" : "=r"(freq));
158         return freq;
159 }
160 #else
161 static inline uint64_t
162 cnxk_tim_cntvct(void)
163 {
164         return 0;
165 }
166
167 static inline uint64_t
168 cnxk_tim_cntfrq(void)
169 {
170         return 0;
171 }
172 #endif
173
174 int cnxk_tim_caps_get(const struct rte_eventdev *dev, uint64_t flags,
175                       uint32_t *caps,
176                       const struct rte_event_timer_adapter_ops **ops);
177
178 void cnxk_tim_init(struct roc_sso *sso);
179 void cnxk_tim_fini(void);
180
181 #endif /* __CNXK_TIM_EVDEV_H__ */