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