18c2ecf20Sopenharmony_ci/******************************************************************************
28c2ecf20Sopenharmony_ci * ring.h
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Shared producer-consumer ring macros.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
78c2ecf20Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
88c2ecf20Sopenharmony_ci * deal in the Software without restriction, including without limitation the
98c2ecf20Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
108c2ecf20Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
118c2ecf20Sopenharmony_ci * furnished to do so, subject to the following conditions:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
148c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
178c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
188c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
198c2ecf20Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
208c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
218c2ecf20Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
228c2ecf20Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Tim Deegan and Andrew Warfield November 2004.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#ifndef __XEN_PUBLIC_IO_RING_H__
288c2ecf20Sopenharmony_ci#define __XEN_PUBLIC_IO_RING_H__
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * When #include'ing this header, you need to provide the following
328c2ecf20Sopenharmony_ci * declaration upfront:
338c2ecf20Sopenharmony_ci * - standard integers types (uint8_t, uint16_t, etc)
348c2ecf20Sopenharmony_ci * They are provided by stdint.h of the standard headers.
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * In addition, if you intend to use the FLEX macros, you also need to
378c2ecf20Sopenharmony_ci * provide the following, before invoking the FLEX macros:
388c2ecf20Sopenharmony_ci * - size_t
398c2ecf20Sopenharmony_ci * - memcpy
408c2ecf20Sopenharmony_ci * - grant_ref_t
418c2ecf20Sopenharmony_ci * These declarations are provided by string.h of the standard headers,
428c2ecf20Sopenharmony_ci * and grant_table.h from the Xen public headers.
438c2ecf20Sopenharmony_ci */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#include <xen/interface/grant_table.h>
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_citypedef unsigned int RING_IDX;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* Round a 32-bit unsigned constant down to the nearest power of two. */
508c2ecf20Sopenharmony_ci#define __RD2(_x)  (((_x) & 0x00000002) ? 0x2                  : ((_x) & 0x1))
518c2ecf20Sopenharmony_ci#define __RD4(_x)  (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2    : __RD2(_x))
528c2ecf20Sopenharmony_ci#define __RD8(_x)  (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4    : __RD4(_x))
538c2ecf20Sopenharmony_ci#define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8    : __RD8(_x))
548c2ecf20Sopenharmony_ci#define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/*
578c2ecf20Sopenharmony_ci * Calculate size of a shared ring, given the total available space for the
588c2ecf20Sopenharmony_ci * ring and indexes (_sz), and the name tag of the request/response structure.
598c2ecf20Sopenharmony_ci * A ring contains as many entries as will fit, rounded down to the nearest
608c2ecf20Sopenharmony_ci * power of two (so we can mask with (size-1) to loop around).
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci#define __CONST_RING_SIZE(_s, _sz) \
638c2ecf20Sopenharmony_ci    (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
648c2ecf20Sopenharmony_ci	    sizeof(((struct _s##_sring *)0)->ring[0])))
658c2ecf20Sopenharmony_ci/*
668c2ecf20Sopenharmony_ci * The same for passing in an actual pointer instead of a name tag.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_ci#define __RING_SIZE(_s, _sz) \
698c2ecf20Sopenharmony_ci    (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/*
728c2ecf20Sopenharmony_ci * Macros to make the correct C datatypes for a new kind of ring.
738c2ecf20Sopenharmony_ci *
748c2ecf20Sopenharmony_ci * To make a new ring datatype, you need to have two message structures,
758c2ecf20Sopenharmony_ci * let's say request_t, and response_t already defined.
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * In a header where you want the ring datatype declared, you then do:
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci *     DEFINE_RING_TYPES(mytag, request_t, response_t);
808c2ecf20Sopenharmony_ci *
818c2ecf20Sopenharmony_ci * These expand out to give you a set of types, as you can see below.
828c2ecf20Sopenharmony_ci * The most important of these are:
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci *     mytag_sring_t      - The shared ring.
858c2ecf20Sopenharmony_ci *     mytag_front_ring_t - The 'front' half of the ring.
868c2ecf20Sopenharmony_ci *     mytag_back_ring_t  - The 'back' half of the ring.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * To initialize a ring in your code you need to know the location and size
898c2ecf20Sopenharmony_ci * of the shared memory area (PAGE_SIZE, for instance). To initialise
908c2ecf20Sopenharmony_ci * the front half:
918c2ecf20Sopenharmony_ci *
928c2ecf20Sopenharmony_ci *     mytag_front_ring_t front_ring;
938c2ecf20Sopenharmony_ci *     SHARED_RING_INIT((mytag_sring_t *)shared_page);
948c2ecf20Sopenharmony_ci *     FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
958c2ecf20Sopenharmony_ci *
968c2ecf20Sopenharmony_ci * Initializing the back follows similarly (note that only the front
978c2ecf20Sopenharmony_ci * initializes the shared ring):
988c2ecf20Sopenharmony_ci *
998c2ecf20Sopenharmony_ci *     mytag_back_ring_t back_ring;
1008c2ecf20Sopenharmony_ci *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
1018c2ecf20Sopenharmony_ci */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
1048c2ecf20Sopenharmony_ci                                                                        \
1058c2ecf20Sopenharmony_ci/* Shared ring entry */                                                 \
1068c2ecf20Sopenharmony_ciunion __name##_sring_entry {                                            \
1078c2ecf20Sopenharmony_ci    __req_t req;                                                        \
1088c2ecf20Sopenharmony_ci    __rsp_t rsp;                                                        \
1098c2ecf20Sopenharmony_ci};                                                                      \
1108c2ecf20Sopenharmony_ci                                                                        \
1118c2ecf20Sopenharmony_ci/* Shared ring page */                                                  \
1128c2ecf20Sopenharmony_cistruct __name##_sring {                                                 \
1138c2ecf20Sopenharmony_ci    RING_IDX req_prod, req_event;                                       \
1148c2ecf20Sopenharmony_ci    RING_IDX rsp_prod, rsp_event;                                       \
1158c2ecf20Sopenharmony_ci    uint8_t __pad[48];                                                  \
1168c2ecf20Sopenharmony_ci    union __name##_sring_entry ring[1]; /* variable-length */           \
1178c2ecf20Sopenharmony_ci};                                                                      \
1188c2ecf20Sopenharmony_ci                                                                        \
1198c2ecf20Sopenharmony_ci/* "Front" end's private variables */                                   \
1208c2ecf20Sopenharmony_cistruct __name##_front_ring {                                            \
1218c2ecf20Sopenharmony_ci    RING_IDX req_prod_pvt;                                              \
1228c2ecf20Sopenharmony_ci    RING_IDX rsp_cons;                                                  \
1238c2ecf20Sopenharmony_ci    unsigned int nr_ents;                                               \
1248c2ecf20Sopenharmony_ci    struct __name##_sring *sring;                                       \
1258c2ecf20Sopenharmony_ci};                                                                      \
1268c2ecf20Sopenharmony_ci                                                                        \
1278c2ecf20Sopenharmony_ci/* "Back" end's private variables */                                    \
1288c2ecf20Sopenharmony_cistruct __name##_back_ring {                                             \
1298c2ecf20Sopenharmony_ci    RING_IDX rsp_prod_pvt;                                              \
1308c2ecf20Sopenharmony_ci    RING_IDX req_cons;                                                  \
1318c2ecf20Sopenharmony_ci    unsigned int nr_ents;                                               \
1328c2ecf20Sopenharmony_ci    struct __name##_sring *sring;                                       \
1338c2ecf20Sopenharmony_ci};                                                                      \
1348c2ecf20Sopenharmony_ci                                                                        \
1358c2ecf20Sopenharmony_ci/*
1368c2ecf20Sopenharmony_ci * Macros for manipulating rings.
1378c2ecf20Sopenharmony_ci *
1388c2ecf20Sopenharmony_ci * FRONT_RING_whatever works on the "front end" of a ring: here
1398c2ecf20Sopenharmony_ci * requests are pushed on to the ring and responses taken off it.
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * BACK_RING_whatever works on the "back end" of a ring: here
1428c2ecf20Sopenharmony_ci * requests are taken off the ring and responses put on.
1438c2ecf20Sopenharmony_ci *
1448c2ecf20Sopenharmony_ci * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
1458c2ecf20Sopenharmony_ci * This is OK in 1-for-1 request-response situations where the
1468c2ecf20Sopenharmony_ci * requestor (front end) never has more than RING_SIZE()-1
1478c2ecf20Sopenharmony_ci * outstanding requests.
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/* Initialising empty rings */
1518c2ecf20Sopenharmony_ci#define SHARED_RING_INIT(_s) do {                                       \
1528c2ecf20Sopenharmony_ci    (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
1538c2ecf20Sopenharmony_ci    (_s)->req_event = (_s)->rsp_event = 1;                              \
1548c2ecf20Sopenharmony_ci    (void)memset((_s)->__pad, 0, sizeof((_s)->__pad));                  \
1558c2ecf20Sopenharmony_ci} while(0)
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#define FRONT_RING_ATTACH(_r, _s, _i, __size) do {                      \
1588c2ecf20Sopenharmony_ci    (_r)->req_prod_pvt = (_i);                                          \
1598c2ecf20Sopenharmony_ci    (_r)->rsp_cons = (_i);                                              \
1608c2ecf20Sopenharmony_ci    (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
1618c2ecf20Sopenharmony_ci    (_r)->sring = (_s);                                                 \
1628c2ecf20Sopenharmony_ci} while (0)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __size)
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci#define BACK_RING_ATTACH(_r, _s, _i, __size) do {                       \
1678c2ecf20Sopenharmony_ci    (_r)->rsp_prod_pvt = (_i);                                          \
1688c2ecf20Sopenharmony_ci    (_r)->req_cons = (_i);                                              \
1698c2ecf20Sopenharmony_ci    (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
1708c2ecf20Sopenharmony_ci    (_r)->sring = (_s);                                                 \
1718c2ecf20Sopenharmony_ci} while (0)
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size)
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/* How big is this ring? */
1768c2ecf20Sopenharmony_ci#define RING_SIZE(_r)                                                   \
1778c2ecf20Sopenharmony_ci    ((_r)->nr_ents)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* Number of free requests (for use on front side only). */
1808c2ecf20Sopenharmony_ci#define RING_FREE_REQUESTS(_r)                                          \
1818c2ecf20Sopenharmony_ci    (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* Test if there is an empty slot available on the front ring.
1848c2ecf20Sopenharmony_ci * (This is only meaningful from the front. )
1858c2ecf20Sopenharmony_ci */
1868c2ecf20Sopenharmony_ci#define RING_FULL(_r)                                                   \
1878c2ecf20Sopenharmony_ci    (RING_FREE_REQUESTS(_r) == 0)
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/* Test if there are outstanding messages to be processed on a ring. */
1908c2ecf20Sopenharmony_ci#define RING_HAS_UNCONSUMED_RESPONSES(_r)                               \
1918c2ecf20Sopenharmony_ci    ((_r)->sring->rsp_prod - (_r)->rsp_cons)
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci#define RING_HAS_UNCONSUMED_REQUESTS(_r) ({                             \
1948c2ecf20Sopenharmony_ci    unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
1958c2ecf20Sopenharmony_ci    unsigned int rsp = RING_SIZE(_r) -                                  \
1968c2ecf20Sopenharmony_ci        ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
1978c2ecf20Sopenharmony_ci    req < rsp ? req : rsp;                                              \
1988c2ecf20Sopenharmony_ci})
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/* Direct access to individual ring elements, by index. */
2018c2ecf20Sopenharmony_ci#define RING_GET_REQUEST(_r, _idx)                                      \
2028c2ecf20Sopenharmony_ci    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#define RING_GET_RESPONSE(_r, _idx)                                     \
2058c2ecf20Sopenharmony_ci    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/*
2088c2ecf20Sopenharmony_ci * Get a local copy of a request/response.
2098c2ecf20Sopenharmony_ci *
2108c2ecf20Sopenharmony_ci * Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is
2118c2ecf20Sopenharmony_ci * done on a local copy that cannot be modified by the other end.
2128c2ecf20Sopenharmony_ci *
2138c2ecf20Sopenharmony_ci * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
2148c2ecf20Sopenharmony_ci * to be ineffective where dest is a struct which consists of only bitfields.
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_ci#define RING_COPY_(type, r, idx, dest) do {				\
2178c2ecf20Sopenharmony_ci	/* Use volatile to force the copy into dest. */			\
2188c2ecf20Sopenharmony_ci	*(dest) = *(volatile typeof(dest))RING_GET_##type(r, idx);	\
2198c2ecf20Sopenharmony_ci} while (0)
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci#define RING_COPY_REQUEST(r, idx, req)  RING_COPY_(REQUEST, r, idx, req)
2228c2ecf20Sopenharmony_ci#define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp)
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/* Loop termination condition: Would the specified index overflow the ring? */
2258c2ecf20Sopenharmony_ci#define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
2268c2ecf20Sopenharmony_ci    (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/* Ill-behaved frontend determination: Can there be this many requests? */
2298c2ecf20Sopenharmony_ci#define RING_REQUEST_PROD_OVERFLOW(_r, _prod)                           \
2308c2ecf20Sopenharmony_ci    (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/* Ill-behaved backend determination: Can there be this many responses? */
2338c2ecf20Sopenharmony_ci#define RING_RESPONSE_PROD_OVERFLOW(_r, _prod)                          \
2348c2ecf20Sopenharmony_ci    (((_prod) - (_r)->rsp_cons) > RING_SIZE(_r))
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci#define RING_PUSH_REQUESTS(_r) do {                                     \
2378c2ecf20Sopenharmony_ci    virt_wmb(); /* back sees requests /before/ updated producer index */\
2388c2ecf20Sopenharmony_ci    (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
2398c2ecf20Sopenharmony_ci} while (0)
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci#define RING_PUSH_RESPONSES(_r) do {                                    \
2428c2ecf20Sopenharmony_ci    virt_wmb(); /* front sees resps /before/ updated producer index */  \
2438c2ecf20Sopenharmony_ci    (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
2448c2ecf20Sopenharmony_ci} while (0)
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/*
2478c2ecf20Sopenharmony_ci * Notification hold-off (req_event and rsp_event):
2488c2ecf20Sopenharmony_ci *
2498c2ecf20Sopenharmony_ci * When queueing requests or responses on a shared ring, it may not always be
2508c2ecf20Sopenharmony_ci * necessary to notify the remote end. For example, if requests are in flight
2518c2ecf20Sopenharmony_ci * in a backend, the front may be able to queue further requests without
2528c2ecf20Sopenharmony_ci * notifying the back (if the back checks for new requests when it queues
2538c2ecf20Sopenharmony_ci * responses).
2548c2ecf20Sopenharmony_ci *
2558c2ecf20Sopenharmony_ci * When enqueuing requests or responses:
2568c2ecf20Sopenharmony_ci *
2578c2ecf20Sopenharmony_ci *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
2588c2ecf20Sopenharmony_ci *  is a boolean return value. True indicates that the receiver requires an
2598c2ecf20Sopenharmony_ci *  asynchronous notification.
2608c2ecf20Sopenharmony_ci *
2618c2ecf20Sopenharmony_ci * After dequeuing requests or responses (before sleeping the connection):
2628c2ecf20Sopenharmony_ci *
2638c2ecf20Sopenharmony_ci *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
2648c2ecf20Sopenharmony_ci *  The second argument is a boolean return value. True indicates that there
2658c2ecf20Sopenharmony_ci *  are pending messages on the ring (i.e., the connection should not be put
2668c2ecf20Sopenharmony_ci *  to sleep).
2678c2ecf20Sopenharmony_ci *
2688c2ecf20Sopenharmony_ci *  These macros will set the req_event/rsp_event field to trigger a
2698c2ecf20Sopenharmony_ci *  notification on the very next message that is enqueued. If you want to
2708c2ecf20Sopenharmony_ci *  create batches of work (i.e., only receive a notification after several
2718c2ecf20Sopenharmony_ci *  messages have been enqueued) then you will need to create a customised
2728c2ecf20Sopenharmony_ci *  version of the FINAL_CHECK macro in your own code, which sets the event
2738c2ecf20Sopenharmony_ci *  field appropriately.
2748c2ecf20Sopenharmony_ci */
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
2778c2ecf20Sopenharmony_ci    RING_IDX __old = (_r)->sring->req_prod;                             \
2788c2ecf20Sopenharmony_ci    RING_IDX __new = (_r)->req_prod_pvt;                                \
2798c2ecf20Sopenharmony_ci    virt_wmb(); /* back sees requests /before/ updated producer index */\
2808c2ecf20Sopenharmony_ci    (_r)->sring->req_prod = __new;                                      \
2818c2ecf20Sopenharmony_ci    virt_mb(); /* back sees new requests /before/ we check req_event */ \
2828c2ecf20Sopenharmony_ci    (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
2838c2ecf20Sopenharmony_ci                 (RING_IDX)(__new - __old));                            \
2848c2ecf20Sopenharmony_ci} while (0)
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
2878c2ecf20Sopenharmony_ci    RING_IDX __old = (_r)->sring->rsp_prod;                             \
2888c2ecf20Sopenharmony_ci    RING_IDX __new = (_r)->rsp_prod_pvt;                                \
2898c2ecf20Sopenharmony_ci    virt_wmb(); /* front sees resps /before/ updated producer index */  \
2908c2ecf20Sopenharmony_ci    (_r)->sring->rsp_prod = __new;                                      \
2918c2ecf20Sopenharmony_ci    virt_mb(); /* front sees new resps /before/ we check rsp_event */   \
2928c2ecf20Sopenharmony_ci    (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
2938c2ecf20Sopenharmony_ci                 (RING_IDX)(__new - __old));                            \
2948c2ecf20Sopenharmony_ci} while (0)
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
2978c2ecf20Sopenharmony_ci    (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
2988c2ecf20Sopenharmony_ci    if (_work_to_do) break;                                             \
2998c2ecf20Sopenharmony_ci    (_r)->sring->req_event = (_r)->req_cons + 1;                        \
3008c2ecf20Sopenharmony_ci    virt_mb();                                                          \
3018c2ecf20Sopenharmony_ci    (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
3028c2ecf20Sopenharmony_ci} while (0)
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
3058c2ecf20Sopenharmony_ci    (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
3068c2ecf20Sopenharmony_ci    if (_work_to_do) break;                                             \
3078c2ecf20Sopenharmony_ci    (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
3088c2ecf20Sopenharmony_ci    virt_mb();                                                          \
3098c2ecf20Sopenharmony_ci    (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
3108c2ecf20Sopenharmony_ci} while (0)
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci/*
3148c2ecf20Sopenharmony_ci * DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and
3158c2ecf20Sopenharmony_ci * functions to check if there is data on the ring, and to read and
3168c2ecf20Sopenharmony_ci * write to them.
3178c2ecf20Sopenharmony_ci *
3188c2ecf20Sopenharmony_ci * DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but
3198c2ecf20Sopenharmony_ci * does not define the indexes page. As different protocols can have
3208c2ecf20Sopenharmony_ci * extensions to the basic format, this macro allow them to define their
3218c2ecf20Sopenharmony_ci * own struct.
3228c2ecf20Sopenharmony_ci *
3238c2ecf20Sopenharmony_ci * XEN_FLEX_RING_SIZE
3248c2ecf20Sopenharmony_ci *   Convenience macro to calculate the size of one of the two rings
3258c2ecf20Sopenharmony_ci *   from the overall order.
3268c2ecf20Sopenharmony_ci *
3278c2ecf20Sopenharmony_ci * $NAME_mask
3288c2ecf20Sopenharmony_ci *   Function to apply the size mask to an index, to reduce the index
3298c2ecf20Sopenharmony_ci *   within the range [0-size].
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci * $NAME_read_packet
3328c2ecf20Sopenharmony_ci *   Function to read data from the ring. The amount of data to read is
3338c2ecf20Sopenharmony_ci *   specified by the "size" argument.
3348c2ecf20Sopenharmony_ci *
3358c2ecf20Sopenharmony_ci * $NAME_write_packet
3368c2ecf20Sopenharmony_ci *   Function to write data to the ring. The amount of data to write is
3378c2ecf20Sopenharmony_ci *   specified by the "size" argument.
3388c2ecf20Sopenharmony_ci *
3398c2ecf20Sopenharmony_ci * $NAME_get_ring_ptr
3408c2ecf20Sopenharmony_ci *   Convenience function that returns a pointer to read/write to the
3418c2ecf20Sopenharmony_ci *   ring at the right location.
3428c2ecf20Sopenharmony_ci *
3438c2ecf20Sopenharmony_ci * $NAME_data_intf
3448c2ecf20Sopenharmony_ci *   Indexes page, shared between frontend and backend. It also
3458c2ecf20Sopenharmony_ci *   contains the array of grant refs.
3468c2ecf20Sopenharmony_ci *
3478c2ecf20Sopenharmony_ci * $NAME_queued
3488c2ecf20Sopenharmony_ci *   Function to calculate how many bytes are currently on the ring,
3498c2ecf20Sopenharmony_ci *   ready to be read. It can also be used to calculate how much free
3508c2ecf20Sopenharmony_ci *   space is currently on the ring (XEN_FLEX_RING_SIZE() -
3518c2ecf20Sopenharmony_ci *   $NAME_queued()).
3528c2ecf20Sopenharmony_ci */
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci#ifndef XEN_PAGE_SHIFT
3558c2ecf20Sopenharmony_ci/* The PAGE_SIZE for ring protocols and hypercall interfaces is always
3568c2ecf20Sopenharmony_ci * 4K, regardless of the architecture, and page granularity chosen by
3578c2ecf20Sopenharmony_ci * operating systems.
3588c2ecf20Sopenharmony_ci */
3598c2ecf20Sopenharmony_ci#define XEN_PAGE_SHIFT 12
3608c2ecf20Sopenharmony_ci#endif
3618c2ecf20Sopenharmony_ci#define XEN_FLEX_RING_SIZE(order)                                             \
3628c2ecf20Sopenharmony_ci    (1UL << ((order) + XEN_PAGE_SHIFT - 1))
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci#define DEFINE_XEN_FLEX_RING(name)                                            \
3658c2ecf20Sopenharmony_cistatic inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size)          \
3668c2ecf20Sopenharmony_ci{                                                                             \
3678c2ecf20Sopenharmony_ci    return idx & (ring_size - 1);                                             \
3688c2ecf20Sopenharmony_ci}                                                                             \
3698c2ecf20Sopenharmony_ci                                                                              \
3708c2ecf20Sopenharmony_cistatic inline unsigned char *name##_get_ring_ptr(unsigned char *buf,          \
3718c2ecf20Sopenharmony_ci                                                 RING_IDX idx,                \
3728c2ecf20Sopenharmony_ci                                                 RING_IDX ring_size)          \
3738c2ecf20Sopenharmony_ci{                                                                             \
3748c2ecf20Sopenharmony_ci    return buf + name##_mask(idx, ring_size);                                 \
3758c2ecf20Sopenharmony_ci}                                                                             \
3768c2ecf20Sopenharmony_ci                                                                              \
3778c2ecf20Sopenharmony_cistatic inline void name##_read_packet(void *opaque,                           \
3788c2ecf20Sopenharmony_ci                                      const unsigned char *buf,               \
3798c2ecf20Sopenharmony_ci                                      size_t size,                            \
3808c2ecf20Sopenharmony_ci                                      RING_IDX masked_prod,                   \
3818c2ecf20Sopenharmony_ci                                      RING_IDX *masked_cons,                  \
3828c2ecf20Sopenharmony_ci                                      RING_IDX ring_size)                     \
3838c2ecf20Sopenharmony_ci{                                                                             \
3848c2ecf20Sopenharmony_ci    if (*masked_cons < masked_prod ||                                         \
3858c2ecf20Sopenharmony_ci        size <= ring_size - *masked_cons) {                                   \
3868c2ecf20Sopenharmony_ci        memcpy(opaque, buf + *masked_cons, size);                             \
3878c2ecf20Sopenharmony_ci    } else {                                                                  \
3888c2ecf20Sopenharmony_ci        memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons);         \
3898c2ecf20Sopenharmony_ci        memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf,       \
3908c2ecf20Sopenharmony_ci               size - (ring_size - *masked_cons));                            \
3918c2ecf20Sopenharmony_ci    }                                                                         \
3928c2ecf20Sopenharmony_ci    *masked_cons = name##_mask(*masked_cons + size, ring_size);               \
3938c2ecf20Sopenharmony_ci}                                                                             \
3948c2ecf20Sopenharmony_ci                                                                              \
3958c2ecf20Sopenharmony_cistatic inline void name##_write_packet(unsigned char *buf,                    \
3968c2ecf20Sopenharmony_ci                                       const void *opaque,                    \
3978c2ecf20Sopenharmony_ci                                       size_t size,                           \
3988c2ecf20Sopenharmony_ci                                       RING_IDX *masked_prod,                 \
3998c2ecf20Sopenharmony_ci                                       RING_IDX masked_cons,                  \
4008c2ecf20Sopenharmony_ci                                       RING_IDX ring_size)                    \
4018c2ecf20Sopenharmony_ci{                                                                             \
4028c2ecf20Sopenharmony_ci    if (*masked_prod < masked_cons ||                                         \
4038c2ecf20Sopenharmony_ci        size <= ring_size - *masked_prod) {                                   \
4048c2ecf20Sopenharmony_ci        memcpy(buf + *masked_prod, opaque, size);                             \
4058c2ecf20Sopenharmony_ci    } else {                                                                  \
4068c2ecf20Sopenharmony_ci        memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod);         \
4078c2ecf20Sopenharmony_ci        memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod),     \
4088c2ecf20Sopenharmony_ci               size - (ring_size - *masked_prod));                            \
4098c2ecf20Sopenharmony_ci    }                                                                         \
4108c2ecf20Sopenharmony_ci    *masked_prod = name##_mask(*masked_prod + size, ring_size);               \
4118c2ecf20Sopenharmony_ci}                                                                             \
4128c2ecf20Sopenharmony_ci                                                                              \
4138c2ecf20Sopenharmony_cistatic inline RING_IDX name##_queued(RING_IDX prod,                           \
4148c2ecf20Sopenharmony_ci                                     RING_IDX cons,                           \
4158c2ecf20Sopenharmony_ci                                     RING_IDX ring_size)                      \
4168c2ecf20Sopenharmony_ci{                                                                             \
4178c2ecf20Sopenharmony_ci    RING_IDX size;                                                            \
4188c2ecf20Sopenharmony_ci                                                                              \
4198c2ecf20Sopenharmony_ci    if (prod == cons)                                                         \
4208c2ecf20Sopenharmony_ci        return 0;                                                             \
4218c2ecf20Sopenharmony_ci                                                                              \
4228c2ecf20Sopenharmony_ci    prod = name##_mask(prod, ring_size);                                      \
4238c2ecf20Sopenharmony_ci    cons = name##_mask(cons, ring_size);                                      \
4248c2ecf20Sopenharmony_ci                                                                              \
4258c2ecf20Sopenharmony_ci    if (prod == cons)                                                         \
4268c2ecf20Sopenharmony_ci        return ring_size;                                                     \
4278c2ecf20Sopenharmony_ci                                                                              \
4288c2ecf20Sopenharmony_ci    if (prod > cons)                                                          \
4298c2ecf20Sopenharmony_ci        size = prod - cons;                                                   \
4308c2ecf20Sopenharmony_ci    else                                                                      \
4318c2ecf20Sopenharmony_ci        size = ring_size - (cons - prod);                                     \
4328c2ecf20Sopenharmony_ci    return size;                                                              \
4338c2ecf20Sopenharmony_ci}                                                                             \
4348c2ecf20Sopenharmony_ci                                                                              \
4358c2ecf20Sopenharmony_cistruct name##_data {                                                          \
4368c2ecf20Sopenharmony_ci    unsigned char *in; /* half of the allocation */                           \
4378c2ecf20Sopenharmony_ci    unsigned char *out; /* half of the allocation */                          \
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci#define DEFINE_XEN_FLEX_RING_AND_INTF(name)                                   \
4418c2ecf20Sopenharmony_cistruct name##_data_intf {                                                     \
4428c2ecf20Sopenharmony_ci    RING_IDX in_cons, in_prod;                                                \
4438c2ecf20Sopenharmony_ci                                                                              \
4448c2ecf20Sopenharmony_ci    uint8_t pad1[56];                                                         \
4458c2ecf20Sopenharmony_ci                                                                              \
4468c2ecf20Sopenharmony_ci    RING_IDX out_cons, out_prod;                                              \
4478c2ecf20Sopenharmony_ci                                                                              \
4488c2ecf20Sopenharmony_ci    uint8_t pad2[56];                                                         \
4498c2ecf20Sopenharmony_ci                                                                              \
4508c2ecf20Sopenharmony_ci    RING_IDX ring_order;                                                      \
4518c2ecf20Sopenharmony_ci    grant_ref_t ref[];                                                        \
4528c2ecf20Sopenharmony_ci};                                                                            \
4538c2ecf20Sopenharmony_ciDEFINE_XEN_FLEX_RING(name)
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci#endif /* __XEN_PUBLIC_IO_RING_H__ */
456