eventdev: make vdev init and uninit functions optional
[dpdk.git] / drivers / event / octeontx / ssovf_evdev.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium networks Ltd. 2017.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium networks nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <inttypes.h>
34
35 #include <rte_common.h>
36 #include <rte_debug.h>
37 #include <rte_dev.h>
38 #include <rte_eal.h>
39 #include <rte_lcore.h>
40 #include <rte_log.h>
41 #include <rte_malloc.h>
42 #include <rte_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_vdev.h>
45
46 #include "ssovf_evdev.h"
47
48 /* SSOPF Mailbox messages */
49
50 struct ssovf_mbox_dev_info {
51         uint64_t min_deq_timeout_ns;
52         uint64_t max_deq_timeout_ns;
53         uint32_t max_num_events;
54 };
55
56 static int
57 ssovf_mbox_dev_info(struct ssovf_mbox_dev_info *info)
58 {
59         struct octeontx_mbox_hdr hdr = {0};
60         uint16_t len = sizeof(struct ssovf_mbox_dev_info);
61
62         hdr.coproc = SSO_COPROC;
63         hdr.msg = SSO_GET_DEV_INFO;
64         hdr.vfid = 0;
65
66         memset(info, 0, len);
67         return octeontx_ssovf_mbox_send(&hdr, NULL, 0, info, len);
68 }
69
70 struct ssovf_mbox_getwork_wait {
71         uint64_t wait_ns;
72 };
73
74 static int
75 ssovf_mbox_getwork_tmo_set(uint32_t timeout_ns)
76 {
77         struct octeontx_mbox_hdr hdr = {0};
78         struct ssovf_mbox_getwork_wait tmo_set;
79         uint16_t len = sizeof(struct ssovf_mbox_getwork_wait);
80         int ret;
81
82         hdr.coproc = SSO_COPROC;
83         hdr.msg = SSO_SET_GETWORK_WAIT;
84         hdr.vfid = 0;
85
86         tmo_set.wait_ns = timeout_ns;
87         ret = octeontx_ssovf_mbox_send(&hdr, &tmo_set, len, NULL, 0);
88         if (ret)
89                 ssovf_log_err("Failed to set getwork timeout(%d)", ret);
90
91         return ret;
92 }
93
94 struct ssovf_mbox_grp_pri {
95         uint8_t wgt_left; /* Read only */
96         uint8_t weight;
97         uint8_t affinity;
98         uint8_t priority;
99 };
100
101 static int
102 ssovf_mbox_priority_set(uint8_t queue, uint8_t prio)
103 {
104         struct octeontx_mbox_hdr hdr = {0};
105         struct ssovf_mbox_grp_pri grp;
106         uint16_t len = sizeof(struct ssovf_mbox_grp_pri);
107         int ret;
108
109         hdr.coproc = SSO_COPROC;
110         hdr.msg = SSO_GRP_SET_PRIORITY;
111         hdr.vfid = queue;
112
113         grp.weight = 0xff;
114         grp.affinity = 0xff;
115         grp.priority = prio / 32; /* Normalize to 0 to 7 */
116
117         ret = octeontx_ssovf_mbox_send(&hdr, &grp, len, NULL, 0);
118         if (ret)
119                 ssovf_log_err("Failed to set grp=%d prio=%d", queue, prio);
120
121         return ret;
122 }
123
124 struct ssovf_mbox_convert_ns_getworks_iter {
125         uint64_t wait_ns;
126         uint32_t getwork_iter;/* Get_work iterations for the given wait_ns */
127 };
128
129 static int
130 ssovf_mbox_timeout_ticks(uint64_t ns, uint64_t *tmo_ticks)
131 {
132         struct octeontx_mbox_hdr hdr = {0};
133         struct ssovf_mbox_convert_ns_getworks_iter ns2iter;
134         uint16_t len = sizeof(ns2iter);
135         int ret;
136
137         hdr.coproc = SSO_COPROC;
138         hdr.msg = SSO_CONVERT_NS_GETWORK_ITER;
139         hdr.vfid = 0;
140
141         memset(&ns2iter, 0, len);
142         ns2iter.wait_ns = ns;
143         ret = octeontx_ssovf_mbox_send(&hdr, &ns2iter, len, &ns2iter, len);
144         if (ret < 0 || (ret != len)) {
145                 ssovf_log_err("Failed to get tmo ticks ns=%"PRId64"", ns);
146                 return -EIO;
147         }
148
149         *tmo_ticks = ns2iter.getwork_iter;
150         return 0;
151 }
152
153 static void
154 ssovf_fastpath_fns_set(struct rte_eventdev *dev)
155 {
156         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
157
158         dev->schedule      = NULL;
159         dev->enqueue       = ssows_enq;
160         dev->enqueue_burst = ssows_enq_burst;
161         dev->dequeue       = ssows_deq;
162         dev->dequeue_burst = ssows_deq_burst;
163
164         if (edev->is_timeout_deq) {
165                 dev->dequeue       = ssows_deq_timeout;
166                 dev->dequeue_burst = ssows_deq_timeout_burst;
167         }
168 }
169
170 static void
171 ssovf_info_get(struct rte_eventdev *dev, struct rte_event_dev_info *dev_info)
172 {
173         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
174
175         dev_info->driver_name = RTE_STR(EVENTDEV_NAME_OCTEONTX_PMD);
176         dev_info->min_dequeue_timeout_ns = edev->min_deq_timeout_ns;
177         dev_info->max_dequeue_timeout_ns = edev->max_deq_timeout_ns;
178         dev_info->max_event_queues = edev->max_event_queues;
179         dev_info->max_event_queue_flows = (1ULL << 20);
180         dev_info->max_event_queue_priority_levels = 8;
181         dev_info->max_event_priority_levels = 1;
182         dev_info->max_event_ports = edev->max_event_ports;
183         dev_info->max_event_port_dequeue_depth = 1;
184         dev_info->max_event_port_enqueue_depth = 1;
185         dev_info->max_num_events =  edev->max_num_events;
186         dev_info->event_dev_cap = RTE_EVENT_DEV_CAP_QUEUE_QOS |
187                                         RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
188                                         RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES;
189 }
190
191 static int
192 ssovf_configure(const struct rte_eventdev *dev)
193 {
194         struct rte_event_dev_config *conf = &dev->data->dev_conf;
195         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
196         uint64_t deq_tmo_ns;
197
198         ssovf_func_trace();
199         deq_tmo_ns = conf->dequeue_timeout_ns;
200
201         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT) {
202                 edev->is_timeout_deq = 1;
203                 deq_tmo_ns = edev->min_deq_timeout_ns;
204         }
205         edev->nb_event_queues = conf->nb_event_queues;
206         edev->nb_event_ports = conf->nb_event_ports;
207
208         return ssovf_mbox_getwork_tmo_set(deq_tmo_ns);
209 }
210
211 static void
212 ssovf_queue_def_conf(struct rte_eventdev *dev, uint8_t queue_id,
213                                  struct rte_event_queue_conf *queue_conf)
214 {
215         RTE_SET_USED(dev);
216         RTE_SET_USED(queue_id);
217
218         queue_conf->nb_atomic_flows = (1ULL << 20);
219         queue_conf->nb_atomic_order_sequences = (1ULL << 20);
220         queue_conf->event_queue_cfg = RTE_EVENT_QUEUE_CFG_ALL_TYPES;
221         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
222 }
223
224 static void
225 ssovf_queue_release(struct rte_eventdev *dev, uint8_t queue_id)
226 {
227         RTE_SET_USED(dev);
228         RTE_SET_USED(queue_id);
229 }
230
231 static int
232 ssovf_queue_setup(struct rte_eventdev *dev, uint8_t queue_id,
233                               const struct rte_event_queue_conf *queue_conf)
234 {
235         RTE_SET_USED(dev);
236         ssovf_func_trace("queue=%d prio=%d", queue_id, queue_conf->priority);
237
238         return ssovf_mbox_priority_set(queue_id, queue_conf->priority);
239 }
240
241 static void
242 ssovf_port_def_conf(struct rte_eventdev *dev, uint8_t port_id,
243                                  struct rte_event_port_conf *port_conf)
244 {
245         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
246
247         RTE_SET_USED(port_id);
248         port_conf->new_event_threshold = edev->max_num_events;
249         port_conf->dequeue_depth = 1;
250         port_conf->enqueue_depth = 1;
251 }
252
253 static void
254 ssovf_port_release(void *port)
255 {
256         rte_free(port);
257 }
258
259 static int
260 ssovf_port_setup(struct rte_eventdev *dev, uint8_t port_id,
261                                 const struct rte_event_port_conf *port_conf)
262 {
263         struct ssows *ws;
264         uint32_t reg_off;
265         uint8_t q;
266         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
267
268         ssovf_func_trace("port=%d", port_id);
269         RTE_SET_USED(port_conf);
270
271         /* Free memory prior to re-allocation if needed */
272         if (dev->data->ports[port_id] != NULL) {
273                 ssovf_port_release(dev->data->ports[port_id]);
274                 dev->data->ports[port_id] = NULL;
275         }
276
277         /* Allocate event port memory */
278         ws = rte_zmalloc_socket("eventdev ssows",
279                         sizeof(struct ssows), RTE_CACHE_LINE_SIZE,
280                         dev->data->socket_id);
281         if (ws == NULL) {
282                 ssovf_log_err("Failed to alloc memory for port=%d", port_id);
283                 return -ENOMEM;
284         }
285
286         ws->base = octeontx_ssovf_bar(OCTEONTX_SSO_HWS, port_id, 0);
287         if (ws->base == NULL) {
288                 rte_free(ws);
289                 ssovf_log_err("Failed to get hws base addr port=%d", port_id);
290                 return -EINVAL;
291         }
292
293         reg_off = SSOW_VHWS_OP_GET_WORK0;
294         reg_off |= 1 << 4; /* Index_ggrp_mask (Use maskset zero) */
295         reg_off |= 1 << 16; /* Wait */
296         ws->getwork = ws->base + reg_off;
297         ws->port = port_id;
298
299         for (q = 0; q < edev->nb_event_queues; q++) {
300                 ws->grps[q] = octeontx_ssovf_bar(OCTEONTX_SSO_GROUP, q, 2);
301                 if (ws->grps[q] == NULL) {
302                         rte_free(ws);
303                         ssovf_log_err("Failed to get grp%d base addr", q);
304                         return -EINVAL;
305                 }
306         }
307
308         dev->data->ports[port_id] = ws;
309         ssovf_log_dbg("port=%d ws=%p", port_id, ws);
310         return 0;
311 }
312
313 static int
314 ssovf_port_link(struct rte_eventdev *dev, void *port, const uint8_t queues[],
315                 const uint8_t priorities[], uint16_t nb_links)
316 {
317         uint16_t link;
318         uint64_t val;
319         struct ssows *ws = port;
320
321         ssovf_func_trace("port=%d nb_links=%d", ws->port, nb_links);
322         RTE_SET_USED(dev);
323         RTE_SET_USED(priorities);
324
325         for (link = 0; link < nb_links; link++) {
326                 val = queues[link];
327                 val |= (1ULL << 24); /* Set membership */
328                 ssovf_write64(val, ws->base + SSOW_VHWS_GRPMSK_CHGX(0));
329         }
330         return (int)nb_links;
331 }
332
333 static int
334 ssovf_port_unlink(struct rte_eventdev *dev, void *port, uint8_t queues[],
335                         uint16_t nb_unlinks)
336 {
337         uint16_t unlink;
338         uint64_t val;
339         struct ssows *ws = port;
340
341         ssovf_func_trace("port=%d nb_links=%d", ws->port, nb_unlinks);
342         RTE_SET_USED(dev);
343
344         for (unlink = 0; unlink < nb_unlinks; unlink++) {
345                 val = queues[unlink];
346                 val &= ~(1ULL << 24); /* Clear membership */
347                 ssovf_write64(val, ws->base + SSOW_VHWS_GRPMSK_CHGX(0));
348         }
349         return (int)nb_unlinks;
350 }
351
352 static int
353 ssovf_timeout_ticks(struct rte_eventdev *dev, uint64_t ns, uint64_t *tmo_ticks)
354 {
355         RTE_SET_USED(dev);
356
357         return ssovf_mbox_timeout_ticks(ns, tmo_ticks);
358 }
359
360 static void
361 ssows_dump(struct ssows *ws, FILE *f)
362 {
363         uint8_t *base = ws->base;
364         uint64_t val;
365
366         fprintf(f, "\t---------------port%d---------------\n", ws->port);
367         val = ssovf_read64(base + SSOW_VHWS_TAG);
368         fprintf(f, "\ttag=0x%x tt=%d head=%d tail=%d grp=%d index=%d tail=%d\n",
369                 (uint32_t)(val & 0xffffffff), (int)(val >> 32) & 0x3,
370                 (int)(val >> 34) & 0x1, (int)(val >> 35) & 0x1,
371                 (int)(val >> 36) & 0x3ff, (int)(val >> 48) & 0x3ff,
372                 (int)(val >> 63) & 0x1);
373
374         val = ssovf_read64(base + SSOW_VHWS_WQP);
375         fprintf(f, "\twqp=0x%"PRIx64"\n", val);
376
377         val = ssovf_read64(base + SSOW_VHWS_LINKS);
378         fprintf(f, "\tindex=%d valid=%d revlink=%d tail=%d head=%d grp=%d\n",
379                 (int)(val & 0x3ff), (int)(val >> 10) & 0x1,
380                 (int)(val >> 11) & 0x3ff, (int)(val >> 26) & 0x1,
381                 (int)(val >> 27) & 0x1, (int)(val >> 28) & 0x3ff);
382
383         val = ssovf_read64(base + SSOW_VHWS_PENDTAG);
384         fprintf(f, "\tptag=0x%x ptt=%d pgwi=%d pdesc=%d pgw=%d pgww=%d ps=%d\n",
385                 (uint32_t)(val & 0xffffffff), (int)(val >> 32) & 0x3,
386                 (int)(val >> 56) & 0x1, (int)(val >> 58) & 0x1,
387                 (int)(val >> 61) & 0x1, (int)(val >> 62) & 0x1,
388                 (int)(val >> 63) & 0x1);
389
390         val = ssovf_read64(base + SSOW_VHWS_PENDWQP);
391         fprintf(f, "\tpwqp=0x%"PRIx64"\n", val);
392 }
393
394 static void
395 ssovf_dump(struct rte_eventdev *dev, FILE *f)
396 {
397         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
398         uint8_t port;
399
400         /* Dump SSOWVF debug registers */
401         for (port = 0; port < edev->nb_event_ports; port++)
402                 ssows_dump(dev->data->ports[port], f);
403 }
404
405 static int
406 ssovf_start(struct rte_eventdev *dev)
407 {
408         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
409         struct ssows *ws;
410         uint8_t *base;
411         uint8_t i;
412
413         ssovf_func_trace();
414         for (i = 0; i < edev->nb_event_ports; i++) {
415                 ws = dev->data->ports[i];
416                 ssows_reset(ws);
417                 ws->swtag_req = 0;
418         }
419
420         for (i = 0; i < edev->nb_event_queues; i++) {
421                 /* Consume all the events through HWS0 */
422                 ssows_flush_events(dev->data->ports[0], i);
423
424                 base = octeontx_ssovf_bar(OCTEONTX_SSO_GROUP, i, 0);
425                 base += SSO_VHGRP_QCTL;
426                 ssovf_write64(1, base); /* Enable SSO group */
427         }
428
429         ssovf_fastpath_fns_set(dev);
430         return 0;
431 }
432
433 static void
434 ssovf_stop(struct rte_eventdev *dev)
435 {
436         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
437         struct ssows *ws;
438         uint8_t *base;
439         uint8_t i;
440
441         ssovf_func_trace();
442         for (i = 0; i < edev->nb_event_ports; i++) {
443                 ws = dev->data->ports[i];
444                 ssows_reset(ws);
445                 ws->swtag_req = 0;
446         }
447
448         for (i = 0; i < edev->nb_event_queues; i++) {
449                 /* Consume all the events through HWS0 */
450                 ssows_flush_events(dev->data->ports[0], i);
451
452                 base = octeontx_ssovf_bar(OCTEONTX_SSO_GROUP, i, 0);
453                 base += SSO_VHGRP_QCTL;
454                 ssovf_write64(0, base); /* Disable SSO group */
455         }
456 }
457
458 static int
459 ssovf_close(struct rte_eventdev *dev)
460 {
461         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
462         uint8_t all_queues[RTE_EVENT_MAX_QUEUES_PER_DEV];
463         uint8_t i;
464
465         for (i = 0; i < edev->nb_event_queues; i++)
466                 all_queues[i] = i;
467
468         for (i = 0; i < edev->nb_event_ports; i++)
469                 ssovf_port_unlink(dev, dev->data->ports[i], all_queues,
470                         edev->nb_event_queues);
471         return 0;
472 }
473
474 /* Initialize and register event driver with DPDK Application */
475 static const struct rte_eventdev_ops ssovf_ops = {
476         .dev_infos_get    = ssovf_info_get,
477         .dev_configure    = ssovf_configure,
478         .queue_def_conf   = ssovf_queue_def_conf,
479         .queue_setup      = ssovf_queue_setup,
480         .queue_release    = ssovf_queue_release,
481         .port_def_conf    = ssovf_port_def_conf,
482         .port_setup       = ssovf_port_setup,
483         .port_release     = ssovf_port_release,
484         .port_link        = ssovf_port_link,
485         .port_unlink      = ssovf_port_unlink,
486         .timeout_ticks    = ssovf_timeout_ticks,
487         .dump             = ssovf_dump,
488         .dev_start        = ssovf_start,
489         .dev_stop         = ssovf_stop,
490         .dev_close        = ssovf_close
491 };
492
493 static int
494 ssovf_vdev_probe(struct rte_vdev_device *vdev)
495 {
496         struct octeontx_ssovf_info oinfo;
497         struct ssovf_mbox_dev_info info;
498         struct ssovf_evdev *edev;
499         struct rte_eventdev *eventdev;
500         static int ssovf_init_once;
501         const char *name;
502         int ret;
503
504         name = rte_vdev_device_name(vdev);
505         /* More than one instance is not supported */
506         if (ssovf_init_once) {
507                 ssovf_log_err("Request to create >1 %s instance", name);
508                 return -EINVAL;
509         }
510
511         eventdev = rte_event_pmd_vdev_init(name, sizeof(struct ssovf_evdev),
512                                 rte_socket_id());
513         if (eventdev == NULL) {
514                 ssovf_log_err("Failed to create eventdev vdev %s", name);
515                 return -ENOMEM;
516         }
517         eventdev->dev_ops = &ssovf_ops;
518
519         /* For secondary processes, the primary has done all the work */
520         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
521                 ssovf_fastpath_fns_set(eventdev);
522                 return 0;
523         }
524
525         ret = octeontx_ssovf_info(&oinfo);
526         if (ret) {
527                 ssovf_log_err("Failed to probe and validate ssovfs %d", ret);
528                 goto error;
529         }
530
531         edev = ssovf_pmd_priv(eventdev);
532         edev->max_event_ports = oinfo.total_ssowvfs;
533         edev->max_event_queues = oinfo.total_ssovfs;
534         edev->is_timeout_deq = 0;
535
536         ret = ssovf_mbox_dev_info(&info);
537         if (ret < 0 || ret != sizeof(struct ssovf_mbox_dev_info)) {
538                 ssovf_log_err("Failed to get mbox devinfo %d", ret);
539                 goto error;
540         }
541
542         edev->min_deq_timeout_ns = info.min_deq_timeout_ns;
543         edev->max_deq_timeout_ns = info.max_deq_timeout_ns;
544         edev->max_num_events =  info.max_num_events;
545         ssovf_log_dbg("min_deq_tmo=%"PRId64" max_deq_tmo=%"PRId64" max_evts=%d",
546                         info.min_deq_timeout_ns, info.max_deq_timeout_ns,
547                         info.max_num_events);
548
549         if (!edev->max_event_ports || !edev->max_event_queues) {
550                 ssovf_log_err("Not enough eventdev resource queues=%d ports=%d",
551                         edev->max_event_queues, edev->max_event_ports);
552                 ret = -ENODEV;
553                 goto error;
554         }
555
556         ssovf_log_info("Initializing %s domain=%d max_queues=%d max_ports=%d",
557                         name, oinfo.domain, edev->max_event_queues,
558                         edev->max_event_ports);
559
560         ssovf_init_once = 1;
561         return 0;
562
563 error:
564         rte_event_pmd_vdev_uninit(name);
565         return ret;
566 }
567
568 static int
569 ssovf_vdev_remove(struct rte_vdev_device *vdev)
570 {
571         const char *name;
572
573         name = rte_vdev_device_name(vdev);
574         ssovf_log_info("Closing %s", name);
575         return rte_event_pmd_vdev_uninit(name);
576 }
577
578 static struct rte_vdev_driver vdev_ssovf_pmd = {
579         .probe = ssovf_vdev_probe,
580         .remove = ssovf_vdev_remove
581 };
582
583 RTE_PMD_REGISTER_VDEV(EVENTDEV_NAME_OCTEONTX_PMD, vdev_ssovf_pmd);