event/dpaa: add queue config get/set
[dpdk.git] / drivers / event / dpaa / dpaa_eventdev.c
1 /*   SPDX-License-Identifier:        BSD-3-Clause
2  *   Copyright 2017 NXP
3  */
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdbool.h>
8 #include <errno.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include <sys/epoll.h>
12
13 #include <rte_atomic.h>
14 #include <rte_byteorder.h>
15 #include <rte_common.h>
16 #include <rte_debug.h>
17 #include <rte_dev.h>
18 #include <rte_eal.h>
19 #include <rte_lcore.h>
20 #include <rte_log.h>
21 #include <rte_malloc.h>
22 #include <rte_memcpy.h>
23 #include <rte_memory.h>
24 #include <rte_memzone.h>
25 #include <rte_pci.h>
26 #include <rte_eventdev.h>
27 #include <rte_eventdev_pmd_vdev.h>
28 #include <rte_ethdev.h>
29 #include <rte_event_eth_rx_adapter.h>
30 #include <rte_dpaa_bus.h>
31 #include <rte_dpaa_logs.h>
32 #include <rte_cycles_64.h>
33
34 #include <dpaa_ethdev.h>
35 #include "dpaa_eventdev.h"
36 #include <dpaa_mempool.h>
37
38 /*
39  * Clarifications
40  * Evendev = Virtual Instance for SoC
41  * Eventport = Portal Instance
42  * Eventqueue = Channel Instance
43  * 1 Eventdev can have N Eventqueue
44  */
45
46 static int
47 dpaa_event_dequeue_timeout_ticks(struct rte_eventdev *dev, uint64_t ns,
48                                  uint64_t *timeout_ticks)
49 {
50         uint64_t cycles_per_second;
51
52         EVENTDEV_DRV_FUNC_TRACE();
53
54         RTE_SET_USED(dev);
55
56         cycles_per_second = rte_get_timer_hz();
57         *timeout_ticks = ns * (cycles_per_second / NS_PER_S);
58
59         return 0;
60 }
61
62 static void
63 dpaa_event_dev_info_get(struct rte_eventdev *dev,
64                         struct rte_event_dev_info *dev_info)
65 {
66         EVENTDEV_DRV_FUNC_TRACE();
67
68         RTE_SET_USED(dev);
69         dev_info->driver_name = "event_dpaa";
70         dev_info->min_dequeue_timeout_ns =
71                 DPAA_EVENT_MIN_DEQUEUE_TIMEOUT;
72         dev_info->max_dequeue_timeout_ns =
73                 DPAA_EVENT_MAX_DEQUEUE_TIMEOUT;
74         dev_info->dequeue_timeout_ns =
75                 DPAA_EVENT_MIN_DEQUEUE_TIMEOUT;
76         dev_info->max_event_queues =
77                 DPAA_EVENT_MAX_QUEUES;
78         dev_info->max_event_queue_flows =
79                 DPAA_EVENT_MAX_QUEUE_FLOWS;
80         dev_info->max_event_queue_priority_levels =
81                 DPAA_EVENT_MAX_QUEUE_PRIORITY_LEVELS;
82         dev_info->max_event_priority_levels =
83                 DPAA_EVENT_MAX_EVENT_PRIORITY_LEVELS;
84         dev_info->max_event_ports =
85                 DPAA_EVENT_MAX_EVENT_PORT;
86         dev_info->max_event_port_dequeue_depth =
87                 DPAA_EVENT_MAX_PORT_DEQUEUE_DEPTH;
88         dev_info->max_event_port_enqueue_depth =
89                 DPAA_EVENT_MAX_PORT_ENQUEUE_DEPTH;
90         /*
91          * TODO: Need to find out that how to fetch this info
92          * from kernel or somewhere else.
93          */
94         dev_info->max_num_events =
95                 DPAA_EVENT_MAX_NUM_EVENTS;
96         dev_info->event_dev_cap =
97                 RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
98                 RTE_EVENT_DEV_CAP_BURST_MODE |
99                 RTE_EVENT_DEV_CAP_MULTIPLE_QUEUE_PORT |
100                 RTE_EVENT_DEV_CAP_NONSEQ_MODE;
101 }
102
103 static int
104 dpaa_event_dev_configure(const struct rte_eventdev *dev)
105 {
106         struct dpaa_eventdev *priv = dev->data->dev_private;
107         struct rte_event_dev_config *conf = &dev->data->dev_conf;
108         int ret, i;
109         uint32_t *ch_id;
110
111         EVENTDEV_DRV_FUNC_TRACE();
112
113         priv->dequeue_timeout_ns = conf->dequeue_timeout_ns;
114         priv->nb_events_limit = conf->nb_events_limit;
115         priv->nb_event_queues = conf->nb_event_queues;
116         priv->nb_event_ports = conf->nb_event_ports;
117         priv->nb_event_queue_flows = conf->nb_event_queue_flows;
118         priv->nb_event_port_dequeue_depth = conf->nb_event_port_dequeue_depth;
119         priv->nb_event_port_enqueue_depth = conf->nb_event_port_enqueue_depth;
120         priv->event_dev_cfg = conf->event_dev_cfg;
121
122         /* Check dequeue timeout method is per dequeue or global */
123         if (priv->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT) {
124                 /*
125                  * Use timeout value as given in dequeue operation.
126                  * So invalidating this timetout value.
127                  */
128                 priv->dequeue_timeout_ns = 0;
129         }
130
131         ch_id = rte_malloc("dpaa-channels",
132                           sizeof(uint32_t) * priv->nb_event_queues,
133                           RTE_CACHE_LINE_SIZE);
134         if (ch_id == NULL) {
135                 EVENTDEV_DRV_ERR("Fail to allocate memory for dpaa channels\n");
136                 return -ENOMEM;
137         }
138         /* Create requested event queues within the given event device */
139         ret = qman_alloc_pool_range(ch_id, priv->nb_event_queues, 1, 0);
140         if (ret < 0) {
141                 EVENTDEV_DRV_ERR("Failed to create internal channel\n");
142                 rte_free(ch_id);
143                 return ret;
144         }
145         for (i = 0; i < priv->nb_event_queues; i++)
146                 priv->evq_info[i].ch_id = (u16)ch_id[i];
147
148         /* Lets prepare event ports */
149         memset(&priv->ports[0], 0,
150               sizeof(struct dpaa_port) * priv->nb_event_ports);
151         if (priv->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT) {
152                 for (i = 0; i < priv->nb_event_ports; i++) {
153                         priv->ports[i].timeout =
154                                 DPAA_EVENT_PORT_DEQUEUE_TIMEOUT_INVALID;
155                 }
156         } else if (priv->dequeue_timeout_ns == 0) {
157                 for (i = 0; i < priv->nb_event_ports; i++) {
158                         dpaa_event_dequeue_timeout_ticks(NULL,
159                                 DPAA_EVENT_PORT_DEQUEUE_TIMEOUT_NS,
160                                 &priv->ports[i].timeout);
161                 }
162         } else {
163                 for (i = 0; i < priv->nb_event_ports; i++) {
164                         dpaa_event_dequeue_timeout_ticks(NULL,
165                                 priv->dequeue_timeout_ns,
166                                 &priv->ports[i].timeout);
167                 }
168         }
169         /*
170          * TODO: Currently portals are affined with threads. Maximum threads
171          * can be created equals to number of lcore.
172          */
173         rte_free(ch_id);
174         EVENTDEV_DRV_LOG("Configured eventdev devid=%d", dev->data->dev_id);
175
176         return 0;
177 }
178
179 static int
180 dpaa_event_dev_start(struct rte_eventdev *dev)
181 {
182         EVENTDEV_DRV_FUNC_TRACE();
183         RTE_SET_USED(dev);
184
185         return 0;
186 }
187
188 static void
189 dpaa_event_dev_stop(struct rte_eventdev *dev)
190 {
191         EVENTDEV_DRV_FUNC_TRACE();
192         RTE_SET_USED(dev);
193 }
194
195 static int
196 dpaa_event_dev_close(struct rte_eventdev *dev)
197 {
198         EVENTDEV_DRV_FUNC_TRACE();
199         RTE_SET_USED(dev);
200
201         return 0;
202 }
203
204 static void
205 dpaa_event_queue_def_conf(struct rte_eventdev *dev, uint8_t queue_id,
206                           struct rte_event_queue_conf *queue_conf)
207 {
208         EVENTDEV_DRV_FUNC_TRACE();
209
210         RTE_SET_USED(dev);
211         RTE_SET_USED(queue_id);
212
213         memset(queue_conf, 0, sizeof(struct rte_event_queue_conf));
214         queue_conf->schedule_type = RTE_SCHED_TYPE_PARALLEL;
215         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_HIGHEST;
216 }
217
218 static int
219 dpaa_event_queue_setup(struct rte_eventdev *dev, uint8_t queue_id,
220                        const struct rte_event_queue_conf *queue_conf)
221 {
222         struct dpaa_eventdev *priv = dev->data->dev_private;
223         struct dpaa_eventq *evq_info = &priv->evq_info[queue_id];
224
225         EVENTDEV_DRV_FUNC_TRACE();
226
227         switch (queue_conf->schedule_type) {
228         case RTE_SCHED_TYPE_PARALLEL:
229         case RTE_SCHED_TYPE_ATOMIC:
230                 break;
231         case RTE_SCHED_TYPE_ORDERED:
232                 EVENTDEV_DRV_ERR("Schedule type is not supported.");
233                 return -1;
234         }
235         evq_info->event_queue_cfg = queue_conf->event_queue_cfg;
236         evq_info->event_queue_id = queue_id;
237
238         return 0;
239 }
240
241 static void
242 dpaa_event_queue_release(struct rte_eventdev *dev, uint8_t queue_id)
243 {
244         EVENTDEV_DRV_FUNC_TRACE();
245
246         RTE_SET_USED(dev);
247         RTE_SET_USED(queue_id);
248 }
249
250 static const struct rte_eventdev_ops dpaa_eventdev_ops = {
251         .dev_infos_get    = dpaa_event_dev_info_get,
252         .dev_configure    = dpaa_event_dev_configure,
253         .dev_start        = dpaa_event_dev_start,
254         .dev_stop         = dpaa_event_dev_stop,
255         .dev_close        = dpaa_event_dev_close,
256         .queue_def_conf   = dpaa_event_queue_def_conf,
257         .queue_setup      = dpaa_event_queue_setup,
258         .queue_release    = dpaa_event_queue_release,
259         .timeout_ticks    = dpaa_event_dequeue_timeout_ticks,
260 };
261
262 static int
263 dpaa_event_dev_create(const char *name)
264 {
265         struct rte_eventdev *eventdev;
266         struct dpaa_eventdev *priv;
267
268         eventdev = rte_event_pmd_vdev_init(name,
269                                            sizeof(struct dpaa_eventdev),
270                                            rte_socket_id());
271         if (eventdev == NULL) {
272                 EVENTDEV_DRV_ERR("Failed to create eventdev vdev %s", name);
273                 goto fail;
274         }
275
276         eventdev->dev_ops       = &dpaa_eventdev_ops;
277
278         /* For secondary processes, the primary has done all the work */
279         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
280                 return 0;
281
282         priv = eventdev->data->dev_private;
283         priv->max_event_queues = DPAA_EVENT_MAX_QUEUES;
284
285         return 0;
286 fail:
287         return -EFAULT;
288 }
289
290 static int
291 dpaa_event_dev_probe(struct rte_vdev_device *vdev)
292 {
293         const char *name;
294
295         name = rte_vdev_device_name(vdev);
296         EVENTDEV_DRV_LOG("Initializing %s", name);
297
298         return dpaa_event_dev_create(name);
299 }
300
301 static int
302 dpaa_event_dev_remove(struct rte_vdev_device *vdev)
303 {
304         const char *name;
305
306         name = rte_vdev_device_name(vdev);
307         EVENTDEV_DRV_LOG("Closing %s", name);
308
309         return rte_event_pmd_vdev_uninit(name);
310 }
311
312 static struct rte_vdev_driver vdev_eventdev_dpaa_pmd = {
313         .probe = dpaa_event_dev_probe,
314         .remove = dpaa_event_dev_remove
315 };
316
317 RTE_PMD_REGISTER_VDEV(EVENTDEV_NAME_DPAA_PMD, vdev_eventdev_dpaa_pmd);