mempool/octeontx: support capabilities query
[dpdk.git] / drivers / mempool / octeontx / rte_mempool_octeontx.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2017 Cavium Inc. All rights reserved.
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 Intel Corporation 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 #include <stdio.h>
33 #include <rte_mempool.h>
34 #include <rte_malloc.h>
35 #include <rte_mbuf.h>
36
37 #include "octeontx_fpavf.h"
38
39 static int
40 octeontx_fpavf_alloc(struct rte_mempool *mp)
41 {
42         uintptr_t pool;
43         uint32_t memseg_count = mp->size;
44         uint32_t object_size;
45         uintptr_t va_start;
46         int rc = 0;
47
48         /* virtual hugepage mapped addr */
49         va_start = ~(uint64_t)0;
50
51         object_size = mp->elt_size + mp->header_size + mp->trailer_size;
52
53         pool = octeontx_fpa_bufpool_create(object_size, memseg_count,
54                                                 OCTEONTX_FPAVF_BUF_OFFSET,
55                                                 (char **)&va_start,
56                                                 mp->socket_id);
57         rc = octeontx_fpa_bufpool_block_size(pool);
58         if (rc < 0)
59                 goto _end;
60
61         if ((uint32_t)rc != object_size)
62                 fpavf_log_err("buffer size mismatch: %d instead of %u\n",
63                                 rc, object_size);
64
65         fpavf_log_info("Pool created %p with .. ", (void *)pool);
66         fpavf_log_info("obj_sz %d, cnt %d\n", object_size, memseg_count);
67
68         /* assign pool handle to mempool */
69         mp->pool_id = (uint64_t)pool;
70
71         return 0;
72
73 _end:
74         return rc;
75 }
76
77 static void
78 octeontx_fpavf_free(struct rte_mempool *mp)
79 {
80         uintptr_t pool;
81
82         pool = (uintptr_t)mp->pool_id;
83
84         octeontx_fpa_bufpool_destroy(pool, mp->socket_id);
85 }
86
87 static __rte_always_inline void *
88 octeontx_fpa_bufpool_alloc(uintptr_t handle)
89 {
90         return (void *)(uintptr_t)fpavf_read64((void *)(handle +
91                                                 FPA_VF_VHAURA_OP_ALLOC(0)));
92 }
93
94 static __rte_always_inline void
95 octeontx_fpa_bufpool_free(uintptr_t handle, void *buf)
96 {
97         uint64_t free_addr = FPA_VF_FREE_ADDRS_S(FPA_VF_VHAURA_OP_FREE(0),
98                                                  0 /* DWB */, 1 /* FABS */);
99
100         fpavf_write64((uintptr_t)buf, (void *)(uintptr_t)(handle + free_addr));
101 }
102
103 static int
104 octeontx_fpavf_enqueue(struct rte_mempool *mp, void * const *obj_table,
105                         unsigned int n)
106 {
107         uintptr_t pool;
108         unsigned int index;
109
110         pool = (uintptr_t)mp->pool_id;
111         /* Get pool bar address from handle */
112         pool &= ~(uint64_t)FPA_GPOOL_MASK;
113         for (index = 0; index < n; index++, obj_table++)
114                 octeontx_fpa_bufpool_free(pool, *obj_table);
115
116         return 0;
117 }
118
119 static int
120 octeontx_fpavf_dequeue(struct rte_mempool *mp, void **obj_table,
121                         unsigned int n)
122 {
123         unsigned int index;
124         uintptr_t pool;
125         void *obj;
126
127         pool = (uintptr_t)mp->pool_id;
128         /* Get pool bar address from handle */
129         pool &= ~(uint64_t)FPA_GPOOL_MASK;
130         for (index = 0; index < n; index++, obj_table++) {
131                 obj = octeontx_fpa_bufpool_alloc(pool);
132                 if (obj == NULL) {
133                         /*
134                          * Failed to allocate the requested number of objects
135                          * from the pool. Current pool implementation requires
136                          * completing the entire request or returning error
137                          * otherwise.
138                          * Free already allocated buffers to the pool.
139                          */
140                         for (; index > 0; index--) {
141                                 obj_table--;
142                                 octeontx_fpa_bufpool_free(pool, *obj_table);
143                         }
144                         return -ENOMEM;
145                 }
146                 *obj_table = obj;
147         }
148
149         return 0;
150 }
151
152 static unsigned int
153 octeontx_fpavf_get_count(const struct rte_mempool *mp)
154 {
155         uintptr_t pool;
156
157         pool = (uintptr_t)mp->pool_id;
158
159         return octeontx_fpa_bufpool_free_count(pool);
160 }
161
162 static int
163 octeontx_fpavf_get_capabilities(const struct rte_mempool *mp,
164                                 unsigned int *flags)
165 {
166         RTE_SET_USED(mp);
167         *flags |= (MEMPOOL_F_CAPA_PHYS_CONTIG |
168                         MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS);
169         return 0;
170 }
171
172 static struct rte_mempool_ops octeontx_fpavf_ops = {
173         .name = "octeontx_fpavf",
174         .alloc = octeontx_fpavf_alloc,
175         .free = octeontx_fpavf_free,
176         .enqueue = octeontx_fpavf_enqueue,
177         .dequeue = octeontx_fpavf_dequeue,
178         .get_count = octeontx_fpavf_get_count,
179         .get_capabilities = octeontx_fpavf_get_capabilities,
180         .register_memory_area = NULL,
181 };
182
183 MEMPOOL_REGISTER_OPS(octeontx_fpavf_ops);