05469ae636683108323bc39fbd7157bf77063563
[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 void
251 dpaa_event_port_default_conf_get(struct rte_eventdev *dev, uint8_t port_id,
252                                  struct rte_event_port_conf *port_conf)
253 {
254         EVENTDEV_DRV_FUNC_TRACE();
255
256         RTE_SET_USED(dev);
257         RTE_SET_USED(port_id);
258
259         port_conf->new_event_threshold = DPAA_EVENT_MAX_NUM_EVENTS;
260         port_conf->dequeue_depth = DPAA_EVENT_MAX_PORT_DEQUEUE_DEPTH;
261         port_conf->enqueue_depth = DPAA_EVENT_MAX_PORT_ENQUEUE_DEPTH;
262 }
263
264 static int
265 dpaa_event_port_setup(struct rte_eventdev *dev, uint8_t port_id,
266                       const struct rte_event_port_conf *port_conf)
267 {
268         struct dpaa_eventdev *eventdev = dev->data->dev_private;
269
270         EVENTDEV_DRV_FUNC_TRACE();
271
272         RTE_SET_USED(port_conf);
273         dev->data->ports[port_id] = &eventdev->ports[port_id];
274
275         return 0;
276 }
277
278 static void
279 dpaa_event_port_release(void *port)
280 {
281         EVENTDEV_DRV_FUNC_TRACE();
282
283         RTE_SET_USED(port);
284 }
285
286 static int
287 dpaa_event_port_link(struct rte_eventdev *dev, void *port,
288                      const uint8_t queues[], const uint8_t priorities[],
289                      uint16_t nb_links)
290 {
291         struct dpaa_eventdev *priv = dev->data->dev_private;
292         struct dpaa_port *event_port = (struct dpaa_port *)port;
293         struct dpaa_eventq *event_queue;
294         uint8_t eventq_id;
295         int i;
296
297         RTE_SET_USED(dev);
298         RTE_SET_USED(priorities);
299
300         /* First check that input configuration are valid */
301         for (i = 0; i < nb_links; i++) {
302                 eventq_id = queues[i];
303                 event_queue = &priv->evq_info[eventq_id];
304                 if ((event_queue->event_queue_cfg
305                         & RTE_EVENT_QUEUE_CFG_SINGLE_LINK)
306                         && (event_queue->event_port)) {
307                         return -EINVAL;
308                 }
309         }
310
311         for (i = 0; i < nb_links; i++) {
312                 eventq_id = queues[i];
313                 event_queue = &priv->evq_info[eventq_id];
314                 event_port->evq_info[i].event_queue_id = eventq_id;
315                 event_port->evq_info[i].ch_id = event_queue->ch_id;
316                 event_queue->event_port = port;
317         }
318
319         event_port->num_linked_evq = event_port->num_linked_evq + i;
320
321         return (int)i;
322 }
323
324 static int
325 dpaa_event_port_unlink(struct rte_eventdev *dev, void *port,
326                        uint8_t queues[], uint16_t nb_links)
327 {
328         int i;
329         uint8_t eventq_id;
330         struct dpaa_eventq *event_queue;
331         struct dpaa_eventdev *priv = dev->data->dev_private;
332         struct dpaa_port *event_port = (struct dpaa_port *)port;
333
334         if (!event_port->num_linked_evq)
335                 return nb_links;
336
337         for (i = 0; i < nb_links; i++) {
338                 eventq_id = queues[i];
339                 event_port->evq_info[eventq_id].event_queue_id = -1;
340                 event_port->evq_info[eventq_id].ch_id = 0;
341                 event_queue = &priv->evq_info[eventq_id];
342                 event_queue->event_port = NULL;
343         }
344
345         event_port->num_linked_evq = event_port->num_linked_evq - i;
346
347         return (int)i;
348 }
349
350 static int
351 dpaa_event_eth_rx_adapter_caps_get(const struct rte_eventdev *dev,
352                                    const struct rte_eth_dev *eth_dev,
353                                    uint32_t *caps)
354 {
355         const char *ethdev_driver = eth_dev->device->driver->name;
356
357         EVENTDEV_DRV_FUNC_TRACE();
358
359         RTE_SET_USED(dev);
360
361         if (!strcmp(ethdev_driver, "net_dpaa"))
362                 *caps = RTE_EVENT_ETH_RX_ADAPTER_DPAA_CAP;
363         else
364                 *caps = RTE_EVENT_ETH_RX_ADAPTER_SW_CAP;
365
366         return 0;
367 }
368
369 static int
370 dpaa_event_eth_rx_adapter_queue_add(
371                 const struct rte_eventdev *dev,
372                 const struct rte_eth_dev *eth_dev,
373                 int32_t rx_queue_id,
374                 const struct rte_event_eth_rx_adapter_queue_conf *queue_conf)
375 {
376         struct dpaa_eventdev *eventdev = dev->data->dev_private;
377         uint8_t ev_qid = queue_conf->ev.queue_id;
378         u16 ch_id = eventdev->evq_info[ev_qid].ch_id;
379         struct dpaa_if *dpaa_intf = eth_dev->data->dev_private;
380         int ret, i;
381
382         EVENTDEV_DRV_FUNC_TRACE();
383
384         if (rx_queue_id == -1) {
385                 for (i = 0; i < dpaa_intf->nb_rx_queues; i++) {
386                         ret = dpaa_eth_eventq_attach(eth_dev, i, ch_id,
387                                                      queue_conf);
388                         if (ret) {
389                                 EVENTDEV_DRV_ERR(
390                                         "Event Queue attach failed:%d\n", ret);
391                                 goto detach_configured_queues;
392                         }
393                 }
394                 return 0;
395         }
396
397         ret = dpaa_eth_eventq_attach(eth_dev, rx_queue_id, ch_id, queue_conf);
398         if (ret)
399                 EVENTDEV_DRV_ERR("dpaa_eth_eventq_attach failed:%d\n", ret);
400         return ret;
401
402 detach_configured_queues:
403
404         for (i = (i - 1); i >= 0 ; i--)
405                 dpaa_eth_eventq_detach(eth_dev, i);
406
407         return ret;
408 }
409
410 static int
411 dpaa_event_eth_rx_adapter_queue_del(const struct rte_eventdev *dev,
412                                     const struct rte_eth_dev *eth_dev,
413                                     int32_t rx_queue_id)
414 {
415         int ret, i;
416         struct dpaa_if *dpaa_intf = eth_dev->data->dev_private;
417
418         EVENTDEV_DRV_FUNC_TRACE();
419
420         RTE_SET_USED(dev);
421         if (rx_queue_id == -1) {
422                 for (i = 0; i < dpaa_intf->nb_rx_queues; i++) {
423                         ret = dpaa_eth_eventq_detach(eth_dev, i);
424                         if (ret)
425                                 EVENTDEV_DRV_ERR(
426                                         "Event Queue detach failed:%d\n", ret);
427                 }
428
429                 return 0;
430         }
431
432         ret = dpaa_eth_eventq_detach(eth_dev, rx_queue_id);
433         if (ret)
434                 EVENTDEV_DRV_ERR("dpaa_eth_eventq_detach failed:%d\n", ret);
435         return ret;
436 }
437
438 static int
439 dpaa_event_eth_rx_adapter_start(const struct rte_eventdev *dev,
440                                 const struct rte_eth_dev *eth_dev)
441 {
442         EVENTDEV_DRV_FUNC_TRACE();
443
444         RTE_SET_USED(dev);
445         RTE_SET_USED(eth_dev);
446
447         return 0;
448 }
449
450 static int
451 dpaa_event_eth_rx_adapter_stop(const struct rte_eventdev *dev,
452                                const struct rte_eth_dev *eth_dev)
453 {
454         EVENTDEV_DRV_FUNC_TRACE();
455
456         RTE_SET_USED(dev);
457         RTE_SET_USED(eth_dev);
458
459         return 0;
460 }
461
462 static const struct rte_eventdev_ops dpaa_eventdev_ops = {
463         .dev_infos_get    = dpaa_event_dev_info_get,
464         .dev_configure    = dpaa_event_dev_configure,
465         .dev_start        = dpaa_event_dev_start,
466         .dev_stop         = dpaa_event_dev_stop,
467         .dev_close        = dpaa_event_dev_close,
468         .queue_def_conf   = dpaa_event_queue_def_conf,
469         .queue_setup      = dpaa_event_queue_setup,
470         .queue_release    = dpaa_event_queue_release,
471         .port_def_conf    = dpaa_event_port_default_conf_get,
472         .port_setup       = dpaa_event_port_setup,
473         .port_release       = dpaa_event_port_release,
474         .port_link        = dpaa_event_port_link,
475         .port_unlink      = dpaa_event_port_unlink,
476         .timeout_ticks    = dpaa_event_dequeue_timeout_ticks,
477         .eth_rx_adapter_caps_get = dpaa_event_eth_rx_adapter_caps_get,
478         .eth_rx_adapter_queue_add = dpaa_event_eth_rx_adapter_queue_add,
479         .eth_rx_adapter_queue_del = dpaa_event_eth_rx_adapter_queue_del,
480         .eth_rx_adapter_start = dpaa_event_eth_rx_adapter_start,
481         .eth_rx_adapter_stop = dpaa_event_eth_rx_adapter_stop,
482 };
483
484 static int
485 dpaa_event_dev_create(const char *name)
486 {
487         struct rte_eventdev *eventdev;
488         struct dpaa_eventdev *priv;
489
490         eventdev = rte_event_pmd_vdev_init(name,
491                                            sizeof(struct dpaa_eventdev),
492                                            rte_socket_id());
493         if (eventdev == NULL) {
494                 EVENTDEV_DRV_ERR("Failed to create eventdev vdev %s", name);
495                 goto fail;
496         }
497
498         eventdev->dev_ops       = &dpaa_eventdev_ops;
499
500         /* For secondary processes, the primary has done all the work */
501         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
502                 return 0;
503
504         priv = eventdev->data->dev_private;
505         priv->max_event_queues = DPAA_EVENT_MAX_QUEUES;
506
507         return 0;
508 fail:
509         return -EFAULT;
510 }
511
512 static int
513 dpaa_event_dev_probe(struct rte_vdev_device *vdev)
514 {
515         const char *name;
516
517         name = rte_vdev_device_name(vdev);
518         EVENTDEV_DRV_LOG("Initializing %s", name);
519
520         return dpaa_event_dev_create(name);
521 }
522
523 static int
524 dpaa_event_dev_remove(struct rte_vdev_device *vdev)
525 {
526         const char *name;
527
528         name = rte_vdev_device_name(vdev);
529         EVENTDEV_DRV_LOG("Closing %s", name);
530
531         return rte_event_pmd_vdev_uninit(name);
532 }
533
534 static struct rte_vdev_driver vdev_eventdev_dpaa_pmd = {
535         .probe = dpaa_event_dev_probe,
536         .remove = dpaa_event_dev_remove
537 };
538
539 RTE_PMD_REGISTER_VDEV(EVENTDEV_NAME_DPAA_PMD, vdev_eventdev_dpaa_pmd);