net/bnx2x: get cache line size from build configuration
[dpdk.git] / drivers / net / virtio / virtio_rxtx_simple_sse.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   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 Intel Corporation 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
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #include <tmmintrin.h>
41
42 #include <rte_byteorder.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_cycles.h>
45 #include <rte_ether.h>
46 #include <rte_ethdev.h>
47 #include <rte_errno.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_mempool.h>
51 #include <rte_malloc.h>
52 #include <rte_mbuf.h>
53 #include <rte_prefetch.h>
54 #include <rte_string_fns.h>
55
56 #include "virtio_rxtx_simple.h"
57
58 #define RTE_VIRTIO_VPMD_RX_BURST 32
59 #define RTE_VIRTIO_DESC_PER_LOOP 8
60 #define RTE_VIRTIO_VPMD_RX_REARM_THRESH RTE_VIRTIO_VPMD_RX_BURST
61
62 /* virtio vPMD receive routine, only accept(nb_pkts >= RTE_VIRTIO_DESC_PER_LOOP)
63  *
64  * This routine is for non-mergeable RX, one desc for each guest buffer.
65  * This routine is based on the RX ring layout optimization. Each entry in the
66  * avail ring points to the desc with the same index in the desc ring and this
67  * will never be changed in the driver.
68  *
69  * - nb_pkts < RTE_VIRTIO_DESC_PER_LOOP, just return no packet
70  */
71 uint16_t
72 virtio_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
73         uint16_t nb_pkts)
74 {
75         struct virtnet_rx *rxvq = rx_queue;
76         struct virtqueue *vq = rxvq->vq;
77         uint16_t nb_used;
78         uint16_t desc_idx;
79         struct vring_used_elem *rused;
80         struct rte_mbuf **sw_ring;
81         struct rte_mbuf **sw_ring_end;
82         uint16_t nb_pkts_received;
83         __m128i shuf_msk1, shuf_msk2, len_adjust;
84
85         shuf_msk1 = _mm_set_epi8(
86                 0xFF, 0xFF, 0xFF, 0xFF,
87                 0xFF, 0xFF,             /* vlan tci */
88                 5, 4,                   /* dat len */
89                 0xFF, 0xFF, 5, 4,       /* pkt len */
90                 0xFF, 0xFF, 0xFF, 0xFF  /* packet type */
91
92         );
93
94         shuf_msk2 = _mm_set_epi8(
95                 0xFF, 0xFF, 0xFF, 0xFF,
96                 0xFF, 0xFF,             /* vlan tci */
97                 13, 12,                 /* dat len */
98                 0xFF, 0xFF, 13, 12,     /* pkt len */
99                 0xFF, 0xFF, 0xFF, 0xFF  /* packet type */
100         );
101
102         /* Subtract the header length.
103         *  In which case do we need the header length in used->len ?
104         */
105         len_adjust = _mm_set_epi16(
106                 0, 0,
107                 0,
108                 (uint16_t)-vq->hw->vtnet_hdr_size,
109                 0, (uint16_t)-vq->hw->vtnet_hdr_size,
110                 0, 0);
111
112         if (unlikely(nb_pkts < RTE_VIRTIO_DESC_PER_LOOP))
113                 return 0;
114
115         nb_used = VIRTQUEUE_NUSED(vq);
116
117         rte_compiler_barrier();
118
119         if (unlikely(nb_used == 0))
120                 return 0;
121
122         nb_pkts = RTE_ALIGN_FLOOR(nb_pkts, RTE_VIRTIO_DESC_PER_LOOP);
123         nb_used = RTE_MIN(nb_used, nb_pkts);
124
125         desc_idx = (uint16_t)(vq->vq_used_cons_idx & (vq->vq_nentries - 1));
126         rused = &vq->vq_ring.used->ring[desc_idx];
127         sw_ring  = &vq->sw_ring[desc_idx];
128         sw_ring_end = &vq->sw_ring[vq->vq_nentries];
129
130         rte_prefetch0(rused);
131
132         if (vq->vq_free_cnt >= RTE_VIRTIO_VPMD_RX_REARM_THRESH) {
133                 virtio_rxq_rearm_vec(rxvq);
134                 if (unlikely(virtqueue_kick_prepare(vq)))
135                         virtqueue_notify(vq);
136         }
137
138         for (nb_pkts_received = 0;
139                 nb_pkts_received < nb_used;) {
140                 __m128i desc[RTE_VIRTIO_DESC_PER_LOOP / 2];
141                 __m128i mbp[RTE_VIRTIO_DESC_PER_LOOP / 2];
142                 __m128i pkt_mb[RTE_VIRTIO_DESC_PER_LOOP];
143
144                 mbp[0] = _mm_loadu_si128((__m128i *)(sw_ring + 0));
145                 desc[0] = _mm_loadu_si128((__m128i *)(rused + 0));
146                 _mm_storeu_si128((__m128i *)&rx_pkts[0], mbp[0]);
147
148                 mbp[1] = _mm_loadu_si128((__m128i *)(sw_ring + 2));
149                 desc[1] = _mm_loadu_si128((__m128i *)(rused + 2));
150                 _mm_storeu_si128((__m128i *)&rx_pkts[2], mbp[1]);
151
152                 mbp[2] = _mm_loadu_si128((__m128i *)(sw_ring + 4));
153                 desc[2] = _mm_loadu_si128((__m128i *)(rused + 4));
154                 _mm_storeu_si128((__m128i *)&rx_pkts[4], mbp[2]);
155
156                 mbp[3] = _mm_loadu_si128((__m128i *)(sw_ring + 6));
157                 desc[3] = _mm_loadu_si128((__m128i *)(rused + 6));
158                 _mm_storeu_si128((__m128i *)&rx_pkts[6], mbp[3]);
159
160                 pkt_mb[1] = _mm_shuffle_epi8(desc[0], shuf_msk2);
161                 pkt_mb[0] = _mm_shuffle_epi8(desc[0], shuf_msk1);
162                 pkt_mb[1] = _mm_add_epi16(pkt_mb[1], len_adjust);
163                 pkt_mb[0] = _mm_add_epi16(pkt_mb[0], len_adjust);
164                 _mm_storeu_si128((void *)&rx_pkts[1]->rx_descriptor_fields1,
165                         pkt_mb[1]);
166                 _mm_storeu_si128((void *)&rx_pkts[0]->rx_descriptor_fields1,
167                         pkt_mb[0]);
168
169                 pkt_mb[3] = _mm_shuffle_epi8(desc[1], shuf_msk2);
170                 pkt_mb[2] = _mm_shuffle_epi8(desc[1], shuf_msk1);
171                 pkt_mb[3] = _mm_add_epi16(pkt_mb[3], len_adjust);
172                 pkt_mb[2] = _mm_add_epi16(pkt_mb[2], len_adjust);
173                 _mm_storeu_si128((void *)&rx_pkts[3]->rx_descriptor_fields1,
174                         pkt_mb[3]);
175                 _mm_storeu_si128((void *)&rx_pkts[2]->rx_descriptor_fields1,
176                         pkt_mb[2]);
177
178                 pkt_mb[5] = _mm_shuffle_epi8(desc[2], shuf_msk2);
179                 pkt_mb[4] = _mm_shuffle_epi8(desc[2], shuf_msk1);
180                 pkt_mb[5] = _mm_add_epi16(pkt_mb[5], len_adjust);
181                 pkt_mb[4] = _mm_add_epi16(pkt_mb[4], len_adjust);
182                 _mm_storeu_si128((void *)&rx_pkts[5]->rx_descriptor_fields1,
183                         pkt_mb[5]);
184                 _mm_storeu_si128((void *)&rx_pkts[4]->rx_descriptor_fields1,
185                         pkt_mb[4]);
186
187                 pkt_mb[7] = _mm_shuffle_epi8(desc[3], shuf_msk2);
188                 pkt_mb[6] = _mm_shuffle_epi8(desc[3], shuf_msk1);
189                 pkt_mb[7] = _mm_add_epi16(pkt_mb[7], len_adjust);
190                 pkt_mb[6] = _mm_add_epi16(pkt_mb[6], len_adjust);
191                 _mm_storeu_si128((void *)&rx_pkts[7]->rx_descriptor_fields1,
192                         pkt_mb[7]);
193                 _mm_storeu_si128((void *)&rx_pkts[6]->rx_descriptor_fields1,
194                         pkt_mb[6]);
195
196                 if (unlikely(nb_used <= RTE_VIRTIO_DESC_PER_LOOP)) {
197                         if (sw_ring + nb_used <= sw_ring_end)
198                                 nb_pkts_received += nb_used;
199                         else
200                                 nb_pkts_received += sw_ring_end - sw_ring;
201                         break;
202                 } else {
203                         if (unlikely(sw_ring + RTE_VIRTIO_DESC_PER_LOOP >=
204                                 sw_ring_end)) {
205                                 nb_pkts_received += sw_ring_end - sw_ring;
206                                 break;
207                         } else {
208                                 nb_pkts_received += RTE_VIRTIO_DESC_PER_LOOP;
209
210                                 rx_pkts += RTE_VIRTIO_DESC_PER_LOOP;
211                                 sw_ring += RTE_VIRTIO_DESC_PER_LOOP;
212                                 rused   += RTE_VIRTIO_DESC_PER_LOOP;
213                                 nb_used -= RTE_VIRTIO_DESC_PER_LOOP;
214                         }
215                 }
216         }
217
218         vq->vq_used_cons_idx += nb_pkts_received;
219         vq->vq_free_cnt += nb_pkts_received;
220         rxvq->stats.packets += nb_pkts_received;
221         return nb_pkts_received;
222 }