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