Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / xen / include / xen3-public / io / ring.h
blobf0c782d9138fee3df0e45796e2ff0a5b5e5305af
1 /* $NetBSD: ring.h,v 1.10 2008/08/22 14:14:04 cegger Exp $ */
2 /******************************************************************************
3 * ring.h
4 *
5 * Shared producer-consumer ring macros.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to
9 * deal in the Software without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Tim Deegan and Andrew Warfield November 2004.
28 #ifndef __XEN_PUBLIC_IO_RING_H__
29 #define __XEN_PUBLIC_IO_RING_H__
31 #include "../xen-compat.h"
33 #if __XEN_INTERFACE_VERSION__ < 0x00030208
34 #if defined(__Linux__)
35 #define xen_mb() mb()
36 #define xen_rmb() rmb()
37 #define xen_wmb() wmb()
38 #endif
39 #endif
41 #if defined(__NetBSD__)
42 #define xen_mb() x86_mfence()
43 #define xen_rmb() x86_lfence()
44 #define xen_wmb() x86_sfence()
45 #endif
47 typedef unsigned int RING_IDX;
49 /* Round a 32-bit unsigned constant down to the nearest power of two. */
50 #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
51 #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
52 #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
53 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
54 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
57 * Calculate size of a shared ring, given the total available space for the
58 * ring and indexes (_sz), and the name tag of the request/response structure.
59 * A ring contains as many entries as will fit, rounded down to the nearest
60 * power of two (so we can mask with (size-1) to loop around).
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
84 * the front half:
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 { \
101 __req_t req; \
102 __rsp_t rsp; \
103 }; \
105 /* Shared ring page */ \
106 struct __name##_sring { \
107 RING_IDX req_prod, req_event; \
108 RING_IDX rsp_prod, rsp_event; \
109 uint8_t pad[48]; \
110 union __name##_sring_entry ring[1]; /* variable-length */ \
111 }; \
113 /* "Front" end's private variables */ \
114 struct __name##_front_ring { \
115 RING_IDX req_prod_pvt; \
116 RING_IDX rsp_cons; \
117 unsigned int nr_ents; \
118 struct __name##_sring *sring; \
119 }; \
121 /* "Back" end's private variables */ \
122 struct __name##_back_ring { \
123 RING_IDX rsp_prod_pvt; \
124 RING_IDX req_cons; \
125 unsigned int nr_ents; \
126 struct __name##_sring *sring; \
127 }; \
129 /* Syntactic sugar */ \
130 typedef struct __name##_sring __name##_sring_t; \
131 typedef struct __name##_front_ring __name##_front_ring_t; \
132 typedef struct __name##_back_ring __name##_back_ring_t
135 * Macros for manipulating rings.
137 * FRONT_RING_whatever works on the "front end" of a ring: here
138 * requests are pushed on to the ring and responses taken off it.
140 * BACK_RING_whatever works on the "back end" of a ring: here
141 * requests are taken off the ring and responses put on.
143 * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
144 * This is OK in 1-for-1 request-response situations where the
145 * requestor (front end) never has more than RING_SIZE()-1
146 * outstanding requests.
149 /* Initialising empty rings */
150 #define SHARED_RING_INIT(_s) do { \
151 (_s)->req_prod = (_s)->rsp_prod = 0; \
152 (_s)->req_event = (_s)->rsp_event = 1; \
153 (void)memset((_s)->pad, 0, sizeof((_s)->pad)); \
154 } while(0)
156 #define FRONT_RING_INIT(_r, _s, __size) do { \
157 (_r)->req_prod_pvt = 0; \
158 (_r)->rsp_cons = 0; \
159 (_r)->nr_ents = __RING_SIZE(_s, __size); \
160 (_r)->sring = (_s); \
161 } while (0)
163 #define BACK_RING_INIT(_r, _s, __size) do { \
164 (_r)->rsp_prod_pvt = 0; \
165 (_r)->req_cons = 0; \
166 (_r)->nr_ents = __RING_SIZE(_s, __size); \
167 (_r)->sring = (_s); \
168 } while (0)
170 /* Initialize to existing shared indexes -- for recovery */
171 #define FRONT_RING_ATTACH(_r, _s, __size) do { \
172 (_r)->sring = (_s); \
173 (_r)->req_prod_pvt = (_s)->req_prod; \
174 (_r)->rsp_cons = (_s)->rsp_prod; \
175 (_r)->nr_ents = __RING_SIZE(_s, __size); \
176 } while (0)
178 #define BACK_RING_ATTACH(_r, _s, __size) do { \
179 (_r)->sring = (_s); \
180 (_r)->rsp_prod_pvt = (_s)->rsp_prod; \
181 (_r)->req_cons = (_s)->req_prod; \
182 (_r)->nr_ents = __RING_SIZE(_s, __size); \
183 } while (0)
185 /* How big is this ring? */
186 #define RING_SIZE(_r) \
187 ((_r)->nr_ents)
189 /* Number of free requests (for use on front side only). */
190 #define RING_FREE_REQUESTS(_r) \
191 (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
193 /* Test if there is an empty slot available on the front ring.
194 * (This is only meaningful from the front. )
196 #define RING_FULL(_r) \
197 (RING_FREE_REQUESTS(_r) == 0)
199 /* Test if there are outstanding messages to be processed on a ring. */
200 #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
201 ((_r)->sring->rsp_prod - (_r)->rsp_cons)
203 #ifdef __GNUC__
204 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \
205 unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
206 unsigned int rsp = RING_SIZE(_r) - \
207 ((_r)->req_cons - (_r)->rsp_prod_pvt); \
208 req < rsp ? req : rsp; \
210 #else
211 /* Same as above, but without the nice GCC ({ ... }) syntax. */
212 #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
213 ((((_r)->sring->req_prod - (_r)->req_cons) < \
214 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \
215 ((_r)->sring->req_prod - (_r)->req_cons) : \
216 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
217 #endif
219 /* Direct access to individual ring elements, by index. */
220 #define RING_GET_REQUEST(_r, _idx) \
221 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
223 #define RING_GET_RESPONSE(_r, _idx) \
224 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
226 /* Loop termination condition: Would the specified index overflow the ring? */
227 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
228 (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
230 #define RING_PUSH_REQUESTS(_r) do { \
231 xen_wmb(); /* back sees requests /before/ updated producer index */ \
232 (_r)->sring->req_prod = (_r)->req_prod_pvt; \
233 } while (0)
235 #define RING_PUSH_RESPONSES(_r) do { \
236 xen_wmb(); /* front sees resps /before/ updated producer index */ \
237 (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
238 } while (0)
241 * Notification hold-off (req_event and rsp_event):
243 * When queueing requests or responses on a shared ring, it may not always be
244 * necessary to notify the remote end. For example, if requests are in flight
245 * in a backend, the front may be able to queue further requests without
246 * notifying the back (if the back checks for new requests when it queues
247 * responses).
249 * When enqueuing requests or responses:
251 * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
252 * is a boolean return value. True indicates that the receiver requires an
253 * asynchronous notification.
255 * After dequeuing requests or responses (before sleeping the connection):
257 * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
258 * The second argument is a boolean return value. True indicates that there
259 * are pending messages on the ring (i.e., the connection should not be put
260 * to sleep).
262 * These macros will set the req_event/rsp_event field to trigger a
263 * notification on the very next message that is enqueued. If you want to
264 * create batches of work (i.e., only receive a notification after several
265 * messages have been enqueued) then you will need to create a customised
266 * version of the FINAL_CHECK macro in your own code, which sets the event
267 * field appropriately.
270 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
271 RING_IDX __old = (_r)->sring->req_prod; \
272 RING_IDX __new = (_r)->req_prod_pvt; \
273 xen_wmb(); /* back sees requests /before/ updated producer index */ \
274 (_r)->sring->req_prod = __new; \
275 xen_mb(); /* back sees new requests /before/ we check req_event */ \
276 (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
277 (RING_IDX)(__new - __old)); \
278 } while (0)
280 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
281 RING_IDX __old = (_r)->sring->rsp_prod; \
282 RING_IDX __new = (_r)->rsp_prod_pvt; \
283 xen_wmb(); /* front sees resps /before/ updated producer index */ \
284 (_r)->sring->rsp_prod = __new; \
285 xen_mb(); /* front sees new resps /before/ we check rsp_event */ \
286 (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
287 (RING_IDX)(__new - __old)); \
288 } while (0)
290 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
291 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
292 if (_work_to_do) break; \
293 (_r)->sring->req_event = (_r)->req_cons + 1; \
294 xen_mb(); \
295 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
296 } while (0)
298 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
299 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
300 if (_work_to_do) break; \
301 (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
302 xen_mb(); \
303 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
304 } while (0)
306 #endif /* __XEN_PUBLIC_IO_RING_H__ */
309 * Local variables:
310 * mode: C
311 * c-set-style: "BSD"
312 * c-basic-offset: 4
313 * tab-width: 4
314 * indent-tabs-mode: nil
315 * End: