7663dc375f15a91209554977a2f99ee246c45815
[dpdk.git] / drivers / event / octeontx / timvf_evdev.h
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright(c) 2017 Cavium, Inc
4  */
5
6 #ifndef __TIMVF_EVDEV_H__
7 #define __TIMVF_EVDEV_H__
8
9 #include <rte_common.h>
10 #include <rte_cycles.h>
11 #include <rte_debug.h>
12 #include <rte_eal.h>
13 #include <rte_eventdev.h>
14 #include <rte_event_timer_adapter.h>
15 #include <rte_event_timer_adapter_pmd.h>
16 #include <rte_io.h>
17 #include <rte_lcore.h>
18 #include <rte_log.h>
19 #include <rte_malloc.h>
20 #include <rte_mbuf_pool_ops.h>
21 #include <rte_mempool.h>
22 #include <rte_memzone.h>
23 #include <rte_pci.h>
24 #include <rte_prefetch.h>
25 #include <rte_reciprocal.h>
26
27 #include <octeontx_mbox.h>
28
29 #define timvf_log(level, fmt, args...) \
30         rte_log(RTE_LOG_ ## level, otx_logtype_timvf, \
31                         "[%s] %s() " fmt "\n", \
32                         RTE_STR(event_timer_octeontx), __func__, ## args)
33
34 #define timvf_log_info(fmt, ...) timvf_log(INFO, fmt, ##__VA_ARGS__)
35 #define timvf_log_dbg(fmt, ...) timvf_log(DEBUG, fmt, ##__VA_ARGS__)
36 #define timvf_log_err(fmt, ...) timvf_log(ERR, fmt, ##__VA_ARGS__)
37 #define timvf_func_trace timvf_log_dbg
38
39 #define TIM_COPROC                              (8)
40 #define TIM_GET_DEV_INFO                        (1)
41 #define TIM_GET_RING_INFO                       (2)
42 #define TIM_SET_RING_INFO                       (3)
43 #define TIM_RING_START_CYC_GET                  (4)
44
45 #define TIM_MAX_RINGS                           (64)
46 #define TIM_DEV_PER_NODE                        (1)
47 #define TIM_VF_PER_DEV                          (64)
48 #define TIM_RING_PER_DEV                        (TIM_VF_PER_DEV)
49 #define TIM_RING_NODE_SHIFT                     (6)
50 #define TIM_RING_MASK                           ((TIM_RING_PER_DEV) - 1)
51 #define TIM_RING_INVALID                        (-1)
52
53 #define TIM_MIN_INTERVAL                        (1E3)
54 #define TIM_MAX_INTERVAL                        ((1ull << 32) - 1)
55 #define TIM_MAX_BUCKETS                         (1ull << 20)
56 #define TIM_CHUNK_SIZE                          (4096)
57 #define TIM_MAX_CHUNKS_PER_BUCKET               (1ull << 32)
58
59 #define TIMVF_MAX_BURST                         (8)
60
61 /* TIM VF Control/Status registers (CSRs): */
62 /* VF_BAR0: */
63 #define TIM_VF_NRSPERR_INT                      (0x0)
64 #define TIM_VF_NRSPERR_INT_W1S                  (0x8)
65 #define TIM_VF_NRSPERR_ENA_W1C                  (0x10)
66 #define TIM_VF_NRSPERR_ENA_W1S                  (0x18)
67 #define TIM_VRING_FR_RN_CYCLES                  (0x20)
68 #define TIM_VRING_FR_RN_GPIOS                   (0x28)
69 #define TIM_VRING_FR_RN_GTI                     (0x30)
70 #define TIM_VRING_FR_RN_PTP                     (0x38)
71 #define TIM_VRING_CTL0                          (0x40)
72 #define TIM_VRING_CTL1                          (0x50)
73 #define TIM_VRING_CTL2                          (0x60)
74 #define TIM_VRING_BASE                          (0x100)
75 #define TIM_VRING_AURA                          (0x108)
76 #define TIM_VRING_REL                           (0x110)
77
78
79 #define NSEC_PER_SEC 1E9
80 #define NSEC2CLK(__ns, __freq) (((__ns) * (__freq)) / NSEC_PER_SEC)
81 #define CLK2NSEC(__clk, __freq) (((__clk) * NSEC_PER_SEC) / (__freq))
82
83 #define timvf_read64 rte_read64_relaxed
84 #define timvf_write64 rte_write64_relaxed
85
86 extern int otx_logtype_timvf;
87 static const uint16_t nb_chunk_slots = (TIM_CHUNK_SIZE / 16) - 1;
88
89 struct timvf_info {
90         uint16_t domain; /* Domain id */
91         uint8_t total_timvfs; /* Total timvf available in domain */
92 };
93
94 enum timvf_clk_src {
95         TIM_CLK_SRC_SCLK = RTE_EVENT_TIMER_ADAPTER_CPU_CLK,
96         TIM_CLK_SRC_GPIO = RTE_EVENT_TIMER_ADAPTER_EXT_CLK0,
97         TIM_CLK_SRC_GTI = RTE_EVENT_TIMER_ADAPTER_EXT_CLK1,
98         TIM_CLK_SRC_PTP = RTE_EVENT_TIMER_ADAPTER_EXT_CLK2,
99 };
100
101 /* TIM_MEM_BUCKET */
102 struct tim_mem_bucket {
103         uint64_t first_chunk;
104         union {
105                 uint64_t w1;
106                 struct {
107                         uint32_t nb_entry;
108                         uint8_t sbt:1;
109                         uint8_t hbt:1;
110                         uint8_t bsk:1;
111                         uint8_t rsvd:5;
112                         uint8_t lock;
113                         int16_t chunk_remainder;
114                 };
115         };
116         uint64_t current_chunk;
117         uint64_t pad;
118 } __rte_packed;
119
120 struct tim_mem_entry {
121         uint64_t w0;
122         uint64_t wqe;
123 } __rte_packed;
124
125 struct timvf_ctrl_reg {
126         uint64_t rctrl0;
127         uint64_t rctrl1;
128         uint64_t rctrl2;
129         uint8_t use_pmu;
130 } __rte_packed;
131
132 struct timvf_ring;
133
134 typedef uint32_t (*bkt_id)(const uint32_t bkt_tcks, const uint32_t nb_bkts);
135 typedef struct tim_mem_entry * (*refill_chunk)(
136                 struct tim_mem_bucket * const bkt,
137                 struct timvf_ring * const timr);
138
139 struct timvf_ring {
140         bkt_id get_target_bkt;
141         refill_chunk refill_chunk;
142         struct rte_reciprocal_u64 fast_div;
143         uint64_t ring_start_cyc;
144         uint32_t nb_bkts;
145         struct tim_mem_bucket *bkt;
146         void *chunk_pool;
147         uint64_t tck_int;
148         uint64_t tck_nsec;
149         void  *vbar0;
150         void *bkt_pos;
151         uint64_t max_tout;
152         uint64_t nb_chunks;
153         enum timvf_clk_src clk_src;
154         uint16_t tim_ring_id;
155 } __rte_cache_aligned;
156
157 static __rte_always_inline uint32_t
158 bkt_mod(const uint32_t rel_bkt, const uint32_t nb_bkts)
159 {
160         return rel_bkt % nb_bkts;
161 }
162
163 int timvf_info(struct timvf_info *tinfo);
164 void *timvf_bar(uint8_t id, uint8_t bar);
165 int timvf_timer_adapter_caps_get(const struct rte_eventdev *dev, uint64_t flags,
166                 uint32_t *caps, const struct rte_event_timer_adapter_ops **ops,
167                 uint8_t enable_stats);
168
169 #endif /* __TIMVF_EVDEV_H__ */