xref: /titanic_51/usr/src/uts/common/xen/public/io/ring.h (revision 876de206688d9fe008ad80c116a23a56701579d1)
1843e1988Sjohnlev /******************************************************************************
2843e1988Sjohnlev  * ring.h
3843e1988Sjohnlev  *
4843e1988Sjohnlev  * Shared producer-consumer ring macros.
5843e1988Sjohnlev  *
6843e1988Sjohnlev  * Permission is hereby granted, free of charge, to any person obtaining a copy
7843e1988Sjohnlev  * of this software and associated documentation files (the "Software"), to
8843e1988Sjohnlev  * deal in the Software without restriction, including without limitation the
9843e1988Sjohnlev  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10843e1988Sjohnlev  * sell copies of the Software, and to permit persons to whom the Software is
11843e1988Sjohnlev  * furnished to do so, subject to the following conditions:
12843e1988Sjohnlev  *
13843e1988Sjohnlev  * The above copyright notice and this permission notice shall be included in
14843e1988Sjohnlev  * all copies or substantial portions of the Software.
15843e1988Sjohnlev  *
16843e1988Sjohnlev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17843e1988Sjohnlev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18843e1988Sjohnlev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19843e1988Sjohnlev  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20843e1988Sjohnlev  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21843e1988Sjohnlev  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22843e1988Sjohnlev  * DEALINGS IN THE SOFTWARE.
23843e1988Sjohnlev  *
24843e1988Sjohnlev  * Tim Deegan and Andrew Warfield November 2004.
25843e1988Sjohnlev  */
26843e1988Sjohnlev 
27843e1988Sjohnlev #ifndef __XEN_PUBLIC_IO_RING_H__
28843e1988Sjohnlev #define __XEN_PUBLIC_IO_RING_H__
29843e1988Sjohnlev 
30349b53ddSStuart Maybee #include "../xen-compat.h"
31349b53ddSStuart Maybee 
32349b53ddSStuart Maybee #if __XEN_INTERFACE_VERSION__ < 0x00030208
33349b53ddSStuart Maybee #define xen_mb()  mb()
34349b53ddSStuart Maybee #define xen_rmb() rmb()
35349b53ddSStuart Maybee #define xen_wmb() wmb()
36349b53ddSStuart Maybee #endif
37349b53ddSStuart Maybee 
38843e1988Sjohnlev typedef unsigned int RING_IDX;
39843e1988Sjohnlev 
40843e1988Sjohnlev /* Round a 32-bit unsigned constant down to the nearest power of two. */
41843e1988Sjohnlev #define __RD2(_x)  (((_x) & 0x00000002) ? 0x2                  : ((_x) & 0x1))
42843e1988Sjohnlev #define __RD4(_x)  (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2    : __RD2(_x))
43843e1988Sjohnlev #define __RD8(_x)  (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4    : __RD4(_x))
44843e1988Sjohnlev #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8    : __RD8(_x))
45843e1988Sjohnlev #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
46843e1988Sjohnlev 
47843e1988Sjohnlev /*
48843e1988Sjohnlev  * Calculate size of a shared ring, given the total available space for the
49843e1988Sjohnlev  * ring and indexes (_sz), and the name tag of the request/response structure.
50843e1988Sjohnlev  * A ring contains as many entries as will fit, rounded down to the nearest
51843e1988Sjohnlev  * power of two (so we can mask with (size-1) to loop around).
52*876de206SRichard Lowe  */
53*876de206SRichard Lowe #define __CONST_RING_SIZE(_s, _sz) \
54*876de206SRichard Lowe     (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
55*876de206SRichard Lowe        sizeof(((struct _s##_sring *)0)->ring[0])))
56*876de206SRichard Lowe /*
57*876de206SRichard Lowe  * The same for passing in an actual pointer instead of a name tag.
58843e1988Sjohnlev  */
59843e1988Sjohnlev #define __RING_SIZE(_s, _sz) \
60843e1988Sjohnlev     (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
61843e1988Sjohnlev 
62843e1988Sjohnlev /*
63843e1988Sjohnlev  * Macros to make the correct C datatypes for a new kind of ring.
64843e1988Sjohnlev  *
65843e1988Sjohnlev  * To make a new ring datatype, you need to have two message structures,
66843e1988Sjohnlev  * let's say request_t, and response_t already defined.
67843e1988Sjohnlev  *
68843e1988Sjohnlev  * In a header where you want the ring datatype declared, you then do:
69843e1988Sjohnlev  *
70843e1988Sjohnlev  *     DEFINE_RING_TYPES(mytag, request_t, response_t);
71843e1988Sjohnlev  *
72843e1988Sjohnlev  * These expand out to give you a set of types, as you can see below.
73843e1988Sjohnlev  * The most important of these are:
74843e1988Sjohnlev  *
75843e1988Sjohnlev  *     mytag_sring_t      - The shared ring.
76843e1988Sjohnlev  *     mytag_front_ring_t - The 'front' half of the ring.
77843e1988Sjohnlev  *     mytag_back_ring_t  - The 'back' half of the ring.
78843e1988Sjohnlev  *
79843e1988Sjohnlev  * To initialize a ring in your code you need to know the location and size
80843e1988Sjohnlev  * of the shared memory area (PAGE_SIZE, for instance). To initialise
81843e1988Sjohnlev  * the front half:
82843e1988Sjohnlev  *
83843e1988Sjohnlev  *     mytag_front_ring_t front_ring;
84843e1988Sjohnlev  *     SHARED_RING_INIT((mytag_sring_t *)shared_page);
85843e1988Sjohnlev  *     FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
86843e1988Sjohnlev  *
87843e1988Sjohnlev  * Initializing the back follows similarly (note that only the front
88843e1988Sjohnlev  * initializes the shared ring):
89843e1988Sjohnlev  *
90843e1988Sjohnlev  *     mytag_back_ring_t back_ring;
91843e1988Sjohnlev  *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
92843e1988Sjohnlev  */
93843e1988Sjohnlev 
94843e1988Sjohnlev #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
95843e1988Sjohnlev                                                                         \
96843e1988Sjohnlev /* Shared ring entry */                                                 \
97843e1988Sjohnlev union __name##_sring_entry {                                            \
98843e1988Sjohnlev     __req_t req;                                                        \
99843e1988Sjohnlev     __rsp_t rsp;                                                        \
100843e1988Sjohnlev };                                                                      \
101843e1988Sjohnlev                                                                         \
102843e1988Sjohnlev /* Shared ring page */                                                  \
103843e1988Sjohnlev struct __name##_sring {                                                 \
104843e1988Sjohnlev     RING_IDX req_prod, req_event;                                       \
105843e1988Sjohnlev     RING_IDX rsp_prod, rsp_event;                                       \
106843e1988Sjohnlev     uint8_t  pad[48];                                                   \
107843e1988Sjohnlev     union __name##_sring_entry ring[1]; /* variable-length */           \
108843e1988Sjohnlev };                                                                      \
109843e1988Sjohnlev                                                                         \
110843e1988Sjohnlev /* "Front" end's private variables */                                   \
111843e1988Sjohnlev struct __name##_front_ring {                                            \
112843e1988Sjohnlev     RING_IDX req_prod_pvt;                                              \
113843e1988Sjohnlev     RING_IDX rsp_cons;                                                  \
114843e1988Sjohnlev     unsigned int nr_ents;                                               \
115843e1988Sjohnlev     struct __name##_sring *sring;                                       \
116843e1988Sjohnlev };                                                                      \
117843e1988Sjohnlev                                                                         \
118843e1988Sjohnlev /* "Back" end's private variables */                                    \
119843e1988Sjohnlev struct __name##_back_ring {                                             \
120843e1988Sjohnlev     RING_IDX rsp_prod_pvt;                                              \
121843e1988Sjohnlev     RING_IDX req_cons;                                                  \
122843e1988Sjohnlev     unsigned int nr_ents;                                               \
123843e1988Sjohnlev     struct __name##_sring *sring;                                       \
124843e1988Sjohnlev };                                                                      \
125843e1988Sjohnlev                                                                         \
126843e1988Sjohnlev /* Syntactic sugar */                                                   \
127843e1988Sjohnlev typedef struct __name##_sring __name##_sring_t;                         \
128843e1988Sjohnlev typedef struct __name##_front_ring __name##_front_ring_t;               \
129843e1988Sjohnlev typedef struct __name##_back_ring __name##_back_ring_t
130843e1988Sjohnlev 
131843e1988Sjohnlev /*
132843e1988Sjohnlev  * Macros for manipulating rings.
133843e1988Sjohnlev  *
134843e1988Sjohnlev  * FRONT_RING_whatever works on the "front end" of a ring: here
135843e1988Sjohnlev  * requests are pushed on to the ring and responses taken off it.
136843e1988Sjohnlev  *
137843e1988Sjohnlev  * BACK_RING_whatever works on the "back end" of a ring: here
138843e1988Sjohnlev  * requests are taken off the ring and responses put on.
139843e1988Sjohnlev  *
140843e1988Sjohnlev  * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
141843e1988Sjohnlev  * This is OK in 1-for-1 request-response situations where the
142843e1988Sjohnlev  * requestor (front end) never has more than RING_SIZE()-1
143843e1988Sjohnlev  * outstanding requests.
144843e1988Sjohnlev  */
145843e1988Sjohnlev 
146843e1988Sjohnlev /* Initialising empty rings */
147843e1988Sjohnlev #define SHARED_RING_INIT(_s) do {                                       \
148843e1988Sjohnlev     (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
149843e1988Sjohnlev     (_s)->req_event = (_s)->rsp_event = 1;                              \
150843e1988Sjohnlev     (void)memset((_s)->pad, 0, sizeof((_s)->pad));                      \
151843e1988Sjohnlev } while(0)
152843e1988Sjohnlev 
153843e1988Sjohnlev #define FRONT_RING_INIT(_r, _s, __size) do {                            \
154843e1988Sjohnlev     (_r)->req_prod_pvt = 0;                                             \
155843e1988Sjohnlev     (_r)->rsp_cons = 0;                                                 \
156843e1988Sjohnlev     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
157843e1988Sjohnlev     (_r)->sring = (_s);                                                 \
158843e1988Sjohnlev } while (0)
159843e1988Sjohnlev 
160843e1988Sjohnlev #define BACK_RING_INIT(_r, _s, __size) do {                             \
161843e1988Sjohnlev     (_r)->rsp_prod_pvt = 0;                                             \
162843e1988Sjohnlev     (_r)->req_cons = 0;                                                 \
163843e1988Sjohnlev     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
164843e1988Sjohnlev     (_r)->sring = (_s);                                                 \
165843e1988Sjohnlev } while (0)
166843e1988Sjohnlev 
167843e1988Sjohnlev /* Initialize to existing shared indexes -- for recovery */
168843e1988Sjohnlev #define FRONT_RING_ATTACH(_r, _s, __size) do {                          \
169843e1988Sjohnlev     (_r)->sring = (_s);                                                 \
170843e1988Sjohnlev     (_r)->req_prod_pvt = (_s)->req_prod;                                \
171843e1988Sjohnlev     (_r)->rsp_cons = (_s)->rsp_prod;                                    \
172843e1988Sjohnlev     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
173843e1988Sjohnlev } while (0)
174843e1988Sjohnlev 
175843e1988Sjohnlev #define BACK_RING_ATTACH(_r, _s, __size) do {                           \
176843e1988Sjohnlev     (_r)->sring = (_s);                                                 \
177843e1988Sjohnlev     (_r)->rsp_prod_pvt = (_s)->rsp_prod;                                \
178843e1988Sjohnlev     (_r)->req_cons = (_s)->req_prod;                                    \
179843e1988Sjohnlev     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
180843e1988Sjohnlev } while (0)
181843e1988Sjohnlev 
182843e1988Sjohnlev /* How big is this ring? */
183843e1988Sjohnlev #define RING_SIZE(_r)                                                   \
184843e1988Sjohnlev     ((_r)->nr_ents)
185843e1988Sjohnlev 
186843e1988Sjohnlev /* Number of free requests (for use on front side only). */
187843e1988Sjohnlev #define RING_FREE_REQUESTS(_r)                                          \
188843e1988Sjohnlev     (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
189843e1988Sjohnlev 
190843e1988Sjohnlev /* Test if there is an empty slot available on the front ring.
191843e1988Sjohnlev  * (This is only meaningful from the front. )
192843e1988Sjohnlev  */
193843e1988Sjohnlev #define RING_FULL(_r)                                                   \
194843e1988Sjohnlev     (RING_FREE_REQUESTS(_r) == 0)
195843e1988Sjohnlev 
196843e1988Sjohnlev /* Test if there are outstanding messages to be processed on a ring. */
197843e1988Sjohnlev #define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
198843e1988Sjohnlev     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
199843e1988Sjohnlev 
200843e1988Sjohnlev #ifdef __GNUC__
201843e1988Sjohnlev #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
202843e1988Sjohnlev     unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
203843e1988Sjohnlev     unsigned int rsp = RING_SIZE(_r) -                                  \
204843e1988Sjohnlev         ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
205843e1988Sjohnlev     req < rsp ? req : rsp;                                              \
206843e1988Sjohnlev })
207843e1988Sjohnlev #else
208843e1988Sjohnlev /* Same as above, but without the nice GCC ({ ... }) syntax. */
209843e1988Sjohnlev #define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
210843e1988Sjohnlev     ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
211843e1988Sjohnlev       (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
212843e1988Sjohnlev      ((_r)->sring->req_prod - (_r)->req_cons) :                         \
213843e1988Sjohnlev      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
214843e1988Sjohnlev #endif
215843e1988Sjohnlev 
216843e1988Sjohnlev /* Direct access to individual ring elements, by index. */
217843e1988Sjohnlev #define RING_GET_REQUEST(_r, _idx)                                      \
218843e1988Sjohnlev     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
219843e1988Sjohnlev 
220843e1988Sjohnlev #define RING_GET_RESPONSE(_r, _idx)                                     \
221843e1988Sjohnlev     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
222843e1988Sjohnlev 
223843e1988Sjohnlev /* Loop termination condition: Would the specified index overflow the ring? */
224843e1988Sjohnlev #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
225843e1988Sjohnlev     (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
226843e1988Sjohnlev 
227843e1988Sjohnlev #define RING_PUSH_REQUESTS(_r) do {                                     \
228349b53ddSStuart Maybee     xen_wmb(); /* back sees requests /before/ updated producer index */ \
229843e1988Sjohnlev     (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
230843e1988Sjohnlev } while (0)
231843e1988Sjohnlev 
232843e1988Sjohnlev #define RING_PUSH_RESPONSES(_r) do {                                    \
233349b53ddSStuart Maybee     xen_wmb(); /* front sees resps /before/ updated producer index */   \
234843e1988Sjohnlev     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
235843e1988Sjohnlev } while (0)
236843e1988Sjohnlev 
237843e1988Sjohnlev /*
238843e1988Sjohnlev  * Notification hold-off (req_event and rsp_event):
239843e1988Sjohnlev  *
240843e1988Sjohnlev  * When queueing requests or responses on a shared ring, it may not always be
241843e1988Sjohnlev  * necessary to notify the remote end. For example, if requests are in flight
242843e1988Sjohnlev  * in a backend, the front may be able to queue further requests without
243843e1988Sjohnlev  * notifying the back (if the back checks for new requests when it queues
244843e1988Sjohnlev  * responses).
245843e1988Sjohnlev  *
246843e1988Sjohnlev  * When enqueuing requests or responses:
247843e1988Sjohnlev  *
248843e1988Sjohnlev  *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
249843e1988Sjohnlev  *  is a boolean return value. True indicates that the receiver requires an
250843e1988Sjohnlev  *  asynchronous notification.
251843e1988Sjohnlev  *
252843e1988Sjohnlev  * After dequeuing requests or responses (before sleeping the connection):
253843e1988Sjohnlev  *
254843e1988Sjohnlev  *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
255843e1988Sjohnlev  *  The second argument is a boolean return value. True indicates that there
256843e1988Sjohnlev  *  are pending messages on the ring (i.e., the connection should not be put
257843e1988Sjohnlev  *  to sleep).
258843e1988Sjohnlev  *
259843e1988Sjohnlev  *  These macros will set the req_event/rsp_event field to trigger a
260843e1988Sjohnlev  *  notification on the very next message that is enqueued. If you want to
261843e1988Sjohnlev  *  create batches of work (i.e., only receive a notification after several
262843e1988Sjohnlev  *  messages have been enqueued) then you will need to create a customised
263843e1988Sjohnlev  *  version of the FINAL_CHECK macro in your own code, which sets the event
264843e1988Sjohnlev  *  field appropriately.
265843e1988Sjohnlev  */
266843e1988Sjohnlev 
267843e1988Sjohnlev #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
268843e1988Sjohnlev     RING_IDX __old = (_r)->sring->req_prod;                             \
269843e1988Sjohnlev     RING_IDX __new = (_r)->req_prod_pvt;                                \
270349b53ddSStuart Maybee     xen_wmb(); /* back sees requests /before/ updated producer index */ \
271843e1988Sjohnlev     (_r)->sring->req_prod = __new;                                      \
272349b53ddSStuart Maybee     xen_mb(); /* back sees new requests /before/ we check req_event */  \
273843e1988Sjohnlev     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
274843e1988Sjohnlev                  (RING_IDX)(__new - __old));                            \
275843e1988Sjohnlev } while (0)
276843e1988Sjohnlev 
277843e1988Sjohnlev #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
278843e1988Sjohnlev     RING_IDX __old = (_r)->sring->rsp_prod;                             \
279843e1988Sjohnlev     RING_IDX __new = (_r)->rsp_prod_pvt;                                \
280349b53ddSStuart Maybee     xen_wmb(); /* front sees resps /before/ updated producer index */   \
281843e1988Sjohnlev     (_r)->sring->rsp_prod = __new;                                      \
282349b53ddSStuart Maybee     xen_mb(); /* front sees new resps /before/ we check rsp_event */    \
283843e1988Sjohnlev     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
284843e1988Sjohnlev                  (RING_IDX)(__new - __old));                            \
285843e1988Sjohnlev } while (0)
286843e1988Sjohnlev 
287843e1988Sjohnlev #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
288843e1988Sjohnlev     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
289843e1988Sjohnlev     if (_work_to_do) break;                                             \
290843e1988Sjohnlev     (_r)->sring->req_event = (_r)->req_cons + 1;                        \
291349b53ddSStuart Maybee     xen_mb();                                                           \
292843e1988Sjohnlev     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
293843e1988Sjohnlev } while (0)
294843e1988Sjohnlev 
295843e1988Sjohnlev #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
296843e1988Sjohnlev     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
297843e1988Sjohnlev     if (_work_to_do) break;                                             \
298843e1988Sjohnlev     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
299349b53ddSStuart Maybee     xen_mb();                                                           \
300843e1988Sjohnlev     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
301843e1988Sjohnlev } while (0)
302843e1988Sjohnlev 
303843e1988Sjohnlev #endif /* __XEN_PUBLIC_IO_RING_H__ */
304843e1988Sjohnlev 
305843e1988Sjohnlev /*
306843e1988Sjohnlev  * Local variables:
307843e1988Sjohnlev  * mode: C
308843e1988Sjohnlev  * c-set-style: "BSD"
309843e1988Sjohnlev  * c-basic-offset: 4
310843e1988Sjohnlev  * tab-width: 4
311843e1988Sjohnlev  * indent-tabs-mode: nil
312843e1988Sjohnlev  * End:
313843e1988Sjohnlev  */
314