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