2290598d0a9c4e200c75ca6d5549c9cbf62d53b7
[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_pci.h>
12
13 #include "otx2_evdev.h"
14
15 static void
16 otx2_sso_info_get(struct rte_eventdev *event_dev,
17                   struct rte_event_dev_info *dev_info)
18 {
19         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
20
21         dev_info->driver_name = RTE_STR(EVENTDEV_NAME_OCTEONTX2_PMD);
22         dev_info->min_dequeue_timeout_ns = dev->min_dequeue_timeout_ns;
23         dev_info->max_dequeue_timeout_ns = dev->max_dequeue_timeout_ns;
24         dev_info->max_event_queues = dev->max_event_queues;
25         dev_info->max_event_queue_flows = (1ULL << 20);
26         dev_info->max_event_queue_priority_levels = 8;
27         dev_info->max_event_priority_levels = 1;
28         dev_info->max_event_ports = dev->max_event_ports;
29         dev_info->max_event_port_dequeue_depth = 1;
30         dev_info->max_event_port_enqueue_depth = 1;
31         dev_info->max_num_events =  dev->max_num_events;
32         dev_info->event_dev_cap = RTE_EVENT_DEV_CAP_QUEUE_QOS |
33                                         RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
34                                         RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES |
35                                         RTE_EVENT_DEV_CAP_RUNTIME_PORT_LINK |
36                                         RTE_EVENT_DEV_CAP_MULTIPLE_QUEUE_PORT |
37                                         RTE_EVENT_DEV_CAP_NONSEQ_MODE;
38 }
39
40 static int
41 sso_hw_lf_cfg(struct otx2_mbox *mbox, enum otx2_sso_lf_type type,
42               uint16_t nb_lf, uint8_t attach)
43 {
44         if (attach) {
45                 struct rsrc_attach_req *req;
46
47                 req = otx2_mbox_alloc_msg_attach_resources(mbox);
48                 switch (type) {
49                 case SSO_LF_GGRP:
50                         req->sso = nb_lf;
51                         break;
52                 case SSO_LF_GWS:
53                         req->ssow = nb_lf;
54                         break;
55                 default:
56                         return -EINVAL;
57                 }
58                 req->modify = true;
59                 if (otx2_mbox_process(mbox) < 0)
60                         return -EIO;
61         } else {
62                 struct rsrc_detach_req *req;
63
64                 req = otx2_mbox_alloc_msg_detach_resources(mbox);
65                 switch (type) {
66                 case SSO_LF_GGRP:
67                         req->sso = true;
68                         break;
69                 case SSO_LF_GWS:
70                         req->ssow = true;
71                         break;
72                 default:
73                         return -EINVAL;
74                 }
75                 req->partial = true;
76                 if (otx2_mbox_process(mbox) < 0)
77                         return -EIO;
78         }
79
80         return 0;
81 }
82
83 static int
84 sso_lf_cfg(struct otx2_sso_evdev *dev, struct otx2_mbox *mbox,
85            enum otx2_sso_lf_type type, uint16_t nb_lf, uint8_t alloc)
86 {
87         void *rsp;
88         int rc;
89
90         if (alloc) {
91                 switch (type) {
92                 case SSO_LF_GGRP:
93                         {
94                         struct sso_lf_alloc_req *req_ggrp;
95                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_alloc(mbox);
96                         req_ggrp->hwgrps = nb_lf;
97                         }
98                         break;
99                 case SSO_LF_GWS:
100                         {
101                         struct ssow_lf_alloc_req *req_hws;
102                         req_hws = otx2_mbox_alloc_msg_ssow_lf_alloc(mbox);
103                         req_hws->hws = nb_lf;
104                         }
105                         break;
106                 default:
107                         return -EINVAL;
108                 }
109         } else {
110                 switch (type) {
111                 case SSO_LF_GGRP:
112                         {
113                         struct sso_lf_free_req *req_ggrp;
114                         req_ggrp = otx2_mbox_alloc_msg_sso_lf_free(mbox);
115                         req_ggrp->hwgrps = nb_lf;
116                         }
117                         break;
118                 case SSO_LF_GWS:
119                         {
120                         struct ssow_lf_free_req *req_hws;
121                         req_hws = otx2_mbox_alloc_msg_ssow_lf_free(mbox);
122                         req_hws->hws = nb_lf;
123                         }
124                         break;
125                 default:
126                         return -EINVAL;
127                 }
128         }
129
130         rc = otx2_mbox_process_msg_tmo(mbox, (void **)&rsp, ~0);
131         if (rc < 0)
132                 return rc;
133
134         if (alloc && type == SSO_LF_GGRP) {
135                 struct sso_lf_alloc_rsp *rsp_ggrp = rsp;
136
137                 dev->xaq_buf_size = rsp_ggrp->xaq_buf_size;
138                 dev->xae_waes = rsp_ggrp->xaq_wq_entries;
139                 dev->iue = rsp_ggrp->in_unit_entries;
140         }
141
142         return 0;
143 }
144
145 static void
146 otx2_sso_queue_release(struct rte_eventdev *event_dev, uint8_t queue_id)
147 {
148         RTE_SET_USED(event_dev);
149         RTE_SET_USED(queue_id);
150 }
151
152 static int
153 sso_configure_ports(const struct rte_eventdev *event_dev)
154 {
155         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
156         struct otx2_mbox *mbox = dev->mbox;
157         uint8_t nb_lf;
158         int rc;
159
160         otx2_sso_dbg("Configuring event ports %d", dev->nb_event_ports);
161
162         nb_lf = dev->nb_event_ports;
163         /* Ask AF to attach required LFs. */
164         rc = sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, true);
165         if (rc < 0) {
166                 otx2_err("Failed to attach SSO GWS LF");
167                 return -ENODEV;
168         }
169
170         if (sso_lf_cfg(dev, mbox, SSO_LF_GWS, nb_lf, true) < 0) {
171                 sso_hw_lf_cfg(mbox, SSO_LF_GWS, nb_lf, false);
172                 otx2_err("Failed to init SSO GWS LF");
173                 return -ENODEV;
174         }
175
176         return rc;
177 }
178
179 static int
180 sso_configure_queues(const struct rte_eventdev *event_dev)
181 {
182         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
183         struct otx2_mbox *mbox = dev->mbox;
184         uint8_t nb_lf;
185         int rc;
186
187         otx2_sso_dbg("Configuring event queues %d", dev->nb_event_queues);
188
189         nb_lf = dev->nb_event_queues;
190         /* Ask AF to attach required LFs. */
191         rc = sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, true);
192         if (rc < 0) {
193                 otx2_err("Failed to attach SSO GGRP LF");
194                 return -ENODEV;
195         }
196
197         if (sso_lf_cfg(dev, mbox, SSO_LF_GGRP, nb_lf, true) < 0) {
198                 sso_hw_lf_cfg(mbox, SSO_LF_GGRP, nb_lf, false);
199                 otx2_err("Failed to init SSO GGRP LF");
200                 return -ENODEV;
201         }
202
203         return rc;
204 }
205
206 static void
207 sso_lf_teardown(struct otx2_sso_evdev *dev,
208                 enum otx2_sso_lf_type lf_type)
209 {
210         uint8_t nb_lf;
211
212         switch (lf_type) {
213         case SSO_LF_GGRP:
214                 nb_lf = dev->nb_event_queues;
215                 break;
216         case SSO_LF_GWS:
217                 nb_lf = dev->nb_event_ports;
218                 break;
219         default:
220                 return;
221         }
222
223         sso_lf_cfg(dev, dev->mbox, lf_type, nb_lf, false);
224         sso_hw_lf_cfg(dev->mbox, lf_type, nb_lf, false);
225 }
226
227 static int
228 otx2_sso_configure(const struct rte_eventdev *event_dev)
229 {
230         struct rte_event_dev_config *conf = &event_dev->data->dev_conf;
231         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
232         uint32_t deq_tmo_ns;
233         int rc;
234
235         sso_func_trace();
236         deq_tmo_ns = conf->dequeue_timeout_ns;
237
238         if (deq_tmo_ns == 0)
239                 deq_tmo_ns = dev->min_dequeue_timeout_ns;
240
241         if (deq_tmo_ns < dev->min_dequeue_timeout_ns ||
242             deq_tmo_ns > dev->max_dequeue_timeout_ns) {
243                 otx2_err("Unsupported dequeue timeout requested");
244                 return -EINVAL;
245         }
246
247         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT)
248                 dev->is_timeout_deq = 1;
249
250         dev->deq_tmo_ns = deq_tmo_ns;
251
252         if (conf->nb_event_ports > dev->max_event_ports ||
253             conf->nb_event_queues > dev->max_event_queues) {
254                 otx2_err("Unsupported event queues/ports requested");
255                 return -EINVAL;
256         }
257
258         if (conf->nb_event_port_dequeue_depth > 1) {
259                 otx2_err("Unsupported event port deq depth requested");
260                 return -EINVAL;
261         }
262
263         if (conf->nb_event_port_enqueue_depth > 1) {
264                 otx2_err("Unsupported event port enq depth requested");
265                 return -EINVAL;
266         }
267
268         if (dev->nb_event_queues) {
269                 /* Finit any previous queues. */
270                 sso_lf_teardown(dev, SSO_LF_GGRP);
271         }
272         if (dev->nb_event_ports) {
273                 /* Finit any previous ports. */
274                 sso_lf_teardown(dev, SSO_LF_GWS);
275         }
276
277         dev->nb_event_queues = conf->nb_event_queues;
278         dev->nb_event_ports = conf->nb_event_ports;
279
280         if (sso_configure_ports(event_dev)) {
281                 otx2_err("Failed to configure event ports");
282                 return -ENODEV;
283         }
284
285         if (sso_configure_queues(event_dev) < 0) {
286                 otx2_err("Failed to configure event queues");
287                 rc = -ENODEV;
288                 goto teardown_hws;
289         }
290
291         dev->configured = 1;
292         rte_mb();
293
294         return 0;
295
296 teardown_hws:
297         sso_lf_teardown(dev, SSO_LF_GWS);
298         dev->nb_event_queues = 0;
299         dev->nb_event_ports = 0;
300         dev->configured = 0;
301         return rc;
302 }
303
304 static void
305 otx2_sso_queue_def_conf(struct rte_eventdev *event_dev, uint8_t queue_id,
306                         struct rte_event_queue_conf *queue_conf)
307 {
308         RTE_SET_USED(event_dev);
309         RTE_SET_USED(queue_id);
310
311         queue_conf->nb_atomic_flows = (1ULL << 20);
312         queue_conf->nb_atomic_order_sequences = (1ULL << 20);
313         queue_conf->event_queue_cfg = RTE_EVENT_QUEUE_CFG_ALL_TYPES;
314         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
315 }
316
317 static int
318 otx2_sso_queue_setup(struct rte_eventdev *event_dev, uint8_t queue_id,
319                      const struct rte_event_queue_conf *queue_conf)
320 {
321         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
322         struct otx2_mbox *mbox = dev->mbox;
323         struct sso_grp_priority *req;
324         int rc;
325
326         sso_func_trace("Queue=%d prio=%d", queue_id, queue_conf->priority);
327
328         req = otx2_mbox_alloc_msg_sso_grp_set_priority(dev->mbox);
329         req->grp = queue_id;
330         req->weight = 0xFF;
331         req->affinity = 0xFF;
332         /* Normalize <0-255> to <0-7> */
333         req->priority = queue_conf->priority / 32;
334
335         rc = otx2_mbox_process(mbox);
336         if (rc < 0) {
337                 otx2_err("Failed to set priority queue=%d", queue_id);
338                 return rc;
339         }
340
341         return 0;
342 }
343
344 /* Initialize and register event driver with DPDK Application */
345 static struct rte_eventdev_ops otx2_sso_ops = {
346         .dev_infos_get    = otx2_sso_info_get,
347         .dev_configure    = otx2_sso_configure,
348         .queue_def_conf   = otx2_sso_queue_def_conf,
349         .queue_setup      = otx2_sso_queue_setup,
350         .queue_release    = otx2_sso_queue_release,
351 };
352
353 static int
354 otx2_sso_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
355 {
356         return rte_event_pmd_pci_probe(pci_drv, pci_dev,
357                                        sizeof(struct otx2_sso_evdev),
358                                        otx2_sso_init);
359 }
360
361 static int
362 otx2_sso_remove(struct rte_pci_device *pci_dev)
363 {
364         return rte_event_pmd_pci_remove(pci_dev, otx2_sso_fini);
365 }
366
367 static const struct rte_pci_id pci_sso_map[] = {
368         {
369                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
370                                PCI_DEVID_OCTEONTX2_RVU_SSO_TIM_PF)
371         },
372         {
373                 .vendor_id = 0,
374         },
375 };
376
377 static struct rte_pci_driver pci_sso = {
378         .id_table = pci_sso_map,
379         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_IOVA_AS_VA,
380         .probe = otx2_sso_probe,
381         .remove = otx2_sso_remove,
382 };
383
384 int
385 otx2_sso_init(struct rte_eventdev *event_dev)
386 {
387         struct free_rsrcs_rsp *rsrc_cnt;
388         struct rte_pci_device *pci_dev;
389         struct otx2_sso_evdev *dev;
390         int rc;
391
392         event_dev->dev_ops = &otx2_sso_ops;
393         /* For secondary processes, the primary has done all the work */
394         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
395                 return 0;
396
397         dev = sso_pmd_priv(event_dev);
398
399         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
400
401         /* Initialize the base otx2_dev object */
402         rc = otx2_dev_init(pci_dev, dev);
403         if (rc < 0) {
404                 otx2_err("Failed to initialize otx2_dev rc=%d", rc);
405                 goto error;
406         }
407
408         /* Get SSO and SSOW MSIX rsrc cnt */
409         otx2_mbox_alloc_msg_free_rsrc_cnt(dev->mbox);
410         rc = otx2_mbox_process_msg(dev->mbox, (void *)&rsrc_cnt);
411         if (rc < 0) {
412                 otx2_err("Unable to get free rsrc count");
413                 goto otx2_dev_uninit;
414         }
415         otx2_sso_dbg("SSO %d SSOW %d NPA %d provisioned", rsrc_cnt->sso,
416                      rsrc_cnt->ssow, rsrc_cnt->npa);
417
418         dev->max_event_ports = RTE_MIN(rsrc_cnt->ssow, OTX2_SSO_MAX_VHWS);
419         dev->max_event_queues = RTE_MIN(rsrc_cnt->sso, OTX2_SSO_MAX_VHGRP);
420         /* Grab the NPA LF if required */
421         rc = otx2_npa_lf_init(pci_dev, dev);
422         if (rc < 0) {
423                 otx2_err("Unable to init NPA lf. It might not be provisioned");
424                 goto otx2_dev_uninit;
425         }
426
427         dev->drv_inited = true;
428         dev->is_timeout_deq = 0;
429         dev->min_dequeue_timeout_ns = USEC2NSEC(1);
430         dev->max_dequeue_timeout_ns = USEC2NSEC(0x3FF);
431         dev->max_num_events = -1;
432         dev->nb_event_queues = 0;
433         dev->nb_event_ports = 0;
434
435         if (!dev->max_event_ports || !dev->max_event_queues) {
436                 otx2_err("Not enough eventdev resource queues=%d ports=%d",
437                          dev->max_event_queues, dev->max_event_ports);
438                 rc = -ENODEV;
439                 goto otx2_npa_lf_uninit;
440         }
441
442         otx2_sso_pf_func_set(dev->pf_func);
443         otx2_sso_dbg("Initializing %s max_queues=%d max_ports=%d",
444                      event_dev->data->name, dev->max_event_queues,
445                      dev->max_event_ports);
446
447
448         return 0;
449
450 otx2_npa_lf_uninit:
451         otx2_npa_lf_fini();
452 otx2_dev_uninit:
453         otx2_dev_fini(pci_dev, dev);
454 error:
455         return rc;
456 }
457
458 int
459 otx2_sso_fini(struct rte_eventdev *event_dev)
460 {
461         struct otx2_sso_evdev *dev = sso_pmd_priv(event_dev);
462         struct rte_pci_device *pci_dev;
463
464         /* For secondary processes, nothing to be done */
465         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
466                 return 0;
467
468         pci_dev = container_of(event_dev->dev, struct rte_pci_device, device);
469
470         if (!dev->drv_inited)
471                 goto dev_fini;
472
473         dev->drv_inited = false;
474         otx2_npa_lf_fini();
475
476 dev_fini:
477         if (otx2_npa_lf_active(dev)) {
478                 otx2_info("Common resource in use by other devices");
479                 return -EAGAIN;
480         }
481
482         otx2_dev_fini(pci_dev, dev);
483
484         return 0;
485 }
486
487 RTE_PMD_REGISTER_PCI(event_octeontx2, pci_sso);
488 RTE_PMD_REGISTER_PCI_TABLE(event_octeontx2, pci_sso_map);
489 RTE_PMD_REGISTER_KMOD_DEP(event_octeontx2, "vfio-pci");