283441b4f607647e22ecef382138c8c7712f2732
[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 <fslmc_vfio.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
94 /* Set the STASH Destination depending on Current CPU ID.
95  * e.g. Valid values of SDEST are 4,5,6,7. Where,
96  * CPU 0-1 will have SDEST 4
97  * CPU 2-3 will have SDEST 5.....and so on.
98  */
99 static int
100 dpaa2_core_cluster_sdest(int cpu_id)
101 {
102         int x = cpu_id / dpaa2_cluster_sz;
103
104         if (x > 3)
105                 x = 3;
106
107         return dpaa2_core_cluster_base + x;
108 }
109
110 static void dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id)
111 {
112 #define STRING_LEN      28
113 #define COMMAND_LEN     50
114         uint32_t cpu_mask = 1;
115         int ret;
116         size_t len = 0;
117         char *temp = NULL, *token = NULL;
118         char string[STRING_LEN], command[COMMAND_LEN];
119         FILE *file;
120
121         snprintf(string, STRING_LEN, "dpio.%d", dpio_id);
122         file = fopen("/proc/interrupts", "r");
123         if (!file) {
124                 PMD_DRV_LOG(WARNING, "Failed to open /proc/interrupts file\n");
125                 return;
126         }
127         while (getline(&temp, &len, file) != -1) {
128                 if ((strstr(temp, string)) != NULL) {
129                         token = strtok(temp, ":");
130                         break;
131                 }
132         }
133
134         if (!token) {
135                 PMD_DRV_LOG(WARNING, "Failed to get interrupt id for dpio.%d\n",
136                             dpio_id);
137                 if (temp)
138                         free(temp);
139                 fclose(file);
140                 return;
141         }
142
143         cpu_mask = cpu_mask << rte_lcore_id();
144         snprintf(command, COMMAND_LEN, "echo %X > /proc/irq/%s/smp_affinity",
145                  cpu_mask, token);
146         ret = system(command);
147         if (ret < 0)
148                 PMD_DRV_LOG(WARNING,
149                         "Failed to affine interrupts on respective core\n");
150         else
151                 PMD_DRV_LOG(WARNING, " %s command is executed\n", command);
152
153         free(temp);
154         fclose(file);
155 }
156
157 static int dpaa2_dpio_intr_init(struct dpaa2_dpio_dev *dpio_dev)
158 {
159         struct epoll_event epoll_ev;
160         int eventfd, dpio_epoll_fd, ret;
161         int threshold = 0x3, timeout = 0xFF;
162
163         dpio_epoll_fd = epoll_create(1);
164         ret = rte_dpaa2_intr_enable(&dpio_dev->intr_handle, 0);
165         if (ret) {
166                 PMD_DRV_LOG(ERR, "Interrupt registeration failed\n");
167                 return -1;
168         }
169
170         if (getenv("DPAA2_PORTAL_INTR_THRESHOLD"))
171                 threshold = atoi(getenv("DPAA2_PORTAL_INTR_THRESHOLD"));
172
173         if (getenv("DPAA2_PORTAL_INTR_TIMEOUT"))
174                 sscanf(getenv("DPAA2_PORTAL_INTR_TIMEOUT"), "%x", &timeout);
175
176         qbman_swp_interrupt_set_trigger(dpio_dev->sw_portal,
177                                         QBMAN_SWP_INTERRUPT_DQRI);
178         qbman_swp_interrupt_clear_status(dpio_dev->sw_portal, 0xffffffff);
179         qbman_swp_interrupt_set_inhibit(dpio_dev->sw_portal, 0);
180         qbman_swp_dqrr_thrshld_write(dpio_dev->sw_portal, threshold);
181         qbman_swp_intr_timeout_write(dpio_dev->sw_portal, timeout);
182
183         eventfd = dpio_dev->intr_handle.fd;
184         epoll_ev.events = EPOLLIN | EPOLLPRI | EPOLLET;
185         epoll_ev.data.fd = eventfd;
186
187         ret = epoll_ctl(dpio_epoll_fd, EPOLL_CTL_ADD, eventfd, &epoll_ev);
188         if (ret < 0) {
189                 PMD_DRV_LOG(ERR, "epoll_ctl failed\n");
190                 return -1;
191         }
192         dpio_dev->epoll_fd = dpio_epoll_fd;
193
194         dpaa2_affine_dpio_intr_to_respective_core(dpio_dev->hw_id);
195
196         return 0;
197 }
198
199 static int
200 configure_dpio_qbman_swp(struct dpaa2_dpio_dev *dpio_dev)
201 {
202         struct qbman_swp_desc p_des;
203         struct dpio_attr attr;
204
205         dpio_dev->dpio = malloc(sizeof(struct fsl_mc_io));
206         if (!dpio_dev->dpio) {
207                 PMD_INIT_LOG(ERR, "Memory allocation failure\n");
208                 return -1;
209         }
210
211         PMD_DRV_LOG(DEBUG, "\t Allocated  DPIO Portal[%p]", dpio_dev->dpio);
212         dpio_dev->dpio->regs = dpio_dev->mc_portal;
213         if (dpio_open(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->hw_id,
214                       &dpio_dev->token)) {
215                 PMD_INIT_LOG(ERR, "Failed to allocate IO space\n");
216                 free(dpio_dev->dpio);
217                 return -1;
218         }
219
220         if (dpio_reset(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token)) {
221                 PMD_INIT_LOG(ERR, "Failed to reset dpio\n");
222                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
223                 free(dpio_dev->dpio);
224                 return -1;
225         }
226
227         if (dpio_enable(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token)) {
228                 PMD_INIT_LOG(ERR, "Failed to Enable dpio\n");
229                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
230                 free(dpio_dev->dpio);
231                 return -1;
232         }
233
234         if (dpio_get_attributes(dpio_dev->dpio, CMD_PRI_LOW,
235                                 dpio_dev->token, &attr)) {
236                 PMD_INIT_LOG(ERR, "DPIO Get attribute failed\n");
237                 dpio_disable(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
238                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW,  dpio_dev->token);
239                 free(dpio_dev->dpio);
240                 return -1;
241         }
242
243         PMD_INIT_LOG(DEBUG, "Qbman Portal ID %d", attr.qbman_portal_id);
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                 }
282                 first_time = 1;
283         }
284
285         /* Set the Stashing Destination */
286         if (cpu_id < 0) {
287                 cpu_id = rte_get_master_lcore();
288                 if (cpu_id < 0) {
289                         RTE_LOG(ERR, PMD, "\tGetting CPU Index failed\n");
290                         return -1;
291                 }
292         }
293         /* Set the STASH Destination depending on Current CPU ID.
294          * Valid values of SDEST are 4,5,6,7. Where,
295          */
296
297         sdest = dpaa2_core_cluster_sdest(cpu_id);
298         PMD_DRV_LOG(DEBUG, "Portal= %d  CPU= %u SDEST= %d",
299                     dpio_dev->index, cpu_id, sdest);
300
301         ret = dpio_set_stashing_destination(dpio_dev->dpio, CMD_PRI_LOW,
302                                             dpio_dev->token, sdest);
303         if (ret) {
304                 PMD_DRV_LOG(ERR, "%d ERROR in SDEST\n",  ret);
305                 return -1;
306         }
307
308         if (dpaa2_dpio_intr_init(dpio_dev)) {
309                 PMD_DRV_LOG(ERR, "Interrupt registration failed for dpio\n");
310                 return -1;
311         }
312
313         return 0;
314 }
315
316 struct dpaa2_dpio_dev *dpaa2_get_qbman_swp(int cpu_id)
317 {
318         struct dpaa2_dpio_dev *dpio_dev = NULL;
319         int ret;
320
321         /* Get DPIO dev handle from list using index */
322         TAILQ_FOREACH(dpio_dev, &dpio_dev_list, next) {
323                 if (dpio_dev && rte_atomic16_test_and_set(&dpio_dev->ref_count))
324                         break;
325         }
326         if (!dpio_dev)
327                 return NULL;
328
329         PMD_DRV_LOG(DEBUG, "New Portal=0x%x (%d) affined thread - %lu",
330                     dpio_dev, dpio_dev->index, syscall(SYS_gettid));
331
332         ret = dpaa2_configure_stashing(dpio_dev, cpu_id);
333         if (ret)
334                 PMD_DRV_LOG(ERR, "dpaa2_configure_stashing failed");
335
336         return dpio_dev;
337 }
338
339 int
340 dpaa2_affine_qbman_swp(void)
341 {
342         unsigned int lcore_id = rte_lcore_id();
343         uint64_t tid = syscall(SYS_gettid);
344
345         if (lcore_id == LCORE_ID_ANY)
346                 lcore_id = rte_get_master_lcore();
347         /* if the core id is not supported */
348         else if (lcore_id >= RTE_MAX_LCORE)
349                 return -1;
350
351         if (dpaa2_io_portal[lcore_id].dpio_dev) {
352                 PMD_DRV_LOG(INFO, "DPAA Portal=0x%x (%d) is being shared"
353                             " between thread %lu and current  %lu",
354                             dpaa2_io_portal[lcore_id].dpio_dev,
355                             dpaa2_io_portal[lcore_id].dpio_dev->index,
356                             dpaa2_io_portal[lcore_id].net_tid,
357                             tid);
358                 RTE_PER_LCORE(_dpaa2_io).dpio_dev
359                         = dpaa2_io_portal[lcore_id].dpio_dev;
360                 rte_atomic16_inc(&dpaa2_io_portal
361                                  [lcore_id].dpio_dev->ref_count);
362                 dpaa2_io_portal[lcore_id].net_tid = tid;
363
364                 PMD_DRV_LOG(DEBUG, "Old Portal=0x%x (%d) affined thread - %lu",
365                             dpaa2_io_portal[lcore_id].dpio_dev,
366                             dpaa2_io_portal[lcore_id].dpio_dev->index,
367                             tid);
368                 return 0;
369         }
370
371         /* Populate the dpaa2_io_portal structure */
372         dpaa2_io_portal[lcore_id].dpio_dev = dpaa2_get_qbman_swp(lcore_id);
373
374         if (dpaa2_io_portal[lcore_id].dpio_dev) {
375                 RTE_PER_LCORE(_dpaa2_io).dpio_dev
376                         = dpaa2_io_portal[lcore_id].dpio_dev;
377                 dpaa2_io_portal[lcore_id].net_tid = tid;
378
379                 return 0;
380         } else {
381                 return -1;
382         }
383 }
384
385 int
386 dpaa2_affine_qbman_swp_sec(void)
387 {
388         unsigned int lcore_id = rte_lcore_id();
389         uint64_t tid = syscall(SYS_gettid);
390
391         if (lcore_id == LCORE_ID_ANY)
392                 lcore_id = rte_get_master_lcore();
393         /* if the core id is not supported */
394         else if (lcore_id >= RTE_MAX_LCORE)
395                 return -1;
396
397         if (dpaa2_io_portal[lcore_id].sec_dpio_dev) {
398                 PMD_DRV_LOG(INFO, "DPAA Portal=0x%x (%d) is being shared"
399                             " between thread %lu and current  %lu",
400                             dpaa2_io_portal[lcore_id].sec_dpio_dev,
401                             dpaa2_io_portal[lcore_id].sec_dpio_dev->index,
402                             dpaa2_io_portal[lcore_id].sec_tid,
403                             tid);
404                 RTE_PER_LCORE(_dpaa2_io).sec_dpio_dev
405                         = dpaa2_io_portal[lcore_id].sec_dpio_dev;
406                 rte_atomic16_inc(&dpaa2_io_portal
407                                  [lcore_id].sec_dpio_dev->ref_count);
408                 dpaa2_io_portal[lcore_id].sec_tid = tid;
409
410                 PMD_DRV_LOG(DEBUG, "Old Portal=0x%x (%d) affined thread - %lu",
411                             dpaa2_io_portal[lcore_id].sec_dpio_dev,
412                             dpaa2_io_portal[lcore_id].sec_dpio_dev->index,
413                             tid);
414                 return 0;
415         }
416
417         /* Populate the dpaa2_io_portal structure */
418         dpaa2_io_portal[lcore_id].sec_dpio_dev = dpaa2_get_qbman_swp(lcore_id);
419
420         if (dpaa2_io_portal[lcore_id].sec_dpio_dev) {
421                 RTE_PER_LCORE(_dpaa2_io).sec_dpio_dev
422                         = dpaa2_io_portal[lcore_id].sec_dpio_dev;
423                 dpaa2_io_portal[lcore_id].sec_tid = tid;
424                 return 0;
425         } else {
426                 return -1;
427         }
428 }
429
430 static int
431 dpaa2_create_dpio_device(struct fslmc_vfio_device *vdev,
432                          struct vfio_device_info *obj_info,
433                          int object_id)
434 {
435         struct dpaa2_dpio_dev *dpio_dev;
436         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info)};
437         int vfio_dev_fd;
438
439         if (obj_info->num_regions < NUM_DPIO_REGIONS) {
440                 PMD_INIT_LOG(ERR, "ERROR, Not sufficient number "
441                                 "of DPIO regions.\n");
442                 return -1;
443         }
444
445         dpio_dev = rte_malloc(NULL, sizeof(struct dpaa2_dpio_dev),
446                               RTE_CACHE_LINE_SIZE);
447         if (!dpio_dev) {
448                 PMD_INIT_LOG(ERR, "Memory allocation failed for DPIO Device\n");
449                 return -1;
450         }
451
452         dpio_dev->dpio = NULL;
453         dpio_dev->hw_id = object_id;
454         dpio_dev->intr_handle.vfio_dev_fd = vdev->fd;
455         rte_atomic16_init(&dpio_dev->ref_count);
456         /* Using single portal  for all devices */
457         dpio_dev->mc_portal = rte_mcp_ptr_list[MC_PORTAL_INDEX];
458
459         reg_info.index = 0;
460         vfio_dev_fd = dpio_dev->intr_handle.vfio_dev_fd;
461         if (ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info)) {
462                 PMD_INIT_LOG(ERR, "vfio: error getting region info\n");
463                 rte_free(dpio_dev);
464                 return -1;
465         }
466
467         dpio_dev->ce_size = reg_info.size;
468         dpio_dev->qbman_portal_ce_paddr = (uint64_t)mmap(NULL, reg_info.size,
469                                 PROT_WRITE | PROT_READ, MAP_SHARED,
470                                 vfio_dev_fd, reg_info.offset);
471
472         reg_info.index = 1;
473         if (ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info)) {
474                 PMD_INIT_LOG(ERR, "vfio: error getting region info\n");
475                 rte_free(dpio_dev);
476                 return -1;
477         }
478
479         dpio_dev->ci_size = reg_info.size;
480         dpio_dev->qbman_portal_ci_paddr = (uint64_t)mmap(NULL, reg_info.size,
481                                 PROT_WRITE | PROT_READ, MAP_SHARED,
482                                 vfio_dev_fd, reg_info.offset);
483
484         if (configure_dpio_qbman_swp(dpio_dev)) {
485                 PMD_INIT_LOG(ERR,
486                              "Fail to configure the dpio qbman portal for %d\n",
487                              dpio_dev->hw_id);
488                 rte_free(dpio_dev);
489                 return -1;
490         }
491
492         io_space_count++;
493         dpio_dev->index = io_space_count;
494         TAILQ_INSERT_TAIL(&dpio_dev_list, dpio_dev, next);
495         PMD_INIT_LOG(DEBUG, "DPAA2: Added [dpio.%d]", object_id);
496
497         return 0;
498 }
499
500 void
501 dpaa2_free_dq_storage(struct queue_storage_info_t *q_storage)
502 {
503         int i = 0;
504
505         for (i = 0; i < NUM_DQS_PER_QUEUE; i++) {
506                 if (q_storage->dq_storage[i])
507                         rte_free(q_storage->dq_storage[i]);
508         }
509 }
510
511 int
512 dpaa2_alloc_dq_storage(struct queue_storage_info_t *q_storage)
513 {
514         int i = 0;
515
516         for (i = 0; i < NUM_DQS_PER_QUEUE; i++) {
517                 q_storage->dq_storage[i] = rte_malloc(NULL,
518                         DPAA2_DQRR_RING_SIZE * sizeof(struct qbman_result),
519                         RTE_CACHE_LINE_SIZE);
520                 if (!q_storage->dq_storage[i])
521                         goto fail;
522         }
523         return 0;
524 fail:
525         while (--i >= 0)
526                 rte_free(q_storage->dq_storage[i]);
527
528         return -1;
529 }
530
531 static struct rte_dpaa2_object rte_dpaa2_dpio_obj = {
532         .object_id = DPAA2_MC_DPIO_DEVID,
533         .create = dpaa2_create_dpio_device,
534 };
535
536 RTE_PMD_REGISTER_DPAA2_OBJECT(dpio, rte_dpaa2_dpio_obj);