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