New upstream version 17.11.1
[deb_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         int rc = 0;
46
47         object_size = mp->elt_size + mp->header_size + mp->trailer_size;
48
49         pool = octeontx_fpa_bufpool_create(object_size, memseg_count,
50                                                 OCTEONTX_FPAVF_BUF_OFFSET,
51                                                 mp->socket_id);
52         rc = octeontx_fpa_bufpool_block_size(pool);
53         if (rc < 0)
54                 goto _end;
55
56         if ((uint32_t)rc != object_size)
57                 fpavf_log_err("buffer size mismatch: %d instead of %u\n",
58                                 rc, object_size);
59
60         fpavf_log_info("Pool created %p with .. ", (void *)pool);
61         fpavf_log_info("obj_sz %d, cnt %d\n", object_size, memseg_count);
62
63         /* assign pool handle to mempool */
64         mp->pool_id = (uint64_t)pool;
65
66         return 0;
67
68 _end:
69         return rc;
70 }
71
72 static void
73 octeontx_fpavf_free(struct rte_mempool *mp)
74 {
75         uintptr_t pool;
76         pool = (uintptr_t)mp->pool_id;
77
78         octeontx_fpa_bufpool_destroy(pool, mp->socket_id);
79 }
80
81 static __rte_always_inline void *
82 octeontx_fpa_bufpool_alloc(uintptr_t handle)
83 {
84         return (void *)(uintptr_t)fpavf_read64((void *)(handle +
85                                                 FPA_VF_VHAURA_OP_ALLOC(0)));
86 }
87
88 static __rte_always_inline void
89 octeontx_fpa_bufpool_free(uintptr_t handle, void *buf)
90 {
91         uint64_t free_addr = FPA_VF_FREE_ADDRS_S(FPA_VF_VHAURA_OP_FREE(0),
92                                                  0 /* DWB */, 1 /* FABS */);
93
94         fpavf_write64((uintptr_t)buf, (void *)(uintptr_t)(handle + free_addr));
95 }
96
97 static int
98 octeontx_fpavf_enqueue(struct rte_mempool *mp, void * const *obj_table,
99                         unsigned int n)
100 {
101         uintptr_t pool;
102         unsigned int index;
103
104         pool = (uintptr_t)mp->pool_id;
105         /* Get pool bar address from handle */
106         pool &= ~(uint64_t)FPA_GPOOL_MASK;
107         for (index = 0; index < n; index++, obj_table++)
108                 octeontx_fpa_bufpool_free(pool, *obj_table);
109
110         return 0;
111 }
112
113 static int
114 octeontx_fpavf_dequeue(struct rte_mempool *mp, void **obj_table,
115                         unsigned int n)
116 {
117         unsigned int index;
118         uintptr_t pool;
119         void *obj;
120
121         pool = (uintptr_t)mp->pool_id;
122         /* Get pool bar address from handle */
123         pool &= ~(uint64_t)FPA_GPOOL_MASK;
124         for (index = 0; index < n; index++, obj_table++) {
125                 obj = octeontx_fpa_bufpool_alloc(pool);
126                 if (obj == NULL) {
127                         /*
128                          * Failed to allocate the requested number of objects
129                          * from the pool. Current pool implementation requires
130                          * completing the entire request or returning error
131                          * otherwise.
132                          * Free already allocated buffers to the pool.
133                          */
134                         for (; index > 0; index--) {
135                                 obj_table--;
136                                 octeontx_fpa_bufpool_free(pool, *obj_table);
137                         }
138                         return -ENOMEM;
139                 }
140                 *obj_table = obj;
141         }
142
143         return 0;
144 }
145
146 static unsigned int
147 octeontx_fpavf_get_count(const struct rte_mempool *mp)
148 {
149         uintptr_t pool;
150
151         pool = (uintptr_t)mp->pool_id;
152
153         return octeontx_fpa_bufpool_free_count(pool);
154 }
155
156 static int
157 octeontx_fpavf_get_capabilities(const struct rte_mempool *mp,
158                                 unsigned int *flags)
159 {
160         RTE_SET_USED(mp);
161         *flags |= (MEMPOOL_F_CAPA_PHYS_CONTIG |
162                         MEMPOOL_F_CAPA_BLK_ALIGNED_OBJECTS);
163         return 0;
164 }
165
166 static int
167 octeontx_fpavf_register_memory_area(const struct rte_mempool *mp,
168                                     char *vaddr, rte_iova_t paddr, size_t len)
169 {
170         RTE_SET_USED(paddr);
171         uint8_t gpool;
172         uintptr_t pool_bar;
173
174         gpool = octeontx_fpa_bufpool_gpool(mp->pool_id);
175         pool_bar = mp->pool_id & ~(uint64_t)FPA_GPOOL_MASK;
176
177         return octeontx_fpavf_pool_set_range(pool_bar, len, vaddr, gpool);
178 }
179
180 static struct rte_mempool_ops octeontx_fpavf_ops = {
181         .name = "octeontx_fpavf",
182         .alloc = octeontx_fpavf_alloc,
183         .free = octeontx_fpavf_free,
184         .enqueue = octeontx_fpavf_enqueue,
185         .dequeue = octeontx_fpavf_dequeue,
186         .get_count = octeontx_fpavf_get_count,
187         .get_capabilities = octeontx_fpavf_get_capabilities,
188         .register_memory_area = octeontx_fpavf_register_memory_area,
189 };
190
191 MEMPOOL_REGISTER_OPS(octeontx_fpavf_ops);