event/octeontx: support dequeue timeout tick conversion
[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_info_get(struct rte_eventdev *dev, struct rte_event_dev_info *dev_info)
153 {
154         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
155
156         dev_info->min_dequeue_timeout_ns = edev->min_deq_timeout_ns;
157         dev_info->max_dequeue_timeout_ns = edev->max_deq_timeout_ns;
158         dev_info->max_event_queues = edev->max_event_queues;
159         dev_info->max_event_queue_flows = (1ULL << 20);
160         dev_info->max_event_queue_priority_levels = 8;
161         dev_info->max_event_priority_levels = 1;
162         dev_info->max_event_ports = edev->max_event_ports;
163         dev_info->max_event_port_dequeue_depth = 1;
164         dev_info->max_event_port_enqueue_depth = 1;
165         dev_info->max_num_events =  edev->max_num_events;
166         dev_info->event_dev_cap = RTE_EVENT_DEV_CAP_QUEUE_QOS |
167                                         RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED |
168                                         RTE_EVENT_DEV_CAP_QUEUE_ALL_TYPES;
169 }
170
171 static int
172 ssovf_configure(const struct rte_eventdev *dev)
173 {
174         struct rte_event_dev_config *conf = &dev->data->dev_conf;
175         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
176         uint64_t deq_tmo_ns;
177
178         ssovf_func_trace();
179         deq_tmo_ns = conf->dequeue_timeout_ns;
180
181         if (conf->event_dev_cfg & RTE_EVENT_DEV_CFG_PER_DEQUEUE_TIMEOUT) {
182                 edev->is_timeout_deq = 1;
183                 deq_tmo_ns = edev->min_deq_timeout_ns;
184         }
185         edev->nb_event_queues = conf->nb_event_queues;
186         edev->nb_event_ports = conf->nb_event_ports;
187
188         return ssovf_mbox_getwork_tmo_set(deq_tmo_ns);
189 }
190
191 static void
192 ssovf_queue_def_conf(struct rte_eventdev *dev, uint8_t queue_id,
193                                  struct rte_event_queue_conf *queue_conf)
194 {
195         RTE_SET_USED(dev);
196         RTE_SET_USED(queue_id);
197
198         queue_conf->nb_atomic_flows = (1ULL << 20);
199         queue_conf->nb_atomic_order_sequences = (1ULL << 20);
200         queue_conf->event_queue_cfg = RTE_EVENT_QUEUE_CFG_ALL_TYPES;
201         queue_conf->priority = RTE_EVENT_DEV_PRIORITY_NORMAL;
202 }
203
204 static void
205 ssovf_queue_release(struct rte_eventdev *dev, uint8_t queue_id)
206 {
207         RTE_SET_USED(dev);
208         RTE_SET_USED(queue_id);
209 }
210
211 static int
212 ssovf_queue_setup(struct rte_eventdev *dev, uint8_t queue_id,
213                               const struct rte_event_queue_conf *queue_conf)
214 {
215         RTE_SET_USED(dev);
216         ssovf_func_trace("queue=%d prio=%d", queue_id, queue_conf->priority);
217
218         return ssovf_mbox_priority_set(queue_id, queue_conf->priority);
219 }
220
221 static void
222 ssovf_port_def_conf(struct rte_eventdev *dev, uint8_t port_id,
223                                  struct rte_event_port_conf *port_conf)
224 {
225         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
226
227         RTE_SET_USED(port_id);
228         port_conf->new_event_threshold = edev->max_num_events;
229         port_conf->dequeue_depth = 1;
230         port_conf->enqueue_depth = 1;
231 }
232
233 static void
234 ssovf_port_release(void *port)
235 {
236         rte_free(port);
237 }
238
239 static int
240 ssovf_port_setup(struct rte_eventdev *dev, uint8_t port_id,
241                                 const struct rte_event_port_conf *port_conf)
242 {
243         struct ssows *ws;
244         uint32_t reg_off;
245         uint8_t q;
246         struct ssovf_evdev *edev = ssovf_pmd_priv(dev);
247
248         ssovf_func_trace("port=%d", port_id);
249         RTE_SET_USED(port_conf);
250
251         /* Free memory prior to re-allocation if needed */
252         if (dev->data->ports[port_id] != NULL) {
253                 ssovf_port_release(dev->data->ports[port_id]);
254                 dev->data->ports[port_id] = NULL;
255         }
256
257         /* Allocate event port memory */
258         ws = rte_zmalloc_socket("eventdev ssows",
259                         sizeof(struct ssows), RTE_CACHE_LINE_SIZE,
260                         dev->data->socket_id);
261         if (ws == NULL) {
262                 ssovf_log_err("Failed to alloc memory for port=%d", port_id);
263                 return -ENOMEM;
264         }
265
266         ws->base = octeontx_ssovf_bar(OCTEONTX_SSO_HWS, port_id, 0);
267         if (ws->base == NULL) {
268                 rte_free(ws);
269                 ssovf_log_err("Failed to get hws base addr port=%d", port_id);
270                 return -EINVAL;
271         }
272
273         reg_off = SSOW_VHWS_OP_GET_WORK0;
274         reg_off |= 1 << 4; /* Index_ggrp_mask (Use maskset zero) */
275         reg_off |= 1 << 16; /* Wait */
276         ws->getwork = ws->base + reg_off;
277         ws->port = port_id;
278
279         for (q = 0; q < edev->nb_event_queues; q++) {
280                 ws->grps[q] = octeontx_ssovf_bar(OCTEONTX_SSO_GROUP, q, 2);
281                 if (ws->grps[q] == NULL) {
282                         rte_free(ws);
283                         ssovf_log_err("Failed to get grp%d base addr", q);
284                         return -EINVAL;
285                 }
286         }
287
288         dev->data->ports[port_id] = ws;
289         ssovf_log_dbg("port=%d ws=%p", port_id, ws);
290         return 0;
291 }
292
293 static int
294 ssovf_port_link(struct rte_eventdev *dev, void *port, const uint8_t queues[],
295                 const uint8_t priorities[], uint16_t nb_links)
296 {
297         uint16_t link;
298         uint64_t val;
299         struct ssows *ws = port;
300
301         ssovf_func_trace("port=%d nb_links=%d", ws->port, nb_links);
302         RTE_SET_USED(dev);
303         RTE_SET_USED(priorities);
304
305         for (link = 0; link < nb_links; link++) {
306                 val = queues[link];
307                 val |= (1ULL << 24); /* Set membership */
308                 ssovf_write64(val, ws->base + SSOW_VHWS_GRPMSK_CHGX(0));
309         }
310         return (int)nb_links;
311 }
312
313 static int
314 ssovf_port_unlink(struct rte_eventdev *dev, void *port, uint8_t queues[],
315                         uint16_t nb_unlinks)
316 {
317         uint16_t unlink;
318         uint64_t val;
319         struct ssows *ws = port;
320
321         ssovf_func_trace("port=%d nb_links=%d", ws->port, nb_unlinks);
322         RTE_SET_USED(dev);
323
324         for (unlink = 0; unlink < nb_unlinks; unlink++) {
325                 val = queues[unlink];
326                 val &= ~(1ULL << 24); /* Clear membership */
327                 ssovf_write64(val, ws->base + SSOW_VHWS_GRPMSK_CHGX(0));
328         }
329         return (int)nb_unlinks;
330 }
331
332 static int
333 ssovf_timeout_ticks(struct rte_eventdev *dev, uint64_t ns, uint64_t *tmo_ticks)
334 {
335         RTE_SET_USED(dev);
336
337         return ssovf_mbox_timeout_ticks(ns, tmo_ticks);
338 }
339
340 /* Initialize and register event driver with DPDK Application */
341 static const struct rte_eventdev_ops ssovf_ops = {
342         .dev_infos_get    = ssovf_info_get,
343         .dev_configure    = ssovf_configure,
344         .queue_def_conf   = ssovf_queue_def_conf,
345         .queue_setup      = ssovf_queue_setup,
346         .queue_release    = ssovf_queue_release,
347         .port_def_conf    = ssovf_port_def_conf,
348         .port_setup       = ssovf_port_setup,
349         .port_release     = ssovf_port_release,
350         .port_link        = ssovf_port_link,
351         .port_unlink      = ssovf_port_unlink,
352         .timeout_ticks    = ssovf_timeout_ticks,
353 };
354
355 static int
356 ssovf_vdev_probe(const char *name, const char *params)
357 {
358         struct octeontx_ssovf_info oinfo;
359         struct ssovf_mbox_dev_info info;
360         struct ssovf_evdev *edev;
361         struct rte_eventdev *eventdev;
362         static int ssovf_init_once;
363         int ret;
364
365         RTE_SET_USED(params);
366
367         /* More than one instance is not supported */
368         if (ssovf_init_once) {
369                 ssovf_log_err("Request to create >1 %s instance", name);
370                 return -EINVAL;
371         }
372
373         eventdev = rte_event_pmd_vdev_init(name, sizeof(struct ssovf_evdev),
374                                 rte_socket_id());
375         if (eventdev == NULL) {
376                 ssovf_log_err("Failed to create eventdev vdev %s", name);
377                 return -ENOMEM;
378         }
379         eventdev->dev_ops = &ssovf_ops;
380
381         /* For secondary processes, the primary has done all the work */
382         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
383                 return 0;
384
385         ret = octeontx_ssovf_info(&oinfo);
386         if (ret) {
387                 ssovf_log_err("Failed to probe and validate ssovfs %d", ret);
388                 goto error;
389         }
390
391         edev = ssovf_pmd_priv(eventdev);
392         edev->max_event_ports = oinfo.total_ssowvfs;
393         edev->max_event_queues = oinfo.total_ssovfs;
394         edev->is_timeout_deq = 0;
395
396         ret = ssovf_mbox_dev_info(&info);
397         if (ret < 0 || ret != sizeof(struct ssovf_mbox_dev_info)) {
398                 ssovf_log_err("Failed to get mbox devinfo %d", ret);
399                 goto error;
400         }
401
402         edev->min_deq_timeout_ns = info.min_deq_timeout_ns;
403         edev->max_deq_timeout_ns = info.max_deq_timeout_ns;
404         edev->max_num_events =  info.max_num_events;
405         ssovf_log_dbg("min_deq_tmo=%"PRId64" max_deq_tmo=%"PRId64" max_evts=%d",
406                         info.min_deq_timeout_ns, info.max_deq_timeout_ns,
407                         info.max_num_events);
408
409         if (!edev->max_event_ports || !edev->max_event_queues) {
410                 ssovf_log_err("Not enough eventdev resource queues=%d ports=%d",
411                         edev->max_event_queues, edev->max_event_ports);
412                 ret = -ENODEV;
413                 goto error;
414         }
415
416         ssovf_log_info("Initializing %s domain=%d max_queues=%d max_ports=%d",
417                         name, oinfo.domain, edev->max_event_queues,
418                         edev->max_event_ports);
419
420         ssovf_init_once = 1;
421         return 0;
422
423 error:
424         rte_event_pmd_vdev_uninit(name);
425         return ret;
426 }
427
428 static int
429 ssovf_vdev_remove(const char *name)
430 {
431         ssovf_log_info("Closing %s", name);
432         return rte_event_pmd_vdev_uninit(name);
433 }
434
435 static struct rte_vdev_driver vdev_ssovf_pmd = {
436         .probe = ssovf_vdev_probe,
437         .remove = ssovf_vdev_remove
438 };
439
440 RTE_PMD_REGISTER_VDEV(EVENTDEV_NAME_OCTEONTX_PMD, vdev_ssovf_pmd);