MAINTAINERS: Make section QOM cover hw/core/*bus.c as well
[qemu/armbru.git] / include / hw / xen / interface / io / ring.h
blob5d048b335c57febf0e9c0c0062e9d473e3f59a83
1 /******************************************************************************
2 * ring.h
3 *
4 * Shared producer-consumer ring macros.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Tim Deegan and Andrew Warfield November 2004.
27 #ifndef __XEN_PUBLIC_IO_RING_H__
28 #define __XEN_PUBLIC_IO_RING_H__
31 * When #include'ing this header, you need to provide the following
32 * declaration upfront:
33 * - standard integers types (uint8_t, uint16_t, etc)
34 * They are provided by stdint.h of the standard headers.
36 * Before using the different macros, you need to provide the following
37 * macros:
38 * - xen_mb() a memory barrier
39 * - xen_rmb() a read memory barrier
40 * - xen_wmb() a write memory barrier
41 * Example of those can be found in xenctrl.h.
43 * In addition, if you intend to use the FLEX macros, you also need to
44 * provide the following, before invoking the FLEX macros:
45 * - size_t
46 * - memcpy
47 * - grant_ref_t
48 * These declarations are provided by string.h of the standard headers,
49 * and grant_table.h from the Xen public headers.
52 typedef unsigned int RING_IDX;
54 /* Round a 32-bit unsigned constant down to the nearest power of two. */
55 #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
56 #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
57 #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
58 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
59 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
62 * Calculate size of a shared ring, given the total available space for the
63 * ring and indexes (_sz), and the name tag of the request/response structure.
64 * A ring contains as many entries as will fit, rounded down to the nearest
65 * power of two (so we can mask with (size-1) to loop around).
67 #define __CONST_RING_SIZE(_s, _sz) \
68 (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
69 sizeof_field(struct _s##_sring, ring[0])))
71 * The same for passing in an actual pointer instead of a name tag.
73 #define __RING_SIZE(_s, _sz) \
74 (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
77 * Macros to make the correct C datatypes for a new kind of ring.
79 * To make a new ring datatype, you need to have two message structures,
80 * let's say request_t, and response_t already defined.
82 * In a header where you want the ring datatype declared, you then do:
84 * DEFINE_RING_TYPES(mytag, request_t, response_t);
86 * These expand out to give you a set of types, as you can see below.
87 * The most important of these are:
89 * mytag_sring_t - The shared ring.
90 * mytag_front_ring_t - The 'front' half of the ring.
91 * mytag_back_ring_t - The 'back' half of the ring.
93 * To initialize a ring in your code you need to know the location and size
94 * of the shared memory area (PAGE_SIZE, for instance). To initialise
95 * the front half:
97 * mytag_front_ring_t front_ring;
98 * SHARED_RING_INIT((mytag_sring_t *)shared_page);
99 * FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
101 * Initializing the back follows similarly (note that only the front
102 * initializes the shared ring):
104 * mytag_back_ring_t back_ring;
105 * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
108 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
110 /* Shared ring entry */ \
111 union __name##_sring_entry { \
112 __req_t req; \
113 __rsp_t rsp; \
114 }; \
116 /* Shared ring page */ \
117 struct __name##_sring { \
118 RING_IDX req_prod, req_event; \
119 RING_IDX rsp_prod, rsp_event; \
120 union { \
121 struct { \
122 uint8_t smartpoll_active; \
123 } netif; \
124 struct { \
125 uint8_t msg; \
126 } tapif_user; \
127 uint8_t pvt_pad[4]; \
128 } pvt; \
129 uint8_t __pad[44]; \
130 union __name##_sring_entry ring[1]; /* variable-length */ \
131 }; \
133 /* "Front" end's private variables */ \
134 struct __name##_front_ring { \
135 RING_IDX req_prod_pvt; \
136 RING_IDX rsp_cons; \
137 unsigned int nr_ents; \
138 struct __name##_sring *sring; \
139 }; \
141 /* "Back" end's private variables */ \
142 struct __name##_back_ring { \
143 RING_IDX rsp_prod_pvt; \
144 RING_IDX req_cons; \
145 unsigned int nr_ents; \
146 struct __name##_sring *sring; \
147 }; \
149 /* Syntactic sugar */ \
150 typedef struct __name##_sring __name##_sring_t; \
151 typedef struct __name##_front_ring __name##_front_ring_t; \
152 typedef struct __name##_back_ring __name##_back_ring_t
155 * Macros for manipulating rings.
157 * FRONT_RING_whatever works on the "front end" of a ring: here
158 * requests are pushed on to the ring and responses taken off it.
160 * BACK_RING_whatever works on the "back end" of a ring: here
161 * requests are taken off the ring and responses put on.
163 * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
164 * This is OK in 1-for-1 request-response situations where the
165 * requestor (front end) never has more than RING_SIZE()-1
166 * outstanding requests.
169 /* Initialising empty rings */
170 #define SHARED_RING_INIT(_s) do { \
171 (_s)->req_prod = (_s)->rsp_prod = 0; \
172 (_s)->req_event = (_s)->rsp_event = 1; \
173 (void)memset((_s)->pvt.pvt_pad, 0, sizeof((_s)->pvt.pvt_pad)); \
174 (void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \
175 } while(0)
177 #define FRONT_RING_INIT(_r, _s, __size) do { \
178 (_r)->req_prod_pvt = 0; \
179 (_r)->rsp_cons = 0; \
180 (_r)->nr_ents = __RING_SIZE(_s, __size); \
181 (_r)->sring = (_s); \
182 } while (0)
184 #define BACK_RING_INIT(_r, _s, __size) do { \
185 (_r)->rsp_prod_pvt = 0; \
186 (_r)->req_cons = 0; \
187 (_r)->nr_ents = __RING_SIZE(_s, __size); \
188 (_r)->sring = (_s); \
189 } while (0)
191 /* How big is this ring? */
192 #define RING_SIZE(_r) \
193 ((_r)->nr_ents)
195 /* Number of free requests (for use on front side only). */
196 #define RING_FREE_REQUESTS(_r) \
197 (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
199 /* Test if there is an empty slot available on the front ring.
200 * (This is only meaningful from the front. )
202 #define RING_FULL(_r) \
203 (RING_FREE_REQUESTS(_r) == 0)
205 /* Test if there are outstanding messages to be processed on a ring. */
206 #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
207 ((_r)->sring->rsp_prod - (_r)->rsp_cons)
209 #ifdef __GNUC__
210 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \
211 unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
212 unsigned int rsp = RING_SIZE(_r) - \
213 ((_r)->req_cons - (_r)->rsp_prod_pvt); \
214 req < rsp ? req : rsp; \
216 #else
217 /* Same as above, but without the nice GCC ({ ... }) syntax. */
218 #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
219 ((((_r)->sring->req_prod - (_r)->req_cons) < \
220 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \
221 ((_r)->sring->req_prod - (_r)->req_cons) : \
222 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
223 #endif
225 /* Direct access to individual ring elements, by index. */
226 #define RING_GET_REQUEST(_r, _idx) \
227 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
230 * Get a local copy of a request.
232 * Use this in preference to RING_GET_REQUEST() so all processing is
233 * done on a local copy that cannot be modified by the other end.
235 * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
236 * to be ineffective where _req is a struct which consists of only bitfields.
238 #define RING_COPY_REQUEST(_r, _idx, _req) do { \
239 /* Use volatile to force the copy into _req. */ \
240 *(_req) = *(volatile typeof(_req))RING_GET_REQUEST(_r, _idx); \
241 } while (0)
243 #define RING_GET_RESPONSE(_r, _idx) \
244 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
246 /* Loop termination condition: Would the specified index overflow the ring? */
247 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
248 (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
250 /* Ill-behaved frontend determination: Can there be this many requests? */
251 #define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \
252 (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
254 #define RING_PUSH_REQUESTS(_r) do { \
255 xen_wmb(); /* back sees requests /before/ updated producer index */ \
256 (_r)->sring->req_prod = (_r)->req_prod_pvt; \
257 } while (0)
259 #define RING_PUSH_RESPONSES(_r) do { \
260 xen_wmb(); /* front sees resps /before/ updated producer index */ \
261 (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
262 } while (0)
265 * Notification hold-off (req_event and rsp_event):
267 * When queueing requests or responses on a shared ring, it may not always be
268 * necessary to notify the remote end. For example, if requests are in flight
269 * in a backend, the front may be able to queue further requests without
270 * notifying the back (if the back checks for new requests when it queues
271 * responses).
273 * When enqueuing requests or responses:
275 * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
276 * is a boolean return value. True indicates that the receiver requires an
277 * asynchronous notification.
279 * After dequeuing requests or responses (before sleeping the connection):
281 * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
282 * The second argument is a boolean return value. True indicates that there
283 * are pending messages on the ring (i.e., the connection should not be put
284 * to sleep).
286 * These macros will set the req_event/rsp_event field to trigger a
287 * notification on the very next message that is enqueued. If you want to
288 * create batches of work (i.e., only receive a notification after several
289 * messages have been enqueued) then you will need to create a customised
290 * version of the FINAL_CHECK macro in your own code, which sets the event
291 * field appropriately.
294 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
295 RING_IDX __old = (_r)->sring->req_prod; \
296 RING_IDX __new = (_r)->req_prod_pvt; \
297 xen_wmb(); /* back sees requests /before/ updated producer index */ \
298 (_r)->sring->req_prod = __new; \
299 xen_mb(); /* back sees new requests /before/ we check req_event */ \
300 (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
301 (RING_IDX)(__new - __old)); \
302 } while (0)
304 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
305 RING_IDX __old = (_r)->sring->rsp_prod; \
306 RING_IDX __new = (_r)->rsp_prod_pvt; \
307 xen_wmb(); /* front sees resps /before/ updated producer index */ \
308 (_r)->sring->rsp_prod = __new; \
309 xen_mb(); /* front sees new resps /before/ we check rsp_event */ \
310 (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
311 (RING_IDX)(__new - __old)); \
312 } while (0)
314 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
315 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
316 if (_work_to_do) break; \
317 (_r)->sring->req_event = (_r)->req_cons + 1; \
318 xen_mb(); \
319 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
320 } while (0)
322 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
323 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
324 if (_work_to_do) break; \
325 (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
326 xen_mb(); \
327 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
328 } while (0)
332 * DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and
333 * functions to check if there is data on the ring, and to read and
334 * write to them.
336 * DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but
337 * does not define the indexes page. As different protocols can have
338 * extensions to the basic format, this macro allow them to define their
339 * own struct.
341 * XEN_FLEX_RING_SIZE
342 * Convenience macro to calculate the size of one of the two rings
343 * from the overall order.
345 * $NAME_mask
346 * Function to apply the size mask to an index, to reduce the index
347 * within the range [0-size].
349 * $NAME_read_packet
350 * Function to read data from the ring. The amount of data to read is
351 * specified by the "size" argument.
353 * $NAME_write_packet
354 * Function to write data to the ring. The amount of data to write is
355 * specified by the "size" argument.
357 * $NAME_get_ring_ptr
358 * Convenience function that returns a pointer to read/write to the
359 * ring at the right location.
361 * $NAME_data_intf
362 * Indexes page, shared between frontend and backend. It also
363 * contains the array of grant refs.
365 * $NAME_queued
366 * Function to calculate how many bytes are currently on the ring,
367 * ready to be read. It can also be used to calculate how much free
368 * space is currently on the ring (XEN_FLEX_RING_SIZE() -
369 * $NAME_queued()).
372 #ifndef XEN_PAGE_SHIFT
373 /* The PAGE_SIZE for ring protocols and hypercall interfaces is always
374 * 4K, regardless of the architecture, and page granularity chosen by
375 * operating systems.
377 #define XEN_PAGE_SHIFT 12
378 #endif
379 #define XEN_FLEX_RING_SIZE(order) \
380 (1UL << ((order) + XEN_PAGE_SHIFT - 1))
382 #define DEFINE_XEN_FLEX_RING(name) \
383 static inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size) \
385 return idx & (ring_size - 1); \
388 static inline unsigned char *name##_get_ring_ptr(unsigned char *buf, \
389 RING_IDX idx, \
390 RING_IDX ring_size) \
392 return buf + name##_mask(idx, ring_size); \
395 static inline void name##_read_packet(void *opaque, \
396 const unsigned char *buf, \
397 size_t size, \
398 RING_IDX masked_prod, \
399 RING_IDX *masked_cons, \
400 RING_IDX ring_size) \
402 if (*masked_cons < masked_prod || \
403 size <= ring_size - *masked_cons) { \
404 memcpy(opaque, buf + *masked_cons, size); \
405 } else { \
406 memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons); \
407 memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf, \
408 size - (ring_size - *masked_cons)); \
410 *masked_cons = name##_mask(*masked_cons + size, ring_size); \
413 static inline void name##_write_packet(unsigned char *buf, \
414 const void *opaque, \
415 size_t size, \
416 RING_IDX *masked_prod, \
417 RING_IDX masked_cons, \
418 RING_IDX ring_size) \
420 if (*masked_prod < masked_cons || \
421 size <= ring_size - *masked_prod) { \
422 memcpy(buf + *masked_prod, opaque, size); \
423 } else { \
424 memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod); \
425 memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod), \
426 size - (ring_size - *masked_prod)); \
428 *masked_prod = name##_mask(*masked_prod + size, ring_size); \
431 static inline RING_IDX name##_queued(RING_IDX prod, \
432 RING_IDX cons, \
433 RING_IDX ring_size) \
435 RING_IDX size; \
437 if (prod == cons) \
438 return 0; \
440 prod = name##_mask(prod, ring_size); \
441 cons = name##_mask(cons, ring_size); \
443 if (prod == cons) \
444 return ring_size; \
446 if (prod > cons) \
447 size = prod - cons; \
448 else \
449 size = ring_size - (cons - prod); \
450 return size; \
453 struct name##_data { \
454 unsigned char *in; /* half of the allocation */ \
455 unsigned char *out; /* half of the allocation */ \
458 #define DEFINE_XEN_FLEX_RING_AND_INTF(name) \
459 struct name##_data_intf { \
460 RING_IDX in_cons, in_prod; \
462 uint8_t pad1[56]; \
464 RING_IDX out_cons, out_prod; \
466 uint8_t pad2[56]; \
468 RING_IDX ring_order; \
469 grant_ref_t ref[]; \
470 }; \
471 DEFINE_XEN_FLEX_RING(name)
473 #endif /* __XEN_PUBLIC_IO_RING_H__ */
476 * Local variables:
477 * mode: C
478 * c-file-style: "BSD"
479 * c-basic-offset: 4
480 * tab-width: 4
481 * indent-tabs-mode: nil
482 * End: