668d987c211dda6c69bb49913f3619430c1d0bf4
[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 (c) 2016 NXP. All rights reserved.
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
50 #include <rte_mbuf.h>
51 #include <rte_ethdev.h>
52 #include <rte_malloc.h>
53 #include <rte_memcpy.h>
54 #include <rte_string_fns.h>
55 #include <rte_cycles.h>
56 #include <rte_kvargs.h>
57 #include <rte_dev.h>
58 #include <rte_ethdev.h>
59
60 #include <fslmc_logs.h>
61 #include <fslmc_vfio.h>
62 #include "dpaa2_hw_pvt.h"
63 #include "dpaa2_hw_dpio.h"
64 #include <mc/fsl_dpmng.h>
65
66 #define NUM_HOST_CPUS RTE_MAX_LCORE
67
68 struct dpaa2_io_portal_t dpaa2_io_portal[RTE_MAX_LCORE];
69 RTE_DEFINE_PER_LCORE(struct dpaa2_io_portal_t, _dpaa2_io);
70
71 struct swp_active_dqs rte_global_active_dqs_list[NUM_MAX_SWP];
72
73 TAILQ_HEAD(dpio_dev_list, dpaa2_dpio_dev);
74 static struct dpio_dev_list dpio_dev_list
75         = TAILQ_HEAD_INITIALIZER(dpio_dev_list); /*!< DPIO device list */
76 static uint32_t io_space_count;
77
78 /*Stashing Macros default for LS208x*/
79 static int dpaa2_core_cluster_base = 0x04;
80 static int dpaa2_cluster_sz = 2;
81
82 /* For LS208X platform There are four clusters with following mapping:
83  * Cluster 1 (ID = x04) : CPU0, CPU1;
84  * Cluster 2 (ID = x05) : CPU2, CPU3;
85  * Cluster 3 (ID = x06) : CPU4, CPU5;
86  * Cluster 4 (ID = x07) : CPU6, CPU7;
87  */
88 /* For LS108X platform There are two clusters with following mapping:
89  * Cluster 1 (ID = x02) : CPU0, CPU1, CPU2, CPU3;
90  * Cluster 2 (ID = x03) : CPU4, CPU5, CPU6, CPU7;
91  */
92
93 /* Set the STASH Destination depending on Current CPU ID.
94  * e.g. Valid values of SDEST are 4,5,6,7. Where,
95  * CPU 0-1 will have SDEST 4
96  * CPU 2-3 will have SDEST 5.....and so on.
97  */
98 static int
99 dpaa2_core_cluster_sdest(int cpu_id)
100 {
101         int x = cpu_id / dpaa2_cluster_sz;
102
103         if (x > 3)
104                 x = 3;
105
106         return dpaa2_core_cluster_base + x;
107 }
108
109 static int
110 configure_dpio_qbman_swp(struct dpaa2_dpio_dev *dpio_dev)
111 {
112         struct qbman_swp_desc p_des;
113         struct dpio_attr attr;
114
115         dpio_dev->dpio = malloc(sizeof(struct fsl_mc_io));
116         if (!dpio_dev->dpio) {
117                 PMD_INIT_LOG(ERR, "Memory allocation failure\n");
118                 return -1;
119         }
120
121         PMD_DRV_LOG(DEBUG, "\t Allocated  DPIO Portal[%p]", dpio_dev->dpio);
122         dpio_dev->dpio->regs = dpio_dev->mc_portal;
123         if (dpio_open(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->hw_id,
124                       &dpio_dev->token)) {
125                 PMD_INIT_LOG(ERR, "Failed to allocate IO space\n");
126                 free(dpio_dev->dpio);
127                 return -1;
128         }
129
130         if (dpio_reset(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token)) {
131                 PMD_INIT_LOG(ERR, "Failed to reset dpio\n");
132                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
133                 free(dpio_dev->dpio);
134                 return -1;
135         }
136
137         if (dpio_enable(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token)) {
138                 PMD_INIT_LOG(ERR, "Failed to Enable dpio\n");
139                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
140                 free(dpio_dev->dpio);
141                 return -1;
142         }
143
144         if (dpio_get_attributes(dpio_dev->dpio, CMD_PRI_LOW,
145                                 dpio_dev->token, &attr)) {
146                 PMD_INIT_LOG(ERR, "DPIO Get attribute failed\n");
147                 dpio_disable(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
148                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW,  dpio_dev->token);
149                 free(dpio_dev->dpio);
150                 return -1;
151         }
152
153         PMD_INIT_LOG(DEBUG, "Qbman Portal ID %d", attr.qbman_portal_id);
154         PMD_INIT_LOG(DEBUG, "Portal CE adr 0x%lX", attr.qbman_portal_ce_offset);
155         PMD_INIT_LOG(DEBUG, "Portal CI adr 0x%lX", attr.qbman_portal_ci_offset);
156
157         /* Configure & setup SW portal */
158         p_des.block = NULL;
159         p_des.idx = attr.qbman_portal_id;
160         p_des.cena_bar = (void *)(dpio_dev->qbman_portal_ce_paddr);
161         p_des.cinh_bar = (void *)(dpio_dev->qbman_portal_ci_paddr);
162         p_des.irq = -1;
163         p_des.qman_version = attr.qbman_version;
164
165         dpio_dev->sw_portal = qbman_swp_init(&p_des);
166         if (dpio_dev->sw_portal == NULL) {
167                 PMD_DRV_LOG(ERR, " QBMan SW Portal Init failed\n");
168                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
169                 free(dpio_dev->dpio);
170                 return -1;
171         }
172
173         PMD_INIT_LOG(DEBUG, "QBMan SW Portal 0x%p\n", dpio_dev->sw_portal);
174
175         return 0;
176 }
177
178 static int
179 dpaa2_configure_stashing(struct dpaa2_dpio_dev *dpio_dev)
180 {
181         int sdest;
182         int cpu_id, ret;
183         static int first_time;
184
185         /* find the SoC type for the first time */
186         if (!first_time) {
187                 struct mc_soc_version mc_plat_info = {0};
188
189                 if (mc_get_soc_version(dpio_dev->dpio,
190                                        CMD_PRI_LOW, &mc_plat_info)) {
191                         PMD_INIT_LOG(ERR, "\tmc_get_soc_version failed\n");
192                 } else if ((mc_plat_info.svr & 0xffff0000) == SVR_LS1080A) {
193                         dpaa2_core_cluster_base = 0x02;
194                         dpaa2_cluster_sz = 4;
195                         PMD_INIT_LOG(DEBUG, "\tLS108x (A53) Platform Detected");
196                 }
197                 first_time = 1;
198         }
199
200         /* Set the Stashing Destination */
201         cpu_id = rte_lcore_id();
202         if (cpu_id < 0) {
203                 cpu_id = rte_get_master_lcore();
204                 if (cpu_id < 0) {
205                         RTE_LOG(ERR, PMD, "\tGetting CPU Index failed\n");
206                         return -1;
207                 }
208         }
209         /* Set the STASH Destination depending on Current CPU ID.
210          * Valid values of SDEST are 4,5,6,7. Where,
211          */
212
213         sdest = dpaa2_core_cluster_sdest(cpu_id);
214         PMD_DRV_LOG(DEBUG, "Portal= %d  CPU= %u SDEST= %d",
215                     dpio_dev->index, cpu_id, sdest);
216
217         ret = dpio_set_stashing_destination(dpio_dev->dpio, CMD_PRI_LOW,
218                                             dpio_dev->token, sdest);
219         if (ret) {
220                 PMD_DRV_LOG(ERR, "%d ERROR in SDEST\n",  ret);
221                 return -1;
222         }
223
224         return 0;
225 }
226
227 static inline struct dpaa2_dpio_dev *dpaa2_get_qbman_swp(void)
228 {
229         struct dpaa2_dpio_dev *dpio_dev = NULL;
230         int ret;
231
232         /* Get DPIO dev handle from list using index */
233         TAILQ_FOREACH(dpio_dev, &dpio_dev_list, next) {
234                 if (dpio_dev && rte_atomic16_test_and_set(&dpio_dev->ref_count))
235                         break;
236         }
237         if (!dpio_dev)
238                 return NULL;
239
240         PMD_DRV_LOG(DEBUG, "New Portal=0x%x (%d) affined thread - %lu",
241                     dpio_dev, dpio_dev->index, syscall(SYS_gettid));
242
243         ret = dpaa2_configure_stashing(dpio_dev);
244         if (ret)
245                 PMD_DRV_LOG(ERR, "dpaa2_configure_stashing failed");
246
247         return dpio_dev;
248 }
249
250 int
251 dpaa2_affine_qbman_swp(void)
252 {
253         unsigned int lcore_id = rte_lcore_id();
254         uint64_t tid = syscall(SYS_gettid);
255
256         if (lcore_id == LCORE_ID_ANY)
257                 lcore_id = rte_get_master_lcore();
258         /* if the core id is not supported */
259         else if (lcore_id >= RTE_MAX_LCORE)
260                 return -1;
261
262         if (dpaa2_io_portal[lcore_id].dpio_dev) {
263                 PMD_DRV_LOG(INFO, "DPAA Portal=0x%x (%d) is being shared"
264                             " between thread %lu and current  %lu",
265                             dpaa2_io_portal[lcore_id].dpio_dev,
266                             dpaa2_io_portal[lcore_id].dpio_dev->index,
267                             dpaa2_io_portal[lcore_id].net_tid,
268                             tid);
269                 RTE_PER_LCORE(_dpaa2_io).dpio_dev
270                         = dpaa2_io_portal[lcore_id].dpio_dev;
271                 rte_atomic16_inc(&dpaa2_io_portal
272                                  [lcore_id].dpio_dev->ref_count);
273                 dpaa2_io_portal[lcore_id].net_tid = tid;
274
275                 PMD_DRV_LOG(DEBUG, "Old Portal=0x%x (%d) affined thread - %lu",
276                             dpaa2_io_portal[lcore_id].dpio_dev,
277                             dpaa2_io_portal[lcore_id].dpio_dev->index,
278                             tid);
279                 return 0;
280         }
281
282         /* Populate the dpaa2_io_portal structure */
283         dpaa2_io_portal[lcore_id].dpio_dev = dpaa2_get_qbman_swp();
284
285         if (dpaa2_io_portal[lcore_id].dpio_dev) {
286                 RTE_PER_LCORE(_dpaa2_io).dpio_dev
287                         = dpaa2_io_portal[lcore_id].dpio_dev;
288                 dpaa2_io_portal[lcore_id].net_tid = tid;
289
290                 return 0;
291         } else {
292                 return -1;
293         }
294 }
295
296 int
297 dpaa2_affine_qbman_swp_sec(void)
298 {
299         unsigned int lcore_id = rte_lcore_id();
300         uint64_t tid = syscall(SYS_gettid);
301
302         if (lcore_id == LCORE_ID_ANY)
303                 lcore_id = rte_get_master_lcore();
304         /* if the core id is not supported */
305         else if (lcore_id >= RTE_MAX_LCORE)
306                 return -1;
307
308         if (dpaa2_io_portal[lcore_id].sec_dpio_dev) {
309                 PMD_DRV_LOG(INFO, "DPAA Portal=0x%x (%d) is being shared"
310                             " between thread %lu and current  %lu",
311                             dpaa2_io_portal[lcore_id].sec_dpio_dev,
312                             dpaa2_io_portal[lcore_id].sec_dpio_dev->index,
313                             dpaa2_io_portal[lcore_id].sec_tid,
314                             tid);
315                 RTE_PER_LCORE(_dpaa2_io).sec_dpio_dev
316                         = dpaa2_io_portal[lcore_id].sec_dpio_dev;
317                 rte_atomic16_inc(&dpaa2_io_portal
318                                  [lcore_id].sec_dpio_dev->ref_count);
319                 dpaa2_io_portal[lcore_id].sec_tid = tid;
320
321                 PMD_DRV_LOG(DEBUG, "Old Portal=0x%x (%d) affined thread - %lu",
322                             dpaa2_io_portal[lcore_id].sec_dpio_dev,
323                             dpaa2_io_portal[lcore_id].sec_dpio_dev->index,
324                             tid);
325                 return 0;
326         }
327
328         /* Populate the dpaa2_io_portal structure */
329         dpaa2_io_portal[lcore_id].sec_dpio_dev = dpaa2_get_qbman_swp();
330
331         if (dpaa2_io_portal[lcore_id].sec_dpio_dev) {
332                 RTE_PER_LCORE(_dpaa2_io).sec_dpio_dev
333                         = dpaa2_io_portal[lcore_id].sec_dpio_dev;
334                 dpaa2_io_portal[lcore_id].sec_tid = tid;
335                 return 0;
336         } else {
337                 return -1;
338         }
339 }
340
341 int
342 dpaa2_create_dpio_device(struct fslmc_vfio_device *vdev,
343                          struct vfio_device_info *obj_info,
344                 int object_id)
345 {
346         struct dpaa2_dpio_dev *dpio_dev;
347         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info)};
348
349         if (obj_info->num_regions < NUM_DPIO_REGIONS) {
350                 PMD_INIT_LOG(ERR, "ERROR, Not sufficient number "
351                                 "of DPIO regions.\n");
352                 return -1;
353         }
354
355         dpio_dev = rte_malloc(NULL, sizeof(struct dpaa2_dpio_dev),
356                               RTE_CACHE_LINE_SIZE);
357         if (!dpio_dev) {
358                 PMD_INIT_LOG(ERR, "Memory allocation failed for DPIO Device\n");
359                 return -1;
360         }
361
362         PMD_DRV_LOG(INFO, "\t Aloocated DPIO [%p]", dpio_dev);
363         dpio_dev->dpio = NULL;
364         dpio_dev->hw_id = object_id;
365         dpio_dev->vfio_fd = vdev->fd;
366         rte_atomic16_init(&dpio_dev->ref_count);
367         /* Using single portal  for all devices */
368         dpio_dev->mc_portal = rte_mcp_ptr_list[MC_PORTAL_INDEX];
369
370         reg_info.index = 0;
371         if (ioctl(dpio_dev->vfio_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info)) {
372                 PMD_INIT_LOG(ERR, "vfio: error getting region info\n");
373                 rte_free(dpio_dev);
374                 return -1;
375         }
376
377         PMD_DRV_LOG(DEBUG, "\t  Region Offset = %llx", reg_info.offset);
378         PMD_DRV_LOG(DEBUG, "\t  Region Size = %llx", reg_info.size);
379         dpio_dev->ce_size = reg_info.size;
380         dpio_dev->qbman_portal_ce_paddr = (uint64_t)mmap(NULL, reg_info.size,
381                                 PROT_WRITE | PROT_READ, MAP_SHARED,
382                                 dpio_dev->vfio_fd, reg_info.offset);
383
384         /* Create Mapping for QBMan Cache Enabled area. This is a fix for
385          * SMMU fault for DQRR statshing transaction.
386          */
387         if (vfio_dmamap_mem_region(dpio_dev->qbman_portal_ce_paddr,
388                                    reg_info.offset, reg_info.size)) {
389                 PMD_INIT_LOG(ERR, "DMAMAP for Portal CE area failed.\n");
390                 rte_free(dpio_dev);
391                 return -1;
392         }
393
394         reg_info.index = 1;
395         if (ioctl(dpio_dev->vfio_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info)) {
396                 PMD_INIT_LOG(ERR, "vfio: error getting region info\n");
397                 rte_free(dpio_dev);
398                 return -1;
399         }
400
401         PMD_DRV_LOG(DEBUG, "\t  Region Offset = %llx", reg_info.offset);
402         PMD_DRV_LOG(DEBUG, "\t  Region Size = %llx", reg_info.size);
403         dpio_dev->ci_size = reg_info.size;
404         dpio_dev->qbman_portal_ci_paddr = (uint64_t)mmap(NULL, reg_info.size,
405                                 PROT_WRITE | PROT_READ, MAP_SHARED,
406                                 dpio_dev->vfio_fd, reg_info.offset);
407
408         if (configure_dpio_qbman_swp(dpio_dev)) {
409                 PMD_INIT_LOG(ERR,
410                              "Fail to configure the dpio qbman portal for %d\n",
411                              dpio_dev->hw_id);
412                 rte_free(dpio_dev);
413                 return -1;
414         }
415
416         io_space_count++;
417         dpio_dev->index = io_space_count;
418         TAILQ_INSERT_TAIL(&dpio_dev_list, dpio_dev, next);
419         PMD_INIT_LOG(DEBUG, "DPAA2:Added [dpio-%d]", object_id);
420
421         return 0;
422 }
423
424 void
425 dpaa2_free_dq_storage(struct queue_storage_info_t *q_storage)
426 {
427         int i = 0;
428
429         for (i = 0; i < NUM_DQS_PER_QUEUE; i++) {
430                 if (q_storage->dq_storage[i])
431                         rte_free(q_storage->dq_storage[i]);
432         }
433 }
434
435 int
436 dpaa2_alloc_dq_storage(struct queue_storage_info_t *q_storage)
437 {
438         int i = 0;
439
440         for (i = 0; i < NUM_DQS_PER_QUEUE; i++) {
441                 q_storage->dq_storage[i] = rte_malloc(NULL,
442                         DPAA2_DQRR_RING_SIZE * sizeof(struct qbman_result),
443                         RTE_CACHE_LINE_SIZE);
444                 if (!q_storage->dq_storage[i])
445                         goto fail;
446         }
447         return 0;
448 fail:
449         i -= 1;
450         while (i >= 0)
451                 rte_free(q_storage->dq_storage[i]);
452
453         return -1;
454 }