]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/xen/interface/io/ring.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / xen / interface / 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 #include "../xen-compat.h"
31
32 #if __XEN_INTERFACE_VERSION__ < 0x00030208
33 #define xen_mb()  mb()
34 #define xen_rmb() rmb()
35 #define xen_wmb() wmb()
36 #endif
37
38 typedef unsigned int RING_IDX;
39
40 /* Round a 32-bit unsigned constant down to the nearest power of two. */
41 #define __RD2(_x)  (((_x) & 0x00000002) ? 0x2                  : ((_x) & 0x1))
42 #define __RD4(_x)  (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2    : __RD2(_x))
43 #define __RD8(_x)  (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4    : __RD4(_x))
44 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8    : __RD8(_x))
45 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
46
47 /*
48  * The amount of space reserved in the shared ring for accounting information.
49  */
50 #define __RING_HEADER_SIZE(_s) \
51     ((intptr_t)(_s)->ring - (intptr_t)(_s))
52
53 /*
54  * Calculate size of a shared ring, given the total available space for the
55  * ring and indexes (_sz), and the name tag of the request/response structure.
56  * A ring contains as many entries as will fit, rounded down to the nearest 
57  * power of two (so we can mask with (size-1) to loop around).
58  */
59 #define __RING_SIZE(_s, _sz) \
60     (__RD32(((_sz) - __RING_HEADER_SIZE(_s)) / sizeof((_s)->ring[0])))
61
62 /*
63  * The number of pages needed to support a given number of request/reponse
64  * entries.  The entry count is rounded down to the nearest power of two
65  * as required by the ring macros.
66  */
67 #define __RING_PAGES(_s, _entries)              \
68     ((__RING_HEADER_SIZE(_s)                    \
69    + (__RD32(_entries) * sizeof((_s)->ring[0])) \
70    + PAGE_SIZE - 1) / PAGE_SIZE)
71
72 /*
73  * Macros to make the correct C datatypes for a new kind of ring.
74  * 
75  * To make a new ring datatype, you need to have two message structures,
76  * let's say request_t, and response_t already defined.
77  *
78  * In a header where you want the ring datatype declared, you then do:
79  *
80  *     DEFINE_RING_TYPES(mytag, request_t, response_t);
81  *
82  * These expand out to give you a set of types, as you can see below.
83  * The most important of these are:
84  * 
85  *     mytag_sring_t      - The shared ring.
86  *     mytag_front_ring_t - The 'front' half of the ring.
87  *     mytag_back_ring_t  - The 'back' half of the ring.
88  *
89  * To initialize a ring in your code you need to know the location and size
90  * of the shared memory area (PAGE_SIZE, for instance). To initialise
91  * the front half:
92  *
93  *     mytag_front_ring_t front_ring;
94  *     SHARED_RING_INIT((mytag_sring_t *)shared_page);
95  *     FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
96  *
97  * Initializing the back follows similarly (note that only the front
98  * initializes the shared ring):
99  *
100  *     mytag_back_ring_t back_ring;
101  *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
102  */
103
104 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
105                                                                         \
106 /* Shared ring entry */                                                 \
107 union __name##_sring_entry {                                            \
108     __req_t req;                                                        \
109     __rsp_t rsp;                                                        \
110 };                                                                      \
111                                                                         \
112 /* Shared ring page */                                                  \
113 struct __name##_sring {                                                 \
114     RING_IDX req_prod, req_event;                                       \
115     RING_IDX rsp_prod, rsp_event;                                       \
116     uint8_t  pad[48];                                                   \
117     union __name##_sring_entry ring[1]; /* variable-length */           \
118 };                                                                      \
119                                                                         \
120 /* "Front" end's private variables */                                   \
121 struct __name##_front_ring {                                            \
122     RING_IDX req_prod_pvt;                                              \
123     RING_IDX rsp_cons;                                                  \
124     unsigned int nr_ents;                                               \
125     struct __name##_sring *sring;                                       \
126 };                                                                      \
127                                                                         \
128 /* "Back" end's private variables */                                    \
129 struct __name##_back_ring {                                             \
130     RING_IDX rsp_prod_pvt;                                              \
131     RING_IDX req_cons;                                                  \
132     unsigned int nr_ents;                                               \
133     struct __name##_sring *sring;                                       \
134 };                                                                      \
135                                                                         \
136 /* Syntactic sugar */                                                   \
137 typedef struct __name##_sring __name##_sring_t;                         \
138 typedef struct __name##_front_ring __name##_front_ring_t;               \
139 typedef struct __name##_back_ring __name##_back_ring_t
140
141 /*
142  * Macros for manipulating rings.
143  * 
144  * FRONT_RING_whatever works on the "front end" of a ring: here 
145  * requests are pushed on to the ring and responses taken off it.
146  * 
147  * BACK_RING_whatever works on the "back end" of a ring: here 
148  * requests are taken off the ring and responses put on.
149  * 
150  * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL. 
151  * This is OK in 1-for-1 request-response situations where the 
152  * requestor (front end) never has more than RING_SIZE()-1
153  * outstanding requests.
154  */
155
156 /* Initialising empty rings */
157 #define SHARED_RING_INIT(_s) do {                                       \
158     (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
159     (_s)->req_event = (_s)->rsp_event = 1;                              \
160     (void)memset((_s)->pad, 0, sizeof((_s)->pad));                      \
161 } while(0)
162
163 #define FRONT_RING_INIT(_r, _s, __size) do {                            \
164     (_r)->req_prod_pvt = 0;                                             \
165     (_r)->rsp_cons = 0;                                                 \
166     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
167     (_r)->sring = (_s);                                                 \
168 } while (0)
169
170 #define BACK_RING_INIT(_r, _s, __size) do {                             \
171     (_r)->rsp_prod_pvt = 0;                                             \
172     (_r)->req_cons = 0;                                                 \
173     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
174     (_r)->sring = (_s);                                                 \
175 } while (0)
176
177 /* Initialize to existing shared indexes -- for recovery */
178 #define FRONT_RING_ATTACH(_r, _s, __size) do {                          \
179     (_r)->sring = (_s);                                                 \
180     (_r)->req_prod_pvt = (_s)->req_prod;                                \
181     (_r)->rsp_cons = (_s)->rsp_prod;                                    \
182     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
183 } while (0)
184
185 #define BACK_RING_ATTACH(_r, _s, __size) do {                           \
186     (_r)->sring = (_s);                                                 \
187     (_r)->rsp_prod_pvt = (_s)->rsp_prod;                                \
188     (_r)->req_cons = (_s)->req_prod;                                    \
189     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
190 } while (0)
191
192 /* How big is this ring? */
193 #define RING_SIZE(_r)                                                   \
194     ((_r)->nr_ents)
195
196 /* Number of free requests (for use on front side only). */
197 #define RING_FREE_REQUESTS(_r)                                          \
198     (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
199
200 /* Test if there is an empty slot available on the front ring.
201  * (This is only meaningful from the front. )
202  */
203 #define RING_FULL(_r)                                                   \
204     (RING_FREE_REQUESTS(_r) == 0)
205
206 /* Test if there are outstanding messages to be processed on a ring. */
207 #define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
208     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
209
210 #ifdef __GNUC__
211 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
212     unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
213     unsigned int rsp = RING_SIZE(_r) -                                  \
214         ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
215     req < rsp ? req : rsp;                                              \
216 })
217 #else
218 /* Same as above, but without the nice GCC ({ ... }) syntax. */
219 #define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
220     ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
221       (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
222      ((_r)->sring->req_prod - (_r)->req_cons) :                         \
223      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
224 #endif
225
226 /* Direct access to individual ring elements, by index. */
227 #define RING_GET_REQUEST(_r, _idx)                                      \
228     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
229
230 #define RING_GET_RESPONSE(_r, _idx)                                     \
231     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
232
233 /* Loop termination condition: Would the specified index overflow the ring? */
234 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
235     (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
236
237 #define RING_PUSH_REQUESTS(_r) do {                                     \
238     xen_wmb(); /* back sees requests /before/ updated producer index */ \
239     (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
240 } while (0)
241
242 #define RING_PUSH_RESPONSES(_r) do {                                    \
243     xen_wmb(); /* front sees resps /before/ updated producer index */   \
244     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
245 } while (0)
246
247 /*
248  * Notification hold-off (req_event and rsp_event):
249  * 
250  * When queueing requests or responses on a shared ring, it may not always be
251  * necessary to notify the remote end. For example, if requests are in flight
252  * in a backend, the front may be able to queue further requests without
253  * notifying the back (if the back checks for new requests when it queues
254  * responses).
255  * 
256  * When enqueuing requests or responses:
257  * 
258  *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
259  *  is a boolean return value. True indicates that the receiver requires an
260  *  asynchronous notification.
261  * 
262  * After dequeuing requests or responses (before sleeping the connection):
263  * 
264  *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
265  *  The second argument is a boolean return value. True indicates that there
266  *  are pending messages on the ring (i.e., the connection should not be put
267  *  to sleep).
268  * 
269  *  These macros will set the req_event/rsp_event field to trigger a
270  *  notification on the very next message that is enqueued. If you want to
271  *  create batches of work (i.e., only receive a notification after several
272  *  messages have been enqueued) then you will need to create a customised
273  *  version of the FINAL_CHECK macro in your own code, which sets the event
274  *  field appropriately.
275  */
276
277 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
278     RING_IDX __old = (_r)->sring->req_prod;                             \
279     RING_IDX __new = (_r)->req_prod_pvt;                                \
280     xen_wmb(); /* back sees requests /before/ updated producer index */ \
281     (_r)->sring->req_prod = __new;                                      \
282     xen_mb(); /* back sees new requests /before/ we check req_event */  \
283     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
284                  (RING_IDX)(__new - __old));                            \
285 } while (0)
286
287 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
288     RING_IDX __old = (_r)->sring->rsp_prod;                             \
289     RING_IDX __new = (_r)->rsp_prod_pvt;                                \
290     xen_wmb(); /* front sees resps /before/ updated producer index */   \
291     (_r)->sring->rsp_prod = __new;                                      \
292     xen_mb(); /* front sees new resps /before/ we check rsp_event */    \
293     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
294                  (RING_IDX)(__new - __old));                            \
295 } while (0)
296
297 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
298     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
299     if (_work_to_do) break;                                             \
300     (_r)->sring->req_event = (_r)->req_cons + 1;                        \
301     xen_mb();                                                           \
302     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
303 } while (0)
304
305 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
306     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
307     if (_work_to_do) break;                                             \
308     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
309     xen_mb();                                                           \
310     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
311 } while (0)
312
313 #endif /* __XEN_PUBLIC_IO_RING_H__ */
314
315 /*
316  * Local variables:
317  * mode: C
318  * c-set-style: "BSD"
319  * c-basic-offset: 4
320  * tab-width: 4
321  * indent-tabs-mode: nil
322  * End:
323  */