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