New upstream version 17.11-rc3
[deb_dpdk.git] / drivers / bus / fslmc / portal / dpaa2_hw_dpio.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright 2016 NXP.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Freescale Semiconductor, Inc nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <inttypes.h>
41 #include <signal.h>
42 #include <pthread.h>
43 #include <sys/types.h>
44 #include <sys/queue.h>
45 #include <sys/ioctl.h>
46 #include <sys/stat.h>
47 #include <sys/mman.h>
48 #include <sys/syscall.h>
49 #include <sys/epoll.h>
50 #include<sys/eventfd.h>
51
52 #include <rte_mbuf.h>
53 #include <rte_ethdev.h>
54 #include <rte_malloc.h>
55 #include <rte_memcpy.h>
56 #include <rte_string_fns.h>
57 #include <rte_cycles.h>
58 #include <rte_kvargs.h>
59 #include <rte_dev.h>
60
61 #include <fslmc_logs.h>
62 #include <rte_fslmc.h>
63 #include "dpaa2_hw_pvt.h"
64 #include "dpaa2_hw_dpio.h"
65 #include <mc/fsl_dpmng.h>
66
67 #define NUM_HOST_CPUS RTE_MAX_LCORE
68
69 struct dpaa2_io_portal_t dpaa2_io_portal[RTE_MAX_LCORE];
70 RTE_DEFINE_PER_LCORE(struct dpaa2_io_portal_t, _dpaa2_io);
71
72 struct swp_active_dqs rte_global_active_dqs_list[NUM_MAX_SWP];
73
74 TAILQ_HEAD(dpio_dev_list, dpaa2_dpio_dev);
75 static struct dpio_dev_list dpio_dev_list
76         = TAILQ_HEAD_INITIALIZER(dpio_dev_list); /*!< DPIO device list */
77 static uint32_t io_space_count;
78
79 /*Stashing Macros default for LS208x*/
80 static int dpaa2_core_cluster_base = 0x04;
81 static int dpaa2_cluster_sz = 2;
82
83 /* For LS208X platform There are four clusters with following mapping:
84  * Cluster 1 (ID = x04) : CPU0, CPU1;
85  * Cluster 2 (ID = x05) : CPU2, CPU3;
86  * Cluster 3 (ID = x06) : CPU4, CPU5;
87  * Cluster 4 (ID = x07) : CPU6, CPU7;
88  */
89 /* For LS108X platform There are two clusters with following mapping:
90  * Cluster 1 (ID = x02) : CPU0, CPU1, CPU2, CPU3;
91  * Cluster 2 (ID = x03) : CPU4, CPU5, CPU6, CPU7;
92  */
93 /* For LX2160 platform There are four clusters with following mapping:
94  * Cluster 1 (ID = x00) : CPU0, CPU1;
95  * Cluster 2 (ID = x01) : CPU2, CPU3;
96  * Cluster 3 (ID = x02) : CPU4, CPU5;
97  * Cluster 4 (ID = x03) : CPU6, CPU7;
98  * Cluster 1 (ID = x04) : CPU8, CPU9;
99  * Cluster 2 (ID = x05) : CPU10, CP11;
100  * Cluster 3 (ID = x06) : CPU12, CPU13;
101  * Cluster 4 (ID = x07) : CPU14, CPU15;
102  */
103
104 static int
105 dpaa2_core_cluster_sdest(int cpu_id)
106 {
107         int x = cpu_id / dpaa2_cluster_sz;
108
109         return dpaa2_core_cluster_base + x;
110 }
111
112 static void dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id)
113 {
114 #define STRING_LEN      28
115 #define COMMAND_LEN     50
116         uint32_t cpu_mask = 1;
117         int ret;
118         size_t len = 0;
119         char *temp = NULL, *token = NULL;
120         char string[STRING_LEN], command[COMMAND_LEN];
121         FILE *file;
122
123         snprintf(string, STRING_LEN, "dpio.%d", dpio_id);
124         file = fopen("/proc/interrupts", "r");
125         if (!file) {
126                 PMD_DRV_LOG(WARNING, "Failed to open /proc/interrupts file\n");
127                 return;
128         }
129         while (getline(&temp, &len, file) != -1) {
130                 if ((strstr(temp, string)) != NULL) {
131                         token = strtok(temp, ":");
132                         break;
133                 }
134         }
135
136         if (!token) {
137                 PMD_DRV_LOG(WARNING, "Failed to get interrupt id for dpio.%d\n",
138                             dpio_id);
139                 if (temp)
140                         free(temp);
141                 fclose(file);
142                 return;
143         }
144
145         cpu_mask = cpu_mask << rte_lcore_id();
146         snprintf(command, COMMAND_LEN, "echo %X > /proc/irq/%s/smp_affinity",
147                  cpu_mask, token);
148         ret = system(command);
149         if (ret < 0)
150                 PMD_DRV_LOG(WARNING,
151                         "Failed to affine interrupts on respective core\n");
152         else
153                 PMD_DRV_LOG(WARNING, " %s command is executed\n", command);
154
155         free(temp);
156         fclose(file);
157 }
158
159 static int dpaa2_dpio_intr_init(struct dpaa2_dpio_dev *dpio_dev)
160 {
161         struct epoll_event epoll_ev;
162         int eventfd, dpio_epoll_fd, ret;
163         int threshold = 0x3, timeout = 0xFF;
164
165         dpio_epoll_fd = epoll_create(1);
166         ret = rte_dpaa2_intr_enable(&dpio_dev->intr_handle, 0);
167         if (ret) {
168                 PMD_DRV_LOG(ERR, "Interrupt registeration failed\n");
169                 return -1;
170         }
171
172         if (getenv("DPAA2_PORTAL_INTR_THRESHOLD"))
173                 threshold = atoi(getenv("DPAA2_PORTAL_INTR_THRESHOLD"));
174
175         if (getenv("DPAA2_PORTAL_INTR_TIMEOUT"))
176                 sscanf(getenv("DPAA2_PORTAL_INTR_TIMEOUT"), "%x", &timeout);
177
178         qbman_swp_interrupt_set_trigger(dpio_dev->sw_portal,
179                                         QBMAN_SWP_INTERRUPT_DQRI);
180         qbman_swp_interrupt_clear_status(dpio_dev->sw_portal, 0xffffffff);
181         qbman_swp_interrupt_set_inhibit(dpio_dev->sw_portal, 0);
182         qbman_swp_dqrr_thrshld_write(dpio_dev->sw_portal, threshold);
183         qbman_swp_intr_timeout_write(dpio_dev->sw_portal, timeout);
184
185         eventfd = dpio_dev->intr_handle.fd;
186         epoll_ev.events = EPOLLIN | EPOLLPRI | EPOLLET;
187         epoll_ev.data.fd = eventfd;
188
189         ret = epoll_ctl(dpio_epoll_fd, EPOLL_CTL_ADD, eventfd, &epoll_ev);
190         if (ret < 0) {
191                 PMD_DRV_LOG(ERR, "epoll_ctl failed\n");
192                 return -1;
193         }
194         dpio_dev->epoll_fd = dpio_epoll_fd;
195
196         dpaa2_affine_dpio_intr_to_respective_core(dpio_dev->hw_id);
197
198         return 0;
199 }
200
201 static int
202 configure_dpio_qbman_swp(struct dpaa2_dpio_dev *dpio_dev)
203 {
204         struct qbman_swp_desc p_des;
205         struct dpio_attr attr;
206
207         dpio_dev->dpio = malloc(sizeof(struct fsl_mc_io));
208         if (!dpio_dev->dpio) {
209                 PMD_INIT_LOG(ERR, "Memory allocation failure\n");
210                 return -1;
211         }
212
213         PMD_DRV_LOG(DEBUG, "Allocated  DPIO Portal[%p]", dpio_dev->dpio);
214         dpio_dev->dpio->regs = dpio_dev->mc_portal;
215         if (dpio_open(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->hw_id,
216                       &dpio_dev->token)) {
217                 PMD_INIT_LOG(ERR, "Failed to allocate IO space\n");
218                 free(dpio_dev->dpio);
219                 return -1;
220         }
221
222         if (dpio_reset(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token)) {
223                 PMD_INIT_LOG(ERR, "Failed to reset dpio\n");
224                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
225                 free(dpio_dev->dpio);
226                 return -1;
227         }
228
229         if (dpio_enable(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token)) {
230                 PMD_INIT_LOG(ERR, "Failed to Enable dpio\n");
231                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
232                 free(dpio_dev->dpio);
233                 return -1;
234         }
235
236         if (dpio_get_attributes(dpio_dev->dpio, CMD_PRI_LOW,
237                                 dpio_dev->token, &attr)) {
238                 PMD_INIT_LOG(ERR, "DPIO Get attribute failed\n");
239                 dpio_disable(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
240                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW,  dpio_dev->token);
241                 free(dpio_dev->dpio);
242                 return -1;
243         }
244
245         /* Configure & setup SW portal */
246         p_des.block = NULL;
247         p_des.idx = attr.qbman_portal_id;
248         p_des.cena_bar = (void *)(dpio_dev->qbman_portal_ce_paddr);
249         p_des.cinh_bar = (void *)(dpio_dev->qbman_portal_ci_paddr);
250         p_des.irq = -1;
251         p_des.qman_version = attr.qbman_version;
252
253         dpio_dev->sw_portal = qbman_swp_init(&p_des);
254         if (dpio_dev->sw_portal == NULL) {
255                 PMD_DRV_LOG(ERR, " QBMan SW Portal Init failed\n");
256                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
257                 free(dpio_dev->dpio);
258                 return -1;
259         }
260
261         return 0;
262 }
263
264 static int
265 dpaa2_configure_stashing(struct dpaa2_dpio_dev *dpio_dev, int cpu_id)
266 {
267         int sdest, ret;
268         static int first_time;
269
270         /* find the SoC type for the first time */
271         if (!first_time) {
272                 struct mc_soc_version mc_plat_info = {0};
273
274                 if (mc_get_soc_version(dpio_dev->dpio,
275                                        CMD_PRI_LOW, &mc_plat_info)) {
276                         PMD_INIT_LOG(ERR, "\tmc_get_soc_version failed\n");
277                 } else if ((mc_plat_info.svr & 0xffff0000) == SVR_LS1080A) {
278                         dpaa2_core_cluster_base = 0x02;
279                         dpaa2_cluster_sz = 4;
280                         PMD_INIT_LOG(DEBUG, "\tLS108x (A53) Platform Detected");
281                 } else if ((mc_plat_info.svr & 0xffff0000) == SVR_LX2160A) {
282                         dpaa2_core_cluster_base = 0x00;
283                         dpaa2_cluster_sz = 2;
284                         PMD_INIT_LOG(DEBUG, "\tLX2160 Platform Detected");
285                 }
286                 first_time = 1;
287         }
288
289         /* Set the Stashing Destination */
290         if (cpu_id < 0) {
291                 cpu_id = rte_get_master_lcore();
292                 if (cpu_id < 0) {
293                         RTE_LOG(ERR, PMD, "\tGetting CPU Index failed\n");
294                         return -1;
295                 }
296         }
297         /* Set the STASH Destination depending on Current CPU ID.
298          * Valid values of SDEST are 4,5,6,7. Where,
299          */
300
301         sdest = dpaa2_core_cluster_sdest(cpu_id);
302         PMD_DRV_LOG(DEBUG, "Portal= %d  CPU= %u SDEST= %d",
303                     dpio_dev->index, cpu_id, sdest);
304
305         ret = dpio_set_stashing_destination(dpio_dev->dpio, CMD_PRI_LOW,
306                                             dpio_dev->token, sdest);
307         if (ret) {
308                 PMD_DRV_LOG(ERR, "%d ERROR in SDEST\n",  ret);
309                 return -1;
310         }
311
312         if (dpaa2_dpio_intr_init(dpio_dev)) {
313                 PMD_DRV_LOG(ERR, "Interrupt registration failed for dpio\n");
314                 return -1;
315         }
316
317         return 0;
318 }
319
320 struct dpaa2_dpio_dev *dpaa2_get_qbman_swp(int cpu_id)
321 {
322         struct dpaa2_dpio_dev *dpio_dev = NULL;
323         int ret;
324
325         /* Get DPIO dev handle from list using index */
326         TAILQ_FOREACH(dpio_dev, &dpio_dev_list, next) {
327                 if (dpio_dev && rte_atomic16_test_and_set(&dpio_dev->ref_count))
328                         break;
329         }
330         if (!dpio_dev)
331                 return NULL;
332
333         PMD_DRV_LOG(DEBUG, "New Portal=0x%x (%d) affined thread - %lu",
334                     dpio_dev, dpio_dev->index, syscall(SYS_gettid));
335
336         ret = dpaa2_configure_stashing(dpio_dev, cpu_id);
337         if (ret)
338                 PMD_DRV_LOG(ERR, "dpaa2_configure_stashing failed");
339
340         return dpio_dev;
341 }
342
343 int
344 dpaa2_affine_qbman_swp(void)
345 {
346         unsigned int lcore_id = rte_lcore_id();
347         uint64_t tid = syscall(SYS_gettid);
348
349         if (lcore_id == LCORE_ID_ANY)
350                 lcore_id = rte_get_master_lcore();
351         /* if the core id is not supported */
352         else if (lcore_id >= RTE_MAX_LCORE)
353                 return -1;
354
355         if (dpaa2_io_portal[lcore_id].dpio_dev) {
356                 PMD_DRV_LOG(INFO, "DPAA Portal=0x%x (%d) is being shared"
357                             " between thread %lu and current  %lu",
358                             dpaa2_io_portal[lcore_id].dpio_dev,
359                             dpaa2_io_portal[lcore_id].dpio_dev->index,
360                             dpaa2_io_portal[lcore_id].net_tid,
361                             tid);
362                 RTE_PER_LCORE(_dpaa2_io).dpio_dev
363                         = dpaa2_io_portal[lcore_id].dpio_dev;
364                 rte_atomic16_inc(&dpaa2_io_portal
365                                  [lcore_id].dpio_dev->ref_count);
366                 dpaa2_io_portal[lcore_id].net_tid = tid;
367
368                 PMD_DRV_LOG(DEBUG, "Old Portal=0x%x (%d) affined thread - %lu",
369                             dpaa2_io_portal[lcore_id].dpio_dev,
370                             dpaa2_io_portal[lcore_id].dpio_dev->index,
371                             tid);
372                 return 0;
373         }
374
375         /* Populate the dpaa2_io_portal structure */
376         dpaa2_io_portal[lcore_id].dpio_dev = dpaa2_get_qbman_swp(lcore_id);
377
378         if (dpaa2_io_portal[lcore_id].dpio_dev) {
379                 RTE_PER_LCORE(_dpaa2_io).dpio_dev
380                         = dpaa2_io_portal[lcore_id].dpio_dev;
381                 dpaa2_io_portal[lcore_id].net_tid = tid;
382
383                 return 0;
384         } else {
385                 return -1;
386         }
387 }
388
389 int
390 dpaa2_affine_qbman_swp_sec(void)
391 {
392         unsigned int lcore_id = rte_lcore_id();
393         uint64_t tid = syscall(SYS_gettid);
394
395         if (lcore_id == LCORE_ID_ANY)
396                 lcore_id = rte_get_master_lcore();
397         /* if the core id is not supported */
398         else if (lcore_id >= RTE_MAX_LCORE)
399                 return -1;
400
401         if (dpaa2_io_portal[lcore_id].sec_dpio_dev) {
402                 PMD_DRV_LOG(INFO, "DPAA Portal=0x%x (%d) is being shared"
403                             " between thread %lu and current  %lu",
404                             dpaa2_io_portal[lcore_id].sec_dpio_dev,
405                             dpaa2_io_portal[lcore_id].sec_dpio_dev->index,
406                             dpaa2_io_portal[lcore_id].sec_tid,
407                             tid);
408                 RTE_PER_LCORE(_dpaa2_io).sec_dpio_dev
409                         = dpaa2_io_portal[lcore_id].sec_dpio_dev;
410                 rte_atomic16_inc(&dpaa2_io_portal
411                                  [lcore_id].sec_dpio_dev->ref_count);
412                 dpaa2_io_portal[lcore_id].sec_tid = tid;
413
414                 PMD_DRV_LOG(DEBUG, "Old Portal=0x%x (%d) affined thread - %lu",
415                             dpaa2_io_portal[lcore_id].sec_dpio_dev,
416                             dpaa2_io_portal[lcore_id].sec_dpio_dev->index,
417                             tid);
418                 return 0;
419         }
420
421         /* Populate the dpaa2_io_portal structure */
422         dpaa2_io_portal[lcore_id].sec_dpio_dev = dpaa2_get_qbman_swp(lcore_id);
423
424         if (dpaa2_io_portal[lcore_id].sec_dpio_dev) {
425                 RTE_PER_LCORE(_dpaa2_io).sec_dpio_dev
426                         = dpaa2_io_portal[lcore_id].sec_dpio_dev;
427                 dpaa2_io_portal[lcore_id].sec_tid = tid;
428                 return 0;
429         } else {
430                 return -1;
431         }
432 }
433
434 static int
435 dpaa2_create_dpio_device(int vdev_fd,
436                          struct vfio_device_info *obj_info,
437                          int object_id)
438 {
439         struct dpaa2_dpio_dev *dpio_dev;
440         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info)};
441
442         if (obj_info->num_regions < NUM_DPIO_REGIONS) {
443                 PMD_INIT_LOG(ERR, "ERROR, Not sufficient number "
444                                 "of DPIO regions.\n");
445                 return -1;
446         }
447
448         dpio_dev = rte_malloc(NULL, sizeof(struct dpaa2_dpio_dev),
449                               RTE_CACHE_LINE_SIZE);
450         if (!dpio_dev) {
451                 PMD_INIT_LOG(ERR, "Memory allocation failed for DPIO Device\n");
452                 return -1;
453         }
454
455         dpio_dev->dpio = NULL;
456         dpio_dev->hw_id = object_id;
457         rte_atomic16_init(&dpio_dev->ref_count);
458         /* Using single portal  for all devices */
459         dpio_dev->mc_portal = rte_mcp_ptr_list[MC_PORTAL_INDEX];
460
461         reg_info.index = 0;
462         if (ioctl(vdev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info)) {
463                 PMD_INIT_LOG(ERR, "vfio: error getting region info\n");
464                 rte_free(dpio_dev);
465                 return -1;
466         }
467
468         dpio_dev->ce_size = reg_info.size;
469         dpio_dev->qbman_portal_ce_paddr = (uint64_t)mmap(NULL, reg_info.size,
470                                 PROT_WRITE | PROT_READ, MAP_SHARED,
471                                 vdev_fd, reg_info.offset);
472
473         reg_info.index = 1;
474         if (ioctl(vdev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info)) {
475                 PMD_INIT_LOG(ERR, "vfio: error getting region info\n");
476                 rte_free(dpio_dev);
477                 return -1;
478         }
479
480         dpio_dev->ci_size = reg_info.size;
481         dpio_dev->qbman_portal_ci_paddr = (uint64_t)mmap(NULL, reg_info.size,
482                                 PROT_WRITE | PROT_READ, MAP_SHARED,
483                                 vdev_fd, reg_info.offset);
484
485         if (configure_dpio_qbman_swp(dpio_dev)) {
486                 PMD_INIT_LOG(ERR,
487                              "Fail to configure the dpio qbman portal for %d\n",
488                              dpio_dev->hw_id);
489                 rte_free(dpio_dev);
490                 return -1;
491         }
492
493         io_space_count++;
494         dpio_dev->index = io_space_count;
495
496         if (rte_dpaa2_vfio_setup_intr(&dpio_dev->intr_handle, vdev_fd, 1)) {
497                 PMD_INIT_LOG(ERR, "Fail to setup interrupt for %d\n",
498                              dpio_dev->hw_id);
499                 rte_free(dpio_dev);
500         }
501
502         TAILQ_INSERT_TAIL(&dpio_dev_list, dpio_dev, next);
503         RTE_LOG(DEBUG, PMD, "DPAA2: Added [dpio.%d]\n", object_id);
504
505         return 0;
506 }
507
508 void
509 dpaa2_free_dq_storage(struct queue_storage_info_t *q_storage)
510 {
511         int i = 0;
512
513         for (i = 0; i < NUM_DQS_PER_QUEUE; i++) {
514                 if (q_storage->dq_storage[i])
515                         rte_free(q_storage->dq_storage[i]);
516         }
517 }
518
519 int
520 dpaa2_alloc_dq_storage(struct queue_storage_info_t *q_storage)
521 {
522         int i = 0;
523
524         for (i = 0; i < NUM_DQS_PER_QUEUE; i++) {
525                 q_storage->dq_storage[i] = rte_malloc(NULL,
526                         DPAA2_DQRR_RING_SIZE * sizeof(struct qbman_result),
527                         RTE_CACHE_LINE_SIZE);
528                 if (!q_storage->dq_storage[i])
529                         goto fail;
530         }
531         return 0;
532 fail:
533         while (--i >= 0)
534                 rte_free(q_storage->dq_storage[i]);
535
536         return -1;
537 }
538
539 static struct rte_dpaa2_object rte_dpaa2_dpio_obj = {
540         .dev_type = DPAA2_IO,
541         .create = dpaa2_create_dpio_device,
542 };
543
544 RTE_PMD_REGISTER_DPAA2_OBJECT(dpio, rte_dpaa2_dpio_obj);