Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / include / xen / io / ring.h
1 /******************************************************************************
2  * ring.h
3  *
4  * Shared producer-consumer ring macros.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Tim Deegan and Andrew Warfield November 2004.
25  */
26
27 #ifndef __XEN_PUBLIC_IO_RING_H__
28 #define __XEN_PUBLIC_IO_RING_H__
29
30 FILE_LICENCE ( MIT );
31
32 #include "../xen-compat.h"
33
34 #if __XEN_INTERFACE_VERSION__ < 0x00030208
35 #define xen_mb()  mb()
36 #define xen_rmb() rmb()
37 #define xen_wmb() wmb()
38 #endif
39
40 typedef unsigned int RING_IDX;
41
42 /* Round a 32-bit unsigned constant down to the nearest power of two. */
43 #define __RD2(_x)  (((_x) & 0x00000002) ? 0x2                  : ((_x) & 0x1))
44 #define __RD4(_x)  (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2    : __RD2(_x))
45 #define __RD8(_x)  (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4    : __RD4(_x))
46 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8    : __RD8(_x))
47 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
48
49 /*
50  * Calculate size of a shared ring, given the total available space for the
51  * ring and indexes (_sz), and the name tag of the request/response structure.
52  * A ring contains as many entries as will fit, rounded down to the nearest
53  * power of two (so we can mask with (size-1) to loop around).
54  */
55 #define __CONST_RING_SIZE(_s, _sz) \
56     (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
57             sizeof(((struct _s##_sring *)0)->ring[0])))
58 /*
59  * The same for passing in an actual pointer instead of a name tag.
60  */
61 #define __RING_SIZE(_s, _sz) \
62     (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
63
64 /*
65  * Macros to make the correct C datatypes for a new kind of ring.
66  *
67  * To make a new ring datatype, you need to have two message structures,
68  * let's say request_t, and response_t already defined.
69  *
70  * In a header where you want the ring datatype declared, you then do:
71  *
72  *     DEFINE_RING_TYPES(mytag, request_t, response_t);
73  *
74  * These expand out to give you a set of types, as you can see below.
75  * The most important of these are:
76  *
77  *     mytag_sring_t      - The shared ring.
78  *     mytag_front_ring_t - The 'front' half of the ring.
79  *     mytag_back_ring_t  - The 'back' half of the ring.
80  *
81  * To initialize a ring in your code you need to know the location and size
82  * of the shared memory area (PAGE_SIZE, for instance). To initialise
83  * the front half:
84  *
85  *     mytag_front_ring_t front_ring;
86  *     SHARED_RING_INIT((mytag_sring_t *)shared_page);
87  *     FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
88  *
89  * Initializing the back follows similarly (note that only the front
90  * initializes the shared ring):
91  *
92  *     mytag_back_ring_t back_ring;
93  *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
94  */
95
96 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
97                                                                         \
98 /* Shared ring entry */                                                 \
99 union __name##_sring_entry {                                            \
100     __req_t req;                                                        \
101     __rsp_t rsp;                                                        \
102 };                                                                      \
103                                                                         \
104 /* Shared ring page */                                                  \
105 struct __name##_sring {                                                 \
106     RING_IDX req_prod, req_event;                                       \
107     RING_IDX rsp_prod, rsp_event;                                       \
108     union {                                                             \
109         struct {                                                        \
110             uint8_t smartpoll_active;                                   \
111         } netif;                                                        \
112         struct {                                                        \
113             uint8_t msg;                                                \
114         } tapif_user;                                                   \
115         uint8_t pvt_pad[4];                                             \
116     } private;                                                          \
117     uint8_t __pad[44];                                                  \
118     union __name##_sring_entry ring[1]; /* variable-length */           \
119 };                                                                      \
120                                                                         \
121 /* "Front" end's private variables */                                   \
122 struct __name##_front_ring {                                            \
123     RING_IDX req_prod_pvt;                                              \
124     RING_IDX rsp_cons;                                                  \
125     unsigned int nr_ents;                                               \
126     struct __name##_sring *sring;                                       \
127 };                                                                      \
128                                                                         \
129 /* "Back" end's private variables */                                    \
130 struct __name##_back_ring {                                             \
131     RING_IDX rsp_prod_pvt;                                              \
132     RING_IDX req_cons;                                                  \
133     unsigned int nr_ents;                                               \
134     struct __name##_sring *sring;                                       \
135 };                                                                      \
136                                                                         \
137 /* Syntactic sugar */                                                   \
138 typedef struct __name##_sring __name##_sring_t;                         \
139 typedef struct __name##_front_ring __name##_front_ring_t;               \
140 typedef struct __name##_back_ring __name##_back_ring_t
141
142 /*
143  * Macros for manipulating rings.
144  *
145  * FRONT_RING_whatever works on the "front end" of a ring: here
146  * requests are pushed on to the ring and responses taken off it.
147  *
148  * BACK_RING_whatever works on the "back end" of a ring: here
149  * requests are taken off the ring and responses put on.
150  *
151  * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
152  * This is OK in 1-for-1 request-response situations where the
153  * requestor (front end) never has more than RING_SIZE()-1
154  * outstanding requests.
155  */
156
157 /* Initialising empty rings */
158 #define SHARED_RING_INIT(_s) do {                                       \
159     (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
160     (_s)->req_event = (_s)->rsp_event = 1;                              \
161     (void)memset((_s)->private.pvt_pad, 0, sizeof((_s)->private.pvt_pad)); \
162     (void)memset((_s)->__pad, 0, sizeof((_s)->__pad));                  \
163 } while(0)
164
165 #define FRONT_RING_INIT(_r, _s, __size) do {                            \
166     (_r)->req_prod_pvt = 0;                                             \
167     (_r)->rsp_cons = 0;                                                 \
168     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
169     (_r)->sring = (_s);                                                 \
170 } while (0)
171
172 #define BACK_RING_INIT(_r, _s, __size) do {                             \
173     (_r)->rsp_prod_pvt = 0;                                             \
174     (_r)->req_cons = 0;                                                 \
175     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
176     (_r)->sring = (_s);                                                 \
177 } while (0)
178
179 /* How big is this ring? */
180 #define RING_SIZE(_r)                                                   \
181     ((_r)->nr_ents)
182
183 /* Number of free requests (for use on front side only). */
184 #define RING_FREE_REQUESTS(_r)                                          \
185     (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
186
187 /* Test if there is an empty slot available on the front ring.
188  * (This is only meaningful from the front. )
189  */
190 #define RING_FULL(_r)                                                   \
191     (RING_FREE_REQUESTS(_r) == 0)
192
193 /* Test if there are outstanding messages to be processed on a ring. */
194 #define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
195     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
196
197 #ifdef __GNUC__
198 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
199     unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
200     unsigned int rsp = RING_SIZE(_r) -                                  \
201         ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
202     req < rsp ? req : rsp;                                              \
203 })
204 #else
205 /* Same as above, but without the nice GCC ({ ... }) syntax. */
206 #define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
207     ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
208       (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
209      ((_r)->sring->req_prod - (_r)->req_cons) :                         \
210      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
211 #endif
212
213 /* Direct access to individual ring elements, by index. */
214 #define RING_GET_REQUEST(_r, _idx)                                      \
215     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
216
217 #define RING_GET_RESPONSE(_r, _idx)                                     \
218     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
219
220 /* Loop termination condition: Would the specified index overflow the ring? */
221 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
222     (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
223
224 /* Ill-behaved frontend determination: Can there be this many requests? */
225 #define RING_REQUEST_PROD_OVERFLOW(_r, _prod)                           \
226     (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
227
228 #define RING_PUSH_REQUESTS(_r) do {                                     \
229     xen_wmb(); /* back sees requests /before/ updated producer index */ \
230     (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
231 } while (0)
232
233 #define RING_PUSH_RESPONSES(_r) do {                                    \
234     xen_wmb(); /* front sees resps /before/ updated producer index */   \
235     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
236 } while (0)
237
238 /*
239  * Notification hold-off (req_event and rsp_event):
240  *
241  * When queueing requests or responses on a shared ring, it may not always be
242  * necessary to notify the remote end. For example, if requests are in flight
243  * in a backend, the front may be able to queue further requests without
244  * notifying the back (if the back checks for new requests when it queues
245  * responses).
246  *
247  * When enqueuing requests or responses:
248  *
249  *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
250  *  is a boolean return value. True indicates that the receiver requires an
251  *  asynchronous notification.
252  *
253  * After dequeuing requests or responses (before sleeping the connection):
254  *
255  *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
256  *  The second argument is a boolean return value. True indicates that there
257  *  are pending messages on the ring (i.e., the connection should not be put
258  *  to sleep).
259  *
260  *  These macros will set the req_event/rsp_event field to trigger a
261  *  notification on the very next message that is enqueued. If you want to
262  *  create batches of work (i.e., only receive a notification after several
263  *  messages have been enqueued) then you will need to create a customised
264  *  version of the FINAL_CHECK macro in your own code, which sets the event
265  *  field appropriately.
266  */
267
268 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
269     RING_IDX __old = (_r)->sring->req_prod;                             \
270     RING_IDX __new = (_r)->req_prod_pvt;                                \
271     xen_wmb(); /* back sees requests /before/ updated producer index */ \
272     (_r)->sring->req_prod = __new;                                      \
273     xen_mb(); /* back sees new requests /before/ we check req_event */  \
274     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
275                  (RING_IDX)(__new - __old));                            \
276 } while (0)
277
278 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
279     RING_IDX __old = (_r)->sring->rsp_prod;                             \
280     RING_IDX __new = (_r)->rsp_prod_pvt;                                \
281     xen_wmb(); /* front sees resps /before/ updated producer index */   \
282     (_r)->sring->rsp_prod = __new;                                      \
283     xen_mb(); /* front sees new resps /before/ we check rsp_event */    \
284     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
285                  (RING_IDX)(__new - __old));                            \
286 } while (0)
287
288 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
289     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
290     if (_work_to_do) break;                                             \
291     (_r)->sring->req_event = (_r)->req_cons + 1;                        \
292     xen_mb();                                                           \
293     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
294 } while (0)
295
296 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
297     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
298     if (_work_to_do) break;                                             \
299     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
300     xen_mb();                                                           \
301     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
302 } while (0)
303
304 #endif /* __XEN_PUBLIC_IO_RING_H__ */
305
306 /*
307  * Local variables:
308  * mode: C
309  * c-file-style: "BSD"
310  * c-basic-offset: 4
311  * tab-width: 4
312  * indent-tabs-mode: nil
313  * End:
314  */