/* SPDX-License-Identifier: MIT */1/******************************************************************************2* ring.h3*4* Shared producer-consumer ring macros.5*6* Tim Deegan and Andrew Warfield November 2004.7*/89#ifndef __XEN_PUBLIC_IO_RING_H__10#define __XEN_PUBLIC_IO_RING_H__1112/*13* When #include'ing this header, you need to provide the following14* declaration upfront:15* - standard integers types (uint8_t, uint16_t, etc)16* They are provided by stdint.h of the standard headers.17*18* In addition, if you intend to use the FLEX macros, you also need to19* provide the following, before invoking the FLEX macros:20* - size_t21* - memcpy22* - grant_ref_t23* These declarations are provided by string.h of the standard headers,24* and grant_table.h from the Xen public headers.25*/2627#include <xen/interface/grant_table.h>2829typedef unsigned int RING_IDX;3031/* Round a 32-bit unsigned constant down to the nearest power of two. */32#define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))33#define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))34#define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))35#define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))36#define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))3738/*39* Calculate size of a shared ring, given the total available space for the40* ring and indexes (_sz), and the name tag of the request/response structure.41* A ring contains as many entries as will fit, rounded down to the nearest42* power of two (so we can mask with (size-1) to loop around).43*/44#define __CONST_RING_SIZE(_s, _sz) \45(__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \46sizeof(((struct _s##_sring *)0)->ring[0])))47/*48* The same for passing in an actual pointer instead of a name tag.49*/50#define __RING_SIZE(_s, _sz) \51(__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))5253/*54* Macros to make the correct C datatypes for a new kind of ring.55*56* To make a new ring datatype, you need to have two message structures,57* let's say request_t, and response_t already defined.58*59* In a header where you want the ring datatype declared, you then do:60*61* DEFINE_RING_TYPES(mytag, request_t, response_t);62*63* These expand out to give you a set of types, as you can see below.64* The most important of these are:65*66* mytag_sring_t - The shared ring.67* mytag_front_ring_t - The 'front' half of the ring.68* mytag_back_ring_t - The 'back' half of the ring.69*70* To initialize a ring in your code you need to know the location and size71* of the shared memory area (PAGE_SIZE, for instance). To initialise72* the front half:73*74* mytag_front_ring_t ring;75* XEN_FRONT_RING_INIT(&ring, (mytag_sring_t *)shared_page, PAGE_SIZE);76*77* Initializing the back follows similarly (note that only the front78* initializes the shared ring):79*80* mytag_back_ring_t back_ring;81* BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);82*/8384#define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \85\86/* Shared ring entry */ \87union __name##_sring_entry { \88__req_t req; \89__rsp_t rsp; \90}; \91\92/* Shared ring page */ \93struct __name##_sring { \94RING_IDX req_prod, req_event; \95RING_IDX rsp_prod, rsp_event; \96uint8_t __pad[48]; \97union __name##_sring_entry ring[]; \98}; \99\100/* "Front" end's private variables */ \101struct __name##_front_ring { \102RING_IDX req_prod_pvt; \103RING_IDX rsp_cons; \104unsigned int nr_ents; \105struct __name##_sring *sring; \106}; \107\108/* "Back" end's private variables */ \109struct __name##_back_ring { \110RING_IDX rsp_prod_pvt; \111RING_IDX req_cons; \112unsigned int nr_ents; \113struct __name##_sring *sring; \114}; \115\116/*117* Macros for manipulating rings.118*119* FRONT_RING_whatever works on the "front end" of a ring: here120* requests are pushed on to the ring and responses taken off it.121*122* BACK_RING_whatever works on the "back end" of a ring: here123* requests are taken off the ring and responses put on.124*125* N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.126* This is OK in 1-for-1 request-response situations where the127* requestor (front end) never has more than RING_SIZE()-1128* outstanding requests.129*/130131/* Initialising empty rings */132#define SHARED_RING_INIT(_s) do { \133(_s)->req_prod = (_s)->rsp_prod = 0; \134(_s)->req_event = (_s)->rsp_event = 1; \135(void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \136} while(0)137138#define FRONT_RING_ATTACH(_r, _s, _i, __size) do { \139(_r)->req_prod_pvt = (_i); \140(_r)->rsp_cons = (_i); \141(_r)->nr_ents = __RING_SIZE(_s, __size); \142(_r)->sring = (_s); \143} while (0)144145#define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __size)146147#define XEN_FRONT_RING_INIT(r, s, size) do { \148SHARED_RING_INIT(s); \149FRONT_RING_INIT(r, s, size); \150} while (0)151152#define BACK_RING_ATTACH(_r, _s, _i, __size) do { \153(_r)->rsp_prod_pvt = (_i); \154(_r)->req_cons = (_i); \155(_r)->nr_ents = __RING_SIZE(_s, __size); \156(_r)->sring = (_s); \157} while (0)158159#define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size)160161/* How big is this ring? */162#define RING_SIZE(_r) \163((_r)->nr_ents)164165/* Number of free requests (for use on front side only). */166#define RING_FREE_REQUESTS(_r) \167(RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))168169/* Test if there is an empty slot available on the front ring.170* (This is only meaningful from the front. )171*/172#define RING_FULL(_r) \173(RING_FREE_REQUESTS(_r) == 0)174175/* Test if there are outstanding messages to be processed on a ring. */176#define XEN_RING_NR_UNCONSUMED_RESPONSES(_r) \177((_r)->sring->rsp_prod - (_r)->rsp_cons)178179#define XEN_RING_NR_UNCONSUMED_REQUESTS(_r) ({ \180unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \181unsigned int rsp = RING_SIZE(_r) - \182((_r)->req_cons - (_r)->rsp_prod_pvt); \183req < rsp ? req : rsp; \184})185186#define RING_HAS_UNCONSUMED_RESPONSES(_r) \187(!!XEN_RING_NR_UNCONSUMED_RESPONSES(_r))188#define RING_HAS_UNCONSUMED_REQUESTS(_r) \189(!!XEN_RING_NR_UNCONSUMED_REQUESTS(_r))190191/* Direct access to individual ring elements, by index. */192#define RING_GET_REQUEST(_r, _idx) \193(&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))194195#define RING_GET_RESPONSE(_r, _idx) \196(&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))197198/*199* Get a local copy of a request/response.200*201* Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is202* done on a local copy that cannot be modified by the other end.203*204* Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this205* to be ineffective where dest is a struct which consists of only bitfields.206*/207#define RING_COPY_(type, r, idx, dest) do { \208/* Use volatile to force the copy into dest. */ \209*(dest) = *(volatile typeof(dest))RING_GET_##type(r, idx); \210} while (0)211212#define RING_COPY_REQUEST(r, idx, req) RING_COPY_(REQUEST, r, idx, req)213#define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp)214215/* Loop termination condition: Would the specified index overflow the ring? */216#define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \217(((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))218219/* Ill-behaved frontend determination: Can there be this many requests? */220#define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \221(((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))222223/* Ill-behaved backend determination: Can there be this many responses? */224#define RING_RESPONSE_PROD_OVERFLOW(_r, _prod) \225(((_prod) - (_r)->rsp_cons) > RING_SIZE(_r))226227#define RING_PUSH_REQUESTS(_r) do { \228virt_wmb(); /* back sees requests /before/ updated producer index */\229(_r)->sring->req_prod = (_r)->req_prod_pvt; \230} while (0)231232#define RING_PUSH_RESPONSES(_r) do { \233virt_wmb(); /* front sees resps /before/ updated producer index */ \234(_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \235} while (0)236237/*238* Notification hold-off (req_event and rsp_event):239*240* When queueing requests or responses on a shared ring, it may not always be241* necessary to notify the remote end. For example, if requests are in flight242* in a backend, the front may be able to queue further requests without243* notifying the back (if the back checks for new requests when it queues244* responses).245*246* When enqueuing requests or responses:247*248* Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument249* is a boolean return value. True indicates that the receiver requires an250* asynchronous notification.251*252* After dequeuing requests or responses (before sleeping the connection):253*254* Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().255* The second argument is a boolean return value. True indicates that there256* are pending messages on the ring (i.e., the connection should not be put257* to sleep).258*259* These macros will set the req_event/rsp_event field to trigger a260* notification on the very next message that is enqueued. If you want to261* create batches of work (i.e., only receive a notification after several262* messages have been enqueued) then you will need to create a customised263* version of the FINAL_CHECK macro in your own code, which sets the event264* field appropriately.265*/266267#define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \268RING_IDX __old = (_r)->sring->req_prod; \269RING_IDX __new = (_r)->req_prod_pvt; \270virt_wmb(); /* back sees requests /before/ updated producer index */\271(_r)->sring->req_prod = __new; \272virt_mb(); /* back sees new requests /before/ we check req_event */ \273(_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \274(RING_IDX)(__new - __old)); \275} while (0)276277#define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \278RING_IDX __old = (_r)->sring->rsp_prod; \279RING_IDX __new = (_r)->rsp_prod_pvt; \280virt_wmb(); /* front sees resps /before/ updated producer index */ \281(_r)->sring->rsp_prod = __new; \282virt_mb(); /* front sees new resps /before/ we check rsp_event */ \283(_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \284(RING_IDX)(__new - __old)); \285} while (0)286287#define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \288(_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \289if (_work_to_do) break; \290(_r)->sring->req_event = (_r)->req_cons + 1; \291virt_mb(); \292(_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \293} while (0)294295#define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \296(_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \297if (_work_to_do) break; \298(_r)->sring->rsp_event = (_r)->rsp_cons + 1; \299virt_mb(); \300(_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \301} while (0)302303304/*305* DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and306* functions to check if there is data on the ring, and to read and307* write to them.308*309* DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but310* does not define the indexes page. As different protocols can have311* extensions to the basic format, this macro allow them to define their312* own struct.313*314* XEN_FLEX_RING_SIZE315* Convenience macro to calculate the size of one of the two rings316* from the overall order.317*318* $NAME_mask319* Function to apply the size mask to an index, to reduce the index320* within the range [0-size].321*322* $NAME_read_packet323* Function to read data from the ring. The amount of data to read is324* specified by the "size" argument.325*326* $NAME_write_packet327* Function to write data to the ring. The amount of data to write is328* specified by the "size" argument.329*330* $NAME_get_ring_ptr331* Convenience function that returns a pointer to read/write to the332* ring at the right location.333*334* $NAME_data_intf335* Indexes page, shared between frontend and backend. It also336* contains the array of grant refs.337*338* $NAME_queued339* Function to calculate how many bytes are currently on the ring,340* ready to be read. It can also be used to calculate how much free341* space is currently on the ring (XEN_FLEX_RING_SIZE() -342* $NAME_queued()).343*/344345#ifndef XEN_PAGE_SHIFT346/* The PAGE_SIZE for ring protocols and hypercall interfaces is always347* 4K, regardless of the architecture, and page granularity chosen by348* operating systems.349*/350#define XEN_PAGE_SHIFT 12351#endif352#define XEN_FLEX_RING_SIZE(order) \353(1UL << ((order) + XEN_PAGE_SHIFT - 1))354355#define DEFINE_XEN_FLEX_RING(name) \356static inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size) \357{ \358return idx & (ring_size - 1); \359} \360\361static inline unsigned char *name##_get_ring_ptr(unsigned char *buf, \362RING_IDX idx, \363RING_IDX ring_size) \364{ \365return buf + name##_mask(idx, ring_size); \366} \367\368static inline void name##_read_packet(void *opaque, \369const unsigned char *buf, \370size_t size, \371RING_IDX masked_prod, \372RING_IDX *masked_cons, \373RING_IDX ring_size) \374{ \375if (*masked_cons < masked_prod || \376size <= ring_size - *masked_cons) { \377memcpy(opaque, buf + *masked_cons, size); \378} else { \379memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons); \380memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf, \381size - (ring_size - *masked_cons)); \382} \383*masked_cons = name##_mask(*masked_cons + size, ring_size); \384} \385\386static inline void name##_write_packet(unsigned char *buf, \387const void *opaque, \388size_t size, \389RING_IDX *masked_prod, \390RING_IDX masked_cons, \391RING_IDX ring_size) \392{ \393if (*masked_prod < masked_cons || \394size <= ring_size - *masked_prod) { \395memcpy(buf + *masked_prod, opaque, size); \396} else { \397memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod); \398memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod), \399size - (ring_size - *masked_prod)); \400} \401*masked_prod = name##_mask(*masked_prod + size, ring_size); \402} \403\404static inline RING_IDX name##_queued(RING_IDX prod, \405RING_IDX cons, \406RING_IDX ring_size) \407{ \408RING_IDX size; \409\410if (prod == cons) \411return 0; \412\413prod = name##_mask(prod, ring_size); \414cons = name##_mask(cons, ring_size); \415\416if (prod == cons) \417return ring_size; \418\419if (prod > cons) \420size = prod - cons; \421else \422size = ring_size - (cons - prod); \423return size; \424} \425\426struct name##_data { \427unsigned char *in; /* half of the allocation */ \428unsigned char *out; /* half of the allocation */ \429}430431#define DEFINE_XEN_FLEX_RING_AND_INTF(name) \432struct name##_data_intf { \433RING_IDX in_cons, in_prod; \434\435uint8_t pad1[56]; \436\437RING_IDX out_cons, out_prod; \438\439uint8_t pad2[56]; \440\441RING_IDX ring_order; \442grant_ref_t ref[]; \443}; \444DEFINE_XEN_FLEX_RING(name)445446#endif /* __XEN_PUBLIC_IO_RING_H__ */447448449