New upstream version 18.02
[deb_dpdk.git] / drivers / event / dpaa2 / dpaa2_hw_dpcon.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright 2017 NXP
4  *
5  */
6
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <errno.h>
14
15 #include <rte_malloc.h>
16 #include <rte_memcpy.h>
17 #include <rte_string_fns.h>
18 #include <rte_cycles.h>
19 #include <rte_kvargs.h>
20 #include <rte_dev.h>
21 #include <rte_ethdev_driver.h>
22
23 #include <fslmc_logs.h>
24 #include <rte_fslmc.h>
25 #include <mc/fsl_dpcon.h>
26 #include <portal/dpaa2_hw_pvt.h>
27 #include "dpaa2_eventdev.h"
28
29 TAILQ_HEAD(dpcon_dev_list, dpaa2_dpcon_dev);
30 static struct dpcon_dev_list dpcon_dev_list
31         = TAILQ_HEAD_INITIALIZER(dpcon_dev_list); /*!< DPCON device list */
32
33 static int
34 rte_dpaa2_create_dpcon_device(int dev_fd __rte_unused,
35                               struct vfio_device_info *obj_info __rte_unused,
36                               int dpcon_id)
37 {
38         struct dpaa2_dpcon_dev *dpcon_node;
39         struct dpcon_attr attr;
40         int ret;
41
42         /* Allocate DPAA2 dpcon handle */
43         dpcon_node = rte_malloc(NULL, sizeof(struct dpaa2_dpcon_dev), 0);
44         if (!dpcon_node) {
45                 PMD_DRV_LOG(ERR, "Memory allocation failed for DPCON Device");
46                 return -1;
47         }
48
49         /* Open the dpcon object */
50         dpcon_node->dpcon.regs = rte_mcp_ptr_list[MC_PORTAL_INDEX];
51         ret = dpcon_open(&dpcon_node->dpcon,
52                          CMD_PRI_LOW, dpcon_id, &dpcon_node->token);
53         if (ret) {
54                 PMD_DRV_LOG(ERR, "Resource alloc failure with err code: %d",
55                             ret);
56                 rte_free(dpcon_node);
57                 return -1;
58         }
59
60         /* Get the device attributes */
61         ret = dpcon_get_attributes(&dpcon_node->dpcon,
62                                    CMD_PRI_LOW, dpcon_node->token, &attr);
63         if (ret != 0) {
64                 PMD_DRV_LOG(ERR, "Reading device failed with err code: %d",
65                             ret);
66                 rte_free(dpcon_node);
67                 return -1;
68         }
69
70         /* Updating device specific private information*/
71         dpcon_node->qbman_ch_id = attr.qbman_ch_id;
72         dpcon_node->num_priorities = attr.num_priorities;
73         dpcon_node->dpcon_id = dpcon_id;
74         rte_atomic16_init(&dpcon_node->in_use);
75
76         TAILQ_INSERT_TAIL(&dpcon_dev_list, dpcon_node, next);
77
78         RTE_LOG(DEBUG, PMD, "DPAA2: Added [dpcon.%d]\n", dpcon_id);
79
80         return 0;
81 }
82
83 struct dpaa2_dpcon_dev *rte_dpaa2_alloc_dpcon_dev(void)
84 {
85         struct dpaa2_dpcon_dev *dpcon_dev = NULL;
86
87         /* Get DPCON dev handle from list using index */
88         TAILQ_FOREACH(dpcon_dev, &dpcon_dev_list, next) {
89                 if (dpcon_dev && rte_atomic16_test_and_set(&dpcon_dev->in_use))
90                         break;
91         }
92
93         return dpcon_dev;
94 }
95
96 void rte_dpaa2_free_dpcon_dev(struct dpaa2_dpcon_dev *dpcon)
97 {
98         struct dpaa2_dpcon_dev *dpcon_dev = NULL;
99
100         /* Match DPCON handle and mark it free */
101         TAILQ_FOREACH(dpcon_dev, &dpcon_dev_list, next) {
102                 if (dpcon_dev == dpcon) {
103                         rte_atomic16_dec(&dpcon_dev->in_use);
104                         return;
105                 }
106         }
107 }
108
109 static struct rte_dpaa2_object rte_dpaa2_dpcon_obj = {
110         .dev_type = DPAA2_CON,
111         .create = rte_dpaa2_create_dpcon_device,
112 };
113
114 RTE_PMD_REGISTER_DPAA2_OBJECT(dpcon, rte_dpaa2_dpcon_obj);