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