xref: /openbmc/qemu/include/hw/xen/interface/io/ring.h (revision 50c88402ca599e577f025e78a4380431be2b3f6d)
1a3434a2dSAnthony PERARD /******************************************************************************
2a3434a2dSAnthony PERARD  * ring.h
3a3434a2dSAnthony PERARD  *
4a3434a2dSAnthony PERARD  * Shared producer-consumer ring macros.
5a3434a2dSAnthony PERARD  *
6a3434a2dSAnthony PERARD  * Permission is hereby granted, free of charge, to any person obtaining a copy
7a3434a2dSAnthony PERARD  * of this software and associated documentation files (the "Software"), to
8a3434a2dSAnthony PERARD  * deal in the Software without restriction, including without limitation the
9a3434a2dSAnthony PERARD  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10a3434a2dSAnthony PERARD  * sell copies of the Software, and to permit persons to whom the Software is
11a3434a2dSAnthony PERARD  * furnished to do so, subject to the following conditions:
12a3434a2dSAnthony PERARD  *
13a3434a2dSAnthony PERARD  * The above copyright notice and this permission notice shall be included in
14a3434a2dSAnthony PERARD  * all copies or substantial portions of the Software.
15a3434a2dSAnthony PERARD  *
16a3434a2dSAnthony PERARD  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17a3434a2dSAnthony PERARD  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18a3434a2dSAnthony PERARD  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19a3434a2dSAnthony PERARD  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20a3434a2dSAnthony PERARD  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21a3434a2dSAnthony PERARD  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22a3434a2dSAnthony PERARD  * DEALINGS IN THE SOFTWARE.
23a3434a2dSAnthony PERARD  *
24a3434a2dSAnthony PERARD  * Tim Deegan and Andrew Warfield November 2004.
25a3434a2dSAnthony PERARD  */
26a3434a2dSAnthony PERARD 
27a3434a2dSAnthony PERARD #ifndef __XEN_PUBLIC_IO_RING_H__
28a3434a2dSAnthony PERARD #define __XEN_PUBLIC_IO_RING_H__
29a3434a2dSAnthony PERARD 
30a3434a2dSAnthony PERARD /*
31a3434a2dSAnthony PERARD  * When #include'ing this header, you need to provide the following
32a3434a2dSAnthony PERARD  * declaration upfront:
33a3434a2dSAnthony PERARD  * - standard integers types (uint8_t, uint16_t, etc)
34a3434a2dSAnthony PERARD  * They are provided by stdint.h of the standard headers.
35a3434a2dSAnthony PERARD  *
36a3434a2dSAnthony PERARD  * In addition, if you intend to use the FLEX macros, you also need to
37a3434a2dSAnthony PERARD  * provide the following, before invoking the FLEX macros:
38a3434a2dSAnthony PERARD  * - size_t
39a3434a2dSAnthony PERARD  * - memcpy
40a3434a2dSAnthony PERARD  * - grant_ref_t
41a3434a2dSAnthony PERARD  * These declarations are provided by string.h of the standard headers,
42a3434a2dSAnthony PERARD  * and grant_table.h from the Xen public headers.
43a3434a2dSAnthony PERARD  */
44a3434a2dSAnthony PERARD 
45*50c88402SJoao Martins #include "../xen-compat.h"
46*50c88402SJoao Martins 
47*50c88402SJoao Martins #if __XEN_INTERFACE_VERSION__ < 0x00030208
48*50c88402SJoao Martins #define xen_mb()  mb()
49*50c88402SJoao Martins #define xen_rmb() rmb()
50*50c88402SJoao Martins #define xen_wmb() wmb()
51*50c88402SJoao Martins #endif
52*50c88402SJoao Martins 
53a3434a2dSAnthony PERARD typedef unsigned int RING_IDX;
54a3434a2dSAnthony PERARD 
55a3434a2dSAnthony PERARD /* Round a 32-bit unsigned constant down to the nearest power of two. */
56a3434a2dSAnthony PERARD #define __RD2(_x)  (((_x) & 0x00000002) ? 0x2                  : ((_x) & 0x1))
57a3434a2dSAnthony PERARD #define __RD4(_x)  (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2    : __RD2(_x))
58a3434a2dSAnthony PERARD #define __RD8(_x)  (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4    : __RD4(_x))
59a3434a2dSAnthony PERARD #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8    : __RD8(_x))
60a3434a2dSAnthony PERARD #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
61a3434a2dSAnthony PERARD 
62a3434a2dSAnthony PERARD /*
63a3434a2dSAnthony PERARD  * Calculate size of a shared ring, given the total available space for the
64a3434a2dSAnthony PERARD  * ring and indexes (_sz), and the name tag of the request/response structure.
65a3434a2dSAnthony PERARD  * A ring contains as many entries as will fit, rounded down to the nearest
66a3434a2dSAnthony PERARD  * power of two (so we can mask with (size-1) to loop around).
67a3434a2dSAnthony PERARD  */
68a3434a2dSAnthony PERARD #define __CONST_RING_SIZE(_s, _sz) \
69a3434a2dSAnthony PERARD     (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
70*50c88402SJoao Martins 	    sizeof(((struct _s##_sring *)0)->ring[0])))
71a3434a2dSAnthony PERARD /*
72a3434a2dSAnthony PERARD  * The same for passing in an actual pointer instead of a name tag.
73a3434a2dSAnthony PERARD  */
74a3434a2dSAnthony PERARD #define __RING_SIZE(_s, _sz) \
75a3434a2dSAnthony PERARD     (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
76a3434a2dSAnthony PERARD 
77a3434a2dSAnthony PERARD /*
78a3434a2dSAnthony PERARD  * Macros to make the correct C datatypes for a new kind of ring.
79a3434a2dSAnthony PERARD  *
80a3434a2dSAnthony PERARD  * To make a new ring datatype, you need to have two message structures,
81a3434a2dSAnthony PERARD  * let's say request_t, and response_t already defined.
82a3434a2dSAnthony PERARD  *
83a3434a2dSAnthony PERARD  * In a header where you want the ring datatype declared, you then do:
84a3434a2dSAnthony PERARD  *
85a3434a2dSAnthony PERARD  *     DEFINE_RING_TYPES(mytag, request_t, response_t);
86a3434a2dSAnthony PERARD  *
87a3434a2dSAnthony PERARD  * These expand out to give you a set of types, as you can see below.
88a3434a2dSAnthony PERARD  * The most important of these are:
89a3434a2dSAnthony PERARD  *
90a3434a2dSAnthony PERARD  *     mytag_sring_t      - The shared ring.
91a3434a2dSAnthony PERARD  *     mytag_front_ring_t - The 'front' half of the ring.
92a3434a2dSAnthony PERARD  *     mytag_back_ring_t  - The 'back' half of the ring.
93a3434a2dSAnthony PERARD  *
94a3434a2dSAnthony PERARD  * To initialize a ring in your code you need to know the location and size
95a3434a2dSAnthony PERARD  * of the shared memory area (PAGE_SIZE, for instance). To initialise
96a3434a2dSAnthony PERARD  * the front half:
97a3434a2dSAnthony PERARD  *
98a3434a2dSAnthony PERARD  *     mytag_front_ring_t front_ring;
99a3434a2dSAnthony PERARD  *     SHARED_RING_INIT((mytag_sring_t *)shared_page);
100a3434a2dSAnthony PERARD  *     FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
101a3434a2dSAnthony PERARD  *
102a3434a2dSAnthony PERARD  * Initializing the back follows similarly (note that only the front
103a3434a2dSAnthony PERARD  * initializes the shared ring):
104a3434a2dSAnthony PERARD  *
105a3434a2dSAnthony PERARD  *     mytag_back_ring_t back_ring;
106a3434a2dSAnthony PERARD  *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
107a3434a2dSAnthony PERARD  */
108a3434a2dSAnthony PERARD 
109a3434a2dSAnthony PERARD #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
110a3434a2dSAnthony PERARD                                                                         \
111a3434a2dSAnthony PERARD /* Shared ring entry */                                                 \
112a3434a2dSAnthony PERARD union __name##_sring_entry {                                            \
113a3434a2dSAnthony PERARD     __req_t req;                                                        \
114a3434a2dSAnthony PERARD     __rsp_t rsp;                                                        \
115a3434a2dSAnthony PERARD };                                                                      \
116a3434a2dSAnthony PERARD                                                                         \
117a3434a2dSAnthony PERARD /* Shared ring page */                                                  \
118a3434a2dSAnthony PERARD struct __name##_sring {                                                 \
119a3434a2dSAnthony PERARD     RING_IDX req_prod, req_event;                                       \
120a3434a2dSAnthony PERARD     RING_IDX rsp_prod, rsp_event;                                       \
121a3434a2dSAnthony PERARD     union {                                                             \
122a3434a2dSAnthony PERARD         struct {                                                        \
123a3434a2dSAnthony PERARD             uint8_t smartpoll_active;                                   \
124a3434a2dSAnthony PERARD         } netif;                                                        \
125a3434a2dSAnthony PERARD         struct {                                                        \
126a3434a2dSAnthony PERARD             uint8_t msg;                                                \
127a3434a2dSAnthony PERARD         } tapif_user;                                                   \
128a3434a2dSAnthony PERARD         uint8_t pvt_pad[4];                                             \
129a3434a2dSAnthony PERARD     } pvt;                                                              \
130a3434a2dSAnthony PERARD     uint8_t __pad[44];                                                  \
131a3434a2dSAnthony PERARD     union __name##_sring_entry ring[1]; /* variable-length */           \
132a3434a2dSAnthony PERARD };                                                                      \
133a3434a2dSAnthony PERARD                                                                         \
134a3434a2dSAnthony PERARD /* "Front" end's private variables */                                   \
135a3434a2dSAnthony PERARD struct __name##_front_ring {                                            \
136a3434a2dSAnthony PERARD     RING_IDX req_prod_pvt;                                              \
137a3434a2dSAnthony PERARD     RING_IDX rsp_cons;                                                  \
138a3434a2dSAnthony PERARD     unsigned int nr_ents;                                               \
139a3434a2dSAnthony PERARD     struct __name##_sring *sring;                                       \
140a3434a2dSAnthony PERARD };                                                                      \
141a3434a2dSAnthony PERARD                                                                         \
142a3434a2dSAnthony PERARD /* "Back" end's private variables */                                    \
143a3434a2dSAnthony PERARD struct __name##_back_ring {                                             \
144a3434a2dSAnthony PERARD     RING_IDX rsp_prod_pvt;                                              \
145a3434a2dSAnthony PERARD     RING_IDX req_cons;                                                  \
146a3434a2dSAnthony PERARD     unsigned int nr_ents;                                               \
147a3434a2dSAnthony PERARD     struct __name##_sring *sring;                                       \
148a3434a2dSAnthony PERARD };                                                                      \
149a3434a2dSAnthony PERARD                                                                         \
150a3434a2dSAnthony PERARD /* Syntactic sugar */                                                   \
151a3434a2dSAnthony PERARD typedef struct __name##_sring __name##_sring_t;                         \
152a3434a2dSAnthony PERARD typedef struct __name##_front_ring __name##_front_ring_t;               \
153a3434a2dSAnthony PERARD typedef struct __name##_back_ring __name##_back_ring_t
154a3434a2dSAnthony PERARD 
155a3434a2dSAnthony PERARD /*
156a3434a2dSAnthony PERARD  * Macros for manipulating rings.
157a3434a2dSAnthony PERARD  *
158a3434a2dSAnthony PERARD  * FRONT_RING_whatever works on the "front end" of a ring: here
159a3434a2dSAnthony PERARD  * requests are pushed on to the ring and responses taken off it.
160a3434a2dSAnthony PERARD  *
161a3434a2dSAnthony PERARD  * BACK_RING_whatever works on the "back end" of a ring: here
162a3434a2dSAnthony PERARD  * requests are taken off the ring and responses put on.
163a3434a2dSAnthony PERARD  *
164a3434a2dSAnthony PERARD  * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
165a3434a2dSAnthony PERARD  * This is OK in 1-for-1 request-response situations where the
166a3434a2dSAnthony PERARD  * requestor (front end) never has more than RING_SIZE()-1
167a3434a2dSAnthony PERARD  * outstanding requests.
168a3434a2dSAnthony PERARD  */
169a3434a2dSAnthony PERARD 
170a3434a2dSAnthony PERARD /* Initialising empty rings */
171a3434a2dSAnthony PERARD #define SHARED_RING_INIT(_s) do {                                       \
172a3434a2dSAnthony PERARD     (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
173a3434a2dSAnthony PERARD     (_s)->req_event = (_s)->rsp_event = 1;                              \
174a3434a2dSAnthony PERARD     (void)memset((_s)->pvt.pvt_pad, 0, sizeof((_s)->pvt.pvt_pad));      \
175a3434a2dSAnthony PERARD     (void)memset((_s)->__pad, 0, sizeof((_s)->__pad));                  \
176a3434a2dSAnthony PERARD } while(0)
177a3434a2dSAnthony PERARD 
178*50c88402SJoao Martins #define FRONT_RING_ATTACH(_r, _s, _i, __size) do {                      \
179*50c88402SJoao Martins     (_r)->req_prod_pvt = (_i);                                          \
180*50c88402SJoao Martins     (_r)->rsp_cons = (_i);                                              \
181a3434a2dSAnthony PERARD     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
182a3434a2dSAnthony PERARD     (_r)->sring = (_s);                                                 \
183a3434a2dSAnthony PERARD } while (0)
184a3434a2dSAnthony PERARD 
185*50c88402SJoao Martins #define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __size)
186*50c88402SJoao Martins 
187*50c88402SJoao Martins #define BACK_RING_ATTACH(_r, _s, _i, __size) do {                       \
188*50c88402SJoao Martins     (_r)->rsp_prod_pvt = (_i);                                          \
189*50c88402SJoao Martins     (_r)->req_cons = (_i);                                              \
190a3434a2dSAnthony PERARD     (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
191a3434a2dSAnthony PERARD     (_r)->sring = (_s);                                                 \
192a3434a2dSAnthony PERARD } while (0)
193a3434a2dSAnthony PERARD 
194*50c88402SJoao Martins #define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size)
195*50c88402SJoao Martins 
196a3434a2dSAnthony PERARD /* How big is this ring? */
197a3434a2dSAnthony PERARD #define RING_SIZE(_r)                                                   \
198a3434a2dSAnthony PERARD     ((_r)->nr_ents)
199a3434a2dSAnthony PERARD 
200a3434a2dSAnthony PERARD /* Number of free requests (for use on front side only). */
201a3434a2dSAnthony PERARD #define RING_FREE_REQUESTS(_r)                                          \
202a3434a2dSAnthony PERARD     (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
203a3434a2dSAnthony PERARD 
204a3434a2dSAnthony PERARD /* Test if there is an empty slot available on the front ring.
205a3434a2dSAnthony PERARD  * (This is only meaningful from the front. )
206a3434a2dSAnthony PERARD  */
207a3434a2dSAnthony PERARD #define RING_FULL(_r)                                                   \
208a3434a2dSAnthony PERARD     (RING_FREE_REQUESTS(_r) == 0)
209a3434a2dSAnthony PERARD 
210a3434a2dSAnthony PERARD /* Test if there are outstanding messages to be processed on a ring. */
211a3434a2dSAnthony PERARD #define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
212a3434a2dSAnthony PERARD     ((_r)->sring->rsp_prod - (_r)->rsp_cons)
213a3434a2dSAnthony PERARD 
214*50c88402SJoao Martins #ifdef __GNUC__
215a3434a2dSAnthony PERARD #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
216a3434a2dSAnthony PERARD     unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
217a3434a2dSAnthony PERARD     unsigned int rsp = RING_SIZE(_r) -                                  \
218a3434a2dSAnthony PERARD         ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
219a3434a2dSAnthony PERARD     req < rsp ? req : rsp;                                              \
220a3434a2dSAnthony PERARD })
221*50c88402SJoao Martins #else
222*50c88402SJoao Martins /* Same as above, but without the nice GCC ({ ... }) syntax. */
223*50c88402SJoao Martins #define RING_HAS_UNCONSUMED_REQUESTS(_r)                                \
224*50c88402SJoao Martins     ((((_r)->sring->req_prod - (_r)->req_cons) <                        \
225*50c88402SJoao Martins       (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ?        \
226*50c88402SJoao Martins      ((_r)->sring->req_prod - (_r)->req_cons) :                         \
227*50c88402SJoao Martins      (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
228*50c88402SJoao Martins #endif
229a3434a2dSAnthony PERARD 
230a3434a2dSAnthony PERARD /* Direct access to individual ring elements, by index. */
231a3434a2dSAnthony PERARD #define RING_GET_REQUEST(_r, _idx)                                      \
232a3434a2dSAnthony PERARD     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
233a3434a2dSAnthony PERARD 
234*50c88402SJoao Martins #define RING_GET_RESPONSE(_r, _idx)                                     \
235*50c88402SJoao Martins     (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
236*50c88402SJoao Martins 
237a3434a2dSAnthony PERARD /*
238*50c88402SJoao Martins  * Get a local copy of a request/response.
239a3434a2dSAnthony PERARD  *
240*50c88402SJoao Martins  * Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is
241a3434a2dSAnthony PERARD  * done on a local copy that cannot be modified by the other end.
242a3434a2dSAnthony PERARD  *
243a3434a2dSAnthony PERARD  * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
244*50c88402SJoao Martins  * to be ineffective where dest is a struct which consists of only bitfields.
245a3434a2dSAnthony PERARD  */
246*50c88402SJoao Martins #define RING_COPY_(type, r, idx, dest) do {				\
247*50c88402SJoao Martins 	/* Use volatile to force the copy into dest. */			\
248*50c88402SJoao Martins 	*(dest) = *(volatile __typeof__(dest))RING_GET_##type(r, idx);	\
249a3434a2dSAnthony PERARD } while (0)
250a3434a2dSAnthony PERARD 
251*50c88402SJoao Martins #define RING_COPY_REQUEST(r, idx, req)  RING_COPY_(REQUEST, r, idx, req)
252*50c88402SJoao Martins #define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp)
253a3434a2dSAnthony PERARD 
254a3434a2dSAnthony PERARD /* Loop termination condition: Would the specified index overflow the ring? */
255a3434a2dSAnthony PERARD #define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
256a3434a2dSAnthony PERARD     (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
257a3434a2dSAnthony PERARD 
258a3434a2dSAnthony PERARD /* Ill-behaved frontend determination: Can there be this many requests? */
259a3434a2dSAnthony PERARD #define RING_REQUEST_PROD_OVERFLOW(_r, _prod)                           \
260a3434a2dSAnthony PERARD     (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
261a3434a2dSAnthony PERARD 
262*50c88402SJoao Martins /* Ill-behaved backend determination: Can there be this many responses? */
263*50c88402SJoao Martins #define RING_RESPONSE_PROD_OVERFLOW(_r, _prod)                          \
264*50c88402SJoao Martins     (((_prod) - (_r)->rsp_cons) > RING_SIZE(_r))
265*50c88402SJoao Martins 
266a3434a2dSAnthony PERARD #define RING_PUSH_REQUESTS(_r) do {                                     \
267a3434a2dSAnthony PERARD     xen_wmb(); /* back sees requests /before/ updated producer index */ \
268a3434a2dSAnthony PERARD     (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
269a3434a2dSAnthony PERARD } while (0)
270a3434a2dSAnthony PERARD 
271a3434a2dSAnthony PERARD #define RING_PUSH_RESPONSES(_r) do {                                    \
272a3434a2dSAnthony PERARD     xen_wmb(); /* front sees resps /before/ updated producer index */   \
273a3434a2dSAnthony PERARD     (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
274a3434a2dSAnthony PERARD } while (0)
275a3434a2dSAnthony PERARD 
276a3434a2dSAnthony PERARD /*
277a3434a2dSAnthony PERARD  * Notification hold-off (req_event and rsp_event):
278a3434a2dSAnthony PERARD  *
279a3434a2dSAnthony PERARD  * When queueing requests or responses on a shared ring, it may not always be
280a3434a2dSAnthony PERARD  * necessary to notify the remote end. For example, if requests are in flight
281a3434a2dSAnthony PERARD  * in a backend, the front may be able to queue further requests without
282a3434a2dSAnthony PERARD  * notifying the back (if the back checks for new requests when it queues
283a3434a2dSAnthony PERARD  * responses).
284a3434a2dSAnthony PERARD  *
285a3434a2dSAnthony PERARD  * When enqueuing requests or responses:
286a3434a2dSAnthony PERARD  *
287a3434a2dSAnthony PERARD  *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
288a3434a2dSAnthony PERARD  *  is a boolean return value. True indicates that the receiver requires an
289a3434a2dSAnthony PERARD  *  asynchronous notification.
290a3434a2dSAnthony PERARD  *
291a3434a2dSAnthony PERARD  * After dequeuing requests or responses (before sleeping the connection):
292a3434a2dSAnthony PERARD  *
293a3434a2dSAnthony PERARD  *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
294a3434a2dSAnthony PERARD  *  The second argument is a boolean return value. True indicates that there
295a3434a2dSAnthony PERARD  *  are pending messages on the ring (i.e., the connection should not be put
296a3434a2dSAnthony PERARD  *  to sleep).
297a3434a2dSAnthony PERARD  *
298a3434a2dSAnthony PERARD  *  These macros will set the req_event/rsp_event field to trigger a
299a3434a2dSAnthony PERARD  *  notification on the very next message that is enqueued. If you want to
300a3434a2dSAnthony PERARD  *  create batches of work (i.e., only receive a notification after several
301a3434a2dSAnthony PERARD  *  messages have been enqueued) then you will need to create a customised
302a3434a2dSAnthony PERARD  *  version of the FINAL_CHECK macro in your own code, which sets the event
303a3434a2dSAnthony PERARD  *  field appropriately.
304a3434a2dSAnthony PERARD  */
305a3434a2dSAnthony PERARD 
306a3434a2dSAnthony PERARD #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
307a3434a2dSAnthony PERARD     RING_IDX __old = (_r)->sring->req_prod;                             \
308a3434a2dSAnthony PERARD     RING_IDX __new = (_r)->req_prod_pvt;                                \
309a3434a2dSAnthony PERARD     xen_wmb(); /* back sees requests /before/ updated producer index */ \
310a3434a2dSAnthony PERARD     (_r)->sring->req_prod = __new;                                      \
311a3434a2dSAnthony PERARD     xen_mb(); /* back sees new requests /before/ we check req_event */  \
312a3434a2dSAnthony PERARD     (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
313a3434a2dSAnthony PERARD                  (RING_IDX)(__new - __old));                            \
314a3434a2dSAnthony PERARD } while (0)
315a3434a2dSAnthony PERARD 
316a3434a2dSAnthony PERARD #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
317a3434a2dSAnthony PERARD     RING_IDX __old = (_r)->sring->rsp_prod;                             \
318a3434a2dSAnthony PERARD     RING_IDX __new = (_r)->rsp_prod_pvt;                                \
319a3434a2dSAnthony PERARD     xen_wmb(); /* front sees resps /before/ updated producer index */   \
320a3434a2dSAnthony PERARD     (_r)->sring->rsp_prod = __new;                                      \
321a3434a2dSAnthony PERARD     xen_mb(); /* front sees new resps /before/ we check rsp_event */    \
322a3434a2dSAnthony PERARD     (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
323a3434a2dSAnthony PERARD                  (RING_IDX)(__new - __old));                            \
324a3434a2dSAnthony PERARD } while (0)
325a3434a2dSAnthony PERARD 
326a3434a2dSAnthony PERARD #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
327a3434a2dSAnthony PERARD     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
328a3434a2dSAnthony PERARD     if (_work_to_do) break;                                             \
329a3434a2dSAnthony PERARD     (_r)->sring->req_event = (_r)->req_cons + 1;                        \
330a3434a2dSAnthony PERARD     xen_mb();                                                           \
331a3434a2dSAnthony PERARD     (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
332a3434a2dSAnthony PERARD } while (0)
333a3434a2dSAnthony PERARD 
334a3434a2dSAnthony PERARD #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
335a3434a2dSAnthony PERARD     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
336a3434a2dSAnthony PERARD     if (_work_to_do) break;                                             \
337a3434a2dSAnthony PERARD     (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
338a3434a2dSAnthony PERARD     xen_mb();                                                           \
339a3434a2dSAnthony PERARD     (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
340a3434a2dSAnthony PERARD } while (0)
341a3434a2dSAnthony PERARD 
342a3434a2dSAnthony PERARD 
343a3434a2dSAnthony PERARD /*
344a3434a2dSAnthony PERARD  * DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and
345a3434a2dSAnthony PERARD  * functions to check if there is data on the ring, and to read and
346a3434a2dSAnthony PERARD  * write to them.
347a3434a2dSAnthony PERARD  *
348a3434a2dSAnthony PERARD  * DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but
349a3434a2dSAnthony PERARD  * does not define the indexes page. As different protocols can have
350a3434a2dSAnthony PERARD  * extensions to the basic format, this macro allow them to define their
351a3434a2dSAnthony PERARD  * own struct.
352a3434a2dSAnthony PERARD  *
353a3434a2dSAnthony PERARD  * XEN_FLEX_RING_SIZE
354a3434a2dSAnthony PERARD  *   Convenience macro to calculate the size of one of the two rings
355a3434a2dSAnthony PERARD  *   from the overall order.
356a3434a2dSAnthony PERARD  *
357a3434a2dSAnthony PERARD  * $NAME_mask
358a3434a2dSAnthony PERARD  *   Function to apply the size mask to an index, to reduce the index
359a3434a2dSAnthony PERARD  *   within the range [0-size].
360a3434a2dSAnthony PERARD  *
361a3434a2dSAnthony PERARD  * $NAME_read_packet
362a3434a2dSAnthony PERARD  *   Function to read data from the ring. The amount of data to read is
363a3434a2dSAnthony PERARD  *   specified by the "size" argument.
364a3434a2dSAnthony PERARD  *
365a3434a2dSAnthony PERARD  * $NAME_write_packet
366a3434a2dSAnthony PERARD  *   Function to write data to the ring. The amount of data to write is
367a3434a2dSAnthony PERARD  *   specified by the "size" argument.
368a3434a2dSAnthony PERARD  *
369a3434a2dSAnthony PERARD  * $NAME_get_ring_ptr
370a3434a2dSAnthony PERARD  *   Convenience function that returns a pointer to read/write to the
371a3434a2dSAnthony PERARD  *   ring at the right location.
372a3434a2dSAnthony PERARD  *
373a3434a2dSAnthony PERARD  * $NAME_data_intf
374a3434a2dSAnthony PERARD  *   Indexes page, shared between frontend and backend. It also
375a3434a2dSAnthony PERARD  *   contains the array of grant refs.
376a3434a2dSAnthony PERARD  *
377a3434a2dSAnthony PERARD  * $NAME_queued
378a3434a2dSAnthony PERARD  *   Function to calculate how many bytes are currently on the ring,
379a3434a2dSAnthony PERARD  *   ready to be read. It can also be used to calculate how much free
380a3434a2dSAnthony PERARD  *   space is currently on the ring (XEN_FLEX_RING_SIZE() -
381a3434a2dSAnthony PERARD  *   $NAME_queued()).
382a3434a2dSAnthony PERARD  */
383a3434a2dSAnthony PERARD 
384a3434a2dSAnthony PERARD #ifndef XEN_PAGE_SHIFT
385a3434a2dSAnthony PERARD /* The PAGE_SIZE for ring protocols and hypercall interfaces is always
386a3434a2dSAnthony PERARD  * 4K, regardless of the architecture, and page granularity chosen by
387a3434a2dSAnthony PERARD  * operating systems.
388a3434a2dSAnthony PERARD  */
389a3434a2dSAnthony PERARD #define XEN_PAGE_SHIFT 12
390a3434a2dSAnthony PERARD #endif
391a3434a2dSAnthony PERARD #define XEN_FLEX_RING_SIZE(order)                                             \
392a3434a2dSAnthony PERARD     (1UL << ((order) + XEN_PAGE_SHIFT - 1))
393a3434a2dSAnthony PERARD 
394a3434a2dSAnthony PERARD #define DEFINE_XEN_FLEX_RING(name)                                            \
395a3434a2dSAnthony PERARD static inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size)          \
396a3434a2dSAnthony PERARD {                                                                             \
397a3434a2dSAnthony PERARD     return idx & (ring_size - 1);                                             \
398a3434a2dSAnthony PERARD }                                                                             \
399a3434a2dSAnthony PERARD                                                                               \
400a3434a2dSAnthony PERARD static inline unsigned char *name##_get_ring_ptr(unsigned char *buf,          \
401a3434a2dSAnthony PERARD                                                  RING_IDX idx,                \
402a3434a2dSAnthony PERARD                                                  RING_IDX ring_size)          \
403a3434a2dSAnthony PERARD {                                                                             \
404a3434a2dSAnthony PERARD     return buf + name##_mask(idx, ring_size);                                 \
405a3434a2dSAnthony PERARD }                                                                             \
406a3434a2dSAnthony PERARD                                                                               \
407a3434a2dSAnthony PERARD static inline void name##_read_packet(void *opaque,                           \
408a3434a2dSAnthony PERARD                                       const unsigned char *buf,               \
409a3434a2dSAnthony PERARD                                       size_t size,                            \
410a3434a2dSAnthony PERARD                                       RING_IDX masked_prod,                   \
411a3434a2dSAnthony PERARD                                       RING_IDX *masked_cons,                  \
412a3434a2dSAnthony PERARD                                       RING_IDX ring_size)                     \
413a3434a2dSAnthony PERARD {                                                                             \
414a3434a2dSAnthony PERARD     if (*masked_cons < masked_prod ||                                         \
415a3434a2dSAnthony PERARD         size <= ring_size - *masked_cons) {                                   \
416a3434a2dSAnthony PERARD         memcpy(opaque, buf + *masked_cons, size);                             \
417a3434a2dSAnthony PERARD     } else {                                                                  \
418a3434a2dSAnthony PERARD         memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons);         \
419a3434a2dSAnthony PERARD         memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf,       \
420a3434a2dSAnthony PERARD                size - (ring_size - *masked_cons));                            \
421a3434a2dSAnthony PERARD     }                                                                         \
422a3434a2dSAnthony PERARD     *masked_cons = name##_mask(*masked_cons + size, ring_size);               \
423a3434a2dSAnthony PERARD }                                                                             \
424a3434a2dSAnthony PERARD                                                                               \
425a3434a2dSAnthony PERARD static inline void name##_write_packet(unsigned char *buf,                    \
426a3434a2dSAnthony PERARD                                        const void *opaque,                    \
427a3434a2dSAnthony PERARD                                        size_t size,                           \
428a3434a2dSAnthony PERARD                                        RING_IDX *masked_prod,                 \
429a3434a2dSAnthony PERARD                                        RING_IDX masked_cons,                  \
430a3434a2dSAnthony PERARD                                        RING_IDX ring_size)                    \
431a3434a2dSAnthony PERARD {                                                                             \
432a3434a2dSAnthony PERARD     if (*masked_prod < masked_cons ||                                         \
433a3434a2dSAnthony PERARD         size <= ring_size - *masked_prod) {                                   \
434a3434a2dSAnthony PERARD         memcpy(buf + *masked_prod, opaque, size);                             \
435a3434a2dSAnthony PERARD     } else {                                                                  \
436a3434a2dSAnthony PERARD         memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod);         \
437a3434a2dSAnthony PERARD         memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod),     \
438a3434a2dSAnthony PERARD                size - (ring_size - *masked_prod));                            \
439a3434a2dSAnthony PERARD     }                                                                         \
440a3434a2dSAnthony PERARD     *masked_prod = name##_mask(*masked_prod + size, ring_size);               \
441a3434a2dSAnthony PERARD }                                                                             \
442a3434a2dSAnthony PERARD                                                                               \
443a3434a2dSAnthony PERARD static inline RING_IDX name##_queued(RING_IDX prod,                           \
444a3434a2dSAnthony PERARD                                      RING_IDX cons,                           \
445a3434a2dSAnthony PERARD                                      RING_IDX ring_size)                      \
446a3434a2dSAnthony PERARD {                                                                             \
447a3434a2dSAnthony PERARD     RING_IDX size;                                                            \
448a3434a2dSAnthony PERARD                                                                               \
449a3434a2dSAnthony PERARD     if (prod == cons)                                                         \
450a3434a2dSAnthony PERARD         return 0;                                                             \
451a3434a2dSAnthony PERARD                                                                               \
452a3434a2dSAnthony PERARD     prod = name##_mask(prod, ring_size);                                      \
453a3434a2dSAnthony PERARD     cons = name##_mask(cons, ring_size);                                      \
454a3434a2dSAnthony PERARD                                                                               \
455a3434a2dSAnthony PERARD     if (prod == cons)                                                         \
456a3434a2dSAnthony PERARD         return ring_size;                                                     \
457a3434a2dSAnthony PERARD                                                                               \
458a3434a2dSAnthony PERARD     if (prod > cons)                                                          \
459a3434a2dSAnthony PERARD         size = prod - cons;                                                   \
460a3434a2dSAnthony PERARD     else                                                                      \
461a3434a2dSAnthony PERARD         size = ring_size - (cons - prod);                                     \
462a3434a2dSAnthony PERARD     return size;                                                              \
463a3434a2dSAnthony PERARD }                                                                             \
464a3434a2dSAnthony PERARD                                                                               \
465a3434a2dSAnthony PERARD struct name##_data {                                                          \
466a3434a2dSAnthony PERARD     unsigned char *in; /* half of the allocation */                           \
467a3434a2dSAnthony PERARD     unsigned char *out; /* half of the allocation */                          \
468a3434a2dSAnthony PERARD }
469a3434a2dSAnthony PERARD 
470a3434a2dSAnthony PERARD #define DEFINE_XEN_FLEX_RING_AND_INTF(name)                                   \
471a3434a2dSAnthony PERARD struct name##_data_intf {                                                     \
472a3434a2dSAnthony PERARD     RING_IDX in_cons, in_prod;                                                \
473a3434a2dSAnthony PERARD                                                                               \
474a3434a2dSAnthony PERARD     uint8_t pad1[56];                                                         \
475a3434a2dSAnthony PERARD                                                                               \
476a3434a2dSAnthony PERARD     RING_IDX out_cons, out_prod;                                              \
477a3434a2dSAnthony PERARD                                                                               \
478a3434a2dSAnthony PERARD     uint8_t pad2[56];                                                         \
479a3434a2dSAnthony PERARD                                                                               \
480a3434a2dSAnthony PERARD     RING_IDX ring_order;                                                      \
481a3434a2dSAnthony PERARD     grant_ref_t ref[];                                                        \
482a3434a2dSAnthony PERARD };                                                                            \
483a3434a2dSAnthony PERARD DEFINE_XEN_FLEX_RING(name)
484a3434a2dSAnthony PERARD 
485a3434a2dSAnthony PERARD #endif /* __XEN_PUBLIC_IO_RING_H__ */
486a3434a2dSAnthony PERARD 
487a3434a2dSAnthony PERARD /*
488a3434a2dSAnthony PERARD  * Local variables:
489a3434a2dSAnthony PERARD  * mode: C
490a3434a2dSAnthony PERARD  * c-file-style: "BSD"
491a3434a2dSAnthony PERARD  * c-basic-offset: 4
492a3434a2dSAnthony PERARD  * tab-width: 4
493a3434a2dSAnthony PERARD  * indent-tabs-mode: nil
494a3434a2dSAnthony PERARD  * End:
495a3434a2dSAnthony PERARD  */
496