event/cnxk: create and free timer adapter
[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 struct cnxk_tim_evdev {
37         struct roc_tim tim;
38         struct rte_eventdev *event_dev;
39         uint16_t nb_rings;
40         uint32_t chunk_sz;
41 };
42
43 enum cnxk_tim_clk_src {
44         CNXK_TIM_CLK_SRC_10NS = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
45         CNXK_TIM_CLK_SRC_GPIO = RTE_EVENT_TIMER_ADAPTER_EXT_CLK0,
46         CNXK_TIM_CLK_SRC_GTI = RTE_EVENT_TIMER_ADAPTER_EXT_CLK1,
47         CNXK_TIM_CLK_SRC_PTP = RTE_EVENT_TIMER_ADAPTER_EXT_CLK2,
48 };
49
50 struct cnxk_tim_bkt {
51         uint64_t first_chunk;
52         union {
53                 uint64_t w1;
54                 struct {
55                         uint32_t nb_entry;
56                         uint8_t sbt : 1;
57                         uint8_t hbt : 1;
58                         uint8_t bsk : 1;
59                         uint8_t rsvd : 5;
60                         uint8_t lock;
61                         int16_t chunk_remainder;
62                 };
63         };
64         uint64_t current_chunk;
65         uint64_t pad;
66 };
67
68 struct cnxk_tim_ring {
69         uintptr_t base;
70         uint16_t nb_chunk_slots;
71         uint32_t nb_bkts;
72         uint64_t tck_int;
73         uint64_t tot_int;
74         struct cnxk_tim_bkt *bkt;
75         struct rte_mempool *chunk_pool;
76         uint64_t arm_cnt;
77         uint8_t prod_type_sp;
78         uint8_t ena_dfb;
79         uint16_t ring_id;
80         uint32_t aura;
81         uint64_t nb_timers;
82         uint64_t tck_nsec;
83         uint64_t max_tout;
84         uint64_t nb_chunks;
85         uint64_t chunk_sz;
86         enum cnxk_tim_clk_src clk_src;
87 } __rte_cache_aligned;
88
89 struct cnxk_tim_ent {
90         uint64_t w0;
91         uint64_t wqe;
92 };
93
94 static inline struct cnxk_tim_evdev *
95 cnxk_tim_priv_get(void)
96 {
97         const struct rte_memzone *mz;
98
99         mz = rte_memzone_lookup(RTE_STR(CNXK_TIM_EVDEV_NAME));
100         if (mz == NULL)
101                 return NULL;
102
103         return mz->addr;
104 }
105
106 static inline uint64_t
107 cnxk_tim_min_tmo_ticks(uint64_t freq)
108 {
109         if (roc_model_runtime_is_cn9k())
110                 return CN9K_TIM_MIN_TMO_TKS;
111         else /* CN10K min tick is of 1us */
112                 return freq / USECPERSEC;
113 }
114
115 static inline uint64_t
116 cnxk_tim_min_resolution_ns(uint64_t freq)
117 {
118         return NSECPERSEC / freq;
119 }
120
121 static inline enum roc_tim_clk_src
122 cnxk_tim_convert_clk_src(enum cnxk_tim_clk_src clk_src)
123 {
124         switch (clk_src) {
125         case RTE_EVENT_TIMER_ADAPTER_CPU_CLK:
126                 return roc_model_runtime_is_cn9k() ? ROC_TIM_CLK_SRC_10NS :
127                                                            ROC_TIM_CLK_SRC_GTI;
128         default:
129                 return ROC_TIM_CLK_SRC_INVALID;
130         }
131 }
132
133 #ifdef RTE_ARCH_ARM64
134 static inline uint64_t
135 cnxk_tim_cntvct(void)
136 {
137         uint64_t tsc;
138
139         asm volatile("mrs %0, cntvct_el0" : "=r"(tsc));
140         return tsc;
141 }
142
143 static inline uint64_t
144 cnxk_tim_cntfrq(void)
145 {
146         uint64_t freq;
147
148         asm volatile("mrs %0, cntfrq_el0" : "=r"(freq));
149         return freq;
150 }
151 #else
152 static inline uint64_t
153 cnxk_tim_cntvct(void)
154 {
155         return 0;
156 }
157
158 static inline uint64_t
159 cnxk_tim_cntfrq(void)
160 {
161         return 0;
162 }
163 #endif
164
165 int cnxk_tim_caps_get(const struct rte_eventdev *dev, uint64_t flags,
166                       uint32_t *caps,
167                       const struct rte_event_timer_adapter_ops **ops);
168
169 void cnxk_tim_init(struct roc_sso *sso);
170 void cnxk_tim_fini(void);
171
172 #endif /* __CNXK_TIM_EVDEV_H__ */