162306a36Sopenharmony_ci/* SPDX-License-Identifier: MIT */
262306a36Sopenharmony_ci/******************************************************************************
362306a36Sopenharmony_ci * ring.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Shared producer-consumer ring macros.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Tim Deegan and Andrew Warfield November 2004.
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef __XEN_PUBLIC_IO_RING_H__
1162306a36Sopenharmony_ci#define __XEN_PUBLIC_IO_RING_H__
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/*
1462306a36Sopenharmony_ci * When #include'ing this header, you need to provide the following
1562306a36Sopenharmony_ci * declaration upfront:
1662306a36Sopenharmony_ci * - standard integers types (uint8_t, uint16_t, etc)
1762306a36Sopenharmony_ci * They are provided by stdint.h of the standard headers.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * In addition, if you intend to use the FLEX macros, you also need to
2062306a36Sopenharmony_ci * provide the following, before invoking the FLEX macros:
2162306a36Sopenharmony_ci * - size_t
2262306a36Sopenharmony_ci * - memcpy
2362306a36Sopenharmony_ci * - grant_ref_t
2462306a36Sopenharmony_ci * These declarations are provided by string.h of the standard headers,
2562306a36Sopenharmony_ci * and grant_table.h from the Xen public headers.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include <xen/interface/grant_table.h>
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_citypedef unsigned int RING_IDX;
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* Round a 32-bit unsigned constant down to the nearest power of two. */
3362306a36Sopenharmony_ci#define __RD2(_x)  (((_x) & 0x00000002) ? 0x2                  : ((_x) & 0x1))
3462306a36Sopenharmony_ci#define __RD4(_x)  (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2    : __RD2(_x))
3562306a36Sopenharmony_ci#define __RD8(_x)  (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4    : __RD4(_x))
3662306a36Sopenharmony_ci#define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8    : __RD8(_x))
3762306a36Sopenharmony_ci#define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/*
4062306a36Sopenharmony_ci * Calculate size of a shared ring, given the total available space for the
4162306a36Sopenharmony_ci * ring and indexes (_sz), and the name tag of the request/response structure.
4262306a36Sopenharmony_ci * A ring contains as many entries as will fit, rounded down to the nearest
4362306a36Sopenharmony_ci * power of two (so we can mask with (size-1) to loop around).
4462306a36Sopenharmony_ci */
4562306a36Sopenharmony_ci#define __CONST_RING_SIZE(_s, _sz) \
4662306a36Sopenharmony_ci    (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
4762306a36Sopenharmony_ci	    sizeof(((struct _s##_sring *)0)->ring[0])))
4862306a36Sopenharmony_ci/*
4962306a36Sopenharmony_ci * The same for passing in an actual pointer instead of a name tag.
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_ci#define __RING_SIZE(_s, _sz) \
5262306a36Sopenharmony_ci    (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/*
5562306a36Sopenharmony_ci * Macros to make the correct C datatypes for a new kind of ring.
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * To make a new ring datatype, you need to have two message structures,
5862306a36Sopenharmony_ci * let's say request_t, and response_t already defined.
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * In a header where you want the ring datatype declared, you then do:
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci *     DEFINE_RING_TYPES(mytag, request_t, response_t);
6362306a36Sopenharmony_ci *
6462306a36Sopenharmony_ci * These expand out to give you a set of types, as you can see below.
6562306a36Sopenharmony_ci * The most important of these are:
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci *     mytag_sring_t      - The shared ring.
6862306a36Sopenharmony_ci *     mytag_front_ring_t - The 'front' half of the ring.
6962306a36Sopenharmony_ci *     mytag_back_ring_t  - The 'back' half of the ring.
7062306a36Sopenharmony_ci *
7162306a36Sopenharmony_ci * To initialize a ring in your code you need to know the location and size
7262306a36Sopenharmony_ci * of the shared memory area (PAGE_SIZE, for instance). To initialise
7362306a36Sopenharmony_ci * the front half:
7462306a36Sopenharmony_ci *
7562306a36Sopenharmony_ci *     mytag_front_ring_t ring;
7662306a36Sopenharmony_ci *     XEN_FRONT_RING_INIT(&ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
7762306a36Sopenharmony_ci *
7862306a36Sopenharmony_ci * Initializing the back follows similarly (note that only the front
7962306a36Sopenharmony_ci * initializes the shared ring):
8062306a36Sopenharmony_ci *
8162306a36Sopenharmony_ci *     mytag_back_ring_t back_ring;
8262306a36Sopenharmony_ci *     BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t)                     \
8662306a36Sopenharmony_ci                                                                        \
8762306a36Sopenharmony_ci/* Shared ring entry */                                                 \
8862306a36Sopenharmony_ciunion __name##_sring_entry {                                            \
8962306a36Sopenharmony_ci    __req_t req;                                                        \
9062306a36Sopenharmony_ci    __rsp_t rsp;                                                        \
9162306a36Sopenharmony_ci};                                                                      \
9262306a36Sopenharmony_ci                                                                        \
9362306a36Sopenharmony_ci/* Shared ring page */                                                  \
9462306a36Sopenharmony_cistruct __name##_sring {                                                 \
9562306a36Sopenharmony_ci    RING_IDX req_prod, req_event;                                       \
9662306a36Sopenharmony_ci    RING_IDX rsp_prod, rsp_event;                                       \
9762306a36Sopenharmony_ci    uint8_t __pad[48];                                                  \
9862306a36Sopenharmony_ci    union __name##_sring_entry ring[1]; /* variable-length */           \
9962306a36Sopenharmony_ci};                                                                      \
10062306a36Sopenharmony_ci                                                                        \
10162306a36Sopenharmony_ci/* "Front" end's private variables */                                   \
10262306a36Sopenharmony_cistruct __name##_front_ring {                                            \
10362306a36Sopenharmony_ci    RING_IDX req_prod_pvt;                                              \
10462306a36Sopenharmony_ci    RING_IDX rsp_cons;                                                  \
10562306a36Sopenharmony_ci    unsigned int nr_ents;                                               \
10662306a36Sopenharmony_ci    struct __name##_sring *sring;                                       \
10762306a36Sopenharmony_ci};                                                                      \
10862306a36Sopenharmony_ci                                                                        \
10962306a36Sopenharmony_ci/* "Back" end's private variables */                                    \
11062306a36Sopenharmony_cistruct __name##_back_ring {                                             \
11162306a36Sopenharmony_ci    RING_IDX rsp_prod_pvt;                                              \
11262306a36Sopenharmony_ci    RING_IDX req_cons;                                                  \
11362306a36Sopenharmony_ci    unsigned int nr_ents;                                               \
11462306a36Sopenharmony_ci    struct __name##_sring *sring;                                       \
11562306a36Sopenharmony_ci};                                                                      \
11662306a36Sopenharmony_ci                                                                        \
11762306a36Sopenharmony_ci/*
11862306a36Sopenharmony_ci * Macros for manipulating rings.
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci * FRONT_RING_whatever works on the "front end" of a ring: here
12162306a36Sopenharmony_ci * requests are pushed on to the ring and responses taken off it.
12262306a36Sopenharmony_ci *
12362306a36Sopenharmony_ci * BACK_RING_whatever works on the "back end" of a ring: here
12462306a36Sopenharmony_ci * requests are taken off the ring and responses put on.
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
12762306a36Sopenharmony_ci * This is OK in 1-for-1 request-response situations where the
12862306a36Sopenharmony_ci * requestor (front end) never has more than RING_SIZE()-1
12962306a36Sopenharmony_ci * outstanding requests.
13062306a36Sopenharmony_ci */
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci/* Initialising empty rings */
13362306a36Sopenharmony_ci#define SHARED_RING_INIT(_s) do {                                       \
13462306a36Sopenharmony_ci    (_s)->req_prod  = (_s)->rsp_prod  = 0;                              \
13562306a36Sopenharmony_ci    (_s)->req_event = (_s)->rsp_event = 1;                              \
13662306a36Sopenharmony_ci    (void)memset((_s)->__pad, 0, sizeof((_s)->__pad));                  \
13762306a36Sopenharmony_ci} while(0)
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#define FRONT_RING_ATTACH(_r, _s, _i, __size) do {                      \
14062306a36Sopenharmony_ci    (_r)->req_prod_pvt = (_i);                                          \
14162306a36Sopenharmony_ci    (_r)->rsp_cons = (_i);                                              \
14262306a36Sopenharmony_ci    (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
14362306a36Sopenharmony_ci    (_r)->sring = (_s);                                                 \
14462306a36Sopenharmony_ci} while (0)
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __size)
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci#define XEN_FRONT_RING_INIT(r, s, size) do {                            \
14962306a36Sopenharmony_ci    SHARED_RING_INIT(s);                                                \
15062306a36Sopenharmony_ci    FRONT_RING_INIT(r, s, size);                                        \
15162306a36Sopenharmony_ci} while (0)
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci#define BACK_RING_ATTACH(_r, _s, _i, __size) do {                       \
15462306a36Sopenharmony_ci    (_r)->rsp_prod_pvt = (_i);                                          \
15562306a36Sopenharmony_ci    (_r)->req_cons = (_i);                                              \
15662306a36Sopenharmony_ci    (_r)->nr_ents = __RING_SIZE(_s, __size);                            \
15762306a36Sopenharmony_ci    (_r)->sring = (_s);                                                 \
15862306a36Sopenharmony_ci} while (0)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci#define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size)
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci/* How big is this ring? */
16362306a36Sopenharmony_ci#define RING_SIZE(_r)                                                   \
16462306a36Sopenharmony_ci    ((_r)->nr_ents)
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci/* Number of free requests (for use on front side only). */
16762306a36Sopenharmony_ci#define RING_FREE_REQUESTS(_r)                                          \
16862306a36Sopenharmony_ci    (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci/* Test if there is an empty slot available on the front ring.
17162306a36Sopenharmony_ci * (This is only meaningful from the front. )
17262306a36Sopenharmony_ci */
17362306a36Sopenharmony_ci#define RING_FULL(_r)                                                   \
17462306a36Sopenharmony_ci    (RING_FREE_REQUESTS(_r) == 0)
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci/* Test if there are outstanding messages to be processed on a ring. */
17762306a36Sopenharmony_ci#define XEN_RING_NR_UNCONSUMED_RESPONSES(_r)                            \
17862306a36Sopenharmony_ci    ((_r)->sring->rsp_prod - (_r)->rsp_cons)
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci#define XEN_RING_NR_UNCONSUMED_REQUESTS(_r) ({                          \
18162306a36Sopenharmony_ci    unsigned int req = (_r)->sring->req_prod - (_r)->req_cons;          \
18262306a36Sopenharmony_ci    unsigned int rsp = RING_SIZE(_r) -                                  \
18362306a36Sopenharmony_ci        ((_r)->req_cons - (_r)->rsp_prod_pvt);                          \
18462306a36Sopenharmony_ci    req < rsp ? req : rsp;                                              \
18562306a36Sopenharmony_ci})
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci#define RING_HAS_UNCONSUMED_RESPONSES(_r) \
18862306a36Sopenharmony_ci    (!!XEN_RING_NR_UNCONSUMED_RESPONSES(_r))
18962306a36Sopenharmony_ci#define RING_HAS_UNCONSUMED_REQUESTS(_r)  \
19062306a36Sopenharmony_ci    (!!XEN_RING_NR_UNCONSUMED_REQUESTS(_r))
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci/* Direct access to individual ring elements, by index. */
19362306a36Sopenharmony_ci#define RING_GET_REQUEST(_r, _idx)                                      \
19462306a36Sopenharmony_ci    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci#define RING_GET_RESPONSE(_r, _idx)                                     \
19762306a36Sopenharmony_ci    (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci/*
20062306a36Sopenharmony_ci * Get a local copy of a request/response.
20162306a36Sopenharmony_ci *
20262306a36Sopenharmony_ci * Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is
20362306a36Sopenharmony_ci * done on a local copy that cannot be modified by the other end.
20462306a36Sopenharmony_ci *
20562306a36Sopenharmony_ci * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
20662306a36Sopenharmony_ci * to be ineffective where dest is a struct which consists of only bitfields.
20762306a36Sopenharmony_ci */
20862306a36Sopenharmony_ci#define RING_COPY_(type, r, idx, dest) do {				\
20962306a36Sopenharmony_ci	/* Use volatile to force the copy into dest. */			\
21062306a36Sopenharmony_ci	*(dest) = *(volatile typeof(dest))RING_GET_##type(r, idx);	\
21162306a36Sopenharmony_ci} while (0)
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci#define RING_COPY_REQUEST(r, idx, req)  RING_COPY_(REQUEST, r, idx, req)
21462306a36Sopenharmony_ci#define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp)
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci/* Loop termination condition: Would the specified index overflow the ring? */
21762306a36Sopenharmony_ci#define RING_REQUEST_CONS_OVERFLOW(_r, _cons)                           \
21862306a36Sopenharmony_ci    (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci/* Ill-behaved frontend determination: Can there be this many requests? */
22162306a36Sopenharmony_ci#define RING_REQUEST_PROD_OVERFLOW(_r, _prod)                           \
22262306a36Sopenharmony_ci    (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci/* Ill-behaved backend determination: Can there be this many responses? */
22562306a36Sopenharmony_ci#define RING_RESPONSE_PROD_OVERFLOW(_r, _prod)                          \
22662306a36Sopenharmony_ci    (((_prod) - (_r)->rsp_cons) > RING_SIZE(_r))
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci#define RING_PUSH_REQUESTS(_r) do {                                     \
22962306a36Sopenharmony_ci    virt_wmb(); /* back sees requests /before/ updated producer index */\
23062306a36Sopenharmony_ci    (_r)->sring->req_prod = (_r)->req_prod_pvt;                         \
23162306a36Sopenharmony_ci} while (0)
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci#define RING_PUSH_RESPONSES(_r) do {                                    \
23462306a36Sopenharmony_ci    virt_wmb(); /* front sees resps /before/ updated producer index */  \
23562306a36Sopenharmony_ci    (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt;                         \
23662306a36Sopenharmony_ci} while (0)
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci/*
23962306a36Sopenharmony_ci * Notification hold-off (req_event and rsp_event):
24062306a36Sopenharmony_ci *
24162306a36Sopenharmony_ci * When queueing requests or responses on a shared ring, it may not always be
24262306a36Sopenharmony_ci * necessary to notify the remote end. For example, if requests are in flight
24362306a36Sopenharmony_ci * in a backend, the front may be able to queue further requests without
24462306a36Sopenharmony_ci * notifying the back (if the back checks for new requests when it queues
24562306a36Sopenharmony_ci * responses).
24662306a36Sopenharmony_ci *
24762306a36Sopenharmony_ci * When enqueuing requests or responses:
24862306a36Sopenharmony_ci *
24962306a36Sopenharmony_ci *  Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
25062306a36Sopenharmony_ci *  is a boolean return value. True indicates that the receiver requires an
25162306a36Sopenharmony_ci *  asynchronous notification.
25262306a36Sopenharmony_ci *
25362306a36Sopenharmony_ci * After dequeuing requests or responses (before sleeping the connection):
25462306a36Sopenharmony_ci *
25562306a36Sopenharmony_ci *  Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
25662306a36Sopenharmony_ci *  The second argument is a boolean return value. True indicates that there
25762306a36Sopenharmony_ci *  are pending messages on the ring (i.e., the connection should not be put
25862306a36Sopenharmony_ci *  to sleep).
25962306a36Sopenharmony_ci *
26062306a36Sopenharmony_ci *  These macros will set the req_event/rsp_event field to trigger a
26162306a36Sopenharmony_ci *  notification on the very next message that is enqueued. If you want to
26262306a36Sopenharmony_ci *  create batches of work (i.e., only receive a notification after several
26362306a36Sopenharmony_ci *  messages have been enqueued) then you will need to create a customised
26462306a36Sopenharmony_ci *  version of the FINAL_CHECK macro in your own code, which sets the event
26562306a36Sopenharmony_ci *  field appropriately.
26662306a36Sopenharmony_ci */
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do {           \
26962306a36Sopenharmony_ci    RING_IDX __old = (_r)->sring->req_prod;                             \
27062306a36Sopenharmony_ci    RING_IDX __new = (_r)->req_prod_pvt;                                \
27162306a36Sopenharmony_ci    virt_wmb(); /* back sees requests /before/ updated producer index */\
27262306a36Sopenharmony_ci    (_r)->sring->req_prod = __new;                                      \
27362306a36Sopenharmony_ci    virt_mb(); /* back sees new requests /before/ we check req_event */ \
27462306a36Sopenharmony_ci    (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) <           \
27562306a36Sopenharmony_ci                 (RING_IDX)(__new - __old));                            \
27662306a36Sopenharmony_ci} while (0)
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do {          \
27962306a36Sopenharmony_ci    RING_IDX __old = (_r)->sring->rsp_prod;                             \
28062306a36Sopenharmony_ci    RING_IDX __new = (_r)->rsp_prod_pvt;                                \
28162306a36Sopenharmony_ci    virt_wmb(); /* front sees resps /before/ updated producer index */  \
28262306a36Sopenharmony_ci    (_r)->sring->rsp_prod = __new;                                      \
28362306a36Sopenharmony_ci    virt_mb(); /* front sees new resps /before/ we check rsp_event */   \
28462306a36Sopenharmony_ci    (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) <           \
28562306a36Sopenharmony_ci                 (RING_IDX)(__new - __old));                            \
28662306a36Sopenharmony_ci} while (0)
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do {             \
28962306a36Sopenharmony_ci    (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
29062306a36Sopenharmony_ci    if (_work_to_do) break;                                             \
29162306a36Sopenharmony_ci    (_r)->sring->req_event = (_r)->req_cons + 1;                        \
29262306a36Sopenharmony_ci    virt_mb();                                                          \
29362306a36Sopenharmony_ci    (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r);                   \
29462306a36Sopenharmony_ci} while (0)
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do {            \
29762306a36Sopenharmony_ci    (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
29862306a36Sopenharmony_ci    if (_work_to_do) break;                                             \
29962306a36Sopenharmony_ci    (_r)->sring->rsp_event = (_r)->rsp_cons + 1;                        \
30062306a36Sopenharmony_ci    virt_mb();                                                          \
30162306a36Sopenharmony_ci    (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r);                  \
30262306a36Sopenharmony_ci} while (0)
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci/*
30662306a36Sopenharmony_ci * DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and
30762306a36Sopenharmony_ci * functions to check if there is data on the ring, and to read and
30862306a36Sopenharmony_ci * write to them.
30962306a36Sopenharmony_ci *
31062306a36Sopenharmony_ci * DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but
31162306a36Sopenharmony_ci * does not define the indexes page. As different protocols can have
31262306a36Sopenharmony_ci * extensions to the basic format, this macro allow them to define their
31362306a36Sopenharmony_ci * own struct.
31462306a36Sopenharmony_ci *
31562306a36Sopenharmony_ci * XEN_FLEX_RING_SIZE
31662306a36Sopenharmony_ci *   Convenience macro to calculate the size of one of the two rings
31762306a36Sopenharmony_ci *   from the overall order.
31862306a36Sopenharmony_ci *
31962306a36Sopenharmony_ci * $NAME_mask
32062306a36Sopenharmony_ci *   Function to apply the size mask to an index, to reduce the index
32162306a36Sopenharmony_ci *   within the range [0-size].
32262306a36Sopenharmony_ci *
32362306a36Sopenharmony_ci * $NAME_read_packet
32462306a36Sopenharmony_ci *   Function to read data from the ring. The amount of data to read is
32562306a36Sopenharmony_ci *   specified by the "size" argument.
32662306a36Sopenharmony_ci *
32762306a36Sopenharmony_ci * $NAME_write_packet
32862306a36Sopenharmony_ci *   Function to write data to the ring. The amount of data to write is
32962306a36Sopenharmony_ci *   specified by the "size" argument.
33062306a36Sopenharmony_ci *
33162306a36Sopenharmony_ci * $NAME_get_ring_ptr
33262306a36Sopenharmony_ci *   Convenience function that returns a pointer to read/write to the
33362306a36Sopenharmony_ci *   ring at the right location.
33462306a36Sopenharmony_ci *
33562306a36Sopenharmony_ci * $NAME_data_intf
33662306a36Sopenharmony_ci *   Indexes page, shared between frontend and backend. It also
33762306a36Sopenharmony_ci *   contains the array of grant refs.
33862306a36Sopenharmony_ci *
33962306a36Sopenharmony_ci * $NAME_queued
34062306a36Sopenharmony_ci *   Function to calculate how many bytes are currently on the ring,
34162306a36Sopenharmony_ci *   ready to be read. It can also be used to calculate how much free
34262306a36Sopenharmony_ci *   space is currently on the ring (XEN_FLEX_RING_SIZE() -
34362306a36Sopenharmony_ci *   $NAME_queued()).
34462306a36Sopenharmony_ci */
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci#ifndef XEN_PAGE_SHIFT
34762306a36Sopenharmony_ci/* The PAGE_SIZE for ring protocols and hypercall interfaces is always
34862306a36Sopenharmony_ci * 4K, regardless of the architecture, and page granularity chosen by
34962306a36Sopenharmony_ci * operating systems.
35062306a36Sopenharmony_ci */
35162306a36Sopenharmony_ci#define XEN_PAGE_SHIFT 12
35262306a36Sopenharmony_ci#endif
35362306a36Sopenharmony_ci#define XEN_FLEX_RING_SIZE(order)                                             \
35462306a36Sopenharmony_ci    (1UL << ((order) + XEN_PAGE_SHIFT - 1))
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci#define DEFINE_XEN_FLEX_RING(name)                                            \
35762306a36Sopenharmony_cistatic inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size)          \
35862306a36Sopenharmony_ci{                                                                             \
35962306a36Sopenharmony_ci    return idx & (ring_size - 1);                                             \
36062306a36Sopenharmony_ci}                                                                             \
36162306a36Sopenharmony_ci                                                                              \
36262306a36Sopenharmony_cistatic inline unsigned char *name##_get_ring_ptr(unsigned char *buf,          \
36362306a36Sopenharmony_ci                                                 RING_IDX idx,                \
36462306a36Sopenharmony_ci                                                 RING_IDX ring_size)          \
36562306a36Sopenharmony_ci{                                                                             \
36662306a36Sopenharmony_ci    return buf + name##_mask(idx, ring_size);                                 \
36762306a36Sopenharmony_ci}                                                                             \
36862306a36Sopenharmony_ci                                                                              \
36962306a36Sopenharmony_cistatic inline void name##_read_packet(void *opaque,                           \
37062306a36Sopenharmony_ci                                      const unsigned char *buf,               \
37162306a36Sopenharmony_ci                                      size_t size,                            \
37262306a36Sopenharmony_ci                                      RING_IDX masked_prod,                   \
37362306a36Sopenharmony_ci                                      RING_IDX *masked_cons,                  \
37462306a36Sopenharmony_ci                                      RING_IDX ring_size)                     \
37562306a36Sopenharmony_ci{                                                                             \
37662306a36Sopenharmony_ci    if (*masked_cons < masked_prod ||                                         \
37762306a36Sopenharmony_ci        size <= ring_size - *masked_cons) {                                   \
37862306a36Sopenharmony_ci        memcpy(opaque, buf + *masked_cons, size);                             \
37962306a36Sopenharmony_ci    } else {                                                                  \
38062306a36Sopenharmony_ci        memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons);         \
38162306a36Sopenharmony_ci        memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf,       \
38262306a36Sopenharmony_ci               size - (ring_size - *masked_cons));                            \
38362306a36Sopenharmony_ci    }                                                                         \
38462306a36Sopenharmony_ci    *masked_cons = name##_mask(*masked_cons + size, ring_size);               \
38562306a36Sopenharmony_ci}                                                                             \
38662306a36Sopenharmony_ci                                                                              \
38762306a36Sopenharmony_cistatic inline void name##_write_packet(unsigned char *buf,                    \
38862306a36Sopenharmony_ci                                       const void *opaque,                    \
38962306a36Sopenharmony_ci                                       size_t size,                           \
39062306a36Sopenharmony_ci                                       RING_IDX *masked_prod,                 \
39162306a36Sopenharmony_ci                                       RING_IDX masked_cons,                  \
39262306a36Sopenharmony_ci                                       RING_IDX ring_size)                    \
39362306a36Sopenharmony_ci{                                                                             \
39462306a36Sopenharmony_ci    if (*masked_prod < masked_cons ||                                         \
39562306a36Sopenharmony_ci        size <= ring_size - *masked_prod) {                                   \
39662306a36Sopenharmony_ci        memcpy(buf + *masked_prod, opaque, size);                             \
39762306a36Sopenharmony_ci    } else {                                                                  \
39862306a36Sopenharmony_ci        memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod);         \
39962306a36Sopenharmony_ci        memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod),     \
40062306a36Sopenharmony_ci               size - (ring_size - *masked_prod));                            \
40162306a36Sopenharmony_ci    }                                                                         \
40262306a36Sopenharmony_ci    *masked_prod = name##_mask(*masked_prod + size, ring_size);               \
40362306a36Sopenharmony_ci}                                                                             \
40462306a36Sopenharmony_ci                                                                              \
40562306a36Sopenharmony_cistatic inline RING_IDX name##_queued(RING_IDX prod,                           \
40662306a36Sopenharmony_ci                                     RING_IDX cons,                           \
40762306a36Sopenharmony_ci                                     RING_IDX ring_size)                      \
40862306a36Sopenharmony_ci{                                                                             \
40962306a36Sopenharmony_ci    RING_IDX size;                                                            \
41062306a36Sopenharmony_ci                                                                              \
41162306a36Sopenharmony_ci    if (prod == cons)                                                         \
41262306a36Sopenharmony_ci        return 0;                                                             \
41362306a36Sopenharmony_ci                                                                              \
41462306a36Sopenharmony_ci    prod = name##_mask(prod, ring_size);                                      \
41562306a36Sopenharmony_ci    cons = name##_mask(cons, ring_size);                                      \
41662306a36Sopenharmony_ci                                                                              \
41762306a36Sopenharmony_ci    if (prod == cons)                                                         \
41862306a36Sopenharmony_ci        return ring_size;                                                     \
41962306a36Sopenharmony_ci                                                                              \
42062306a36Sopenharmony_ci    if (prod > cons)                                                          \
42162306a36Sopenharmony_ci        size = prod - cons;                                                   \
42262306a36Sopenharmony_ci    else                                                                      \
42362306a36Sopenharmony_ci        size = ring_size - (cons - prod);                                     \
42462306a36Sopenharmony_ci    return size;                                                              \
42562306a36Sopenharmony_ci}                                                                             \
42662306a36Sopenharmony_ci                                                                              \
42762306a36Sopenharmony_cistruct name##_data {                                                          \
42862306a36Sopenharmony_ci    unsigned char *in; /* half of the allocation */                           \
42962306a36Sopenharmony_ci    unsigned char *out; /* half of the allocation */                          \
43062306a36Sopenharmony_ci}
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_ci#define DEFINE_XEN_FLEX_RING_AND_INTF(name)                                   \
43362306a36Sopenharmony_cistruct name##_data_intf {                                                     \
43462306a36Sopenharmony_ci    RING_IDX in_cons, in_prod;                                                \
43562306a36Sopenharmony_ci                                                                              \
43662306a36Sopenharmony_ci    uint8_t pad1[56];                                                         \
43762306a36Sopenharmony_ci                                                                              \
43862306a36Sopenharmony_ci    RING_IDX out_cons, out_prod;                                              \
43962306a36Sopenharmony_ci                                                                              \
44062306a36Sopenharmony_ci    uint8_t pad2[56];                                                         \
44162306a36Sopenharmony_ci                                                                              \
44262306a36Sopenharmony_ci    RING_IDX ring_order;                                                      \
44362306a36Sopenharmony_ci    grant_ref_t ref[];                                                        \
44462306a36Sopenharmony_ci};                                                                            \
44562306a36Sopenharmony_ciDEFINE_XEN_FLEX_RING(name)
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci#endif /* __XEN_PUBLIC_IO_RING_H__ */
448