event/octeontx2: allocate event inflight buffers
[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_mbuf_pool_ops.h>
12 #include <rte_pci.h>
13
14 #include "otx2_evdev.h"
15
16 static void
17 otx2_sso_info_get(struct rte_eventdev *event_dev,
18                   struct rte_event_dev_info *dev_info)
19 {
20         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
21
22         dev_info->driver_name = RTE_STR(EVENTDEV_NAME_OCTEONTX2_PMD);
23         dev_info->min_dequeue_timeout_ns = dev->min_dequeue_timeout_ns;
24         dev_info->max_dequeue_timeout_ns = dev->max_dequeue_timeout_ns;
25         dev_info->max_event_queues = dev->max_event_queues;
26         dev_info->max_event_queue_flows = (1ULL << 20);
27         dev_info->max_event_queue_priority_levels = 8;
28         dev_info->max_event_priority_levels = 1;
29         dev_info->max_event_ports = dev->max_event_ports;
30         dev_info->max_event_port_dequeue_depth = 1;
31         dev_info->max_event_port_enqueue_depth = 1;
32         dev_info->max_num_events =  dev->max_num_events;
33         dev_info->event_dev_cap = RTE_EVENT_DEV_CAP_QUEUE_QOS |
34                                         RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
35                                         RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES |
36                                         RTE_EVENT_DEV_CAP_RUNTIME_PORT_LINK |
37                                         RTE_EVENT_DEV_CAP_MULTIPLE_QUEUE_PORT |
38                                         RTE_EVENT_DEV_CAP_NONSEQ_MODE;
39 }
40
41 static int
42 sso_hw_lf_cfg(struct otx2_mbox *mbox, enum otx2_sso_lf_type type,
43               uint16_t nb_lf, uint8_t attach)
44 {
45         if (attach) {
46                 struct rsrc_attach_req *req;
47
48                 req = otx2_mbox_alloc_msg_attach_resources(mbox);
49                 switch (type) {
50                 case SSO_LF_GGRP:
51                         req->sso = nb_lf;
52                         break;
53                 case SSO_LF_GWS:
54                         req->ssow = nb_lf;
55                         break;
56                 default:
57                         return -EINVAL;
58                 }
59                 req->modify = true;
60                 if (otx2_mbox_process(mbox) < 0)
61                         return -EIO;
62         } else {
63                 struct rsrc_detach_req *req;
64
65                 req = otx2_mbox_alloc_msg_detach_resources(mbox);
66                 switch (type) {
67                 case SSO_LF_GGRP:
68                         req->sso = true;
69                         break;
70                 case SSO_LF_GWS:
71                         req->ssow = true;
72                         break;
73                 default:
74                         return -EINVAL;
75                 }
76                 req->partial = true;
77                 if (otx2_mbox_process(mbox) < 0)
78                         return -EIO;
79         }
80
81         return 0;
82 }
83
84 static int
85 sso_lf_cfg(struct otx2_sso_evdev *dev, struct otx2_mbox *mbox,
86            enum otx2_sso_lf_type type, uint16_t nb_lf, uint8_t alloc)
87 {
88         void *rsp;
89         int rc;
90
91         if (alloc) {
92                 switch (type) {
93                 case SSO_LF_GGRP:
94                         {
95                         struct sso_lf_alloc_req *req_ggrp;
96                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_alloc(mbox);
97                         req_ggrp->hwgrps = nb_lf;
98                         }
99                         break;
100                 case SSO_LF_GWS:
101                         {
102                         struct ssow_lf_alloc_req *req_hws;
103                         req_hws = otx2_mbox_alloc_msg_ssow_lf_alloc(mbox);
104                         req_hws->hws = nb_lf;
105                         }
106                         break;
107                 default:
108                         return -EINVAL;
109                 }
110         } else {
111                 switch (type) {
112                 case SSO_LF_GGRP:
113                         {
114                         struct sso_lf_free_req *req_ggrp;
115                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_free(mbox);
116                         req_ggrp->hwgrps = nb_lf;
117                         }
118                         break;
119                 case SSO_LF_GWS:
120                         {
121                         struct ssow_lf_free_req *req_hws;
122                         req_hws = otx2_mbox_alloc_msg_ssow_lf_free(mbox);
123                         req_hws->hws = nb_lf;
124                         }
125                         break;
126                 default:
127                         return -EINVAL;
128                 }
129         }
130
131         rc = otx2_mbox_process_msg_tmo(mbox, (void **)&rsp, ~0);
132         if (rc < 0)
133                 return rc;
134
135         if (alloc && type == SSO_LF_GGRP) {
136                 struct sso_lf_alloc_rsp *rsp_ggrp = rsp;
137
138                 dev->xaq_buf_size = rsp_ggrp->xaq_buf_size;
139                 dev->xae_waes = rsp_ggrp->xaq_wq_entries;
140                 dev->iue = rsp_ggrp->in_unit_entries;
141         }
142
143         return 0;
144 }
145
146 static void
147 otx2_sso_queue_release(struct rte_eventdev *event_dev, uint8_t queue_id)
148 {
149         RTE_SET_USED(event_dev);
150         RTE_SET_USED(queue_id);
151 }
152
153 static int
154 sso_configure_ports(const struct rte_eventdev *event_dev)
155 {
156         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
157         struct otx2_mbox *mbox = dev->mbox;
158         uint8_t nb_lf;
159         int rc;
160
161         otx2_sso_dbg("Configuring event ports %d", dev->nb_event_ports);
162
163         nb_lf = dev->nb_event_ports;
164         /* Ask AF to attach required LFs. */
165         rc = sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, true);
166         if (rc < 0) {
167                 otx2_err("Failed to attach SSO GWS LF");
168                 return -ENODEV;
169         }
170
171         if (sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, true) < 0) {
172                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
173                 otx2_err("Failed to init SSO GWS LF");
174                 return -ENODEV;
175         }
176
177         return rc;
178 }
179
180 static int
181 sso_configure_queues(const struct rte_eventdev *event_dev)
182 {
183         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
184         struct otx2_mbox *mbox = dev->mbox;
185         uint8_t nb_lf;
186         int rc;
187
188         otx2_sso_dbg("Configuring event queues %d", dev->nb_event_queues);
189
190         nb_lf = dev->nb_event_queues;
191         /* Ask AF to attach required LFs. */
192         rc = sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, true);
193         if (rc < 0) {
194                 otx2_err("Failed to attach SSO GGRP LF");
195                 return -ENODEV;
196         }
197
198         if (sso_lf_cfg(dev, mbox, SSO_LF_GGRP, nb_lf, true) < 0) {
199                 sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, false);
200                 otx2_err("Failed to init SSO GGRP LF");
201                 return -ENODEV;
202         }
203
204         return rc;
205 }
206
207 static int
208 sso_xaq_allocate(struct otx2_sso_evdev *dev)
209 {
210         const struct rte_memzone *mz;
211         struct npa_aura_s *aura;
212         static int reconfig_cnt;
213         char pool_name[RTE_MEMZONE_NAMESIZE];
214         uint32_t xaq_cnt;
215         int rc;
216
217         if (dev->xaq_pool)
218                 rte_mempool_free(dev->xaq_pool);
219
220         /*
221          * Allocate memory for Add work backpressure.
222          */
223         mz = rte_memzone_lookup(OTX2_SSO_FC_NAME);
224         if (mz == NULL)
225                 mz = rte_memzone_reserve_aligned(OTX2_SSO_FC_NAME,
226                                                  OTX2_ALIGN +
227                                                  sizeof(struct npa_aura_s),
228                                                  rte_socket_id(),
229                                                  RTE_MEMZONE_IOVA_CONTIG,
230                                                  OTX2_ALIGN);
231         if (mz == NULL) {
232                 otx2_err("Failed to allocate mem for fcmem");
233                 return -ENOMEM;
234         }
235
236         dev->fc_iova = mz->iova;
237         dev->fc_mem = mz->addr;
238
239         aura = (struct npa_aura_s *)((uintptr_t)dev->fc_mem + OTX2_ALIGN);
240         memset(aura, 0, sizeof(struct npa_aura_s));
241
242         aura->fc_ena = 1;
243         aura->fc_addr = dev->fc_iova;
244         aura->fc_hyst_bits = 0; /* Store count on all updates */
245
246         /* Taken from HRM 14.3.3(4) */
247         xaq_cnt = dev->nb_event_queues * OTX2_SSO_XAQ_CACHE_CNT;
248         xaq_cnt += (dev->iue / dev->xae_waes) +
249                         (OTX2_SSO_XAQ_SLACK * dev->nb_event_queues);
250
251         otx2_sso_dbg("Configuring %d xaq buffers", xaq_cnt);
252         /* Setup XAQ based on number of nb queues. */
253         snprintf(pool_name, 30, "otx2_xaq_buf_pool_%d", reconfig_cnt);
254         dev->xaq_pool = (void *)rte_mempool_create_empty(pool_name,
255                         xaq_cnt, dev->xaq_buf_size, 0, 0,
256                         rte_socket_id(), 0);
257
258         if (dev->xaq_pool == NULL) {
259                 otx2_err("Unable to create empty mempool.");
260                 rte_memzone_free(mz);
261                 return -ENOMEM;
262         }
263
264         rc = rte_mempool_set_ops_byname(dev->xaq_pool,
265                                         rte_mbuf_platform_mempool_ops(), aura);
266         if (rc != 0) {
267                 otx2_err("Unable to set xaqpool ops.");
268                 goto alloc_fail;
269         }
270
271         rc = rte_mempool_populate_default(dev->xaq_pool);
272         if (rc < 0) {
273                 otx2_err("Unable to set populate xaqpool.");
274                 goto alloc_fail;
275         }
276         reconfig_cnt++;
277         /* When SW does addwork (enqueue) check if there is space in XAQ by
278          * comparing fc_addr above against the xaq_lmt calculated below.
279          * There should be a minimum headroom (OTX2_SSO_XAQ_SLACK / 2) for SSO
280          * to request XAQ to cache them even before enqueue is called.
281          */
282         dev->xaq_lmt = xaq_cnt - (OTX2_SSO_XAQ_SLACK / 2 *
283                                   dev->nb_event_queues);
284         dev->nb_xaq_cfg = xaq_cnt;
285
286         return 0;
287 alloc_fail:
288         rte_mempool_free(dev->xaq_pool);
289         rte_memzone_free(mz);
290         return rc;
291 }
292
293 static int
294 sso_ggrp_alloc_xaq(struct otx2_sso_evdev *dev)
295 {
296         struct otx2_mbox *mbox = dev->mbox;
297         struct sso_hw_setconfig *req;
298
299         otx2_sso_dbg("Configuring XAQ for GGRPs");
300         req = otx2_mbox_alloc_msg_sso_hw_setconfig(mbox);
301         req->npa_pf_func = otx2_npa_pf_func_get();
302         req->npa_aura_id = npa_lf_aura_handle_to_aura(dev->xaq_pool->pool_id);
303         req->hwgrps = dev->nb_event_queues;
304
305         return otx2_mbox_process(mbox);
306 }
307
308 static void
309 sso_lf_teardown(struct otx2_sso_evdev *dev,
310                 enum otx2_sso_lf_type lf_type)
311 {
312         uint8_t nb_lf;
313
314         switch (lf_type) {
315         case SSO_LF_GGRP:
316                 nb_lf = dev->nb_event_queues;
317                 break;
318         case SSO_LF_GWS:
319                 nb_lf = dev->nb_event_ports;
320                 break;
321         default:
322                 return;
323         }
324
325         sso_lf_cfg(dev, dev->mbox, lf_type, nb_lf, false);
326         sso_hw_lf_cfg(dev->mbox, lf_type, nb_lf, false);
327 }
328
329 static int
330 otx2_sso_configure(const struct rte_eventdev *event_dev)
331 {
332         struct rte_event_dev_config *conf = &event_dev->data->dev_conf;
333         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
334         uint32_t deq_tmo_ns;
335         int rc;
336
337         sso_func_trace();
338         deq_tmo_ns = conf->dequeue_timeout_ns;
339
340         if (deq_tmo_ns == 0)
341                 deq_tmo_ns = dev->min_dequeue_timeout_ns;
342
343         if (deq_tmo_ns < dev->min_dequeue_timeout_ns ||
344             deq_tmo_ns > dev->max_dequeue_timeout_ns) {
345                 otx2_err("Unsupported dequeue timeout requested");
346                 return -EINVAL;
347         }
348
349         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT)
350                 dev->is_timeout_deq = 1;
351
352         dev->deq_tmo_ns = deq_tmo_ns;
353
354         if (conf->nb_event_ports > dev->max_event_ports ||
355             conf->nb_event_queues > dev->max_event_queues) {
356                 otx2_err("Unsupported event queues/ports requested");
357                 return -EINVAL;
358         }
359
360         if (conf->nb_event_port_dequeue_depth > 1) {
361                 otx2_err("Unsupported event port deq depth requested");
362                 return -EINVAL;
363         }
364
365         if (conf->nb_event_port_enqueue_depth > 1) {
366                 otx2_err("Unsupported event port enq depth requested");
367                 return -EINVAL;
368         }
369
370         if (dev->nb_event_queues) {
371                 /* Finit any previous queues. */
372                 sso_lf_teardown(dev, SSO_LF_GGRP);
373         }
374         if (dev->nb_event_ports) {
375                 /* Finit any previous ports. */
376                 sso_lf_teardown(dev, SSO_LF_GWS);
377         }
378
379         dev->nb_event_queues = conf->nb_event_queues;
380         dev->nb_event_ports = conf->nb_event_ports;
381
382         if (sso_configure_ports(event_dev)) {
383                 otx2_err("Failed to configure event ports");
384                 return -ENODEV;
385         }
386
387         if (sso_configure_queues(event_dev) < 0) {
388                 otx2_err("Failed to configure event queues");
389                 rc = -ENODEV;
390                 goto teardown_hws;
391         }
392
393         if (sso_xaq_allocate(dev) < 0) {
394                 rc = -ENOMEM;
395                 goto teardown_hwggrp;
396         }
397
398         rc = sso_ggrp_alloc_xaq(dev);
399         if (rc < 0) {
400                 otx2_err("Failed to alloc xaq to ggrp %d", rc);
401                 goto teardown_hwggrp;
402         }
403
404         dev->configured = 1;
405         rte_mb();
406
407         return 0;
408 teardown_hwggrp:
409         sso_lf_teardown(dev, SSO_LF_GGRP);
410 teardown_hws:
411         sso_lf_teardown(dev, SSO_LF_GWS);
412         dev->nb_event_queues = 0;
413         dev->nb_event_ports = 0;
414         dev->configured = 0;
415         return rc;
416 }
417
418 static void
419 otx2_sso_queue_def_conf(struct rte_eventdev *event_dev, uint8_t queue_id,
420                         struct rte_event_queue_conf *queue_conf)
421 {
422         RTE_SET_USED(event_dev);
423         RTE_SET_USED(queue_id);
424
425         queue_conf->nb_atomic_flows = (1ULL << 20);
426         queue_conf->nb_atomic_order_sequences = (1ULL << 20);
427         queue_conf->event_queue_cfg = RTE_EVENT_QUEUE_CFG_ALL_TYPES;
428         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
429 }
430
431 static int
432 otx2_sso_queue_setup(struct rte_eventdev *event_dev, uint8_t queue_id,
433                      const struct rte_event_queue_conf *queue_conf)
434 {
435         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
436         struct otx2_mbox *mbox = dev->mbox;
437         struct sso_grp_priority *req;
438         int rc;
439
440         sso_func_trace("Queue=%d prio=%d", queue_id, queue_conf->priority);
441
442         req = otx2_mbox_alloc_msg_sso_grp_set_priority(dev->mbox);
443         req->grp = queue_id;
444         req->weight = 0xFF;
445         req->affinity = 0xFF;
446         /* Normalize <0-255> to <0-7> */
447         req->priority = queue_conf->priority / 32;
448
449         rc = otx2_mbox_process(mbox);
450         if (rc < 0) {
451                 otx2_err("Failed to set priority queue=%d", queue_id);
452                 return rc;
453         }
454
455         return 0;
456 }
457
458 /* Initialize and register event driver with DPDK Application */
459 static struct rte_eventdev_ops otx2_sso_ops = {
460         .dev_infos_get    = otx2_sso_info_get,
461         .dev_configure    = otx2_sso_configure,
462         .queue_def_conf   = otx2_sso_queue_def_conf,
463         .queue_setup      = otx2_sso_queue_setup,
464         .queue_release    = otx2_sso_queue_release,
465 };
466
467 static int
468 otx2_sso_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
469 {
470         return rte_event_pmd_pci_probe(pci_drv, pci_dev,
471                                        sizeof(struct otx2_sso_evdev),
472                                        otx2_sso_init);
473 }
474
475 static int
476 otx2_sso_remove(struct rte_pci_device *pci_dev)
477 {
478         return rte_event_pmd_pci_remove(pci_dev, otx2_sso_fini);
479 }
480
481 static const struct rte_pci_id pci_sso_map[] = {
482         {
483                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
484                                PCI_DEVID_OCTEONTX2_RVU_SSO_TIM_PF)
485         },
486         {
487                 .vendor_id = 0,
488         },
489 };
490
491 static struct rte_pci_driver pci_sso = {
492         .id_table = pci_sso_map,
493         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
494         .probe = otx2_sso_probe,
495         .remove = otx2_sso_remove,
496 };
497
498 int
499 otx2_sso_init(struct rte_eventdev *event_dev)
500 {
501         struct free_rsrcs_rsp *rsrc_cnt;
502         struct rte_pci_device *pci_dev;
503         struct otx2_sso_evdev *dev;
504         int rc;
505
506         event_dev->dev_ops = &otx2_sso_ops;
507         /* For secondary processes, the primary has done all the work */
508         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
509                 return 0;
510
511         dev = sso_pmd_priv(event_dev);
512
513         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
514
515         /* Initialize the base otx2_dev object */
516         rc = otx2_dev_init(pci_dev, dev);
517         if (rc < 0) {
518                 otx2_err("Failed to initialize otx2_dev rc=%d", rc);
519                 goto error;
520         }
521
522         /* Get SSO and SSOW MSIX rsrc cnt */
523         otx2_mbox_alloc_msg_free_rsrc_cnt(dev->mbox);
524         rc = otx2_mbox_process_msg(dev->mbox, (void *)&rsrc_cnt);
525         if (rc < 0) {
526                 otx2_err("Unable to get free rsrc count");
527                 goto otx2_dev_uninit;
528         }
529         otx2_sso_dbg("SSO %d SSOW %d NPA %d provisioned", rsrc_cnt->sso,
530                      rsrc_cnt->ssow, rsrc_cnt->npa);
531
532         dev->max_event_ports = RTE_MIN(rsrc_cnt->ssow, OTX2_SSO_MAX_VHWS);
533         dev->max_event_queues = RTE_MIN(rsrc_cnt->sso, OTX2_SSO_MAX_VHGRP);
534         /* Grab the NPA LF if required */
535         rc = otx2_npa_lf_init(pci_dev, dev);
536         if (rc < 0) {
537                 otx2_err("Unable to init NPA lf. It might not be provisioned");
538                 goto otx2_dev_uninit;
539         }
540
541         dev->drv_inited = true;
542         dev->is_timeout_deq = 0;
543         dev->min_dequeue_timeout_ns = USEC2NSEC(1);
544         dev->max_dequeue_timeout_ns = USEC2NSEC(0x3FF);
545         dev->max_num_events = -1;
546         dev->nb_event_queues = 0;
547         dev->nb_event_ports = 0;
548
549         if (!dev->max_event_ports || !dev->max_event_queues) {
550                 otx2_err("Not enough eventdev resource queues=%d ports=%d",
551                          dev->max_event_queues, dev->max_event_ports);
552                 rc = -ENODEV;
553                 goto otx2_npa_lf_uninit;
554         }
555
556         otx2_sso_pf_func_set(dev->pf_func);
557         otx2_sso_dbg("Initializing %s max_queues=%d max_ports=%d",
558                      event_dev->data->name, dev->max_event_queues,
559                      dev->max_event_ports);
560
561
562         return 0;
563
564 otx2_npa_lf_uninit:
565         otx2_npa_lf_fini();
566 otx2_dev_uninit:
567         otx2_dev_fini(pci_dev, dev);
568 error:
569         return rc;
570 }
571
572 int
573 otx2_sso_fini(struct rte_eventdev *event_dev)
574 {
575         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
576         struct rte_pci_device *pci_dev;
577
578         /* For secondary processes, nothing to be done */
579         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
580                 return 0;
581
582         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
583
584         if (!dev->drv_inited)
585                 goto dev_fini;
586
587         dev->drv_inited = false;
588         otx2_npa_lf_fini();
589
590 dev_fini:
591         if (otx2_npa_lf_active(dev)) {
592                 otx2_info("Common resource in use by other devices");
593                 return -EAGAIN;
594         }
595
596         otx2_dev_fini(pci_dev, dev);
597
598         return 0;
599 }
600
601 RTE_PMD_REGISTER_PCI(event_octeontx2, pci_sso);
602 RTE_PMD_REGISTER_PCI_TABLE(event_octeontx2, pci_sso_map);
603 RTE_PMD_REGISTER_KMOD_DEP(event_octeontx2, "vfio-pci");