1 /******************************************************************************
4 * Shared producer-consumer ring macros.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Tim Deegan and Andrew Warfield November 2004.
29 #include <ros/arch/membar.h>
32 #define xen_rmb() rmb()
33 #define xen_wmb() wmb()
35 /* Helpers for generic power-of-2 ring buffers */
36 #define __ring_nr_empty(sz, prod, cons) ((sz) - ((prod) - (cons)))
37 #define __ring_empty(prod, cons) ((prod) == (cons))
38 #define __ring_nr_full(prod, cons) ((prod) - (cons))
39 #define __ring_full(sz, prod, cons) (__ring_nr_empty((sz), (prod), (cons)) == 0)
41 typedef unsigned int RING_IDX;
43 /* Round a 32-bit unsigned constant down to the nearest power of two. */
44 #define __RD2(_x) (((_x) & 0x00000002UL) ? 0x2 : ((_x) & 0x1))
45 #define __RD4(_x) (((_x) & 0x0000000cUL) ? __RD2((_x)>>2)<<2 : __RD2(_x))
46 #define __RD8(_x) (((_x) & 0x000000f0UL) ? __RD4((_x)>>4)<<4 : __RD4(_x))
47 #define __RD16(_x) (((_x) & 0x0000ff00UL) ? __RD8((_x)>>8)<<8 : __RD8(_x))
48 #define __RD32(_x) (((_x) & 0xffff0000UL) ? __RD16((_x)>>16)<<16 : __RD16(_x))
51 * Calculate size of a shared ring, given the total available space for the
52 * ring and indexes (_sz), and the name tag of the request/response structure.
53 * A ring contains as many entries as will fit, rounded down to the nearest
54 * power of two (so we can mask with (size-1) to loop around).
56 #define __CONST_RING_SIZE(_s, _sz) \
57 (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
58 sizeof(((struct _s##_sring *)0)->ring[0])))
60 * The same for passing in an actual pointer instead of a name tag.
62 #define __RING_SIZE(_s, _sz) \
63 (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
66 * Macros to make the correct C datatypes for a new kind of ring.
68 * To make a new ring datatype, you need to have two message structures,
69 * let's say request_t, and response_t already defined.
71 * In a header where you want the ring datatype declared, you then do:
73 * DEFINE_RING_TYPES(mytag, request_t, response_t);
75 * These expand out to give you a set of types, as you can see below.
76 * The most important of these are:
78 * mytag_sring_t - The shared ring.
79 * mytag_front_ring_t - The 'front' half of the ring.
80 * mytag_back_ring_t - The 'back' half of the ring.
82 * To initialize a ring in your code you need to know the location and size
83 * of the shared memory area (PAGE_SIZE, for instance). To initialise
86 * mytag_front_ring_t front_ring;
87 * SHARED_RING_INIT((mytag_sring_t *)shared_page);
88 * FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
90 * Initializing the back follows similarly (note that only the front
91 * initializes the shared ring):
93 * mytag_back_ring_t back_ring;
94 * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
97 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
99 /* Shared ring entry */ \
100 union __name##_sring_entry { \
105 /* Shared ring page */ \
106 struct __name##_sring { \
107 RING_IDX req_prod, req_event; \
108 RING_IDX rsp_prod, rsp_event; \
111 uint8_t smartpoll_active; \
116 uint8_t pvt_pad[4]; \
119 union __name##_sring_entry ring[1]; /* variable-length */ \
122 /* "Front" end's private variables */ \
123 struct __name##_front_ring { \
124 RING_IDX req_prod_pvt; \
126 unsigned int nr_ents; \
127 struct __name##_sring *sring; \
130 /* "Back" end's private variables */ \
131 struct __name##_back_ring { \
132 RING_IDX rsp_prod_pvt; \
134 unsigned int nr_ents; \
135 struct __name##_sring *sring; \
138 /* Syntactic sugar */ \
139 typedef struct __name##_sring __name##_sring_t; \
140 typedef struct __name##_front_ring __name##_front_ring_t; \
141 typedef struct __name##_back_ring __name##_back_ring_t
144 * Macros for manipulating rings.
146 * FRONT_RING_whatever works on the "front end" of a ring: here
147 * requests are pushed on to the ring and responses taken off it.
149 * BACK_RING_whatever works on the "back end" of a ring: here
150 * requests are taken off the ring and responses put on.
152 * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
153 * This is OK in 1-for-1 request-response situations where the
154 * requestor (front end) never has more than RING_SIZE()-1
155 * outstanding requests.
158 /* Initialising empty rings */
159 #define SHARED_RING_INIT(_s) do { \
160 (_s)->req_prod = (_s)->rsp_prod = 0; \
161 (_s)->req_event = (_s)->rsp_event = 1; \
162 (void)memset((_s)->priv.pvt_pad, 0, sizeof((_s)->priv.pvt_pad)); \
163 (void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \
166 #define FRONT_RING_INIT(_r, _s, __size) do { \
167 (_r)->req_prod_pvt = 0; \
168 (_r)->rsp_cons = 0; \
169 (_r)->nr_ents = __RING_SIZE(_s, __size); \
170 (_r)->sring = (_s); \
173 #define BACK_RING_INIT(_r, _s, __size) do { \
174 (_r)->rsp_prod_pvt = 0; \
175 (_r)->req_cons = 0; \
176 (_r)->nr_ents = __RING_SIZE(_s, __size); \
177 (_r)->sring = (_s); \
180 /* Initialize to existing shared indexes -- for recovery */
181 #define FRONT_RING_ATTACH(_r, _s, __size) do { \
182 (_r)->sring = (_s); \
183 (_r)->req_prod_pvt = (_s)->req_prod; \
184 (_r)->rsp_cons = (_s)->rsp_prod; \
185 (_r)->nr_ents = __RING_SIZE(_s, __size); \
188 #define BACK_RING_ATTACH(_r, _s, __size) do { \
189 (_r)->sring = (_s); \
190 (_r)->rsp_prod_pvt = (_s)->rsp_prod; \
191 (_r)->req_cons = (_s)->req_prod; \
192 (_r)->nr_ents = __RING_SIZE(_s, __size); \
195 /* How big is this ring? */
196 #define RING_SIZE(_r) \
199 /* Number of free requests (for use on front side only). */
200 #define RING_FREE_REQUESTS(_r) \
201 (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
203 /* Test if there is an empty slot available on the front ring.
204 * (This is only meaningful from the front. )
206 #define RING_FULL(_r) \
207 (RING_FREE_REQUESTS(_r) == 0)
209 /* Test if there are outstanding messages to be processed on a ring. */
210 #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
211 ((_r)->sring->rsp_prod - (_r)->rsp_cons)
214 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \
215 unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
216 unsigned int rsp = RING_SIZE(_r) - \
217 ((_r)->req_cons - (_r)->rsp_prod_pvt); \
218 req < rsp ? req : rsp; \
221 /* Same as above, but without the nice GCC ({ ... }) syntax. */
222 #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
223 ((((_r)->sring->req_prod - (_r)->req_cons) < \
224 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \
225 ((_r)->sring->req_prod - (_r)->req_cons) : \
226 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
229 /* Direct access to individual ring elements, by index. */
230 #define RING_GET_REQUEST(_r, _idx) \
231 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
233 #define RING_GET_RESPONSE(_r, _idx) \
234 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
236 /* Loop termination condition: Would the specified index overflow the ring? */
237 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
238 (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
240 #define RING_PUSH_REQUESTS(_r) do { \
241 xen_wmb(); /* back sees requests /before/ updated producer index */ \
242 (_r)->sring->req_prod = (_r)->req_prod_pvt; \
245 #define RING_PUSH_RESPONSES(_r) do { \
246 xen_wmb(); /* front sees resps /before/ updated producer index */ \
247 (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
251 * Notification hold-off (req_event and rsp_event):
253 * When queueing requests or responses on a shared ring, it may not always be
254 * necessary to notify the remote end. For example, if requests are in flight
255 * in a backend, the front may be able to queue further requests without
256 * notifying the back (if the back checks for new requests when it queues
259 * When enqueuing requests or responses:
261 * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
262 * is a boolean return value. True indicates that the receiver requires an
263 * asynchronous notification.
265 * After dequeuing requests or responses (before sleeping the connection):
267 * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
268 * The second argument is a boolean return value. True indicates that there
269 * are pending messages on the ring (i.e., the connection should not be put
272 * These macros will set the req_event/rsp_event field to trigger a
273 * notification on the very next message that is enqueued. If you want to
274 * create batches of work (i.e., only receive a notification after several
275 * messages have been enqueued) then you will need to create a customised
276 * version of the FINAL_CHECK macro in your own code, which sets the event
277 * field appropriately.
280 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
281 RING_IDX __old = (_r)->sring->req_prod; \
282 RING_IDX __new = (_r)->req_prod_pvt; \
283 xen_wmb(); /* back sees requests /before/ updated producer index */ \
284 (_r)->sring->req_prod = __new; \
285 xen_mb(); /* back sees new requests /before/ we check req_event */ \
286 (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
287 (RING_IDX)(__new - __old)); \
290 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
291 RING_IDX __old = (_r)->sring->rsp_prod; \
292 RING_IDX __new = (_r)->rsp_prod_pvt; \
293 xen_wmb(); /* front sees resps /before/ updated producer index */ \
294 (_r)->sring->rsp_prod = __new; \
295 xen_mb(); /* front sees new resps /before/ we check rsp_event */ \
296 (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
297 (RING_IDX)(__new - __old)); \
300 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
301 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
302 if (_work_to_do) break; \
303 (_r)->sring->req_event = (_r)->req_cons + 1; \
305 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
308 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
309 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
310 if (_work_to_do) break; \
311 (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
313 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
322 * indent-tabs-mode: nil