eal: bump ABI version for bus refactoring
[dpdk.git] / lib / librte_acl / acl_run_altivec.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) IBM Corporation 2016.
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 IBM 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
33 #include "acl_run.h"
34 #include "acl_vect.h"
35
36 struct _altivec_acl_const {
37         rte_xmm_t xmm_shuffle_input;
38         rte_xmm_t xmm_index_mask;
39         rte_xmm_t xmm_ones_16;
40         rte_xmm_t range_base;
41 } altivec_acl_const  __attribute__((aligned(RTE_CACHE_LINE_SIZE))) = {
42         {
43                 .u32 = {0x00000000, 0x04040404, 0x08080808, 0x0c0c0c0c}
44         },
45         {
46                 .u32 = {RTE_ACL_NODE_INDEX, RTE_ACL_NODE_INDEX,
47                 RTE_ACL_NODE_INDEX, RTE_ACL_NODE_INDEX}
48         },
49         {
50                 .u16 = {1, 1, 1, 1, 1, 1, 1, 1}
51         },
52         {
53                 .u32 = {0xffffff00, 0xffffff04, 0xffffff08, 0xffffff0c}
54         },
55 };
56
57 /*
58  * Resolve priority for multiple results (altivec version).
59  * This consists comparing the priority of the current traversal with the
60  * running set of results for the packet.
61  * For each result, keep a running array of the result (rule number) and
62  * its priority for each category.
63  */
64 static inline void
65 resolve_priority_altivec(uint64_t transition, int n,
66         const struct rte_acl_ctx *ctx, struct parms *parms,
67         const struct rte_acl_match_results *p, uint32_t categories)
68 {
69         uint32_t x;
70         xmm_t results, priority, results1, priority1;
71         vector bool int selector;
72         xmm_t *saved_results, *saved_priority;
73
74         for (x = 0; x < categories; x += RTE_ACL_RESULTS_MULTIPLIER) {
75
76                 saved_results = (xmm_t *)(&parms[n].cmplt->results[x]);
77                 saved_priority =
78                         (xmm_t *)(&parms[n].cmplt->priority[x]);
79
80                 /* get results and priorities for completed trie */
81                 results = *(const xmm_t *)&p[transition].results[x];
82                 priority = *(const xmm_t *)&p[transition].priority[x];
83
84                 /* if this is not the first completed trie */
85                 if (parms[n].cmplt->count != ctx->num_tries) {
86
87                         /* get running best results and their priorities */
88                         results1 = *saved_results;
89                         priority1 = *saved_priority;
90
91                         /* select results that are highest priority */
92                         selector = vec_cmpgt(priority1, priority);
93                         results = vec_sel(results, results1, selector);
94                         priority = vec_sel(priority, priority1,
95                                 selector);
96                 }
97
98                 /* save running best results and their priorities */
99                 *saved_results = results;
100                 *saved_priority = priority;
101         }
102 }
103
104 /*
105  * Check for any match in 4 transitions
106  */
107 static inline __attribute__((always_inline)) uint32_t
108 check_any_match_x4(uint64_t val[])
109 {
110         return (val[0] | val[1] | val[2] | val[3]) & RTE_ACL_NODE_MATCH;
111 }
112
113 static inline __attribute__((always_inline)) void
114 acl_match_check_x4(int slot, const struct rte_acl_ctx *ctx, struct parms *parms,
115         struct acl_flow_data *flows, uint64_t transitions[])
116 {
117         while (check_any_match_x4(transitions)) {
118                 transitions[0] = acl_match_check(transitions[0], slot, ctx,
119                         parms, flows, resolve_priority_altivec);
120                 transitions[1] = acl_match_check(transitions[1], slot + 1, ctx,
121                         parms, flows, resolve_priority_altivec);
122                 transitions[2] = acl_match_check(transitions[2], slot + 2, ctx,
123                         parms, flows, resolve_priority_altivec);
124                 transitions[3] = acl_match_check(transitions[3], slot + 3, ctx,
125                         parms, flows, resolve_priority_altivec);
126         }
127 }
128
129 /*
130  * Process 4 transitions (in 2 XMM registers) in parallel
131  */
132 static inline __attribute__((optimize("O2"))) xmm_t
133 transition4(xmm_t next_input, const uint64_t *trans,
134         xmm_t *indices1, xmm_t *indices2)
135 {
136         xmm_t addr, tr_lo, tr_hi;
137         xmm_t in, node_type, r, t;
138         xmm_t dfa_ofs, quad_ofs;
139         xmm_t *index_mask, *tp;
140         vector bool int dfa_msk;
141         vector signed char zeroes = {};
142         union {
143                 uint64_t d64[2];
144                 uint32_t d32[4];
145         } v;
146
147         /* Move low 32 into tr_lo and high 32 into tr_hi */
148         tr_lo = (xmm_t){(*indices1)[0], (*indices1)[2],
149                         (*indices2)[0], (*indices2)[2]};
150         tr_hi = (xmm_t){(*indices1)[1], (*indices1)[3],
151                         (*indices2)[1], (*indices2)[3]};
152
153          /* Calculate the address (array index) for all 4 transitions. */
154         index_mask = (xmm_t *)&altivec_acl_const.xmm_index_mask.u32;
155         t = vec_xor(*index_mask, *index_mask);
156         in = vec_perm(next_input, (xmm_t){},
157                 *(vector unsigned char *)&altivec_acl_const.xmm_shuffle_input);
158
159         /* Calc node type and node addr */
160         node_type = vec_and(vec_nor(*index_mask, *index_mask), tr_lo);
161         addr = vec_and(tr_lo, *index_mask);
162
163         /* mask for DFA type(0) nodes */
164         dfa_msk = vec_cmpeq(node_type, t);
165
166         /* DFA calculations. */
167         r = vec_sr(in, (vector unsigned int){30, 30, 30, 30});
168         tp = (xmm_t *)&altivec_acl_const.range_base.u32;
169         r = vec_add(r, *tp);
170         t = vec_sr(in, (vector unsigned int){24, 24, 24, 24});
171         r = vec_perm(tr_hi, (xmm_t){(uint16_t)0 << 16},
172                 (vector unsigned char)r);
173
174         dfa_ofs = vec_sub(t, r);
175
176         /* QUAD/SINGLE caluclations. */
177         t = (xmm_t)vec_cmpgt((vector signed char)in, (vector signed char)tr_hi);
178         t = (xmm_t)vec_sel(
179                 vec_sel(
180                         (vector signed char)vec_sub(
181                                 zeroes, (vector signed char)t),
182                         (vector signed char)t,
183                         vec_cmpgt((vector signed char)t, zeroes)),
184                 zeroes,
185                 vec_cmpeq((vector signed char)t, zeroes));
186
187         t = (xmm_t)vec_msum((vector signed char)t,
188                 (vector unsigned char)t, (xmm_t){});
189         quad_ofs = (xmm_t)vec_msum((vector signed short)t,
190                 *(vector signed short *)&altivec_acl_const.xmm_ones_16.u16,
191                 (xmm_t){});
192
193         /* blend DFA and QUAD/SINGLE. */
194         t = vec_sel(quad_ofs, dfa_ofs, dfa_msk);
195
196         /* calculate address for next transitions. */
197         addr = vec_add(addr, t);
198
199         v.d64[0] = (uint64_t)trans[addr[0]];
200         v.d64[1] = (uint64_t)trans[addr[1]];
201         *indices1 = (xmm_t){v.d32[0], v.d32[1], v.d32[2], v.d32[3]};
202         v.d64[0] = (uint64_t)trans[addr[2]];
203         v.d64[1] = (uint64_t)trans[addr[3]];
204         *indices2 = (xmm_t){v.d32[0], v.d32[1], v.d32[2], v.d32[3]};
205
206         return vec_sr(next_input,
207                 (vector unsigned int){CHAR_BIT, CHAR_BIT, CHAR_BIT, CHAR_BIT});
208 }
209
210 /*
211  * Execute trie traversal with 8 traversals in parallel
212  */
213 static inline int
214 search_altivec_8(const struct rte_acl_ctx *ctx, const uint8_t **data,
215         uint32_t *results, uint32_t total_packets, uint32_t categories)
216 {
217         int n;
218         struct acl_flow_data flows;
219         uint64_t index_array[MAX_SEARCHES_ALTIVEC8];
220         struct completion cmplt[MAX_SEARCHES_ALTIVEC8];
221         struct parms parms[MAX_SEARCHES_ALTIVEC8];
222         xmm_t input0, input1;
223
224         acl_set_flow(&flows, cmplt, RTE_DIM(cmplt), data, results,
225                 total_packets, categories, ctx->trans_table);
226
227         for (n = 0; n < MAX_SEARCHES_ALTIVEC8; n++) {
228                 cmplt[n].count = 0;
229                 index_array[n] = acl_start_next_trie(&flows, parms, n, ctx);
230         }
231
232          /* Check for any matches. */
233         acl_match_check_x4(0, ctx, parms, &flows, (uint64_t *)&index_array[0]);
234         acl_match_check_x4(4, ctx, parms, &flows, (uint64_t *)&index_array[4]);
235
236         while (flows.started > 0) {
237
238                 /* Gather 4 bytes of input data for each stream. */
239                 input0 = (xmm_t){GET_NEXT_4BYTES(parms, 0),
240                                 GET_NEXT_4BYTES(parms, 1),
241                                 GET_NEXT_4BYTES(parms, 2),
242                                 GET_NEXT_4BYTES(parms, 3)};
243
244                 input1 = (xmm_t){GET_NEXT_4BYTES(parms, 4),
245                                 GET_NEXT_4BYTES(parms, 5),
246                                 GET_NEXT_4BYTES(parms, 6),
247                                 GET_NEXT_4BYTES(parms, 7)};
248
249                  /* Process the 4 bytes of input on each stream. */
250
251                 input0 = transition4(input0, flows.trans,
252                         (xmm_t *)&index_array[0], (xmm_t *)&index_array[2]);
253                 input1 = transition4(input1, flows.trans,
254                         (xmm_t *)&index_array[4], (xmm_t *)&index_array[6]);
255
256                 input0 = transition4(input0, flows.trans,
257                         (xmm_t *)&index_array[0], (xmm_t *)&index_array[2]);
258                 input1 = transition4(input1, flows.trans,
259                         (xmm_t *)&index_array[4], (xmm_t *)&index_array[6]);
260
261                 input0 = transition4(input0, flows.trans,
262                         (xmm_t *)&index_array[0], (xmm_t *)&index_array[2]);
263                 input1 = transition4(input1, flows.trans,
264                         (xmm_t *)&index_array[4], (xmm_t *)&index_array[6]);
265
266                 input0 = transition4(input0, flows.trans,
267                         (xmm_t *)&index_array[0], (xmm_t *)&index_array[2]);
268                 input1 = transition4(input1, flows.trans,
269                         (xmm_t *)&index_array[4], (xmm_t *)&index_array[6]);
270
271                  /* Check for any matches. */
272                 acl_match_check_x4(0, ctx, parms, &flows,
273                         (uint64_t *)&index_array[0]);
274                 acl_match_check_x4(4, ctx, parms, &flows,
275                         (uint64_t *)&index_array[4]);
276         }
277
278         return 0;
279 }
280
281 /*
282  * Execute trie traversal with 4 traversals in parallel
283  */
284 static inline int
285 search_altivec_4(const struct rte_acl_ctx *ctx, const uint8_t **data,
286          uint32_t *results, int total_packets, uint32_t categories)
287 {
288         int n;
289         struct acl_flow_data flows;
290         uint64_t index_array[MAX_SEARCHES_ALTIVEC4];
291         struct completion cmplt[MAX_SEARCHES_ALTIVEC4];
292         struct parms parms[MAX_SEARCHES_ALTIVEC4];
293         xmm_t input;
294
295         acl_set_flow(&flows, cmplt, RTE_DIM(cmplt), data, results,
296                 total_packets, categories, ctx->trans_table);
297
298         for (n = 0; n < MAX_SEARCHES_ALTIVEC4; n++) {
299                 cmplt[n].count = 0;
300                 index_array[n] = acl_start_next_trie(&flows, parms, n, ctx);
301         }
302
303         /* Check for any matches. */
304         acl_match_check_x4(0, ctx, parms, &flows, index_array);
305
306         while (flows.started > 0) {
307
308                 /* Gather 4 bytes of input data for each stream. */
309                 input = (xmm_t){GET_NEXT_4BYTES(parms, 0),
310                                 GET_NEXT_4BYTES(parms, 1),
311                                 GET_NEXT_4BYTES(parms, 2),
312                                 GET_NEXT_4BYTES(parms, 3)};
313
314                 /* Process the 4 bytes of input on each stream. */
315                 input = transition4(input, flows.trans,
316                         (xmm_t *)&index_array[0], (xmm_t *)&index_array[2]);
317                 input = transition4(input, flows.trans,
318                         (xmm_t *)&index_array[0], (xmm_t *)&index_array[2]);
319                 input = transition4(input, flows.trans,
320                         (xmm_t *)&index_array[0], (xmm_t *)&index_array[2]);
321                 input = transition4(input, flows.trans,
322                         (xmm_t *)&index_array[0], (xmm_t *)&index_array[2]);
323
324                 /* Check for any matches. */
325                 acl_match_check_x4(0, ctx, parms, &flows, index_array);
326         }
327
328         return 0;
329 }