event/octeontx2: add devargs to force legacy mode
[dpdk.git] / drivers / event / octeontx2 / otx2_evdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <inttypes.h>
6
7 #include <rte_bus_pci.h>
8 #include <rte_common.h>
9 #include <rte_eal.h>
10 #include <rte_eventdev_pmd_pci.h>
11 #include <rte_kvargs.h>
12 #include <rte_mbuf_pool_ops.h>
13 #include <rte_pci.h>
14
15 #include "otx2_evdev_stats.h"
16 #include "otx2_evdev.h"
17 #include "otx2_irq.h"
18
19 static inline int
20 sso_get_msix_offsets(const struct rte_eventdev *event_dev)
21 {
22         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
23         uint8_t nb_ports = dev->nb_event_ports * (dev->dual_ws ? 2 : 1);
24         struct otx2_mbox *mbox = dev->mbox;
25         struct msix_offset_rsp *msix_rsp;
26         int i, rc;
27
28         /* Get SSO and SSOW MSIX vector offsets */
29         otx2_mbox_alloc_msg_msix_offset(mbox);
30         rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
31
32         for (i = 0; i < nb_ports; i++)
33                 dev->ssow_msixoff[i] = msix_rsp->ssow_msixoff[i];
34
35         for (i = 0; i < dev->nb_event_queues; i++)
36                 dev->sso_msixoff[i] = msix_rsp->sso_msixoff[i];
37
38         return rc;
39 }
40
41 static void
42 otx2_sso_info_get(struct rte_eventdev *event_dev,
43                   struct rte_event_dev_info *dev_info)
44 {
45         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
46
47         dev_info->driver_name = RTE_STR(EVENTDEV_NAME_OCTEONTX2_PMD);
48         dev_info->min_dequeue_timeout_ns = dev->min_dequeue_timeout_ns;
49         dev_info->max_dequeue_timeout_ns = dev->max_dequeue_timeout_ns;
50         dev_info->max_event_queues = dev->max_event_queues;
51         dev_info->max_event_queue_flows = (1ULL << 20);
52         dev_info->max_event_queue_priority_levels = 8;
53         dev_info->max_event_priority_levels = 1;
54         dev_info->max_event_ports = dev->max_event_ports;
55         dev_info->max_event_port_dequeue_depth = 1;
56         dev_info->max_event_port_enqueue_depth = 1;
57         dev_info->max_num_events =  dev->max_num_events;
58         dev_info->event_dev_cap = RTE_EVENT_DEV_CAP_QUEUE_QOS |
59                                         RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
60                                         RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES |
61                                         RTE_EVENT_DEV_CAP_RUNTIME_PORT_LINK |
62                                         RTE_EVENT_DEV_CAP_MULTIPLE_QUEUE_PORT |
63                                         RTE_EVENT_DEV_CAP_NONSEQ_MODE;
64 }
65
66 static void
67 sso_port_link_modify(struct otx2_ssogws *ws, uint8_t queue, uint8_t enable)
68 {
69         uintptr_t base = OTX2_SSOW_GET_BASE_ADDR(ws->getwrk_op);
70         uint64_t val;
71
72         val = queue;
73         val |= 0ULL << 12; /* SET 0 */
74         val |= 0x8000800080000000; /* Dont modify rest of the masks */
75         val |= (uint64_t)enable << 14;   /* Enable/Disable Membership. */
76
77         otx2_write64(val, base + SSOW_LF_GWS_GRPMSK_CHG);
78 }
79
80 static int
81 otx2_sso_port_link(struct rte_eventdev *event_dev, void *port,
82                    const uint8_t queues[], const uint8_t priorities[],
83                    uint16_t nb_links)
84 {
85         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
86         uint8_t port_id = 0;
87         uint16_t link;
88
89         RTE_SET_USED(priorities);
90         for (link = 0; link < nb_links; link++) {
91                 if (dev->dual_ws) {
92                         struct otx2_ssogws_dual *ws = port;
93
94                         port_id = ws->port;
95                         sso_port_link_modify((struct otx2_ssogws *)
96                                         &ws->ws_state[0], queues[link], true);
97                         sso_port_link_modify((struct otx2_ssogws *)
98                                         &ws->ws_state[1], queues[link], true);
99                 } else {
100                         struct otx2_ssogws *ws = port;
101
102                         port_id = ws->port;
103                         sso_port_link_modify(ws, queues[link], true);
104                 }
105         }
106         sso_func_trace("Port=%d nb_links=%d", port_id, nb_links);
107
108         return (int)nb_links;
109 }
110
111 static int
112 otx2_sso_port_unlink(struct rte_eventdev *event_dev, void *port,
113                      uint8_t queues[], uint16_t nb_unlinks)
114 {
115         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
116         uint8_t port_id = 0;
117         uint16_t unlink;
118
119         for (unlink = 0; unlink < nb_unlinks; unlink++) {
120                 if (dev->dual_ws) {
121                         struct otx2_ssogws_dual *ws = port;
122
123                         port_id = ws->port;
124                         sso_port_link_modify((struct otx2_ssogws *)
125                                         &ws->ws_state[0], queues[unlink],
126                                         false);
127                         sso_port_link_modify((struct otx2_ssogws *)
128                                         &ws->ws_state[1], queues[unlink],
129                                         false);
130                 } else {
131                         struct otx2_ssogws *ws = port;
132
133                         port_id = ws->port;
134                         sso_port_link_modify(ws, queues[unlink], false);
135                 }
136         }
137         sso_func_trace("Port=%d nb_unlinks=%d", port_id, nb_unlinks);
138
139         return (int)nb_unlinks;
140 }
141
142 static int
143 sso_hw_lf_cfg(struct otx2_mbox *mbox, enum otx2_sso_lf_type type,
144               uint16_t nb_lf, uint8_t attach)
145 {
146         if (attach) {
147                 struct rsrc_attach_req *req;
148
149                 req = otx2_mbox_alloc_msg_attach_resources(mbox);
150                 switch (type) {
151                 case SSO_LF_GGRP:
152                         req->sso = nb_lf;
153                         break;
154                 case SSO_LF_GWS:
155                         req->ssow = nb_lf;
156                         break;
157                 default:
158                         return -EINVAL;
159                 }
160                 req->modify = true;
161                 if (otx2_mbox_process(mbox) < 0)
162                         return -EIO;
163         } else {
164                 struct rsrc_detach_req *req;
165
166                 req = otx2_mbox_alloc_msg_detach_resources(mbox);
167                 switch (type) {
168                 case SSO_LF_GGRP:
169                         req->sso = true;
170                         break;
171                 case SSO_LF_GWS:
172                         req->ssow = true;
173                         break;
174                 default:
175                         return -EINVAL;
176                 }
177                 req->partial = true;
178                 if (otx2_mbox_process(mbox) < 0)
179                         return -EIO;
180         }
181
182         return 0;
183 }
184
185 static int
186 sso_lf_cfg(struct otx2_sso_evdev *dev, struct otx2_mbox *mbox,
187            enum otx2_sso_lf_type type, uint16_t nb_lf, uint8_t alloc)
188 {
189         void *rsp;
190         int rc;
191
192         if (alloc) {
193                 switch (type) {
194                 case SSO_LF_GGRP:
195                         {
196                         struct sso_lf_alloc_req *req_ggrp;
197                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_alloc(mbox);
198                         req_ggrp->hwgrps = nb_lf;
199                         }
200                         break;
201                 case SSO_LF_GWS:
202                         {
203                         struct ssow_lf_alloc_req *req_hws;
204                         req_hws = otx2_mbox_alloc_msg_ssow_lf_alloc(mbox);
205                         req_hws->hws = nb_lf;
206                         }
207                         break;
208                 default:
209                         return -EINVAL;
210                 }
211         } else {
212                 switch (type) {
213                 case SSO_LF_GGRP:
214                         {
215                         struct sso_lf_free_req *req_ggrp;
216                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_free(mbox);
217                         req_ggrp->hwgrps = nb_lf;
218                         }
219                         break;
220                 case SSO_LF_GWS:
221                         {
222                         struct ssow_lf_free_req *req_hws;
223                         req_hws = otx2_mbox_alloc_msg_ssow_lf_free(mbox);
224                         req_hws->hws = nb_lf;
225                         }
226                         break;
227                 default:
228                         return -EINVAL;
229                 }
230         }
231
232         rc = otx2_mbox_process_msg_tmo(mbox, (void **)&rsp, ~0);
233         if (rc < 0)
234                 return rc;
235
236         if (alloc && type == SSO_LF_GGRP) {
237                 struct sso_lf_alloc_rsp *rsp_ggrp = rsp;
238
239                 dev->xaq_buf_size = rsp_ggrp->xaq_buf_size;
240                 dev->xae_waes = rsp_ggrp->xaq_wq_entries;
241                 dev->iue = rsp_ggrp->in_unit_entries;
242         }
243
244         return 0;
245 }
246
247 static void
248 otx2_sso_port_release(void *port)
249 {
250         rte_free(port);
251 }
252
253 static void
254 otx2_sso_queue_release(struct rte_eventdev *event_dev, uint8_t queue_id)
255 {
256         RTE_SET_USED(event_dev);
257         RTE_SET_USED(queue_id);
258 }
259
260 static void
261 sso_clr_links(const struct rte_eventdev *event_dev)
262 {
263         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
264         int i, j;
265
266         for (i = 0; i < dev->nb_event_ports; i++) {
267                 if (dev->dual_ws) {
268                         struct otx2_ssogws_dual *ws;
269
270                         ws = event_dev->data->ports[i];
271                         for (j = 0; j < dev->nb_event_queues; j++) {
272                                 sso_port_link_modify((struct otx2_ssogws *)
273                                                 &ws->ws_state[0], j, false);
274                                 sso_port_link_modify((struct otx2_ssogws *)
275                                                 &ws->ws_state[1], j, false);
276                         }
277                 } else {
278                         struct otx2_ssogws *ws;
279
280                         ws = event_dev->data->ports[i];
281                         for (j = 0; j < dev->nb_event_queues; j++)
282                                 sso_port_link_modify(ws, j, false);
283                 }
284         }
285 }
286
287 static void
288 sso_set_port_ops(struct otx2_ssogws *ws, uintptr_t base)
289 {
290         ws->tag_op              = base + SSOW_LF_GWS_TAG;
291         ws->wqp_op              = base + SSOW_LF_GWS_WQP;
292         ws->getwrk_op           = base + SSOW_LF_GWS_OP_GET_WORK;
293         ws->swtp_op             = base + SSOW_LF_GWS_SWTP;
294         ws->swtag_norm_op       = base + SSOW_LF_GWS_OP_SWTAG_NORM;
295         ws->swtag_desched_op    = base + SSOW_LF_GWS_OP_SWTAG_DESCHED;
296 }
297
298 static int
299 sso_configure_dual_ports(const struct rte_eventdev *event_dev)
300 {
301         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
302         struct otx2_mbox *mbox = dev->mbox;
303         uint8_t vws = 0;
304         uint8_t nb_lf;
305         int i, rc;
306
307         otx2_sso_dbg("Configuring event ports %d", dev->nb_event_ports);
308
309         nb_lf = dev->nb_event_ports * 2;
310         /* Ask AF to attach required LFs. */
311         rc = sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, true);
312         if (rc < 0) {
313                 otx2_err("Failed to attach SSO GWS LF");
314                 return -ENODEV;
315         }
316
317         if (sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, true) < 0) {
318                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
319                 otx2_err("Failed to init SSO GWS LF");
320                 return -ENODEV;
321         }
322
323         for (i = 0; i < dev->nb_event_ports; i++) {
324                 struct otx2_ssogws_dual *ws;
325                 uintptr_t base;
326
327                 /* Free memory prior to re-allocation if needed */
328                 if (event_dev->data->ports[i] != NULL) {
329                         ws = event_dev->data->ports[i];
330                         rte_free(ws);
331                         ws = NULL;
332                 }
333
334                 /* Allocate event port memory */
335                 ws = rte_zmalloc_socket("otx2_sso_ws",
336                                         sizeof(struct otx2_ssogws_dual),
337                                         RTE_CACHE_LINE_SIZE,
338                                         event_dev->data->socket_id);
339                 if (ws == NULL) {
340                         otx2_err("Failed to alloc memory for port=%d", i);
341                         rc = -ENOMEM;
342                         break;
343                 }
344
345                 ws->port = i;
346                 base = dev->bar2 + (RVU_BLOCK_ADDR_SSOW << 20 | vws << 12);
347                 sso_set_port_ops((struct otx2_ssogws *)&ws->ws_state[0], base);
348                 vws++;
349
350                 base = dev->bar2 + (RVU_BLOCK_ADDR_SSOW << 20 | vws << 12);
351                 sso_set_port_ops((struct otx2_ssogws *)&ws->ws_state[1], base);
352                 vws++;
353
354                 event_dev->data->ports[i] = ws;
355         }
356
357         if (rc < 0) {
358                 sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, false);
359                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
360         }
361
362         return rc;
363 }
364
365 static int
366 sso_configure_ports(const struct rte_eventdev *event_dev)
367 {
368         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
369         struct otx2_mbox *mbox = dev->mbox;
370         uint8_t nb_lf;
371         int i, rc;
372
373         otx2_sso_dbg("Configuring event ports %d", dev->nb_event_ports);
374
375         nb_lf = dev->nb_event_ports;
376         /* Ask AF to attach required LFs. */
377         rc = sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, true);
378         if (rc < 0) {
379                 otx2_err("Failed to attach SSO GWS LF");
380                 return -ENODEV;
381         }
382
383         if (sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, true) < 0) {
384                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
385                 otx2_err("Failed to init SSO GWS LF");
386                 return -ENODEV;
387         }
388
389         for (i = 0; i < nb_lf; i++) {
390                 struct otx2_ssogws *ws;
391                 uintptr_t base;
392
393                 /* Free memory prior to re-allocation if needed */
394                 if (event_dev->data->ports[i] != NULL) {
395                         ws = event_dev->data->ports[i];
396                         rte_free(ws);
397                         ws = NULL;
398                 }
399
400                 /* Allocate event port memory */
401                 ws = rte_zmalloc_socket("otx2_sso_ws",
402                                         sizeof(struct otx2_ssogws),
403                                         RTE_CACHE_LINE_SIZE,
404                                         event_dev->data->socket_id);
405                 if (ws == NULL) {
406                         otx2_err("Failed to alloc memory for port=%d", i);
407                         rc = -ENOMEM;
408                         break;
409                 }
410
411                 ws->port = i;
412                 base = dev->bar2 + (RVU_BLOCK_ADDR_SSOW << 20 | i << 12);
413                 sso_set_port_ops(ws, base);
414
415                 event_dev->data->ports[i] = ws;
416         }
417
418         if (rc < 0) {
419                 sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, false);
420                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
421         }
422
423         return rc;
424 }
425
426 static int
427 sso_configure_queues(const struct rte_eventdev *event_dev)
428 {
429         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
430         struct otx2_mbox *mbox = dev->mbox;
431         uint8_t nb_lf;
432         int rc;
433
434         otx2_sso_dbg("Configuring event queues %d", dev->nb_event_queues);
435
436         nb_lf = dev->nb_event_queues;
437         /* Ask AF to attach required LFs. */
438         rc = sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, true);
439         if (rc < 0) {
440                 otx2_err("Failed to attach SSO GGRP LF");
441                 return -ENODEV;
442         }
443
444         if (sso_lf_cfg(dev, mbox, SSO_LF_GGRP, nb_lf, true) < 0) {
445                 sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, false);
446                 otx2_err("Failed to init SSO GGRP LF");
447                 return -ENODEV;
448         }
449
450         return rc;
451 }
452
453 static int
454 sso_xaq_allocate(struct otx2_sso_evdev *dev)
455 {
456         const struct rte_memzone *mz;
457         struct npa_aura_s *aura;
458         static int reconfig_cnt;
459         char pool_name[RTE_MEMZONE_NAMESIZE];
460         uint32_t xaq_cnt;
461         int rc;
462
463         if (dev->xaq_pool)
464                 rte_mempool_free(dev->xaq_pool);
465
466         /*
467          * Allocate memory for Add work backpressure.
468          */
469         mz = rte_memzone_lookup(OTX2_SSO_FC_NAME);
470         if (mz == NULL)
471                 mz = rte_memzone_reserve_aligned(OTX2_SSO_FC_NAME,
472                                                  OTX2_ALIGN +
473                                                  sizeof(struct npa_aura_s),
474                                                  rte_socket_id(),
475                                                  RTE_MEMZONE_IOVA_CONTIG,
476                                                  OTX2_ALIGN);
477         if (mz == NULL) {
478                 otx2_err("Failed to allocate mem for fcmem");
479                 return -ENOMEM;
480         }
481
482         dev->fc_iova = mz->iova;
483         dev->fc_mem = mz->addr;
484
485         aura = (struct npa_aura_s *)((uintptr_t)dev->fc_mem + OTX2_ALIGN);
486         memset(aura, 0, sizeof(struct npa_aura_s));
487
488         aura->fc_ena = 1;
489         aura->fc_addr = dev->fc_iova;
490         aura->fc_hyst_bits = 0; /* Store count on all updates */
491
492         /* Taken from HRM 14.3.3(4) */
493         xaq_cnt = dev->nb_event_queues * OTX2_SSO_XAQ_CACHE_CNT;
494         if (dev->xae_cnt)
495                 xaq_cnt += dev->xae_cnt / dev->xae_waes;
496         else
497                 xaq_cnt += (dev->iue / dev->xae_waes) +
498                         (OTX2_SSO_XAQ_SLACK * dev->nb_event_queues);
499
500         otx2_sso_dbg("Configuring %d xaq buffers", xaq_cnt);
501         /* Setup XAQ based on number of nb queues. */
502         snprintf(pool_name, 30, "otx2_xaq_buf_pool_%d", reconfig_cnt);
503         dev->xaq_pool = (void *)rte_mempool_create_empty(pool_name,
504                         xaq_cnt, dev->xaq_buf_size, 0, 0,
505                         rte_socket_id(), 0);
506
507         if (dev->xaq_pool == NULL) {
508                 otx2_err("Unable to create empty mempool.");
509                 rte_memzone_free(mz);
510                 return -ENOMEM;
511         }
512
513         rc = rte_mempool_set_ops_byname(dev->xaq_pool,
514                                         rte_mbuf_platform_mempool_ops(), aura);
515         if (rc != 0) {
516                 otx2_err("Unable to set xaqpool ops.");
517                 goto alloc_fail;
518         }
519
520         rc = rte_mempool_populate_default(dev->xaq_pool);
521         if (rc < 0) {
522                 otx2_err("Unable to set populate xaqpool.");
523                 goto alloc_fail;
524         }
525         reconfig_cnt++;
526         /* When SW does addwork (enqueue) check if there is space in XAQ by
527          * comparing fc_addr above against the xaq_lmt calculated below.
528          * There should be a minimum headroom (OTX2_SSO_XAQ_SLACK / 2) for SSO
529          * to request XAQ to cache them even before enqueue is called.
530          */
531         dev->xaq_lmt = xaq_cnt - (OTX2_SSO_XAQ_SLACK / 2 *
532                                   dev->nb_event_queues);
533         dev->nb_xaq_cfg = xaq_cnt;
534
535         return 0;
536 alloc_fail:
537         rte_mempool_free(dev->xaq_pool);
538         rte_memzone_free(mz);
539         return rc;
540 }
541
542 static int
543 sso_ggrp_alloc_xaq(struct otx2_sso_evdev *dev)
544 {
545         struct otx2_mbox *mbox = dev->mbox;
546         struct sso_hw_setconfig *req;
547
548         otx2_sso_dbg("Configuring XAQ for GGRPs");
549         req = otx2_mbox_alloc_msg_sso_hw_setconfig(mbox);
550         req->npa_pf_func = otx2_npa_pf_func_get();
551         req->npa_aura_id = npa_lf_aura_handle_to_aura(dev->xaq_pool->pool_id);
552         req->hwgrps = dev->nb_event_queues;
553
554         return otx2_mbox_process(mbox);
555 }
556
557 static void
558 sso_lf_teardown(struct otx2_sso_evdev *dev,
559                 enum otx2_sso_lf_type lf_type)
560 {
561         uint8_t nb_lf;
562
563         switch (lf_type) {
564         case SSO_LF_GGRP:
565                 nb_lf = dev->nb_event_queues;
566                 break;
567         case SSO_LF_GWS:
568                 nb_lf = dev->nb_event_ports;
569                 nb_lf *= dev->dual_ws ? 2 : 1;
570                 break;
571         default:
572                 return;
573         }
574
575         sso_lf_cfg(dev, dev->mbox, lf_type, nb_lf, false);
576         sso_hw_lf_cfg(dev->mbox, lf_type, nb_lf, false);
577 }
578
579 static int
580 otx2_sso_configure(const struct rte_eventdev *event_dev)
581 {
582         struct rte_event_dev_config *conf = &event_dev->data->dev_conf;
583         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
584         uint32_t deq_tmo_ns;
585         int rc;
586
587         sso_func_trace();
588         deq_tmo_ns = conf->dequeue_timeout_ns;
589
590         if (deq_tmo_ns == 0)
591                 deq_tmo_ns = dev->min_dequeue_timeout_ns;
592
593         if (deq_tmo_ns < dev->min_dequeue_timeout_ns ||
594             deq_tmo_ns > dev->max_dequeue_timeout_ns) {
595                 otx2_err("Unsupported dequeue timeout requested");
596                 return -EINVAL;
597         }
598
599         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT)
600                 dev->is_timeout_deq = 1;
601
602         dev->deq_tmo_ns = deq_tmo_ns;
603
604         if (conf->nb_event_ports > dev->max_event_ports ||
605             conf->nb_event_queues > dev->max_event_queues) {
606                 otx2_err("Unsupported event queues/ports requested");
607                 return -EINVAL;
608         }
609
610         if (conf->nb_event_port_dequeue_depth > 1) {
611                 otx2_err("Unsupported event port deq depth requested");
612                 return -EINVAL;
613         }
614
615         if (conf->nb_event_port_enqueue_depth > 1) {
616                 otx2_err("Unsupported event port enq depth requested");
617                 return -EINVAL;
618         }
619
620         if (dev->configured)
621                 sso_unregister_irqs(event_dev);
622
623         if (dev->nb_event_queues) {
624                 /* Finit any previous queues. */
625                 sso_lf_teardown(dev, SSO_LF_GGRP);
626         }
627         if (dev->nb_event_ports) {
628                 /* Finit any previous ports. */
629                 sso_lf_teardown(dev, SSO_LF_GWS);
630         }
631
632         dev->nb_event_queues = conf->nb_event_queues;
633         dev->nb_event_ports = conf->nb_event_ports;
634
635         if (dev->dual_ws)
636                 rc = sso_configure_dual_ports(event_dev);
637         else
638                 rc = sso_configure_ports(event_dev);
639
640         if (rc < 0) {
641                 otx2_err("Failed to configure event ports");
642                 return -ENODEV;
643         }
644
645         if (sso_configure_queues(event_dev) < 0) {
646                 otx2_err("Failed to configure event queues");
647                 rc = -ENODEV;
648                 goto teardown_hws;
649         }
650
651         if (sso_xaq_allocate(dev) < 0) {
652                 rc = -ENOMEM;
653                 goto teardown_hwggrp;
654         }
655
656         /* Clear any prior port-queue mapping. */
657         sso_clr_links(event_dev);
658         rc = sso_ggrp_alloc_xaq(dev);
659         if (rc < 0) {
660                 otx2_err("Failed to alloc xaq to ggrp %d", rc);
661                 goto teardown_hwggrp;
662         }
663
664         rc = sso_get_msix_offsets(event_dev);
665         if (rc < 0) {
666                 otx2_err("Failed to get msix offsets %d", rc);
667                 goto teardown_hwggrp;
668         }
669
670         rc = sso_register_irqs(event_dev);
671         if (rc < 0) {
672                 otx2_err("Failed to register irq %d", rc);
673                 goto teardown_hwggrp;
674         }
675
676         dev->configured = 1;
677         rte_mb();
678
679         return 0;
680 teardown_hwggrp:
681         sso_lf_teardown(dev, SSO_LF_GGRP);
682 teardown_hws:
683         sso_lf_teardown(dev, SSO_LF_GWS);
684         dev->nb_event_queues = 0;
685         dev->nb_event_ports = 0;
686         dev->configured = 0;
687         return rc;
688 }
689
690 static void
691 otx2_sso_queue_def_conf(struct rte_eventdev *event_dev, uint8_t queue_id,
692                         struct rte_event_queue_conf *queue_conf)
693 {
694         RTE_SET_USED(event_dev);
695         RTE_SET_USED(queue_id);
696
697         queue_conf->nb_atomic_flows = (1ULL << 20);
698         queue_conf->nb_atomic_order_sequences = (1ULL << 20);
699         queue_conf->event_queue_cfg = RTE_EVENT_QUEUE_CFG_ALL_TYPES;
700         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
701 }
702
703 static int
704 otx2_sso_queue_setup(struct rte_eventdev *event_dev, uint8_t queue_id,
705                      const struct rte_event_queue_conf *queue_conf)
706 {
707         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
708         struct otx2_mbox *mbox = dev->mbox;
709         struct sso_grp_priority *req;
710         int rc;
711
712         sso_func_trace("Queue=%d prio=%d", queue_id, queue_conf->priority);
713
714         req = otx2_mbox_alloc_msg_sso_grp_set_priority(dev->mbox);
715         req->grp = queue_id;
716         req->weight = 0xFF;
717         req->affinity = 0xFF;
718         /* Normalize <0-255> to <0-7> */
719         req->priority = queue_conf->priority / 32;
720
721         rc = otx2_mbox_process(mbox);
722         if (rc < 0) {
723                 otx2_err("Failed to set priority queue=%d", queue_id);
724                 return rc;
725         }
726
727         return 0;
728 }
729
730 static void
731 otx2_sso_port_def_conf(struct rte_eventdev *event_dev, uint8_t port_id,
732                        struct rte_event_port_conf *port_conf)
733 {
734         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
735
736         RTE_SET_USED(port_id);
737         port_conf->new_event_threshold = dev->max_num_events;
738         port_conf->dequeue_depth = 1;
739         port_conf->enqueue_depth = 1;
740 }
741
742 static int
743 otx2_sso_port_setup(struct rte_eventdev *event_dev, uint8_t port_id,
744                     const struct rte_event_port_conf *port_conf)
745 {
746         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
747         uintptr_t grps_base[OTX2_SSO_MAX_VHGRP] = {0};
748         uint64_t val;
749         uint16_t q;
750
751         sso_func_trace("Port=%d", port_id);
752         RTE_SET_USED(port_conf);
753
754         if (event_dev->data->ports[port_id] == NULL) {
755                 otx2_err("Invalid port Id %d", port_id);
756                 return -EINVAL;
757         }
758
759         for (q = 0; q < dev->nb_event_queues; q++) {
760                 grps_base[q] = dev->bar2 + (RVU_BLOCK_ADDR_SSO << 20 | q << 12);
761                 if (grps_base[q] == 0) {
762                         otx2_err("Failed to get grp[%d] base addr", q);
763                         return -EINVAL;
764                 }
765         }
766
767         /* Set get_work timeout for HWS */
768         val = NSEC2USEC(dev->deq_tmo_ns) - 1;
769
770         if (dev->dual_ws) {
771                 struct otx2_ssogws_dual *ws = event_dev->data->ports[port_id];
772
773                 rte_memcpy(ws->grps_base, grps_base,
774                            sizeof(uintptr_t) * OTX2_SSO_MAX_VHGRP);
775                 ws->fc_mem = dev->fc_mem;
776                 ws->xaq_lmt = dev->xaq_lmt;
777                 otx2_write64(val, OTX2_SSOW_GET_BASE_ADDR(
778                              ws->ws_state[0].getwrk_op) + SSOW_LF_GWS_NW_TIM);
779                 otx2_write64(val, OTX2_SSOW_GET_BASE_ADDR(
780                              ws->ws_state[1].getwrk_op) + SSOW_LF_GWS_NW_TIM);
781         } else {
782                 struct otx2_ssogws *ws = event_dev->data->ports[port_id];
783                 uintptr_t base = OTX2_SSOW_GET_BASE_ADDR(ws->getwrk_op);
784
785                 rte_memcpy(ws->grps_base, grps_base,
786                            sizeof(uintptr_t) * OTX2_SSO_MAX_VHGRP);
787                 ws->fc_mem = dev->fc_mem;
788                 ws->xaq_lmt = dev->xaq_lmt;
789                 otx2_write64(val, base + SSOW_LF_GWS_NW_TIM);
790         }
791
792         otx2_sso_dbg("Port=%d ws=%p", port_id, event_dev->data->ports[port_id]);
793
794         return 0;
795 }
796
797 static int
798 otx2_sso_timeout_ticks(struct rte_eventdev *event_dev, uint64_t ns,
799                        uint64_t *tmo_ticks)
800 {
801         RTE_SET_USED(event_dev);
802         *tmo_ticks = NSEC2TICK(ns, rte_get_timer_hz());
803
804         return 0;
805 }
806
807 static void
808 ssogws_dump(struct otx2_ssogws *ws, FILE *f)
809 {
810         uintptr_t base = OTX2_SSOW_GET_BASE_ADDR(ws->getwrk_op);
811
812         fprintf(f, "SSOW_LF_GWS Base addr   0x%" PRIx64 "\n", (uint64_t)base);
813         fprintf(f, "SSOW_LF_GWS_LINKS       0x%" PRIx64 "\n",
814                 otx2_read64(base + SSOW_LF_GWS_LINKS));
815         fprintf(f, "SSOW_LF_GWS_PENDWQP     0x%" PRIx64 "\n",
816                 otx2_read64(base + SSOW_LF_GWS_PENDWQP));
817         fprintf(f, "SSOW_LF_GWS_PENDSTATE   0x%" PRIx64 "\n",
818                 otx2_read64(base + SSOW_LF_GWS_PENDSTATE));
819         fprintf(f, "SSOW_LF_GWS_NW_TIM      0x%" PRIx64 "\n",
820                 otx2_read64(base + SSOW_LF_GWS_NW_TIM));
821         fprintf(f, "SSOW_LF_GWS_TAG         0x%" PRIx64 "\n",
822                 otx2_read64(base + SSOW_LF_GWS_TAG));
823         fprintf(f, "SSOW_LF_GWS_WQP         0x%" PRIx64 "\n",
824                 otx2_read64(base + SSOW_LF_GWS_TAG));
825         fprintf(f, "SSOW_LF_GWS_SWTP        0x%" PRIx64 "\n",
826                 otx2_read64(base + SSOW_LF_GWS_SWTP));
827         fprintf(f, "SSOW_LF_GWS_PENDTAG     0x%" PRIx64 "\n",
828                 otx2_read64(base + SSOW_LF_GWS_PENDTAG));
829 }
830
831 static void
832 ssoggrp_dump(uintptr_t base, FILE *f)
833 {
834         fprintf(f, "SSO_LF_GGRP Base addr   0x%" PRIx64 "\n", (uint64_t)base);
835         fprintf(f, "SSO_LF_GGRP_QCTL        0x%" PRIx64 "\n",
836                 otx2_read64(base + SSO_LF_GGRP_QCTL));
837         fprintf(f, "SSO_LF_GGRP_XAQ_CNT     0x%" PRIx64 "\n",
838                 otx2_read64(base + SSO_LF_GGRP_XAQ_CNT));
839         fprintf(f, "SSO_LF_GGRP_INT_THR     0x%" PRIx64 "\n",
840                 otx2_read64(base + SSO_LF_GGRP_INT_THR));
841         fprintf(f, "SSO_LF_GGRP_INT_CNT     0x%" PRIX64 "\n",
842                 otx2_read64(base + SSO_LF_GGRP_INT_CNT));
843         fprintf(f, "SSO_LF_GGRP_AQ_CNT      0x%" PRIX64 "\n",
844                 otx2_read64(base + SSO_LF_GGRP_AQ_CNT));
845         fprintf(f, "SSO_LF_GGRP_AQ_THR      0x%" PRIX64 "\n",
846                 otx2_read64(base + SSO_LF_GGRP_AQ_THR));
847         fprintf(f, "SSO_LF_GGRP_MISC_CNT    0x%" PRIx64 "\n",
848                 otx2_read64(base + SSO_LF_GGRP_MISC_CNT));
849 }
850
851 static void
852 otx2_sso_dump(struct rte_eventdev *event_dev, FILE *f)
853 {
854         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
855         uint8_t queue;
856         uint8_t port;
857
858         fprintf(f, "[%s] SSO running in [%s] mode\n", __func__, dev->dual_ws ?
859                 "dual_ws" : "single_ws");
860         /* Dump SSOW registers */
861         for (port = 0; port < dev->nb_event_ports; port++) {
862                 if (dev->dual_ws) {
863                         struct otx2_ssogws_dual *ws =
864                                 event_dev->data->ports[port];
865
866                         fprintf(f, "[%s] SSO dual workslot[%d] vws[%d] dump\n",
867                                 __func__, port, 0);
868                         ssogws_dump((struct otx2_ssogws *)&ws->ws_state[0], f);
869                         fprintf(f, "[%s]SSO dual workslot[%d] vws[%d] dump\n",
870                                 __func__, port, 1);
871                         ssogws_dump((struct otx2_ssogws *)&ws->ws_state[1], f);
872                 } else {
873                         fprintf(f, "[%s]SSO single workslot[%d] dump\n",
874                                 __func__, port);
875                         ssogws_dump(event_dev->data->ports[port], f);
876                 }
877         }
878
879         /* Dump SSO registers */
880         for (queue = 0; queue < dev->nb_event_queues; queue++) {
881                 fprintf(f, "[%s]SSO group[%d] dump\n", __func__, queue);
882                 if (dev->dual_ws) {
883                         struct otx2_ssogws_dual *ws = event_dev->data->ports[0];
884                         ssoggrp_dump(ws->grps_base[queue], f);
885                 } else {
886                         struct otx2_ssogws *ws = event_dev->data->ports[0];
887                         ssoggrp_dump(ws->grps_base[queue], f);
888                 }
889         }
890 }
891
892 /* Initialize and register event driver with DPDK Application */
893 static struct rte_eventdev_ops otx2_sso_ops = {
894         .dev_infos_get    = otx2_sso_info_get,
895         .dev_configure    = otx2_sso_configure,
896         .queue_def_conf   = otx2_sso_queue_def_conf,
897         .queue_setup      = otx2_sso_queue_setup,
898         .queue_release    = otx2_sso_queue_release,
899         .port_def_conf    = otx2_sso_port_def_conf,
900         .port_setup       = otx2_sso_port_setup,
901         .port_release     = otx2_sso_port_release,
902         .port_link        = otx2_sso_port_link,
903         .port_unlink      = otx2_sso_port_unlink,
904         .timeout_ticks    = otx2_sso_timeout_ticks,
905
906         .xstats_get       = otx2_sso_xstats_get,
907         .xstats_reset     = otx2_sso_xstats_reset,
908         .xstats_get_names = otx2_sso_xstats_get_names,
909
910         .dump             = otx2_sso_dump,
911 };
912
913 #define OTX2_SSO_XAE_CNT        "xae_cnt"
914 #define OTX2_SSO_SINGLE_WS      "single_ws"
915
916 static void
917 sso_parse_devargs(struct otx2_sso_evdev *dev, struct rte_devargs *devargs)
918 {
919         struct rte_kvargs *kvlist;
920         uint8_t single_ws = 0;
921
922         if (devargs == NULL)
923                 return;
924         kvlist = rte_kvargs_parse(devargs->args, NULL);
925         if (kvlist == NULL)
926                 return;
927
928         rte_kvargs_process(kvlist, OTX2_SSO_XAE_CNT, &parse_kvargs_value,
929                            &dev->xae_cnt);
930         rte_kvargs_process(kvlist, OTX2_SSO_SINGLE_WS, &parse_kvargs_flag,
931                            &single_ws);
932
933         dev->dual_ws = !single_ws;
934         rte_kvargs_free(kvlist);
935 }
936
937 static int
938 otx2_sso_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
939 {
940         return rte_event_pmd_pci_probe(pci_drv, pci_dev,
941                                        sizeof(struct otx2_sso_evdev),
942                                        otx2_sso_init);
943 }
944
945 static int
946 otx2_sso_remove(struct rte_pci_device *pci_dev)
947 {
948         return rte_event_pmd_pci_remove(pci_dev, otx2_sso_fini);
949 }
950
951 static const struct rte_pci_id pci_sso_map[] = {
952         {
953                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
954                                PCI_DEVID_OCTEONTX2_RVU_SSO_TIM_PF)
955         },
956         {
957                 .vendor_id = 0,
958         },
959 };
960
961 static struct rte_pci_driver pci_sso = {
962         .id_table = pci_sso_map,
963         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
964         .probe = otx2_sso_probe,
965         .remove = otx2_sso_remove,
966 };
967
968 int
969 otx2_sso_init(struct rte_eventdev *event_dev)
970 {
971         struct free_rsrcs_rsp *rsrc_cnt;
972         struct rte_pci_device *pci_dev;
973         struct otx2_sso_evdev *dev;
974         int rc;
975
976         event_dev->dev_ops = &otx2_sso_ops;
977         /* For secondary processes, the primary has done all the work */
978         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
979                 return 0;
980
981         dev = sso_pmd_priv(event_dev);
982
983         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
984
985         /* Initialize the base otx2_dev object */
986         rc = otx2_dev_init(pci_dev, dev);
987         if (rc < 0) {
988                 otx2_err("Failed to initialize otx2_dev rc=%d", rc);
989                 goto error;
990         }
991
992         /* Get SSO and SSOW MSIX rsrc cnt */
993         otx2_mbox_alloc_msg_free_rsrc_cnt(dev->mbox);
994         rc = otx2_mbox_process_msg(dev->mbox, (void *)&rsrc_cnt);
995         if (rc < 0) {
996                 otx2_err("Unable to get free rsrc count");
997                 goto otx2_dev_uninit;
998         }
999         otx2_sso_dbg("SSO %d SSOW %d NPA %d provisioned", rsrc_cnt->sso,
1000                      rsrc_cnt->ssow, rsrc_cnt->npa);
1001
1002         dev->max_event_ports = RTE_MIN(rsrc_cnt->ssow, OTX2_SSO_MAX_VHWS);
1003         dev->max_event_queues = RTE_MIN(rsrc_cnt->sso, OTX2_SSO_MAX_VHGRP);
1004         /* Grab the NPA LF if required */
1005         rc = otx2_npa_lf_init(pci_dev, dev);
1006         if (rc < 0) {
1007                 otx2_err("Unable to init NPA lf. It might not be provisioned");
1008                 goto otx2_dev_uninit;
1009         }
1010
1011         dev->drv_inited = true;
1012         dev->is_timeout_deq = 0;
1013         dev->min_dequeue_timeout_ns = USEC2NSEC(1);
1014         dev->max_dequeue_timeout_ns = USEC2NSEC(0x3FF);
1015         dev->max_num_events = -1;
1016         dev->nb_event_queues = 0;
1017         dev->nb_event_ports = 0;
1018
1019         if (!dev->max_event_ports || !dev->max_event_queues) {
1020                 otx2_err("Not enough eventdev resource queues=%d ports=%d",
1021                          dev->max_event_queues, dev->max_event_ports);
1022                 rc = -ENODEV;
1023                 goto otx2_npa_lf_uninit;
1024         }
1025
1026         dev->dual_ws = 1;
1027         sso_parse_devargs(dev, pci_dev->device.devargs);
1028         if (dev->dual_ws) {
1029                 otx2_sso_dbg("Using dual workslot mode");
1030                 dev->max_event_ports = dev->max_event_ports / 2;
1031         } else {
1032                 otx2_sso_dbg("Using single workslot mode");
1033         }
1034
1035         otx2_sso_pf_func_set(dev->pf_func);
1036         otx2_sso_dbg("Initializing %s max_queues=%d max_ports=%d",
1037                      event_dev->data->name, dev->max_event_queues,
1038                      dev->max_event_ports);
1039
1040
1041         return 0;
1042
1043 otx2_npa_lf_uninit:
1044         otx2_npa_lf_fini();
1045 otx2_dev_uninit:
1046         otx2_dev_fini(pci_dev, dev);
1047 error:
1048         return rc;
1049 }
1050
1051 int
1052 otx2_sso_fini(struct rte_eventdev *event_dev)
1053 {
1054         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
1055         struct rte_pci_device *pci_dev;
1056
1057         /* For secondary processes, nothing to be done */
1058         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1059                 return 0;
1060
1061         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
1062
1063         if (!dev->drv_inited)
1064                 goto dev_fini;
1065
1066         dev->drv_inited = false;
1067         otx2_npa_lf_fini();
1068
1069 dev_fini:
1070         if (otx2_npa_lf_active(dev)) {
1071                 otx2_info("Common resource in use by other devices");
1072                 return -EAGAIN;
1073         }
1074
1075         otx2_dev_fini(pci_dev, dev);
1076
1077         return 0;
1078 }
1079
1080 RTE_PMD_REGISTER_PCI(event_octeontx2, pci_sso);
1081 RTE_PMD_REGISTER_PCI_TABLE(event_octeontx2, pci_sso_map);
1082 RTE_PMD_REGISTER_KMOD_DEP(event_octeontx2, "vfio-pci");
1083 RTE_PMD_REGISTER_PARAM_STRING(event_octeontx2, OTX2_SSO_XAE_CNT "=<int>"
1084                               OTX2_SSO_SINGLE_WS "=1");